From bf1aecfa8b7eb80c7c68f7cf5b436e8b690215b7 Mon Sep 17 00:00:00 2001 From: Atul R Date: Sat, 7 Sep 2019 08:12:41 +0200 Subject: [PATCH] fixes all flex layout issues --- src/cpp/QtWidgets/QMainWindow/nmainwindow.h | 19 --------------- src/cpp/QtWidgets/QWidget/nwidget.h | 16 ++++++------- src/cpp/core/FlexLayout/flexlayout.cpp | 26 ++++++++++++++++----- src/cpp/core/FlexLayout/flexlayout.h | 1 + src/demo.ts | 7 +++++- 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/cpp/QtWidgets/QMainWindow/nmainwindow.h b/src/cpp/QtWidgets/QMainWindow/nmainwindow.h index dbcb5b947..0d62b5500 100644 --- a/src/cpp/QtWidgets/QMainWindow/nmainwindow.h +++ b/src/cpp/QtWidgets/QMainWindow/nmainwindow.h @@ -9,25 +9,6 @@ class NMainWindow: public QMainWindow, public NodeWidget NODEWIDGET_IMPLEMENTATIONS(QMainWindow) public: using QMainWindow::QMainWindow; //inherit all constructors of QMainWindow -private: - void calculateLayout(){ - YGDirection direction = YGNodeStyleGetDirection(this->getFlexNode()); - YGNodeCalculateLayout(this->getFlexNode(),width(),height(),direction); - } - bool eventFilter(QObject *object, QEvent *event) { // This will be installed on mainwidgetwrap - switch(event->type()) { - case QEvent::LayoutRequest: - case QEvent::ChildRemoved: { - calculateLayout(); break; - } - default: ; // do nothing - } - return QMainWindow::eventFilter(object, event); - } - void resizeEvent(QResizeEvent * event){ - calculateLayout(); - QMainWindow::resizeEvent(event); - } }; diff --git a/src/cpp/QtWidgets/QWidget/nwidget.h b/src/cpp/QtWidgets/QWidget/nwidget.h index e1d29cf7b..a538868af 100644 --- a/src/cpp/QtWidgets/QWidget/nwidget.h +++ b/src/cpp/QtWidgets/QWidget/nwidget.h @@ -11,14 +11,14 @@ class NWidget: public QWidget, public NodeWidget public: using QWidget::QWidget; // https://doc.qt.io/qt-5/stylesheet-reference.html - void paintEvent(QPaintEvent *e) - { - QStyleOption opt; - opt.init(this); - QPainter p(this); - style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); - QWidget::paintEvent(e); - } + // void paintEvent(QPaintEvent *e) + // { + // QStyleOption opt; + // opt.init(this); + // QPainter p(this); + // style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); + // QWidget::paintEvent(e); + // } }; diff --git a/src/cpp/core/FlexLayout/flexlayout.cpp b/src/cpp/core/FlexLayout/flexlayout.cpp index ad87c0ded..e2fd8363e 100644 --- a/src/cpp/core/FlexLayout/flexlayout.cpp +++ b/src/cpp/core/FlexLayout/flexlayout.cpp @@ -4,7 +4,7 @@ #include "spdlog/spdlog.h" #include "src/cpp/core/YogaWidget/yogawidget.h" -FlexLayout::NodeContext *FlexLayout::getNodeContext(YGNodeRef node) +FlexLayout::NodeContext* FlexLayout::getNodeContext(YGNodeRef node) { if(!node){ return nullptr; @@ -16,7 +16,6 @@ FlexLayout::NodeContext *FlexLayout::getNodeContext(YGNodeRef node) FlexLayout::FlexLayout(QWidget *parentWidget, YGNodeRef parentNode): QLayout(parentWidget) { - // spdlog::set_level(spdlog::level::off); this->node = parentNode; } @@ -58,7 +57,6 @@ QLayoutItem *FlexLayout::itemAt(int index) const YGNodeRef childNode = YGNodeGetChild(this->node, static_cast(index)); NodeContext *ctx = getNodeContext(childNode); if(!ctx){ - // spdlog::info("flexlayout: itemAt null context {}",index); return nullptr; } return ctx->item; @@ -91,14 +89,13 @@ void FlexLayout::addWidget(QWidget* childWidget, YGNodeRef childNode) 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(childContext)); QLayout::addWidget(childWidget); + this->invalidate(); } void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode) @@ -114,6 +111,7 @@ void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode) } YGNodeRemoveChild(this->node, childNode); QLayout::removeWidget(childWidget); + this->invalidate(); } void FlexLayout::insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNode, YGNodeRef childNode) @@ -135,14 +133,30 @@ void FlexLayout::insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNo NodeContext* childContext = new NodeContext(layoutItem); YGNodeSetContext(childNode, static_cast(childContext)); QLayout::addWidget(childWidget); + this->invalidate(); } + +YGNodeRef FlexLayout::getRootNode(YGNodeRef node){ + YGNodeRef parent = node->getOwner(); + if(!parent){ + return node; + }else { + return getRootNode(parent); + } +} + + void FlexLayout::setGeometry(const QRect &rect) { if(!this->node){ return; } - + YGNodeRef rootNode = getRootNode(this->node); + QWidget* parentWidget = this->parentWidget(); + QWidget* window = parentWidget->window(); + YGDirection direction = YGNodeStyleGetDirection(rootNode); + YGNodeCalculateLayout(rootNode,window->width(),window->height(),direction); uint count = YGNodeGetChildCount(this->node); for (uint i = 0; i < count; ++i) { diff --git a/src/cpp/core/FlexLayout/flexlayout.h b/src/cpp/core/FlexLayout/flexlayout.h index ab35ef896..bdcaf46ae 100644 --- a/src/cpp/core/FlexLayout/flexlayout.h +++ b/src/cpp/core/FlexLayout/flexlayout.h @@ -24,6 +24,7 @@ class FlexLayout: public QLayout { private: YGNodeRef node; + YGNodeRef getRootNode(YGNodeRef node); public: struct NodeContext { diff --git a/src/demo.ts b/src/demo.ts index 161527dbc..13e88495f 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -49,6 +49,11 @@ button.addEventListener("clicked", () => { const clipboard = QApplication.clipboard(); console.log("clipboard: ", clipboard.text(QClipboardMode.Clipboard)); clipboard.setText("yooooo", QClipboardMode.Clipboard); + if (rootView.layout) { + (rootView.layout as FlexLayout).removeWidget(dial); + rootView.layout.invalidate(); + // rootView.update(); + } }); const nodeguiLogo = new QIcon( @@ -111,7 +116,7 @@ win.setStyleSheet(` win.setWindowIcon(nodeguiLogo); win.setWindowTitle("NodeGUI Demo"); -win.resize(400, 400); +win.resize(400, 700); win.show(); win.setWindowState(WindowState.WindowActive);