diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e0f8f25a..7ce90ca78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/src/cpp/include/nodegui/QtWidgets/QBoxLayout/nboxlayout.hpp b/src/cpp/include/nodegui/QtWidgets/QBoxLayout/nboxlayout.hpp new file mode 100644 index 000000000..967b6cf85 --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QBoxLayout/nboxlayout.hpp @@ -0,0 +1,11 @@ +#pragma once +#include + +#include "core/Events/eventwidget_macro.h" + +class NBoxLayout : public QBoxLayout, public EventWidget { + public: + Q_OBJECT + EVENTWIDGET_IMPLEMENTATIONS(QBoxLayout) + using QBoxLayout::QBoxLayout; +}; diff --git a/src/cpp/include/nodegui/QtWidgets/QBoxLayout/qboxlayout_wrap.h b/src/cpp/include/nodegui/QtWidgets/QBoxLayout/qboxlayout_wrap.h new file mode 100644 index 000000000..155bd33a9 --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QBoxLayout/qboxlayout_wrap.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +#include +#include + +#include "QtWidgets/QBoxLayout/nboxlayout.hpp" +#include "QtWidgets/QLayout/qlayout_macro.h" + +class QBoxLayoutWrap : public Napi::ObjectWrap { + private: + QPointer 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 +}; diff --git a/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp b/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp new file mode 100644 index 000000000..bcd42ab15 --- /dev/null +++ b/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp @@ -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(info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if (info.Length() == 2) { + QBoxLayout::Direction dir = static_cast( + info[0].As().Int32Value()); + Napi::Object parentObject = info[1].As(); + QWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NBoxLayout(dir, parentWidgetWrap->getInternalInstance()); + } else if (info.Length() == 1) { + QBoxLayout::Direction dir = static_cast( + info[0].As().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(); + QLayoutWrap* layout = Napi::ObjectWrap::Unwrap(qlayoutObject); + int stretch = info[1].As().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().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().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().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(); + QWidgetWrap* widget = Napi::ObjectWrap::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().Int32Value(); + Napi::Object qlayoutObject = info[1].As(); + QLayoutWrap* layout = Napi::ObjectWrap::Unwrap(qlayoutObject); + int stretch = info[2].As().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(); + QWidgetWrap* widget = Napi::ObjectWrap::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( + info[0].As().Int32Value()); + this->instance->setDirection(dir); + return env.Null(); +} \ No newline at end of file diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 63a5fa574..f234cefbf 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -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); diff --git a/src/demo.ts b/src/demo.ts index ac38f8a3c..08642c797 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -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; diff --git a/src/index.ts b/src/index.ts index 091d3aa91..1d990cdda 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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: diff --git a/src/lib/QtEnums/Direction/index.ts b/src/lib/QtEnums/Direction/index.ts new file mode 100644 index 000000000..bcdff0d2c --- /dev/null +++ b/src/lib/QtEnums/Direction/index.ts @@ -0,0 +1,6 @@ +export enum Direction { + LeftToRight = 0, + RightToLeft = 1, + TopToBottom = 2, + BottomToTop = 3, +} diff --git a/src/lib/QtEnums/index.ts b/src/lib/QtEnums/index.ts index 5cf7ab141..3316c5b9a 100644 --- a/src/lib/QtEnums/index.ts +++ b/src/lib/QtEnums/index.ts @@ -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'; diff --git a/src/lib/QtWidgets/QBoxLayout.ts b/src/lib/QtWidgets/QBoxLayout.ts new file mode 100644 index 000000000..8c9e6787d --- /dev/null +++ b/src/lib/QtWidgets/QBoxLayout.ts @@ -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; + 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); + } +}