From 229ecddeb1c5ae6caa7d6167ca09c6c9c90a89d4 Mon Sep 17 00:00:00 2001 From: Atul R Date: Sat, 15 Jun 2019 18:29:16 +0200 Subject: [PATCH 1/4] events support --- config/application.gypi | 1 + demo.ts | 6 +- src/cpp/QtWidgets/QPushButton/npushbutton.h | 29 ++++ .../QPushButton/qpushbutton_wrap.cpp | 18 ++- .../QtWidgets/QPushButton/qpushbutton_wrap.h | 4 +- src/cpp/core/Events/eventsmap.cpp | 149 ++++++++++++++++++ src/cpp/core/Events/eventsmap.h | 8 + src/cpp/core/YogaWidget/nodestyle.h | 1 - 8 files changed, 206 insertions(+), 10 deletions(-) create mode 100644 src/cpp/core/Events/eventsmap.cpp diff --git a/config/application.gypi b/config/application.gypi index ad39581d0..5f7f9003d 100644 --- a/config/application.gypi +++ b/config/application.gypi @@ -13,6 +13,7 @@ "../src/cpp/core/FlexLayout/flexlayout.cpp", "../src/cpp/core/FlexLayout/flexitem.cpp", "../src/cpp/core/YogaWidget/nodestyle.cpp", + "../src/cpp/core/Events/eventsmap.cpp", "../src/cpp/core/YogaWidget/yogawidget.cpp", # wrapped cpps "../src/cpp/QtGui/QApplication/qapplication_wrap.cpp", diff --git a/demo.ts b/demo.ts index 26af97119..fa904e6ae 100644 --- a/demo.ts +++ b/demo.ts @@ -71,7 +71,7 @@ const testFlexLayout = () => { const win = new QMainWindow(); win.setObjectName("win"); - win.resize(300,300); + win.resize(300, 300); const rootView = new QWidget(); rootView.setStyleSheet(` * { @@ -127,6 +127,10 @@ const testFlexLayout = () => { const button = new QPushButton(view2); button.setObjectName("button"); button.setText("Hululu"); + button.native.subscribeToEvent("MouseButtonPress"); + button.setSignalListener("MouseButtonPress", () => { + console.log("MouseButtonPress"); + }); flayout2.addWidget(button, button.getFlexNode()); rootLayout.addWidget(view, view.getFlexNode()); diff --git a/src/cpp/QtWidgets/QPushButton/npushbutton.h b/src/cpp/QtWidgets/QPushButton/npushbutton.h index 21af214c3..f1effddd9 100644 --- a/src/cpp/QtWidgets/QPushButton/npushbutton.h +++ b/src/cpp/QtWidgets/QPushButton/npushbutton.h @@ -4,13 +4,42 @@ #include #include "src/cpp/core/YogaWidget/yogawidget.h" #include "napi.h" +#include +#include +#include "src/cpp/core/Events/eventsmap.h" +#include class NPushButton: public QPushButton, public YogaWidget { public: + std::unique_ptr emitOnNode; SET_YOGA_WIDGET_Q_PROPERTIES using QPushButton::QPushButton; //inherit all constructors of QPushButton Q_OBJECT +public: + std::unordered_map subscribedEvents; + + void subscribeToEvent(std::string evtString){ + try { + int evtType = EventsMap::events.at(evtString); + this->subscribedEvents.insert({static_cast(evtType), evtString}); + } catch (...) { + qDebug()<< "Coudn't subscribe to event "<< evtString.c_str(); + } + } + bool event(QEvent* event){ + try { + QEvent::Type e = event->type(); + std::string eventTypeString = subscribedEvents.at(e); + this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, eventTypeString) }; + }); + } catch (...) {} + return QWidget::event(event); + } + ~NPushButton(){ + this->emitOnNode.release(); //cleanup instance->emitOnNode + } }; diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp index 2f588d6d4..eb862934c 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp @@ -10,6 +10,7 @@ Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) { Napi::Function func = DefineClass(env, CLASSNAME, { InstanceMethod("setText", &QPushButtonWrap::setText), InstanceMethod("setupSignalListeners",&QPushButtonWrap::setupSignalListeners), + InstanceMethod("subscribeToEvent",&QPushButtonWrap::subscribeToEvent), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPushButtonWrap) }); constructor = Napi::Persistent(func); @@ -39,37 +40,42 @@ QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info): Napi::ObjectWr } QPushButtonWrap::~QPushButtonWrap() { - this->emitOnNode.release(); //cleanup emitOnNode delete this->instance; } Napi::Value QPushButtonWrap::setupSignalListeners(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); - this->emitOnNode = std::make_unique(info[0].As()); + this->instance->emitOnNode = std::make_unique(info[0].As()); // Qt Connects: Implement all signal connects here QObject::connect(this->instance, &QPushButton::clicked, [=](bool checked) { - this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + this->instance->emitOnNode->call([=](Napi::Env env, std::vector& args) { args = { Napi::String::New(env, "clicked"), Napi::Value::From(env, checked) }; }); }); QObject::connect(this->instance, &QPushButton::released, [=]() { - this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + this->instance->emitOnNode->call([=](Napi::Env env, std::vector& args) { args = { Napi::String::New(env, "released") }; }); }); QObject::connect(this->instance, &QPushButton::pressed, [=]() { - this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + this->instance->emitOnNode->call([=](Napi::Env env, std::vector& args) { args = { Napi::String::New(env, "pressed") }; }); }); QObject::connect(this->instance, &QPushButton::toggled, [=](bool checked) { - this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + this->instance->emitOnNode->call([=](Napi::Env env, std::vector& args) { args = { Napi::String::New(env, "toggled"), Napi::Value::From(env, checked) }; }); }); return env.Null(); } +Napi::Value QPushButtonWrap::subscribeToEvent(const Napi::CallbackInfo& info){ + Napi::Env env = info.Env(); + Napi::String eventString = info[0].As(); + this->instance->subscribeToEvent(eventString.Utf8Value()); + return env.Null(); +} Napi::Value QPushButtonWrap::setText(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h index 3f164706b..866e5e0ae 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include "npushbutton.h" #include "src/cpp/QtGui/QWidget/qwidget_macro.h" @@ -10,7 +9,7 @@ class QPushButtonWrap : public Napi::ObjectWrap { private: NPushButton* instance; - std::unique_ptr emitOnNode; + // std::unique_ptr emitOnNode; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QPushButtonWrap(const Napi::CallbackInfo& info); @@ -21,6 +20,7 @@ class QPushButtonWrap : public Napi::ObjectWrap { static Napi::FunctionReference constructor; //wrapped methods Napi::Value setText(const Napi::CallbackInfo& info); + Napi::Value subscribeToEvent(const Napi::CallbackInfo& info); QWIDGET_WRAPPED_METHODS_DECLARATION diff --git a/src/cpp/core/Events/eventsmap.cpp b/src/cpp/core/Events/eventsmap.cpp new file mode 100644 index 000000000..564838f8d --- /dev/null +++ b/src/cpp/core/Events/eventsmap.cpp @@ -0,0 +1,149 @@ +#include "eventsmap.h" + +std::unordered_map EventsMap::events { + { "None", QEvent::None }, + { "ActionAdded", QEvent::ActionAdded }, + { "ActionChanged", QEvent::ActionChanged }, + { "ActionRemoved", QEvent::ActionRemoved }, + { "ActivationChange", QEvent::ActivationChange }, + { "ApplicationActivate", QEvent::ApplicationActivate }, + { "ApplicationActivated", QEvent::ApplicationActivated }, + { "ApplicationDeactivate", QEvent::ApplicationDeactivate }, + { "ApplicationFontChange", QEvent::ApplicationFontChange }, + { "ApplicationLayoutDirectionChange", QEvent::ApplicationLayoutDirectionChange }, + { "ApplicationPaletteChange", QEvent::ApplicationPaletteChange }, + { "ApplicationStateChange", QEvent::ApplicationStateChange }, + { "ApplicationWindowIconChange", QEvent::ApplicationWindowIconChange }, + { "ChildAdded", QEvent::ChildAdded }, + { "ChildPolished", QEvent::ChildPolished }, + { "ChildRemoved", QEvent::ChildRemoved }, + { "Clipboard", QEvent::Clipboard }, + { "Close", QEvent::Close }, + { "CloseSoftwareInputPanel", QEvent::CloseSoftwareInputPanel }, + { "ContentsRectChange", QEvent::ContentsRectChange }, + { "ContextMenu", QEvent::ContextMenu }, + { "CursorChange", QEvent::CursorChange }, + { "DeferredDelete", QEvent::DeferredDelete }, + { "DragEnter", QEvent::DragEnter }, + { "DragLeave", QEvent::DragLeave }, + { "DragMove", QEvent::DragMove }, + { "Drop", QEvent::Drop }, + { "DynamicPropertyChange", QEvent::DynamicPropertyChange }, + { "EnabledChange", QEvent::EnabledChange }, + { "Enter", QEvent::Enter }, + { "EnterWhatsThisMode", QEvent::EnterWhatsThisMode }, + { "Expose", QEvent::Expose }, + { "FileOpen", QEvent::FileOpen }, + { "FocusIn", QEvent::FocusIn }, + { "FocusOut", QEvent::FocusOut }, + { "FocusAboutToChange", QEvent::FocusAboutToChange }, + { "FontChange", QEvent::FontChange }, + { "Gesture", QEvent::Gesture }, + { "GestureOverride", QEvent::GestureOverride }, + { "GrabKeyboard", QEvent::GrabKeyboard }, + { "GrabMouse", QEvent::GrabMouse }, + { "GraphicsSceneContextMenu", QEvent::GraphicsSceneContextMenu }, + { "GraphicsSceneDragEnter", QEvent::GraphicsSceneDragEnter }, + { "GraphicsSceneDragLeave", QEvent::GraphicsSceneDragLeave }, + { "GraphicsSceneDragMove", QEvent::GraphicsSceneDragMove }, + { "GraphicsSceneDrop", QEvent::GraphicsSceneDrop }, + { "GraphicsSceneHelp", QEvent::GraphicsSceneHelp }, + { "GraphicsSceneHoverEnter", QEvent::GraphicsSceneHoverEnter }, + { "GraphicsSceneHoverLeave", QEvent::GraphicsSceneHoverLeave }, + { "GraphicsSceneHoverMove", QEvent::GraphicsSceneHoverMove }, + { "GraphicsSceneMouseDoubleClick", QEvent::GraphicsSceneMouseDoubleClick }, + { "GraphicsSceneMouseMove", QEvent::GraphicsSceneMouseMove }, + { "GraphicsSceneMousePress", QEvent::GraphicsSceneMousePress }, + { "GraphicsSceneMouseRelease", QEvent::GraphicsSceneMouseRelease }, + { "GraphicsSceneMove", QEvent::GraphicsSceneMove }, + { "GraphicsSceneResize", QEvent::GraphicsSceneResize }, + { "GraphicsSceneWheel", QEvent::GraphicsSceneWheel }, + { "Hide", QEvent::Hide }, + { "HideToParent", QEvent::HideToParent }, + { "HoverEnter", QEvent::HoverEnter }, + { "HoverLeave", QEvent::HoverLeave }, + { "HoverMove", QEvent::HoverMove }, + { "IconDrag", QEvent::IconDrag }, + { "IconTextChange", QEvent::IconTextChange }, + { "InputMethod", QEvent::InputMethod }, + { "InputMethodQuery", QEvent::InputMethodQuery }, + { "KeyboardLayoutChange", QEvent::KeyboardLayoutChange }, + { "KeyPress", QEvent::KeyPress }, + { "KeyRelease", QEvent::KeyRelease }, + { "LanguageChange", QEvent::LanguageChange }, + { "LayoutDirectionChange", QEvent::LayoutDirectionChange }, + { "LayoutRequest", QEvent::LayoutRequest }, + { "Leave", QEvent::Leave }, + { "LeaveWhatsThisMode", QEvent::LeaveWhatsThisMode }, + { "LocaleChange", QEvent::LocaleChange }, + { "NonClientAreaMouseButtonDblClick", QEvent::NonClientAreaMouseButtonDblClick }, + { "NonClientAreaMouseButtonPress", QEvent::NonClientAreaMouseButtonPress }, + { "NonClientAreaMouseButtonRelease", QEvent::NonClientAreaMouseButtonRelease }, + { "NonClientAreaMouseMove", QEvent::NonClientAreaMouseMove }, + { "MacSizeChange", QEvent::MacSizeChange }, + { "MetaCall", QEvent::MetaCall }, + { "ModifiedChange", QEvent::ModifiedChange }, + { "MouseButtonDblClick", QEvent::MouseButtonDblClick }, + { "MouseButtonPress", QEvent::MouseButtonPress }, + { "MouseButtonRelease", QEvent::MouseButtonRelease }, + { "MouseMove", QEvent::MouseMove }, + { "MouseTrackingChange", QEvent::MouseTrackingChange }, + { "Move", QEvent::Move }, + { "NativeGesture", QEvent::NativeGesture }, + { "OrientationChange", QEvent::OrientationChange }, + { "Paint", QEvent::Paint }, + { "PaletteChange", QEvent::PaletteChange }, + { "ParentAboutToChange", QEvent::ParentAboutToChange }, + { "ParentChange", QEvent::ParentChange }, + { "PlatformPanel", QEvent::PlatformPanel }, + { "PlatformSurface", QEvent::PlatformSurface }, + { "Polish", QEvent::Polish }, + { "PolishRequest", QEvent::PolishRequest }, + { "QueryWhatsThis", QEvent::QueryWhatsThis }, + { "ReadOnlyChange", QEvent::ReadOnlyChange }, + { "RequestSoftwareInputPanel", QEvent::RequestSoftwareInputPanel }, + { "Resize", QEvent::Resize }, + { "ScrollPrepare", QEvent::ScrollPrepare }, + { "Scroll", QEvent::Scroll }, + { "Shortcut", QEvent::Shortcut }, + { "ShortcutOverride", QEvent::ShortcutOverride }, + { "Show", QEvent::Show }, + { "ShowToParent", QEvent::ShowToParent }, + { "SockAct", QEvent::SockAct }, + { "StateMachineSignal", QEvent::StateMachineSignal }, + { "StateMachineWrapped", QEvent::StateMachineWrapped }, + { "StatusTip", QEvent::StatusTip }, + { "StyleChange", QEvent::StyleChange }, + { "TabletMove", QEvent::TabletMove }, + { "TabletPress", QEvent::TabletPress }, + { "TabletRelease", QEvent::TabletRelease }, + { "TabletEnterProximity", QEvent::TabletEnterProximity }, + { "TabletLeaveProximity", QEvent::TabletLeaveProximity }, + { "TabletTrackingChange", QEvent::TabletTrackingChange }, + { "ThreadChange", QEvent::ThreadChange }, + { "Timer", QEvent::Timer }, + { "ToolBarChange", QEvent::ToolBarChange }, + { "ToolTip", QEvent::ToolTip }, + { "ToolTipChange", QEvent::ToolTipChange }, + { "TouchBegin", QEvent::TouchBegin }, + { "TouchCancel", QEvent::TouchCancel }, + { "TouchEnd", QEvent::TouchEnd }, + { "TouchUpdate", QEvent::TouchUpdate }, + { "UngrabKeyboard", QEvent::UngrabKeyboard }, + { "UngrabMouse", QEvent::UngrabMouse }, + { "UpdateLater", QEvent::UpdateLater }, + { "UpdateRequest", QEvent::UpdateRequest }, + { "WhatsThis", QEvent::WhatsThis }, + { "WhatsThisClicked", QEvent::WhatsThisClicked }, + { "Wheel", QEvent::Wheel }, + { "WinEventAct", QEvent::WinEventAct }, + { "WindowActivate", QEvent::WindowActivate }, + { "WindowBlocked", QEvent::WindowBlocked }, + { "WindowDeactivate", QEvent::WindowDeactivate }, + { "WindowIconChange", QEvent::WindowIconChange }, + { "WindowStateChange", QEvent::WindowStateChange }, + { "WindowTitleChange", QEvent::WindowTitleChange }, + { "WindowUnblocked", QEvent::WindowUnblocked }, + { "WinIdChange", QEvent::WinIdChange }, + { "ZOrderChange", QEvent::ZOrderChange }, + }; \ No newline at end of file diff --git a/src/cpp/core/Events/eventsmap.h b/src/cpp/core/Events/eventsmap.h index e69de29bb..7ab67dd7e 100644 --- a/src/cpp/core/Events/eventsmap.h +++ b/src/cpp/core/Events/eventsmap.h @@ -0,0 +1,8 @@ +#pragma once +#include +#include + +class EventsMap { + public: + static std::unordered_map events; +}; \ No newline at end of file diff --git a/src/cpp/core/YogaWidget/nodestyle.h b/src/cpp/core/YogaWidget/nodestyle.h index 39437bb68..fe34cc000 100644 --- a/src/cpp/core/YogaWidget/nodestyle.h +++ b/src/cpp/core/YogaWidget/nodestyle.h @@ -11,7 +11,6 @@ */ - struct NodeValueUnit{ YGUnit unit; float value; From 3d08908f9a2f8cc37d8eb2746dcc3af397087beb Mon Sep 17 00:00:00 2001 From: Atul R Date: Sat, 15 Jun 2019 23:43:11 +0200 Subject: [PATCH 2/4] Added event listener to all widgets --- config/application.gypi | 1 + demo.ts | 13 +++-- examples/calculator/index.ts | 2 +- src/cpp/QtGui/QWidget/nwidget.h | 3 +- src/cpp/QtGui/QWidget/qwidget_macro.h | 3 ++ src/cpp/QtWidgets/QCheckBox/ncheckbox.h | 3 +- src/cpp/QtWidgets/QLabel/nlabel.h | 3 +- src/cpp/QtWidgets/QLineEdit/nlineedit.h | 3 +- src/cpp/QtWidgets/QMainWindow/nmainwindow.h | 3 +- src/cpp/QtWidgets/QProgressBar/nprogressbar.h | 3 +- src/cpp/QtWidgets/QPushButton/npushbutton.h | 54 +++++++++---------- .../QPushButton/qpushbutton_wrap.cpp | 35 ------------ src/cpp/QtWidgets/QRadioButton/nradiobutton.h | 3 +- src/cpp/autogen/ncheckbox_moc.cpp | 2 + src/cpp/autogen/nlabel_moc.cpp | 2 + src/cpp/autogen/nlineedit_moc.cpp | 2 + src/cpp/autogen/nmainwindow_moc.cpp | 2 + src/cpp/autogen/nprogressbar_moc.cpp | 2 + src/cpp/autogen/npushbutton_moc.cpp | 2 + src/cpp/autogen/nradiobutton_moc.cpp | 2 + src/cpp/autogen/nwidget_moc.cpp | 2 + src/cpp/core/Events/eventwidget.cpp | 36 +++++++++++++ src/cpp/core/Events/eventwidget.h | 19 +++++++ src/cpp/core/Events/eventwidget_macro.h | 38 +++++++++++++ src/lib/QtGui/QWidget/index.ts | 14 ++--- src/lib/QtWidgets/QCheckBox/index.ts | 17 +++--- src/lib/QtWidgets/QLabel/index.ts | 26 +++++---- src/lib/QtWidgets/QLineEdit/index.ts | 11 ++-- src/lib/QtWidgets/QMainWindow/index.ts | 22 +++++--- src/lib/QtWidgets/QProgressBar/index.ts | 11 ++-- src/lib/QtWidgets/QPushButton/index.ts | 4 +- src/lib/QtWidgets/QRadioButton/index.ts | 11 ++-- src/lib/core/EventWidget/index.ts | 20 +++++++ src/lib/core/SignalNodeWidget/index.ts | 22 -------- 34 files changed, 253 insertions(+), 143 deletions(-) create mode 100644 src/cpp/core/Events/eventwidget.cpp create mode 100644 src/cpp/core/Events/eventwidget.h create mode 100644 src/cpp/core/Events/eventwidget_macro.h create mode 100644 src/lib/core/EventWidget/index.ts delete mode 100644 src/lib/core/SignalNodeWidget/index.ts diff --git a/config/application.gypi b/config/application.gypi index 5f7f9003d..b393a5269 100644 --- a/config/application.gypi +++ b/config/application.gypi @@ -14,6 +14,7 @@ "../src/cpp/core/FlexLayout/flexitem.cpp", "../src/cpp/core/YogaWidget/nodestyle.cpp", "../src/cpp/core/Events/eventsmap.cpp", + "../src/cpp/core/Events/eventwidget.cpp", "../src/cpp/core/YogaWidget/yogawidget.cpp", # wrapped cpps "../src/cpp/QtGui/QApplication/qapplication_wrap.cpp", diff --git a/demo.ts b/demo.ts index fa904e6ae..2c1925a22 100644 --- a/demo.ts +++ b/demo.ts @@ -25,16 +25,16 @@ const testGridLayout = () => { label.setStyleSheet("background-color:blue; color:white;"); const button1 = new QPushButton(); - button1.setSignalListener(QPushButtonSignal.clicked, isChecked => { + button1.addEventListener(QPushButtonSignal.clicked, isChecked => { console.log("clicked", isChecked); }); - button1.setSignalListener(QPushButtonSignal.pressed, (...args) => { + button1.addEventListener(QPushButtonSignal.pressed, (...args) => { console.log("pressed", ...args); }); - button1.setSignalListener(QPushButtonSignal.released, (...args) => { + button1.addEventListener(QPushButtonSignal.released, (...args) => { console.log("released", ...args); }); - button1.setSignalListener(QPushButtonSignal.toggled, isToggled => { + button1.addEventListener(QPushButtonSignal.toggled, isToggled => { console.log("toggled", isToggled); }); @@ -127,9 +127,8 @@ const testFlexLayout = () => { const button = new QPushButton(view2); button.setObjectName("button"); button.setText("Hululu"); - button.native.subscribeToEvent("MouseButtonPress"); - button.setSignalListener("MouseButtonPress", () => { - console.log("MouseButtonPress"); + button.addEventListener("pressed", () => { + console.log("pressed"); }); flayout2.addWidget(button, button.getFlexNode()); diff --git a/examples/calculator/index.ts b/examples/calculator/index.ts index 79fd9c7bb..2ad3a0e08 100644 --- a/examples/calculator/index.ts +++ b/examples/calculator/index.ts @@ -20,7 +20,7 @@ const getButton = ( const button = new QPushButton(); button.setText(label); button.setObjectName(`btn${value}`); - button.setSignalListener(QPushButtonSignal.clicked, () => { + button.addEventListener(QPushButtonSignal.clicked, () => { onBtnClick(value, type); }); return { diff --git a/src/cpp/QtGui/QWidget/nwidget.h b/src/cpp/QtGui/QWidget/nwidget.h index b3384ebb5..774326592 100644 --- a/src/cpp/QtGui/QWidget/nwidget.h +++ b/src/cpp/QtGui/QWidget/nwidget.h @@ -2,8 +2,9 @@ #include #include #include "src/cpp/core/YogaWidget/yogawidget.h" +#include "src/cpp/core/Events/eventwidget.h" -class NWidget: public QWidget, public YogaWidget +class NWidget: public QWidget, public YogaWidget, public EventWidget { public: diff --git a/src/cpp/QtGui/QWidget/qwidget_macro.h b/src/cpp/QtGui/QWidget/qwidget_macro.h index a61c7b1e4..a70c3a47e 100644 --- a/src/cpp/QtGui/QWidget/qwidget_macro.h +++ b/src/cpp/QtGui/QWidget/qwidget_macro.h @@ -2,6 +2,7 @@ #include "src/cpp/QtWidgets/QLayout/qlayout_wrap.h" #include "src/cpp/core/YogaWidget/yogawidget_macro.h" +#include "src/cpp/core/Events/eventwidget_macro.h" /* This macro adds common QWidgets exported methods @@ -12,6 +13,7 @@ #define QWIDGET_WRAPPED_METHODS_DECLARATION \ \ YOGAWIDGET_WRAPPED_METHODS_DECLARATION \ +EVENTWIDGET_WRAPPED_METHODS_DECLARATION \ \ Napi::Value show(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ @@ -75,6 +77,7 @@ Napi::Value setObjectName(const Napi::CallbackInfo& info){ \ #define QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ \ YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ + EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ InstanceMethod("show", &WidgetWrapName::show), \ InstanceMethod("resize",&WidgetWrapName::resize), \ InstanceMethod("close",&WidgetWrapName::close), \ diff --git a/src/cpp/QtWidgets/QCheckBox/ncheckbox.h b/src/cpp/QtWidgets/QCheckBox/ncheckbox.h index 430339a3b..305f5d54c 100644 --- a/src/cpp/QtWidgets/QCheckBox/ncheckbox.h +++ b/src/cpp/QtWidgets/QCheckBox/ncheckbox.h @@ -3,8 +3,9 @@ #include #include #include "src/cpp/core/YogaWidget/yogawidget.h" +#include "src/cpp/core/Events/eventwidget.h" -class NCheckBox: public QCheckBox, public YogaWidget +class NCheckBox: public QCheckBox, public YogaWidget, public EventWidget { public: diff --git a/src/cpp/QtWidgets/QLabel/nlabel.h b/src/cpp/QtWidgets/QLabel/nlabel.h index 13bdef8bc..586afc6f5 100644 --- a/src/cpp/QtWidgets/QLabel/nlabel.h +++ b/src/cpp/QtWidgets/QLabel/nlabel.h @@ -3,8 +3,9 @@ #include #include #include "src/cpp/core/YogaWidget/yogawidget.h" +#include "src/cpp/core/Events/eventwidget.h" -class NLabel: public QLabel, public YogaWidget +class NLabel: public QLabel, public YogaWidget, public EventWidget { public: diff --git a/src/cpp/QtWidgets/QLineEdit/nlineedit.h b/src/cpp/QtWidgets/QLineEdit/nlineedit.h index 818ca1c5f..0d7c5a2d9 100644 --- a/src/cpp/QtWidgets/QLineEdit/nlineedit.h +++ b/src/cpp/QtWidgets/QLineEdit/nlineedit.h @@ -3,8 +3,9 @@ #include #include #include "src/cpp/core/YogaWidget/yogawidget.h" +#include "src/cpp/core/Events/eventwidget.h" -class NLineEdit: public QLineEdit, public YogaWidget +class NLineEdit: public QLineEdit, public YogaWidget, public EventWidget { public: diff --git a/src/cpp/QtWidgets/QMainWindow/nmainwindow.h b/src/cpp/QtWidgets/QMainWindow/nmainwindow.h index e5334abfb..364423a22 100644 --- a/src/cpp/QtWidgets/QMainWindow/nmainwindow.h +++ b/src/cpp/QtWidgets/QMainWindow/nmainwindow.h @@ -3,10 +3,11 @@ #include #include #include "src/cpp/core/YogaWidget/yogawidget.h" +#include "src/cpp/core/Events/eventwidget.h" #include "deps/spdlog/spdlog.h" #include -class NMainWindow: public QMainWindow, public YogaWidget +class NMainWindow: public QMainWindow, public YogaWidget, public EventWidget { private: diff --git a/src/cpp/QtWidgets/QProgressBar/nprogressbar.h b/src/cpp/QtWidgets/QProgressBar/nprogressbar.h index 58d44f824..74922c0ab 100644 --- a/src/cpp/QtWidgets/QProgressBar/nprogressbar.h +++ b/src/cpp/QtWidgets/QProgressBar/nprogressbar.h @@ -3,8 +3,9 @@ #include #include #include "src/cpp/core/YogaWidget/yogawidget.h" +#include "src/cpp/core/Events/eventwidget.h" -class NProgressBar: public QProgressBar, public YogaWidget +class NProgressBar: public QProgressBar, public YogaWidget, public EventWidget { public: diff --git a/src/cpp/QtWidgets/QPushButton/npushbutton.h b/src/cpp/QtWidgets/QPushButton/npushbutton.h index f1effddd9..5dff14bd4 100644 --- a/src/cpp/QtWidgets/QPushButton/npushbutton.h +++ b/src/cpp/QtWidgets/QPushButton/npushbutton.h @@ -1,44 +1,44 @@ #pragma once -#include #include #include "src/cpp/core/YogaWidget/yogawidget.h" +#include "src/cpp/core/Events/eventwidget.h" #include "napi.h" -#include -#include -#include "src/cpp/core/Events/eventsmap.h" -#include -class NPushButton: public QPushButton, public YogaWidget +class NPushButton: public QPushButton, public YogaWidget, public EventWidget { public: - std::unique_ptr emitOnNode; SET_YOGA_WIDGET_Q_PROPERTIES using QPushButton::QPushButton; //inherit all constructors of QPushButton Q_OBJECT public: - std::unordered_map subscribedEvents; - - void subscribeToEvent(std::string evtString){ - try { - int evtType = EventsMap::events.at(evtString); - this->subscribedEvents.insert({static_cast(evtType), evtString}); - } catch (...) { - qDebug()<< "Coudn't subscribe to event "<< evtString.c_str(); - } - } bool event(QEvent* event){ - try { - QEvent::Type e = event->type(); - std::string eventTypeString = subscribedEvents.at(e); - this->emitOnNode->call([=](Napi::Env env, std::vector& args) { - args = { Napi::String::New(env, eventTypeString) }; - }); - } catch (...) {} + EventWidget::event(event); return QWidget::event(event); - } - ~NPushButton(){ - this->emitOnNode.release(); //cleanup instance->emitOnNode + } + + void connectWidgetSignalsToEventEmitter(){ + // Qt Connects: Implement all signal connects here + QObject::connect(this, &QPushButton::clicked, [=](bool checked) { + this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, "clicked"), Napi::Value::From(env, checked) }; + }); + }); + QObject::connect(this, &QPushButton::released, [=]() { + this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, "released") }; + }); + }); + QObject::connect(this, &QPushButton::pressed, [=]() { + this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, "pressed") }; + }); + }); + QObject::connect(this, &QPushButton::toggled, [=](bool checked) { + this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, "toggled"), Napi::Value::From(env, checked) }; + }); + }); } }; diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp index eb862934c..29b5b0450 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp @@ -9,8 +9,6 @@ Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QPushButton"; Napi::Function func = DefineClass(env, CLASSNAME, { InstanceMethod("setText", &QPushButtonWrap::setText), - InstanceMethod("setupSignalListeners",&QPushButtonWrap::setupSignalListeners), - InstanceMethod("subscribeToEvent",&QPushButtonWrap::subscribeToEvent), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPushButtonWrap) }); constructor = Napi::Persistent(func); @@ -43,39 +41,6 @@ QPushButtonWrap::~QPushButtonWrap() { delete this->instance; } -Napi::Value QPushButtonWrap::setupSignalListeners(const Napi::CallbackInfo& info) { - Napi::Env env = info.Env(); - this->instance->emitOnNode = std::make_unique(info[0].As()); - // Qt Connects: Implement all signal connects here - QObject::connect(this->instance, &QPushButton::clicked, [=](bool checked) { - this->instance->emitOnNode->call([=](Napi::Env env, std::vector& args) { - args = { Napi::String::New(env, "clicked"), Napi::Value::From(env, checked) }; - }); - }); - QObject::connect(this->instance, &QPushButton::released, [=]() { - this->instance->emitOnNode->call([=](Napi::Env env, std::vector& args) { - args = { Napi::String::New(env, "released") }; - }); - }); - QObject::connect(this->instance, &QPushButton::pressed, [=]() { - this->instance->emitOnNode->call([=](Napi::Env env, std::vector& args) { - args = { Napi::String::New(env, "pressed") }; - }); - }); - QObject::connect(this->instance, &QPushButton::toggled, [=](bool checked) { - this->instance->emitOnNode->call([=](Napi::Env env, std::vector& args) { - args = { Napi::String::New(env, "toggled"), Napi::Value::From(env, checked) }; - }); - }); - return env.Null(); -} - -Napi::Value QPushButtonWrap::subscribeToEvent(const Napi::CallbackInfo& info){ - Napi::Env env = info.Env(); - Napi::String eventString = info[0].As(); - this->instance->subscribeToEvent(eventString.Utf8Value()); - return env.Null(); -} Napi::Value QPushButtonWrap::setText(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); diff --git a/src/cpp/QtWidgets/QRadioButton/nradiobutton.h b/src/cpp/QtWidgets/QRadioButton/nradiobutton.h index 396c5abaa..d8ce1fc05 100644 --- a/src/cpp/QtWidgets/QRadioButton/nradiobutton.h +++ b/src/cpp/QtWidgets/QRadioButton/nradiobutton.h @@ -3,8 +3,9 @@ #include #include #include "src/cpp/core/YogaWidget/yogawidget.h" +#include "src/cpp/core/Events/eventwidget.h" -class NRadioButton: public QRadioButton, public YogaWidget +class NRadioButton: public QRadioButton, public YogaWidget, public EventWidget { public: diff --git a/src/cpp/autogen/ncheckbox_moc.cpp b/src/cpp/autogen/ncheckbox_moc.cpp index d38a4f5bb..4b4475934 100644 --- a/src/cpp/autogen/ncheckbox_moc.cpp +++ b/src/cpp/autogen/ncheckbox_moc.cpp @@ -294,6 +294,8 @@ void *NCheckBox::qt_metacast(const char *_clname) return static_cast(this); if (!strcmp(_clname, "YogaWidget")) return static_cast< YogaWidget*>(this); + if (!strcmp(_clname, "EventWidget")) + return static_cast< EventWidget*>(this); return QCheckBox::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nlabel_moc.cpp b/src/cpp/autogen/nlabel_moc.cpp index 0de5ea7a7..939897a7f 100644 --- a/src/cpp/autogen/nlabel_moc.cpp +++ b/src/cpp/autogen/nlabel_moc.cpp @@ -294,6 +294,8 @@ void *NLabel::qt_metacast(const char *_clname) return static_cast(this); if (!strcmp(_clname, "YogaWidget")) return static_cast< YogaWidget*>(this); + if (!strcmp(_clname, "EventWidget")) + return static_cast< EventWidget*>(this); return QLabel::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nlineedit_moc.cpp b/src/cpp/autogen/nlineedit_moc.cpp index 28daaa870..617545522 100644 --- a/src/cpp/autogen/nlineedit_moc.cpp +++ b/src/cpp/autogen/nlineedit_moc.cpp @@ -294,6 +294,8 @@ void *NLineEdit::qt_metacast(const char *_clname) return static_cast(this); if (!strcmp(_clname, "YogaWidget")) return static_cast< YogaWidget*>(this); + if (!strcmp(_clname, "EventWidget")) + return static_cast< EventWidget*>(this); return QLineEdit::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nmainwindow_moc.cpp b/src/cpp/autogen/nmainwindow_moc.cpp index f8b97a31c..37f824e0e 100644 --- a/src/cpp/autogen/nmainwindow_moc.cpp +++ b/src/cpp/autogen/nmainwindow_moc.cpp @@ -294,6 +294,8 @@ void *NMainWindow::qt_metacast(const char *_clname) return static_cast(this); if (!strcmp(_clname, "YogaWidget")) return static_cast< YogaWidget*>(this); + if (!strcmp(_clname, "EventWidget")) + return static_cast< EventWidget*>(this); return QMainWindow::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nprogressbar_moc.cpp b/src/cpp/autogen/nprogressbar_moc.cpp index 7e23c01e9..7ab6b8b83 100644 --- a/src/cpp/autogen/nprogressbar_moc.cpp +++ b/src/cpp/autogen/nprogressbar_moc.cpp @@ -294,6 +294,8 @@ void *NProgressBar::qt_metacast(const char *_clname) return static_cast(this); if (!strcmp(_clname, "YogaWidget")) return static_cast< YogaWidget*>(this); + if (!strcmp(_clname, "EventWidget")) + return static_cast< EventWidget*>(this); return QProgressBar::qt_metacast(_clname); } diff --git a/src/cpp/autogen/npushbutton_moc.cpp b/src/cpp/autogen/npushbutton_moc.cpp index 55c4690fd..2bc4d8315 100644 --- a/src/cpp/autogen/npushbutton_moc.cpp +++ b/src/cpp/autogen/npushbutton_moc.cpp @@ -294,6 +294,8 @@ void *NPushButton::qt_metacast(const char *_clname) return static_cast(this); if (!strcmp(_clname, "YogaWidget")) return static_cast< YogaWidget*>(this); + if (!strcmp(_clname, "EventWidget")) + return static_cast< EventWidget*>(this); return QPushButton::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nradiobutton_moc.cpp b/src/cpp/autogen/nradiobutton_moc.cpp index f80744e19..ad044e986 100644 --- a/src/cpp/autogen/nradiobutton_moc.cpp +++ b/src/cpp/autogen/nradiobutton_moc.cpp @@ -294,6 +294,8 @@ void *NRadioButton::qt_metacast(const char *_clname) return static_cast(this); if (!strcmp(_clname, "YogaWidget")) return static_cast< YogaWidget*>(this); + if (!strcmp(_clname, "EventWidget")) + return static_cast< EventWidget*>(this); return QRadioButton::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nwidget_moc.cpp b/src/cpp/autogen/nwidget_moc.cpp index 71bef5022..5a8a6dfd4 100644 --- a/src/cpp/autogen/nwidget_moc.cpp +++ b/src/cpp/autogen/nwidget_moc.cpp @@ -294,6 +294,8 @@ void *NWidget::qt_metacast(const char *_clname) return static_cast(this); if (!strcmp(_clname, "YogaWidget")) return static_cast< YogaWidget*>(this); + if (!strcmp(_clname, "EventWidget")) + return static_cast< EventWidget*>(this); return QWidget::qt_metacast(_clname); } diff --git a/src/cpp/core/Events/eventwidget.cpp b/src/cpp/core/Events/eventwidget.cpp new file mode 100644 index 000000000..b42f18bd7 --- /dev/null +++ b/src/cpp/core/Events/eventwidget.cpp @@ -0,0 +1,36 @@ +#include "eventwidget.h" +#include "deps/spdlog/spdlog.h" + +void EventWidget::subscribeToEvent(std::string evtString){ + try { + int evtType = EventsMap::events.at(evtString); + this->subscribedEvents.insert({static_cast(evtType), evtString}); + } catch (...) { + spdlog::info("EventWidget: Couldn't subscribe to qt event {}. If this is a signal you can safely ignore this warning", evtString.c_str()); + } +} + +void EventWidget::event(QEvent* event){ + if(this->emitOnNode){ + try { + QEvent::Type evtType = event->type(); + std::string eventTypeString = subscribedEvents.at(evtType); + this->emitOnNode->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, eventTypeString) }; + }); + } catch (...) { + // Do nothing + } + } +} + +void EventWidget::connectWidgetSignalsToEventEmitter(){ + // Do nothing + // This method should be overriden in sub classes to connect all signals to event emiiter of node. See Push button +} + +EventWidget::~EventWidget(){ + if(this->emitOnNode){ + this->emitOnNode.release(); + } +} \ No newline at end of file diff --git a/src/cpp/core/Events/eventwidget.h b/src/cpp/core/Events/eventwidget.h new file mode 100644 index 000000000..1ec1dd009 --- /dev/null +++ b/src/cpp/core/Events/eventwidget.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include "src/cpp/core/Events/eventsmap.h" + +class EventWidget { +public: + std::unique_ptr emitOnNode = nullptr; + std::unordered_map subscribedEvents; + + void subscribeToEvent(std::string evtString); + + void event(QEvent* event); + + void connectWidgetSignalsToEventEmitter(); + + ~EventWidget(); +}; \ No newline at end of file diff --git a/src/cpp/core/Events/eventwidget_macro.h b/src/cpp/core/Events/eventwidget_macro.h new file mode 100644 index 000000000..77551cd13 --- /dev/null +++ b/src/cpp/core/Events/eventwidget_macro.h @@ -0,0 +1,38 @@ +#pragma once + +#include "eventwidget.h" + +/* + + This macro adds common YogaWidget's exported methods + The exported methods are taken into this macro to avoid writing them in each and every widget we export. + */ + +#ifndef EVENTWIDGET_WRAPPED_METHODS_DECLARATION +#define EVENTWIDGET_WRAPPED_METHODS_DECLARATION \ +\ +Napi::Value initNodeEventEmitter(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + this->instance->emitOnNode = std::make_unique(info[0].As()); \ + this->instance->connectWidgetSignalsToEventEmitter(); \ + return env.Null(); \ +} \ +\ +Napi::Value subscribeToQtEvent(const Napi::CallbackInfo& info){ \ + Napi::Env env = info.Env(); \ + Napi::String eventString = info[0].As(); \ + this->instance->subscribeToEvent(eventString.Utf8Value()); \ + return env.Null(); \ +} \ + +#endif //EVENTWIDGET_WRAPPED_METHODS_DECLARATION + +#ifndef EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE +#define EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ +\ + InstanceMethod("initNodeEventEmitter",&WidgetWrapName::initNodeEventEmitter), \ + InstanceMethod("subscribeToQtEvent",&WidgetWrapName::subscribeToQtEvent), \ + + +#endif // EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE + diff --git a/src/lib/QtGui/QWidget/index.ts b/src/lib/QtGui/QWidget/index.ts index 2b6018716..207c08833 100644 --- a/src/lib/QtGui/QWidget/index.ts +++ b/src/lib/QtGui/QWidget/index.ts @@ -1,10 +1,10 @@ import addon from "../../core/addon"; -import { YogaWidget } from "../../core/YogaWidget"; import { NodeLayout } from "../../QtWidgets/QLayout"; +import { EventWidget } from "../../core/EventWidget"; // All Widgets should extend from NodeWidget // Implement all native QWidget methods here so that all widgets get access to those aswell -export abstract class NodeWidget extends YogaWidget { +export abstract class NodeWidget extends EventWidget { type = "widget"; layout?: NodeLayout; show = () => { @@ -34,12 +34,14 @@ export abstract class NodeWidget extends YogaWidget { export class QWidget extends NodeWidget { native: any; constructor(parent?: QWidget) { - super(); + let native; if (parent) { - this.native = new addon.QWidget(parent.native); - this.parent = parent; + native = new addon.QWidget(parent.native); } else { - this.native = new addon.QWidget(); + native = new addon.QWidget(); } + super(native); + this.parent = parent; + this.native = native; } } diff --git a/src/lib/QtWidgets/QCheckBox/index.ts b/src/lib/QtWidgets/QCheckBox/index.ts index 32726a107..a7df9acb6 100644 --- a/src/lib/QtWidgets/QCheckBox/index.ts +++ b/src/lib/QtWidgets/QCheckBox/index.ts @@ -3,15 +3,20 @@ import { NodeWidget } from "../../QtGui/QWidget"; export class QCheckBox extends NodeWidget { native: any; constructor(parent?: NodeWidget) { - super(); + let native; if (parent) { - this.native = new addon.QCheckBox(parent.native); - this.parent = parent; + native = new addon.QCheckBox(parent.native); } else { - this.native = new addon.QCheckBox(); + native = new addon.QCheckBox(); } + super(native); + this.native = native; + this.parent = parent; + // bind member functions + this.setText.bind(this); } - setText = (text: string) => { + + setText(text: string) { this.native.setText(text); - }; + } } diff --git a/src/lib/QtWidgets/QLabel/index.ts b/src/lib/QtWidgets/QLabel/index.ts index ac690115a..18317821d 100644 --- a/src/lib/QtWidgets/QLabel/index.ts +++ b/src/lib/QtWidgets/QLabel/index.ts @@ -4,21 +4,27 @@ import { NodeWidget } from "../../QtGui/QWidget"; export class QLabel extends NodeWidget { native: any; constructor(parent?: NodeWidget) { - super(); + let native; if (parent) { - this.native = new addon.QLabel(parent.native); - this.parent = parent; + native = new addon.QLabel(parent.native); } else { - this.native = new addon.QLabel(); + native = new addon.QLabel(); } + super(native); + this.native = native; + this.parent = parent; + // bind member functions + this.setWordWrap.bind(this); + this.setText.bind(this); + this.text.bind(this); } - setWordWrap = (on: boolean) => { + setWordWrap(on: boolean) { this.native.setWordWrap(on); - }; - setText = (text: string | number) => { + } + setText(text: string | number) { this.native.setText(`${text}`); - }; - text = () => { + } + text() { return this.native.text(); - }; + } } diff --git a/src/lib/QtWidgets/QLineEdit/index.ts b/src/lib/QtWidgets/QLineEdit/index.ts index 64eabf17c..86c8fd745 100644 --- a/src/lib/QtWidgets/QLineEdit/index.ts +++ b/src/lib/QtWidgets/QLineEdit/index.ts @@ -4,12 +4,15 @@ import { NodeWidget } from "../../QtGui/QWidget"; export class QLineEdit extends NodeWidget { native: any; constructor(parent?: NodeWidget) { - super(); + let native; if (parent) { - this.native = new addon.QLineEdit(parent.native); - this.parent = parent; + native = new addon.QLineEdit(parent.native); } else { - this.native = new addon.QLineEdit(); + native = new addon.QLineEdit(); } + super(native); + this.native = native; + this.parent = parent; + // bind member functions } } diff --git a/src/lib/QtWidgets/QMainWindow/index.ts b/src/lib/QtWidgets/QMainWindow/index.ts index e0e1767af..0c9b975b6 100644 --- a/src/lib/QtWidgets/QMainWindow/index.ts +++ b/src/lib/QtWidgets/QMainWindow/index.ts @@ -6,23 +6,29 @@ export class QMainWindow extends NodeWidget { protected centralWidget?: NodeWidget; protected centralWidgetFlexNode?: FlexNode; constructor(parent?: NodeWidget) { - super(); + let native; if (parent) { - this.native = new addon.QMainWindow(parent.native); - this.parent = parent; + native = new addon.QMainWindow(parent.native); } else { - this.native = new addon.QMainWindow(); + native = new addon.QMainWindow(); } + super(native); + this.native = native; + this.parent = parent; + // bind member functions + this.setCentralWidget.bind(this); + this.setFixedSize.bind(this); } - setCentralWidget = (widget: NodeWidget) => { + + setCentralWidget(widget: NodeWidget) { this.centralWidgetFlexNode = widget.getFlexNode(); this.centralWidget = widget; this.native.setCentralWidget( this.centralWidget.native, this.centralWidgetFlexNode.native ); - }; - setFixedSize = (width: number, height: number) => { + } + setFixedSize(width: number, height: number) { this.native.setFixedSize(width, height); - }; + } } diff --git a/src/lib/QtWidgets/QProgressBar/index.ts b/src/lib/QtWidgets/QProgressBar/index.ts index 3e00b962d..b5ba2b58c 100644 --- a/src/lib/QtWidgets/QProgressBar/index.ts +++ b/src/lib/QtWidgets/QProgressBar/index.ts @@ -4,12 +4,15 @@ import { NodeWidget } from "../../QtGui/QWidget"; export class QProgressBar extends NodeWidget { native: any; constructor(parent?: NodeWidget) { - super(); + let native; if (parent) { - this.native = new addon.QProgressBar(parent.native); - this.parent = parent; + native = new addon.QProgressBar(parent.native); } else { - this.native = new addon.QProgressBar(); + native = new addon.QProgressBar(); } + super(native); + this.native = native; + this.parent = parent; + // bind member functions } } diff --git a/src/lib/QtWidgets/QPushButton/index.ts b/src/lib/QtWidgets/QPushButton/index.ts index 8b48fbc9a..1c371b7f0 100644 --- a/src/lib/QtWidgets/QPushButton/index.ts +++ b/src/lib/QtWidgets/QPushButton/index.ts @@ -1,6 +1,5 @@ import addon from "../../core/addon"; import { NodeWidget } from "../../QtGui/QWidget"; -import { SignalNodeWidget } from "../../core/SignalNodeWidget"; export enum QPushButtonSignal { clicked = "clicked", @@ -9,7 +8,7 @@ export enum QPushButtonSignal { toggled = "toggled" } -export class QPushButton extends SignalNodeWidget { +export class QPushButton extends NodeWidget { native: any; constructor(parent?: NodeWidget) { let native; @@ -21,6 +20,7 @@ export class QPushButton extends SignalNodeWidget { super(native); this.parent = parent; this.native = native; + // bind member functions this.setText.bind(this); } diff --git a/src/lib/QtWidgets/QRadioButton/index.ts b/src/lib/QtWidgets/QRadioButton/index.ts index 28ee3d02f..291268491 100644 --- a/src/lib/QtWidgets/QRadioButton/index.ts +++ b/src/lib/QtWidgets/QRadioButton/index.ts @@ -4,12 +4,15 @@ import { NodeWidget } from "../../QtGui/QWidget"; export class QRadioButton extends NodeWidget { native: any; constructor(parent?: NodeWidget) { - super(); + let native; if (parent) { - this.native = new addon.QRadioButton(parent.native); - this.parent = parent; + native = new addon.QRadioButton(parent.native); } else { - this.native = new addon.QRadioButton(); + native = new addon.QRadioButton(); } + super(native); + this.native = native; + this.parent = parent; + // bind member functions } } diff --git a/src/lib/core/EventWidget/index.ts b/src/lib/core/EventWidget/index.ts new file mode 100644 index 000000000..685b03364 --- /dev/null +++ b/src/lib/core/EventWidget/index.ts @@ -0,0 +1,20 @@ +import { EventEmitter } from "events"; +import { YogaWidget } from "../YogaWidget"; + +export abstract class EventWidget extends YogaWidget { + private emitter: EventEmitter; + constructor(native: any) { + super(); + if (native.initNodeEventEmitter) { + this.emitter = new EventEmitter(); + native.initNodeEventEmitter(this.emitter.emit.bind(this.emitter)); + } else { + throw new Error("initNodeEventEmitter not implemented on native side"); + } + } + + addEventListener = (eventType: string, callback: (payload?: any) => void) => { + this.native.subscribeToQtEvent(eventType); + this.emitter.on(eventType, callback); + }; +} diff --git a/src/lib/core/SignalNodeWidget/index.ts b/src/lib/core/SignalNodeWidget/index.ts deleted file mode 100644 index 99b6e5524..000000000 --- a/src/lib/core/SignalNodeWidget/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { NodeWidget } from "../../QtGui/QWidget"; -import { EventEmitter } from "events"; - -export abstract class SignalNodeWidget extends NodeWidget { - private emitter: EventEmitter; - constructor(native: any) { - super(); - if (native.setupSignalListeners) { - this.emitter = new EventEmitter(); - native.setupSignalListeners(this.emitter.emit.bind(this.emitter)); - } else { - throw new Error("Signal Listeners not implemented on native side"); - } - } - - setSignalListener = ( - signalType: string, - callback: (payload?: any) => void - ) => { - this.emitter.on(signalType, callback); - }; -} From e77ee16c6ee1f7f256924085eaa8cc1e8b7e8f6c Mon Sep 17 00:00:00 2001 From: Atul R Date: Sat, 15 Jun 2019 23:59:42 +0200 Subject: [PATCH 3/4] Single api for events and signals on JS side --- demo.ts | 10 +- examples/calculator/index.ts | 4 +- src/lib/QtWidgets/QCheckBox/index.ts | 5 + src/lib/QtWidgets/QLabel/index.ts | 4 + src/lib/QtWidgets/QLineEdit/index.ts | 4 + src/lib/QtWidgets/QMainWindow/index.ts | 5 + src/lib/QtWidgets/QProgressBar/index.ts | 4 + src/lib/QtWidgets/QPushButton/index.ts | 14 ++- src/lib/QtWidgets/QRadioButton/index.ts | 4 + src/lib/core/EventWidget/index.ts | 148 ++++++++++++++++++++++++ 10 files changed, 189 insertions(+), 13 deletions(-) diff --git a/demo.ts b/demo.ts index 2c1925a22..337e5daab 100644 --- a/demo.ts +++ b/demo.ts @@ -4,7 +4,7 @@ import { QGridLayout } from "./src/lib/QtWidgets/QGridLayout"; import { QLabel } from "./src/lib/QtWidgets/QLabel"; import { QPushButton, - QPushButtonSignal + QPushButtonEvents } from "./src/lib/QtWidgets/QPushButton"; import { QCheckBox } from "./src/lib/QtWidgets/QCheckBox"; import { QProgressBar } from "./src/lib/QtWidgets/QProgressBar"; @@ -25,16 +25,16 @@ const testGridLayout = () => { label.setStyleSheet("background-color:blue; color:white;"); const button1 = new QPushButton(); - button1.addEventListener(QPushButtonSignal.clicked, isChecked => { + button1.addEventListener(QPushButtonEvents.clicked, isChecked => { console.log("clicked", isChecked); }); - button1.addEventListener(QPushButtonSignal.pressed, (...args) => { + button1.addEventListener(QPushButtonEvents.pressed, (...args) => { console.log("pressed", ...args); }); - button1.addEventListener(QPushButtonSignal.released, (...args) => { + button1.addEventListener(QPushButtonEvents.released, (...args) => { console.log("released", ...args); }); - button1.addEventListener(QPushButtonSignal.toggled, isToggled => { + button1.addEventListener(QPushButtonEvents.toggled, isToggled => { console.log("toggled", isToggled); }); diff --git a/examples/calculator/index.ts b/examples/calculator/index.ts index 2ad3a0e08..7571da4b9 100644 --- a/examples/calculator/index.ts +++ b/examples/calculator/index.ts @@ -3,7 +3,7 @@ import { QWidget } from "../../src/lib/QtGui/QWidget"; import { FlexLayout } from "../../src/lib/core/FlexLayout"; import { QPushButton, - QPushButtonSignal + QPushButtonEvents } from "../../src/lib/QtWidgets/QPushButton"; import { QLabel } from "../../src/lib/QtWidgets/QLabel"; @@ -20,7 +20,7 @@ const getButton = ( const button = new QPushButton(); button.setText(label); button.setObjectName(`btn${value}`); - button.addEventListener(QPushButtonSignal.clicked, () => { + button.addEventListener(QPushButtonEvents.clicked, () => { onBtnClick(value, type); }); return { diff --git a/src/lib/QtWidgets/QCheckBox/index.ts b/src/lib/QtWidgets/QCheckBox/index.ts index a7df9acb6..ccbd05774 100644 --- a/src/lib/QtWidgets/QCheckBox/index.ts +++ b/src/lib/QtWidgets/QCheckBox/index.ts @@ -1,5 +1,10 @@ import addon from "../../core/addon"; import { NodeWidget } from "../../QtGui/QWidget"; +import { BaseWidgetEvents } from "../../core/EventWidget"; + +export const QCheckBoxEvents = Object.freeze({ + ...BaseWidgetEvents +}); export class QCheckBox extends NodeWidget { native: any; constructor(parent?: NodeWidget) { diff --git a/src/lib/QtWidgets/QLabel/index.ts b/src/lib/QtWidgets/QLabel/index.ts index 18317821d..4dff662d1 100644 --- a/src/lib/QtWidgets/QLabel/index.ts +++ b/src/lib/QtWidgets/QLabel/index.ts @@ -1,6 +1,10 @@ import addon from "../../core/addon"; import { NodeWidget } from "../../QtGui/QWidget"; +import { BaseWidgetEvents } from "../../core/EventWidget"; +export const QLabelEvents = Object.freeze({ + ...BaseWidgetEvents +}); export class QLabel extends NodeWidget { native: any; constructor(parent?: NodeWidget) { diff --git a/src/lib/QtWidgets/QLineEdit/index.ts b/src/lib/QtWidgets/QLineEdit/index.ts index 86c8fd745..911983100 100644 --- a/src/lib/QtWidgets/QLineEdit/index.ts +++ b/src/lib/QtWidgets/QLineEdit/index.ts @@ -1,6 +1,10 @@ import addon from "../../core/addon"; import { NodeWidget } from "../../QtGui/QWidget"; +import { BaseWidgetEvents } from "../../core/EventWidget"; +export const QLineEditEvents = Object.freeze({ + ...BaseWidgetEvents +}); export class QLineEdit extends NodeWidget { native: any; constructor(parent?: NodeWidget) { diff --git a/src/lib/QtWidgets/QMainWindow/index.ts b/src/lib/QtWidgets/QMainWindow/index.ts index 0c9b975b6..ae654d4fd 100644 --- a/src/lib/QtWidgets/QMainWindow/index.ts +++ b/src/lib/QtWidgets/QMainWindow/index.ts @@ -1,6 +1,11 @@ import addon from "../../core/addon"; import { NodeWidget } from "../../QtGui/QWidget"; import { FlexNode } from "../../core/FlexLayout/FlexNode"; +import { BaseWidgetEvents } from "../../core/EventWidget"; + +export const QMainWindowEvents = Object.freeze({ + ...BaseWidgetEvents +}); export class QMainWindow extends NodeWidget { native: any; protected centralWidget?: NodeWidget; diff --git a/src/lib/QtWidgets/QProgressBar/index.ts b/src/lib/QtWidgets/QProgressBar/index.ts index b5ba2b58c..7e3ad5663 100644 --- a/src/lib/QtWidgets/QProgressBar/index.ts +++ b/src/lib/QtWidgets/QProgressBar/index.ts @@ -1,6 +1,10 @@ import addon from "../../core/addon"; import { NodeWidget } from "../../QtGui/QWidget"; +import { BaseWidgetEvents } from "../../core/EventWidget"; +export const QProgressBarEvents = Object.freeze({ + ...BaseWidgetEvents +}); export class QProgressBar extends NodeWidget { native: any; constructor(parent?: NodeWidget) { diff --git a/src/lib/QtWidgets/QPushButton/index.ts b/src/lib/QtWidgets/QPushButton/index.ts index 1c371b7f0..c5e527a67 100644 --- a/src/lib/QtWidgets/QPushButton/index.ts +++ b/src/lib/QtWidgets/QPushButton/index.ts @@ -1,12 +1,14 @@ import addon from "../../core/addon"; import { NodeWidget } from "../../QtGui/QWidget"; +import { BaseWidgetEvents } from "../../core/EventWidget"; -export enum QPushButtonSignal { - clicked = "clicked", - pressed = "pressed", - released = "released", - toggled = "toggled" -} +export const QPushButtonEvents = Object.freeze({ + ...BaseWidgetEvents, + clicked: "clicked", + pressed: "pressed", + released: "released", + toggled: "toggled" +}); export class QPushButton extends NodeWidget { native: any; diff --git a/src/lib/QtWidgets/QRadioButton/index.ts b/src/lib/QtWidgets/QRadioButton/index.ts index 291268491..d73847b29 100644 --- a/src/lib/QtWidgets/QRadioButton/index.ts +++ b/src/lib/QtWidgets/QRadioButton/index.ts @@ -1,6 +1,10 @@ import addon from "../../core/addon"; import { NodeWidget } from "../../QtGui/QWidget"; +import { BaseWidgetEvents } from "../../core/EventWidget"; +export const QRadioButtonEvents = Object.freeze({ + ...BaseWidgetEvents +}); export class QRadioButton extends NodeWidget { native: any; constructor(parent?: NodeWidget) { diff --git a/src/lib/core/EventWidget/index.ts b/src/lib/core/EventWidget/index.ts index 685b03364..2d583ef85 100644 --- a/src/lib/core/EventWidget/index.ts +++ b/src/lib/core/EventWidget/index.ts @@ -18,3 +18,151 @@ export abstract class EventWidget extends YogaWidget { this.emitter.on(eventType, callback); }; } + +export const BaseWidgetEvents = Object.freeze({ + None: "None", + ActionAdded: "ActionAdded", + ActionChanged: "ActionChanged", + ActionRemoved: "ActionRemoved", + ActivationChange: "ActivationChange", + ApplicationActivate: "ApplicationActivate", + ApplicationActivated: "ApplicationActivated", + ApplicationDeactivate: "ApplicationDeactivate", + ApplicationFontChange: "ApplicationFontChange", + ApplicationLayoutDirectionChange: "ApplicationLayoutDirectionChange", + ApplicationPaletteChange: "ApplicationPaletteChange", + ApplicationStateChange: "ApplicationStateChange", + ApplicationWindowIconChange: "ApplicationWindowIconChange", + ChildAdded: "ChildAdded", + ChildPolished: "ChildPolished", + ChildRemoved: "ChildRemoved", + Clipboard: "Clipboard", + Close: "Close", + CloseSoftwareInputPanel: "CloseSoftwareInputPanel", + ContentsRectChange: "ContentsRectChange", + ContextMenu: "ContextMenu", + CursorChange: "CursorChange", + DeferredDelete: "DeferredDelete", + DragEnter: "DragEnter", + DragLeave: "DragLeave", + DragMove: "DragMove", + Drop: "Drop", + DynamicPropertyChange: "DynamicPropertyChange", + EnabledChange: "EnabledChange", + Enter: "Enter", + EnterWhatsThisMode: "EnterWhatsThisMode", + Expose: "Expose", + FileOpen: "FileOpen", + FocusIn: "FocusIn", + FocusOut: "FocusOut", + FocusAboutToChange: "FocusAboutToChange", + FontChange: "FontChange", + Gesture: "Gesture", + GestureOverride: "GestureOverride", + GrabKeyboard: "GrabKeyboard", + GrabMouse: "GrabMouse", + GraphicsSceneContextMenu: "GraphicsSceneContextMenu", + GraphicsSceneDragEnter: "GraphicsSceneDragEnter", + GraphicsSceneDragLeave: "GraphicsSceneDragLeave", + GraphicsSceneDragMove: "GraphicsSceneDragMove", + GraphicsSceneDrop: "GraphicsSceneDrop", + GraphicsSceneHelp: "GraphicsSceneHelp", + GraphicsSceneHoverEnter: "GraphicsSceneHoverEnter", + GraphicsSceneHoverLeave: "GraphicsSceneHoverLeave", + GraphicsSceneHoverMove: "GraphicsSceneHoverMove", + GraphicsSceneMouseDoubleClick: "GraphicsSceneMouseDoubleClick", + GraphicsSceneMouseMove: "GraphicsSceneMouseMove", + GraphicsSceneMousePress: "GraphicsSceneMousePress", + GraphicsSceneMouseRelease: "GraphicsSceneMouseRelease", + GraphicsSceneMove: "GraphicsSceneMove", + GraphicsSceneResize: "GraphicsSceneResize", + GraphicsSceneWheel: "GraphicsSceneWheel", + Hide: "Hide", + HideToParent: "HideToParent", + HoverEnter: "HoverEnter", + HoverLeave: "HoverLeave", + HoverMove: "HoverMove", + IconDrag: "IconDrag", + IconTextChange: "IconTextChange", + InputMethod: "InputMethod", + InputMethodQuery: "InputMethodQuery", + KeyboardLayoutChange: "KeyboardLayoutChange", + KeyPress: "KeyPress", + KeyRelease: "KeyRelease", + LanguageChange: "LanguageChange", + LayoutDirectionChange: "LayoutDirectionChange", + LayoutRequest: "LayoutRequest", + Leave: "Leave", + LeaveWhatsThisMode: "LeaveWhatsThisMode", + LocaleChange: "LocaleChange", + NonClientAreaMouseButtonDblClick: "NonClientAreaMouseButtonDblClick", + NonClientAreaMouseButtonPress: "NonClientAreaMouseButtonPress", + NonClientAreaMouseButtonRelease: "NonClientAreaMouseButtonRelease", + NonClientAreaMouseMove: "NonClientAreaMouseMove", + MacSizeChange: "MacSizeChange", + MetaCall: "MetaCall", + ModifiedChange: "ModifiedChange", + MouseButtonDblClick: "MouseButtonDblClick", + MouseButtonPress: "MouseButtonPress", + MouseButtonRelease: "MouseButtonRelease", + MouseMove: "MouseMove", + MouseTrackingChange: "MouseTrackingChange", + Move: "Move", + NativeGesture: "NativeGesture", + OrientationChange: "OrientationChange", + Paint: "Paint", + PaletteChange: "PaletteChange", + ParentAboutToChange: "ParentAboutToChange", + ParentChange: "ParentChange", + PlatformPanel: "PlatformPanel", + PlatformSurface: "PlatformSurface", + Polish: "Polish", + PolishRequest: "PolishRequest", + QueryWhatsThis: "QueryWhatsThis", + ReadOnlyChange: "ReadOnlyChange", + RequestSoftwareInputPanel: "RequestSoftwareInputPanel", + Resize: "Resize", + ScrollPrepare: "ScrollPrepare", + Scroll: "Scroll", + Shortcut: "Shortcut", + ShortcutOverride: "ShortcutOverride", + Show: "Show", + ShowToParent: "ShowToParent", + SockAct: "SockAct", + StateMachineSignal: "StateMachineSignal", + StateMachineWrapped: "StateMachineWrapped", + StatusTip: "StatusTip", + StyleChange: "StyleChange", + TabletMove: "TabletMove", + TabletPress: "TabletPress", + TabletRelease: "TabletRelease", + TabletEnterProximity: "TabletEnterProximity", + TabletLeaveProximity: "TabletLeaveProximity", + TabletTrackingChange: "TabletTrackingChange", + ThreadChange: "ThreadChange", + Timer: "Timer", + ToolBarChange: "ToolBarChange", + ToolTip: "ToolTip", + ToolTipChange: "ToolTipChange", + TouchBegin: "TouchBegin", + TouchCancel: "TouchCancel", + TouchEnd: "TouchEnd", + TouchUpdate: "TouchUpdate", + UngrabKeyboard: "UngrabKeyboard", + UngrabMouse: "UngrabMouse", + UpdateLater: "UpdateLater", + UpdateRequest: "UpdateRequest", + WhatsThis: "WhatsThis", + WhatsThisClicked: "WhatsThisClicked", + Wheel: "Wheel", + WinEventAct: "WinEventAct", + WindowActivate: "WindowActivate", + WindowBlocked: "WindowBlocked", + WindowDeactivate: "WindowDeactivate", + WindowIconChange: "WindowIconChange", + WindowStateChange: "WindowStateChange", + WindowTitleChange: "WindowTitleChange", + WindowUnblocked: "WindowUnblocked", + WinIdChange: "WinIdChange", + ZOrderChange: "ZOrderChange" +}); From 085c05dd85037503bf3e686be77e5b3c2ebba028 Mon Sep 17 00:00:00 2001 From: Atul R Date: Sun, 16 Jun 2019 15:55:17 +0200 Subject: [PATCH 4/4] Organised code so that its easier to modify all widgets at once. --- config/moc.json | 2 +- src/cpp/QtWidgets/QCheckBox/ncheckbox.h | 10 +++----- src/cpp/QtWidgets/QLabel/nlabel.h | 10 +++----- src/cpp/QtWidgets/QLineEdit/nlineedit.h | 10 +++----- src/cpp/QtWidgets/QMainWindow/nmainwindow.h | 15 ++++------- src/cpp/QtWidgets/QProgressBar/nprogressbar.h | 10 +++----- src/cpp/QtWidgets/QPushButton/npushbutton.h | 15 +++-------- src/cpp/QtWidgets/QRadioButton/nradiobutton.h | 12 +++------ src/cpp/autogen/ncheckbox_moc.cpp | 6 ++--- src/cpp/autogen/nlabel_moc.cpp | 6 ++--- src/cpp/autogen/nlineedit_moc.cpp | 6 ++--- src/cpp/autogen/nmainwindow_moc.cpp | 6 ++--- src/cpp/autogen/nprogressbar_moc.cpp | 6 ++--- src/cpp/autogen/npushbutton_moc.cpp | 6 ++--- src/cpp/autogen/nradiobutton_moc.cpp | 6 ++--- src/cpp/core/NodeWidget/nodewidget.h | 25 +++++++++++++++++++ 16 files changed, 65 insertions(+), 86 deletions(-) create mode 100644 src/cpp/core/NodeWidget/nodewidget.h diff --git a/config/moc.json b/config/moc.json index 9c3de2d41..92a120e1d 100644 --- a/config/moc.json +++ b/config/moc.json @@ -1,5 +1,5 @@ { - "include": "src/cpp/core/YogaWidget/yogawidget.h", + "include": "src/cpp/core/NodeWidget/nodewidget.h", "headers": [ "src/cpp/QtGui/QWidget/nwidget.h", "src/cpp/QtWidgets/QLabel/nlabel.h", diff --git a/src/cpp/QtWidgets/QCheckBox/ncheckbox.h b/src/cpp/QtWidgets/QCheckBox/ncheckbox.h index 305f5d54c..5213f9f5e 100644 --- a/src/cpp/QtWidgets/QCheckBox/ncheckbox.h +++ b/src/cpp/QtWidgets/QCheckBox/ncheckbox.h @@ -1,17 +1,13 @@ #pragma once -#include #include -#include "src/cpp/core/YogaWidget/yogawidget.h" -#include "src/cpp/core/Events/eventwidget.h" +#include "src/cpp/core/NodeWidget/nodewidget.h" -class NCheckBox: public QCheckBox, public YogaWidget, public EventWidget +class NCheckBox: public QCheckBox, public NodeWidget { - + NODEWIDGET_IMPLEMENTATIONS public: - SET_YOGA_WIDGET_Q_PROPERTIES using QCheckBox::QCheckBox; //inherit all constructors of QCheckBox - Q_OBJECT }; diff --git a/src/cpp/QtWidgets/QLabel/nlabel.h b/src/cpp/QtWidgets/QLabel/nlabel.h index 586afc6f5..830d9491e 100644 --- a/src/cpp/QtWidgets/QLabel/nlabel.h +++ b/src/cpp/QtWidgets/QLabel/nlabel.h @@ -1,17 +1,13 @@ #pragma once -#include #include -#include "src/cpp/core/YogaWidget/yogawidget.h" -#include "src/cpp/core/Events/eventwidget.h" +#include "src/cpp/core/NodeWidget/nodewidget.h" -class NLabel: public QLabel, public YogaWidget, public EventWidget +class NLabel: public QLabel, public NodeWidget { - + NODEWIDGET_IMPLEMENTATIONS public: - SET_YOGA_WIDGET_Q_PROPERTIES using QLabel::QLabel; //inherit all constructors of QLabel - Q_OBJECT }; diff --git a/src/cpp/QtWidgets/QLineEdit/nlineedit.h b/src/cpp/QtWidgets/QLineEdit/nlineedit.h index 0d7c5a2d9..20aea2f41 100644 --- a/src/cpp/QtWidgets/QLineEdit/nlineedit.h +++ b/src/cpp/QtWidgets/QLineEdit/nlineedit.h @@ -1,17 +1,13 @@ #pragma once -#include #include -#include "src/cpp/core/YogaWidget/yogawidget.h" -#include "src/cpp/core/Events/eventwidget.h" +#include "src/cpp/core/NodeWidget/nodewidget.h" -class NLineEdit: public QLineEdit, public YogaWidget, public EventWidget +class NLineEdit: public QLineEdit, public NodeWidget { - + NODEWIDGET_IMPLEMENTATIONS public: - SET_YOGA_WIDGET_Q_PROPERTIES using QLineEdit::QLineEdit; //inherit all constructors of QLineEdit - Q_OBJECT }; diff --git a/src/cpp/QtWidgets/QMainWindow/nmainwindow.h b/src/cpp/QtWidgets/QMainWindow/nmainwindow.h index 364423a22..dc57de4c3 100644 --- a/src/cpp/QtWidgets/QMainWindow/nmainwindow.h +++ b/src/cpp/QtWidgets/QMainWindow/nmainwindow.h @@ -1,15 +1,14 @@ #pragma once -#include #include -#include "src/cpp/core/YogaWidget/yogawidget.h" -#include "src/cpp/core/Events/eventwidget.h" -#include "deps/spdlog/spdlog.h" +#include "src/cpp/core/NodeWidget/nodewidget.h" #include -class NMainWindow: public QMainWindow, public YogaWidget, public EventWidget +class NMainWindow: public QMainWindow, public NodeWidget { - + NODEWIDGET_IMPLEMENTATIONS +public: + using QMainWindow::QMainWindow; //inherit all constructors of QMainWindow private: void calculateLayout(){ YGDirection direction = YGNodeStyleGetDirection(this->getFlexNode()); @@ -28,10 +27,6 @@ private: void resizeEvent(QResizeEvent * event){ calculateLayout(); } -public: - SET_YOGA_WIDGET_Q_PROPERTIES - using QMainWindow::QMainWindow; //inherit all constructors of QMainWindow - Q_OBJECT }; diff --git a/src/cpp/QtWidgets/QProgressBar/nprogressbar.h b/src/cpp/QtWidgets/QProgressBar/nprogressbar.h index 74922c0ab..55141aa71 100644 --- a/src/cpp/QtWidgets/QProgressBar/nprogressbar.h +++ b/src/cpp/QtWidgets/QProgressBar/nprogressbar.h @@ -1,17 +1,13 @@ #pragma once -#include #include -#include "src/cpp/core/YogaWidget/yogawidget.h" -#include "src/cpp/core/Events/eventwidget.h" +#include "src/cpp/core/NodeWidget/nodewidget.h" -class NProgressBar: public QProgressBar, public YogaWidget, public EventWidget +class NProgressBar: public QProgressBar, public NodeWidget { - + NODEWIDGET_IMPLEMENTATIONS public: - SET_YOGA_WIDGET_Q_PROPERTIES using QProgressBar::QProgressBar; //inherit all constructors of QProgressBar - Q_OBJECT }; diff --git a/src/cpp/QtWidgets/QPushButton/npushbutton.h b/src/cpp/QtWidgets/QPushButton/npushbutton.h index 5dff14bd4..d6aec88c8 100644 --- a/src/cpp/QtWidgets/QPushButton/npushbutton.h +++ b/src/cpp/QtWidgets/QPushButton/npushbutton.h @@ -1,23 +1,16 @@ #pragma once #include -#include "src/cpp/core/YogaWidget/yogawidget.h" -#include "src/cpp/core/Events/eventwidget.h" +#include "src/cpp/core/NodeWidget/nodewidget.h" #include "napi.h" -class NPushButton: public QPushButton, public YogaWidget, public EventWidget +class NPushButton: public QPushButton, public NodeWidget { + NODEWIDGET_IMPLEMENTATIONS public: - SET_YOGA_WIDGET_Q_PROPERTIES using QPushButton::QPushButton; //inherit all constructors of QPushButton - Q_OBJECT -public: - bool event(QEvent* event){ - EventWidget::event(event); - return QWidget::event(event); - } - void connectWidgetSignalsToEventEmitter(){ + void connectWidgetSignalsToEventEmitter() { // Qt Connects: Implement all signal connects here QObject::connect(this, &QPushButton::clicked, [=](bool checked) { this->emitOnNode->call([=](Napi::Env env, std::vector& args) { diff --git a/src/cpp/QtWidgets/QRadioButton/nradiobutton.h b/src/cpp/QtWidgets/QRadioButton/nradiobutton.h index d8ce1fc05..a126665d8 100644 --- a/src/cpp/QtWidgets/QRadioButton/nradiobutton.h +++ b/src/cpp/QtWidgets/QRadioButton/nradiobutton.h @@ -1,17 +1,13 @@ #pragma once -#include #include -#include "src/cpp/core/YogaWidget/yogawidget.h" -#include "src/cpp/core/Events/eventwidget.h" - -class NRadioButton: public QRadioButton, public YogaWidget, public EventWidget -{ +#include "src/cpp/core/NodeWidget/nodewidget.h" +class NRadioButton: public QRadioButton, public NodeWidget +{ + NODEWIDGET_IMPLEMENTATIONS public: - SET_YOGA_WIDGET_Q_PROPERTIES using QRadioButton::QRadioButton; //inherit all constructors of QRadioButton - Q_OBJECT }; diff --git a/src/cpp/autogen/ncheckbox_moc.cpp b/src/cpp/autogen/ncheckbox_moc.cpp index 4b4475934..185164613 100644 --- a/src/cpp/autogen/ncheckbox_moc.cpp +++ b/src/cpp/autogen/ncheckbox_moc.cpp @@ -292,10 +292,8 @@ void *NCheckBox::qt_metacast(const char *_clname) if (!_clname) return nullptr; if (!strcmp(_clname, qt_meta_stringdata_NCheckBox.stringdata0)) return static_cast(this); - if (!strcmp(_clname, "YogaWidget")) - return static_cast< YogaWidget*>(this); - if (!strcmp(_clname, "EventWidget")) - return static_cast< EventWidget*>(this); + if (!strcmp(_clname, "NodeWidget")) + return static_cast< NodeWidget*>(this); return QCheckBox::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nlabel_moc.cpp b/src/cpp/autogen/nlabel_moc.cpp index 939897a7f..9e5179448 100644 --- a/src/cpp/autogen/nlabel_moc.cpp +++ b/src/cpp/autogen/nlabel_moc.cpp @@ -292,10 +292,8 @@ void *NLabel::qt_metacast(const char *_clname) if (!_clname) return nullptr; if (!strcmp(_clname, qt_meta_stringdata_NLabel.stringdata0)) return static_cast(this); - if (!strcmp(_clname, "YogaWidget")) - return static_cast< YogaWidget*>(this); - if (!strcmp(_clname, "EventWidget")) - return static_cast< EventWidget*>(this); + if (!strcmp(_clname, "NodeWidget")) + return static_cast< NodeWidget*>(this); return QLabel::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nlineedit_moc.cpp b/src/cpp/autogen/nlineedit_moc.cpp index 617545522..fef87b361 100644 --- a/src/cpp/autogen/nlineedit_moc.cpp +++ b/src/cpp/autogen/nlineedit_moc.cpp @@ -292,10 +292,8 @@ void *NLineEdit::qt_metacast(const char *_clname) if (!_clname) return nullptr; if (!strcmp(_clname, qt_meta_stringdata_NLineEdit.stringdata0)) return static_cast(this); - if (!strcmp(_clname, "YogaWidget")) - return static_cast< YogaWidget*>(this); - if (!strcmp(_clname, "EventWidget")) - return static_cast< EventWidget*>(this); + if (!strcmp(_clname, "NodeWidget")) + return static_cast< NodeWidget*>(this); return QLineEdit::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nmainwindow_moc.cpp b/src/cpp/autogen/nmainwindow_moc.cpp index 37f824e0e..67d92dcd4 100644 --- a/src/cpp/autogen/nmainwindow_moc.cpp +++ b/src/cpp/autogen/nmainwindow_moc.cpp @@ -292,10 +292,8 @@ void *NMainWindow::qt_metacast(const char *_clname) if (!_clname) return nullptr; if (!strcmp(_clname, qt_meta_stringdata_NMainWindow.stringdata0)) return static_cast(this); - if (!strcmp(_clname, "YogaWidget")) - return static_cast< YogaWidget*>(this); - if (!strcmp(_clname, "EventWidget")) - return static_cast< EventWidget*>(this); + if (!strcmp(_clname, "NodeWidget")) + return static_cast< NodeWidget*>(this); return QMainWindow::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nprogressbar_moc.cpp b/src/cpp/autogen/nprogressbar_moc.cpp index 7ab6b8b83..4b30b1a95 100644 --- a/src/cpp/autogen/nprogressbar_moc.cpp +++ b/src/cpp/autogen/nprogressbar_moc.cpp @@ -292,10 +292,8 @@ void *NProgressBar::qt_metacast(const char *_clname) if (!_clname) return nullptr; if (!strcmp(_clname, qt_meta_stringdata_NProgressBar.stringdata0)) return static_cast(this); - if (!strcmp(_clname, "YogaWidget")) - return static_cast< YogaWidget*>(this); - if (!strcmp(_clname, "EventWidget")) - return static_cast< EventWidget*>(this); + if (!strcmp(_clname, "NodeWidget")) + return static_cast< NodeWidget*>(this); return QProgressBar::qt_metacast(_clname); } diff --git a/src/cpp/autogen/npushbutton_moc.cpp b/src/cpp/autogen/npushbutton_moc.cpp index 2bc4d8315..edd37858a 100644 --- a/src/cpp/autogen/npushbutton_moc.cpp +++ b/src/cpp/autogen/npushbutton_moc.cpp @@ -292,10 +292,8 @@ void *NPushButton::qt_metacast(const char *_clname) if (!_clname) return nullptr; if (!strcmp(_clname, qt_meta_stringdata_NPushButton.stringdata0)) return static_cast(this); - if (!strcmp(_clname, "YogaWidget")) - return static_cast< YogaWidget*>(this); - if (!strcmp(_clname, "EventWidget")) - return static_cast< EventWidget*>(this); + if (!strcmp(_clname, "NodeWidget")) + return static_cast< NodeWidget*>(this); return QPushButton::qt_metacast(_clname); } diff --git a/src/cpp/autogen/nradiobutton_moc.cpp b/src/cpp/autogen/nradiobutton_moc.cpp index ad044e986..3ac3c521e 100644 --- a/src/cpp/autogen/nradiobutton_moc.cpp +++ b/src/cpp/autogen/nradiobutton_moc.cpp @@ -292,10 +292,8 @@ void *NRadioButton::qt_metacast(const char *_clname) if (!_clname) return nullptr; if (!strcmp(_clname, qt_meta_stringdata_NRadioButton.stringdata0)) return static_cast(this); - if (!strcmp(_clname, "YogaWidget")) - return static_cast< YogaWidget*>(this); - if (!strcmp(_clname, "EventWidget")) - return static_cast< EventWidget*>(this); + if (!strcmp(_clname, "NodeWidget")) + return static_cast< NodeWidget*>(this); return QRadioButton::qt_metacast(_clname); } diff --git a/src/cpp/core/NodeWidget/nodewidget.h b/src/cpp/core/NodeWidget/nodewidget.h new file mode 100644 index 000000000..1eb78081f --- /dev/null +++ b/src/cpp/core/NodeWidget/nodewidget.h @@ -0,0 +1,25 @@ +#pragma once +#include "src/cpp/core/YogaWidget/yogawidget.h" +#include "src/cpp/core/Events/eventwidget.h" + +// class to unify all the custom features + add extra features if needed +class NodeWidget : public YogaWidget, public EventWidget { + +}; + + + +#ifndef NODEWIDGET_IMPLEMENTATIONS +#define NODEWIDGET_IMPLEMENTATIONS \ +\ +Q_OBJECT \ +public: \ + SET_YOGA_WIDGET_Q_PROPERTIES \ + bool event(QEvent* event) { \ + EventWidget::event(event); \ + return QWidget::event(event); \ + } \ + +#endif //NODEWIDGET_IMPLEMENTATIONS + + \ No newline at end of file