From 3fe22cdc12bfb8794fd7cccddea53d2d5cb39528 Mon Sep 17 00:00:00 2001 From: Atul R Date: Wed, 12 Jun 2019 20:24:57 +0200 Subject: [PATCH] not needed to include yogawidget macro separately. --- ...ndling.md => signal_and_event_handling.md} | 7 ++++++ src/cpp/QtGui/QWidget/qwidget_macro.h | 9 +++---- src/cpp/QtGui/QWidget/qwidget_wrap.cpp | 1 - src/cpp/QtGui/QWidget/qwidget_wrap.h | 3 +-- src/cpp/QtWidgets/QCheckBox/qcheckbox_wrap.h | 3 +-- src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp | 1 - src/cpp/QtWidgets/QLabel/qlabel_wrap.h | 3 +-- .../QtWidgets/QLineEdit/qlineedit_wrap.cpp | 1 - src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h | 3 +-- src/cpp/QtWidgets/QMainWindow/nmainwindow.h | 24 +++++++++---------- .../QMainWindow/qmainwindow_wrap.cpp | 1 - .../QtWidgets/QMainWindow/qmainwindow_wrap.h | 3 +-- .../QProgressBar/qprogressbar_wrap.cpp | 1 - .../QProgressBar/qprogressbar_wrap.h | 2 -- .../QPushButton/qpushbutton_wrap.cpp | 1 - .../QtWidgets/QPushButton/qpushbutton_wrap.h | 3 +-- .../QRadioButton/qradiobutton_wrap.cpp | 1 - .../QRadioButton/qradiobutton_wrap.h | 6 ++--- src/cpp/core/Events/eventsmap.h | 0 src/cpp/core/YogaWidget/nodestyle.h | 5 ++-- 20 files changed, 34 insertions(+), 44 deletions(-) rename devdocs/{signal_handling.md => signal_and_event_handling.md} (82%) create mode 100644 src/cpp/core/Events/eventsmap.h diff --git a/devdocs/signal_handling.md b/devdocs/signal_and_event_handling.md similarity index 82% rename from devdocs/signal_handling.md rename to devdocs/signal_and_event_handling.md index d7e2003d3..3d6c6c92b 100644 --- a/devdocs/signal_handling.md +++ b/devdocs/signal_and_event_handling.md @@ -1,3 +1,10 @@ +In Qt you can respond to an external event like a key press via event handling. Events always are processed by the event loop. Alongside events Qt also has a concept of Signals/Slots. Signals and slots are used to primarily communicate between widgets (more precisely QObjects). So the most common way of interacting between Qt Widgets is done through signals/slots. (More details here: https://doc.qt.io/qt-5/signalsandslots.html). Hence we would be implementing support for both events and signals. + +**Technicals:** + +> An event is a message encapsulated in a class (QEvent) which is processed in an event loop and dispatched to a recipient that can either accept the message or pass it along to others to process. They are usually created in response to external system events like mouse clicks. +> Signals and Slots are a convenient way for QObjects to communicate with one another and are more similar to callback functions. In most circumstances, when a "signal" is emitted, any slot function connected to it is called directly. The exception is when signals and slots cross thread boundaries. In this case, the signal will essentially be converted into an event. + # Implementing Signal handling In Qt signals and slots are used to communicate between different qt widgets. So they can be used to implement things like diff --git a/src/cpp/QtGui/QWidget/qwidget_macro.h b/src/cpp/QtGui/QWidget/qwidget_macro.h index 722c420a9..a61c7b1e4 100644 --- a/src/cpp/QtGui/QWidget/qwidget_macro.h +++ b/src/cpp/QtGui/QWidget/qwidget_macro.h @@ -1,8 +1,7 @@ -#ifndef QWIDGET_MACRO_H -#define QWIDGET_MACRO_H +#pragma once #include "src/cpp/QtWidgets/QLayout/qlayout_wrap.h" - +#include "src/cpp/core/YogaWidget/yogawidget_macro.h" /* This macro adds common QWidgets exported methods @@ -12,6 +11,8 @@ #ifndef QWIDGET_WRAPPED_METHODS_DECLARATION #define QWIDGET_WRAPPED_METHODS_DECLARATION \ \ +YOGAWIDGET_WRAPPED_METHODS_DECLARATION \ +\ Napi::Value show(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ Napi::HandleScope scope(env); \ @@ -73,6 +74,7 @@ Napi::Value setObjectName(const Napi::CallbackInfo& info){ \ #ifndef QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE #define QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ \ + YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ InstanceMethod("show", &WidgetWrapName::show), \ InstanceMethod("resize",&WidgetWrapName::resize), \ InstanceMethod("close",&WidgetWrapName::close), \ @@ -86,4 +88,3 @@ Napi::Value setObjectName(const Napi::CallbackInfo& info){ \ -#endif // QWIDGET_MACRO_H \ No newline at end of file diff --git a/src/cpp/QtGui/QWidget/qwidget_wrap.cpp b/src/cpp/QtGui/QWidget/qwidget_wrap.cpp index fd13418bd..87be350c3 100644 --- a/src/cpp/QtGui/QWidget/qwidget_wrap.cpp +++ b/src/cpp/QtGui/QWidget/qwidget_wrap.cpp @@ -9,7 +9,6 @@ Napi::Object QWidgetWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QWidget"; Napi::Function func = DefineClass(env, CLASSNAME, { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QWidgetWrap) - YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QWidgetWrap) }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/cpp/QtGui/QWidget/qwidget_wrap.h b/src/cpp/QtGui/QWidget/qwidget_wrap.h index 7b61a83ed..7735e53d8 100644 --- a/src/cpp/QtGui/QWidget/qwidget_wrap.h +++ b/src/cpp/QtGui/QWidget/qwidget_wrap.h @@ -1,7 +1,7 @@ #pragma once #include "src/cpp/QtGui/QWidget/qwidget_macro.h" -#include "src/cpp/core/YogaWidget/yogawidget_macro.h" + #include #include "nwidget.h" @@ -17,6 +17,5 @@ class QWidgetWrap : public Napi::ObjectWrap{ static Napi::FunctionReference constructor; //wrapped methods QWIDGET_WRAPPED_METHODS_DECLARATION - YOGAWIDGET_WRAPPED_METHODS_DECLARATION }; diff --git a/src/cpp/QtWidgets/QCheckBox/qcheckbox_wrap.h b/src/cpp/QtWidgets/QCheckBox/qcheckbox_wrap.h index 826618334..a7be7e72e 100644 --- a/src/cpp/QtWidgets/QCheckBox/qcheckbox_wrap.h +++ b/src/cpp/QtWidgets/QCheckBox/qcheckbox_wrap.h @@ -3,7 +3,7 @@ #include #include "ncheckbox.h" #include "src/cpp/QtGui/QWidget/qwidget_macro.h" -#include "src/cpp/core/YogaWidget/yogawidget_macro.h" + class QCheckBoxWrap : public Napi::ObjectWrap{ private: @@ -19,6 +19,5 @@ class QCheckBoxWrap : public Napi::ObjectWrap{ Napi::Value setText(const Napi::CallbackInfo& info); QWIDGET_WRAPPED_METHODS_DECLARATION - YOGAWIDGET_WRAPPED_METHODS_DECLARATION }; diff --git a/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp b/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp index 4f121e021..e19de50be 100644 --- a/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp +++ b/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp @@ -15,7 +15,6 @@ Napi::Object QLabelWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("text", &QLabelWrap::text), InstanceMethod("getFlexNode", &QLabelWrap::getFlexNode), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLabelWrap) - YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLabelWrap) }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/cpp/QtWidgets/QLabel/qlabel_wrap.h b/src/cpp/QtWidgets/QLabel/qlabel_wrap.h index c0b6af3a8..5c8d40589 100644 --- a/src/cpp/QtWidgets/QLabel/qlabel_wrap.h +++ b/src/cpp/QtWidgets/QLabel/qlabel_wrap.h @@ -3,7 +3,7 @@ #include #include "nlabel.h" #include "src/cpp/QtGui/QWidget/qwidget_macro.h" -#include "src/cpp/core/YogaWidget/yogawidget_macro.h" + class QLabelWrap : public Napi::ObjectWrap{ private: @@ -21,7 +21,6 @@ class QLabelWrap : public Napi::ObjectWrap{ Napi::Value text(const Napi::CallbackInfo &info); QWIDGET_WRAPPED_METHODS_DECLARATION - YOGAWIDGET_WRAPPED_METHODS_DECLARATION }; diff --git a/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.cpp b/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.cpp index 7d21a6032..3beabfc10 100644 --- a/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.cpp +++ b/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.cpp @@ -12,7 +12,6 @@ Napi::Object QLineEditWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QLineEdit"; Napi::Function func = DefineClass(env, CLASSNAME, { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLineEditWrap) - YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLineEditWrap) }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h b/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h index abb3e5257..18f8d9401 100644 --- a/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h +++ b/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h @@ -3,7 +3,7 @@ #include #include "nlineedit.h" #include "src/cpp/QtGui/QWidget/qwidget_macro.h" -#include "src/cpp/core/YogaWidget/yogawidget_macro.h" + class QLineEditWrap : public Napi::ObjectWrap{ private: @@ -18,7 +18,6 @@ class QLineEditWrap : public Napi::ObjectWrap{ //wrapped methods QWIDGET_WRAPPED_METHODS_DECLARATION - YOGAWIDGET_WRAPPED_METHODS_DECLARATION }; diff --git a/src/cpp/QtWidgets/QMainWindow/nmainwindow.h b/src/cpp/QtWidgets/QMainWindow/nmainwindow.h index fa2f01be9..e5334abfb 100644 --- a/src/cpp/QtWidgets/QMainWindow/nmainwindow.h +++ b/src/cpp/QtWidgets/QMainWindow/nmainwindow.h @@ -9,28 +9,28 @@ class NMainWindow: public QMainWindow, public YogaWidget { -public: - SET_YOGA_WIDGET_Q_PROPERTIES - using QMainWindow::QMainWindow; //inherit all constructors of QMainWindow - +private: void calculateLayout(){ YGDirection direction = YGNodeStyleGetDirection(this->getFlexNode()); YGNodeCalculateLayout(this->getFlexNode(),width(),height(),direction); } - - Q_OBJECT -public: - bool eventFilter(QObject *object, QEvent *event) - { - if (event->type() == QEvent::LayoutRequest || event->type() == QEvent::ChildRemoved) { - calculateLayout(); + bool eventFilter(QObject *object, QEvent *event) { // This will be installed on mainwidgetwrap + switch(event->type()) { + case QEvent::LayoutRequest: + case QEvent::ChildRemoved: { + calculateLayout(); break; + } + default: ; // do nothing } return false; } 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/QMainWindow/qmainwindow_wrap.cpp b/src/cpp/QtWidgets/QMainWindow/qmainwindow_wrap.cpp index c6e83596b..d6f94be66 100644 --- a/src/cpp/QtWidgets/QMainWindow/qmainwindow_wrap.cpp +++ b/src/cpp/QtWidgets/QMainWindow/qmainwindow_wrap.cpp @@ -9,7 +9,6 @@ Napi::Object QMainWindowWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QMainWindow"; Napi::Function func = DefineClass(env, CLASSNAME, { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMainWindowWrap) - YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMainWindowWrap) InstanceMethod("setCentralWidget",&QMainWindowWrap::setCentralWidget), InstanceMethod("setFixedSize",&QMainWindowWrap::setFixedSize), }); diff --git a/src/cpp/QtWidgets/QMainWindow/qmainwindow_wrap.h b/src/cpp/QtWidgets/QMainWindow/qmainwindow_wrap.h index c7e003e8d..6c9e64013 100644 --- a/src/cpp/QtWidgets/QMainWindow/qmainwindow_wrap.h +++ b/src/cpp/QtWidgets/QMainWindow/qmainwindow_wrap.h @@ -3,7 +3,7 @@ #include #include "nmainwindow.h" #include "src/cpp/QtGui/QWidget/qwidget_macro.h" -#include "src/cpp/core/YogaWidget/yogawidget_macro.h" + class QMainWindowWrap : public Napi::ObjectWrap{ private: @@ -21,6 +21,5 @@ public: Napi::Value setFixedSize(const Napi::CallbackInfo& info); QWIDGET_WRAPPED_METHODS_DECLARATION - YOGAWIDGET_WRAPPED_METHODS_DECLARATION }; diff --git a/src/cpp/QtWidgets/QProgressBar/qprogressbar_wrap.cpp b/src/cpp/QtWidgets/QProgressBar/qprogressbar_wrap.cpp index 2a40d328e..3f7b51085 100644 --- a/src/cpp/QtWidgets/QProgressBar/qprogressbar_wrap.cpp +++ b/src/cpp/QtWidgets/QProgressBar/qprogressbar_wrap.cpp @@ -12,7 +12,6 @@ Napi::Object QProgressBarWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QProgressBar"; Napi::Function func = DefineClass(env, CLASSNAME, { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QProgressBarWrap) - YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QProgressBarWrap) }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/cpp/QtWidgets/QProgressBar/qprogressbar_wrap.h b/src/cpp/QtWidgets/QProgressBar/qprogressbar_wrap.h index ce896bcc4..8fb6f02f9 100644 --- a/src/cpp/QtWidgets/QProgressBar/qprogressbar_wrap.h +++ b/src/cpp/QtWidgets/QProgressBar/qprogressbar_wrap.h @@ -3,7 +3,6 @@ #include #include "nprogressbar.h" #include "src/cpp/QtGui/QWidget/qwidget_macro.h" -#include "src/cpp/core/YogaWidget/yogawidget_macro.h" class QProgressBarWrap : public Napi::ObjectWrap{ private: @@ -18,7 +17,6 @@ class QProgressBarWrap : public Napi::ObjectWrap{ //wrapped methods QWIDGET_WRAPPED_METHODS_DECLARATION - YOGAWIDGET_WRAPPED_METHODS_DECLARATION }; diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp index 35b1152e7..2f588d6d4 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp @@ -11,7 +11,6 @@ Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("setText", &QPushButtonWrap::setText), InstanceMethod("setupSignalListeners",&QPushButtonWrap::setupSignalListeners), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPushButtonWrap) - YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPushButtonWrap) }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h index da77bcee8..3f164706b 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h @@ -4,7 +4,7 @@ #include #include "npushbutton.h" #include "src/cpp/QtGui/QWidget/qwidget_macro.h" -#include "src/cpp/core/YogaWidget/yogawidget_macro.h" + #include "src/cpp/Extras/Utils/utils.h" class QPushButtonWrap : public Napi::ObjectWrap { @@ -23,7 +23,6 @@ class QPushButtonWrap : public Napi::ObjectWrap { Napi::Value setText(const Napi::CallbackInfo& info); QWIDGET_WRAPPED_METHODS_DECLARATION - YOGAWIDGET_WRAPPED_METHODS_DECLARATION }; diff --git a/src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.cpp b/src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.cpp index aba7b516f..0baf63c83 100644 --- a/src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.cpp +++ b/src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.cpp @@ -12,7 +12,6 @@ Napi::Object QRadioButtonWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QRadioButton"; Napi::Function func = DefineClass(env, CLASSNAME, { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QRadioButtonWrap) - YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QRadioButtonWrap) }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.h b/src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.h index c69260d75..49fa54f16 100644 --- a/src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.h +++ b/src/cpp/QtWidgets/QRadioButton/qradiobutton_wrap.h @@ -3,7 +3,7 @@ #include #include "nradiobutton.h" #include "src/cpp/QtGui/QWidget/qwidget_macro.h" -#include "src/cpp/core/YogaWidget/yogawidget_macro.h" + class QRadioButtonWrap : public Napi::ObjectWrap{ private: @@ -17,9 +17,7 @@ class QRadioButtonWrap : public Napi::ObjectWrap{ static Napi::FunctionReference constructor; //wrapped methods - QWIDGET_WRAPPED_METHODS_DECLARATION - YOGAWIDGET_WRAPPED_METHODS_DECLARATION - + QWIDGET_WRAPPED_METHODS_DECLARATION }; \ No newline at end of file diff --git a/src/cpp/core/Events/eventsmap.h b/src/cpp/core/Events/eventsmap.h new file mode 100644 index 000000000..e69de29bb diff --git a/src/cpp/core/YogaWidget/nodestyle.h b/src/cpp/core/YogaWidget/nodestyle.h index 7b7c2a5a5..39437bb68 100644 --- a/src/cpp/core/YogaWidget/nodestyle.h +++ b/src/cpp/core/YogaWidget/nodestyle.h @@ -1,5 +1,4 @@ -#ifndef NODESTYLE_H -#define NODESTYLE_H +#pragma once #include #include @@ -43,4 +42,4 @@ static std::unordered_map NodeWrap; static NodeValueUnit parseMeasurement(QString rawValue); }; -#endif // NODESTYLE_H +