remove widget from layout

This commit is contained in:
Atul R 2019-07-09 21:22:09 +02:00
parent edce5ad84b
commit 687fcc1052
5 changed files with 36 additions and 1 deletions

View File

@ -100,6 +100,21 @@ void FlexLayout::addWidget(QWidget* childWidget, YGNodeRef childNode)
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::setGeometry(const QRect &rect)
{
if(!this->node){

View File

@ -40,6 +40,7 @@ public:
QLayoutItem *takeAt(int index) override;
int count() const override;
void addWidget(QWidget* childWidget, YGNodeRef childNode);
void removeWidget(QWidget* childWidget, YGNodeRef childNode);
void setGeometry(const QRect &rect) override;
void setFlexNode(YGNodeRef parentNode);
static NodeContext* getNodeContext(YGNodeRef node);

View File

@ -1,7 +1,6 @@
#include "flexlayout_wrap.h"
#include "src/cpp/QtGui/QWidget/qwidget_wrap.h"
#include "src/cpp/Extras/Utils/nutils.h"
#include <QDebug>
Napi::FunctionReference FlexLayoutWrap::constructor;
@ -10,6 +9,7 @@ Napi::Object FlexLayoutWrap::init(Napi::Env env, Napi::Object exports) {
char CLASSNAME[] = "FlexLayout";
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("addWidget", &FlexLayoutWrap::addWidget),
InstanceMethod("removeWidget", &FlexLayoutWrap::removeWidget),
InstanceMethod("setFlexNode", &FlexLayoutWrap::setFlexNode),
});
constructor = Napi::Persistent(func);
@ -57,6 +57,19 @@ Napi::Value FlexLayoutWrap::addWidget(const Napi::CallbackInfo& info) {
return env.Null();
}
Napi::Value FlexLayoutWrap::removeWidget(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object qwidgetObject = info[0].As<Napi::Object>();
Napi::External<YGNode> childFlexNodeObject = info[1].As<Napi::External<YGNode>>();
QWidgetWrap* widget = Napi::ObjectWrap<QWidgetWrap>::Unwrap(qwidgetObject);
YGNodeRef childNodeRef = childFlexNodeObject.Data();
this->instance->removeWidget(widget->getInternalInstance(),childNodeRef);
return env.Null();
}
Napi::Value FlexLayoutWrap::setFlexNode(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

View File

@ -17,6 +17,7 @@ class FlexLayoutWrap : public Napi::ObjectWrap<FlexLayoutWrap>{
static Napi::FunctionReference constructor;
//wrapped methods
Napi::Value addWidget(const Napi::CallbackInfo& info);
Napi::Value removeWidget(const Napi::CallbackInfo& info);
Napi::Value setFlexNode(const Napi::CallbackInfo& info);
};

View File

@ -13,6 +13,11 @@ export class FlexLayout extends NodeLayout {
this.children.add(childWidget);
this.native.addWidget(childWidget.native, childYogaNode);
};
removeWidget = (childWidget: NodeWidget, childFlexNode?: FlexNode) => {
const childYogaNode = childFlexNode || childWidget.getFlexNode();
this.native.removeWidget(childWidget.native, childYogaNode);
this.children.delete(childWidget);
};
setFlexNode = (flexNode: FlexNode) => {
this.flexNode = flexNode;
this.native.setFlexNode(flexNode);