Skip to content

Commit bf1aecf

Browse files
committed
fixes all flex layout issues
1 parent b537f76 commit bf1aecf

5 files changed

Lines changed: 35 additions & 34 deletions

File tree

src/cpp/QtWidgets/QMainWindow/nmainwindow.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@ class NMainWindow: public QMainWindow, public NodeWidget
99
NODEWIDGET_IMPLEMENTATIONS(QMainWindow)
1010
public:
1111
using QMainWindow::QMainWindow; //inherit all constructors of QMainWindow
12-
private:
13-
void calculateLayout(){
14-
YGDirection direction = YGNodeStyleGetDirection(this->getFlexNode());
15-
YGNodeCalculateLayout(this->getFlexNode(),width(),height(),direction);
16-
}
17-
bool eventFilter(QObject *object, QEvent *event) { // This will be installed on mainwidgetwrap
18-
switch(event->type()) {
19-
case QEvent::LayoutRequest:
20-
case QEvent::ChildRemoved: {
21-
calculateLayout(); break;
22-
}
23-
default: ; // do nothing
24-
}
25-
return QMainWindow::eventFilter(object, event);
26-
}
27-
void resizeEvent(QResizeEvent * event){
28-
calculateLayout();
29-
QMainWindow::resizeEvent(event);
30-
}
3112
};
3213

3314

src/cpp/QtWidgets/QWidget/nwidget.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ class NWidget: public QWidget, public NodeWidget
1111
public:
1212
using QWidget::QWidget;
1313
// https://doc.qt.io/qt-5/stylesheet-reference.html
14-
void paintEvent(QPaintEvent *e)
15-
{
16-
QStyleOption opt;
17-
opt.init(this);
18-
QPainter p(this);
19-
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
20-
QWidget::paintEvent(e);
21-
}
14+
// void paintEvent(QPaintEvent *e)
15+
// {
16+
// QStyleOption opt;
17+
// opt.init(this);
18+
// QPainter p(this);
19+
// style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
20+
// QWidget::paintEvent(e);
21+
// }
2222
};
2323

2424

src/cpp/core/FlexLayout/flexlayout.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "spdlog/spdlog.h"
55
#include "src/cpp/core/YogaWidget/yogawidget.h"
66

7-
FlexLayout::NodeContext *FlexLayout::getNodeContext(YGNodeRef node)
7+
FlexLayout::NodeContext* FlexLayout::getNodeContext(YGNodeRef node)
88
{
99
if(!node){
1010
return nullptr;
@@ -16,7 +16,6 @@ FlexLayout::NodeContext *FlexLayout::getNodeContext(YGNodeRef node)
1616

1717
FlexLayout::FlexLayout(QWidget *parentWidget, YGNodeRef parentNode): QLayout(parentWidget)
1818
{
19-
// spdlog::set_level(spdlog::level::off);
2019
this->node = parentNode;
2120
}
2221

@@ -58,7 +57,6 @@ QLayoutItem *FlexLayout::itemAt(int index) const
5857
YGNodeRef childNode = YGNodeGetChild(this->node, static_cast<uint>(index));
5958
NodeContext *ctx = getNodeContext(childNode);
6059
if(!ctx){
61-
// spdlog::info("flexlayout: itemAt null context {}",index);
6260
return nullptr;
6361
}
6462
return ctx->item;
@@ -91,14 +89,13 @@ void FlexLayout::addWidget(QWidget* childWidget, YGNodeRef childNode)
9189
spdlog::warn("Flex layout's parent yoga node not set yet. Set it using setFlexNode. Child widget will not be added to Flex Layout");
9290
return;
9391
}
94-
// spdlog::info("flexlayout: addWidget Object: {}",childWidget->metaObject()->className());
95-
9692
uint count = YGNodeGetChildCount(this->node);
9793
YGNodeInsertChild(this->node,childNode, count);
9894
QLayoutItem* layoutItem = new QWidgetItem(childWidget);
9995
NodeContext* childContext = new NodeContext(layoutItem);
10096
YGNodeSetContext(childNode, static_cast<void *>(childContext));
10197
QLayout::addWidget(childWidget);
98+
this->invalidate();
10299
}
103100

104101
void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode)
@@ -114,6 +111,7 @@ void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode)
114111
}
115112
YGNodeRemoveChild(this->node, childNode);
116113
QLayout::removeWidget(childWidget);
114+
this->invalidate();
117115
}
118116

119117
void FlexLayout::insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNode, YGNodeRef childNode)
@@ -135,14 +133,30 @@ void FlexLayout::insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNo
135133
NodeContext* childContext = new NodeContext(layoutItem);
136134
YGNodeSetContext(childNode, static_cast<void *>(childContext));
137135
QLayout::addWidget(childWidget);
136+
this->invalidate();
138137
}
139138

139+
140+
YGNodeRef FlexLayout::getRootNode(YGNodeRef node){
141+
YGNodeRef parent = node->getOwner();
142+
if(!parent){
143+
return node;
144+
}else {
145+
return getRootNode(parent);
146+
}
147+
}
148+
149+
140150
void FlexLayout::setGeometry(const QRect &rect)
141151
{
142152
if(!this->node){
143153
return;
144154
}
145-
155+
YGNodeRef rootNode = getRootNode(this->node);
156+
QWidget* parentWidget = this->parentWidget();
157+
QWidget* window = parentWidget->window();
158+
YGDirection direction = YGNodeStyleGetDirection(rootNode);
159+
YGNodeCalculateLayout(rootNode,window->width(),window->height(),direction);
146160
uint count = YGNodeGetChildCount(this->node);
147161

148162
for (uint i = 0; i < count; ++i) {

src/cpp/core/FlexLayout/flexlayout.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class FlexLayout: public QLayout
2424
{
2525
private:
2626
YGNodeRef node;
27+
YGNodeRef getRootNode(YGNodeRef node);
2728
public:
2829
struct NodeContext
2930
{

src/demo.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ button.addEventListener("clicked", () => {
4949
const clipboard = QApplication.clipboard();
5050
console.log("clipboard: ", clipboard.text(QClipboardMode.Clipboard));
5151
clipboard.setText("yooooo", QClipboardMode.Clipboard);
52+
if (rootView.layout) {
53+
(rootView.layout as FlexLayout).removeWidget(dial);
54+
rootView.layout.invalidate();
55+
// rootView.update();
56+
}
5257
});
5358

5459
const nodeguiLogo = new QIcon(
@@ -111,7 +116,7 @@ win.setStyleSheet(`
111116
win.setWindowIcon(nodeguiLogo);
112117
win.setWindowTitle("NodeGUI Demo");
113118

114-
win.resize(400, 400);
119+
win.resize(400, 700);
115120
win.show();
116121
win.setWindowState(WindowState.WindowActive);
117122

0 commit comments

Comments
 (0)