Add QBoxLayout (#239)

* Add QBoxLayout

* remove unused imports from demo

* add layout prop to QBoxLayout
This commit is contained in:
Atul R 2019-12-04 10:54:30 +01:00 committed by GitHub
commit 8cf93ec241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 274 additions and 22 deletions

View File

@ -51,6 +51,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QRect/qrect_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QUrl/qurl_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QWidget/qwidget_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QLabel/qlabel_wrap.cpp"
@ -94,6 +95,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QMenu/nmenu.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QLayout/nlayout.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QGridLayout/ngridlayout.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QBoxLayout/nboxlayout.hpp"
)
AddCommonConfig(${CORE_WIDGETS_ADDON})

View File

@ -0,0 +1,11 @@
#pragma once
#include <QBoxLayout>
#include "core/Events/eventwidget_macro.h"
class NBoxLayout : public QBoxLayout, public EventWidget {
public:
Q_OBJECT
EVENTWIDGET_IMPLEMENTATIONS(QBoxLayout)
using QBoxLayout::QBoxLayout;
};

View File

@ -0,0 +1,34 @@
#pragma once
#include <napi.h>
#include <stdlib.h>
#include <QBoxLayout>
#include <QPointer>
#include "QtWidgets/QBoxLayout/nboxlayout.hpp"
#include "QtWidgets/QLayout/qlayout_macro.h"
class QBoxLayoutWrap : public Napi::ObjectWrap<QBoxLayoutWrap> {
private:
QPointer<NBoxLayout> instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QBoxLayoutWrap(const Napi::CallbackInfo& info);
~QBoxLayoutWrap();
NBoxLayout* getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value addLayout(const Napi::CallbackInfo& info);
Napi::Value addSpacing(const Napi::CallbackInfo& info);
Napi::Value addStretch(const Napi::CallbackInfo& info);
Napi::Value addStrut(const Napi::CallbackInfo& info);
Napi::Value addWidget(const Napi::CallbackInfo& info);
Napi::Value direction(const Napi::CallbackInfo& info);
Napi::Value insertLayout(const Napi::CallbackInfo& info);
Napi::Value removeWidget(const Napi::CallbackInfo& info);
Napi::Value setDirection(const Napi::CallbackInfo& info);
QLAYOUT_WRAPPED_METHODS_DECLARATION
};

View File

@ -0,0 +1,133 @@
#include "QtWidgets/QBoxLayout/qboxlayout_wrap.h"
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
Napi::FunctionReference QBoxLayoutWrap::constructor;
Napi::Object QBoxLayoutWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QBoxLayout";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("addLayout", &QBoxLayoutWrap::addLayout),
InstanceMethod("addSpacing", &QBoxLayoutWrap::addSpacing),
InstanceMethod("addStretch", &QBoxLayoutWrap::addStretch),
InstanceMethod("addStrut", &QBoxLayoutWrap::addStrut),
InstanceMethod("addWidget", &QBoxLayoutWrap::addWidget),
InstanceMethod("direction", &QBoxLayoutWrap::direction),
InstanceMethod("insertLayout", &QBoxLayoutWrap::insertLayout),
InstanceMethod("removeWidget", &QBoxLayoutWrap::removeWidget),
InstanceMethod("setDirection", &QBoxLayoutWrap::setDirection),
QLAYOUT_WRAPPED_METHODS_EXPORT_DEFINE(QBoxLayoutWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
NBoxLayout* QBoxLayoutWrap::getInternalInstance() { return this->instance; }
QBoxLayoutWrap::~QBoxLayoutWrap() { extrautils::safeDelete(this->instance); }
QBoxLayoutWrap::QBoxLayoutWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QBoxLayoutWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 2) {
QBoxLayout::Direction dir = static_cast<QBoxLayout::Direction>(
info[0].As<Napi::Number>().Int32Value());
Napi::Object parentObject = info[1].As<Napi::Object>();
QWidgetWrap* parentWidgetWrap =
Napi::ObjectWrap<QWidgetWrap>::Unwrap(parentObject);
this->instance =
new NBoxLayout(dir, parentWidgetWrap->getInternalInstance());
} else if (info.Length() == 1) {
QBoxLayout::Direction dir = static_cast<QBoxLayout::Direction>(
info[0].As<Napi::Number>().Int32Value());
this->instance = new NBoxLayout(dir);
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureQObject(this->getInternalInstance());
}
Napi::Value QBoxLayoutWrap::addLayout(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object qlayoutObject = info[0].As<Napi::Object>();
QLayoutWrap* layout = Napi::ObjectWrap<QLayoutWrap>::Unwrap(qlayoutObject);
int stretch = info[1].As<Napi::Number>().Int32Value();
this->instance->addLayout(layout->getInternalInstance(), stretch);
return env.Null();
}
Napi::Value QBoxLayoutWrap::addSpacing(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int size = info[0].As<Napi::Number>().Int32Value();
this->instance->addSpacing(size);
return env.Null();
}
Napi::Value QBoxLayoutWrap::addStretch(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int stretch = info[0].As<Napi::Number>().Int32Value();
this->instance->addStretch(stretch);
return env.Null();
}
Napi::Value QBoxLayoutWrap::addStrut(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int size = info[0].As<Napi::Number>().Int32Value();
this->instance->addStrut(size);
return env.Null();
}
Napi::Value QBoxLayoutWrap::addWidget(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object qwidgetObject = info[0].As<Napi::Object>();
QWidgetWrap* widget = Napi::ObjectWrap<QWidgetWrap>::Unwrap(qwidgetObject);
this->instance->addWidget(widget->getInternalInstance());
return env.Null();
}
Napi::Value QBoxLayoutWrap::direction(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Number::New(env, this->instance->direction());
}
Napi::Value QBoxLayoutWrap::insertLayout(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int index = info[0].As<Napi::Number>().Int32Value();
Napi::Object qlayoutObject = info[1].As<Napi::Object>();
QLayoutWrap* layout = Napi::ObjectWrap<QLayoutWrap>::Unwrap(qlayoutObject);
int stretch = info[2].As<Napi::Number>().Int32Value();
this->instance->insertLayout(index, layout->getInternalInstance(), stretch);
return env.Null();
}
Napi::Value QBoxLayoutWrap::removeWidget(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object qwidgetObject = info[0].As<Napi::Object>();
QWidgetWrap* widget = Napi::ObjectWrap<QWidgetWrap>::Unwrap(qwidgetObject);
this->instance->removeWidget(widget->getInternalInstance());
return env.Null();
}
Napi::Value QBoxLayoutWrap::setDirection(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QBoxLayout::Direction dir = static_cast<QBoxLayout::Direction>(
info[0].As<Napi::Number>().Int32Value());
this->instance->setDirection(dir);
return env.Null();
}

View File

@ -15,6 +15,7 @@
#include "QtGui/QMovie/qmovie_wrap.h"
#include "QtGui/QPixmap/qpixmap_wrap.h"
#include "QtWidgets/QAction/qaction_wrap.h"
#include "QtWidgets/QBoxLayout/qboxlayout_wrap.h"
#include "QtWidgets/QCheckBox/qcheckbox_wrap.h"
#include "QtWidgets/QDial/qdial_wrap.h"
#include "QtWidgets/QGridLayout/qgridlayout_wrap.h"
@ -56,6 +57,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QIconWrap::init(env, exports);
QMovieWrap::init(env, exports);
QCursorWrap::init(env, exports);
QBoxLayoutWrap::init(env, exports);
QGridLayoutWrap::init(env, exports);
FlexLayoutWrap::init(env, exports);
QMainWindowWrap::init(env, exports);

View File

@ -1,12 +1,12 @@
import { QWidget, QMainWindow, FlexLayout, QLabel } from './index';
import { QScrollArea } from './lib/QtWidgets/QScrollArea';
import { QWidget, QMainWindow, QLabel, QBoxLayout, QPushButton } from './index';
import { Direction } from './lib/QtEnums';
const win = new QMainWindow();
const center = new QWidget();
const label = new QLabel();
const scrollArea = new QScrollArea();
label.setText(`
const btn1 = new QPushButton();
const btn2 = new QPushButton();
const boxLayout = new QBoxLayout(Direction.LeftToRight);
btn1.setText(`
😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱😱
Hellloooooo
Hellloooooo
@ -26,25 +26,34 @@ label.setText(`
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
Hellloooooo
`);
btn2.setText(`
Helllo2
`);
center.setInlineStyle(`border: 3px solid blue;`);
label.setInlineStyle(`border: 2px solid green;padding: 10;font-family: "Sans serif";`);
scrollArea.setWidget(label);
center.setLayout(new FlexLayout());
center.layout?.addWidget(scrollArea);
center.setInlineStyle(`border: 3px solid blue`);
btn1.setInlineStyle(`padding: 10;font-family: "Sans serif";`);
btn2.setInlineStyle(`padding: 10;font-family: "Sans serif";`);
center.setLayout(boxLayout);
boxLayout.addWidget(btn1);
boxLayout.addWidget(btn2);
const box2 = new QBoxLayout(Direction.BottomToTop);
const lbl1 = new QLabel();
const lbl2 = new QLabel();
lbl1.setText('lbl1');
lbl2.setText('lbl2');
lbl1.setInlineStyle(`border: 2px solid blue;`);
lbl2.setInlineStyle(`border: 2px solid green;`);
box2.addWidget(lbl1);
box2.addWidget(lbl2);
btn1.addEventListener('clicked', () => {
console.log('direction', boxLayout.direction());
boxLayout.addLayout(box2, 2);
});
btn2.addEventListener('clicked', () => {
boxLayout.addSpacing(20);
});
win.setCentralWidget(center);
win.show();
scrollArea.setInlineStyle(`flex: 1;`);
(global as any).win = win;

View File

@ -47,6 +47,7 @@ export { QSize } from './lib/QtCore/QSize';
export { QRect } from './lib/QtCore/QRect';
export { QUrl, ParsingMode } from './lib/QtCore/QUrl';
// Layouts:
export { QBoxLayout } from './lib/QtWidgets/QBoxLayout';
export { QGridLayout } from './lib/QtWidgets/QGridLayout';
export { FlexLayout } from './lib/core/FlexLayout';
// Others:

View File

@ -0,0 +1,6 @@
export enum Direction {
LeftToRight = 0,
RightToLeft = 1,
TopToBottom = 2,
BottomToTop = 3,
}

View File

@ -19,6 +19,7 @@ export { CursorMoveStyle } from './CursorMoveStyle';
export { CursorShape } from './CursorShape';
export { DateFormat } from './DateFormat';
export { DayOfWeek } from './DayOfWeek';
export { Direction } from './Direction';
export { DockWidgetArea } from './DockWidgetArea';
export { DropAction } from './DropAction';
export { Edge } from './Edge';

View File

@ -0,0 +1,53 @@
import addon from '../utils/addon';
import { NodeWidget } from './QWidget';
import { NodeLayout } from './QLayout';
import { NativeElement } from '../core/Component';
import { Direction } from '../QtEnums';
export class QBoxLayout extends NodeLayout {
native: NativeElement;
childLayouts: Set<NodeLayout>;
constructor(dir: Direction, parent?: NodeWidget) {
let native: NativeElement;
if (parent) {
native = new addon.QBoxLayout(dir, parent.native);
} else {
native = new addon.QBoxLayout(dir);
}
super(native);
this.nodeParent = parent;
this.native = native;
this.childLayouts = new Set();
}
addLayout(layout: NodeLayout, stretch = 0): void {
this.native.addLayout(layout.native, stretch);
this.childLayouts.add(layout);
}
addSpacing(size: number): void {
this.native.addSpacing(size);
}
addStretch(stretch = 0): void {
this.native.addStretch(stretch);
}
addStrut(size: number): void {
this.native.addStrut(size);
}
addWidget(widget: NodeWidget): void {
this.native.addWidget(widget.native);
this.nodeChildren.add(widget);
}
direction(): Direction {
return this.native.direction();
}
insertLayout(index: number, layout: NodeLayout, stretch = 0): void {
this.native.insertLayout(index, layout.native, stretch);
this.childLayouts.add(layout);
}
removeWidget(widget: NodeWidget): void {
this.native.removeWidget(widget.native);
this.nodeChildren.delete(widget);
}
setDirection(dir: Direction): void {
this.native.setDirection(dir);
}
}