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
174 lines (155 loc) · 5.26 KB
/
flexlayout.cpp
File metadata and controls
174 lines (155 loc) · 5.26 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
#include "flexlayout.h"
#include <QDebug>
#include <QWidget>
#include "spdlog/spdlog.h"
#include "src/cpp/core/YogaWidget/yogawidget.h"
FlexLayout::NodeContext *FlexLayout::getNodeContext(YGNodeRef node)
{
if(!node){
return nullptr;
}
void* childContext = YGNodeGetContext(node);
NodeContext *ctx = static_cast<NodeContext*>(childContext); //because we are managing this at all times
return ctx;
}
FlexLayout::FlexLayout(QWidget *parentWidget, YGNodeRef parentNode): QLayout(parentWidget)
{
// spdlog::set_level(spdlog::level::off);
this->node = parentNode;
}
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);
NodeContext* ctx = getNodeContext(oldChild);
if(ctx){
delete ctx->item;
}
}
YGNodeRemoveAllChildren(this->node);
}
QSize FlexLayout::sizeHint() const{
if(!this->node){
return QSize(0,0);
}
int width = static_cast<uint>(YGNodeLayoutGetWidth(this->node));
int height = static_cast<uint>(YGNodeLayoutGetHeight(this->node));
return QSize(width, height);
}
void FlexLayout::addItem(QLayoutItem * item){
// Noop: We already have addWidget doing all the hard work.
}
QLayoutItem *FlexLayout::itemAt(int index) const
{
if(!this->node){
return nullptr;
}
// spdlog::info("flexlayout: itemAt {}",index);
YGNodeRef childNode = YGNodeGetChild(this->node, static_cast<uint>(index));
NodeContext *ctx = getNodeContext(childNode);
if(!ctx){
// spdlog::info("flexlayout: itemAt null context {}",index);
return nullptr;
}
return ctx->item;
}
QLayoutItem *FlexLayout::takeAt(int index)
{
YGNodeRef childNode = YGNodeGetChild(this->node, static_cast<uint>(index));
NodeContext *ctx = getNodeContext(childNode);
QLayoutItem* childLayoutItem = ctx->item;
YGNodeRemoveChild(this->node, childNode);
spdlog::info("flexlayout: takeAt ",index);
delete ctx;
return childLayoutItem;
}
int FlexLayout::count() const
{
if(!this->node){
return 0;
}
float childCount = YGNodeGetChildCount(this->node);
spdlog::info("flexlayout: count {}",childCount);
return static_cast<uint>(childCount);
}
void FlexLayout::addWidget(QWidget* childWidget, YGNodeRef childNode)
{
if(!this->node){
spdlog::warn("Flex layout's parent yoga node not set yet. Set it using setFlexNode. Child widget will not be added to Flex Layout");
return;
}
// spdlog::info("flexlayout: addWidget Object: {}",childWidget->metaObject()->className());
uint count = YGNodeGetChildCount(this->node);
YGNodeInsertChild(this->node,childNode, count);
QLayoutItem* layoutItem = new QWidgetItem(childWidget);
NodeContext* childContext = new NodeContext(layoutItem);
YGNodeSetContext(childNode, static_cast<void *>(childContext));
QLayout::addWidget(childWidget);
}
void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode)
{
if(!this->node){
spdlog::warn("Flex layout's parent yoga node not set yet. Set it using setFlexNode. childwidget cant be removed");
return;
}
NodeContext* ctx = getNodeContext(childNode);
if(ctx){
delete ctx->item;
}
YGNodeRemoveChild(this->node, childNode);
QLayout::removeWidget(childWidget);
}
void FlexLayout::insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNode, YGNodeRef childNode)
{
if(!this->node){
spdlog::warn("Flex layout's parent yoga node not set yet. Set it using setFlexNode. 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);
QLayoutItem* layoutItem = new QWidgetItem(childWidget);
NodeContext* childContext = new NodeContext(layoutItem);
YGNodeSetContext(childNode, static_cast<void *>(childContext));
QLayout::addWidget(childWidget);
}
void FlexLayout::setGeometry(const QRect &rect)
{
if(!this->node){
return;
}
uint count = YGNodeGetChildCount(this->node);
for (uint i = 0; i < count; ++i) {
YGNode *childNode = YGNodeGetChild(this->node, i);
int width = static_cast<uint>(YGNodeLayoutGetWidth(childNode));
int height = static_cast<uint>(YGNodeLayoutGetHeight(childNode));
int left = static_cast<uint>(YGNodeLayoutGetLeft(childNode));
int top = static_cast<uint>(YGNodeLayoutGetTop(childNode));
QRect childRect(left, top, width, height);
NodeContext *ctx = getNodeContext(childNode);
if(ctx){
QLayoutItem* childLayoutItem = ctx->item;
QWidget* childWidget = childLayoutItem->widget();
if(childWidget){
childWidget->setGeometry(childRect);
}else {
childLayoutItem->setGeometry(childRect);
}
}
}
QLayout::setGeometry(rect);
}
void FlexLayout::setFlexNode(YGNodeRef parentNode)
{
this->node = parentNode;
}