Fixes resize needed for layout issue.

This commit is contained in:
Atul R
2019-06-12 00:45:23 +02:00
parent a351526432
commit 7a39ee0fd3
12 changed files with 76 additions and 27 deletions
@@ -3,6 +3,8 @@
#include <QWidget>
#include <QMainWindow>
#include "src/cpp/core/YogaWidget/yogawidget.h"
#include "deps/spdlog/spdlog.h"
#include <QEvent>
class NMainWindow: public QMainWindow, public YogaWidget
{
@@ -10,7 +12,25 @@ class NMainWindow: public QMainWindow, public YogaWidget
public:
SET_YOGA_WIDGET_Q_PROPERTIES
using QMainWindow::QMainWindow; //inherit all constructors of QMainWindow
void calculateLayout(){
YGDirection direction = YGNodeStyleGetDirection(this->getFlexNode());
YGNodeCalculateLayout(this->getFlexNode(),width(),height(),direction);
}
Q_OBJECT
public:
bool eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::LayoutRequest || event->type() == QEvent::ChildRemoved) {
calculateLayout();
}
return false;
}
void resizeEvent(QResizeEvent * event){
calculateLayout();
}
};
@@ -37,6 +37,7 @@ QMainWindowWrap::QMainWindowWrap(const Napi::CallbackInfo& info): Napi::ObjectWr
extrautils::throwTypeError(env, "Wrong number of arguments");
}
this->instance->setAttribute(Qt::WA_DeleteOnClose);
this->instance->installEventFilter(this->instance);
}
QMainWindowWrap::~QMainWindowWrap() {
@@ -49,6 +50,12 @@ Napi::Value QMainWindowWrap::setCentralWidget(const Napi::CallbackInfo& info){
Napi::Object widgetObject = info[0].As<Napi::Object>();
QWidgetWrap* centralWidget = Napi::ObjectWrap<QWidgetWrap>::Unwrap(widgetObject);
Napi::Object flexNodeObject = info[1].As<Napi::Object>();
FlexNodeWrap* flexNodeWrap = Napi::ObjectWrap<FlexNodeWrap>::Unwrap(flexNodeObject);
YGNodeRef node = this->instance->getFlexNode();
YGNodeInsertChild(node, flexNodeWrap->getInternalInstance(), 0);
this->instance->setCentralWidget(centralWidget->getInternalInstance());
return env.Null();
+2 -11
View File
@@ -39,13 +39,9 @@ QSize FlexLayout::sizeHint() const{
if(!this->node){
return QSize(0,0);
}
QSize size;
int width = static_cast<uint>(YGNodeLayoutGetWidth(this->node));
int height = static_cast<uint>(YGNodeLayoutGetHeight(this->node));
spdlog::info("flexlayout: sizeHint {}x{}",width, height);
size.setWidth(width);
size.setHeight(height);
return size;
return QSize(width, height);
}
void FlexLayout::addItem(QLayoutItem * item){
@@ -109,10 +105,6 @@ void FlexLayout::setGeometry(const QRect &rect)
if(!this->node){
return;
}
int availableWidth = rect.width();
int availableHeight = rect.height();
YGDirection direction = YGNodeStyleGetDirection(this->node);
YGNodeCalculateLayout(this->node,availableWidth,availableHeight,direction);
uint count = YGNodeGetChildCount(this->node);
@@ -123,14 +115,13 @@ void FlexLayout::setGeometry(const QRect &rect)
int left = static_cast<uint>(YGNodeLayoutGetLeft(childNode));
int top = static_cast<uint>(YGNodeLayoutGetTop(childNode));
QRect childRect(left, top,width, height);
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);
// spdlog::info("flexlayout setGeometry: {}, rect: w:{}, h:{}, l:{}, t:{}",childWidget->metaObject()->className(),width,height,left,top);
}else {
childLayoutItem->setGeometry(childRect);
}
+9 -2
View File
@@ -1,7 +1,10 @@
import addon from "../../core/addon";
import { NodeWidget } from "../../QtGui/QWidget";
import { FlexNode } from "../../core/FlexLayout/FlexNode";
export class QMainWindow extends NodeWidget {
native: any;
protected centralWidget?: NodeWidget;
protected centralWidgetFlexNode?: FlexNode;
constructor(parent?: NodeWidget) {
super();
if (parent) {
@@ -12,8 +15,12 @@ export class QMainWindow extends NodeWidget {
}
}
setCentralWidget = (widget: NodeWidget) => {
this.native.setCentralWidget(widget.native);
this.children.add(widget);
this.centralWidgetFlexNode = widget.getFlexNode();
this.centralWidget = widget;
this.native.setCentralWidget(
this.centralWidget.native,
this.centralWidgetFlexNode.native
);
};
setFixedSize = (width: number, height: number) => {
this.native.setFixedSize(width, height);