forked from nodegui/nodegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflexlayout.cpp
More file actions
220 lines (191 loc) · 6.77 KB
/
flexlayout.cpp
File metadata and controls
220 lines (191 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "core/FlexLayout/flexlayout.hpp"
#include <QDebug>
#include <QWidget>
#include "core/FlexLayout/flexitem.h"
#include "core/FlexLayout/flexutils.h"
#include "core/YogaWidget/yogawidget.h"
FlexLayout::FlexLayout(QWidget* parentWidget, YGNodeRef parentNode)
: QLayout(parentWidget) {
this->node = parentNode;
// Throttle the setGeometry calls that may happen when dealing with huge
// lists.
this->throttleTimer.setTimerType(Qt::PreciseTimer);
this->throttleTimer.setSingleShot(true);
QObject::connect(&this->throttleTimer, &QTimer::timeout, this,
&FlexLayout::performLayout);
}
FlexLayout::~FlexLayout() {
if (!this->node) {
return;
}
const uint32_t childCount = YGNodeGetChildCount(this->node);
for (uint32_t i = 0; i < childCount; i++) {
const YGNodeRef oldChild = YGNodeGetChild(this->node, i);
FlexNodeContext* childCtx = flexutils::getFlexNodeContext(oldChild);
if (childCtx->layoutItem()) {
delete childCtx->layoutItem();
childCtx->setLayoutItem(nullptr);
}
}
YGNodeRemoveAllChildren(this->node);
}
void FlexLayout::addItem(QLayoutItem* item) {
// Noop: We already have addWidget doing all the hard work.
return;
}
void FlexLayout::addWidget(QWidget* childWidget, YGNodeRef childNode) {
if (!this->node) {
qWarning() << "Flex layout's parent yoga node not set yet. Set it using "
"setFlexNode. \n"
<< "Child widget will not be added to Flex Layout";
return;
}
uint count = YGNodeGetChildCount(this->node);
YGNodeInsertChild(this->node, childNode, count);
FlexNodeContext* childCtx = flexutils::getFlexNodeContext(childNode);
auto layoutitem = new QWidgetItem(childWidget);
childCtx->setLayoutItem(layoutitem);
QLayout::addWidget(childWidget);
this->invalidate();
}
void FlexLayout::insertChildBefore(QWidget* childWidget,
YGNodeRef beforeChildNode,
YGNodeRef childNode) {
if (!this->node) {
qWarning() << "Flex layout's parent yoga node not set yet. Set it using "
"setFlexNode. \n"
<< "childwidget cant be inserted";
return;
}
uint count = YGNodeGetChildCount(this->node);
uint indexToInsert = 0;
for (uint i = 0; i < count; i += 1) {
if (beforeChildNode == YGNodeGetChild(this->node, i)) {
indexToInsert = i;
break;
}
}
YGNodeInsertChild(this->node, childNode, indexToInsert);
FlexNodeContext* ctx = flexutils::getFlexNodeContext(childNode);
ctx->setLayoutItem(new QWidgetItem(childWidget));
QLayout::addWidget(childWidget);
this->invalidate();
}
QLayoutItem* FlexLayout::itemAt(int index) const {
if (!this->node ||
index >= static_cast<int>(YGNodeGetChildCount(this->node))) {
return nullptr;
}
YGNodeRef childNode = YGNodeGetChild(this->node, static_cast<uint>(index));
FlexNodeContext* childCtx = flexutils::getFlexNodeContext(childNode);
return childCtx->layoutItem();
}
QLayoutItem* FlexLayout::takeAt(int index) {
if (!this->node ||
index >= static_cast<int>(YGNodeGetChildCount(this->node))) {
return nullptr;
}
YGNodeRef childNode = YGNodeGetChild(this->node, static_cast<uint>(index));
FlexNodeContext* ctx = flexutils::getFlexNodeContext(childNode);
QLayoutItem* childLayoutItem = ctx->layoutItem();
ctx->setLayoutItem(nullptr);
YGNodeRemoveChild(this->node, childNode);
return childLayoutItem;
}
int FlexLayout::count() const {
if (!this->node) {
return 0;
}
float childCount = YGNodeGetChildCount(this->node);
return static_cast<int>(childCount);
}
void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode) {
if (!this->node) {
qWarning() << "Flex layout's parent yoga node not set yet. Set it using "
"setFlexNode. "
<< "childwidget cant be removed";
return;
}
FlexNodeContext* ctx = flexutils::getFlexNodeContext(childNode);
if (ctx->layoutItem()) {
delete ctx->layoutItem();
ctx->setLayoutItem(nullptr);
}
YGNodeRemoveChild(this->node, childNode);
QLayout::removeWidget(childWidget);
this->invalidate();
}
YGNodeRef FlexLayout::getRootNode(YGNodeRef node) const {
YGNodeRef parent = node->getOwner();
if (!parent) {
return node;
} else {
return getRootNode(parent);
}
}
bool FlexLayout::hasHeightForWidth() const { return false; }
QSize FlexLayout::sizeHint() const {
calculateLayout();
return QSize(YGNodeLayoutGetWidth(this->node),
YGNodeLayoutGetHeight(this->node));
}
QSize FlexLayout::minimumSize() const {
calculateLayout();
QSize minSize = QSize(YGNodeStyleGetMinWidth(this->node).value,
YGNodeStyleGetMinHeight(this->node).value);
return minSize;
}
void FlexLayout::setGeometry(const QRect& rect) {
this->cachedRect = rect;
if (this->throttleTimer.isActive()) {
return;
}
this->throttleTimer.start(10);
// This will call performLayout and throttle requests between 10ms.
}
void FlexLayout::performLayout() {
if (!this->node) {
return;
}
QRect rect = this->cachedRect;
if (flexutils::isFlexNodeSizeControlled(this->node)) {
YGNodeMarkDirtyAndPropogateToDescendants(this->node);
YGNodeStyleSetHeight(this->node, rect.height());
YGNodeStyleSetWidth(this->node, rect.width());
}
calculateLayout();
QRect calculatedRect = flexutils::getFlexNodeGeometry(this->node);
// Set our own geometry to calculated size
QLayout::setGeometry(calculatedRect);
// Iterate over children and set their geometry
uint count = YGNodeGetChildCount(this->node);
for (uint i = 0; i < count; ++i) {
YGNode* childNode = YGNodeGetChild(this->node, i);
QRect childRect = flexutils::getFlexNodeGeometry(childNode);
FlexNodeContext* ctx = flexutils::getFlexNodeContext(childNode);
QLayoutItem* childItem = ctx->layoutItem();
childItem->setGeometry(childRect);
}
}
void FlexLayout::setFlexNode(YGNodeRef parentNode) { this->node = parentNode; }
void FlexLayout::calculateLayout() const {
if (YGNodeIsDirty(this->node)) {
YGNodeRef rootNode = getRootNode(this->node);
YGDirection rootDirection = YGNodeStyleGetDirection(rootNode);
YGNodeCalculateLayout(rootNode, YGUndefined, YGUndefined, rootDirection);
}
}
void FlexLayout::restoreNodeMinStyle(YGValue& previousMinWidth,
YGValue& previousMinHeight) {
if (previousMinHeight.unit == YGUnitPercent) {
YGNodeStyleSetMinHeightPercent(this->node, previousMinHeight.value);
} else {
YGNodeStyleSetMinHeight(this->node, previousMinHeight.value);
}
if (previousMinWidth.unit == YGUnitPercent) {
YGNodeStyleSetMinWidthPercent(this->node, previousMinWidth.value);
} else {
YGNodeStyleSetMinWidth(this->node, previousMinWidth.value);
}
}
void FlexLayout::connectSignalsToEventEmitter() { QLAYOUT_SIGNALS }