Adds supprt for getting flex nodes from c++ side

This commit is contained in:
Atul R 2019-06-06 08:20:47 +02:00
parent 34ba68be5a
commit 2aeda4a556
14 changed files with 188 additions and 4 deletions

View File

@ -17,6 +17,8 @@
# wrapped cpps
"../src/cpp/QtGui/QApplication/qapplication_wrap.cpp",
"../src/cpp/QtGui/QWidget/qwidget_wrap.cpp",
'../src/cpp/core/FlexLayout/flexnode_wrap.cpp',
'../src/cpp/core/FlexLayout/flexlayout_wrap.cpp',
"../src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.cpp",
"../src/cpp/QtWidgets/QLabel/nlabel.cpp",
"../src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp",

View File

@ -7,6 +7,7 @@ import { QCheckBox } from "./src/lib/QtWidgets/QCheckBox";
import { QProgressBar } from "./src/lib/QtWidgets/QProgressBar";
import { QRadioButton } from "./src/lib/QtWidgets/QRadioButton";
import { QLineEdit } from "./src/lib/QtWidgets/QLineEdit";
import { FlexLayout } from "./src/lib/QtWidgets/FlexLayout";
const win = new QMainWindow();
const view = new QWidget();
@ -19,6 +20,10 @@ label.setText("Testing1234");
const label2 = new QLabel();
label2.setText("Hello12321");
label2.setStyleSheet("background-color:blue; color:white;");
const label2FlexNode = label2.getFlexNode();
label2FlexNode.debugValue();
const flayout = new FlexLayout();
flayout.addWidget(label2, label2FlexNode);
const button1 = new QPushButton();
button1.setText("Yolo");

View File

@ -1,6 +1,7 @@
#include "qlabel_wrap.h"
#include "src/cpp/QtGui/QWidget/qwidget_wrap.h"
#include "src/cpp/Extras/Utils/utils.h"
#include "src/cpp/core/FlexLayout/flexnode_wrap.h"
#include <QWidget>
@ -12,6 +13,7 @@ Napi::Object QLabelWrap::init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("setWordWrap", &QLabelWrap::setWordWrap),
InstanceMethod("setText", &QLabelWrap::setText),
InstanceMethod("getFlexNode", &QLabelWrap::getFlexNode),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLabelWrap)
});
constructor = Napi::Persistent(func);
@ -67,3 +69,12 @@ Napi::Value QLabelWrap::setText(const Napi::CallbackInfo& info) {
return env.Null();
}
Napi::Value QLabelWrap::getFlexNode(const Napi::CallbackInfo& info) {
Napi::EscapableHandleScope scope(info.Env());
Napi::Value arg = info[0];
Napi::Object flexNodeObject = FlexNodeWrap::constructor.New({ arg });
FlexNodeWrap* flexNodeWrap = FlexNodeWrap::Unwrap(flexNodeObject);
flexNodeWrap->instance = this->instance->getFlexNode();
return scope.Escape(napi_value(flexNodeObject)).ToObject();
}

View File

@ -17,6 +17,7 @@ class QLabelWrap : public Napi::ObjectWrap<QLabelWrap>{
//wrapped methods
Napi::Value setWordWrap(const Napi::CallbackInfo& info);
Napi::Value setText(const Napi::CallbackInfo& info);
Napi::Value getFlexNode(const Napi::CallbackInfo& info);
QWIDGET_WRAPPED_METHODS_DECLARATION
};

View File

@ -85,7 +85,7 @@ int FlexLayout::count() const
void FlexLayout::addWidget(QWidget* childWidget, YGNodeRef childNode)
{
if(!this->node){
qDebug()<<"Flex layout's parent yoga node not set yet. Child widget will not be added to Flex Layout";
qDebug()<<"Flex layout's parent yoga node not set yet. Set it using setFlexNode. Child widget will not be added to Flex Layout";
return;
}
uint count = YGNodeGetChildCount(this->node);
@ -129,7 +129,7 @@ void FlexLayout::setGeometry(const QRect &rect)
QLayout::setGeometry(rect);
}
void FlexLayout::setYogaNode(YGNodeRef parentNode)
void FlexLayout::setFlexNode(YGNodeRef parentNode)
{
this->node = parentNode;
}

View File

@ -27,7 +27,7 @@ public:
int count() const override;
void addWidget(QWidget* childWidget, YGNodeRef childNode);
void setGeometry(const QRect &rect) override;
void setYogaNode(YGNodeRef parentNode);
void setFlexNode(YGNodeRef parentNode);
};
#endif // FLEXLAYOUT_H

View File

@ -0,0 +1,60 @@
#include "flexlayout_wrap.h"
#include "flexnode_wrap.h"
#include "src/cpp/QtGui/QWidget/qwidget_wrap.h"
#include "src/cpp/Extras/Utils/utils.h"
#include <QDebug>
Napi::FunctionReference FlexLayoutWrap::constructor;
Napi::Object FlexLayoutWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "FlexLayout";
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("addWidget", &FlexLayoutWrap::addWidget),
// InstanceMethod("setFlexNode", &FlexLayoutWrap::setFlexNode)
});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
FlexLayout* FlexLayoutWrap::getInternalInstance() {
return this->instance;
}
FlexLayoutWrap::FlexLayoutWrap(const Napi::CallbackInfo& info): Napi::ObjectWrap<FlexLayoutWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if(info.Length() == 2) {
Napi::Object object_parent = info[0].As<Napi::Object>();
QWidgetWrap* w_parent = Napi::ObjectWrap<QWidgetWrap>::Unwrap(object_parent);
this->instance = new FlexLayout(w_parent->getInternalInstance());
} else if(info.Length() == 1) {
Napi::Object object_parent = info[0].As<Napi::Object>();
QWidgetWrap* w_parent = Napi::ObjectWrap<QWidgetWrap>::Unwrap(object_parent);
this->instance = new FlexLayout(w_parent->getInternalInstance()); //this sets the parent to current widget
}else if (info.Length() == 0){
this->instance = new FlexLayout();
}else {
extrautils::throwTypeError(env, "Wrong number of arguments");
}
}
FlexLayoutWrap::~FlexLayoutWrap() {
delete this->instance;
}
Napi::Value FlexLayoutWrap::addWidget(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object qwidgetObject = info[0].As<Napi::Object>();
Napi::Object childFlexNodeObject = info[1].As<Napi::Object>();
QWidgetWrap* widget = Napi::ObjectWrap<QWidgetWrap>::Unwrap(qwidgetObject);
FlexNodeWrap* childFlexNodeWrap = Napi::ObjectWrap<FlexNodeWrap>::Unwrap(childFlexNodeObject);
this->instance->addWidget(widget->getInternalInstance(),childFlexNodeWrap->getInternalInstance());
return env.Null();
}

View File

@ -0,0 +1,23 @@
#ifndef FLEXLAYOUT_WRAP_H
#define FLEXLAYOUT_WRAP_H
#include <napi.h>
#include "flexlayout.h"
class FlexLayoutWrap : public Napi::ObjectWrap<FlexLayoutWrap>{
private:
FlexLayout* instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
FlexLayoutWrap(const Napi::CallbackInfo& info);
~FlexLayoutWrap();
FlexLayout* getInternalInstance();
//class constructor
static Napi::FunctionReference constructor;
//wrapped methods
Napi::Value addWidget(const Napi::CallbackInfo& info);
// Napi::Value setFlexNode(const Napi::CallbackInfo& info);
};
#endif //FLEXLAYOUT_WRAP_H

View File

@ -0,0 +1,31 @@
#include "flexnode_wrap.h"
#include <QDebug>
Napi::FunctionReference FlexNodeWrap::constructor;
void FlexNodeWrap::init(Napi::Env env) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "FlexNode";
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("debugValue", &FlexNodeWrap::printValue),
});
constructor = Napi::Persistent(func);
}
YGNodeRef FlexNodeWrap::getInternalInstance() {
return this->instance;
}
FlexNodeWrap::FlexNodeWrap(const Napi::CallbackInfo& info): Napi::ObjectWrap<FlexNodeWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
}
Napi::Value FlexNodeWrap::debugValue(const Napi::CallbackInfo& info) {
uint64_t addr = reinterpret_cast<long>(this->getInternalInstance());
return info.Env().Null();
}
FlexNodeWrap::~FlexNodeWrap() {
delete this->instance;
}

View File

@ -0,0 +1,19 @@
#ifndef FLEXNODE_WRAP_H
#define FLEXNODE_WRAP_H
#include <napi.h>
#include "deps/yoga/YGNode.h"
//ABSTRACT CLASS
class FlexNodeWrap : public Napi::ObjectWrap<FlexNodeWrap>{
public:
YGNodeRef instance;
static void init(Napi::Env env);
FlexNodeWrap(const Napi::CallbackInfo& info);
~FlexNodeWrap();
YGNodeRef getInternalInstance();
//class constructor
static Napi::FunctionReference constructor;
Napi::Value debugValue(const Napi::CallbackInfo& info);
};
#endif

View File

@ -9,11 +9,14 @@
#include "src/cpp/QtWidgets/QProgressBar/qprogressbar_wrap.h"
#include "src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.h"
#include "src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h"
#include "src/cpp/core/FlexLayout/flexnode_wrap.h"
#include "src/cpp/core/FlexLayout/flexlayout_wrap.h"
#include <napi.h>
//private : will not be accessibe in js
// These cant be instantiated in JS Side
void InitPrivateHelpers(Napi::Env env){
QLayoutWrap::init(env); //Abstact class wrapper for pointing to any layout
FlexNodeWrap::init(env); //Abstact class wrapper for pointing to flex/yoga node
}
Napi::Object Main(Napi::Env env, Napi::Object exports) {
@ -21,6 +24,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QApplicationWrap::init(env, exports);
QWidgetWrap::init(env, exports);
QGridLayoutWrap::init(env, exports);
FlexLayoutWrap::init(env, exports);
QMainWindowWrap::init(env,exports);
QPushButtonWrap::init(env, exports);
QCheckBoxWrap::init(env, exports);

View File

@ -0,0 +1,11 @@
import addon from "../../core/addon";
import { Component } from "../../core/Component";
import { QWidget } from "../../QtGui/QWidget";
import { FlexNode } from "../../core/FlexLayout/FlexNode";
export class FlexLayout extends Component {
native = new addon.FlexLayout();
addWidget(childWidget: QWidget, childFlexNode: FlexNode) {
this.native.addWidget(childWidget.native, childFlexNode.native);
}
}

View File

@ -1,6 +1,7 @@
import addon from "../../core/addon";
import { NodeWidget } from "../../QtGui/QWidget";
import { QLayout } from "../QLayout";
import { FlexNode } from "../../core/FlexLayout/FlexNode";
export class QLabel extends NodeWidget {
native: any;
@ -20,4 +21,8 @@ export class QLabel extends NodeWidget {
setText(text: string) {
this.native.setText(text);
}
getFlexNode(): FlexNode {
const nativeFlexNode = this.native.getFlexNode();
return new FlexNode(nativeFlexNode);
}
}

View File

@ -0,0 +1,12 @@
import { Component } from "../../../core/Component";
export class FlexNode extends Component {
native: any;
constructor(nativeNode: any) {
super();
this.native = nativeNode;
}
debugValue() {
this.native.debugValue();
}
}