diff --git a/src/cpp/include/nodegui/Extras/Utils/nutils.h b/src/cpp/include/nodegui/Extras/Utils/nutils.h index 5142443ba..e7b54d11e 100644 --- a/src/cpp/include/nodegui/Extras/Utils/nutils.h +++ b/src/cpp/include/nodegui/Extras/Utils/nutils.h @@ -13,8 +13,7 @@ namespace extrautils { DLL_EXPORT QVariant* convertToQVariant(Napi::Env& env, Napi::Value& value); DLL_EXPORT bool isNapiValueInt(Napi::Env& env, Napi::Value& num); DLL_EXPORT std::string getNapiObjectClassName(Napi::Object& object); -DLL_EXPORT void* configureQWidget(QWidget* widget, YGNodeRef node, - bool isLeafNode = false); +DLL_EXPORT void* configureQWidget(QWidget* widget, bool isLeafNode = false); DLL_EXPORT void* configureQObject(QObject* object); DLL_EXPORT void* configureComponent(void* component); DLL_EXPORT uint64_t hashPointerTo53bit(const void* input); diff --git a/src/cpp/include/nodegui/QtCore/QAbstractItemModel/nabstractitemmodel.hpp b/src/cpp/include/nodegui/QtCore/QAbstractItemModel/nabstractitemmodel.hpp index cd7c818af..e5e9d4095 100644 --- a/src/cpp/include/nodegui/QtCore/QAbstractItemModel/nabstractitemmodel.hpp +++ b/src/cpp/include/nodegui/QtCore/QAbstractItemModel/nabstractitemmodel.hpp @@ -35,6 +35,8 @@ class DLL_EXPORT NAbstractItemModel : public QAbstractItemModel, return *newIndex; } + QObject* parent() const { return nullptr; } + QModelIndex parent(const QModelIndex& child) const override { Napi::Env env = this->dispatchOnNode.Env(); Napi::HandleScope scope(env); diff --git a/src/cpp/include/nodegui/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h b/src/cpp/include/nodegui/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h index 3f4ac5101..3bf7883ae 100644 --- a/src/cpp/include/nodegui/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h +++ b/src/cpp/include/nodegui/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h @@ -14,7 +14,6 @@ class DLL_EXPORT QItemSelectionModelWrap private: QPointer instance; - bool disableDeletion; public: static Napi::Object init(Napi::Env env, Napi::Object exports); diff --git a/src/cpp/include/nodegui/QtCore/QMimeData/qmimedata_wrap.h b/src/cpp/include/nodegui/QtCore/QMimeData/qmimedata_wrap.h index e87b1d718..ca42191e0 100644 --- a/src/cpp/include/nodegui/QtCore/QMimeData/qmimedata_wrap.h +++ b/src/cpp/include/nodegui/QtCore/QMimeData/qmimedata_wrap.h @@ -5,13 +5,13 @@ #include #include "Extras/Export/export.h" -#include "core/Component/component_macro.h" +#include "QtCore/QObject/qobject_macro.h" class DLL_EXPORT QMimeDataWrap : public Napi::ObjectWrap { - COMPONENT_WRAPPED_METHODS_DECLARATION + QOBJECT_WRAPPED_METHODS_DECLARATION private: - std::unique_ptr instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); diff --git a/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h b/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h index 83880d5b7..8b745f0f3 100644 --- a/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h +++ b/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h @@ -5,11 +5,12 @@ #include "Extras/Utils/nutils.h" #include "QtCore/QVariant/qvariant_wrap.h" #include "core/Events/eventwidget_macro.h" -/* +#include "core/WrapperCache/wrappercache.h" - This macro adds common QObject exported methods - The exported methods are taken into this macro to avoid writing them in each - and every widget we export. +/* + This macro adds common QObject exported methods + The exported methods are taken into this macro to avoid writing them in each + and every widget we export. */ #ifndef QOBJECT_WRAPPED_METHODS_DECLARATION_WITH_EVENT_SOURCE @@ -20,7 +21,8 @@ Napi::Value __id__(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ return Napi::Value::From( \ - env, extrautils::hashPointerTo53bit(this->instance.data())); \ + env, extrautils::hashPointerTo53bit( \ + static_cast(this->instance.data()))); \ } \ Napi::Value inherits(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ @@ -88,6 +90,35 @@ int id = info[0].As().Int32Value(); \ this->instance->killTimer(id); \ return env.Null(); \ + } \ + Napi::Value parent(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + QObject* parent = this->instance->parent(); \ + if (parent) { \ + return WrapperCache::instance.getWrapper(env, parent); \ + } else { \ + return env.Null(); \ + } \ + } \ + Napi::Value deleteLater(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + this->instance->deleteLater(); \ + return env.Null(); \ + } \ + Napi::Value deleteObject(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + delete static_cast(this->instance); \ + return env.Null(); \ + } \ + Napi::Value children(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + QObjectList children = this->instance->children(); \ + Napi::Array resultArrayNapi = Napi::Array::New(env, children.size()); \ + for (int i = 0; i < children.size(); i++) { \ + resultArrayNapi[i] = \ + WrapperCache::instance.getWrapper(env, children[i]); \ + } \ + return resultArrayNapi; \ } // Ideally this macro below should go in @@ -131,7 +162,11 @@ InstanceMethod("dumpObjectInfo", &ComponentWrapName::dumpObjectInfo), \ InstanceMethod("setParent", &ComponentWrapName::setParent), \ InstanceMethod("startTimer", &ComponentWrapName::startTimer), \ - InstanceMethod("killTimer", &ComponentWrapName::killTimer), + InstanceMethod("killTimer", &ComponentWrapName::killTimer), \ + InstanceMethod("parent", &ComponentWrapName::parent), \ + InstanceMethod("deleteLater", &ComponentWrapName::deleteLater), \ + InstanceMethod("delete", &ComponentWrapName::deleteObject), \ + InstanceMethod("children", &ComponentWrapName::children), #endif // QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE @@ -151,4 +186,22 @@ #define QOBJECT_SIGNALS QOBJECT_SIGNALS_ON_TARGET(this) #endif // QOBJECT_SIGNALS +/* + Macro to register a function to wrap QObject pointers of a + given subclass to wrapper instances. First parameter is the + plain name of the QObject subclass (no quotes), seconds is the + name of the wrapper class. + */ +#ifndef QOBJECT_REGISTER_WRAPPER +#define QOBJECT_REGISTER_WRAPPER(qobjectType, ComponentWrapName) \ + WrapperCache::instance.registerWrapper( \ + QString(#qobjectType), \ + [](Napi::Env env, QObject* qobject) -> Napi::Object { \ + qobjectType* exactQObject = dynamic_cast(qobject); \ + Napi::Object wrapper = ComponentWrapName::constructor.New( \ + {Napi::External::New(env, exactQObject)}); \ + return wrapper; \ + }); +#endif // QOBJECT_REGISTER_WRAPPER + #include "QtCore/QObject/qobject_wrap.h" diff --git a/src/cpp/include/nodegui/QtCore/QObject/qobject_wrap.h b/src/cpp/include/nodegui/QtCore/QObject/qobject_wrap.h index a0129e050..0aca30b2e 100644 --- a/src/cpp/include/nodegui/QtCore/QObject/qobject_wrap.h +++ b/src/cpp/include/nodegui/QtCore/QObject/qobject_wrap.h @@ -11,14 +11,15 @@ class DLL_EXPORT QObjectWrap : public Napi::ObjectWrap { QOBJECT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QObjectWrap(const Napi::CallbackInfo& info); ~QObjectWrap(); - NObject* getInternalInstance(); + QObject* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; + static Napi::Object wrapFunc(Napi::Env env, QObject* qobject); // wrapped methods }; diff --git a/src/cpp/include/nodegui/QtGui/QApplication/napplication.hpp b/src/cpp/include/nodegui/QtGui/QApplication/napplication.hpp index 3d051945c..4e58fd592 100644 --- a/src/cpp/include/nodegui/QtGui/QApplication/napplication.hpp +++ b/src/cpp/include/nodegui/QtGui/QApplication/napplication.hpp @@ -29,8 +29,7 @@ class DLL_EXPORT NApplication : public QApplication, public EventWidget { this, &QGuiApplication::primaryScreenChanged, [=](QScreen* screen) { Napi::Env env = this->emitOnNode.Env(); Napi::HandleScope scope(env); - auto instance = - WrapperCache::instance.get(env, screen); + auto instance = WrapperCache::instance.getWrapper(env, screen, true); this->emitOnNode.Call( {Napi::String::New(env, "primaryScreenChanged"), instance}); }); @@ -38,8 +37,7 @@ class DLL_EXPORT NApplication : public QApplication, public EventWidget { QObject::connect(this, &QGuiApplication::screenAdded, [=](QScreen* screen) { Napi::Env env = this->emitOnNode.Env(); Napi::HandleScope scope(env); - auto instance = - WrapperCache::instance.get(env, screen); + auto instance = WrapperCache::instance.getWrapper(env, screen, true); this->emitOnNode.Call({Napi::String::New(env, "screenAdded"), instance}); }); @@ -47,8 +45,7 @@ class DLL_EXPORT NApplication : public QApplication, public EventWidget { this, &QGuiApplication::screenRemoved, [=](QScreen* screen) { Napi::Env env = this->emitOnNode.Env(); Napi::HandleScope scope(env); - auto instance = - WrapperCache::instance.get(env, screen); + auto instance = WrapperCache::instance.getWrapper(env, screen, true); this->emitOnNode.Call( {Napi::String::New(env, "screenRemoved"), instance}); }); diff --git a/src/cpp/include/nodegui/QtGui/QDrag/qdrag_wrap.h b/src/cpp/include/nodegui/QtGui/QDrag/qdrag_wrap.h index da688bd08..39d17f2ae 100644 --- a/src/cpp/include/nodegui/QtGui/QDrag/qdrag_wrap.h +++ b/src/cpp/include/nodegui/QtGui/QDrag/qdrag_wrap.h @@ -22,13 +22,13 @@ class DLL_EXPORT QDragWrap : public Napi::ObjectWrap { // except that it is automatically cleared when the referenced object is // destroyed (unlike normal C++ pointers, which become "dangling pointers" in // such cases). T must be a subclass of QObject. - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QDragWrap(const Napi::CallbackInfo& info); ~QDragWrap(); - NDrag* getInternalInstance(); + QDrag* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; diff --git a/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h b/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h index 64d71e353..9e6781716 100644 --- a/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h @@ -145,9 +145,7 @@ Napi::Value selectionModel(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ QItemSelectionModel* model = this->instance->selectionModel(); \ - auto modelExt = Napi::External::New(env, model); \ - auto instance = QItemSelectionModelWrap::constructor.New({modelExt}); \ - return instance; \ + return WrapperCache::instance.getWrapper(env, model); \ } \ Napi::Value isPersistentEditorOpen(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ diff --git a/src/cpp/include/nodegui/QtWidgets/QAbstractScrollArea/qabstractscrollarea_macro.h b/src/cpp/include/nodegui/QtWidgets/QAbstractScrollArea/qabstractscrollarea_macro.h index 9bf5d3309..9bb8a13e8 100644 --- a/src/cpp/include/nodegui/QtWidgets/QAbstractScrollArea/qabstractscrollarea_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QAbstractScrollArea/qabstractscrollarea_macro.h @@ -58,9 +58,7 @@ Napi::Value viewport(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ QWidget* viewPort = this->instance->viewport(); \ - NWidget* nviewPort = reinterpret_cast(viewPort); \ - auto instance = QWidgetWrap::constructor.New( \ - {Napi::External::New(env, nviewPort)}); \ + auto instance = WrapperCache::instance.getWrapper(env, viewPort); \ return instance; \ } diff --git a/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h b/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h index d4d118b77..d0b382a11 100644 --- a/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h @@ -11,14 +11,13 @@ class DLL_EXPORT QActionWrap : public Napi::ObjectWrap { QOBJECT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; - bool disableDeletion; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QActionWrap(const Napi::CallbackInfo& info); ~QActionWrap(); - NAction* getInternalInstance(); + QAction* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QBoxLayout/qboxlayout_wrap.h b/src/cpp/include/nodegui/QtWidgets/QBoxLayout/qboxlayout_wrap.h index 09a6646f7..290b02108 100644 --- a/src/cpp/include/nodegui/QtWidgets/QBoxLayout/qboxlayout_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QBoxLayout/qboxlayout_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QBoxLayoutWrap : public Napi::ObjectWrap { QLAYOUT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QBoxLayoutWrap(const Napi::CallbackInfo& info); ~QBoxLayoutWrap(); - NBoxLayout* getInternalInstance(); + QBoxLayout* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QButtonGroup/qbuttongroup_wrap.h b/src/cpp/include/nodegui/QtWidgets/QButtonGroup/qbuttongroup_wrap.h index 1f79b7722..5100dff6e 100644 --- a/src/cpp/include/nodegui/QtWidgets/QButtonGroup/qbuttongroup_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QButtonGroup/qbuttongroup_wrap.h @@ -9,13 +9,13 @@ #include "nbuttongroup.hpp" class DLL_EXPORT QButtonGroupWrap : public Napi::ObjectWrap { private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QButtonGroupWrap(const Napi::CallbackInfo& info); ~QButtonGroupWrap(); - NButtonGroup* getInternalInstance(); + QButtonGroup* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QCalendarWidget/qcalendarwidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QCalendarWidget/qcalendarwidget_wrap.h index 30cdd66cd..cf005eaac 100644 --- a/src/cpp/include/nodegui/QtWidgets/QCalendarWidget/qcalendarwidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QCalendarWidget/qcalendarwidget_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QCalendarWidgetWrap : public Napi::ObjectWrap { private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QCalendarWidgetWrap(const Napi::CallbackInfo &info); ~QCalendarWidgetWrap(); - NCalendarWidget *getInternalInstance(); + QCalendarWidget *getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QCheckBox/qcheckbox_wrap.h b/src/cpp/include/nodegui/QtWidgets/QCheckBox/qcheckbox_wrap.h index 67809fe2d..b15ae5f3a 100644 --- a/src/cpp/include/nodegui/QtWidgets/QCheckBox/qcheckbox_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QCheckBox/qcheckbox_wrap.h @@ -12,14 +12,13 @@ class DLL_EXPORT QCheckBoxWrap : public Napi::ObjectWrap { QABSTRACTBUTTON_WRAPPED_METHODS_DECLARATION private: - QPointer instance; - bool disableDeletion; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QCheckBoxWrap(const Napi::CallbackInfo& info); ~QCheckBoxWrap(); - NCheckBox* getInternalInstance(); + QCheckBox* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QColorDialog/qcolordialog_wrap.h b/src/cpp/include/nodegui/QtWidgets/QColorDialog/qcolordialog_wrap.h index 25a6479c4..968e5cca3 100644 --- a/src/cpp/include/nodegui/QtWidgets/QColorDialog/qcolordialog_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QColorDialog/qcolordialog_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QColorDialogWrap : public Napi::ObjectWrap { QDIALOG_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QColorDialogWrap(const Napi::CallbackInfo& info); ~QColorDialogWrap(); - NColorDialog* getInternalInstance(); + QColorDialog* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QComboBox/qcombobox_wrap.h b/src/cpp/include/nodegui/QtWidgets/QComboBox/qcombobox_wrap.h index c739d5046..e5ed8407a 100644 --- a/src/cpp/include/nodegui/QtWidgets/QComboBox/qcombobox_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QComboBox/qcombobox_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QComboBoxWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QComboBoxWrap(const Napi::CallbackInfo& info); ~QComboBoxWrap(); - NComboBox* getInternalInstance(); + QComboBox* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QDateEdit/qdateedit_wrap.h b/src/cpp/include/nodegui/QtWidgets/QDateEdit/qdateedit_wrap.h index 37d09271e..9498d049f 100644 --- a/src/cpp/include/nodegui/QtWidgets/QDateEdit/qdateedit_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QDateEdit/qdateedit_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QDateEditWrap : public Napi::ObjectWrap { QDATETIMEEDIT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QDateEditWrap(const Napi::CallbackInfo &info); ~QDateEditWrap(); - NDateEdit *getInternalInstance(); + QDateEdit *getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/qdatetimeedit_macro.h b/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/qdatetimeedit_macro.h index c96c1cb2c..06180553c 100644 --- a/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/qdatetimeedit_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/qdatetimeedit_macro.h @@ -24,14 +24,24 @@ this->instance->setCalendarWidget( \ calendarWidgetWrap->getInternalInstance()); \ return env.Null(); \ + } \ + Napi::Value calendarWidget(const Napi::CallbackInfo &info) { \ + Napi::Env env = info.Env(); \ + QObject *calendarWidget = this->instance->calendarWidget(); \ + if (calendarWidget) { \ + return WrapperCache::instance.getWrapper(env, calendarWidget); \ + } else { \ + return env.Null(); \ + } \ } #endif // QDATETIMEEDIT_WRAPPED_METHODS_DECLARATION #ifndef QDATETIMEEDIT_WRAPPED_METHODS_EXPORT_DEFINE -#define QDATETIMEEDIT_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ - QABSTRACTSPINBOX_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ - InstanceMethod("setCalendarWidget", &WidgetWrapName::setCalendarWidget), +#define QDATETIMEEDIT_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ + QABSTRACTSPINBOX_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ + InstanceMethod("setCalendarWidget", &WidgetWrapName::setCalendarWidget), \ + InstanceMethod("calendarWidget", &WidgetWrapName::calendarWidget), #endif // QDATETIMEEDIT_WRAPPED_METHODS_EXPORT_DEFINE diff --git a/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.h b/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.h index 7f6ffcc83..c994acdf7 100644 --- a/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QDateTimeEditWrap : public Napi::ObjectWrap { QDATETIMEEDIT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QDateTimeEditWrap(const Napi::CallbackInfo &info); ~QDateTimeEditWrap(); - NDateTimeEdit *getInternalInstance(); + QDateTimeEdit *getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QDial/qdial_wrap.h b/src/cpp/include/nodegui/QtWidgets/QDial/qdial_wrap.h index f6fa9c4e6..d7e010fb2 100644 --- a/src/cpp/include/nodegui/QtWidgets/QDial/qdial_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QDial/qdial_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QDialWrap : public Napi::ObjectWrap { QABSTRACTSLIDER_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QDialWrap(const Napi::CallbackInfo& info); ~QDialWrap(); - NDial* getInternalInstance(); + QDial* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QDialog/qdialog_wrap.h b/src/cpp/include/nodegui/QtWidgets/QDialog/qdialog_wrap.h index 374ed1d96..5a144e8df 100644 --- a/src/cpp/include/nodegui/QtWidgets/QDialog/qdialog_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QDialog/qdialog_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QDialogWrap : public Napi::ObjectWrap { QDIALOG_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QDialogWrap(const Napi::CallbackInfo& info); ~QDialogWrap(); - NDialog* getInternalInstance(); + QDialog* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.h b/src/cpp/include/nodegui/QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.h index 435aa3a2d..6d358bec3 100644 --- a/src/cpp/include/nodegui/QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QDoubleSpinBoxWrap : public Napi::ObjectWrap { QABSTRACTSPINBOX_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QDoubleSpinBoxWrap(const Napi::CallbackInfo& info); ~QDoubleSpinBoxWrap(); - NDoubleSpinBox* getInternalInstance(); + QDoubleSpinBox* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QErrorMessage/qerrormessage_wrap.h b/src/cpp/include/nodegui/QtWidgets/QErrorMessage/qerrormessage_wrap.h index c9fa3981c..a5d984e47 100644 --- a/src/cpp/include/nodegui/QtWidgets/QErrorMessage/qerrormessage_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QErrorMessage/qerrormessage_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QErrorMessageWrap : public Napi::ObjectWrap { QDIALOG_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QErrorMessageWrap(const Napi::CallbackInfo& info); ~QErrorMessageWrap(); - NErrorMessage* getInternalInstance(); + QErrorMessage* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QFileDialog/qfiledialog_wrap.h b/src/cpp/include/nodegui/QtWidgets/QFileDialog/qfiledialog_wrap.h index 85ad21697..9f90ef19a 100644 --- a/src/cpp/include/nodegui/QtWidgets/QFileDialog/qfiledialog_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QFileDialog/qfiledialog_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QFileDialogWrap : public Napi::ObjectWrap { QDIALOG_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QFileDialogWrap(const Napi::CallbackInfo& info); ~QFileDialogWrap(); - NFileDialog* getInternalInstance(); + QFileDialog* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QFontDialog/qfontdialog_wrap.h b/src/cpp/include/nodegui/QtWidgets/QFontDialog/qfontdialog_wrap.h index d6282f27f..c08b8e1ab 100644 --- a/src/cpp/include/nodegui/QtWidgets/QFontDialog/qfontdialog_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QFontDialog/qfontdialog_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QFontDialogWrap : public Napi::ObjectWrap { QDIALOG_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QFontDialogWrap(const Napi::CallbackInfo& info); ~QFontDialogWrap(); - NFontDialog* getInternalInstance(); + QFontDialog* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QFrame/qframe_wrap.h b/src/cpp/include/nodegui/QtWidgets/QFrame/qframe_wrap.h index cdf8a05ae..c0b705d2d 100644 --- a/src/cpp/include/nodegui/QtWidgets/QFrame/qframe_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QFrame/qframe_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QFrameWrap : public Napi::ObjectWrap { QFRAME_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QFrameWrap(const Napi::CallbackInfo& info); ~QFrameWrap(); - NFrame* getInternalInstance(); + QFrame* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QGraphicsBlurEffect/qgraphicsblureffect_wrap.h b/src/cpp/include/nodegui/QtWidgets/QGraphicsBlurEffect/qgraphicsblureffect_wrap.h index 483526296..8062ef33c 100644 --- a/src/cpp/include/nodegui/QtWidgets/QGraphicsBlurEffect/qgraphicsblureffect_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QGraphicsBlurEffect/qgraphicsblureffect_wrap.h @@ -5,6 +5,7 @@ #include #include "Extras/Export/export.h" +#include "Extras/Utils/nutils.h" #include "QtWidgets/QGraphicsEffect/qgraphicseffect_macro.h" #include "ngraphicsblureffect.hpp" @@ -12,13 +13,13 @@ class DLL_EXPORT QGraphicsBlurEffectWrap : public Napi::ObjectWrap { QGRAPHICSEFFECT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QGraphicsBlurEffectWrap(const Napi::CallbackInfo& info); ~QGraphicsBlurEffectWrap(); - NGraphicsBlurEffect* getInternalInstance(); + QGraphicsBlurEffect* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QGraphicsDropShadowEffect/qgraphicsdropshadoweffect_wrap.h b/src/cpp/include/nodegui/QtWidgets/QGraphicsDropShadowEffect/qgraphicsdropshadoweffect_wrap.h index 0eee0eaee..d6e3a3050 100644 --- a/src/cpp/include/nodegui/QtWidgets/QGraphicsDropShadowEffect/qgraphicsdropshadoweffect_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QGraphicsDropShadowEffect/qgraphicsdropshadoweffect_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QGraphicsDropShadowEffectWrap : public Napi::ObjectWrap { QGRAPHICSEFFECT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QGraphicsDropShadowEffectWrap(const Napi::CallbackInfo& info); ~QGraphicsDropShadowEffectWrap(); - NGraphicsDropShadowEffect* getInternalInstance(); + QGraphicsDropShadowEffect* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QGridLayout/qgridlayout_wrap.h b/src/cpp/include/nodegui/QtWidgets/QGridLayout/qgridlayout_wrap.h index 7d6df8112..d4e8f4cc6 100644 --- a/src/cpp/include/nodegui/QtWidgets/QGridLayout/qgridlayout_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QGridLayout/qgridlayout_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QGridLayoutWrap : public Napi::ObjectWrap { QLAYOUT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QGridLayoutWrap(const Napi::CallbackInfo& info); ~QGridLayoutWrap(); - NGridLayout* getInternalInstance(); + QGridLayout* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QGroupBox/qgroupbox_wrap.h b/src/cpp/include/nodegui/QtWidgets/QGroupBox/qgroupbox_wrap.h index e0aaffff2..a2521b9a1 100644 --- a/src/cpp/include/nodegui/QtWidgets/QGroupBox/qgroupbox_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QGroupBox/qgroupbox_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QGroupBoxWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QGroupBoxWrap(const Napi::CallbackInfo& info); ~QGroupBoxWrap(); - NGroupBox* getInternalInstance(); + QGroupBox* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QHeaderView/qheaderview_wrap.h b/src/cpp/include/nodegui/QtWidgets/QHeaderView/qheaderview_wrap.h index 2f21eaf24..f6b9281cf 100644 --- a/src/cpp/include/nodegui/QtWidgets/QHeaderView/qheaderview_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QHeaderView/qheaderview_wrap.h @@ -45,7 +45,6 @@ class DLL_EXPORT QHeaderViewWrap : public Napi::ObjectWrap { private: QPointer instance; - bool disableDeletion; public: static Napi::Object init(Napi::Env env, Napi::Object exports); diff --git a/src/cpp/include/nodegui/QtWidgets/QInputDialog/qinputdialog_wrap.h b/src/cpp/include/nodegui/QtWidgets/QInputDialog/qinputdialog_wrap.h index 71079fea5..20787251d 100644 --- a/src/cpp/include/nodegui/QtWidgets/QInputDialog/qinputdialog_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QInputDialog/qinputdialog_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QInputDialogWrap : public Napi::ObjectWrap { QDIALOG_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QInputDialogWrap(const Napi::CallbackInfo& info); ~QInputDialogWrap(); - NInputDialog* getInternalInstance(); + QInputDialog* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // members diff --git a/src/cpp/include/nodegui/QtWidgets/QLCDNumber/qlcdnumber_wrap.h b/src/cpp/include/nodegui/QtWidgets/QLCDNumber/qlcdnumber_wrap.h index 218e2a1bb..0fb12240c 100644 --- a/src/cpp/include/nodegui/QtWidgets/QLCDNumber/qlcdnumber_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QLCDNumber/qlcdnumber_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QLCDNumberWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QLCDNumberWrap(const Napi::CallbackInfo& info); ~QLCDNumberWrap(); - NLCDNumber* getInternalInstance(); + QLCDNumber* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QLabel/qlabel_wrap.h b/src/cpp/include/nodegui/QtWidgets/QLabel/qlabel_wrap.h index 5d4e1d89a..85e6b3487 100644 --- a/src/cpp/include/nodegui/QtWidgets/QLabel/qlabel_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QLabel/qlabel_wrap.h @@ -12,19 +12,20 @@ class DLL_EXPORT QLabelWrap : public Napi::ObjectWrap { QFRAME_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QLabelWrap(const Napi::CallbackInfo& info); ~QLabelWrap(); - NLabel* getInternalInstance(); + QLabel* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods Napi::Value setSelection(const Napi::CallbackInfo& info); Napi::Value selectionStart(const Napi::CallbackInfo& info); Napi::Value setBuddy(const Napi::CallbackInfo& info); + Napi::Value buddy(const Napi::CallbackInfo& info); Napi::Value clear(const Napi::CallbackInfo& info); Napi::Value setMovie(const Napi::CallbackInfo& info); Napi::Value setNumDouble(const Napi::CallbackInfo& info); diff --git a/src/cpp/include/nodegui/QtWidgets/QLayout/qlayout_wrap.h b/src/cpp/include/nodegui/QtWidgets/QLayout/qlayout_wrap.h index 3d3e06de0..dffbb21df 100644 --- a/src/cpp/include/nodegui/QtWidgets/QLayout/qlayout_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QLayout/qlayout_wrap.h @@ -13,13 +13,13 @@ class DLL_EXPORT QLayoutWrap : public Napi::ObjectWrap { QLAYOUT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: - static void init(Napi::Env env); + static void init(Napi::Env env, Napi::Object exports); QLayoutWrap(const Napi::CallbackInfo& info); ~QLayoutWrap(); - NLayout* getInternalInstance(); + QLayout* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QLineEdit/qlineedit_wrap.h b/src/cpp/include/nodegui/QtWidgets/QLineEdit/qlineedit_wrap.h index d95e141bf..1d7e43189 100644 --- a/src/cpp/include/nodegui/QtWidgets/QLineEdit/qlineedit_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QLineEdit/qlineedit_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QLineEditWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QLineEditWrap(const Napi::CallbackInfo& info); ~QLineEditWrap(); - NLineEdit* getInternalInstance(); + QLineEdit* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QListView/qlistview_wrap.h b/src/cpp/include/nodegui/QtWidgets/QListView/qlistview_wrap.h index ba0d07f58..6b3e5df27 100644 --- a/src/cpp/include/nodegui/QtWidgets/QListView/qlistview_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QListView/qlistview_wrap.h @@ -11,14 +11,13 @@ class DLL_EXPORT QListViewWrap : public Napi::ObjectWrap { QLISTVIEW_WRAPPED_METHODS_DECLARATION private: - QPointer instance; - bool disableDeletion; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QListViewWrap(const Napi::CallbackInfo& info); ~QListViewWrap(); - NListView* getInternalInstance(); + QListView* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QListWidget/qlistwidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QListWidget/qlistwidget_wrap.h index 2dd7fe50e..684ded35e 100644 --- a/src/cpp/include/nodegui/QtWidgets/QListWidget/qlistwidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QListWidget/qlistwidget_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QListWidgetWrap : public Napi::ObjectWrap { QLISTVIEW_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QListWidgetWrap(const Napi::CallbackInfo& info); ~QListWidgetWrap(); - NListWidget* getInternalInstance(); + QListWidget* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h b/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h index 056c707f9..8cf3560c1 100644 --- a/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h @@ -12,16 +12,17 @@ class DLL_EXPORT QMainWindowWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QMainWindowWrap(const Napi::CallbackInfo& info); ~QMainWindowWrap(); - NMainWindow* getInternalInstance(); + QMainWindow* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods + Napi::Value centralWidget(const Napi::CallbackInfo& info); Napi::Value setCentralWidget(const Napi::CallbackInfo& info); Napi::Value takeCentralWidget(const Napi::CallbackInfo& info); Napi::Value setMenuBar(const Napi::CallbackInfo& info); diff --git a/src/cpp/include/nodegui/QtWidgets/QMenu/nmenu.hpp b/src/cpp/include/nodegui/QtWidgets/QMenu/nmenu.hpp index 0e70aa4df..6fca47757 100644 --- a/src/cpp/include/nodegui/QtWidgets/QMenu/nmenu.hpp +++ b/src/cpp/include/nodegui/QtWidgets/QMenu/nmenu.hpp @@ -17,10 +17,8 @@ class DLL_EXPORT NMenu : public QMenu, public NodeWidget { QObject::connect(this, &QMenu::triggered, [=](QAction* action) { Napi::Env env = this->emitOnNode.Env(); Napi::HandleScope scope(env); - auto actionInstance = QActionWrap::constructor.New( - {Napi::External::New(env, action)}); - this->emitOnNode.Call( - {Napi::String::New(env, "triggered"), actionInstance}); + auto instance = WrapperCache::instance.getWrapper(env, action); + this->emitOnNode.Call({Napi::String::New(env, "triggered"), instance}); }); } }; diff --git a/src/cpp/include/nodegui/QtWidgets/QMenu/qmenu_wrap.h b/src/cpp/include/nodegui/QtWidgets/QMenu/qmenu_wrap.h index f45b158f5..88b599d68 100644 --- a/src/cpp/include/nodegui/QtWidgets/QMenu/qmenu_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QMenu/qmenu_wrap.h @@ -10,13 +10,13 @@ class DLL_EXPORT QMenuWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QMenuWrap(const Napi::CallbackInfo& info); ~QMenuWrap(); - NMenu* getInternalInstance(); + QMenu* getInternalInstance(); static Napi::FunctionReference constructor; // wrapped methods Napi::Value clear(const Napi::CallbackInfo& info); diff --git a/src/cpp/include/nodegui/QtWidgets/QMenuBar/qmenubar_wrap.h b/src/cpp/include/nodegui/QtWidgets/QMenuBar/qmenubar_wrap.h index dd04274ab..6ec6722e8 100644 --- a/src/cpp/include/nodegui/QtWidgets/QMenuBar/qmenubar_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QMenuBar/qmenubar_wrap.h @@ -10,13 +10,13 @@ class DLL_EXPORT QMenuBarWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QMenuBarWrap(const Napi::CallbackInfo& info); ~QMenuBarWrap(); - NMenuBar* getInternalInstance(); + QMenuBar* getInternalInstance(); static Napi::FunctionReference constructor; // wrapped methods Napi::Value addMenu(const Napi::CallbackInfo& info); diff --git a/src/cpp/include/nodegui/QtWidgets/QMessageBox/qmessagebox_wrap.h b/src/cpp/include/nodegui/QtWidgets/QMessageBox/qmessagebox_wrap.h index c21c7e883..6e7fc49c4 100644 --- a/src/cpp/include/nodegui/QtWidgets/QMessageBox/qmessagebox_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QMessageBox/qmessagebox_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QMessageBoxWrap : public Napi::ObjectWrap { QDIALOG_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QMessageBoxWrap(const Napi::CallbackInfo& info); ~QMessageBoxWrap(); - NMessageBox* getInternalInstance(); + QMessageBox* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QPlainTextEdit/qplaintextedit_wrap.h b/src/cpp/include/nodegui/QtWidgets/QPlainTextEdit/qplaintextedit_wrap.h index b3627711c..b8d55b151 100644 --- a/src/cpp/include/nodegui/QtWidgets/QPlainTextEdit/qplaintextedit_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QPlainTextEdit/qplaintextedit_wrap.h @@ -13,13 +13,13 @@ class DLL_EXPORT QPlainTextEditWrap : public Napi::ObjectWrap { QABSTRACTSCROLLAREA_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QPlainTextEditWrap(const Napi::CallbackInfo &info); ~QPlainTextEditWrap(); - NPlainTextEdit *getInternalInstance(); + QPlainTextEdit *getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QProgressBar/qprogressbar_wrap.h b/src/cpp/include/nodegui/QtWidgets/QProgressBar/qprogressbar_wrap.h index e19f97171..43316e46d 100644 --- a/src/cpp/include/nodegui/QtWidgets/QProgressBar/qprogressbar_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QProgressBar/qprogressbar_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QProgressBarWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QProgressBarWrap(const Napi::CallbackInfo& info); ~QProgressBarWrap(); - NProgressBar* getInternalInstance(); + QProgressBar* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QProgressDialog/qprogressdialog_wrap.h b/src/cpp/include/nodegui/QtWidgets/QProgressDialog/qprogressdialog_wrap.h index ca2e88153..73bcdcf9f 100644 --- a/src/cpp/include/nodegui/QtWidgets/QProgressDialog/qprogressdialog_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QProgressDialog/qprogressdialog_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QProgressDialogWrap : public Napi::ObjectWrap { QDIALOG_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QProgressDialogWrap(const Napi::CallbackInfo& info); ~QProgressDialogWrap(); - NProgressDialog* getInternalInstance(); + QProgressDialog* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QPushButton/qpushbutton_wrap.h b/src/cpp/include/nodegui/QtWidgets/QPushButton/qpushbutton_wrap.h index 92f4bd660..941797bb8 100644 --- a/src/cpp/include/nodegui/QtWidgets/QPushButton/qpushbutton_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QPushButton/qpushbutton_wrap.h @@ -12,17 +12,17 @@ class DLL_EXPORT QPushButtonWrap : public Napi::ObjectWrap { QABSTRACTBUTTON_WRAPPED_METHODS_DECLARATION private: - QPointer instance; - bool disableDeletion; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QPushButtonWrap(const Napi::CallbackInfo& info); ~QPushButtonWrap(); - NPushButton* getInternalInstance(); + QPushButton* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods Napi::Value setMenu(const Napi::CallbackInfo& info); + Napi::Value menu(const Napi::CallbackInfo& info); Napi::Value showMenu(const Napi::CallbackInfo& info); }; diff --git a/src/cpp/include/nodegui/QtWidgets/QRadioButton/qradiobutton_wrap.h b/src/cpp/include/nodegui/QtWidgets/QRadioButton/qradiobutton_wrap.h index bd2fc0be0..d8dd3fc32 100644 --- a/src/cpp/include/nodegui/QtWidgets/QRadioButton/qradiobutton_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QRadioButton/qradiobutton_wrap.h @@ -13,14 +13,13 @@ class DLL_EXPORT QRadioButtonWrap : public Napi::ObjectWrap { QABSTRACTBUTTON_WRAPPED_METHODS_DECLARATION private: - QPointer instance; - bool disableDeletion; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QRadioButtonWrap(const Napi::CallbackInfo& info); ~QRadioButtonWrap(); - NRadioButton* getInternalInstance(); + QRadioButton* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QScrollArea/qscrollarea_wrap.h b/src/cpp/include/nodegui/QtWidgets/QScrollArea/qscrollarea_wrap.h index 2e6862503..3bca82c79 100644 --- a/src/cpp/include/nodegui/QtWidgets/QScrollArea/qscrollarea_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QScrollArea/qscrollarea_wrap.h @@ -11,20 +11,20 @@ class DLL_EXPORT QScrollAreaWrap : public Napi::ObjectWrap { QABSTRACTSCROLLAREA_WRAPPED_METHODS_DECLARATION private: - QPointer instance; - YGNodeRef scrollNode; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QScrollAreaWrap(const Napi::CallbackInfo& info); ~QScrollAreaWrap(); - NScrollArea* getInternalInstance(); + QScrollArea* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods Napi::Value ensureVisible(const Napi::CallbackInfo& info); Napi::Value ensureWidgetVisible(const Napi::CallbackInfo& info); Napi::Value setWidget(const Napi::CallbackInfo& info); + Napi::Value widget(const Napi::CallbackInfo& info); Napi::Value takeWidget(const Napi::CallbackInfo& info); Napi::Value setViewportMargins(const Napi::CallbackInfo& info); Napi::Value viewportMargins(const Napi::CallbackInfo& info); diff --git a/src/cpp/include/nodegui/QtWidgets/QScrollBar/qscrollbar_wrap.h b/src/cpp/include/nodegui/QtWidgets/QScrollBar/qscrollbar_wrap.h index ffd8c9a1c..434356ca0 100644 --- a/src/cpp/include/nodegui/QtWidgets/QScrollBar/qscrollbar_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QScrollBar/qscrollbar_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QScrollBarWrap : public Napi::ObjectWrap { QABSTRACTSLIDER_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QScrollBarWrap(const Napi::CallbackInfo& info); ~QScrollBarWrap(); - NScrollBar* getInternalInstance(); + QScrollBar* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QShortcut/qshortcut_wrap.h b/src/cpp/include/nodegui/QtWidgets/QShortcut/qshortcut_wrap.h index 052f962dc..1c7bf5667 100644 --- a/src/cpp/include/nodegui/QtWidgets/QShortcut/qshortcut_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QShortcut/qshortcut_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QShortcutWrap : public Napi::ObjectWrap { QOBJECT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QShortcutWrap(const Napi::CallbackInfo& info); ~QShortcutWrap(); - NShortcut* getInternalInstance(); + QShortcut* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QSlider/qslider_wrap.h b/src/cpp/include/nodegui/QtWidgets/QSlider/qslider_wrap.h index 653ec702c..7bd811ee7 100644 --- a/src/cpp/include/nodegui/QtWidgets/QSlider/qslider_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QSlider/qslider_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QSliderWrap : public Napi::ObjectWrap { QABSTRACTSLIDER_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QSliderWrap(const Napi::CallbackInfo& info); ~QSliderWrap(); - NSlider* getInternalInstance(); + QSlider* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QSpinBox/qspinbox_wrap.h b/src/cpp/include/nodegui/QtWidgets/QSpinBox/qspinbox_wrap.h index e26389542..ca1cb169f 100644 --- a/src/cpp/include/nodegui/QtWidgets/QSpinBox/qspinbox_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QSpinBox/qspinbox_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QSpinBoxWrap : public Napi::ObjectWrap { QABSTRACTSPINBOX_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QSpinBoxWrap(const Napi::CallbackInfo &info); ~QSpinBoxWrap(); - NSpinBox *getInternalInstance(); + QSpinBox *getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QSplitter/qsplitter_wrap.h b/src/cpp/include/nodegui/QtWidgets/QSplitter/qsplitter_wrap.h index bae681e0a..aac552267 100644 --- a/src/cpp/include/nodegui/QtWidgets/QSplitter/qsplitter_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QSplitter/qsplitter_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QSplitterWrap : public Napi::ObjectWrap { QFRAME_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QSplitterWrap(const Napi::CallbackInfo& info); ~QSplitterWrap(); - NSplitter* getInternalInstance(); + QSplitter* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QStackedWidget/qstackedwidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QStackedWidget/qstackedwidget_wrap.h index e29fed9d6..acfba76db 100644 --- a/src/cpp/include/nodegui/QtWidgets/QStackedWidget/qstackedwidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QStackedWidget/qstackedwidget_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QStackedWidgetWrap : public Napi::ObjectWrap { QFRAME_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QStackedWidgetWrap(const Napi::CallbackInfo &info); ~QStackedWidgetWrap(); - NStackedWidget *getInternalInstance(); + QStackedWidget *getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.h b/src/cpp/include/nodegui/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.h index f4bbc5508..4a979896e 100644 --- a/src/cpp/include/nodegui/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.h @@ -10,13 +10,13 @@ class DLL_EXPORT QStandardItemModelWrap : public Napi::ObjectWrap { private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QStandardItemModelWrap(const Napi::CallbackInfo& info); ~QStandardItemModelWrap(); - NStandardItemModel* getInternalInstance(); + QStandardItemModel* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QStatusBar/qstatusbar_wrap.h b/src/cpp/include/nodegui/QtWidgets/QStatusBar/qstatusbar_wrap.h index eaa6a774e..f02414cbc 100644 --- a/src/cpp/include/nodegui/QtWidgets/QStatusBar/qstatusbar_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QStatusBar/qstatusbar_wrap.h @@ -12,7 +12,7 @@ class DLL_EXPORT QStatusBarWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); @@ -22,7 +22,7 @@ class DLL_EXPORT QStatusBarWrap : public Napi::ObjectWrap { QStatusBarWrap(const Napi::CallbackInfo &info); ~QStatusBarWrap(); - NStatusBar *getInternalInstance(); + QStatusBar *getInternalInstance(); // Wrapped methods Napi::Value addPermanentWidget(const Napi::CallbackInfo &info); diff --git a/src/cpp/include/nodegui/QtWidgets/QSvgWidget/qsvgwidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QSvgWidget/qsvgwidget_wrap.h index ebe731ae6..3897a1e2c 100644 --- a/src/cpp/include/nodegui/QtWidgets/QSvgWidget/qsvgwidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QSvgWidget/qsvgwidget_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QSvgWidgetWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QSvgWidgetWrap(const Napi::CallbackInfo& info); ~QSvgWidgetWrap(); - NSvgWidget* getInternalInstance(); + QSvgWidget* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.h b/src/cpp/include/nodegui/QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.h index 1fcdf9695..2549b928c 100644 --- a/src/cpp/include/nodegui/QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QSystemTrayIconWrap : public Napi::ObjectWrap { QOBJECT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QSystemTrayIconWrap(const Napi::CallbackInfo& info); ~QSystemTrayIconWrap(); - NSystemTrayIcon* getInternalInstance(); + QSystemTrayIcon* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QTabBar/qtabbar_wrap.h b/src/cpp/include/nodegui/QtWidgets/QTabBar/qtabbar_wrap.h index ff7bfcbf2..2c338e25d 100644 --- a/src/cpp/include/nodegui/QtWidgets/QTabBar/qtabbar_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QTabBar/qtabbar_wrap.h @@ -11,14 +11,13 @@ class DLL_EXPORT QTabBarWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; - bool disableDeletion; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QTabBarWrap(const Napi::CallbackInfo& info); ~QTabBarWrap(); - NTabBar* getInternalInstance(); + QTabBar* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QTabWidget/qtabwidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QTabWidget/qtabwidget_wrap.h index b25b23550..b58165901 100644 --- a/src/cpp/include/nodegui/QtWidgets/QTabWidget/qtabwidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QTabWidget/qtabwidget_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QTabWidgetWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QTabWidgetWrap(const Napi::CallbackInfo &info); ~QTabWidgetWrap(); - NTabWidget *getInternalInstance(); + QTabWidget *getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods @@ -31,4 +31,5 @@ class DLL_EXPORT QTabWidgetWrap : public Napi::ObjectWrap { Napi::Value setTabsClosable(const Napi::CallbackInfo &info); Napi::Value setTabText(const Napi::CallbackInfo &info); Napi::Value setTabIcon(const Napi::CallbackInfo &info); + Napi::Value widget(const Napi::CallbackInfo &info); }; diff --git a/src/cpp/include/nodegui/QtWidgets/QTableView/qtableview_macro.h b/src/cpp/include/nodegui/QtWidgets/QTableView/qtableview_macro.h index 70457e143..f5fd1a8be 100644 --- a/src/cpp/include/nodegui/QtWidgets/QTableView/qtableview_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QTableView/qtableview_macro.h @@ -181,15 +181,13 @@ Napi::Value horizontalHeader(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ auto header = this->instance->horizontalHeader(); \ - auto instance = QHeaderViewWrap::constructor.New( \ - {Napi::External::New(env, header)}); \ + auto instance = WrapperCache::instance.getWrapper(env, header); \ return instance; \ } \ Napi::Value verticalHeader(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ auto header = this->instance->verticalHeader(); \ - auto instance = QHeaderViewWrap::constructor.New( \ - {Napi::External::New(env, header)}); \ + auto instance = WrapperCache::instance.getWrapper(env, header); \ return instance; \ } diff --git a/src/cpp/include/nodegui/QtWidgets/QTableView/qtableview_wrap.h b/src/cpp/include/nodegui/QtWidgets/QTableView/qtableview_wrap.h index 8d98a44bf..cef62d924 100644 --- a/src/cpp/include/nodegui/QtWidgets/QTableView/qtableview_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QTableView/qtableview_wrap.h @@ -11,14 +11,13 @@ class DLL_EXPORT QTableViewWrap : public Napi::ObjectWrap { QTABLEVIEW_WRAPPED_METHODS_DECLARATION private: - QPointer instance; - bool disableDeletion; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QTableViewWrap(const Napi::CallbackInfo& info); ~QTableViewWrap(); - NTableView* getInternalInstance(); + QTableView* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QTableWidget/qtablewidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QTableWidget/qtablewidget_wrap.h index 833fcd233..724e28c67 100644 --- a/src/cpp/include/nodegui/QtWidgets/QTableWidget/qtablewidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QTableWidget/qtablewidget_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QTableWidgetWrap : public Napi::ObjectWrap { QABSTRACTSCROLLAREA_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QTableWidgetWrap(const Napi::CallbackInfo& info); ~QTableWidgetWrap(); - NTableWidget* getInternalInstance(); + QTableWidget* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QTextBrowser/qtextbrowser_wrap.h b/src/cpp/include/nodegui/QtWidgets/QTextBrowser/qtextbrowser_wrap.h index e72eabddf..33ad4494e 100644 --- a/src/cpp/include/nodegui/QtWidgets/QTextBrowser/qtextbrowser_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QTextBrowser/qtextbrowser_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QTextBrowserWrap : public Napi::ObjectWrap { QTEXTEDIT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QTextBrowserWrap(const Napi::CallbackInfo& info); ~QTextBrowserWrap(); - NTextBrowser* getInternalInstance(); + QTextBrowser* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QTextEdit/qtextedit_wrap.h b/src/cpp/include/nodegui/QtWidgets/QTextEdit/qtextedit_wrap.h index a6173e4f6..68d4981a6 100644 --- a/src/cpp/include/nodegui/QtWidgets/QTextEdit/qtextedit_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QTextEdit/qtextedit_wrap.h @@ -11,13 +11,13 @@ class DLL_EXPORT QTextEditWrap : public Napi::ObjectWrap { QTEXTEDIT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QTextEditWrap(const Napi::CallbackInfo& info); ~QTextEditWrap(); - NTextEdit* getInternalInstance(); + QTextEdit* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QTimeEdit/qtimeedit_wrap.h b/src/cpp/include/nodegui/QtWidgets/QTimeEdit/qtimeedit_wrap.h index fe322e4d4..a4761ba40 100644 --- a/src/cpp/include/nodegui/QtWidgets/QTimeEdit/qtimeedit_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QTimeEdit/qtimeedit_wrap.h @@ -12,13 +12,13 @@ class DLL_EXPORT QTimeEditWrap : public Napi::ObjectWrap { QDATETIMEEDIT_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QTimeEditWrap(const Napi::CallbackInfo &info); ~QTimeEditWrap(); - NTimeEdit *getInternalInstance(); + QTimeEdit *getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/QtWidgets/QToolButton/ntoolbutton.hpp b/src/cpp/include/nodegui/QtWidgets/QToolButton/ntoolbutton.hpp index 45c0a94d0..bc966959d 100644 --- a/src/cpp/include/nodegui/QtWidgets/QToolButton/ntoolbutton.hpp +++ b/src/cpp/include/nodegui/QtWidgets/QToolButton/ntoolbutton.hpp @@ -21,11 +21,7 @@ class DLL_EXPORT NToolButton : public QToolButton, public NodeWidget { QObject::connect(this, &QToolButton::triggered, [=](QAction* action) { Napi::Env env = this->emitOnNode.Env(); Napi::HandleScope scope(env); - - // disable deletion of the native instance for these by passing true - auto instance = QActionWrap::constructor.New( - {Napi::External::New(env, action), - Napi::Boolean::New(env, true)}); + auto instance = WrapperCache::instance.getWrapper(env, action); this->emitOnNode.Call({Napi::String::New(env, "triggered"), instance}); }); } diff --git a/src/cpp/include/nodegui/QtWidgets/QToolButton/qtoolbutton_wrap.h b/src/cpp/include/nodegui/QtWidgets/QToolButton/qtoolbutton_wrap.h index fe3f765e7..ef153296f 100644 --- a/src/cpp/include/nodegui/QtWidgets/QToolButton/qtoolbutton_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QToolButton/qtoolbutton_wrap.h @@ -12,18 +12,18 @@ class DLL_EXPORT QToolButtonWrap : public Napi::ObjectWrap { QABSTRACTBUTTON_WRAPPED_METHODS_DECLARATION private: - QPointer instance; - bool disableDeletion; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QToolButtonWrap(const Napi::CallbackInfo& info); ~QToolButtonWrap(); - NToolButton* getInternalInstance(); + QToolButton* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods Napi::Value setMenu(const Napi::CallbackInfo& info); Napi::Value setDefaultAction(const Napi::CallbackInfo& info); Napi::Value showMenu(const Napi::CallbackInfo& info); + Napi::Value defaultAction(const Napi::CallbackInfo& info); }; diff --git a/src/cpp/include/nodegui/QtWidgets/QTreeWidget/qtreewidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QTreeWidget/qtreewidget_wrap.h index a94b9214f..d654813de 100644 --- a/src/cpp/include/nodegui/QtWidgets/QTreeWidget/qtreewidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QTreeWidget/qtreewidget_wrap.h @@ -11,7 +11,7 @@ class DLL_EXPORT QTreeWidgetWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); @@ -20,7 +20,7 @@ class DLL_EXPORT QTreeWidgetWrap : public Napi::ObjectWrap { ~QTreeWidgetWrap(); - NTreeWidget *getInternalInstance(); + QTreeWidget *getInternalInstance(); // class constructor static Napi::FunctionReference constructor; diff --git a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h index 5c47039f1..f6eb6c937 100644 --- a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h @@ -429,7 +429,7 @@ Napi::Env env = info.Env(); \ QWindow* window = this->instance->windowHandle(); \ if (window) { \ - return WrapperCache::instance.get(env, window); \ + return WrapperCache::instance.getWrapper(env, window, true); \ } else { \ return env.Null(); \ } \ diff --git a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_wrap.h index 0befba43a..fee82900b 100644 --- a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_wrap.h @@ -27,13 +27,13 @@ class DLL_EXPORT NodeWidgetWrap : public Napi::ObjectWrap { class DLL_EXPORT QWidgetWrap : public Napi::ObjectWrap { QWIDGET_WRAPPED_METHODS_DECLARATION private: - QPointer instance; + QPointer instance; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QWidgetWrap(const Napi::CallbackInfo& info); ~QWidgetWrap(); - NWidget* getInternalInstance(); + QWidget* getInternalInstance(); // class constructor static Napi::FunctionReference constructor; // wrapped methods diff --git a/src/cpp/include/nodegui/core/WrapperCache/wrappercache.h b/src/cpp/include/nodegui/core/WrapperCache/wrappercache.h index 4c43d33b4..013d16a5b 100644 --- a/src/cpp/include/nodegui/core/WrapperCache/wrappercache.h +++ b/src/cpp/include/nodegui/core/WrapperCache/wrappercache.h @@ -2,17 +2,21 @@ #include +#include #include +#include #include #include "Extras/Export/export.h" -#include "QtGui/QScreen/qscreen_wrap.h" +#include "Extras/Utils/nutils.h" struct CachedObject { napi_ref ref; napi_env env; }; +typedef Napi::Object (*WrapFunc)(Napi::Env, QObject*); + /** * C++ side cache for wrapper objects. * @@ -24,7 +28,8 @@ class DLL_EXPORT WrapperCache : public QObject { Q_OBJECT private: - QMap cache; + QMap cache; + QMap wrapperRegistry; public: /** @@ -33,7 +38,76 @@ class DLL_EXPORT WrapperCache : public QObject { static WrapperCache instance; /** - * Get a wrapper for a given Qt object. + * Register a function to wrap certain instances of a `QObject` subclass. + * + * @param typeName - The name of the `QObject` subclass this wrapper function + * applies to. + * @param wrapFunc - Function to wrap `QObject` instances. + */ + void registerWrapper(QString typeName, WrapFunc wrapFunc) { + this->wrapperRegistry[typeName] = wrapFunc; + } + + /** + * Get a wrapper for a QObject + * + * @param env - Napi environment + * @param qobject - The QObject or subclass instance to wrap + * @param keepAlive - Set this to true if the wrapper object should be kept + * alive until the underlying QObject is destroyed regardless of whether + * the JS side holding a reference to it or not. (Defaults to false). + * @return Napi object wrapping the object + */ + Napi::Value getWrapper(Napi::Env env, QObject* qobject, + bool keepAlive = false) { + if (qobject == nullptr) { + return env.Null(); + } + + uint64_t ptrHash = extrautils::hashPointerTo53bit(qobject); + if (this->cache.contains(ptrHash)) { + napi_value result = nullptr; + napi_get_reference_value(env, this->cache[ptrHash].ref, &result); + + napi_valuetype valuetype; + napi_typeof(env, result, &valuetype); + if (valuetype != napi_null) { + return Napi::Object(env, result); + } + } + + // Might have to climb up the class hierarchy looking for a wrapper type we + // support. This makes us immune to internal Qt subclasses, i.e. + // `QWidgetWindow` when `QWindow` was expected. + const QMetaObject* meta = qobject->metaObject(); + while (meta != nullptr) { + QString className(meta->className()); + if (this->wrapperRegistry.contains(className)) { + Napi::Object wrapper = this->wrapperRegistry[className](env, qobject); + store(env, ptrHash, qobject, wrapper, !keepAlive); + return wrapper; + } + meta = meta->superClass(); + } + + QMapIterator i(this->wrapperRegistry); + QString allQWrapperNames; + while (i.hasNext()) { + i.next(); + allQWrapperNames.append(i.key()); + allQWrapperNames.append(", "); + } + + qDebug() << "NodeGui: Unable to find wrapper for instance of C++ class " + << qobject->metaObject()->className() + << ". (The following C++ classes are recognized: " + << allQWrapperNames << ")"; + + return env.Null(); + } + + /** + * Store a mapping from Qt Object to wrapper * * @param T - (template argument) The Qt class of the object being cached, * e.g. `QScreen`. @@ -41,32 +115,24 @@ class DLL_EXPORT WrapperCache : public QObject { * `QScreenWrap`. * @param env = Napi environment * @param object - Pointer to the QObject for which a wrapper is required. - * @return The JS wrapper object. + * @param wrapper - The wrapper object matching `object`. */ - template - Napi::Object get(Napi::Env env, T* object) { - if (this->cache.contains(object)) { - napi_value result = nullptr; - napi_get_reference_value(env, this->cache[object].ref, &result); - return Napi::Object(env, result); - } - - Napi::Object wrapper = - W::constructor.New({Napi::External::New(env, object)}); - + void store(Napi::Env env, uint64_t ptrHash, QObject* qobject, + Napi::Object wrapper, bool isWeak) { napi_ref ref = nullptr; - napi_create_reference(env, wrapper, 1, &ref); - this->cache[object].env = napi_env(env); - this->cache[object].ref = ref; - QObject::connect(object, &QObject::destroyed, this, + napi_create_reference(env, wrapper, isWeak ? 0 : 1, &ref); + this->cache[ptrHash].env = napi_env(env); + this->cache[ptrHash].ref = ref; + + QObject::connect(qobject, &QObject::destroyed, this, &WrapperCache::handleDestroyed); - return wrapper; } static Napi::Object init(Napi::Env env, Napi::Object exports) { exports.Set("WrapperCache_injectCallback", Napi::Function::New(env)); + exports.Set("WrapperCache_store", Napi::Function::New(env)); return exports; } @@ -77,11 +143,23 @@ class DLL_EXPORT WrapperCache : public QObject { return env.Null(); } + static Napi::Value storeJS(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + + Napi::Object objectWrapper = info[0].As(); + QObject* qobject = info[1].As>().Data(); + + uint64_t ptrHash = extrautils::hashPointerTo53bit(qobject); + instance.store(env, ptrHash, qobject, objectWrapper, false); + return env.Null(); + } + static Napi::FunctionReference destroyedCallback; public Q_SLOTS: - void handleDestroyed(const QObject* object) { - if (!this->cache.contains(object)) { + void handleDestroyed(const QObject* qobject) { + uint64_t ptrHash = extrautils::hashPointerTo53bit(qobject); + if (!this->cache.contains(ptrHash)) { return; } @@ -90,14 +168,12 @@ class DLL_EXPORT WrapperCache : public QObject { if (destroyedCallback) { Napi::Env env = destroyedCallback.Env(); Napi::HandleScope scope(env); - destroyedCallback.Call( - env.Global(), - {Napi::Value::From(env, extrautils::hashPointerTo53bit(object))}); + destroyedCallback.Call(env.Global(), {Napi::Value::From(env, ptrHash)}); } uint32_t result = 0; - napi_reference_unref(this->cache[object].env, this->cache[object].ref, + napi_reference_unref(this->cache[ptrHash].env, this->cache[ptrHash].ref, &result); - this->cache.remove(object); + this->cache.remove(ptrHash); } }; diff --git a/src/cpp/lib/Extras/Utils/nutils.cpp b/src/cpp/lib/Extras/Utils/nutils.cpp index b52d86f83..ed4990072 100644 --- a/src/cpp/lib/Extras/Utils/nutils.cpp +++ b/src/cpp/lib/Extras/Utils/nutils.cpp @@ -7,6 +7,7 @@ #include "core/Component/component_wrap.h" #include "core/FlexLayout/flexutils.h" +#include "core/YogaWidget/yogawidget.h" bool extrautils::isNapiValueInt(Napi::Env& env, Napi::Value& num) { return env.Global() @@ -101,12 +102,16 @@ QVariant* extrautils::convertToQVariant(Napi::Env& env, Napi::Value& value) { } void* extrautils::configureComponent(void* component) { return component; } + void* extrautils::configureQObject(QObject* object) { return configureComponent(object); } -void* extrautils::configureQWidget(QWidget* widget, YGNodeRef node, - bool isLeafNode) { - flexutils::configureFlexNode(widget, node, isLeafNode); + +void* extrautils::configureQWidget(QWidget* widget, bool isLeafNode) { + YogaWidget* yogaWidget = dynamic_cast(widget); + if (yogaWidget) { + flexutils::configureFlexNode(widget, yogaWidget->getFlexNode(), isLeafNode); + } return configureQObject(widget); } diff --git a/src/cpp/lib/QtCore/QAbstractItemModel/qabstractitemmodel_wrap.cpp b/src/cpp/lib/QtCore/QAbstractItemModel/qabstractitemmodel_wrap.cpp index b0aa9f2ea..ed6360671 100644 --- a/src/cpp/lib/QtCore/QAbstractItemModel/qabstractitemmodel_wrap.cpp +++ b/src/cpp/lib/QtCore/QAbstractItemModel/qabstractitemmodel_wrap.cpp @@ -45,6 +45,7 @@ Napi::Object QAbstractItemModelWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QAbstractItemModelWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(NAbstractItemModel, QAbstractItemModelWrap); return exports; } @@ -58,7 +59,21 @@ QAbstractItemModelWrap::~QAbstractItemModelWrap() { QAbstractItemModelWrap::QAbstractItemModelWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - this->instance = new NAbstractItemModel(); + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NAbstractItemModel(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } + } else { + Napi::TypeError::New(env, + "NodeGui: QAbstractItemModelWrap: Wrong number of " + "arguments to constructor") + .ThrowAsJavaScriptException(); + } } Napi::Value QAbstractItemModelWrap::initNodeDispatcher( diff --git a/src/cpp/lib/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.cpp b/src/cpp/lib/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.cpp index f6ea1f900..e5df598ec 100644 --- a/src/cpp/lib/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.cpp +++ b/src/cpp/lib/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.cpp @@ -38,6 +38,7 @@ Napi::Object QItemSelectionModelWrap::init(Napi::Env env, QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QItemSelectionModelWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QItemSelectionModel, QItemSelectionModelWrap); return exports; } @@ -46,20 +47,33 @@ QItemSelectionModel* QItemSelectionModelWrap::getInternalInstance() { } QItemSelectionModelWrap::~QItemSelectionModelWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } + extrautils::safeDelete(this->instance); } QItemSelectionModelWrap::QItemSelectionModelWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NItemSelectionModel(); - this->disableDeletion = false; + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + // Napi::Object parentObject = info[0].As(); + // QObjectWrap* parentObjectWrap = + // Napi::ObjectWrap::Unwrap(parentObject); + // this->instance = new + // NItemSelectionModel(parentObjectWrap->getInternalInstance()); + } } else { - this->instance = info[0].As>().Data(); - this->disableDeletion = true; + Napi::TypeError::New(env, + "NodeGui: QItemSelectionModelWrap: Wrong number of " + "arguments to constructor") + .ThrowAsJavaScriptException(); } } Napi::Value QItemSelectionModelWrap::columnIntersectsSelection( diff --git a/src/cpp/lib/QtCore/QMimeData/qmimedata_wrap.cpp b/src/cpp/lib/QtCore/QMimeData/qmimedata_wrap.cpp index c0ba24c27..6476be13e 100644 --- a/src/cpp/lib/QtCore/QMimeData/qmimedata_wrap.cpp +++ b/src/cpp/lib/QtCore/QMimeData/qmimedata_wrap.cpp @@ -10,7 +10,6 @@ Napi::Object QMimeDataWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QMimeData"; Napi::Function func = DefineClass(env, CLASSNAME, - {InstanceMethod("clear", &QMimeDataWrap::clear), InstanceMethod("hasColor", &QMimeDataWrap::hasColor), InstanceMethod("hasHtml", &QMimeDataWrap::hasHtml), @@ -25,43 +24,31 @@ Napi::Object QMimeDataWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("setUrls", &QMimeDataWrap::setUrls), InstanceMethod("text", &QMimeDataWrap::text), InstanceMethod("urls", &QMimeDataWrap::urls), - - COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QMimeDataWrap)}); + QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QMimeDataWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QMimeData, QMimeDataWrap); return exports; } -QMimeData* QMimeDataWrap::getInternalInstance() { return this->instance.get(); } +QMimeData* QMimeDataWrap::getInternalInstance() { return this->instance; } -QMimeDataWrap::~QMimeDataWrap() { this->instance.reset(); } +QMimeDataWrap::~QMimeDataWrap() { extrautils::safeDelete(this->instance); } QMimeDataWrap::QMimeDataWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::External eventObject = - info[0].As>(); - this->instance = std::make_unique(); - // Copy data to our instance - QMimeData* mimeReference = eventObject.Data(); - this->cloneFromMimeData(mimeReference); - // foreach(QString format, mimeReference->formats()) - // { - // // Retrieving data - // QByteArray data = mimeReference->data(format); - // // Checking for custom MIME types - // if(format.startsWith("application/x-qt")) - // { - // // Retrieving true format name - // int indexBegin = format.indexOf('"') + 1; - // int indexEnd = format.indexOf('"', indexBegin); - // format = format.mid(indexBegin, indexEnd - indexBegin); - // } - // this->instance->setData(format, data); - // } + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new QMimeData(); + } else if (argCount == 1 && info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); } else { - this->instance = std::make_unique(); + Napi::TypeError::New( + env, "NodeGui: QMimeDataWrap: Wrong number of arguments to constructor") + .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureComponent(this->getInternalInstance()); } diff --git a/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp b/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp index bd0ac6401..c138abedb 100644 --- a/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp +++ b/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp @@ -1,6 +1,7 @@ #include "QtCore/QObject/qobject_wrap.h" #include "Extras/Utils/nutils.h" +#include "core/WrapperCache/wrappercache.h" Napi::FunctionReference QObjectWrap::constructor; @@ -11,29 +12,31 @@ Napi::Object QObjectWrap::init(Napi::Env env, Napi::Object exports) { env, CLASSNAME, {QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QObjectWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QObject, QObjectWrap); return exports; } -NObject* QObjectWrap::getInternalInstance() { return this->instance; } +QObject* QObjectWrap::getInternalInstance() { return this->instance; } QObjectWrap::~QObjectWrap() { extrautils::safeDelete(this->instance); } QObjectWrap::QObjectWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 1) { if (info[0].IsExternal()) { - this->instance = info[0].As>().Data(); + this->instance = info[0].As>().Data(); } else { Napi::Object parentObject = info[0].As(); QObjectWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NObject(parentWidgetWrap->getInternalInstance()); } - } else if (info.Length() == 0) { + } else if (argCount == 0) { this->instance = new NObject(); } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, "NodeGui: QObjectWrap: Wrong number of arguments") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); diff --git a/src/cpp/lib/QtGui/QApplication/qapplication_wrap.cpp b/src/cpp/lib/QtGui/QApplication/qapplication_wrap.cpp index 17189e12b..112b34c86 100644 --- a/src/cpp/lib/QtGui/QApplication/qapplication_wrap.cpp +++ b/src/cpp/lib/QtGui/QApplication/qapplication_wrap.cpp @@ -41,13 +41,15 @@ Napi::Object QApplicationWrap::init(Napi::Env env, Napi::Object exports) { QApplicationWrap::QApplicationWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 1) { this->instance = info[0].As>().Data(); - } else if (info.Length() == 0) { + } else if (argCount == 0) { this->instance = new NApplication(qode::qode_argc, qode::qode_argv); this->_wasManuallyCreated = true; } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, + "NodeGui: QApplicationWrap: Wrong number of arguments") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureComponent(this->getInternalInstance()); @@ -118,8 +120,7 @@ Napi::Value StaticQApplicationWrapMethods::clipboard( Napi::Env env = info.Env(); QClipboard* clipboard = QApplication::clipboard(); if (clipboard) { - return WrapperCache::instance.get(env, - clipboard); + return WrapperCache::instance.getWrapper(env, clipboard, true); } else { return env.Null(); } @@ -163,7 +164,7 @@ Napi::Value StaticQApplicationWrapMethods::primaryScreen( Napi::Env env = info.Env(); auto screen = QApplication::primaryScreen(); if (screen) { - return WrapperCache::instance.get(env, screen); + return WrapperCache::instance.getWrapper(env, screen, true); } else { return env.Null(); } @@ -176,8 +177,7 @@ Napi::Value StaticQApplicationWrapMethods::screens( Napi::Array jsArray = Napi::Array::New(env, screens.size()); for (int i = 0; i < screens.size(); i++) { QScreen* screen = screens[i]; - auto instance = - WrapperCache::instance.get(env, screen); + auto instance = WrapperCache::instance.getWrapper(env, screen, true); jsArray[i] = instance; } return jsArray; diff --git a/src/cpp/lib/QtGui/QClipboard/qclipboard_wrap.cpp b/src/cpp/lib/QtGui/QClipboard/qclipboard_wrap.cpp index 68d00e6d1..0eca693d9 100644 --- a/src/cpp/lib/QtGui/QClipboard/qclipboard_wrap.cpp +++ b/src/cpp/lib/QtGui/QClipboard/qclipboard_wrap.cpp @@ -19,6 +19,7 @@ Napi::Object QClipboardWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QClipboardWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QClipboard, QClipboardWrap); return exports; } diff --git a/src/cpp/lib/QtGui/QDrag/qdrag_wrap.cpp b/src/cpp/lib/QtGui/QDrag/qdrag_wrap.cpp index be3319195..0e80de3cc 100644 --- a/src/cpp/lib/QtGui/QDrag/qdrag_wrap.cpp +++ b/src/cpp/lib/QtGui/QDrag/qdrag_wrap.cpp @@ -32,21 +32,33 @@ Napi::Object QDragWrap::init(Napi::Env env, Napi::Object exports) { COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QDragWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QDrag, QDragWrap); return exports; } -NDrag* QDragWrap::getInternalInstance() { return this->instance; } +QDrag* QDragWrap::getInternalInstance() { return this->instance; } QDragWrap::QDragWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object wrap0_0 = info[0].As(); - QObjectWrap* wrap0_1 = Napi::ObjectWrap::Unwrap(wrap0_0); - QObject* dragSource = wrap0_1->getInternalInstance(); - this->instance = new NDrag(dragSource); + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NDrag(nullptr); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object sourceObject = info[0].As(); + QObjectWrap* sourceObjectWrap = + Napi::ObjectWrap::Unwrap(sourceObject); + this->instance = new NDrag(sourceObjectWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QDragWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureComponent(this->getInternalInstance()); diff --git a/src/cpp/lib/QtGui/QMovie/qmovie_wrap.cpp b/src/cpp/lib/QtGui/QMovie/qmovie_wrap.cpp index 5985af2ef..88ba4ce69 100644 --- a/src/cpp/lib/QtGui/QMovie/qmovie_wrap.cpp +++ b/src/cpp/lib/QtGui/QMovie/qmovie_wrap.cpp @@ -42,21 +42,27 @@ QMovieWrap::~QMovieWrap() { extrautils::safeDelete(this->instance); } QMovieWrap::QMovieWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - if (info[0].IsExternal()) { - this->instance = new NMovie(info[0].As>().Data()); - } else { - Napi::Object parentObject = info[0].As(); - QMovieWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NMovie(parentWidgetWrap->getInternalInstance()); - } - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NMovie(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + QObjectWrap* parentObjectWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NMovie(parentObjectWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QMovieWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } + QOBJECT_REGISTER_WRAPPER(QMovie, QMovieWrap); this->bufferDevice = QSharedPointer(new QBuffer); this->rawData = extrautils::configureQObject(this->getInternalInstance()); } diff --git a/src/cpp/lib/QtGui/QScreen/qscreen_wrap.cpp b/src/cpp/lib/QtGui/QScreen/qscreen_wrap.cpp index 24ef717b7..871182da7 100644 --- a/src/cpp/lib/QtGui/QScreen/qscreen_wrap.cpp +++ b/src/cpp/lib/QtGui/QScreen/qscreen_wrap.cpp @@ -16,6 +16,7 @@ Napi::Object QScreenWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QScreenWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QScreen, QScreenWrap); return exports; } @@ -25,7 +26,7 @@ QScreenWrap::QScreenWrap(const Napi::CallbackInfo& info) if (info[0].IsExternal()) { this->instance = info[0].As>().Data(); } else { - Napi::TypeError::New(env, "Incorrect initialization of QScreenWrap") + Napi::TypeError::New(env, "NodeGui: QScreenWrap: Incorrect initialization") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureComponent(this->getInternalInstance()); diff --git a/src/cpp/lib/QtGui/QStyle/qstyle_wrap.cpp b/src/cpp/lib/QtGui/QStyle/qstyle_wrap.cpp index 99efe1e1e..4ab3c4295 100644 --- a/src/cpp/lib/QtGui/QStyle/qstyle_wrap.cpp +++ b/src/cpp/lib/QtGui/QStyle/qstyle_wrap.cpp @@ -25,7 +25,8 @@ QStyleWrap::QStyleWrap(const Napi::CallbackInfo& info) if (info[0].IsExternal()) { this->instance = info[0].As>().Data(); } else { - Napi::TypeError::New(env, "Incorrect initialization of QStyleWrap") + Napi::TypeError::New( + env, "NodeGui: QStyleWrap: Incorrect initialization of QStyleWrap") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureComponent(this->getInternalInstance()); diff --git a/src/cpp/lib/QtGui/QWindow/qwindow_wrap.cpp b/src/cpp/lib/QtGui/QWindow/qwindow_wrap.cpp index 00b0d98a7..fee76f489 100644 --- a/src/cpp/lib/QtGui/QWindow/qwindow_wrap.cpp +++ b/src/cpp/lib/QtGui/QWindow/qwindow_wrap.cpp @@ -25,6 +25,7 @@ Napi::Object QWindowWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QWindowWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QWindow, QWindowWrap); return exports; } @@ -36,7 +37,8 @@ QWindowWrap::QWindowWrap(const Napi::CallbackInfo& info) if (info.Length() == 1 && info[0].IsExternal()) { this->instance = info[0].As>().Data(); } else { - Napi::TypeError::New(env, "Wrong number of arguments to QWindow.") + Napi::TypeError::New(env, + "NodeGui: QWindowWrap: Bad arguments to constructor.") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); @@ -55,8 +57,7 @@ void QWindowWrap::connectSignalsToEventEmitter() { this->instance.data(), &QWindow::screenChanged, [=](QScreen* screen) { Napi::Env env = this->emitOnNode.Env(); Napi::HandleScope scope(env); - auto instance = - WrapperCache::instance.get(env, screen); + auto instance = WrapperCache::instance.getWrapper(env, screen, true); this->emitOnNode.Call( {Napi::String::New(env, "screenChanged"), instance}); }); @@ -81,7 +82,7 @@ Napi::Value QWindowWrap::screen(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); QScreen* screen = this->instance->screen(); if (screen) { - return WrapperCache::instance.get(env, screen); + return WrapperCache::instance.getWrapper(env, screen, true); } else { return env.Null(); } diff --git a/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp b/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp index cc75c009f..7f6e2f462 100644 --- a/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp @@ -34,44 +34,40 @@ Napi::Object QActionWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QActionWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QAction, QActionWrap); return exports; } -NAction* QActionWrap::getInternalInstance() { return this->instance; } +QAction* QActionWrap::getInternalInstance() { return this->instance; } QActionWrap::QActionWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - - this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { - // --- if external --- - this->instance = info[0].As>().Data(); - if (info.Length() == 2) { - this->disableDeletion = info[1].As().Value(); + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NAction(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + QObjectWrap* parentObjectWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NAction(parentObjectWrap->getInternalInstance()); } } else { - // --- regular cases --- - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NAction(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { - this->instance = new NAction(); - } else { - Napi::TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - } + Napi::TypeError::New( + env, "NodeGui: QActionWrap: Wrong number of arguments to constructor") + .ThrowAsJavaScriptException(); } + this->rawData = extrautils::configureQObject(this->getInternalInstance()); } -QActionWrap::~QActionWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } -} +QActionWrap::~QActionWrap() { extrautils::safeDelete(this->instance); } Napi::Value QActionWrap::setText(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); diff --git a/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp b/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp index 28e53f058..a42745ff2 100644 --- a/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp @@ -27,16 +27,19 @@ Napi::Object QBoxLayoutWrap::init(Napi::Env env, Napi::Object exports) { QLAYOUT_WRAPPED_METHODS_EXPORT_DEFINE(QBoxLayoutWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QBoxLayout, QBoxLayoutWrap); return exports; } -NBoxLayout* QBoxLayoutWrap::getInternalInstance() { return this->instance; } +QBoxLayout* 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(); - if (info.Length() == 2) { + size_t argCount = info.Length(); + if (argCount == 2) { QBoxLayout::Direction dir = static_cast( info[0].As().Int32Value()); Napi::Object parentObject = info[1].As(); @@ -44,12 +47,17 @@ QBoxLayoutWrap::QBoxLayoutWrap(const Napi::CallbackInfo& info) 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 if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + QBoxLayout::Direction dir = static_cast( + info[0].As().Int32Value()); + this->instance = new NBoxLayout(dir); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, "QBoxLayoutWrap: Wrong number of arguments") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); diff --git a/src/cpp/lib/QtWidgets/QButtonGroup/qbuttongroup_wrap.cpp b/src/cpp/lib/QtWidgets/QButtonGroup/qbuttongroup_wrap.cpp index 143c41f62..1da2efaea 100644 --- a/src/cpp/lib/QtWidgets/QButtonGroup/qbuttongroup_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QButtonGroup/qbuttongroup_wrap.cpp @@ -27,25 +27,34 @@ Napi::Object QButtonGroupWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QButtonGroupWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QButtonGroup, QButtonGroupWrap); return exports; } -NButtonGroup* QButtonGroupWrap::getInternalInstance() { return this->instance; } +QButtonGroup* QButtonGroupWrap::getInternalInstance() { return this->instance; } QButtonGroupWrap::QButtonGroupWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NButtonGroup( - parentWidgetWrap - ->getInternalInstance()); // this sets the parent to current widget - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NButtonGroup(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NButtonGroup(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QButtonGroupWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); diff --git a/src/cpp/lib/QtWidgets/QCalendarWidget/qcalendarwidget_wrap.cpp b/src/cpp/lib/QtWidgets/QCalendarWidget/qcalendarwidget_wrap.cpp index 99de9374b..0506189ea 100644 --- a/src/cpp/lib/QtWidgets/QCalendarWidget/qcalendarwidget_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QCalendarWidget/qcalendarwidget_wrap.cpp @@ -28,10 +28,11 @@ Napi::Object QCalendarWidgetWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QCalendarWidgetWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QCalendarWidget, QCalendarWidgetWrap); return exports; } -NCalendarWidget *QCalendarWidgetWrap::getInternalInstance() { +QCalendarWidget *QCalendarWidgetWrap::getInternalInstance() { return this->instance; } @@ -42,22 +43,31 @@ QCalendarWidgetWrap::~QCalendarWidgetWrap() { QCalendarWidgetWrap::QCalendarWidgetWrap(const Napi::CallbackInfo &info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap *parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NCalendarWidget( - parentWidgetWrap - ->getInternalInstance()); // this sets the parent to current widget - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NCalendarWidget(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap *parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NCalendarWidget(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, + "NodeGui: QCalendarWidgetWrap: Wrong number of " + "arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QCalendarWidgetWrap::monthShown(const Napi::CallbackInfo &info) { diff --git a/src/cpp/lib/QtWidgets/QCheckBox/qcheckbox_wrap.cpp b/src/cpp/lib/QtWidgets/QCheckBox/qcheckbox_wrap.cpp index d4297db49..87d5c5879 100644 --- a/src/cpp/lib/QtWidgets/QCheckBox/qcheckbox_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QCheckBox/qcheckbox_wrap.cpp @@ -17,44 +17,40 @@ Napi::Object QCheckBoxWrap::init(Napi::Env env, Napi::Object exports) { QABSTRACTBUTTON_WRAPPED_METHODS_EXPORT_DEFINE(QCheckBoxWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QCheckBox, QCheckBoxWrap); return exports; } -NCheckBox* QCheckBoxWrap::getInternalInstance() { return this->instance; } +QCheckBox* QCheckBoxWrap::getInternalInstance() { return this->instance; } QCheckBoxWrap::QCheckBoxWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { - // --- if external --- - this->instance = info[0].As>().Data(); - if (info.Length() == 2) { - this->disableDeletion = info[1].As().Value(); - } - } else { - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NCheckBox(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NCheckBox(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { - this->instance = new NCheckBox(); - } else { - Napi::TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); } + } else { + Napi::TypeError::New( + env, "NodeGui: QCheckBoxWrap: Wrong number of arguments to constructor") + .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } -QCheckBoxWrap::~QCheckBoxWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } -} +QCheckBoxWrap::~QCheckBoxWrap() { extrautils::safeDelete(this->instance); } Napi::Value QCheckBoxWrap::checkState(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); diff --git a/src/cpp/lib/QtWidgets/QColorDialog/qcolordialog_wrap.cpp b/src/cpp/lib/QtWidgets/QColorDialog/qcolordialog_wrap.cpp index b41c3440f..5fb1e1ad3 100644 --- a/src/cpp/lib/QtWidgets/QColorDialog/qcolordialog_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QColorDialog/qcolordialog_wrap.cpp @@ -27,10 +27,11 @@ Napi::Object QColorDialogWrap::init(Napi::Env env, Napi::Object exports) { QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QColorDialogWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QColorDialog, QColorDialogWrap); return exports; } -NColorDialog* QColorDialogWrap::getInternalInstance() { return this->instance; } +QColorDialog* QColorDialogWrap::getInternalInstance() { return this->instance; } QColorDialogWrap::~QColorDialogWrap() { extrautils::safeDelete(this->instance); @@ -39,21 +40,30 @@ QColorDialogWrap::~QColorDialogWrap() { QColorDialogWrap::QColorDialogWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - QWidget* parent = parentWidgetWrap->getInternalInstance(); - this->instance = new NColorDialog(parent); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NColorDialog(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NColorDialog(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QColorDialogWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QColorDialogWrap::selectedColor(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QComboBox/qcombobox_wrap.cpp b/src/cpp/lib/QtWidgets/QComboBox/qcombobox_wrap.cpp index 714e7402e..6af18286f 100644 --- a/src/cpp/lib/QtWidgets/QComboBox/qcombobox_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QComboBox/qcombobox_wrap.cpp @@ -66,30 +66,39 @@ Napi::Object QComboBoxWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QComboBoxWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QComboBox, QComboBoxWrap); return exports; } -NComboBox* QComboBoxWrap::getInternalInstance() { return this->instance; } +QComboBox* QComboBoxWrap::getInternalInstance() { return this->instance; } + QComboBoxWrap::~QComboBoxWrap() { extrautils::safeDelete(this->instance); } QComboBoxWrap::QComboBoxWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - - this->instance = new NComboBox(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NComboBox(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NComboBox(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QComboBoxWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QComboBoxWrap::addItem(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QDateEdit/qdateedit_wrap.cpp b/src/cpp/lib/QtWidgets/QDateEdit/qdateedit_wrap.cpp index 93ea2fa5b..7bcd6edd2 100644 --- a/src/cpp/lib/QtWidgets/QDateEdit/qdateedit_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QDateEdit/qdateedit_wrap.cpp @@ -12,28 +12,37 @@ Napi::Object QDateEditWrap::init(Napi::Env env, Napi::Object exports) { {QDATETIMEEDIT_WRAPPED_METHODS_EXPORT_DEFINE(QDateEditWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QDateEdit, QDateEditWrap); return exports; } -NDateEdit* QDateEditWrap::getInternalInstance() { return this->instance; } +QDateEdit* QDateEditWrap::getInternalInstance() { return this->instance; } QDateEditWrap::QDateEditWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NDateEdit(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NDateEdit(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NDateEdit(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QDateEditWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QDateEditWrap::~QDateEditWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.cpp b/src/cpp/lib/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.cpp index 776e8bc2c..0082be0a7 100644 --- a/src/cpp/lib/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.cpp @@ -12,30 +12,41 @@ Napi::Object QDateTimeEditWrap::init(Napi::Env env, Napi::Object exports) { {QDATETIMEEDIT_WRAPPED_METHODS_EXPORT_DEFINE(QDateTimeEditWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QDateTimeEdit, QDateTimeEditWrap); return exports; } -NDateTimeEdit* QDateTimeEditWrap::getInternalInstance() { +QDateTimeEdit* QDateTimeEditWrap::getInternalInstance() { return this->instance; } QDateTimeEditWrap::QDateTimeEditWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NDateTimeEdit(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NDateTimeEdit(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NDateTimeEdit(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QDateTimeEditWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QDateTimeEditWrap::~QDateTimeEditWrap() { diff --git a/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp b/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp index be8b1c696..8c4ff98bc 100644 --- a/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp @@ -16,29 +16,37 @@ Napi::Object QDialWrap::init(Napi::Env env, Napi::Object exports) { {QABSTRACTSLIDER_WRAPPED_METHODS_EXPORT_DEFINE(QDialWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QDial, QDialWrap); return exports; } -NDial* QDialWrap::getInternalInstance() { return this->instance; } +QDial* QDialWrap::getInternalInstance() { return this->instance; } QDialWrap::QDialWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NDial(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NDial(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NDial(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QDialWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QDialWrap::~QDialWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QDialog/qdialog_wrap.cpp b/src/cpp/lib/QtWidgets/QDialog/qdialog_wrap.cpp index 0fa9bc0c0..a9faa1e2c 100644 --- a/src/cpp/lib/QtWidgets/QDialog/qdialog_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QDialog/qdialog_wrap.cpp @@ -11,33 +11,37 @@ Napi::Object QDialogWrap::init(Napi::Env env, Napi::Object exports) { env, CLASSNAME, {QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QDialogWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QDialog, QDialogWrap); return exports; } -NDialog *QDialogWrap::getInternalInstance() { return this->instance; } +QDialog *QDialogWrap::getInternalInstance() { return this->instance; } QDialogWrap::~QDialogWrap() { extrautils::safeDelete(this->instance); } QDialogWrap::QDialogWrap(const Napi::CallbackInfo &info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NDialog(); + } else if (argCount == 1) { if (info[0].IsExternal()) { - this->instance = - new NDialog(info[0].As>().Data()); + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); - QDialogWrap *parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); + NodeWidgetWrap *parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NDialog(parentWidgetWrap->getInternalInstance()); } - } else if (info.Length() == 0) { - this->instance = new NDialog(); } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QDialogWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } diff --git a/src/cpp/lib/QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.cpp b/src/cpp/lib/QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.cpp index 55a6157c4..b3ab1d073 100644 --- a/src/cpp/lib/QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.cpp @@ -16,31 +16,41 @@ Napi::Object QDoubleSpinBoxWrap::init(Napi::Env env, Napi::Object exports) { QABSTRACTSPINBOX_WRAPPED_METHODS_EXPORT_DEFINE(QDoubleSpinBoxWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QDoubleSpinBox, QDoubleSpinBoxWrap); return exports; } -NDoubleSpinBox* QDoubleSpinBoxWrap::getInternalInstance() { +QDoubleSpinBox* QDoubleSpinBoxWrap::getInternalInstance() { return this->instance; } QDoubleSpinBoxWrap::QDoubleSpinBoxWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = - new NDoubleSpinBox(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NDoubleSpinBox(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NDoubleSpinBox(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QDoubleSpinBoxWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QDoubleSpinBoxWrap::~QDoubleSpinBoxWrap() { diff --git a/src/cpp/lib/QtWidgets/QErrorMessage/qerrormessage_wrap.cpp b/src/cpp/lib/QtWidgets/QErrorMessage/qerrormessage_wrap.cpp index 9cbb30cf7..a23f4551b 100644 --- a/src/cpp/lib/QtWidgets/QErrorMessage/qerrormessage_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QErrorMessage/qerrormessage_wrap.cpp @@ -16,10 +16,11 @@ Napi::Object QErrorMessageWrap::init(Napi::Env env, Napi::Object exports) { QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QErrorMessageWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QErrorMessage, QErrorMessageWrap); return exports; } -NErrorMessage* QErrorMessageWrap::getInternalInstance() { +QErrorMessage* QErrorMessageWrap::getInternalInstance() { return this->instance; } @@ -30,21 +31,30 @@ QErrorMessageWrap::~QErrorMessageWrap() { QErrorMessageWrap::QErrorMessageWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - QWidget* parent = parentWidgetWrap->getInternalInstance(); - this->instance = new NErrorMessage(parent); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NErrorMessage(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NErrorMessage(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QErrorMessageWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QErrorMessageWrap::showMessage(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QFileDialog/qfiledialog_wrap.cpp b/src/cpp/lib/QtWidgets/QFileDialog/qfiledialog_wrap.cpp index c3766f534..f3e38d31a 100644 --- a/src/cpp/lib/QtWidgets/QFileDialog/qfiledialog_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QFileDialog/qfiledialog_wrap.cpp @@ -24,16 +24,19 @@ Napi::Object QFileDialogWrap::init(Napi::Env env, Napi::Object exports) { QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QFileDialogWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QFileDialog, QFileDialogWrap); return exports; } -NFileDialog* QFileDialogWrap::getInternalInstance() { return this->instance; } +QFileDialog* QFileDialogWrap::getInternalInstance() { return this->instance; } + QFileDialogWrap::~QFileDialogWrap() { extrautils::safeDelete(this->instance); } QFileDialogWrap::QFileDialogWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 4) { + size_t argCount = info.Length(); + if (argCount == 4) { Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); @@ -45,15 +48,15 @@ QFileDialogWrap::QFileDialogWrap(const Napi::CallbackInfo& info) QString filter = QString::fromUtf8(info[3].As().Utf8Value().c_str()); this->instance = new NFileDialog(parent, caption, directory, filter); - } else if (info.Length() == 0) { + } else if (argCount == 0) { this->instance = new NFileDialog(); } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, + "NodeGui: QFileDialogWrap: Wrong number of arguments") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QFileDialogWrap::supportedSchemes(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QFontDialog/qfontdialog_wrap.cpp b/src/cpp/lib/QtWidgets/QFontDialog/qfontdialog_wrap.cpp index 5194c9f15..bec840ae8 100644 --- a/src/cpp/lib/QtWidgets/QFontDialog/qfontdialog_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QFontDialog/qfontdialog_wrap.cpp @@ -18,31 +18,40 @@ Napi::Object QFontDialogWrap::init(Napi::Env env, Napi::Object exports) { QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QFontDialogWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QFontDialog, QFontDialogWrap); return exports; } -NFontDialog* QFontDialogWrap::getInternalInstance() { return this->instance; } +QFontDialog* QFontDialogWrap::getInternalInstance() { return this->instance; } QFontDialogWrap::~QFontDialogWrap() { extrautils::safeDelete(this->instance); } QFontDialogWrap::QFontDialogWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - QWidget* parent = parentWidgetWrap->getInternalInstance(); - this->instance = new NFontDialog(parent); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NFontDialog(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NFontDialog(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QFontDialogWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QFontDialogWrap::selectedFont(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QFrame/qframe_wrap.cpp b/src/cpp/lib/QtWidgets/QFrame/qframe_wrap.cpp index cb337b2a1..c32441204 100644 --- a/src/cpp/lib/QtWidgets/QFrame/qframe_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QFrame/qframe_wrap.cpp @@ -11,28 +11,37 @@ Napi::Object QFrameWrap::init(Napi::Env env, Napi::Object exports) { env, CLASSNAME, {QFRAME_WRAPPED_METHODS_EXPORT_DEFINE(QFrameWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QFrame, QFrameWrap); return exports; } -NFrame* QFrameWrap::getInternalInstance() { return this->instance; } +QFrame* QFrameWrap::getInternalInstance() { return this->instance; } QFrameWrap::~QFrameWrap() { extrautils::safeDelete(this->instance); } QFrameWrap::QFrameWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NFrame(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NFrame(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NFrame(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QFrameWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } diff --git a/src/cpp/lib/QtWidgets/QGraphicsBlurEffect/qgraphicsblureffect_wrap.cpp b/src/cpp/lib/QtWidgets/QGraphicsBlurEffect/qgraphicsblureffect_wrap.cpp index 0786be2a5..12d8e719a 100644 --- a/src/cpp/lib/QtWidgets/QGraphicsBlurEffect/qgraphicsblureffect_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QGraphicsBlurEffect/qgraphicsblureffect_wrap.cpp @@ -2,6 +2,7 @@ #include "Extras/Utils/nutils.h" #include "QtCore/QObject/qobject_wrap.h" +#include "QtWidgets/QWidget/qwidget_wrap.h" Napi::FunctionReference QGraphicsBlurEffectWrap::constructor; @@ -14,10 +15,11 @@ Napi::Object QGraphicsBlurEffectWrap::init(Napi::Env env, {QGRAPHICSEFFECT_WRAPPED_METHODS_EXPORT_DEFINE(QGraphicsBlurEffectWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QGraphicsBlurEffect, QGraphicsBlurEffectWrap); return exports; } -NGraphicsBlurEffect* QGraphicsBlurEffectWrap::getInternalInstance() { +QGraphicsBlurEffect* QGraphicsBlurEffectWrap::getInternalInstance() { return this->instance; } @@ -28,16 +30,26 @@ QGraphicsBlurEffectWrap::~QGraphicsBlurEffectWrap() { QGraphicsBlurEffectWrap::QGraphicsBlurEffectWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - QObjectWrap* parentObjectWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = - new NGraphicsBlurEffect(parentObjectWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NGraphicsBlurEffect(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NGraphicsBlurEffect(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, + "NodeGui: QGraphicsBlurEffectWrap: Wrong number of " + "arguments to constructor") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); diff --git a/src/cpp/lib/QtWidgets/QGraphicsDropShadowEffect/qgraphicsdropshadoweffect_wrap.cpp b/src/cpp/lib/QtWidgets/QGraphicsDropShadowEffect/qgraphicsdropshadoweffect_wrap.cpp index 51e3bd093..b7e9ebb0c 100644 --- a/src/cpp/lib/QtWidgets/QGraphicsDropShadowEffect/qgraphicsdropshadoweffect_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QGraphicsDropShadowEffect/qgraphicsdropshadoweffect_wrap.cpp @@ -2,6 +2,7 @@ #include "Extras/Utils/nutils.h" #include "QtCore/QObject/qobject_wrap.h" +#include "QtWidgets/QWidget/qwidget_wrap.h" Napi::FunctionReference QGraphicsDropShadowEffectWrap::constructor; @@ -15,10 +16,12 @@ Napi::Object QGraphicsDropShadowEffectWrap::init(Napi::Env env, QGraphicsDropShadowEffectWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QGraphicsDropShadowEffect, + QGraphicsDropShadowEffectWrap); return exports; } -NGraphicsDropShadowEffect* +QGraphicsDropShadowEffect* QGraphicsDropShadowEffectWrap::getInternalInstance() { return this->instance; } @@ -31,16 +34,28 @@ QGraphicsDropShadowEffectWrap::QGraphicsDropShadowEffectWrap( const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - QObjectWrap* parentObjectWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = - new NGraphicsDropShadowEffect(parentObjectWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NGraphicsDropShadowEffect(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = + info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NGraphicsDropShadowEffect( + parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QGraphicsDropShadowEffectWrap: Wrong number of " + "arguments to constructor") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); diff --git a/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp b/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp index de09979fc..7786f61d3 100644 --- a/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp @@ -35,24 +35,36 @@ Napi::Object QGridLayoutWrap::init(Napi::Env env, Napi::Object exports) { QLAYOUT_WRAPPED_METHODS_EXPORT_DEFINE(QGridLayoutWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QGridLayout, QGridLayoutWrap); return exports; } -NGridLayout* QGridLayoutWrap::getInternalInstance() { return this->instance; } +QGridLayout* QGridLayoutWrap::getInternalInstance() { return this->instance; } + QGridLayoutWrap::~QGridLayoutWrap() { extrautils::safeDelete(this->instance); } QGridLayoutWrap::QGridLayoutWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NGridLayout(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NGridLayout(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NGridLayout(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QGridLayoutWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); diff --git a/src/cpp/lib/QtWidgets/QGroupBox/qgroupbox_wrap.cpp b/src/cpp/lib/QtWidgets/QGroupBox/qgroupbox_wrap.cpp index 2b90ed7ba..34bd58850 100644 --- a/src/cpp/lib/QtWidgets/QGroupBox/qgroupbox_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QGroupBox/qgroupbox_wrap.cpp @@ -13,29 +13,38 @@ Napi::Object QGroupBoxWrap::init(Napi::Env env, Napi::Object exports) { Napi::Function func = DefineClass( env, CLASSNAME, {QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QGroupBoxWrap)}); constructor = Napi::Persistent(func); + QOBJECT_REGISTER_WRAPPER(QGroupBox, QGroupBoxWrap); exports.Set(CLASSNAME, func); return exports; } -NGroupBox* QGroupBoxWrap::getInternalInstance() { return this->instance; } +QGroupBox* QGroupBoxWrap::getInternalInstance() { return this->instance; } QGroupBoxWrap::QGroupBoxWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NGroupBox(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NGroupBox(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NGroupBox(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QGroupBoxWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } QGroupBoxWrap::~QGroupBoxWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QHeaderView/qheaderview_wrap.cpp b/src/cpp/lib/QtWidgets/QHeaderView/qheaderview_wrap.cpp index 3bc9ef2d9..d9333fd05 100644 --- a/src/cpp/lib/QtWidgets/QHeaderView/qheaderview_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QHeaderView/qheaderview_wrap.cpp @@ -111,27 +111,21 @@ Napi::Object QHeaderViewWrap::init(Napi::Env env, Napi::Object exports) { }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QHeaderView, QHeaderViewWrap); return exports; } QHeaderView* QHeaderViewWrap::getInternalInstance() { return this->instance; } -QHeaderViewWrap::~QHeaderViewWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } -} +QHeaderViewWrap::~QHeaderViewWrap() { extrautils::safeDelete(this->instance); } QHeaderViewWrap::QHeaderViewWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - size_t len = info.Length(); - - this->disableDeletion = false; - if (len == 1) { + size_t argCount = info.Length(); + if (argCount == 1) { if (info[0].IsExternal()) { this->instance = info[0].As>().Data(); - this->disableDeletion = true; } else { int orientation = info[0].As().Int32Value(); this->instance = @@ -152,8 +146,8 @@ QHeaderViewWrap::QHeaderViewWrap(const Napi::CallbackInfo& info) this->rawData = nullptr; FlexItem* item = dynamic_cast(this->getInternalInstance()); if (item) { - this->rawData = extrautils::configureQWidget(this->getInternalInstance(), - item->getFlexNode(), false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } } diff --git a/src/cpp/lib/QtWidgets/QInputDialog/qinputdialog_wrap.cpp b/src/cpp/lib/QtWidgets/QInputDialog/qinputdialog_wrap.cpp index cdeec107d..0cc838c6d 100644 --- a/src/cpp/lib/QtWidgets/QInputDialog/qinputdialog_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QInputDialog/qinputdialog_wrap.cpp @@ -56,10 +56,12 @@ Napi::Object QInputDialogWrap::init(Napi::Env env, Napi::Object exports) { QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QInputDialogWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QInputDialog, QInputDialogWrap); return exports; } -NInputDialog* QInputDialogWrap::getInternalInstance() { return this->instance; } +QInputDialog* QInputDialogWrap::getInternalInstance() { return this->instance; } + QInputDialogWrap::~QInputDialogWrap() { extrautils::safeDelete(this->instance); } @@ -67,20 +69,30 @@ QInputDialogWrap::~QInputDialogWrap() { QInputDialogWrap::QInputDialogWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NInputDialog(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NInputDialog(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NInputDialog(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QInputDialogWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QInputDialogWrap::setCancelButtonText( diff --git a/src/cpp/lib/QtWidgets/QLCDNumber/qlcdnumber_wrap.cpp b/src/cpp/lib/QtWidgets/QLCDNumber/qlcdnumber_wrap.cpp index 33241d646..99f790cf5 100644 --- a/src/cpp/lib/QtWidgets/QLCDNumber/qlcdnumber_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QLCDNumber/qlcdnumber_wrap.cpp @@ -20,31 +20,41 @@ Napi::Object QLCDNumberWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("setOctMode", &QLCDNumberWrap::setOctMode), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLCDNumberWrap)}); constructor = Napi::Persistent(func); + QOBJECT_REGISTER_WRAPPER(QLCDNumber, QLCDNumberWrap); exports.Set(CLASSNAME, func); return exports; } -NLCDNumber* QLCDNumberWrap::getInternalInstance() { return this->instance; } +QLCDNumber* QLCDNumberWrap::getInternalInstance() { return this->instance; } QLCDNumberWrap::~QLCDNumberWrap() { extrautils::safeDelete(this->instance); } QLCDNumberWrap::QLCDNumberWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NLCDNumber(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NLCDNumber(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NLCDNumber(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QLCDNumberWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QLCDNumberWrap::checkOverflow(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QLabel/qlabel_wrap.cpp b/src/cpp/lib/QtWidgets/QLabel/qlabel_wrap.cpp index 59f614084..abe0ed4ff 100644 --- a/src/cpp/lib/QtWidgets/QLabel/qlabel_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QLabel/qlabel_wrap.cpp @@ -18,6 +18,7 @@ Napi::Object QLabelWrap::init(Napi::Env env, Napi::Object exports) { {InstanceMethod("setSelection", &QLabelWrap::setSelection), InstanceMethod("selectionStart", &QLabelWrap::selectionStart), InstanceMethod("setBuddy", &QLabelWrap::setBuddy), + InstanceMethod("buddy", &QLabelWrap::buddy), InstanceMethod("clear", &QLabelWrap::clear), InstanceMethod("setMovie", &QLabelWrap::setMovie), InstanceMethod("setNumDouble", &QLabelWrap::setNumDouble), @@ -27,31 +28,46 @@ Napi::Object QLabelWrap::init(Napi::Env env, Napi::Object exports) { QFRAME_WRAPPED_METHODS_EXPORT_DEFINE(QLabelWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QLabel, QLabelWrap); return exports; } -NLabel* QLabelWrap::getInternalInstance() { return this->instance; } +QLabel* QLabelWrap::getInternalInstance() { return this->instance; } QLabelWrap::~QLabelWrap() { extrautils::safeDelete(this->instance); } QLabelWrap::QLabelWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NLabel(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NLabel(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NLabel(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QLabelWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - auto flexNode = this->getInternalInstance()->getFlexNode(); - YGNodeSetNodeType(flexNode, YGNodeType::YGNodeTypeText); + + YogaWidget* yogaWidget = + dynamic_cast(this->getInternalInstance()); + if (yogaWidget) { + auto flexNode = yogaWidget->getFlexNode(); + YGNodeSetNodeType(flexNode, YGNodeType::YGNodeTypeText); + } this->rawData = - extrautils::configureQWidget(this->getInternalInstance(), flexNode, true); + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QLabelWrap::setSelection(const Napi::CallbackInfo& info) { @@ -76,6 +92,16 @@ Napi::Value QLabelWrap::setBuddy(const Napi::CallbackInfo& info) { return env.Null(); } +Napi::Value QLabelWrap::buddy(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + QObject* parent = this->instance->buddy(); + if (parent) { + return WrapperCache::instance.getWrapper(env, parent); + } else { + return env.Null(); + } +} + Napi::Value QLabelWrap::clear(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); this->instance->clear(); diff --git a/src/cpp/lib/QtWidgets/QLayout/qlayout_wrap.cpp b/src/cpp/lib/QtWidgets/QLayout/qlayout_wrap.cpp index 8d616e135..97f35af4e 100644 --- a/src/cpp/lib/QtWidgets/QLayout/qlayout_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QLayout/qlayout_wrap.cpp @@ -3,16 +3,29 @@ #include "Extras/Utils/nutils.h" Napi::FunctionReference QLayoutWrap::constructor; -void QLayoutWrap::init(Napi::Env env) { +void QLayoutWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QLayout"; Napi::Function func = DefineClass( env, CLASSNAME, {QLAYOUT_WRAPPED_METHODS_EXPORT_DEFINE(QLayoutWrap)}); constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QLayout, QLayoutWrap); } -NLayout* QLayoutWrap::getInternalInstance() { return this->instance; } +QLayout* QLayoutWrap::getInternalInstance() { return this->instance; } QLayoutWrap::QLayoutWrap(const Napi::CallbackInfo& info) - : Napi::ObjectWrap(info) {} + : Napi::ObjectWrap(info) { + Napi::Env env = info.Env(); + size_t argCount = info.Length(); + if (argCount == 1 && info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + Napi::TypeError::New(env, "NodeGui: QLayoutWrap: Wrong number of arguments") + .ThrowAsJavaScriptException(); + } + this->rawData = extrautils::configureQObject(this->getInternalInstance()); +} QLayoutWrap::~QLayoutWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QLineEdit/qlineedit_wrap.cpp b/src/cpp/lib/QtWidgets/QLineEdit/qlineedit_wrap.cpp index 4581a5f6b..56eb00382 100644 --- a/src/cpp/lib/QtWidgets/QLineEdit/qlineedit_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QLineEdit/qlineedit_wrap.cpp @@ -40,28 +40,37 @@ Napi::Object QLineEditWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLineEditWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QLineEdit, QLineEditWrap); return exports; } -NLineEdit* QLineEditWrap::getInternalInstance() { return this->instance; } +QLineEdit* QLineEditWrap::getInternalInstance() { return this->instance; } QLineEditWrap::QLineEditWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NLineEdit(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NLineEdit(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NLineEdit(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QLineEditWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QLineEditWrap::~QLineEditWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QListView/qlistview_wrap.cpp b/src/cpp/lib/QtWidgets/QListView/qlistview_wrap.cpp index c253f28ae..cedc6f575 100644 --- a/src/cpp/lib/QtWidgets/QListView/qlistview_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QListView/qlistview_wrap.cpp @@ -11,42 +11,38 @@ Napi::Object QListViewWrap::init(Napi::Env env, Napi::Object exports) { env, CLASSNAME, {QLISTVIEW_WRAPPED_METHODS_EXPORT_DEFINE(QListViewWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QListView, QListViewWrap); return exports; } -NListView* QListViewWrap::getInternalInstance() { return this->instance; } +QListView* QListViewWrap::getInternalInstance() { return this->instance; } -QListViewWrap::~QListViewWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } -} +QListViewWrap::~QListViewWrap() { extrautils::safeDelete(this->instance); } QListViewWrap::QListViewWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { - // --- if external --- - this->instance = info[0].As>().Data(); - if (info.Length() == 2) { - this->disableDeletion = info[1].As().Value(); - } - } else { - // --- regular cases --- - if (info.Length() == 1) { + + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NListView(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NListView(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { - this->instance = new NListView(); - } else { - Napi::TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); } + } else { + Napi::TypeError::New( + env, "NodeGui: QListViewWrap: Wrong number of arguments to constructor") + .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } diff --git a/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp b/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp index dbed200be..b2a003c7c 100644 --- a/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp @@ -41,30 +41,40 @@ Napi::Object QListWidgetWrap::init(Napi::Env env, Napi::Object exports) { QLISTVIEW_WRAPPED_METHODS_EXPORT_DEFINE(QListWidgetWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QListWidget, QListWidgetWrap); return exports; } -NListWidget* QListWidgetWrap::getInternalInstance() { return this->instance; } +QListWidget* QListWidgetWrap::getInternalInstance() { return this->instance; } QListWidgetWrap::~QListWidgetWrap() { extrautils::safeDelete(this->instance); } QListWidgetWrap::QListWidgetWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NListWidget(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NListWidget(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NListWidget(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QListWidgetWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QListWidgetWrap::addItem(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QListWidgetItem/qlistwidgetitem_wrap.cpp b/src/cpp/lib/QtWidgets/QListWidgetItem/qlistwidgetitem_wrap.cpp index cb8299258..27038fb68 100644 --- a/src/cpp/lib/QtWidgets/QListWidgetItem/qlistwidgetitem_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QListWidgetItem/qlistwidgetitem_wrap.cpp @@ -67,22 +67,24 @@ QListWidgetItemWrap::QListWidgetItemWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { + size_t argCount = info.Length(); + if (argCount > 0 && info[0].IsExternal()) { // --- if external --- this->instance = info[0].As>().Data(); - if (info.Length() == 2) { + if (argCount == 2) { this->disableDeletion = info[1].As().Value(); } } else { // --- regular cases --- - if (info.Length() == 1) { + if (argCount == 1) { QString text = QString::fromUtf8(info[0].As().Utf8Value().c_str()); this->instance = new QListWidgetItem(text); - } else if (info.Length() == 0) { + } else if (argCount == 0) { this->instance = new QListWidgetItem(); } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QListWidgetItemWrap: Wrong number of arguments") .ThrowAsJavaScriptException(); } } diff --git a/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp b/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp index 677507be7..a97159ea8 100644 --- a/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp @@ -16,40 +16,50 @@ Napi::Object QMainWindowWrap::init(Napi::Env env, Napi::Object exports) { Napi::Function func = DefineClass( env, CLASSNAME, {InstanceMethod("setCentralWidget", &QMainWindowWrap::setCentralWidget), + InstanceMethod("centralWidget", &QMainWindowWrap::centralWidget), InstanceMethod("takeCentralWidget", &QMainWindowWrap::takeCentralWidget), InstanceMethod("setMenuBar", &QMainWindowWrap::setMenuBar), + InstanceMethod("menuBar", &QMainWindowWrap::menuBar), InstanceMethod("setMenuWidget", &QMainWindowWrap::setMenuWidget), InstanceMethod("center", &QMainWindowWrap::center), InstanceMethod("setStatusBar", &QMainWindowWrap::setStatusBar), InstanceMethod("statusBar", &QMainWindowWrap::statusBar), - QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMainWindowWrap) - - }); + QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMainWindowWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QMainWindow, QMainWindowWrap); return exports; } -NMainWindow* QMainWindowWrap::getInternalInstance() { return this->instance; } +QMainWindow* QMainWindowWrap::getInternalInstance() { return this->instance; } QMainWindowWrap::~QMainWindowWrap() { extrautils::safeDelete(this->instance); } QMainWindowWrap::QMainWindowWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NMainWindow(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NMainWindow(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NMainWindow(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QMainWindowWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode()); + this->rawData = extrautils::configureQWidget(this->getInternalInstance()); } Napi::Value QMainWindowWrap::setCentralWidget(const Napi::CallbackInfo& info) { @@ -57,15 +67,32 @@ Napi::Value QMainWindowWrap::setCentralWidget(const Napi::CallbackInfo& info) { Napi::Object widgetObject = info[0].As(); NodeWidgetWrap* centralWidget = Napi::ObjectWrap::Unwrap(widgetObject); - this->instance->setCentralWidget(centralWidget->getInternalInstance()); + if (centralWidget != nullptr) { + this->instance->setCentralWidget(centralWidget->getInternalInstance()); + } else { + this->instance->setCentralWidget(nullptr); + } return env.Null(); } +Napi::Value QMainWindowWrap::centralWidget(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + QWidget* widget = this->instance->centralWidget(); + if (widget) { + return WrapperCache::instance.getWrapper(env, widget); + } else { + return env.Null(); + } +} + Napi::Value QMainWindowWrap::takeCentralWidget(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); - this->instance->takeCentralWidget(); - // We will not return the value here since we are doing it in js side anyway - return env.Null(); + QWidget* widget = this->instance->takeCentralWidget(); + if (widget) { + return WrapperCache::instance.getWrapper(env, widget); + } else { + return env.Null(); + } } Napi::Value QMainWindowWrap::setMenuBar(const Napi::CallbackInfo& info) { @@ -119,6 +146,19 @@ Napi::Value QMainWindowWrap::setStatusBar(const Napi::CallbackInfo& info) { Napi::Value QMainWindowWrap::statusBar(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); QStatusBar* statusBar = this->instance->statusBar(); - - return QStatusBarWrap::fromQStatusBar(env, statusBar); + if (statusBar) { + return WrapperCache::instance.getWrapper(env, statusBar); + } else { + return env.Null(); + } +} + +Napi::Value QMainWindowWrap::menuBar(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + QMenuBar* menuBar = this->instance->menuBar(); + if (menuBar) { + return WrapperCache::instance.getWrapper(env, menuBar); + } else { + return env.Null(); + } } diff --git a/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp b/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp index 50964ef3b..a68dfa044 100644 --- a/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp @@ -23,28 +23,37 @@ Napi::Object QMenuWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMenuWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QMenu, QMenuWrap); return exports; } -NMenu* QMenuWrap::getInternalInstance() { return this->instance; } +QMenu* QMenuWrap::getInternalInstance() { return this->instance; } QMenuWrap::QMenuWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NMenu(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NMenu(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NMenu(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QMenuWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QMenuWrap::~QMenuWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QMenuBar/qmenubar_wrap.cpp b/src/cpp/lib/QtWidgets/QMenuBar/qmenubar_wrap.cpp index 7b5b557cf..e063c3632 100644 --- a/src/cpp/lib/QtWidgets/QMenuBar/qmenubar_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QMenuBar/qmenubar_wrap.cpp @@ -21,32 +21,36 @@ Napi::Object QMenuBarWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMenuBarWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QMenuBar, QMenuBarWrap); return exports; } -NMenuBar* QMenuBarWrap::getInternalInstance() { return this->instance; } +QMenuBar* QMenuBarWrap::getInternalInstance() { return this->instance; } QMenuBarWrap::QMenuBarWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NMenuBar(); + } else if (argCount == 1) { if (info[0].IsExternal()) { - this->instance = - new NMenuBar(info[0].As>().Data()); + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NMenuBar(parentWidgetWrap->getInternalInstance()); } - } else if (info.Length() == 0) { - this->instance = new NMenuBar(); } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QMenuBarWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode()); + this->rawData = extrautils::configureQWidget(this->getInternalInstance()); } QMenuBarWrap::~QMenuBarWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QMessageBox/qmessagebox_wrap.cpp b/src/cpp/lib/QtWidgets/QMessageBox/qmessagebox_wrap.cpp index 2eba06311..edb3c3bcc 100644 --- a/src/cpp/lib/QtWidgets/QMessageBox/qmessagebox_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QMessageBox/qmessagebox_wrap.cpp @@ -23,30 +23,41 @@ Napi::Object QMessageBoxWrap::init(Napi::Env env, Napi::Object exports) { QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QMessageBoxWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QMessageBox, QMessageBoxWrap); return exports; } -NMessageBox* QMessageBoxWrap::getInternalInstance() { return this->instance; } +QMessageBox* QMessageBoxWrap::getInternalInstance() { return this->instance; } + QMessageBoxWrap::~QMessageBoxWrap() { extrautils::safeDelete(this->instance); } QMessageBoxWrap::QMessageBoxWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NMessageBox(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NMessageBox(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NMessageBox(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QMessageBoxWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } this->instance->setStandardButtons(QMessageBox::NoButton); - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QMessageBoxWrap::setDefaultButton(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QPlainTextEdit/qplaintextedit_wrap.cpp b/src/cpp/lib/QtWidgets/QPlainTextEdit/qplaintextedit_wrap.cpp index 944d068f4..bd6cd6c5d 100644 --- a/src/cpp/lib/QtWidgets/QPlainTextEdit/qplaintextedit_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QPlainTextEdit/qplaintextedit_wrap.cpp @@ -27,31 +27,41 @@ Napi::Object QPlainTextEditWrap::init(Napi::Env env, Napi::Object exports) { QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(QPlainTextEditWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QPlainTextEdit, QPlainTextEditWrap); return exports; } -NPlainTextEdit *QPlainTextEditWrap::getInternalInstance() { +QPlainTextEdit *QPlainTextEditWrap::getInternalInstance() { return this->instance; } QPlainTextEditWrap::QPlainTextEditWrap(const Napi::CallbackInfo &info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap *parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = - new NPlainTextEdit(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NPlainTextEdit(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap *parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NPlainTextEdit(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QPlainTextEditWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QPlainTextEditWrap::~QPlainTextEditWrap() { diff --git a/src/cpp/lib/QtWidgets/QProgressBar/qprogressbar_wrap.cpp b/src/cpp/lib/QtWidgets/QProgressBar/qprogressbar_wrap.cpp index 5e514d31c..f7cf93c4a 100644 --- a/src/cpp/lib/QtWidgets/QProgressBar/qprogressbar_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QProgressBar/qprogressbar_wrap.cpp @@ -18,10 +18,11 @@ Napi::Object QProgressBarWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QProgressBarWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QProgressBar, QProgressBarWrap); return exports; } -NProgressBar* QProgressBarWrap::getInternalInstance() { return this->instance; } +QProgressBar* QProgressBarWrap::getInternalInstance() { return this->instance; } QProgressBarWrap::~QProgressBarWrap() { extrautils::safeDelete(this->instance); @@ -30,20 +31,30 @@ QProgressBarWrap::~QProgressBarWrap() { QProgressBarWrap::QProgressBarWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NProgressBar(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NProgressBar(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NProgressBar(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QProgressBarWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QProgressBarWrap::resetFormat(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QProgressDialog/qprogressdialog_wrap.cpp b/src/cpp/lib/QtWidgets/QProgressDialog/qprogressdialog_wrap.cpp index b3442dbf3..d3e82faa9 100644 --- a/src/cpp/lib/QtWidgets/QProgressDialog/qprogressdialog_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QProgressDialog/qprogressdialog_wrap.cpp @@ -20,10 +20,11 @@ Napi::Object QProgressDialogWrap::init(Napi::Env env, Napi::Object exports) { QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QProgressDialogWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QProgressDialog, QProgressDialogWrap); return exports; } -NProgressDialog* QProgressDialogWrap::getInternalInstance() { +QProgressDialog* QProgressDialogWrap::getInternalInstance() { return this->instance; } @@ -34,21 +35,30 @@ QProgressDialogWrap::~QProgressDialogWrap() { QProgressDialogWrap::QProgressDialogWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - QWidget* parent = parentWidgetWrap->getInternalInstance(); - this->instance = new NProgressDialog(parent); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NProgressDialog(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NProgressDialog(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, + "NodeGui: QProgressDialogWrap: Wrong number of " + "arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QProgressDialogWrap::cancel(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QPushButton/qpushbutton_wrap.cpp b/src/cpp/lib/QtWidgets/QPushButton/qpushbutton_wrap.cpp index 88b9d32c8..6f5c64b30 100644 --- a/src/cpp/lib/QtWidgets/QPushButton/qpushbutton_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QPushButton/qpushbutton_wrap.cpp @@ -12,48 +12,45 @@ Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) { Napi::Function func = DefineClass( env, CLASSNAME, {InstanceMethod("setMenu", &QPushButtonWrap::setMenu), + InstanceMethod("menu", &QPushButtonWrap::menu), InstanceMethod("showMenu", &QPushButtonWrap::showMenu), QABSTRACTBUTTON_WRAPPED_METHODS_EXPORT_DEFINE(QPushButtonWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QPushButton, QPushButtonWrap); return exports; } -NPushButton* QPushButtonWrap::getInternalInstance() { return this->instance; } +QPushButton* QPushButtonWrap::getInternalInstance() { return this->instance; } -QPushButtonWrap::~QPushButtonWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } -} +QPushButtonWrap::~QPushButtonWrap() { extrautils::safeDelete(this->instance); } QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { - // --- if external --- - this->instance = info[0].As>().Data(); - if (info.Length() == 2) { - this->disableDeletion = info[1].As().Value(); - } - } else { - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NPushButton(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NPushButton(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { - this->instance = new NPushButton(); - } else { - Napi::TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); } + } else { + Napi::TypeError::New( + env, + "NodeGui: QPushButtonWrap: Wrong number of arguments to constructor") + .ThrowAsJavaScriptException(); } - - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QPushButtonWrap::setMenu(const Napi::CallbackInfo& info) { @@ -64,6 +61,16 @@ Napi::Value QPushButtonWrap::setMenu(const Napi::CallbackInfo& info) { return env.Null(); } +Napi::Value QPushButtonWrap::menu(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + QObject* menu = this->instance->menu(); + if (menu) { + return WrapperCache::instance.getWrapper(env, menu); + } else { + return env.Null(); + } +} + Napi::Value QPushButtonWrap::showMenu(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); this->instance->showMenu(); diff --git a/src/cpp/lib/QtWidgets/QRadioButton/qradiobutton_wrap.cpp b/src/cpp/lib/QtWidgets/QRadioButton/qradiobutton_wrap.cpp index 22cf2f72c..0e6ea18c5 100644 --- a/src/cpp/lib/QtWidgets/QRadioButton/qradiobutton_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QRadioButton/qradiobutton_wrap.cpp @@ -16,42 +16,41 @@ Napi::Object QRadioButtonWrap::init(Napi::Env env, Napi::Object exports) { {QABSTRACTBUTTON_WRAPPED_METHODS_EXPORT_DEFINE(QRadioButtonWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QRadioButton, QRadioButtonWrap); return exports; } -NRadioButton* QRadioButtonWrap::getInternalInstance() { return this->instance; } +QRadioButton* QRadioButtonWrap::getInternalInstance() { return this->instance; } QRadioButtonWrap::QRadioButtonWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { - // --- if external --- - this->instance = info[0].As>().Data(); - if (info.Length() == 2) { - this->disableDeletion = info[1].As().Value(); - } - } else { - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NRadioButton(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NRadioButton(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { - this->instance = new NRadioButton(); - } else { - Napi::TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); } + } else { + Napi::TypeError::New( + env, + "NodeGui: QRadioButtonWrap: Wrong number of arguments to constructor") + .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QRadioButtonWrap::~QRadioButtonWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } + extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QScrollArea/qscrollarea_wrap.cpp b/src/cpp/lib/QtWidgets/QScrollArea/qscrollarea_wrap.cpp index 1ea746c5b..934f3ef1d 100644 --- a/src/cpp/lib/QtWidgets/QScrollArea/qscrollarea_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QScrollArea/qscrollarea_wrap.cpp @@ -14,6 +14,7 @@ Napi::Object QScrollAreaWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("ensureWidgetVisible", &QScrollAreaWrap::ensureWidgetVisible), InstanceMethod("setWidget", &QScrollAreaWrap::setWidget), + InstanceMethod("widget", &QScrollAreaWrap::widget), InstanceMethod("takeWidget", &QScrollAreaWrap::takeWidget), InstanceMethod("setViewportMargins", &QScrollAreaWrap::setViewportMargins), @@ -21,38 +22,41 @@ Napi::Object QScrollAreaWrap::init(Napi::Env env, Napi::Object exports) { QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(QScrollAreaWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QScrollArea, QScrollAreaWrap); return exports; } -NScrollArea* QScrollAreaWrap::getInternalInstance() { return this->instance; } +QScrollArea* QScrollAreaWrap::getInternalInstance() { return this->instance; } -QScrollAreaWrap::~QScrollAreaWrap() { - extrautils::safeDelete(this->instance); - YGNodeFree(this->scrollNode); -} +QScrollAreaWrap::~QScrollAreaWrap() { extrautils::safeDelete(this->instance); } QScrollAreaWrap::QScrollAreaWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NScrollArea(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NScrollArea(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NScrollArea(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QScrollAreaWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->scrollNode = YGNodeNew(); - YGConfigSetUseWebDefaults(this->scrollNode->getConfig(), true); - FlexNodeContext* scrollNodeCtx = new FlexNodeContext(this->instance); - YGNodeSetContext(this->scrollNode, scrollNodeCtx); - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QScrollAreaWrap::ensureVisible(const Napi::CallbackInfo& info) { @@ -87,18 +91,31 @@ Napi::Value QScrollAreaWrap::setWidget(const Napi::CallbackInfo& info) { return env.Null(); } +Napi::Value QScrollAreaWrap::widget(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + QObject* widget = this->instance->widget(); + if (widget) { + return WrapperCache::instance.getWrapper(env, widget); + } else { + return env.Null(); + } +} + Napi::Value QScrollAreaWrap::takeWidget(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); - this->instance->takeWidget(); - // We will not return the value here since we are doing it in js side anyway - return env.Null(); + QObject* widget = this->instance->takeWidget(); + if (widget) { + return WrapperCache::instance.getWrapper(env, widget); + } else { + return env.Null(); + } } Napi::Value QScrollAreaWrap::setViewportMargins( const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); - NScrollArea* nScrollArea = qobject_cast(this->instance); - if (nScrollArea != nullptr) { + NScrollArea* nScrollArea = dynamic_cast(this->instance.data()); + if (nScrollArea) { int left = info[0].As().Int32Value(); int top = info[1].As().Int32Value(); int right = info[2].As().Int32Value(); @@ -110,17 +127,16 @@ Napi::Value QScrollAreaWrap::setViewportMargins( Napi::Value QScrollAreaWrap::viewportMargins(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); - NScrollArea* nScrollArea = qobject_cast(this->instance); - - QMargins margins; - if (nScrollArea != nullptr) { - margins = nScrollArea->viewportMargins(); + NScrollArea* nScrollArea = dynamic_cast(this->instance.data()); + if (nScrollArea) { + QMargins margins = nScrollArea->viewportMargins(); + Napi::Array resultNapi = Napi::Array::New(env, 4); + resultNapi[uint32_t(0)] = Napi::Number::From(env, margins.left()); + resultNapi[1] = Napi::Value::From(env, margins.top()); + resultNapi[2] = Napi::Value::From(env, margins.right()); + resultNapi[3] = Napi::Value::From(env, margins.bottom()); + return resultNapi; + } else { + return env.Null(); } - - Napi::Array resultNapi = Napi::Array::New(env, 4); - resultNapi[uint32_t(0)] = Napi::Number::From(env, margins.left()); - resultNapi[1] = Napi::Value::From(env, margins.top()); - resultNapi[2] = Napi::Value::From(env, margins.right()); - resultNapi[3] = Napi::Value::From(env, margins.bottom()); - return resultNapi; } diff --git a/src/cpp/lib/QtWidgets/QScrollBar/qscrollbar_wrap.cpp b/src/cpp/lib/QtWidgets/QScrollBar/qscrollbar_wrap.cpp index 75ad57ae7..df4f6d530 100644 --- a/src/cpp/lib/QtWidgets/QScrollBar/qscrollbar_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QScrollBar/qscrollbar_wrap.cpp @@ -15,29 +15,38 @@ Napi::Object QScrollBarWrap::init(Napi::Env env, Napi::Object exports) { {QABSTRACTSLIDER_WRAPPED_METHODS_EXPORT_DEFINE(QScrollBarWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QScrollBar, QScrollBarWrap); return exports; } -NScrollBar* QScrollBarWrap::getInternalInstance() { return this->instance; } +QScrollBar* QScrollBarWrap::getInternalInstance() { return this->instance; } QScrollBarWrap::QScrollBarWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NScrollBar(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NScrollBar(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NScrollBar(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QScrollBarWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QScrollBarWrap::~QScrollBarWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QShortcut/qshortcut_wrap.cpp b/src/cpp/lib/QtWidgets/QShortcut/qshortcut_wrap.cpp index f7bd8c582..ae8235f71 100644 --- a/src/cpp/lib/QtWidgets/QShortcut/qshortcut_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QShortcut/qshortcut_wrap.cpp @@ -21,21 +21,33 @@ Napi::Object QShortcutWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QShortcutWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QShortcut, QShortcutWrap); return exports; } -NShortcut* QShortcutWrap::getInternalInstance() { return this->instance; } +QShortcut* QShortcutWrap::getInternalInstance() { return this->instance; } QShortcutWrap::QShortcutWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NShortcut(parentWidgetWrap->getInternalInstance()); + size_t argCount = info.Length(); + + // Note: QShortcut object always need a parent or instance to wrap + + if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NShortcut(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QShortcutWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); diff --git a/src/cpp/lib/QtWidgets/QSlider/qslider_wrap.cpp b/src/cpp/lib/QtWidgets/QSlider/qslider_wrap.cpp index 4b1aecea7..51bdbf5a2 100644 --- a/src/cpp/lib/QtWidgets/QSlider/qslider_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QSlider/qslider_wrap.cpp @@ -15,29 +15,37 @@ Napi::Object QSliderWrap::init(Napi::Env env, Napi::Object exports) { {QABSTRACTSLIDER_WRAPPED_METHODS_EXPORT_DEFINE(QSliderWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QSlider, QSliderWrap); return exports; } -NSlider* QSliderWrap::getInternalInstance() { return this->instance; } +QSlider* QSliderWrap::getInternalInstance() { return this->instance; } QSliderWrap::QSliderWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NSlider(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NSlider(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NSlider(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QSliderWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QSliderWrap::~QSliderWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QSpinBox/qspinbox_wrap.cpp b/src/cpp/lib/QtWidgets/QSpinBox/qspinbox_wrap.cpp index dc081a7d6..677fb0d04 100644 --- a/src/cpp/lib/QtWidgets/QSpinBox/qspinbox_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QSpinBox/qspinbox_wrap.cpp @@ -15,28 +15,37 @@ Napi::Object QSpinBoxWrap::init(Napi::Env env, Napi::Object exports) { QABSTRACTSPINBOX_WRAPPED_METHODS_EXPORT_DEFINE(QSpinBoxWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QSpinBox, QSpinBoxWrap); return exports; } -NSpinBox* QSpinBoxWrap::getInternalInstance() { return this->instance; } +QSpinBox* QSpinBoxWrap::getInternalInstance() { return this->instance; } QSpinBoxWrap::QSpinBoxWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NSpinBox(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NSpinBox(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NSpinBox(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QSpinBoxWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QSpinBoxWrap::~QSpinBoxWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QSplitter/qsplitter_wrap.cpp b/src/cpp/lib/QtWidgets/QSplitter/qsplitter_wrap.cpp index c70a5f090..08aa13aeb 100644 --- a/src/cpp/lib/QtWidgets/QSplitter/qsplitter_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QSplitter/qsplitter_wrap.cpp @@ -22,30 +22,39 @@ Napi::Object QSplitterWrap::init(Napi::Env env, Napi::Object exports) { QFRAME_WRAPPED_METHODS_EXPORT_DEFINE(QSplitterWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QSplitter, QSplitterWrap); return exports; } -NSplitter* QSplitterWrap::getInternalInstance() { return this->instance; } +QSplitter* QSplitterWrap::getInternalInstance() { return this->instance; } QSplitterWrap::~QSplitterWrap() { extrautils::safeDelete(this->instance); } QSplitterWrap::QSplitterWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NSplitter(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NSplitter(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NSplitter(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QSplitterWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QSplitterWrap::addWidget(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QStackedWidget/qstackedwidget_wrap.cpp b/src/cpp/lib/QtWidgets/QStackedWidget/qstackedwidget_wrap.cpp index 8fe1f0f49..a7d6e5c00 100644 --- a/src/cpp/lib/QtWidgets/QStackedWidget/qstackedwidget_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QStackedWidget/qstackedwidget_wrap.cpp @@ -21,10 +21,11 @@ Napi::Object QStackedWidgetWrap::init(Napi::Env env, Napi::Object exports) { QFRAME_WRAPPED_METHODS_EXPORT_DEFINE(QStackedWidgetWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QStackedWidget, QStackedWidgetWrap); return exports; } -NStackedWidget* QStackedWidgetWrap::getInternalInstance() { +QStackedWidget* QStackedWidgetWrap::getInternalInstance() { return this->instance; } @@ -35,22 +36,30 @@ QStackedWidgetWrap::~QStackedWidgetWrap() { QStackedWidgetWrap::QStackedWidgetWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NStackedWidget( - parentWidgetWrap - ->getInternalInstance()); // this sets the parent to current widget - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NStackedWidget(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NStackedWidget(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QStackedWidgetWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QStackedWidgetWrap::addWidget(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QStandardItem/qstandarditem_wrap.cpp b/src/cpp/lib/QtWidgets/QStandardItem/qstandarditem_wrap.cpp index ec2af7a2f..761d71176 100644 --- a/src/cpp/lib/QtWidgets/QStandardItem/qstandarditem_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QStandardItem/qstandarditem_wrap.cpp @@ -37,20 +37,21 @@ QStandardItemWrap::QStandardItemWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { + size_t argCount = info.Length(); + if (argCount > 0 && info[0].IsExternal()) { this->instance = info[0].As>().Data(); - if (info.Length() == 2) { + if (argCount == 2) { this->disableDeletion = info[1].As().Value(); } } else { - if (info.Length() == 1) { + if (argCount == 1) { QString text = QString::fromUtf8(info[0].As().Utf8Value().c_str()); this->instance = new QStandardItem(text); - } else if (info.Length() == 0) { + } else if (argCount == 0) { this->instance = new QStandardItem(); } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, "QStandardItemWrap: Wrong number of arguments") .ThrowAsJavaScriptException(); } } diff --git a/src/cpp/lib/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.cpp b/src/cpp/lib/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.cpp index 5454aeea4..d0a5844c0 100644 --- a/src/cpp/lib/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QStandardItemModel/qstandarditemmodel_wrap.cpp @@ -22,27 +22,37 @@ Napi::Object QStandardItemModelWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QStandardItemModelWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QStandardItemModel, QStandardItemModelWrap); return exports; } -NStandardItemModel* QStandardItemModelWrap::getInternalInstance() { +QStandardItemModel* QStandardItemModelWrap::getInternalInstance() { return this->instance; } QStandardItemModelWrap::QStandardItemModelWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NStandardItemModel( - parentWidgetWrap - ->getInternalInstance()); // this sets the parent to current widget - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NStandardItemModel(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NStandardItemModel(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, + "NodeGui: QStandardItemModelWrap: Wrong number of " + "arguments to constructor") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); diff --git a/src/cpp/lib/QtWidgets/QStatusBar/qstatusbar_wrap.cpp b/src/cpp/lib/QtWidgets/QStatusBar/qstatusbar_wrap.cpp index 06965262f..935811104 100644 --- a/src/cpp/lib/QtWidgets/QStatusBar/qstatusbar_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QStatusBar/qstatusbar_wrap.cpp @@ -28,10 +28,11 @@ Napi::Object QStatusBarWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QStatusBarWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QStatusBar, QStatusBarWrap); return exports; } -NStatusBar *QStatusBarWrap::getInternalInstance() { return this->instance; } +QStatusBar *QStatusBarWrap::getInternalInstance() { return this->instance; } Napi::Value QStatusBarWrap::fromQStatusBar(Napi::Env env, QStatusBar *statusBar) { @@ -50,22 +51,29 @@ QStatusBarWrap::~QStatusBarWrap() { extrautils::safeDelete(this->instance); } QStatusBarWrap::QStatusBarWrap(const Napi::CallbackInfo &info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap *parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - - this->instance = new NStatusBar(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NStatusBar(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap *parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NStatusBar(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QStatusBarWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QStatusBarWrap::addPermanentWidget(const Napi::CallbackInfo &info) { diff --git a/src/cpp/lib/QtWidgets/QSvgWidget/qsvgwidget_wrap.cpp b/src/cpp/lib/QtWidgets/QSvgWidget/qsvgwidget_wrap.cpp index 52ca63adb..86539a9fa 100644 --- a/src/cpp/lib/QtWidgets/QSvgWidget/qsvgwidget_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QSvgWidget/qsvgwidget_wrap.cpp @@ -16,30 +16,40 @@ Napi::Object QSvgWidgetWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QSvgWidgetWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QSvgWidget, QSvgWidgetWrap); return exports; } -NSvgWidget* QSvgWidgetWrap::getInternalInstance() { return this->instance; } +QSvgWidget* QSvgWidgetWrap::getInternalInstance() { return this->instance; } QSvgWidgetWrap::~QSvgWidgetWrap() { extrautils::safeDelete(this->instance); } QSvgWidgetWrap::QSvgWidgetWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NSvgWidget(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NSvgWidget(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NSvgWidget(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QSvgWidgetWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QSvgWidgetWrap::load(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.cpp b/src/cpp/lib/QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.cpp index 59d28d255..1f3ab8ff2 100644 --- a/src/cpp/lib/QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.cpp @@ -25,27 +25,37 @@ Napi::Object QSystemTrayIconWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QSystemTrayIconWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QSystemTrayIcon, QSystemTrayIconWrap); return exports; } -NSystemTrayIcon* QSystemTrayIconWrap::getInternalInstance() { +QSystemTrayIcon* QSystemTrayIconWrap::getInternalInstance() { return this->instance; } QSystemTrayIconWrap::QSystemTrayIconWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NSystemTrayIcon( - parentWidgetWrap - ->getInternalInstance()); // this sets the parent to current widget - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NSystemTrayIcon(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NSystemTrayIcon(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New(env, + "NodeGui: QSystemTrayIconWrap: Wrong number of " + "arguments to constructor") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQObject(this->getInternalInstance()); diff --git a/src/cpp/lib/QtWidgets/QTabBar/qtabbar_wrap.cpp b/src/cpp/lib/QtWidgets/QTabBar/qtabbar_wrap.cpp index e1f28d7a3..3cc804f30 100644 --- a/src/cpp/lib/QtWidgets/QTabBar/qtabbar_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QTabBar/qtabbar_wrap.cpp @@ -43,44 +43,39 @@ Napi::Object QTabBarWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QTabBarWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QTabBar, QTabBarWrap); return exports; } -NTabBar* QTabBarWrap::getInternalInstance() { return this->instance; } +QTabBar* QTabBarWrap::getInternalInstance() { return this->instance; } -QTabBarWrap::~QTabBarWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } -} +QTabBarWrap::~QTabBarWrap() { extrautils::safeDelete(this->instance); } QTabBarWrap::QTabBarWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { - // --- if external --- - this->instance = info[0].As>().Data(); - if (info.Length() == 2) { - this->disableDeletion = info[1].As().Value(); - } - } else { - // --- regular cases --- - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NTabBar(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NTabBar(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { - this->instance = new NTabBar(); - } else { - Napi::TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); } + } else { + Napi::TypeError::New( + env, "NodeGui: QTabBarWrap: Wrong number of arguments to constructor") + .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QTabBarWrap::setAccessibleTabName(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QTabWidget/qtabwidget_wrap.cpp b/src/cpp/lib/QtWidgets/QTabWidget/qtabwidget_wrap.cpp index 88f97df25..def4ccd53 100644 --- a/src/cpp/lib/QtWidgets/QTabWidget/qtabwidget_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QTabWidget/qtabwidget_wrap.cpp @@ -23,35 +23,44 @@ Napi::Object QTabWidgetWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("currentIndex", &QTabWidgetWrap::currentIndex), InstanceMethod("removeTab", &QTabWidgetWrap::removeTab), InstanceMethod("setTabsClosable", &QTabWidgetWrap::setTabsClosable), + InstanceMethod("widget", &QTabWidgetWrap::widget), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QTabWidgetWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QTabWidget, QTabWidgetWrap); return exports; } -NTabWidget* QTabWidgetWrap::getInternalInstance() { return this->instance; } +QTabWidget* QTabWidgetWrap::getInternalInstance() { return this->instance; } QTabWidgetWrap::~QTabWidgetWrap() { extrautils::safeDelete(this->instance); } QTabWidgetWrap::QTabWidgetWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NTabWidget( - parentWidgetWrap - ->getInternalInstance()); // this sets the parent to current widget - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NTabWidget(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NTabWidget(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QTabWidgetWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QTabWidgetWrap::addTab(const Napi::CallbackInfo& info) { @@ -151,4 +160,15 @@ Napi::Value QTabWidgetWrap::setTabsClosable(const Napi::CallbackInfo& info) { Napi::Boolean closable = info[0].As(); this->instance->setTabsClosable(closable.Value()); return env.Null(); -} \ No newline at end of file +} + +Napi::Value QTabWidgetWrap::widget(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::Number index = info[0].As(); + QWidget* widget = this->instance->widget(index.Int32Value()); + if (widget) { + return WrapperCache::instance.getWrapper(env, widget); + } else { + return env.Null(); + } +} diff --git a/src/cpp/lib/QtWidgets/QTableView/qtableview_wrap.cpp b/src/cpp/lib/QtWidgets/QTableView/qtableview_wrap.cpp index 2fe5dfc6d..debf4c6ea 100644 --- a/src/cpp/lib/QtWidgets/QTableView/qtableview_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QTableView/qtableview_wrap.cpp @@ -12,42 +12,38 @@ Napi::Object QTableViewWrap::init(Napi::Env env, Napi::Object exports) { {QTABLEVIEW_WRAPPED_METHODS_EXPORT_DEFINE(QTableViewWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QTableView, QTableViewWrap); return exports; } -NTableView* QTableViewWrap::getInternalInstance() { return this->instance; } +QTableView* QTableViewWrap::getInternalInstance() { return this->instance; } -QTableViewWrap::~QTableViewWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } -} +QTableViewWrap::~QTableViewWrap() { extrautils::safeDelete(this->instance); } QTableViewWrap::QTableViewWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { - // --- if external --- - this->instance = info[0].As>().Data(); - if (info.Length() == 2) { - this->disableDeletion = info[1].As().Value(); - } - } else { - // --- regular cases --- - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NTableView(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NTableView(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { - this->instance = new NTableView(); - } else { - Napi::TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); } + } else { + Napi::TypeError::New( + env, + "NodeGui: QTableViewWrap: Wrong number of arguments to constructor") + .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } diff --git a/src/cpp/lib/QtWidgets/QTableWidget/qtablewidget_wrap.cpp b/src/cpp/lib/QtWidgets/QTableWidget/qtablewidget_wrap.cpp index 090089512..3320a5823 100644 --- a/src/cpp/lib/QtWidgets/QTableWidget/qtablewidget_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QTableWidget/qtablewidget_wrap.cpp @@ -86,10 +86,12 @@ Napi::Object QTableWidgetWrap::init(Napi::Env env, Napi::Object exports) { QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(QTableWidgetWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QTableWidget, QTableWidgetWrap); return exports; } -NTableWidget* QTableWidgetWrap::getInternalInstance() { return this->instance; } +QTableWidget* QTableWidgetWrap::getInternalInstance() { return this->instance; } + QTableWidgetWrap::~QTableWidgetWrap() { extrautils::safeDelete(this->instance); } @@ -97,34 +99,43 @@ QTableWidgetWrap::~QTableWidgetWrap() { QTableWidgetWrap::QTableWidgetWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 2 || info.Length() == 3) { - int rows = info[0].As().Int32Value(); - int columns = info[1].As().Int32Value(); - if (info.Length() == 3) { - Napi::Object parentObject = info[2].As(); + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NTableWidget(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NTableWidget( - rows, columns, parentWidgetWrap->getInternalInstance()); - } else { - this->instance = new NTableWidget(rows, columns); + this->instance = + new NTableWidget(parentWidgetWrap->getInternalInstance()); } - - } else if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); + } else if (argCount == 2) { + int rows = info[0].As().Int32Value(); + int columns = info[1].As().Int32Value(); + this->instance = new NTableWidget(rows, columns); + } else if (argCount == 3) { + int rows = info[0].As().Int32Value(); + int columns = info[1].As().Int32Value(); + Napi::Object parentObject = info[2].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NTableWidget(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { - this->instance = new NTableWidget(); + this->instance = new NTableWidget(rows, columns, + parentWidgetWrap->getInternalInstance()); } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QTableWidgetWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } Napi::Value QTableWidgetWrap::selectedRanges(const Napi::CallbackInfo& info) { diff --git a/src/cpp/lib/QtWidgets/QTableWidgetItem/qtablewidgetitem_wrap.cpp b/src/cpp/lib/QtWidgets/QTableWidgetItem/qtablewidgetitem_wrap.cpp index b8f75a4cd..626602d9b 100644 --- a/src/cpp/lib/QtWidgets/QTableWidgetItem/qtablewidgetitem_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QTableWidgetItem/qtablewidgetitem_wrap.cpp @@ -65,19 +65,20 @@ QTableWidgetItemWrap::~QTableWidgetItemWrap() { QTableWidgetItemWrap::QTableWidgetItemWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); + size_t argCount = info.Length(); this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { + if (argCount > 0 && info[0].IsExternal()) { // --- if external --- this->instance = info[0].As>().Data(); - if (info.Length() == 2) { + if (argCount == 2) { this->disableDeletion = info[1].As().Value(); } } else { - if (info.Length() == 1) { + if (argCount == 1) { QString text = QString::fromUtf8(info[0].As().Utf8Value().c_str()); this->instance = new QTableWidgetItem(text); - } else if (info.Length() == 0) { + } else if (argCount == 0) { this->instance = new QTableWidgetItem(); } else { Napi::TypeError::New(env, diff --git a/src/cpp/lib/QtWidgets/QTextBrowser/qtextbrowser_wrap.cpp b/src/cpp/lib/QtWidgets/QTextBrowser/qtextbrowser_wrap.cpp index cccc02e7e..cd0e4cd9d 100644 --- a/src/cpp/lib/QtWidgets/QTextBrowser/qtextbrowser_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QTextBrowser/qtextbrowser_wrap.cpp @@ -30,28 +30,39 @@ Napi::Object QTextBrowserWrap::init(Napi::Env env, Napi::Object exports) { QTEXTEDIT_WRAPPED_METHODS_EXPORT_DEFINE(QTextBrowserWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QTextBrowser, QTextBrowserWrap); return exports; } -NTextBrowser* QTextBrowserWrap::getInternalInstance() { return this->instance; } +QTextBrowser* QTextBrowserWrap::getInternalInstance() { return this->instance; } QTextBrowserWrap::QTextBrowserWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NTextBrowser(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NTextBrowser(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = + new NTextBrowser(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QTextBrowserWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QTextBrowserWrap::~QTextBrowserWrap() { diff --git a/src/cpp/lib/QtWidgets/QTextEdit/qtextedit_wrap.cpp b/src/cpp/lib/QtWidgets/QTextEdit/qtextedit_wrap.cpp index 9f7c00595..e0ae6c127 100644 --- a/src/cpp/lib/QtWidgets/QTextEdit/qtextedit_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QTextEdit/qtextedit_wrap.cpp @@ -14,28 +14,37 @@ Napi::Object QTextEditWrap::init(Napi::Env env, Napi::Object exports) { env, CLASSNAME, {QTEXTEDIT_WRAPPED_METHODS_EXPORT_DEFINE(QTextEditWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QTextEdit, QTextEditWrap); return exports; } -NTextEdit* QTextEditWrap::getInternalInstance() { return this->instance; } +QTextEdit* QTextEditWrap::getInternalInstance() { return this->instance; } QTextEditWrap::QTextEditWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NTextEdit(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NTextEdit(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NTextEdit(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QTextEditWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QTextEditWrap::~QTextEditWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QTimeEdit/qtimeedit_wrap.cpp b/src/cpp/lib/QtWidgets/QTimeEdit/qtimeedit_wrap.cpp index b65ca8046..aad63c731 100644 --- a/src/cpp/lib/QtWidgets/QTimeEdit/qtimeedit_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QTimeEdit/qtimeedit_wrap.cpp @@ -12,28 +12,37 @@ Napi::Object QTimeEditWrap::init(Napi::Env env, Napi::Object exports) { {QDATETIMEEDIT_WRAPPED_METHODS_EXPORT_DEFINE(QTimeEditWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QTimeEdit, QTimeEditWrap); return exports; } -NTimeEdit* QTimeEditWrap::getInternalInstance() { return this->instance; } +QTimeEdit* QTimeEditWrap::getInternalInstance() { return this->instance; } QTimeEditWrap::QTimeEditWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NTimeEdit(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NTimeEdit(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NTimeEdit(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QTimeEditWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QTimeEditWrap::~QTimeEditWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QToolButton/qtoolbutton_wrap.cpp b/src/cpp/lib/QtWidgets/QToolButton/qtoolbutton_wrap.cpp index e763fbf72..2bd7bf837 100644 --- a/src/cpp/lib/QtWidgets/QToolButton/qtoolbutton_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QToolButton/qtoolbutton_wrap.cpp @@ -15,46 +15,44 @@ Napi::Object QToolButtonWrap::init(Napi::Env env, Napi::Object exports) { {InstanceMethod("setMenu", &QToolButtonWrap::setMenu), InstanceMethod("setDefaultAction", &QToolButtonWrap::setDefaultAction), InstanceMethod("showMenu", &QToolButtonWrap::showMenu), + InstanceMethod("defaultAction", &QToolButtonWrap::defaultAction), QABSTRACTBUTTON_WRAPPED_METHODS_EXPORT_DEFINE(QToolButtonWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QToolButton, QToolButtonWrap); return exports; } -NToolButton* QToolButtonWrap::getInternalInstance() { return this->instance; } +QToolButton* QToolButtonWrap::getInternalInstance() { return this->instance; } -QToolButtonWrap::~QToolButtonWrap() { - if (!this->disableDeletion) { - extrautils::safeDelete(this->instance); - } -} +QToolButtonWrap::~QToolButtonWrap() { extrautils::safeDelete(this->instance); } QToolButtonWrap::QToolButtonWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - this->disableDeletion = false; - if (info.Length() > 0 && info[0].IsExternal()) { - // --- if external --- - this->instance = info[0].As>().Data(); - if (info.Length() == 2) { - this->disableDeletion = info[1].As().Value(); - } - } else { - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NToolButton(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); NodeWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NToolButton(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { - this->instance = new NToolButton(); - } else { - Napi::TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); } + } else { + Napi::TypeError::New( + env, + "NodeGui: QToolButtonWrap: Wrong number of arguments to constructor") + .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } Napi::Value QToolButtonWrap::setMenu(const Napi::CallbackInfo& info) { @@ -78,3 +76,13 @@ Napi::Value QToolButtonWrap::showMenu(const Napi::CallbackInfo& info) { this->instance->showMenu(); return env.Null(); } + +Napi::Value QToolButtonWrap::defaultAction(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + QAction* action = this->instance->defaultAction(); + if (action) { + return WrapperCache::instance.getWrapper(env, action); + } else { + return env.Null(); + } +} diff --git a/src/cpp/lib/QtWidgets/QTreeWidget/qtreewidget_wrap.cpp b/src/cpp/lib/QtWidgets/QTreeWidget/qtreewidget_wrap.cpp index 618ee9740..19884ed7c 100644 --- a/src/cpp/lib/QtWidgets/QTreeWidget/qtreewidget_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QTreeWidget/qtreewidget_wrap.cpp @@ -32,28 +32,38 @@ Napi::Object QTreeWidgetWrap::init(Napi::Env env, Napi::Object exports) { QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QTreeWidgetWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QTreeWidget, QTreeWidgetWrap); return exports; } -NTreeWidget* QTreeWidgetWrap::getInternalInstance() { return this->instance; } +QTreeWidget* QTreeWidgetWrap::getInternalInstance() { return this->instance; } QTreeWidgetWrap::QTreeWidgetWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { - Napi::Object parentObject = info[0].As(); - NodeWidgetWrap* parentWidgetWrap = - Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NTreeWidget(parentWidgetWrap->getInternalInstance()); - } else if (info.Length() == 0) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance this->instance = new NTreeWidget(); + } else if (argCount == 1) { + if (info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + // --- Construct a new instance and pass a parent + Napi::Object parentObject = info[0].As(); + NodeWidgetWrap* parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + this->instance = new NTreeWidget(parentWidgetWrap->getInternalInstance()); + } } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, + "NodeGui: QTreeWidgetWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - true); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), true); } QTreeWidgetWrap::~QTreeWidgetWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/lib/QtWidgets/QWidget/qwidget_wrap.cpp b/src/cpp/lib/QtWidgets/QWidget/qwidget_wrap.cpp index 4079a0b12..272a5db35 100644 --- a/src/cpp/lib/QtWidgets/QWidget/qwidget_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QWidget/qwidget_wrap.cpp @@ -12,33 +12,37 @@ Napi::Object QWidgetWrap::init(Napi::Env env, Napi::Object exports) { env, CLASSNAME, {QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QWidgetWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QWidget, QWidgetWrap); return exports; } -NWidget *QWidgetWrap::getInternalInstance() { return this->instance; } +QWidget *QWidgetWrap::getInternalInstance() { return this->instance; } QWidgetWrap::~QWidgetWrap() { extrautils::safeDelete(this->instance); } QWidgetWrap::QWidgetWrap(const Napi::CallbackInfo &info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); - if (info.Length() == 1) { + size_t argCount = info.Length(); + if (argCount == 0) { + // --- Construct a new instance + this->instance = new NWidget(); + } else if (argCount == 1) { if (info[0].IsExternal()) { - this->instance = - new NWidget(info[0].As>().Data()); + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); } else { + // --- Construct a new instance and pass a parent Napi::Object parentObject = info[0].As(); NodeWidgetWrap *parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = new NWidget(parentWidgetWrap->getInternalInstance()); } - } else if (info.Length() == 0) { - this->instance = new NWidget(); } else { - Napi::TypeError::New(env, "Wrong number of arguments") + Napi::TypeError::New( + env, "NodeGui: QWidgetWrap: Wrong number of arguments to constructor") .ThrowAsJavaScriptException(); } - this->rawData = extrautils::configureQWidget( - this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), - false); + this->rawData = + extrautils::configureQWidget(this->getInternalInstance(), false); } diff --git a/src/cpp/lib/test/cachetestqobject_wrap.cpp b/src/cpp/lib/test/cachetestqobject_wrap.cpp index 1eaf062f6..ccb040fe9 100644 --- a/src/cpp/lib/test/cachetestqobject_wrap.cpp +++ b/src/cpp/lib/test/cachetestqobject_wrap.cpp @@ -16,6 +16,7 @@ Napi::Object CacheTestQObjectWrap::init(Napi::Env env, Napi::Object exports) { QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(CacheTestQObjectWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(CacheTestQObject, CacheTestQObjectWrap); return exports; } @@ -47,8 +48,7 @@ void CacheTestQObjectWrap::connectSignalsToEventEmitter() { Napi::Value CacheTestQObjectWrap::foo(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); CacheTestQObject* foo = this->instance->foo(); - return WrapperCache::instance.get( - env, foo); + return WrapperCache::instance.getWrapper(env, foo); } Napi::Value CacheTestQObjectWrap::clearFoo(const Napi::CallbackInfo& info) { @@ -60,6 +60,5 @@ Napi::Value CacheTestQObjectWrap::clearFoo(const Napi::CallbackInfo& info) { Napi::Value CacheTestQObjectWrap::bar(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); CacheTestQObject* bar = this->instance->bar(); - return WrapperCache::instance.get( - env, bar); + return WrapperCache::instance.getWrapper(env, bar); } diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 90b135276..39609727a 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -122,16 +122,14 @@ #include "test/cachetestqobject_wrap.h" // These cant be instantiated in JS Side -void InitPrivateHelpers(Napi::Env env) { - qodeIntegration::integrate(); - QLayoutWrap::init(env); // Abstact class wrapper for pointing to any layout -} +void InitPrivateHelpers(Napi::Env env) { qodeIntegration::integrate(); } Napi::Object Main(Napi::Env env, Napi::Object exports) { InitPrivateHelpers(env); NUtilsWrap::init(env, exports); WrapperCache::init(env, exports); QApplicationWrap::init(env, exports); + QLayoutWrap::init(env, exports); QDateWrap::init(env, exports); QDateTimeWrap::init(env, exports); QMimeDataWrap::init(env, exports); diff --git a/src/index.ts b/src/index.ts index d059c8c60..15ba532f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,8 +39,8 @@ export { QScreen } from './lib/QtGui/QScreen'; export { QWindow } from './lib/QtGui/QWindow'; export { WidgetEventTypes } from './lib/core/EventWidget'; // Abstract: -export { NodeWidget, QWidget, QWidgetSignals } from './lib/QtWidgets/QWidget'; -export { NodeLayout, QLayoutSignals, SizeConstraint } from './lib/QtWidgets/QLayout'; +export { QWidget, QWidgetSignals } from './lib/QtWidgets/QWidget'; +export { QLayout, QLayoutSignals, SizeConstraint } from './lib/QtWidgets/QLayout'; export { QAbstractScrollArea } from './lib/QtWidgets/QAbstractScrollArea'; export { QAbstractSlider, QAbstractSliderSignals, SliderAction } from './lib/QtWidgets/QAbstractSlider'; export { QAbstractButton, QAbstractButtonSignals } from './lib/QtWidgets/QAbstractButton'; @@ -65,7 +65,7 @@ export { QCalendarWidget, QCalendarWidgetSignals } from './lib/QtWidgets/QCalend export { QCheckBox, QCheckBoxSignals } from './lib/QtWidgets/QCheckBox'; export { QColorDialog, QColorDialogSignals } from './lib/QtWidgets/QColorDialog'; export { QDateEdit } from './lib/QtWidgets/QDateEdit'; -export { QDateTimeEdit, NodeDateTimeEdit, QDateTimeEditSignals } from './lib/QtWidgets/QDateTimeEdit'; +export { QDateTimeEdit, QDateTimeEditSignals } from './lib/QtWidgets/QDateTimeEdit'; export { QLabel, QLabelSignals } from './lib/QtWidgets/QLabel'; export { QLCDNumber, QLCDNumberSignals, Mode, SegmentStyle } from './lib/QtWidgets/QLCDNumber'; export { QDial, QDialSignals } from './lib/QtWidgets/QDial'; @@ -153,7 +153,7 @@ export { QDateTime } from './lib/QtCore/QDateTime'; export { QItemSelectionModel, SelectionFlag } from './lib/QtCore/QItemSelectionModel'; export { QModelIndex } from './lib/QtCore/QModelIndex'; export { QMimeData } from './lib/QtCore/QMimeData'; -export { QObject, QObjectSignals, NodeObject } from './lib/QtCore/QObject'; +export { QObject, QObjectSignals } from './lib/QtCore/QObject'; export { QVariant } from './lib/QtCore/QVariant'; export { QSize } from './lib/QtCore/QSize'; export { QSizeF } from './lib/QtCore/QSizeF'; @@ -163,7 +163,13 @@ export { QPoint } from './lib/QtCore/QPoint'; export { QPointF } from './lib/QtCore/QPointF'; export { QColor } from './lib/QtGui/QColor'; export { QTime } from './lib/QtCore/QTime'; -export { QUrl, ParsingMode } from './lib/QtCore/QUrl'; +export { + QUrl, + ParsingMode, + UserInputResolutionOption, + UrlFormattingOption, + ComponentFormattingOption, +} from './lib/QtCore/QUrl'; export { QSettings, QSettingsFormat, QSettingsScope } from './lib/QtCore/QSettings'; // Layouts: export { QBoxLayout, QBoxLayoutSignals } from './lib/QtWidgets/QBoxLayout'; @@ -183,3 +189,5 @@ export { Margins } from './lib/utils/Margins'; // Test: export { CacheTestQObject } from './lib/core/__test__/CacheTestQObject'; + +export { wrapperCache, setLogCreateQObject, setLogDestroyQObject } from './lib/core/WrapperCache'; diff --git a/src/lib/QtCore/QAbstractItemModel.ts b/src/lib/QtCore/QAbstractItemModel.ts index 7a5834b78..d4ce916b7 100644 --- a/src/lib/QtCore/QAbstractItemModel.ts +++ b/src/lib/QtCore/QAbstractItemModel.ts @@ -1,18 +1,14 @@ import addon from '../utils/addon'; -import { NativeElement } from '../core/Component'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { QModelIndex } from './QModelIndex'; import { QVariant } from './QVariant'; import { ItemDataRole, ItemFlag, Orientation } from '../QtEnums'; export type QAbstractItemSignals = QObjectSignals; -export class QAbstractItemModel extends NodeObject { - native: NativeElement; +export class QAbstractItemModel extends QObject { constructor() { - const native = new addon.QAbstractItemModel(); - super(native); - this.native = native; + super(new addon.QAbstractItemModel()); const dispatcher = (methodName: string, ...args: any[]): any => { switch (methodName) { case 'index': @@ -26,9 +22,9 @@ export class QAbstractItemModel extends NodeObject { case 'parent': try { - return this.parent(new QModelIndex(args[0])).native; + return this.parentModelIndex(new QModelIndex(args[0])).native; } catch (e) { - console.log(`An exception was thrown while dispatching to method 'parent':`); + console.log(`An exception was thrown while dispatching to method 'parentModelIndex':`); console.log(e); } return new QModelIndex().native; @@ -158,7 +154,10 @@ export class QAbstractItemModel extends NodeObject { // TODO: bool moveRow(const QModelIndex &sourceParent, int sourceRow, const QModelIndex &destinationParent, int destinationChild) // TODO: virtual bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) - parent(child: QModelIndex): QModelIndex { + /** + * Note: This corresponds to `QAbstractItemModel::parent(QModelIndex)`. It has been given a different name in TS. + */ + parentModelIndex(child: QModelIndex): QModelIndex { return new QModelIndex(); } diff --git a/src/lib/QtCore/QAbstractTableModel.ts b/src/lib/QtCore/QAbstractTableModel.ts index b44ab5412..a3238e1fc 100644 --- a/src/lib/QtCore/QAbstractTableModel.ts +++ b/src/lib/QtCore/QAbstractTableModel.ts @@ -7,7 +7,7 @@ export class QAbstractTableModel extends QAbstractItemModel { return this.hasIndex(row, column, parent) ? this.createIndex(row, column) : new QModelIndex(); } - parent(child: QModelIndex): QModelIndex { + parentModelIndex(child: QModelIndex): QModelIndex { return new QModelIndex(); } diff --git a/src/lib/QtCore/QDate.ts b/src/lib/QtCore/QDate.ts index 71aa8f391..edc0f69b6 100644 --- a/src/lib/QtCore/QDate.ts +++ b/src/lib/QtCore/QDate.ts @@ -5,17 +5,17 @@ import { QVariant } from './QVariant'; import { DateFormat } from '../QtEnums'; export class QDate extends Component { - native: NativeElement; constructor(arg?: NativeElement | number, month?: number, day?: number) { - super(); const count = arguments.length; + let native: NativeElement; if (count == 3) { - this.native = new addon.QDate(arg, month, day); - } else if (count == 1 && checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = new addon.QDate(arg, month, day); + } else if (checkIfNativeElement(arg)) { + native = arg as NativeElement; } else { - this.native = new addon.QDate(); + native = new addon.QDate(); } + super(native); } addDays(ndays: number): QDate { return new QDate(this.native.addDays(ndays)); diff --git a/src/lib/QtCore/QDateTime.ts b/src/lib/QtCore/QDateTime.ts index a4eecb57d..1653f6b6f 100644 --- a/src/lib/QtCore/QDateTime.ts +++ b/src/lib/QtCore/QDateTime.ts @@ -7,17 +7,17 @@ import { QTime } from './QTime'; import { TimeSpec, DateFormat } from '../QtEnums'; export class QDateTime extends Component { - native: NativeElement; constructor(arg?: NativeElement, time?: NativeElement) { - super(); const count = arguments.length; + let native: NativeElement; if (arg && time) { - this.native = new addon.QDateTime(arg.native, time.native); + native = new addon.QDateTime(arg.native, time.native); } else if (count == 1 && checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else { - this.native = new addon.QDateTime(); + native = new addon.QDateTime(); } + super(native); } addDays(ndays: number): QDateTime { return new QDateTime(this.native.addDays(ndays)); diff --git a/src/lib/QtCore/QItemSelectionModel.ts b/src/lib/QtCore/QItemSelectionModel.ts index d468a156c..fee8fb14e 100644 --- a/src/lib/QtCore/QItemSelectionModel.ts +++ b/src/lib/QtCore/QItemSelectionModel.ts @@ -1,13 +1,13 @@ import addon from '../utils/addon'; import { NativeElement } from '../core/Component'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { QModelIndex } from './QModelIndex'; import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; export type QItemSelectionModelSignals = QObjectSignals; -export class QItemSelectionModel extends NodeObject { - native: NativeElement; +export class QItemSelectionModel extends QObject { constructor(arg?: NativeElement) { let native = null; if (arg == null) { @@ -18,7 +18,6 @@ export class QItemSelectionModel extends NodeObject throw new Error('QItemSelectionModel cannot be initialised this way.'); } super(native); - this.native = native; } // *** Public Functions *** @@ -104,6 +103,7 @@ export class QItemSelectionModel extends NodeObject // TODO: void modelChanged(QAbstractItemModel *model) // TODO: void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) } +wrapperCache.registerWrapper('QItemSelectionModelWrap', QItemSelectionModel); export enum SelectionFlag { NoUpdate = 0x0000, diff --git a/src/lib/QtCore/QMimeData.ts b/src/lib/QtCore/QMimeData.ts index f824839f5..a97457ac4 100644 --- a/src/lib/QtCore/QMimeData.ts +++ b/src/lib/QtCore/QMimeData.ts @@ -6,14 +6,14 @@ import { QUrl } from './QUrl'; * description */ export class QMimeData extends Component { - native: NativeElement; constructor(arg?: NativeElement) { - super(); + let native: NativeElement; if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else { - this.native = new addon.QMimeData(); + native = new addon.QMimeData(); } + super(native); } /** diff --git a/src/lib/QtCore/QModelIndex.ts b/src/lib/QtCore/QModelIndex.ts index f210baf13..92f7e90be 100644 --- a/src/lib/QtCore/QModelIndex.ts +++ b/src/lib/QtCore/QModelIndex.ts @@ -6,18 +6,16 @@ import { ItemDataRole } from '../QtEnums/ItemDataRole'; import { ItemFlag } from '../QtEnums/ItemFlag'; export class QModelIndex extends Component { - native: NativeElement; - constructor(); - constructor(nativeElement: NativeElement); constructor(arg?: NativeElement) { - super(); + let native: NativeElement; if (!arg) { - this.native = new addon.QModelIndex(); + native = new addon.QModelIndex(); } else if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else { throw new Error('QModelIndex cannot be initialised this way.'); } + super(native); } column(): number { return this.native.column(); diff --git a/src/lib/QtCore/QObject.ts b/src/lib/QtCore/QObject.ts index e87c9a4b2..766007298 100644 --- a/src/lib/QtCore/QObject.ts +++ b/src/lib/QtCore/QObject.ts @@ -4,8 +4,38 @@ import { checkIfNativeElement } from '../utils/helpers'; import addon from '../utils/addon'; import { QVariant, QVariantType } from './QVariant'; import { TimerType } from '../QtEnums/TimerType'; +import { wrapperCache } from '../core/WrapperCache'; -export abstract class NodeObject extends EventWidget { +export class QObject extends EventWidget { + private __id: number; + + constructor(nativeElementOrParent?: NativeElement | QObject) { + let native: NativeElement; + if (checkIfNativeElement(nativeElementOrParent)) { + native = nativeElementOrParent as NativeElement; + } else if (nativeElementOrParent) { + const parent = nativeElementOrParent as QObject; + native = new addon.QObject(parent.native); + } else { + native = new addon.QObject(); + } + super(native); + this.__id = native.__id__(); + wrapperCache.store(this); + } + + /** + * Get an ID identifying the underlying C++ object. + * + * This can be useful when debugging memory problems with help from + * `setLogCreateQObject()` and `setLogDestroyQObject()`. The number is + * hash of the memory address of the C++ object. + * + * @return a unique number which is valid for the lifetime of the C++ object. + */ + _id(): number { + return this.__id; + } inherits(className: string): boolean { return this.native.inherits(className); } @@ -28,7 +58,7 @@ export abstract class NodeObject extends EventWi dumpObjectInfo(): void { this.native.dumpObjectInfo(); } - setParent(parent: NodeObject): void { + setParent(parent: QObject): void { if (parent != null) { const extern = parent.native.__external_qobject__(); this.native.setParent(extern); @@ -36,36 +66,27 @@ export abstract class NodeObject extends EventWi this.native.setParent(null); } } + parent(): QObject { + return wrapperCache.getWrapper(this.native.parent()); + } startTimer(intervalMS: number, timerType = TimerType.CoarseTimer): number { return this.native.startTimer(intervalMS, timerType); } killTimer(timerId: number): void { this.native.killTimer(timerId); } + delete(): void { + this.native.delete(); + } + deleteLater(): void { + this.native.deleteLater(); + } + children(): QObject[] { + return this.native.children().map((kid: any) => wrapperCache.getWrapper(kid)); + } } +wrapperCache.registerWrapper('QObjectWrap', QObject); export interface QObjectSignals { objectNameChanged: (objectName: string) => void; } - -export class QObject extends NodeObject { - native: NativeElement; - constructor(); - constructor(nativeElement: NativeElement); - constructor(parent: NodeObject); - constructor(arg?: NodeObject | NativeElement) { - let native; - let parent; - if (checkIfNativeElement(arg)) { - native = arg as NativeElement; - } else if (arg) { - parent = arg as NodeObject; - native = new addon.QObject(parent.native); - } else { - native = new addon.QObject(); - } - super(native); - this.setNodeParent(parent); - this.native = native; - } -} diff --git a/src/lib/QtCore/QPoint.ts b/src/lib/QtCore/QPoint.ts index cc4d4d4d0..f8cadfbcb 100644 --- a/src/lib/QtCore/QPoint.ts +++ b/src/lib/QtCore/QPoint.ts @@ -7,19 +7,19 @@ import { QVariant } from './QVariant'; * The QPoint class defines a point in the plane using integer precision. */ export class QPoint extends Component { - native: NativeElement; constructor(); constructor(nativeElement: NativeElement); constructor(x?: number, y?: number); - constructor(arg?: NativeElement | number, y = 0) { - super(); - if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; - } else if (typeof arg === 'number') { - this.native = new addon.QPoint(arg, y); + constructor(nativeOrX?: NativeElement | number, y = 0) { + let native: NativeElement; + if (checkIfNativeElement(nativeOrX)) { + native = nativeOrX as NativeElement; + } else if (typeof nativeOrX === 'number') { + native = new addon.QPoint(nativeOrX, y); } else { - this.native = new addon.QPoint(); + native = new addon.QPoint(); } + super(native); } setX(value: number): void { this.native.setX(value); diff --git a/src/lib/QtCore/QPointF.ts b/src/lib/QtCore/QPointF.ts index 36680e4f7..436b31bbb 100644 --- a/src/lib/QtCore/QPointF.ts +++ b/src/lib/QtCore/QPointF.ts @@ -11,22 +11,18 @@ import { QPoint } from './QPoint'; * In addition, the QPointF class provides a constructor converting a QPoint object into a QPointF object, and a corresponding toPoint() function which returns a QPoint copy of this point. */ export class QPointF extends Component { - native: NativeElement; - constructor(); - constructor(nativeElement: NativeElement); - constructor(x?: number, y?: number); - constructor(point: QPoint); - constructor(arg?: NativeElement | number | QPoint, y = 0) { - super(); - if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; - } else if (typeof arg === 'number') { - this.native = new addon.QPointF(arg, y); - } else if (arg instanceof QPoint) { - this.native = new addon.QPointF(arg.x(), arg.y()); + constructor(nativeOrXOrQPoint?: NativeElement | number | QPoint, y = 0) { + let native: NativeElement; + if (checkIfNativeElement(nativeOrXOrQPoint)) { + native = nativeOrXOrQPoint as NativeElement; + } else if (typeof nativeOrXOrQPoint === 'number') { + native = new addon.QPointF(nativeOrXOrQPoint, y); + } else if (nativeOrXOrQPoint instanceof QPoint) { + native = new addon.QPointF(nativeOrXOrQPoint.x(), nativeOrXOrQPoint.y()); } else { - this.native = new addon.QPointF(); + native = new addon.QPointF(); } + super(native); } /** * Sets the x coordinate of this point to the given x coordinate. diff --git a/src/lib/QtCore/QRect.ts b/src/lib/QtCore/QRect.ts index 49de66ca1..7714ba0dd 100644 --- a/src/lib/QtCore/QRect.ts +++ b/src/lib/QtCore/QRect.ts @@ -4,20 +4,17 @@ import { checkIfNativeElement } from '../utils/helpers'; import { QVariant } from './QVariant'; export class QRect extends Component { - native: NativeElement; - constructor(); - constructor(nativeElement: NativeElement); - constructor(x?: number, y?: number, width?: number, height?: number); - constructor(arg?: NativeElement | number, y = 0, width = 0, height = 0) { - super(); + constructor(nativeOrX?: NativeElement | number, y = 0, width = 0, height = 0) { const count = arguments.length; + let native: NativeElement; if (count > 1) { - this.native = new addon.QRect(arg, y, width, height); - } else if (count == 1 && checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = new addon.QRect(nativeOrX, y, width, height); + } else if (checkIfNativeElement(nativeOrX)) { + native = nativeOrX as NativeElement; } else { - this.native = new addon.QRect(); + native = new addon.QRect(); } + super(native); } setWidth(width: number): void { return this.native.setWidth(width); diff --git a/src/lib/QtCore/QRectF.ts b/src/lib/QtCore/QRectF.ts index b5d3fec51..8e58135f3 100644 --- a/src/lib/QtCore/QRectF.ts +++ b/src/lib/QtCore/QRectF.ts @@ -8,20 +8,17 @@ import { QRect } from './QRect'; * description */ export class QRectF extends Component { - native: NativeElement; - constructor(); - constructor(nativeElement: NativeElement); - constructor(x?: number, y?: number, width?: number, height?: number); - constructor(arg?: NativeElement | number, y = 0, width = 0, height = 0) { - super(); + constructor(nativeOrX?: NativeElement | number, y = 0, width = 0, height = 0) { const count = arguments.length; + let native: NativeElement; if (count > 1) { - this.native = new addon.QRectF(arg, y, width, height); - } else if (count == 1 && checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = new addon.QRectF(nativeOrX, y, width, height); + } else if (count == 1 && checkIfNativeElement(nativeOrX)) { + native = nativeOrX as NativeElement; } else { - this.native = new addon.QRectF(); + native = new addon.QRectF(); } + super(native); } /** diff --git a/src/lib/QtCore/QSettings.ts b/src/lib/QtCore/QSettings.ts index 4e4056f8a..e117d7aa4 100644 --- a/src/lib/QtCore/QSettings.ts +++ b/src/lib/QtCore/QSettings.ts @@ -1,4 +1,4 @@ -import { NativeElement, Component } from '../core/Component'; +import { Component } from '../core/Component'; import addon from '../utils/addon'; import { QVariant } from './QVariant'; @@ -16,10 +16,8 @@ export enum QSettingsScope { } export class QSettings extends Component { - native: NativeElement; constructor(organization: string, application: string) { - super(); - this.native = new addon.QSettings(organization, application); + super(new addon.QSettings(organization, application)); } sync(): void { this.native.sync(); diff --git a/src/lib/QtCore/QSize.ts b/src/lib/QtCore/QSize.ts index bcbf36b6a..bfac0905c 100644 --- a/src/lib/QtCore/QSize.ts +++ b/src/lib/QtCore/QSize.ts @@ -4,19 +4,18 @@ import { checkIfNativeElement } from '../utils/helpers'; import { QVariant } from './QVariant'; export class QSize extends Component { - native: NativeElement; - constructor(); constructor(nativeElement: NativeElement); constructor(width?: number, height?: number); - constructor(arg?: number | NativeElement, height?: number) { - super(); - if (!arg) { - this.native = new addon.QSize(); - } else if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + constructor(nativeOrWidth?: number | NativeElement, height?: number) { + let native: NativeElement; + if (!nativeOrWidth) { + native = new addon.QSize(); + } else if (checkIfNativeElement(nativeOrWidth)) { + native = nativeOrWidth as NativeElement; } else { - this.native = new addon.QSize(arg, height); + native = new addon.QSize(nativeOrWidth, height); } + super(native); } setWidth(width: number): void { return this.native.setWidth(width); diff --git a/src/lib/QtCore/QSizeF.ts b/src/lib/QtCore/QSizeF.ts index bb5acd124..aee07943a 100644 --- a/src/lib/QtCore/QSizeF.ts +++ b/src/lib/QtCore/QSizeF.ts @@ -4,19 +4,16 @@ import { checkIfNativeElement } from '../utils/helpers'; import { QVariant } from './QVariant'; export class QSizeF extends Component { - native: NativeElement; - constructor(); - constructor(nativeElement: NativeElement); - constructor(width?: number, height?: number); - constructor(arg?: number | NativeElement, height?: number) { - super(); - if (!arg) { - this.native = new addon.QSizeF(); - } else if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + constructor(nativeOrWidth?: number | NativeElement, height?: number) { + let native: NativeElement; + if (!nativeOrWidth) { + native = new addon.QSizeF(); + } else if (checkIfNativeElement(nativeOrWidth)) { + native = nativeOrWidth as NativeElement; } else { - this.native = new addon.QSizeF(arg, height); + native = new addon.QSizeF(nativeOrWidth, height); } + super(native); } setWidth(width: number): void { return this.native.setWidth(width); diff --git a/src/lib/QtCore/QTime.ts b/src/lib/QtCore/QTime.ts index b67616b9e..2a190f651 100644 --- a/src/lib/QtCore/QTime.ts +++ b/src/lib/QtCore/QTime.ts @@ -5,17 +5,17 @@ import { QVariant } from './QVariant'; import { DateFormat } from '../QtEnums'; export class QTime extends Component { - native: NativeElement; - constructor(arg?: NativeElement | number, m?: number, s = 0, ms = 0) { - super(); + constructor(nativeOrHours?: NativeElement | number, m?: number, s = 0, ms = 0) { const count = arguments.length; + let native: NativeElement; if (count > 1) { - this.native = new addon.QTime(arg, m, s, ms); - } else if (count == 1 && checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = new addon.QTime(nativeOrHours, m, s, ms); + } else if (checkIfNativeElement(nativeOrHours)) { + native = nativeOrHours as NativeElement; } else { - this.native = new addon.QTime(); + native = new addon.QTime(); } + super(native); } addMSecs(ms: number): QTime { return new QTime(this.native.addMSecs(ms)); diff --git a/src/lib/QtCore/QUrl.ts b/src/lib/QtCore/QUrl.ts index 7cfbd2f3a..8e5d922da 100644 --- a/src/lib/QtCore/QUrl.ts +++ b/src/lib/QtCore/QUrl.ts @@ -3,7 +3,7 @@ import addon from '../utils/addon'; import { checkIfNativeElement } from '../utils/helpers'; import { QVariant } from './QVariant'; -enum ComponentFormattingOption { +export enum ComponentFormattingOption { /** The component is returned in a "pretty form", with most percent-encoded characters decoded. The exact behavior of PrettyDecoded varies from component to component and may also change from Qt release to Qt release. This is the default. */ PrettyDecoded = 0x000000, /** Leave space characters in their encoded form ("%20"). */ @@ -29,7 +29,7 @@ export enum ParsingMode { /** QUrl will interpret the URL component in the fully-decoded form, where percent characters stand for themselves, not as the beginning of a percent-encoded sequence. This mode is only valid for the setters setting components of a URL; it is not permitted in the QUrl constructor, in fromEncoded() or in setUrl(). For more information on this mode, see the documentation for QUrl::FullyDecoded.*/ DecodedMode = 2, } -enum UrlFormattingOption { +export enum UrlFormattingOption { None = 0x0, RemoveScheme = 0x1, RemovePassword = 0x2, @@ -44,7 +44,7 @@ enum UrlFormattingOption { StripTrailingSlash = 0x400, NormalizePathSegments = 0x1000, } -enum UserInputResolutionOption { +export enum UserInputResolutionOption { /** The default resolution mechanism is to check whether a local file exists, in the working directory given to fromUserInput, and only return a local path in that case. Otherwise a URL is assumed. */ DefaultResolution = 0, /** This option makes fromUserInput() always return a local path unless the input contains a scheme, such as http://file.pl. This is useful for applications such as text editors, which are able to create the file if it doesn't exist. */ @@ -52,27 +52,19 @@ enum UserInputResolutionOption { } export class QUrl extends Component { - static readonly ComponentFormattingOption = ComponentFormattingOption; - static readonly ParsingMode = ParsingMode; - static readonly UrlFormattingOption = UrlFormattingOption; - static readonly UserInputResolutionOption = UserInputResolutionOption; - readonly ComponentFormattingOption = ComponentFormattingOption; - readonly ParsingMode = ParsingMode; - readonly UrlFormattingOption = UrlFormattingOption; - readonly UserInputResolutionOption = UserInputResolutionOption; - native: NativeElement; constructor(); constructor(nativeElement: NativeElement); constructor(url: string, parsingMode?: ParsingMode); - constructor(arg?: string | NativeElement, parsingMode: ParsingMode = ParsingMode.TolerantMode) { - super(); - if (!arg) { - this.native = new addon.QUrl(); - } else if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + constructor(nativeOrString?: string | NativeElement, parsingMode: ParsingMode = ParsingMode.TolerantMode) { + let native: NativeElement; + if (!nativeOrString) { + native = new addon.QUrl(); + } else if (checkIfNativeElement(nativeOrString)) { + native = nativeOrString as NativeElement; } else { - this.native = new addon.QUrl(arg, parsingMode); + native = new addon.QUrl(nativeOrString, parsingMode); } + super(native); } static fromQVariant(variant: QVariant): QUrl { diff --git a/src/lib/QtCore/QVariant.ts b/src/lib/QtCore/QVariant.ts index d1f7d6345..1708bffa8 100644 --- a/src/lib/QtCore/QVariant.ts +++ b/src/lib/QtCore/QVariant.ts @@ -5,19 +5,19 @@ import { checkIfNativeElement } from '../utils/helpers'; export type QVariantType = NativeElement | string | string[] | number | boolean; export class QVariant extends Component { - native: NativeElement; constructor(); constructor(nativeElement: NativeElement); constructor(variant: QVariantType); constructor(arg?: QVariantType | NativeElement) { - super(); + let native: NativeElement; if (checkIfNativeElement(arg) && arg instanceof addon.QVariant) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else if (arg) { - this.native = new addon.QVariant.convertToQVariant(arg); + native = new addon.QVariant.convertToQVariant(arg); } else { - this.native = new addon.QVariant(); + native = new addon.QVariant(); } + super(native); } toString(): string { return this.native.toString(); diff --git a/src/lib/QtGui/QApplication.ts b/src/lib/QtGui/QApplication.ts index c4200db03..63a9bfeb7 100644 --- a/src/lib/QtGui/QApplication.ts +++ b/src/lib/QtGui/QApplication.ts @@ -3,7 +3,7 @@ import { NativeElement } from '../core/Component'; import { checkIfNativeElement } from '../utils/helpers'; import { QClipboard } from './QClipboard'; import { QStyle } from './QStyle'; -import { QObjectSignals, NodeObject } from '../QtCore/QObject'; +import { QObjectSignals, QObject } from '../QtCore/QObject'; import { QPalette } from './QPalette'; import { StyleSheet } from '../core/Style/StyleSheet'; import memoizeOne from 'memoize-one'; @@ -27,20 +27,18 @@ const qApp = QApplication.instance(); qApp.quit(); ``` */ -export class QApplication extends NodeObject { - native: NativeElement; - constructor(); - constructor(native: NativeElement); - constructor(arg?: NativeElement) { +export class QApplication extends QObject { + constructor(arg?: QObject | NativeElement) { let native: NativeElement; if (checkIfNativeElement(arg)) { native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QObject; + native = new addon.QApplication(parent.native); } else { native = new addon.QApplication(); } super(native); - this.native = native; - this.setStyleSheet = memoizeOne(this.setStyleSheet); } devicePixelRatio(): number { @@ -104,6 +102,7 @@ export class QApplication extends NodeObject { return new QStyle(addon.QApplication.style()); } } +wrapperCache.registerWrapper('QApplicationWrap', QApplication); export interface QApplicationSignals extends QObjectSignals { focusWindowChanged: () => void; diff --git a/src/lib/QtGui/QBrush.ts b/src/lib/QtGui/QBrush.ts index b13ba953a..4c484015f 100644 --- a/src/lib/QtGui/QBrush.ts +++ b/src/lib/QtGui/QBrush.ts @@ -21,18 +21,18 @@ const brush = new QBrush(); ``` */ export class QBrush extends Component { - native: NativeElement; - constructor(arg?: NativeElement | GlobalColor | QColor, style = BrushStyle.SolidPattern) { - super(); - if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; - } else if (typeof arg === 'number') { - this.native = new addon.QBrush(arg, style); - } else if (arg == null) { - this.native = new addon.QBrush(); + constructor(nativeOrGlobalColor?: NativeElement | GlobalColor | QColor, style = BrushStyle.SolidPattern) { + let native: NativeElement; + if (checkIfNativeElement(nativeOrGlobalColor)) { + native = nativeOrGlobalColor as NativeElement; + } else if (typeof nativeOrGlobalColor === 'number') { + native = new addon.QBrush(nativeOrGlobalColor, style); + } else if (nativeOrGlobalColor == null) { + native = new addon.QBrush(); } else { - this.native = new addon.QBrush(arg?.native, style); + native = new addon.QBrush(nativeOrGlobalColor?.native, style); } + super(native); } isOpaque(): boolean { return this.native.isOpaque(); diff --git a/src/lib/QtGui/QClipboard.ts b/src/lib/QtGui/QClipboard.ts index e7df4e94c..8326db858 100644 --- a/src/lib/QtGui/QClipboard.ts +++ b/src/lib/QtGui/QClipboard.ts @@ -1,6 +1,6 @@ import { NativeElement } from '../core/Component'; import { checkIfNativeElement, registerNativeWrapFunction } from '../utils/helpers'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { QPixmap } from './QPixmap'; import { wrapperCache } from '../core/WrapperCache'; @@ -23,15 +23,12 @@ const clipboard = QApplication.clipboard(); const text = clipboard.text(QClipboardMode.Clipboard); ``` */ -export class QClipboard extends NodeObject { - native: NativeElement; +export class QClipboard extends QObject { constructor(native: NativeElement) { - super(native); - if (checkIfNativeElement(native)) { - this.native = native; - } else { + if (!checkIfNativeElement(native)) { throw new Error('QClipboard cannot be initialised this way. Use QApplication::clipboard()'); } + super(native); } clear(mode = QClipboardMode.Clipboard): void { this.native.clear(mode); @@ -49,6 +46,7 @@ export class QClipboard extends NodeObject { return new QPixmap(this.native.pixmap(mode)); } } +wrapperCache.registerWrapper('QClipboardWrap', QClipboard); export enum QClipboardMode { Clipboard = 0, diff --git a/src/lib/QtGui/QColor.ts b/src/lib/QtGui/QColor.ts index 898bd23fc..f2d026779 100644 --- a/src/lib/QtGui/QColor.ts +++ b/src/lib/QtGui/QColor.ts @@ -5,28 +5,28 @@ import { QVariant } from '../QtCore/QVariant'; import { GlobalColor } from '../QtEnums'; export class QColor extends Component { - native: NativeElement; constructor(); constructor(nativeElement: NativeElement); constructor(colorString: string); constructor(color: GlobalColor); constructor(r?: number, g?: number, b?: number, a?: number); constructor(arg?: NativeElement | number | string, g = 0, b = 0, a = 255) { - super(); + let native: NativeElement; if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else if (typeof arg === 'number') { if (arguments.length === 1) { // This is for QGlobalColor enum - this.native = new addon.QColor(arg); + native = new addon.QColor(arg); } else { - this.native = new addon.QColor(arg, g, b, a); + native = new addon.QColor(arg, g, b, a); } } else if (typeof arg === 'string') { - this.native = new addon.QColor(arg); + native = new addon.QColor(arg); } else { - this.native = new addon.QColor(); + native = new addon.QColor(); } + super(native); } setRed(value: number): void { this.native.setRed(value); diff --git a/src/lib/QtGui/QCursor.ts b/src/lib/QtGui/QCursor.ts index 331b2f6b2..a5884c1b6 100644 --- a/src/lib/QtGui/QCursor.ts +++ b/src/lib/QtGui/QCursor.ts @@ -17,17 +17,17 @@ const cursor = new QCursor(); ``` */ export class QCursor extends Component { - native: NativeElement; constructor(); constructor(native: NativeElement); constructor(shape: CursorShape); constructor(arg?: NativeElement | CursorShape) { - super(); + let native: NativeElement; if (arg) { - this.native = new addon.QCursor(arg); + native = new addon.QCursor(arg); } else { - this.native = new addon.QCursor(); + native = new addon.QCursor(); } + super(native); } pos(): { x: number; y: number } { return this.native.pos(); diff --git a/src/lib/QtGui/QDrag.ts b/src/lib/QtGui/QDrag.ts index 869ac640e..3ab6d142a 100644 --- a/src/lib/QtGui/QDrag.ts +++ b/src/lib/QtGui/QDrag.ts @@ -11,21 +11,18 @@ import { QMimeData } from '../QtCore/QMimeData'; * description */ export class QDrag extends Component { - native: NativeElement; constructor(arg?: NativeElement | QObject) { - super(); + let native: NativeElement; if (!arg) { - this.native = new addon.QDrag(); + native = new addon.QDrag(); + } else if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg.native) { + native = new addon.QDrag(arg.native); } else { - const isNative = checkIfNativeElement(arg); - if (isNative) { - this.native = arg as NativeElement; - } else if (arg.native) { - this.native = new addon.QDrag(arg.native); - } else { - this.native = new addon.QDrag(); - } + native = new addon.QDrag(); } + super(native); } /** diff --git a/src/lib/QtGui/QFont.ts b/src/lib/QtGui/QFont.ts index 645ea0057..eeb00c4cc 100644 --- a/src/lib/QtGui/QFont.ts +++ b/src/lib/QtGui/QFont.ts @@ -4,22 +4,22 @@ import { QVariant } from '../QtCore/QVariant'; import { checkIfNativeElement } from '../utils/helpers'; export class QFont extends Component { - native: NativeElement; constructor(); constructor(font: QFont); constructor(native: NativeElement); constructor(family: string, pointSize?: number, weight?: QFontWeight, italic?: boolean); constructor(arg?: QFont | string | NativeElement, pointSize = -1, weight = -1, italic = false) { - super(); + let native: NativeElement; if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else if (arg instanceof QFont) { - this.native = arg.native; + native = arg.native; } else if (typeof arg === 'string') { - this.native = new addon.QFont(arg, pointSize, weight, italic); + native = new addon.QFont(arg, pointSize, weight, italic); } else { - this.native = new addon.QFont(); + native = new addon.QFont(); } + super(native); } bold(): boolean { return this.native.bold(); diff --git a/src/lib/QtGui/QFontDatabase.ts b/src/lib/QtGui/QFontDatabase.ts index d38218aac..8e5414fbd 100644 --- a/src/lib/QtGui/QFontDatabase.ts +++ b/src/lib/QtGui/QFontDatabase.ts @@ -1,5 +1,5 @@ import addon from '../utils/addon'; -import { Component, NativeElement } from '../core/Component'; +import { Component } from '../core/Component'; export enum WritingSystem { Any = 0, @@ -40,10 +40,8 @@ export enum WritingSystem { } export class QFontDatabase extends Component { - native: NativeElement; constructor() { - super(); - this.native = new addon.QFontDatabase(); + super(new addon.QFontDatabase()); } families(ws: WritingSystem = WritingSystem.Any): string[] { return this.native.families(ws); diff --git a/src/lib/QtGui/QFontMetrics.ts b/src/lib/QtGui/QFontMetrics.ts index 84dde5468..128f9b089 100644 --- a/src/lib/QtGui/QFontMetrics.ts +++ b/src/lib/QtGui/QFontMetrics.ts @@ -7,19 +7,19 @@ import { TextElideMode, TextFlag } from '../QtEnums'; import { QRect } from '../..'; export class QFontMetrics extends Component { - native: NativeElement; constructor(native: NativeElement); constructor(qfont: QFont); constructor(qfontmetrics: QFontMetrics); constructor(arg: QFont | QFontMetrics | NativeElement) { - super(); + let native: NativeElement; if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else if (arg instanceof QFontMetrics) { - this.native = arg.native; + native = arg.native; } else { - this.native = new addon.QFontMetrics(arg.native); + native = new addon.QFontMetrics(arg.native); } + super(native); } // *** Public Functions *** diff --git a/src/lib/QtGui/QFontMetricsF.ts b/src/lib/QtGui/QFontMetricsF.ts index f5c840d8c..2b8d594dc 100644 --- a/src/lib/QtGui/QFontMetricsF.ts +++ b/src/lib/QtGui/QFontMetricsF.ts @@ -7,19 +7,19 @@ import { TextElideMode, TextFlag } from '../QtEnums'; import { QRect } from '../..'; export class QFontMetricsF extends Component { - native: NativeElement; constructor(native: NativeElement); constructor(qfont: QFont); constructor(qfontmetricsf: QFontMetricsF); constructor(arg: QFont | QFontMetricsF | NativeElement) { - super(); + let native: NativeElement; if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else if (arg instanceof QFontMetricsF) { - this.native = arg.native; + native = arg.native; } else { - this.native = new addon.QFontMetricsF(arg.native); + native = new addon.QFontMetricsF(arg.native); } + super(native); } // *** Public Functions *** diff --git a/src/lib/QtGui/QIcon.ts b/src/lib/QtGui/QIcon.ts index 66e699c7d..bc074f0bb 100644 --- a/src/lib/QtGui/QIcon.ts +++ b/src/lib/QtGui/QIcon.ts @@ -20,20 +20,20 @@ const icon = new QIcon(imageUrl); ``` */ export class QIcon extends Component { - native: NativeElement; constructor(); constructor(native: NativeElement); constructor(filePath: string); constructor(arg?: string | NativeElement) { - super(); + let native: NativeElement; if (typeof arg === 'string') { const imagePath = arg; - this.native = new addon.QIcon(imagePath); + native = new addon.QIcon(imagePath); } else if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else { - this.native = new addon.QIcon(); + native = new addon.QIcon(); } + super(native); } pixmap(width: number, height: number, mode?: QIconMode, state?: QIconState): QPixmap { let nativePixmap; diff --git a/src/lib/QtGui/QImage.ts b/src/lib/QtGui/QImage.ts index 615cb70a8..8fed74577 100644 --- a/src/lib/QtGui/QImage.ts +++ b/src/lib/QtGui/QImage.ts @@ -24,19 +24,13 @@ const image = new QImage(); ``` */ export class QImage extends Component { - native!: NativeElement; - /** Constructs a null image */ constructor(); - constructor(native: NativeElement); - /** Constructs an image and tries to load the image from the file with the given fileName */ constructor(filename: string); - /** Constructs an image with the given width, height and format */ constructor(width: number, height: number, format: QImageFormat); - /** Constructs an image with the given size and format */ constructor(size: QSize, format: QImageFormat); constructor( @@ -44,19 +38,19 @@ export class QImage extends Component { formatOrHeight?: QImageFormat | string | number, format?: QImageFormat, ) { - super(); - + let native: NativeElement; if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else if (typeof arg === 'string') { - this.native = new addon.QImage(arg); + native = new addon.QImage(arg); } else if (typeof arg === 'number') { - this.native = new addon.QImage(arg, formatOrHeight, format); + native = new addon.QImage(arg, formatOrHeight, format); } else if (arg instanceof QSize) { - this.native = new addon.QImage(arg.native, formatOrHeight); + native = new addon.QImage(arg.native, formatOrHeight); } else { - this.native = new addon.QImage(); + native = new addon.QImage(); } + super(native); } /** diff --git a/src/lib/QtGui/QKeySequence.ts b/src/lib/QtGui/QKeySequence.ts index 158ad191f..efcef2775 100644 --- a/src/lib/QtGui/QKeySequence.ts +++ b/src/lib/QtGui/QKeySequence.ts @@ -18,20 +18,20 @@ const keySequence = new QKeySequence(`Ctrl+L`); ``` */ export class QKeySequence extends Component { - native: NativeElement; constructor(); constructor(native: NativeElement); constructor(keySequence: string); constructor(arg?: string | NativeElement) { - super(); + let native: NativeElement; if (typeof arg === 'string') { const keySequence = arg; - this.native = new addon.QKeySequence(keySequence); + native = new addon.QKeySequence(keySequence); } else if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else { - this.native = new addon.QKeySequence(); + native = new addon.QKeySequence(); } + super(native); } count(): number { return this.native.count(); diff --git a/src/lib/QtGui/QMovie.ts b/src/lib/QtGui/QMovie.ts index f9c25871b..c322cd3c8 100644 --- a/src/lib/QtGui/QMovie.ts +++ b/src/lib/QtGui/QMovie.ts @@ -1,28 +1,23 @@ import addon from '../utils/addon'; import { NativeElement } from '../core/Component'; import { checkIfNativeElement } from '../utils/helpers'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { QSize } from '../QtCore/QSize'; import { QPixmap } from './QPixmap'; +import { wrapperCache } from '../core/WrapperCache'; -export class QMovie extends NodeObject { - native: NativeElement; - constructor(); - constructor(native: NativeElement); - constructor(parent: NodeObject); - constructor(arg?: NodeObject | NativeElement) { +export class QMovie extends QObject { + constructor(arg?: QObject | NativeElement) { let native: NativeElement; - if (arg) { - if (checkIfNativeElement(arg)) { - native = arg as NativeElement; - } else { - native = new addon.QMovie(arg); - } + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QObject; + native = new addon.QMovie(parent.native); } else { native = new addon.QMovie(); } super(native); - this.native = native; } //Methods setFileName(fileName: string): void { @@ -78,6 +73,7 @@ export class QMovie extends NodeObject { return this.native.frameCount(); } } +wrapperCache.registerWrapper('QMovieWrap', QMovie); export enum CacheMode { CacheNone, diff --git a/src/lib/QtGui/QPalette.ts b/src/lib/QtGui/QPalette.ts index d8365ec31..377ac911a 100644 --- a/src/lib/QtGui/QPalette.ts +++ b/src/lib/QtGui/QPalette.ts @@ -1,4 +1,4 @@ -import { Component, NativeElement } from '../core/Component'; +import { Component } from '../core/Component'; import { QColor } from './QColor'; export enum ColorGroup { @@ -33,12 +33,6 @@ export enum ColorRole { } export class QPalette extends Component { - native: NativeElement; - constructor(native: NativeElement) { - super(); - this.native = native; - } - color(group: ColorGroup, role: ColorRole): QColor { return new QColor(this.native.color(group, role)); } diff --git a/src/lib/QtGui/QPen.ts b/src/lib/QtGui/QPen.ts index 69f0f33de..119db3d56 100644 --- a/src/lib/QtGui/QPen.ts +++ b/src/lib/QtGui/QPen.ts @@ -1,4 +1,4 @@ -import { Component, NativeElement } from '../core/Component'; +import { Component } from '../core/Component'; import addon from '../utils/addon'; import { GlobalColor, PenStyle, PenCapStyle } from '../QtEnums'; import { QColor } from './QColor'; @@ -18,10 +18,8 @@ const pen = new QPen(); ``` */ export class QPen extends Component { - native: NativeElement; constructor() { - super(); - this.native = new addon.QPen(); + super(new addon.QPen()); } setColor(color: QColor | GlobalColor): void { if (typeof color === 'number') { diff --git a/src/lib/QtGui/QPicture.ts b/src/lib/QtGui/QPicture.ts index 11e1e8a89..a420416a0 100644 --- a/src/lib/QtGui/QPicture.ts +++ b/src/lib/QtGui/QPicture.ts @@ -18,20 +18,20 @@ const picture = new QPicture(); ``` */ export class QPicture extends Component { - native: NativeElement; constructor(); constructor(native: NativeElement); constructor(formatVersion: number); constructor(arg?: number | NativeElement) { - super(); + let native: NativeElement; if (typeof arg === 'number') { const formatVersion = arg; - this.native = new addon.QPicture(formatVersion); + native = new addon.QPicture(formatVersion); } else if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else { - this.native = new addon.QPicture(); + native = new addon.QPicture(); } + super(native); } setBoundingRect(r: QRect): void { this.native.setBoundingRect(r.native); diff --git a/src/lib/QtGui/QPixmap.ts b/src/lib/QtGui/QPixmap.ts index 3cabe183e..8999810cc 100644 --- a/src/lib/QtGui/QPixmap.ts +++ b/src/lib/QtGui/QPixmap.ts @@ -23,20 +23,20 @@ const pixMap = new QPixmap(imageUrl); ``` */ export class QPixmap extends Component { - native: NativeElement; constructor(); constructor(native: NativeElement); constructor(filePath: string); constructor(arg?: string | NativeElement) { - super(); + let native: NativeElement; if (typeof arg === 'string') { const imagePath = arg; - this.native = new addon.QPixmap(imagePath); + native = new addon.QPixmap(imagePath); } else if (checkIfNativeElement(arg)) { - this.native = arg as NativeElement; + native = arg as NativeElement; } else { - this.native = new addon.QPixmap(); + native = new addon.QPixmap(); } + super(native); } load(imagePath: string): boolean { return this.native.load(imagePath); diff --git a/src/lib/QtGui/QScreen.ts b/src/lib/QtGui/QScreen.ts index 39f3611ba..f7cd375f6 100644 --- a/src/lib/QtGui/QScreen.ts +++ b/src/lib/QtGui/QScreen.ts @@ -1,21 +1,18 @@ import { NativeElement } from '../core/Component'; import { checkIfNativeElement, registerNativeWrapFunction } from '../utils/helpers'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { QRect } from '../QtCore/QRect'; import { QSizeF } from '../QtCore/QSizeF'; import { QSize } from '../QtCore/QSize'; import { wrapperCache } from '../core/WrapperCache'; import { QPixmap } from './QPixmap'; -export class QScreen extends NodeObject { - native: NativeElement; +export class QScreen extends QObject { constructor(native: NativeElement) { - super(native); - if (checkIfNativeElement(native)) { - this.native = native; - } else { + if (!checkIfNativeElement(native)) { throw new Error('QScreen cannot be initialised this way.'); } + super(native); } availableGeometry(): QRect { @@ -97,6 +94,7 @@ export class QScreen extends NodeObject { return QSize.fromQVariant(this.property('virtualSize')); } } +wrapperCache.registerWrapper('QScreenWrap', QScreen); export interface QScreenSignals extends QObjectSignals { availableGeometryChanged: (geometry: QRect) => void; diff --git a/src/lib/QtGui/QStyle.ts b/src/lib/QtGui/QStyle.ts index 2db30408c..df5080cd9 100644 --- a/src/lib/QtGui/QStyle.ts +++ b/src/lib/QtGui/QStyle.ts @@ -1,25 +1,22 @@ -import { NodeWidget } from '../..'; +import { QWidget } from '../..'; import { Component, NativeElement } from '../core/Component'; import { checkIfNativeElement } from '../utils/helpers'; export class QStyle extends Component { - native: NativeElement; constructor(native: NativeElement) { - super(); - if (checkIfNativeElement(native)) { - this.native = native; - } else { + if (!checkIfNativeElement(native)) { throw new Error('QStyle cannot be initialised this way. Use QApplication::style()'); } + super(native); } pixelMetric(metric: QStylePixelMetric): number { return this.native.pixelMetric(metric); } - polish(widget: NodeWidget): void { + polish(widget: QWidget): void { this.native.polish(widget.native); } - unpolish(widget: NodeWidget): void { + unpolish(widget: QWidget): void { this.native.unpolish(widget.native); } } diff --git a/src/lib/QtGui/QWindow.ts b/src/lib/QtGui/QWindow.ts index 7adcc620e..2dd0634e3 100644 --- a/src/lib/QtGui/QWindow.ts +++ b/src/lib/QtGui/QWindow.ts @@ -1,20 +1,16 @@ import { NativeElement } from '../core/Component'; import { checkIfNativeElement, registerNativeWrapFunction } from '../utils/helpers'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { QScreen } from './QScreen'; import { wrapperCache } from '../core/WrapperCache'; import { Edge, Visibility, WindowState } from '../QtEnums'; -export class QWindow extends NodeObject { - native: NativeElement; +export class QWindow extends QObject { constructor(native: NativeElement) { - super(native); - - if (checkIfNativeElement(native)) { - this.native = native; - } else { + if (!checkIfNativeElement(native)) { throw new Error('QWindow cannot be initialised this way.'); } + super(native); } screen(): QScreen { @@ -73,6 +69,7 @@ export class QWindow extends NodeObject { return this.native.setVisibility(visibility); } } +wrapperCache.registerWrapper('QWindowWrap', QWindow); export interface QWindowSignals extends QObjectSignals { screenChanged: (screen: QScreen) => void; diff --git a/src/lib/QtWidgets/QAbstractButton.ts b/src/lib/QtWidgets/QAbstractButton.ts index b2e61b295..f62b11e8a 100644 --- a/src/lib/QtWidgets/QAbstractButton.ts +++ b/src/lib/QtWidgets/QAbstractButton.ts @@ -1,10 +1,10 @@ -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { QIcon } from '../QtGui/QIcon'; import { QSize } from '../QtCore/QSize'; import { QKeySequence } from '../QtGui/QKeySequence'; /** - + > This is the abstract base class of button widgets, providing their functionality. * **This class is a JS wrapper around Qt's [QAbstractButton class](https://doc.qt.io/qt-5/qabstractbutton.html)** @@ -13,7 +13,7 @@ The QAbstractButton class is an abstract class and therefore, technically, no fu It is inherited by QCheckBox, QPushButton, QRadioButton, and QToolButton. */ -export abstract class QAbstractButton extends NodeWidget { +export abstract class QAbstractButton extends QWidget { animateClick(msec: number): void { this.native.animateClick(msec); } diff --git a/src/lib/QtWidgets/QAbstractItemView.ts b/src/lib/QtWidgets/QAbstractItemView.ts index a7f0d482e..472163564 100644 --- a/src/lib/QtWidgets/QAbstractItemView.ts +++ b/src/lib/QtWidgets/QAbstractItemView.ts @@ -8,6 +8,7 @@ import { QAbstractItemModel } from '../QtCore/QAbstractItemModel'; import { QPoint } from '../QtCore/QPoint'; import { QItemSelectionModel } from '../QtCore/QItemSelectionModel'; import { NativeElement } from '../core/Component'; +import { wrapperCache } from '../core/WrapperCache'; /** @@ -63,7 +64,7 @@ export abstract class QAbstractItemView extends NodeFrame { - viewportWidget?: NodeWidget; - setViewport(widget: NodeWidget): void { - this.viewportWidget = widget; +export abstract class QAbstractScrollArea extends QFrame { + setViewport(widget: QWidget): void { this.native.setViewport(widget.native); } viewport(): QWidget { - if (!this.viewportWidget) { - this.viewportWidget = new QWidget(this.native.viewport()); - } - return this.viewportWidget; + return wrapperCache.getWrapper(this.native.viewport()) as QWidget; } maximumViewportSize(): QSize { return this.native.maximumViewportSize(); diff --git a/src/lib/QtWidgets/QAbstractSlider.ts b/src/lib/QtWidgets/QAbstractSlider.ts index 17a89f19a..be8521482 100644 --- a/src/lib/QtWidgets/QAbstractSlider.ts +++ b/src/lib/QtWidgets/QAbstractSlider.ts @@ -1,8 +1,8 @@ -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { Orientation } from '../QtEnums'; /** - + > Abstract class to add functionalities common to all slider based widgets. **This class implements all methods, properties of Qt's [QAbstractSlider class](https://doc.qt.io/qt-5/qabstractslider.html) so that it can be inherited by all slider based widgets** @@ -14,7 +14,7 @@ import { Orientation } from '../QtEnums'; QAbstractSlider will list all methods and properties that are common to all slider widgets in the NodeGui world. */ -export abstract class QAbstractSlider extends NodeWidget { +export abstract class QAbstractSlider extends QWidget { triggerAction(action: SliderAction): void { this.native.triggerAction(action); } diff --git a/src/lib/QtWidgets/QAbstractSpinBox.ts b/src/lib/QtWidgets/QAbstractSpinBox.ts index fdbd7fb40..6433c9ff9 100644 --- a/src/lib/QtWidgets/QAbstractSpinBox.ts +++ b/src/lib/QtWidgets/QAbstractSpinBox.ts @@ -1,8 +1,8 @@ -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { AlignmentFlag } from '../QtEnums'; /** - + > This is the abstract base class of button widgets, providing their functionality. * **This class is a JS wrapper around Qt's [QAbstractSpinBox class](https://doc.qt.io/qt-5/qabstractspinbox.html)** @@ -11,7 +11,7 @@ The QAbstractSpinBox class is an abstract class and therefore, technically, no f It is inherited by QDateTimeEdit and QSpinBox. (n/a QDoubleSpinBox) */ -export abstract class QAbstractSpinBox extends NodeWidget { +export abstract class QAbstractSpinBox extends QWidget { selectAll(): void { this.native.selectAll(); } diff --git a/src/lib/QtWidgets/QAction.ts b/src/lib/QtWidgets/QAction.ts index 28772a8e7..9dddb5fc2 100644 --- a/src/lib/QtWidgets/QAction.ts +++ b/src/lib/QtWidgets/QAction.ts @@ -1,14 +1,14 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; import { NativeElement } from '../core/Component'; import { QMenu } from './QMenu'; import { QIcon } from '../QtGui/QIcon'; import { QFont } from '../QtGui/QFont'; import { QKeySequence } from '../QtGui/QKeySequence'; import { ShortcutContext } from '../QtEnums'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { checkIfNativeElement } from '../utils/helpers'; import { QVariant } from '../QtCore/QVariant'; +import { wrapperCache } from '../core/WrapperCache'; /** @@ -30,22 +30,21 @@ menuAction.addEventListener("triggered", () => { menu.addAction(menuAction); ``` */ -export class QAction extends NodeObject { - native: NativeElement; +export class QAction extends QObject { constructor(); constructor(native: NativeElement); - constructor(parent: NodeWidget); - constructor(parent?: NativeElement | NodeWidget) { - let native; - if (checkIfNativeElement(parent)) { - native = parent as NativeElement; - } else if (parent) { + constructor(parent: QObject); + constructor(arg?: NativeElement | QObject) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg) { + const parent = arg as QObject; native = new addon.QAction(parent.native); } else { native = new addon.QAction(); } super(native); - this.native = native; } setText(text: string): void { this.native.setText(text); @@ -96,6 +95,7 @@ export class QAction extends NodeObject { return new QVariant(this.native.data()); } } +wrapperCache.registerWrapper('QActionWrap', QAction); export interface QActionSignals extends QObjectSignals { triggered: (checked: boolean) => void; @@ -103,3 +103,4 @@ export interface QActionSignals extends QObjectSignals { hovered: () => void; toggled: (checked: boolean) => void; } +wrapperCache.registerWrapper('QActionWrap', QAction); diff --git a/src/lib/QtWidgets/QBoxLayout.ts b/src/lib/QtWidgets/QBoxLayout.ts index 39df53f36..731939284 100644 --- a/src/lib/QtWidgets/QBoxLayout.ts +++ b/src/lib/QtWidgets/QBoxLayout.ts @@ -1,8 +1,10 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NodeLayout, QLayoutSignals } from './QLayout'; +import { QWidget } from './QWidget'; +import { QLayout, QLayoutSignals } from './QLayout'; import { NativeElement } from '../core/Component'; import { AlignmentFlag, Direction } from '../QtEnums'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** @@ -25,26 +27,20 @@ boxLayout.addWidget(new QCalendarWidget()); centralWidget.setLayout(boxLayout); ``` */ -export class QBoxLayout extends NodeLayout { - native: NativeElement; - childLayouts: Set>; - constructor(dir: Direction); - constructor(dir: Direction, parent: NodeWidget); - constructor(dir: Direction, parent?: NodeWidget) { +export class QBoxLayout extends QLayout { + constructor(arg: NativeElement | Direction, parent?: QWidget) { let native: NativeElement; - if (parent) { - native = new addon.QBoxLayout(dir, parent.native); + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (parent) { + native = new addon.QBoxLayout(arg as Direction, parent.native); } else { - native = new addon.QBoxLayout(dir); + native = new addon.QBoxLayout(arg as Direction); } super(native); - this.setNodeParent(parent); - this.native = native; - this.childLayouts = new Set(); } - addLayout(layout: NodeLayout, stretch = 0): void { + addLayout(layout: QLayout, stretch = 0): void { this.native.addLayout(layout.native, stretch); - this.childLayouts.add(layout); } addSpacing(size: number): void { this.native.addSpacing(size); @@ -55,20 +51,17 @@ export class QBoxLayout extends NodeLayout { addStrut(size: number): void { this.native.addStrut(size); } - addWidget(widget: NodeWidget, stretch = 0, alignment: AlignmentFlag = 0): void { + addWidget(widget: QWidget, stretch = 0, alignment: AlignmentFlag = 0): void { this.native.addWidget(widget.native, stretch, alignment); - this.nodeChildren.add(widget); } - insertWidget(index: number, widget: NodeWidget, stretch = 0): void { + insertWidget(index: number, widget: QWidget, stretch = 0): void { this.native.insertWidget(index, widget.native, stretch); - this.nodeChildren.add(widget); } direction(): Direction { return this.native.direction(); } - insertLayout(index: number, layout: NodeLayout, stretch = 0): void { + insertLayout(index: number, layout: QLayout, stretch = 0): void { this.native.insertLayout(index, layout.native, stretch); - this.childLayouts.add(layout); } insertSpacing(index: number, size: number): void { this.native.insertSpacing(index, size); @@ -76,9 +69,8 @@ export class QBoxLayout extends NodeLayout { insertStretch(index: number, stretch = 0): void { this.native.insertStretch(index, stretch); } - removeWidget(widget: NodeWidget): void { + removeWidget(widget: QWidget): void { this.native.removeWidget(widget.native); - this.nodeChildren.delete(widget); } setDirection(dir: Direction): void { this.native.setDirection(dir); @@ -90,5 +82,6 @@ export class QBoxLayout extends NodeLayout { return this.native.count(); } } +wrapperCache.registerWrapper('QBoxLayoutWrap', QBoxLayout); export type QBoxLayoutSignals = QLayoutSignals; diff --git a/src/lib/QtWidgets/QButtonGroup.ts b/src/lib/QtWidgets/QButtonGroup.ts index 817aaaed9..3fbecdafb 100644 --- a/src/lib/QtWidgets/QButtonGroup.ts +++ b/src/lib/QtWidgets/QButtonGroup.ts @@ -1,35 +1,33 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement, NativeRawPointer } from '../core/Component'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { QAbstractButton, QAbstractButtonSignals } from './QAbstractButton'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; export interface QButtonGroupSignals extends QObjectSignals { buttonClicked: (id?: number) => void; } -export class QButtonGroup extends NodeObject { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QButtonGroup extends QObject { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QButtonGroup(parent.native); } else { native = new addon.QButtonGroup(); } super(native); - this.native = native; - parent && parent.nodeChildren.add(this); } addButton(button: QAbstractButton, id = -1): void { this.native.addButton(button.native, id); - this.nodeChildren.add(button); } removeButton(button: QAbstractButton): void { this.native.removeButton(button.native); - this.nodeChildren.delete(button); } setExclusive(exculsive: boolean): void { this.native.setProperty('exclusive', exculsive); @@ -56,3 +54,4 @@ export class QButtonGroup extends NodeObject { return this.native.button(id); } } +wrapperCache.registerWrapper('QButtonGroupWrap', QButtonGroup); diff --git a/src/lib/QtWidgets/QCalendarWidget.ts b/src/lib/QtWidgets/QCalendarWidget.ts index b8ef50dcf..1b3430147 100644 --- a/src/lib/QtWidgets/QCalendarWidget.ts +++ b/src/lib/QtWidgets/QCalendarWidget.ts @@ -1,11 +1,13 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QDate } from '../QtCore/QDate'; import { DayOfWeek } from '../QtEnums'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Create and control a selectable monthly calendar. * **This class is a JS wrapper around Qt's [QCalendarWidget class](https://doc.qt.io/qt-5/qcalendarwidget.html)** @@ -21,20 +23,18 @@ const calendarWidget = new QCalendarWidget(); // more will follow when .selectedDate() et cetera are implemented ``` */ -export class QCalendarWidget extends NodeWidget { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QCalendarWidget extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QCalendarWidget(parent.native); } else { native = new addon.QCalendarWidget(); } super(native); - this.native = native; - this.setNodeParent(parent); } setDateEditAcceptDelay(delay: number): void { this.setProperty('dateEditAcceptDelay', delay); @@ -85,6 +85,7 @@ export class QCalendarWidget extends NodeWidget { return this.property('verticalHeaderFormat').toInt(); } } +wrapperCache.registerWrapper('QCalendarWidgetWrap', QCalendarWidget); export enum HorizontalHeaderFormat { NoHorizontalHeader, diff --git a/src/lib/QtWidgets/QCheckBox.ts b/src/lib/QtWidgets/QCheckBox.ts index 73d8b5c0d..4d24bc1fd 100644 --- a/src/lib/QtWidgets/QCheckBox.ts +++ b/src/lib/QtWidgets/QCheckBox.ts @@ -1,12 +1,13 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement, NativeRawPointer, Component } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { NativeElement } from '../core/Component'; import { QAbstractButton, QAbstractButtonSignals } from './QAbstractButton'; -import { checkIfNativeElement, checkIfNapiExternal } from '../utils/helpers'; +import { checkIfNativeElement } from '../utils/helpers'; import { CheckState } from '../QtEnums'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Create and control checkbox. * **This class is a JS wrapper around Qt's [QCheckBox class](https://doc.qt.io/qt-5/qcheckbox.html)** @@ -23,28 +24,19 @@ checkbox.setText("Hello"); ``` */ export class QCheckBox extends QAbstractButton { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(rawPointer: NativeRawPointer, disableNativeDeletion?: boolean); - constructor(arg?: NodeWidget | NativeRawPointer | NativeElement, disableNativeDeletion = true) { - let native; - let parent: Component | undefined; + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; if (checkIfNativeElement(arg)) { native = arg as NativeElement; - } else if (checkIfNapiExternal(arg)) { - native = new addon.QCheckBox(arg, disableNativeDeletion); - } else if (arg) { - const parentWidget = arg as NodeWidget; - native = new addon.QCheckBox(parentWidget.native); - parent = parentWidget; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QCheckBox(parent.native); } else { native = new addon.QCheckBox(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } + setTristate(y = true): void { this.setProperty('tristate', y); } @@ -58,6 +50,7 @@ export class QCheckBox extends QAbstractButton { this.native.setCheckState(state); } } +wrapperCache.registerWrapper('QCheckBoxWrap', QCheckBox); export interface QCheckBoxSignals extends QAbstractButtonSignals { stateChanged: (state: number) => void; diff --git a/src/lib/QtWidgets/QColorDialog.ts b/src/lib/QtWidgets/QColorDialog.ts index a4784d3e5..5656810ef 100644 --- a/src/lib/QtWidgets/QColorDialog.ts +++ b/src/lib/QtWidgets/QColorDialog.ts @@ -1,11 +1,13 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; -import { NodeDialog, QDialogSignals } from './QDialog'; +import { QDialog, QDialogSignals } from './QDialog'; import { QColor } from '../QtGui/QColor'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control color dialogs. * **This class is a JS wrapper around Qt's [QColorDialog class](https://doc.qt.io/qt-5/qcolordialog.html)** @@ -26,21 +28,20 @@ console.log(color.red(), color.green(), color.blue()); ``` */ -export class QColorDialog extends NodeDialog { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QColorDialog extends QDialog { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QColorDialog(parent.native); } else { native = new addon.QColorDialog(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } + setCurrentColor(color: QColor): void { this.setProperty('currentColor', color.native); } @@ -89,3 +90,5 @@ export interface QColorDialogSignals extends QDialogSignals { colorSelected: (color: QColor) => void; currentColorChanged: (color: QColor) => void; } + +wrapperCache.registerWrapper('QColorDialogWrap', QColorDialog); diff --git a/src/lib/QtWidgets/QComboBox.ts b/src/lib/QtWidgets/QComboBox.ts index 77149bd9a..17f4ccadd 100644 --- a/src/lib/QtWidgets/QComboBox.ts +++ b/src/lib/QtWidgets/QComboBox.ts @@ -1,5 +1,5 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { SizeAdjustPolicy } from '../QtEnums'; import { QIcon } from '../QtGui/QIcon'; @@ -7,6 +7,8 @@ import { QVariant } from '../QtCore/QVariant'; import { QStandardItemModel } from './QStandardItemModel'; import { QSize } from '../QtCore/QSize'; import { QModelIndex } from '../QtCore/QModelIndex'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** @@ -36,20 +38,18 @@ comboBox.addEventListener('currentIndexChanged', (index) => { }); ``` */ -export class QComboBox extends NodeWidget { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QComboBox extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QComboBox(parent.native); } else { native = new addon.QComboBox(); } super(native); - this.native = native; - this.setNodeParent(parent); } // *** Public Functions *** addItem(icon: QIcon | undefined, text: string, userData: QVariant = new QVariant()): void { @@ -226,3 +226,5 @@ export interface QComboBoxSignals extends QWidgetSignals { textActivated: (text: string) => void; textHighlighted: (text: string) => void; } + +wrapperCache.registerWrapper('QComboBoxWrap', QComboBox); diff --git a/src/lib/QtWidgets/QDateEdit.ts b/src/lib/QtWidgets/QDateEdit.ts index 1aa329f50..6b58cd75c 100644 --- a/src/lib/QtWidgets/QDateEdit.ts +++ b/src/lib/QtWidgets/QDateEdit.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; -import { NodeDateTimeEdit } from './QDateTimeEdit'; +import { QDateTimeEdit } from './QDateTimeEdit'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Creates a widget to edit dates with spin box layout. WIP! * **This class is a JS wrapper around Qt's [QDateEdit class](https://doc.qt.io/qt-5/qdateedit.html)** @@ -20,19 +22,18 @@ const dateEdit = new QDateEdit(); // must be implemented ``` */ -export class QDateEdit extends NodeDateTimeEdit { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QDateEdit extends QDateTimeEdit { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QDateEdit(parent.native); } else { native = new addon.QDateEdit(); } super(native); - this.native = native; - this.setNodeParent(parent); } } +wrapperCache.registerWrapper('QDateEditWrap', QDateEdit); diff --git a/src/lib/QtWidgets/QDateTimeEdit.ts b/src/lib/QtWidgets/QDateTimeEdit.ts index 951a1a677..bcd46c17f 100644 --- a/src/lib/QtWidgets/QDateTimeEdit.ts +++ b/src/lib/QtWidgets/QDateTimeEdit.ts @@ -1,25 +1,56 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; import { QAbstractSpinBox, QAbstractSpinBoxSignals } from './QAbstractSpinBox'; import { QCalendarWidget } from './QCalendarWidget'; import { QDate } from '../QtCore/QDate'; import { QDateTime } from '../QtCore/QDateTime'; import { QTime } from '../QtCore/QTime'; import { TimeSpec } from '../QtEnums'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; -export abstract class NodeDateTimeEdit extends QAbstractSpinBox { - calendar?: QCalendarWidget; +/** + +> Creates and controls a widget for editing dates and times with spin box layout. + +* **This class is a JS wrapper around Qt's [QDateTimeEdit class](https://doc.qt.io/qt-5/qdatetimeedit.html)** + +### Example + +```javascript +const { QDateTimeEdit, QDate, QTime } = require("@nodegui/nodegui"); + +const dateTimeEdit = new QDateTimeEdit(); + +let date = new QDate(); +date.setDate(2020, 1, 1); + +let time = new QTime(); +time.setHMS(16, 30, 0); + +dateTimeEdit.setDate(date); +dateTimeEdit.setTime(time); +``` + */ +export class QDateTimeEdit extends QAbstractSpinBox { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QDateTimeEdit(parent.native); + } else { + native = new addon.QDateTimeEdit(); + } + super(native); + } setCalendarWidget(calendarWidget: QCalendarWidget): void { - this.calendar = calendarWidget; this.native.setCalendarWidget(calendarWidget.native); } calendarWidget(): QCalendarWidget | null { - const calendar = this.calendar; - if (calendar) { - return calendar; - } - return null; + return wrapperCache.getWrapper(this.native.calendarWidget()) as QCalendarWidget; } setCalendarPopup(enable: boolean): void { this.setProperty('calendarPopup', enable); @@ -61,46 +92,7 @@ export abstract class NodeDateTimeEdit extends QAbstractSpinBox Creates and controls a widget for editing dates and times with spin box layout. - -* **This class is a JS wrapper around Qt's [QDateTimeEdit class](https://doc.qt.io/qt-5/qdatetimeedit.html)** - -### Example - -```javascript -const { QDateTimeEdit, QDate, QTime } = require("@nodegui/nodegui"); - -const dateTimeEdit = new QDateTimeEdit(); - -let date = new QDate(); -date.setDate(2020, 1, 1); - -let time = new QTime(); -time.setHMS(16, 30, 0); - -dateTimeEdit.setDate(date); -dateTimeEdit.setTime(time); -``` - */ -export class QDateTimeEdit extends NodeDateTimeEdit { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { - native = new addon.QDateTimeEdit(parent.native); - } else { - native = new addon.QDateTimeEdit(); - } - super(native); - this.native = native; - this.setNodeParent(parent); - } -} +wrapperCache.registerWrapper('QDateTimeEditWrap', QDateTimeEdit); export interface QDateTimeEditSignals extends QAbstractSpinBoxSignals { dateChanged: (date: QDate) => void; diff --git a/src/lib/QtWidgets/QDial.ts b/src/lib/QtWidgets/QDial.ts index 928e72923..c7ef89654 100644 --- a/src/lib/QtWidgets/QDial.ts +++ b/src/lib/QtWidgets/QDial.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QAbstractSlider, QAbstractSliderSignals } from './QAbstractSlider'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control dial slider widgets. * **This class is a JS wrapper around Qt's [QDial class](https://doc.qt.io/qt-5/qdial.html)** @@ -20,19 +22,17 @@ const dial = new QDial(); ``` */ export class QDial extends QAbstractSlider { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QDial(parent.native); } else { native = new addon.QDial(); } super(native); - this.native = native; - this.setNodeParent(parent); } notchSize(): number { return this.property('notchSize').toInt(); @@ -56,5 +56,6 @@ export class QDial extends QAbstractSlider { return this.property('wrapping').toBool(); } } +wrapperCache.registerWrapper('QDialWrap', QDial); export type QDialSignals = QAbstractSliderSignals; diff --git a/src/lib/QtWidgets/QDialog.ts b/src/lib/QtWidgets/QDialog.ts index a5958ac3d..f0f93639b 100644 --- a/src/lib/QtWidgets/QDialog.ts +++ b/src/lib/QtWidgets/QDialog.ts @@ -2,12 +2,32 @@ import addon from '../utils/addon'; import { NativeElement } from '../core/Component'; import { checkIfNativeElement } from '../utils/helpers'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { DialogCode } from '../QtEnums'; +import { wrapperCache } from '../core/WrapperCache'; + +/** + +> This is the base class of dialog windows. + +* **This class is a JS wrapper around Qt's [QDialog class](https://doc.qt.io/qt-5/qdialog.html)** + +It is inherited by QFileDialog and QMessageBox (n/a QColorDialog, QErrorMessage, QFontDialog, QInputDialog, QMessageBox, QProgressDialog, and QWizard) + */ +export class QDialog extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QDialog(parent.native); + } else { + native = new addon.QDialog(); + } + super(native); + } -// All Dialogs should extend from NodeDialog -// Implement all native QDialog methods here so that all dialogs get access to those aswell -export abstract class NodeDialog extends NodeWidget { setResult(i: number): void { this.native.setResult(i); } @@ -33,33 +53,7 @@ export abstract class NodeDialog extends NodeWid this.native.reject(); } } - -/** - -> This is the base class of dialog windows. - -* **This class is a JS wrapper around Qt's [QDialog class](https://doc.qt.io/qt-5/qdialog.html)** - -It is inherited by QFileDialog and QMessageBox (n/a QColorDialog, QErrorMessage, QFontDialog, QInputDialog, QMessageBox, QProgressDialog, and QWizard) - */ -export class QDialog extends NodeDialog { - native: NativeElement; - constructor(arg?: NodeDialog | NativeElement) { - let native; - let parent; - if (checkIfNativeElement(arg)) { - native = arg as NativeElement; - } else if (arg as NodeDialog) { - parent = arg as NodeDialog; - native = new addon.QDialog(parent.native); - } else { - native = new addon.QDialog(); - } - super(native); - this.setNodeParent(parent); - this.native = native; - } -} +wrapperCache.registerWrapper('QDialogWrap', QDialog); export interface QDialogSignals extends QWidgetSignals { accepted: () => void; diff --git a/src/lib/QtWidgets/QDoubleSpinBox.ts b/src/lib/QtWidgets/QDoubleSpinBox.ts index 38d08e5a2..8cbc73e03 100644 --- a/src/lib/QtWidgets/QDoubleSpinBox.ts +++ b/src/lib/QtWidgets/QDoubleSpinBox.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QAbstractSpinBox, QAbstractSpinBoxSignals, StepType } from './QAbstractSpinBox'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Create and control double spin box widgets. * **This class is a JS wrapper around Qt's [QDoubleSpinBox class](https://doc.qt.io/qt-5/qdoublespinbox.html)** @@ -20,19 +22,17 @@ const doublespinBox = new QDoubleSpinBox(); ``` */ export class QDoubleSpinBox extends QAbstractSpinBox { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QDoubleSpinBox(parent.native); } else { native = new addon.QDoubleSpinBox(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } cleanText(): string { return this.property('cleanText').toString(); @@ -95,6 +95,7 @@ export class QDoubleSpinBox extends QAbstractSpinBox { return this.native.valueFromText(text); } } +wrapperCache.registerWrapper('QDoubleSpinBoxWrap', QDoubleSpinBox); export interface QDoubleSpinBoxSignals extends QAbstractSpinBoxSignals { valueChanged: (value: number) => void; diff --git a/src/lib/QtWidgets/QErrorMessage.ts b/src/lib/QtWidgets/QErrorMessage.ts index adef87987..b6188b803 100644 --- a/src/lib/QtWidgets/QErrorMessage.ts +++ b/src/lib/QtWidgets/QErrorMessage.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; -import { NodeDialog, QDialogSignals } from './QDialog'; +import { QDialog, QDialogSignals } from './QDialog'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Create and control error message dialogs. * **This class is a JS wrapper around Qt's [QErrorMessage class](https://doc.qt.io/qt-5/qerrormessage.html)** @@ -20,24 +22,23 @@ const errorMessage = new QErrorMessage(); ``` */ -export class QErrorMessage extends NodeDialog { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QErrorMessage extends QDialog { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QErrorMessage(parent.native); } else { native = new addon.QErrorMessage(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } showMessage(message: string): void { this.native.showMessage(message); } } +wrapperCache.registerWrapper('QErrorMessageWrap', QErrorMessage); export type QErrorMessageSignals = QDialogSignals; diff --git a/src/lib/QtWidgets/QFileDialog.ts b/src/lib/QtWidgets/QFileDialog.ts index 4f5b94f62..d79ef7724 100644 --- a/src/lib/QtWidgets/QFileDialog.ts +++ b/src/lib/QtWidgets/QFileDialog.ts @@ -1,11 +1,13 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget } from './QWidget'; import { NativeElement } from '../core/Component'; import { AcceptMode, DialogLabel, FileMode, Option, ViewMode } from '../QtEnums'; -import { NodeDialog, QDialogSignals } from './QDialog'; +import { QDialog, QDialogSignals } from './QDialog'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control file dialogs. * **This class is a JS wrapper around Qt's [QFileDialog class](https://doc.qt.io/qt-5/qfiledialog.html)** @@ -27,20 +29,19 @@ console.log(selectedFiles); ``` */ -export class QFileDialog extends NodeDialog { - native: NativeElement; +export class QFileDialog extends QDialog { constructor(); - constructor(parent: NodeWidget, caption?: string, directory?: string, filter?: string); - constructor(parent?: NodeWidget, caption = 'Select File', directory = '', filter = '') { - let native; - if (parent) { - native = new addon.QFileDialog(parent.native, caption, directory, filter); + constructor(parent: QWidget, caption?: string, directory?: string, filter?: string); + constructor(arg?: QWidget, caption = 'Select File', directory = '', filter = '') { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + native = new addon.QFileDialog(arg.native, caption, directory, filter); } else { native = new addon.QFileDialog(); } super(native); - this.native = native; - this.setNodeParent(parent); } supportedSchemes(): string[] { return this.native.supportedSchemes(); @@ -91,6 +92,7 @@ export class QFileDialog extends NodeDialog { this.setProperty('options', options); } } +wrapperCache.registerWrapper('QFileDialogWrap', QFileDialog); export interface QFileDialogSignals extends QDialogSignals { currentChanged: (path: string) => void; diff --git a/src/lib/QtWidgets/QFontDialog.ts b/src/lib/QtWidgets/QFontDialog.ts index cb553a276..ccf22b759 100644 --- a/src/lib/QtWidgets/QFontDialog.ts +++ b/src/lib/QtWidgets/QFontDialog.ts @@ -1,11 +1,13 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; -import { NodeDialog, QDialogSignals } from './QDialog'; +import { QDialog, QDialogSignals } from './QDialog'; import { QFont } from '../QtGui/QFont'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control font dialogs. * **This class is a JS wrapper around Qt's [QFontDialog class](https://doc.qt.io/qt-5/qfontdialog.html)** @@ -24,20 +26,18 @@ console.log(font); ``` */ -export class QFontDialog extends NodeDialog { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QFontDialog extends QDialog { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QFontDialog(parent.native); } else { native = new addon.QFontDialog(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } setCurrentFont(font: QFont): void { this.setProperty('currentFont', font.native); @@ -61,6 +61,7 @@ export class QFontDialog extends NodeDialog { return this.native.testOption(option); } } +wrapperCache.registerWrapper('QFontDialogWrap', QFontDialog); export enum FontDialogOption { NoButtons = 0x00000001, diff --git a/src/lib/QtWidgets/QFrame.ts b/src/lib/QtWidgets/QFrame.ts index c1cce1c8d..13d0f25c9 100644 --- a/src/lib/QtWidgets/QFrame.ts +++ b/src/lib/QtWidgets/QFrame.ts @@ -1,10 +1,38 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { checkIfNativeElement } from '../utils/helpers'; import { QRect } from '../QtCore/QRect'; +import { wrapperCache } from '../core/WrapperCache'; -export abstract class NodeFrame extends NodeWidget { +/** + > Create and control frame. + +* **This class is a JS wrapper around Qt's [QFrame class](https://doc.qt.io/qt-5/qframe.html)** + +The QFrame class is the base class of widgets that can have a frame. It can be used directly for creating simple placeholder frames without any contents. + +### Example + +```javascript +const { QFrame } = require("@nodegui/nodegui"); + +const frame = new QFrame(); +``` + */ +export class QFrame extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QFrame(parent.native); + } else { + native = new addon.QFrame(); + } + super(native); + } setFrameRect(r: QRect): void { this.setProperty('frameRect', r.native); } @@ -45,6 +73,7 @@ export abstract class NodeFrame extends NodeWidge return this.native.frameStyle(); } } +wrapperCache.registerWrapper('QFrameWrap', QFrame); export enum Shadow { Plain = 0x0010, @@ -63,37 +92,3 @@ export enum Shape { } export type QFrameSignals = QWidgetSignals; - -/** - > Create and control frame. - -* **This class is a JS wrapper around Qt's [QFrame class](https://doc.qt.io/qt-5/qframe.html)** - -The QFrame class is the base class of widgets that can have a frame. It can be used directly for creating simple placeholder frames without any contents. - -### Example - -```javascript -const { QFrame } = require("@nodegui/nodegui"); - -const frame = new QFrame(); -``` - */ -export class QFrame extends NodeFrame { - native: NativeElement; - constructor(arg?: NodeWidget | NativeElement) { - let native; - let parent; - if (checkIfNativeElement(arg)) { - native = arg as NativeElement; - } else if (arg as NodeWidget) { - parent = arg as NodeWidget; - native = new addon.QFrame(parent.native); - } else { - native = new addon.QFrame(); - } - super(native); - this.setNodeParent(parent); - this.native = native; - } -} diff --git a/src/lib/QtWidgets/QGraphicsBlurEffect.ts b/src/lib/QtWidgets/QGraphicsBlurEffect.ts index 2a5c18a24..e7b70e2d4 100644 --- a/src/lib/QtWidgets/QGraphicsBlurEffect.ts +++ b/src/lib/QtWidgets/QGraphicsBlurEffect.ts @@ -1,11 +1,12 @@ import addon from '../utils/addon'; import { NativeElement } from '../core/Component'; import { checkIfNativeElement } from '../utils/helpers'; -import { NodeObject } from '../QtCore/QObject'; +import { QObject } from '../QtCore/QObject'; import { QGraphicsEffect, QGraphicsEffectSignals } from './QGraphicsEffect'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > The QGraphicsBlurEffect class provides a blur effect. * **This class is a JS wrapper around Qt's [QGraphicsBlurEffect class](https://doc.qt.io/qt-5/qgraphicsblureffect.html)** @@ -22,23 +23,17 @@ blur.setBlurRadius(8); ``` */ export class QGraphicsBlurEffect extends QGraphicsEffect { - native: NativeElement; - constructor(); - constructor(native: NativeElement); - constructor(parent: NodeObject); - constructor(arg?: NodeObject | NativeElement) { + constructor(arg?: QObject | NativeElement) { let native: NativeElement; - if (arg) { - if (checkIfNativeElement(arg)) { - native = arg as NativeElement; - } else { - native = new addon.QGraphicsBlurEffect(arg.native); - } + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QObject; + native = new addon.QGraphicsBlurEffect(parent.native); } else { native = new addon.QGraphicsBlurEffect(); } super(native); - this.native = native; } setBlurHints(hints: BlurHint): void { this.setProperty('blurHints', hints); @@ -53,6 +48,7 @@ export class QGraphicsBlurEffect extends QGraphicsEffect The QGraphicsDropShadowEffect class provides a drop shadow effect. * **This class is a JS wrapper around Qt's [QGraphicsDropShadowEffect class](https://doc.qt.io/qt-5/qgraphicsdropshadoweffect.html)** -A drop shadow effect renders the source with a drop shadow. +A drop shadow effect renders the source with a drop shadow. ### Example @@ -23,23 +24,17 @@ shadow.setBlurRadius(8); ``` */ export class QGraphicsDropShadowEffect extends QGraphicsEffect { - native: NativeElement; - constructor(); - constructor(native: NativeElement); - constructor(parent: NodeObject); - constructor(arg?: NodeObject | NativeElement) { + constructor(arg?: QObject | NativeElement) { let native: NativeElement; - if (arg) { - if (checkIfNativeElement(arg)) { - native = arg as NativeElement; - } else { - native = new addon.QGraphicsDropShadowEffect(arg.native); - } + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QObject; + native = new addon.QGraphicsDropShadowEffect(parent.native); } else { native = new addon.QGraphicsDropShadowEffect(); } super(native); - this.native = native; } setBlurRadius(blurRadius: number): void { this.setProperty('blurRadius', blurRadius); @@ -66,6 +61,7 @@ export class QGraphicsDropShadowEffect extends QGraphicsEffect void; diff --git a/src/lib/QtWidgets/QGraphicsEffect.ts b/src/lib/QtWidgets/QGraphicsEffect.ts index 0703e839f..77258170f 100644 --- a/src/lib/QtWidgets/QGraphicsEffect.ts +++ b/src/lib/QtWidgets/QGraphicsEffect.ts @@ -1,7 +1,7 @@ -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; /** - + > This is the abstract base class of graphicseffect, providing their functionality. * **This class is a JS wrapper around Qt's [QGraphicsEffect class](https://doc.qt.io/qt-5/qgraphicseffect.html)** @@ -10,7 +10,7 @@ The QGraphicsEffect class is an abstract class and therefore, technically, no fu It is inherited by QGraphicsBlurEffect, QGraphicsColorizeEffect, QGraphicsDropShadowEffect, and QGraphicsOpacityEffect. */ -export abstract class QGraphicsEffect extends NodeObject { +export abstract class QGraphicsEffect extends QObject { setEnabled(enable: boolean): void { this.setProperty('enabled', enable); } diff --git a/src/lib/QtWidgets/QGridLayout.ts b/src/lib/QtWidgets/QGridLayout.ts index 8f477b738..ae054ffff 100644 --- a/src/lib/QtWidgets/QGridLayout.ts +++ b/src/lib/QtWidgets/QGridLayout.ts @@ -1,8 +1,10 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NodeLayout, QLayoutSignals } from './QLayout'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { QLayout, QLayoutSignals } from './QLayout'; import { NativeElement } from '../core/Component'; import { AlignmentFlag } from '../QtEnums'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -29,24 +31,21 @@ layout.addWidget(label2); ``` */ -export class QGridLayout extends NodeLayout { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { +export class QGridLayout extends QLayout { + constructor(arg?: QWidget | NativeElement) { let native: NativeElement; - if (parent) { + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QGridLayout(parent.native); } else { native = new addon.QGridLayout(); } super(native); - this.setNodeParent(parent); - this.native = native; } - addLayout( - layout: NodeLayout, + layout: QLayout, row: number, column: number, rowSpan = 1, @@ -56,13 +55,11 @@ export class QGridLayout extends NodeLayout { this.native.addLayout(layout.native, row, column, rowSpan, columnSpan, alignment); } - addWidget(widget: NodeWidget, row = 0, col = 0, rowSpan = 1, colSpan = 1, alignment: AlignmentFlag = 0): void { + addWidget(widget: QWidget, row = 0, col = 0, rowSpan = 1, colSpan = 1, alignment: AlignmentFlag = 0): void { this.native.addWidget(widget.native, row, col, rowSpan, colSpan, alignment); - this.nodeChildren.add(widget); } - removeWidget(widget: NodeWidget): void { + removeWidget(widget: QWidget): void { this.native.removeWidget(widget.native); - this.nodeChildren.delete(widget); } columnStretch(column: number): number { return this.native.columnStretch(column); @@ -107,5 +104,6 @@ export class QGridLayout extends NodeLayout { return this.native.rowCount(); } } +wrapperCache.registerWrapper('QGridLayoutWrap', QGridLayout); export type QGridLayoutSignals = QLayoutSignals; diff --git a/src/lib/QtWidgets/QGroupBox.ts b/src/lib/QtWidgets/QGroupBox.ts index 0db6a035e..b6bb1caa1 100644 --- a/src/lib/QtWidgets/QGroupBox.ts +++ b/src/lib/QtWidgets/QGroupBox.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { AlignmentFlag } from '../QtEnums/AlignmentFlag'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control a group of checkboxes including a title. * **This class is a JS wrapper around Qt's [QGroupBox class](https://doc.qt.io/qt-5/qgroupbox.html)** @@ -39,21 +41,20 @@ win.show(); (global as any).win = win; ``` */ -export class QGroupBox extends NodeWidget { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QGroupBox extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QGroupBox(parent.native); } else { native = new addon.QGroupBox(); } super(native); - this.native = native; - this.setNodeParent(parent); } + setAlignment(alignment: AlignmentFlag): void { this.setProperty('alignment', alignment); } @@ -85,6 +86,7 @@ export class QGroupBox extends NodeWidget { return this.property('title').toString(); } } +wrapperCache.registerWrapper('QGroupBoxWrap', QGroupBox); export interface QGroupBoxSignals extends QWidgetSignals { clicked: (checked: boolean) => void; diff --git a/src/lib/QtWidgets/QHeaderView.ts b/src/lib/QtWidgets/QHeaderView.ts index 48a6c99d3..9c646b023 100644 --- a/src/lib/QtWidgets/QHeaderView.ts +++ b/src/lib/QtWidgets/QHeaderView.ts @@ -1,8 +1,9 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget } from './QWidget'; import { NativeElement } from '../core/Component'; import { QAbstractItemView, QAbstractItemViewSignals } from './QAbstractItemView'; import { AlignmentFlag, checkIfNativeElement, Orientation, QPoint, SortOrder } from '../..'; +import { wrapperCache } from '../core/WrapperCache'; /** @@ -11,7 +12,21 @@ import { AlignmentFlag, checkIfNativeElement, Orientation, QPoint, SortOrder } f * **This class is a JS wrapper around Qt's [QHeaderView class](https://doc.qt.io/qt-5/qheaderview.html)** */ -export abstract class NodeHeaderView extends QAbstractItemView { +export class QHeaderView extends QAbstractItemView { + constructor(orientationOrNative: Orientation | NativeElement, parent: QWidget | null = null) { + let native: NativeElement; + if (checkIfNativeElement(orientationOrNative)) { + native = orientationOrNative as NativeElement; + } else { + if (parent != null) { + native = new addon.QHeaderView(orientationOrNative, parent.native); + } else { + native = new addon.QHeaderView(orientationOrNative); + } + } + super(native); + } + // *** Public Function *** cascadingSectionResizes(): boolean { return this.native.cascadingSectionResizes(); @@ -203,25 +218,7 @@ export abstract class NodeHeaderView extends this.native.setOffsetToSectionPosition(visualSectionNumber); } } - -export class QHeaderView extends NodeHeaderView { - native: NativeElement; - constructor(orientationOrNative: Orientation | NativeElement, parent: NodeWidget | null = null) { - let native; - if (checkIfNativeElement(orientationOrNative)) { - native = orientationOrNative as NativeElement; - } else { - if (parent != null) { - native = new addon.QHeaderView(orientationOrNative, parent.native); - } else { - native = new addon.QHeaderView(orientationOrNative); - } - } - super(native); - this.native = native; - parent && this.setNodeParent(parent); - } -} +wrapperCache.registerWrapper('QHeaderViewWrap', QHeaderView); export enum QHeaderViewResizeMode { Interactive = 0, diff --git a/src/lib/QtWidgets/QInputDialog.ts b/src/lib/QtWidgets/QInputDialog.ts index 43e623818..962acc895 100644 --- a/src/lib/QtWidgets/QInputDialog.ts +++ b/src/lib/QtWidgets/QInputDialog.ts @@ -1,11 +1,13 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement } from '../core/Component'; -import { NodeDialog, QDialogSignals } from './QDialog'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { QDialog, QDialogSignals } from './QDialog'; import { EchoMode } from './QLineEdit'; +import { wrapperCache } from '../core/WrapperCache'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control input modal dialogs. * **This class is a JS wrapper around Qt's [QInputDialog class](https://doc.qt.io/qt-5/qinputdialog.html)** @@ -22,20 +24,18 @@ dialog.exec(); ``` */ -export class QInputDialog extends NodeDialog { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QInputDialog extends QDialog { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QInputDialog(parent.native); } else { native = new addon.QInputDialog(); } super(native); - this.native = native; - this.setNodeParent(parent); } setCancelButtonText(text: string): void { this.native.setCancelButtonText(text); @@ -140,6 +140,7 @@ export class QInputDialog extends NodeDialog { this.native.setTextValue(value); } } +wrapperCache.registerWrapper('QInputDialogWrap', QInputDialog); export interface QInputDialogSignals extends QDialogSignals { doubleValueChanged: (value: number) => void; diff --git a/src/lib/QtWidgets/QLCDNumber.ts b/src/lib/QtWidgets/QLCDNumber.ts index 9e875dbb1..0da1494b4 100644 --- a/src/lib/QtWidgets/QLCDNumber.ts +++ b/src/lib/QtWidgets/QLCDNumber.ts @@ -1,9 +1,11 @@ -import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; +import { wrapperCache } from '../core/WrapperCache'; +import addon from '../utils/addon'; +import { checkIfNativeElement } from '../utils/helpers'; +import { QWidget, QWidgetSignals } from './QWidget'; /** - + > Create and control number. * **This class is a JS wrapper around Qt's [QLCDNumber class](https://doc.qt.io/qt-5/qlcdnumber.html)** @@ -20,20 +22,18 @@ const lcd = new QLCDNumber(); ``` */ -export class QLCDNumber extends NodeWidget { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QLCDNumber extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QLCDNumber(parent.native); } else { native = new addon.QLCDNumber(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } setDigitCount(numDigits: number): void { this.setProperty('digitCount', numDigits); @@ -84,6 +84,7 @@ export class QLCDNumber extends NodeWidget { this.native.setOctMode(); } } +wrapperCache.registerWrapper('QLCDNumberWrap', QLCDNumber); export enum Mode { Hex, diff --git a/src/lib/QtWidgets/QLabel.ts b/src/lib/QtWidgets/QLabel.ts index ed65bea94..354139a7d 100644 --- a/src/lib/QtWidgets/QLabel.ts +++ b/src/lib/QtWidgets/QLabel.ts @@ -1,16 +1,18 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NodeFrame, QFrameSignals } from './QFrame'; -import { NativeElement } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { QFrame, QFrameSignals } from './QFrame'; import { QPixmap } from '../QtGui/QPixmap'; import { QMovie } from '../QtGui/QMovie'; import { AlignmentFlag } from '../QtEnums/AlignmentFlag'; import { TextFormat } from '../QtEnums/TextFormat'; import { TextInteractionFlag } from '../QtEnums'; import { QPicture } from '../QtGui/QPicture'; +import { wrapperCache } from '../core/WrapperCache'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control text. * **This class is a JS wrapper around Qt's [QLabel class](https://doc.qt.io/qt-5/qlabel.html)** @@ -27,24 +29,23 @@ label.setText("Hello"); ``` */ -export class QLabel extends NodeFrame { - native: NativeElement; +export class QLabel extends QFrame { private _picture?: QPicture; private _pixmap?: QPixmap; private _movie?: QMovie; - private _buddy?: NodeWidget | null; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { + + // TODO + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QLabel(parent.native); } else { native = new addon.QLabel(); } super(native); - this.native = native; - this.setNodeParent(parent); } setAlignment(alignment: AlignmentFlag): void { this.setProperty('alignment', alignment); @@ -112,15 +113,11 @@ export class QLabel extends NodeFrame { selectionStart(): number { return this.native.selectionStart(); } - setBuddy(buddy: NodeWidget): void { + setBuddy(buddy: QWidget): void { this.native.setBuddy(buddy.native); - this._buddy = buddy; } - buddy(): NodeWidget | null { - if (this._buddy) { - return this._buddy; - } - return null; + buddy(): QWidget | null { + return wrapperCache.getWrapper(this.native.budd()) as QWidget; } setMovie(movie: QMovie): void { this.native.setMovie(movie.native); @@ -153,6 +150,7 @@ export class QLabel extends NodeFrame { this.native.clear(); } } +wrapperCache.registerWrapper('QLabelWrap', QLabel); export interface QLabelSignals extends QFrameSignals { linkActivated: (link: string) => void; diff --git a/src/lib/QtWidgets/QLayout.ts b/src/lib/QtWidgets/QLayout.ts index 018accb2e..e0c2864b3 100644 --- a/src/lib/QtWidgets/QLayout.ts +++ b/src/lib/QtWidgets/QLayout.ts @@ -1,30 +1,29 @@ -import { NodeWidget } from './QWidget'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QWidget } from './QWidget'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; // All Layouts should extend this abstract class. /** - + > Abstract class to add functionalities common to all Layout. **This class implements all methods, properties of Qt's [QLayout class](https://doc.qt.io/qt-5/qlayout.html) so that it can be inherited by all layouts** -`NodeLayout` is an abstract class and hence no instances of the same should be created. It exists so that we can add similar functionalities to all layout's easily. Additionally it helps in typechecking process. +`QLayout` is an abstract class and hence no instances of the same should be created. ### Example ```javascript const { - NodeLayout, - NodeWidget, + QLayout, FlexLayout, GridLayout, QPushButton, QWidget } = require("@nodegui/nodegui"); -// addChildToLayout can accept any layout since it expects NodeLayout -const addChildToLayout = (layout: NodeLayout, widget: NodeWidget) => { +// addChildToLayout can accept any layout since it expects QLayout +const addChildToLayout = (layout: QLayout, widget: QWidget) => { layout.addWidget(widget); }; @@ -32,10 +31,10 @@ addChildToLayout(new FlexLayout(), new QPushButton()); addChildToLayout(new GridLayout(), new QWidget()); ``` */ -export abstract class NodeLayout extends NodeObject { +export abstract class QLayout extends QObject { type = 'layout'; - abstract addWidget(childWidget: NodeWidget, ...args: any[]): void; - abstract removeWidget(childWidget: NodeWidget): void; + abstract addWidget(childWidget: QWidget, ...args: any[]): void; + abstract removeWidget(childWidget: QWidget): void; setSizeConstraint(constraint: SizeConstraint): void { this.setProperty('sizeConstraint', constraint); } @@ -68,10 +67,6 @@ export abstract class NodeLayout extends NodeObj } } -// export class QLayout extends NodeLayout { //Dont need QLayout for now -// native: any; -// } - export enum SizeConstraint { SetDefaultConstraint = 0, SetNoConstraint = 1, diff --git a/src/lib/QtWidgets/QLineEdit.ts b/src/lib/QtWidgets/QLineEdit.ts index 00e89d3c8..5a11e97d1 100644 --- a/src/lib/QtWidgets/QLineEdit.ts +++ b/src/lib/QtWidgets/QLineEdit.ts @@ -1,9 +1,11 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; -import { NativeElement } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; import { AlignmentFlag } from '../QtEnums/AlignmentFlag'; import { CursorMoveStyle } from '../QtEnums/CursorMoveStyle'; import { QPoint } from '../QtCore/QPoint'; +import { wrapperCache } from '../core/WrapperCache'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -22,20 +24,18 @@ const lineEdit = new QLineEdit(); ``` */ -export class QLineEdit extends NodeWidget { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QLineEdit extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QLineEdit(parent.native); } else { native = new addon.QLineEdit(); } super(native); - this.native = native; - this.setNodeParent(parent); } // TODO: void addAction(QAction *action, QLineEdit::ActionPosition position) // TODO: QAction * addAction(const QIcon &icon, QLineEdit::ActionPosition position) @@ -214,6 +214,7 @@ export class QLineEdit extends NodeWidget { this.native.undo(); } } +wrapperCache.registerWrapper('QLineEditWrap', QLineEdit); export enum EchoMode { Normal, diff --git a/src/lib/QtWidgets/QListView.ts b/src/lib/QtWidgets/QListView.ts index c1a1bcb70..7e7ccb697 100644 --- a/src/lib/QtWidgets/QListView.ts +++ b/src/lib/QtWidgets/QListView.ts @@ -1,12 +1,14 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QAbstractItemView, QAbstractItemViewSignals } from './QAbstractItemView'; import { QSize } from '../QtCore/QSize'; import { AlignmentFlag } from '../..'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > The QListView class provides a list or icon view onto a model. * **This class is a JS wrapper around Qt's [QListView class](https://doc.qt.io/qt-5/qlistview.html)** @@ -20,7 +22,19 @@ const listview = new QListView(); ``` */ -export abstract class NodeListView extends QAbstractItemView { +export class QListView extends QAbstractItemView { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QListView(parent.native); + } else { + native = new addon.QListView(); + } + super(native); + } setBatchSize(batchSize: number): void { this.setProperty('batchSize', batchSize); } @@ -116,6 +130,7 @@ export abstract class NodeListView extends QAb return this.native.isRowHidden(row); } } +wrapperCache.registerWrapper('QListViewWrap', QListView); export enum Flow { LeftToRight, @@ -143,21 +158,4 @@ export enum ListViewMode { IconMode, } -export class QListView extends NodeListView { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { - native = new addon.QListView(parent.native); - } else { - native = new addon.QListView(); - } - super(native); - this.native = native; - parent && this.setNodeParent(parent); - } -} - export type QListViewSignals = QAbstractItemViewSignals; diff --git a/src/lib/QtWidgets/QListWidget.ts b/src/lib/QtWidgets/QListWidget.ts index d4fb1ca36..b2e1619bc 100644 --- a/src/lib/QtWidgets/QListWidget.ts +++ b/src/lib/QtWidgets/QListWidget.ts @@ -1,11 +1,13 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement, Component } from '../core/Component'; import { QListWidgetItem } from './QListWidgetItem'; -import { NodeListView, QListViewSignals } from './QListView'; +import { QListView, QListViewSignals } from './QListView'; import { QRect } from '../QtCore/QRect'; import { SortOrder, ScrollHint, MatchFlag } from '../QtEnums'; import { QModelIndex } from '../QtCore/QModelIndex'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -32,21 +34,20 @@ for (let i = 0; i < 30; i++) { } ``` */ -export class QListWidget extends NodeListView { - native: NativeElement; - items: Set; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QListWidget extends QListView { + items: Set; + + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QListWidget(parent.native); } else { native = new addon.QListWidget(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); this.items = new Set(); } count(): number { @@ -135,7 +136,7 @@ export class QListWidget extends NodeListView { setCurrentItem(item: QListWidgetItem): void { this.native.setCurrentItem(item.native); } - setItemWidget(item: QListWidgetItem, widget: NodeWidget): void { + setItemWidget(item: QListWidgetItem, widget: QWidget): void { this.native.setItemWidget(item.native, widget.native); } sortItems(order = SortOrder.AscendingOrder): void { @@ -154,6 +155,7 @@ export class QListWidget extends NodeListView { this.native.scrollToItem(item.native, hint); } } +wrapperCache.registerWrapper('QListWidgetWrap', QListWidget); export interface QListWidgetSignals extends QListViewSignals { currentItemChanged: (current: QListWidgetItem, previous: QListWidgetItem) => void; diff --git a/src/lib/QtWidgets/QListWidgetItem.ts b/src/lib/QtWidgets/QListWidgetItem.ts index b4be0fc2c..b9997b86d 100644 --- a/src/lib/QtWidgets/QListWidgetItem.ts +++ b/src/lib/QtWidgets/QListWidgetItem.ts @@ -10,7 +10,7 @@ import { CheckState } from '../QtEnums'; import { ItemFlag } from '../QtEnums/ItemFlag'; /** - + > Creates an item for QListWidget. * **This class is a JS wrapper around Qt's [QListWidgetItem class](https://doc.qt.io/qt-5/qlistwidgetitem.html)** @@ -35,11 +35,6 @@ for (let i = 0; i < 30; i++) { ``` */ export class QListWidgetItem extends Component { - native: NativeElement; - constructor(); - constructor(other: QListWidgetItem); - constructor(native: NativeElement); - constructor(text: string); constructor(arg?: QListWidgetItem | NativeElement | string) { let native; if (typeof arg === 'string') { @@ -49,8 +44,7 @@ export class QListWidgetItem extends Component { } else { native = new addon.QListWidgetItem(); } - super(); - this.native = native; + super(native); } setBackground(brush: QBrush): void { this.native.setBackground(brush.native); diff --git a/src/lib/QtWidgets/QMainWindow.ts b/src/lib/QtWidgets/QMainWindow.ts index fbbd974eb..54a6b2ef5 100644 --- a/src/lib/QtWidgets/QMainWindow.ts +++ b/src/lib/QtWidgets/QMainWindow.ts @@ -1,12 +1,14 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; -import { NativeElement } from '../core/Component'; -import { NodeLayout } from './QLayout'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { QLayout } from './QLayout'; import { QMenuBar } from './QMenuBar'; import { QStatusBar } from './QStatusBar'; +import { NativeElement } from '../core/Component'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control windows. * **This class is a JS wrapper around Qt's [QMainWindow class](https://doc.qt.io/qt-5/qmainwindow.html)** @@ -31,63 +33,61 @@ global.win = win; // prevent's gc of win QMainWindow needs to have a central widget set before other widgets can be added as a children/nested children. Once a central widget is set you can add children/layout to the central widget. */ -export class QMainWindow extends NodeWidget { - native: NativeElement; - public centralWidget?: NodeWidget | null; - private _menuBar?: QMenuBar; - private _statusBar?: QStatusBar | null; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QMainWindow extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QMainWindow(parent.native); } else { native = new addon.QMainWindow(); } super(native); - this.native = native; - this.setNodeParent(parent); - - this.setLayout = (parentLayout: NodeLayout): void => { - if (this.centralWidget) { - this.centralWidget.setLayout(parentLayout); - } else { - this.native.setLayout(parentLayout.native); - super.layout = parentLayout; - } - }; } - setCentralWidget(widget: NodeWidget): void { + setLayout(parentLayout: QLayout): void { + const centralWidget = this.centralWidget(); + if (centralWidget) { + centralWidget.setLayout(parentLayout); + } else { + super.setLayout(parentLayout); + } + } + setCentralWidget(widget: QWidget): void { this.native.setCentralWidget(widget.native); - this.centralWidget = widget; - this.centralWidget.setFlexNodeSizeControlled(true); + const centralWidget = this.centralWidget(); + if (centralWidget) { + centralWidget.setFlexNodeSizeControlled(true); + } } - takeCentralWidget(): NodeWidget | null { - const centralWidget = this.centralWidget; + centralWidget(): QWidget { + return wrapperCache.getWrapper(this.native.centralWidget()) as QWidget; + } + takeCentralWidget(): QWidget | null { + const centralWidget = this.centralWidget(); this.centralWidget = null; if (centralWidget) { centralWidget.setFlexNodeSizeControlled(false); - this.native.takeCentralWidget(); - return centralWidget; + return wrapperCache.getWrapper(this.native.takeCentralWidget()) as QWidget; } return null; } setMenuBar(menuBar: QMenuBar): void { this.native.setMenuBar(menuBar.native); - this._menuBar = menuBar; } menuBar(): QMenuBar | undefined { - return this._menuBar; + return wrapperCache.getWrapper(this.native.menuBar()) as QMenuBar; } - setMenuWidget(menuWidget: NodeWidget): void { + setMenuWidget(menuWidget: QWidget): void { this.native.setMenuWidget(menuWidget.native); } - get layout(): NodeLayout | undefined { - if (this.centralWidget) { - return this.centralWidget.layout; + layout(): QLayout | undefined { + const centralWidget = this.centralWidget(); + if (centralWidget) { + return centralWidget.layout(); } - return super.layout; + return super.layout(); } center(): void { this.native.center(); @@ -99,24 +99,20 @@ export class QMainWindow extends NodeWidget { * @param statusBar The status bar. */ setStatusBar(statusBar: QStatusBar): void { - this.native.setStatusBar(statusBar.native); - this._statusBar = statusBar; - } - - /** - * Removes the status bar from the main window. - */ - removeStatusBar(): void { - this.native.setStatusBar(null); - this._statusBar = null; + if (statusBar != null) { + this.native.setStatusBar(statusBar.native); + } else { + this.native.setStatusBar(null); + } } /** * Returns the status bar for the main window. */ statusBar(): QStatusBar { - return this.native.statusBar(); + return wrapperCache.getWrapper(this.native.statusBar()) as QStatusBar; } } +wrapperCache.registerWrapper('QMainWindowWrap', QMainWindow); export type QMainWindowSignals = QWidgetSignals; diff --git a/src/lib/QtWidgets/QMenu.ts b/src/lib/QtWidgets/QMenu.ts index 14346496b..ca8f639b6 100644 --- a/src/lib/QtWidgets/QMenu.ts +++ b/src/lib/QtWidgets/QMenu.ts @@ -1,8 +1,10 @@ import { NativeElement } from '../core/Component'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import addon from '../utils/addon'; import { QAction } from './QAction'; import { QPoint } from '../QtCore/QPoint'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -18,20 +20,18 @@ const { QMenu } = require("@nodegui/nodegui"); const menu = new QMenu(); ``` */ -export class QMenu extends NodeWidget { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QMenu extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QMenu(parent.native); } else { native = new addon.QMenu(); } super(native); - this.native = native; - this.setNodeParent(parent); } clear(): void { this.native.clear(); @@ -55,6 +55,7 @@ export class QMenu extends NodeWidget { this.native.popup(point.native, action?.native); } } +wrapperCache.registerWrapper('QMenuWrap', QMenu); export interface QMenuSignals extends QWidgetSignals { triggered: (action: NativeElement) => void; diff --git a/src/lib/QtWidgets/QMenuBar.ts b/src/lib/QtWidgets/QMenuBar.ts index 15f065865..8491ce771 100644 --- a/src/lib/QtWidgets/QMenuBar.ts +++ b/src/lib/QtWidgets/QMenuBar.ts @@ -1,12 +1,13 @@ import { QMenu } from './QMenu'; import { NativeElement } from '../core/Component'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import addon from '../utils/addon'; import { checkIfNativeElement } from '../utils/helpers'; import { QAction } from './QAction'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > The QMenuBar class provides a menu widget for use in menu bars, context menus, and other popup menus. * **This class is a JS wrapper around Qt's [QMenuBar class](https://doc.qt.io/qt-5/qmenu.html)** @@ -23,38 +24,28 @@ win.show(); global.win = win; ``` */ -export class QMenuBar extends NodeWidget { - native: NativeElement; - _menus: Set; - constructor(); - constructor(parent: NodeWidget); - constructor(native: NativeElement); - constructor(arg?: NodeWidget | NativeElement) { - let native; - let parent; +export class QMenuBar extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; if (checkIfNativeElement(arg)) { native = arg as NativeElement; - } else if (typeof arg === 'object') { - native = new addon.QMenuBar(arg.native); - parent = arg as NodeWidget; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QMenuBar(parent.native); } else { native = new addon.QMenuBar(); } super(native); - this.native = native; - this._menus = new Set(); - this.setNodeParent(parent); } + addMenu(menu: QMenu | string): QMenu { if (typeof menu === 'string') { const qmenu = new QMenu(); qmenu.setTitle(menu); this.native.addMenu(qmenu.native); - this._menus.add(qmenu); return qmenu; } this.native.addMenu(menu.native); - this._menus.add(menu); return menu; } addSeparator(): QAction { @@ -64,5 +55,6 @@ export class QMenuBar extends NodeWidget { this.native.setNativeMenuBar(nativeMenuBar); } } +wrapperCache.registerWrapper('QMenuBarWrap', QMenuBar); export type QMenuBarSignals = QWidgetSignals; diff --git a/src/lib/QtWidgets/QMessageBox.ts b/src/lib/QtWidgets/QMessageBox.ts index 3a2817c94..8a69ef93a 100644 --- a/src/lib/QtWidgets/QMessageBox.ts +++ b/src/lib/QtWidgets/QMessageBox.ts @@ -1,9 +1,11 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement, NativeRawPointer } from '../core/Component'; -import { NodeDialog, QDialogSignals } from './QDialog'; +import { QDialog, QDialogSignals } from './QDialog'; import { QAbstractButton, QAbstractButtonSignals } from './QAbstractButton'; import { QPushButton } from './QPushButton'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; export enum ButtonRole { InvalidRole, @@ -19,7 +21,7 @@ export enum ButtonRole { } /** - + > Create and control classic modal dialogs. * **This class is a JS wrapper around Qt's [QMessageBox class](https://doc.qt.io/qt-5/qmessagebox.html)** @@ -39,20 +41,18 @@ messageBox.exec(); ``` */ -export class QMessageBox extends NodeDialog { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QMessageBox extends QDialog { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QMessageBox(parent.native); } else { native = new addon.QMessageBox(); } super(native); - this.native = native; - this.setNodeParent(parent); } accept(): void { this.native.accept(); @@ -82,22 +82,21 @@ export class QMessageBox extends NodeDialog { setDefaultButton(button: QPushButton): void { this.native.setDefaultButton(button); - this.nodeChildren.add(button); } addButton(button: QAbstractButton, role: ButtonRole = ButtonRole.NoRole): void { this.native.addButton(button.native, role); - this.nodeChildren.add(button); } - static about(parent: NodeWidget, title: string, text: string): void { + static about(parent: QWidget, title: string, text: string): void { addon.QMessageBox.about(parent.native, title, text); } - static aboutQt(parent: NodeWidget, title: string): void { + static aboutQt(parent: QWidget, title: string): void { addon.QMessageBox.aboutQt(parent.native, title); } } +wrapperCache.registerWrapper('QMessageBoxWrap', QMessageBox); export interface QMessageBoxSignals extends QDialogSignals { buttonClicked: (buttonRawPointer: NativeRawPointer<'QAbstractButton*'>) => void; diff --git a/src/lib/QtWidgets/QPainter.ts b/src/lib/QtWidgets/QPainter.ts index 481d250ac..97673b2f6 100644 --- a/src/lib/QtWidgets/QPainter.ts +++ b/src/lib/QtWidgets/QPainter.ts @@ -46,18 +46,14 @@ win.show(); https://github.com/nodegui/examples/blob/master/nodegui/custom-native-widget-qpainter */ export class QPainter extends Component { - native: NativeElement; - constructor(); - constructor(device: Component); constructor(device?: Component) { - let native; + let native: NativeElement; if (device) { native = new addon.QPainter(device.native); } else { native = new addon.QPainter(); } - super(); - this.native = native; + super(native); } // *** Public Functions *** diff --git a/src/lib/QtWidgets/QPainterPath.ts b/src/lib/QtWidgets/QPainterPath.ts index 358bd22cc..8b13bbd93 100644 --- a/src/lib/QtWidgets/QPainterPath.ts +++ b/src/lib/QtWidgets/QPainterPath.ts @@ -1,5 +1,5 @@ import addon from '../utils/addon'; -import { Component, NativeElement } from '../core/Component'; +import { Component } from '../core/Component'; import { SizeMode, FillRule } from '../QtEnums'; import { QFont } from '../QtGui/QFont'; @@ -12,11 +12,8 @@ import { QFont } from '../QtGui/QFont'; */ export class QPainterPath extends Component { - native: NativeElement; constructor() { - super(); - const native = new addon.QPainterPath(); - this.native = native; + super(new addon.QPainterPath()); } moveTo(x: number, y: number): void { diff --git a/src/lib/QtWidgets/QPlainTextEdit.ts b/src/lib/QtWidgets/QPlainTextEdit.ts index 2db011c96..191c568db 100644 --- a/src/lib/QtWidgets/QPlainTextEdit.ts +++ b/src/lib/QtWidgets/QPlainTextEdit.ts @@ -1,8 +1,10 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QAbstractScrollArea, QAbstractScrollAreaSignals } from './QAbstractScrollArea'; import { QTextOptionWrapMode } from '../QtGui/QTextOption'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; export interface QPlainTextEditSignals extends QAbstractScrollAreaSignals { textChanged: () => void; @@ -16,7 +18,7 @@ export interface QPlainTextEditSignals extends QAbstractScrollAreaSignals { } /** - + > Used to edit and display plain text. * **This class is a JS wrapper around Qt's [QPlainTextEdit class](https://doc.qt.io/qt-5/qplaintextedit.html)** @@ -32,20 +34,17 @@ const plainTextEdit = new QPlainTextEdit(); ``` */ export class QPlainTextEdit extends QAbstractScrollArea { - native: NativeElement; - placeholderText?: string; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QPlainTextEdit(parent.native); } else { native = new addon.QPlainTextEdit(); } super(native); - this.native = native; - this.setNodeParent(parent); } setPlainText(text: string | number): void { // react:✓ @@ -53,7 +52,6 @@ export class QPlainTextEdit extends QAbstractScrollArea { } setPlaceholderText(text: string): void { // react:✓, //TODO:getter - this.placeholderText = text; this.native.setPlaceholderText(text); } toPlainText(): string { @@ -84,6 +82,7 @@ export class QPlainTextEdit extends QAbstractScrollArea { this.native.insertPlainText(`${text}`); } } +wrapperCache.registerWrapper('QPlainTextEditWrap', QPlainTextEdit); export enum LineWrapMode { NoWrap, diff --git a/src/lib/QtWidgets/QProgressBar.ts b/src/lib/QtWidgets/QProgressBar.ts index bc82fc840..c893ce35f 100644 --- a/src/lib/QtWidgets/QProgressBar.ts +++ b/src/lib/QtWidgets/QProgressBar.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { Orientation, AlignmentFlag } from '../QtEnums'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control progress bar widgets. * **This class is a JS wrapper around Qt's [QProgressBar class](https://doc.qt.io/qt-5/qprogressbar.html)** @@ -19,20 +21,18 @@ const { QProgressBar } = require("@nodegui/nodegui"); const progressBar = new QProgressBar(); ``` */ -export class QProgressBar extends NodeWidget { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QProgressBar extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QProgressBar(parent.native); } else { native = new addon.QProgressBar(); } super(native); - this.native = native; - this.setNodeParent(parent); } setAlignment(alignment: AlignmentFlag): void { this.setProperty('alignment', alignment); @@ -101,6 +101,7 @@ export class QProgressBar extends NodeWidget { this.native.setRange(minimum, maximum); } } +wrapperCache.registerWrapper('QProgressBarWrap', QProgressBar); export enum QProgressBarDirection { TopToBottom, diff --git a/src/lib/QtWidgets/QProgressDialog.ts b/src/lib/QtWidgets/QProgressDialog.ts index fbd86fc6c..baed5ef64 100644 --- a/src/lib/QtWidgets/QProgressDialog.ts +++ b/src/lib/QtWidgets/QProgressDialog.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { QDialog, QDialogSignals } from './QDialog'; +import { wrapperCache } from '../core/WrapperCache'; import { NativeElement } from '../core/Component'; -import { NodeDialog, QDialogSignals } from './QDialog'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control progress dialogs. * **This class is a JS wrapper around Qt's [QProgressDialog class](https://doc.qt.io/qt-5/qprogressdialog.html)** @@ -20,20 +22,18 @@ const progressDialog = new QProgressDialog(); ``` */ -export class QProgressDialog extends NodeDialog { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QProgressDialog extends QDialog { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QProgressDialog(parent.native); } else { native = new addon.QProgressDialog(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } setAutoClose(close: boolean): void { this.setProperty('autoClose', close); @@ -93,6 +93,7 @@ export class QProgressDialog extends NodeDialog { this.native.setRange(minimum, maximum); } } +wrapperCache.registerWrapper('QProgressDialogWrap', QProgressDialog); export interface QProgressDialogSignals extends QDialogSignals { canceled: () => void; diff --git a/src/lib/QtWidgets/QPushButton.ts b/src/lib/QtWidgets/QPushButton.ts index 67131110f..35413003b 100644 --- a/src/lib/QtWidgets/QPushButton.ts +++ b/src/lib/QtWidgets/QPushButton.ts @@ -1,12 +1,13 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement, NativeRawPointer, Component } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { NativeElement } from '../core/Component'; import { QAbstractButton, QAbstractButtonSignals } from './QAbstractButton'; -import { checkIfNativeElement, checkIfNapiExternal } from '../utils/helpers'; +import { checkIfNativeElement } from '../utils/helpers'; import { QMenu } from './QMenu'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Create and control buttons. * **This class is a JS wrapper around Qt's [QPushButton class](https://doc.qt.io/qt-5/qpushbutton.html)** @@ -23,29 +24,17 @@ button.setText("Hello"); ``` */ export class QPushButton extends QAbstractButton { - native: NativeElement; - private _menu?: QMenu | null; - constructor(); - constructor(parent: NodeWidget); - constructor(native: NativeElement); - constructor(rawPointer: NativeRawPointer, disableNativeDeletion?: boolean); - constructor(arg?: NodeWidget | NativeRawPointer | NativeElement, disableNativeDeletion = true) { - let native; - let parent: Component | undefined; + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; if (checkIfNativeElement(arg)) { native = arg as NativeElement; - } else if (checkIfNapiExternal(arg)) { - native = new addon.QPushButton(arg, disableNativeDeletion); - } else if (arg) { - const parentWidget = arg as NodeWidget; - native = new addon.QPushButton(parentWidget.native); - parent = parentWidget; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QPushButton(parent.native); } else { native = new addon.QPushButton(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } setAutoDefault(auto: boolean): void { this.setProperty('autoDefault', auto); @@ -66,18 +55,15 @@ export class QPushButton extends QAbstractButton { return this.property('flat').toBool(); } setMenu(menu: QMenu): void { - this._menu = menu; this.native.setMenu(menu.native); } menu(): QMenu | null { - if (this._menu) { - return this._menu; - } - return null; + return wrapperCache.getWrapper(this.native.menu()) as QMenu; } showMenu(): void { this.native.showMenu(); } } +wrapperCache.registerWrapper('QPushButtonWrap', QPushButton); export type QPushButtonSignals = QAbstractButtonSignals; diff --git a/src/lib/QtWidgets/QRadioButton.ts b/src/lib/QtWidgets/QRadioButton.ts index 1f7f332c0..63c352954 100644 --- a/src/lib/QtWidgets/QRadioButton.ts +++ b/src/lib/QtWidgets/QRadioButton.ts @@ -1,11 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement, NativeRawPointer, Component } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { NativeElement } from '../core/Component'; import { QAbstractButton, QAbstractButtonSignals } from './QAbstractButton'; -import { checkIfNativeElement, checkIfNapiExternal } from '../utils/helpers'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Create and control radio button. * **This class is a JS wrapper around Qt's [QRadioButton class](https://doc.qt.io/qt-5/qradiobutton.html)** @@ -23,28 +24,19 @@ radioButton.setText("Hello"); */ export class QRadioButton extends QAbstractButton { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(rawPointer: NativeRawPointer, disableNativeDeletion?: boolean); - constructor(arg?: NodeWidget | NativeRawPointer | NativeElement, disableNativeDeletion = true) { - let native; - let parent: Component | undefined; + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; if (checkIfNativeElement(arg)) { native = arg as NativeElement; - } else if (checkIfNapiExternal(arg)) { - native = new addon.QRadioButton(arg, disableNativeDeletion); - } else if (arg) { - const parentWidget = arg as NodeWidget; - native = new addon.QRadioButton(parentWidget.native); - parent = parentWidget; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QRadioButton(parent.native); } else { native = new addon.QRadioButton(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } } +wrapperCache.registerWrapper('QRadioButtonWrap', QRadioButton); export type QRadioButtonSignals = QAbstractButtonSignals; diff --git a/src/lib/QtWidgets/QScrollArea.ts b/src/lib/QtWidgets/QScrollArea.ts index bdd4fbb5c..f1153f22a 100644 --- a/src/lib/QtWidgets/QScrollArea.ts +++ b/src/lib/QtWidgets/QScrollArea.ts @@ -1,9 +1,11 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QAbstractScrollArea, QAbstractScrollAreaSignals } from './QAbstractScrollArea'; import { AlignmentFlag } from '../QtEnums'; import { Margins } from '../utils/Margins'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -29,20 +31,17 @@ scrollArea.setWidget(imageLabel); ``` */ export class QScrollArea extends QAbstractScrollArea { - native: NativeElement; - contentWidget?: NodeWidget | null; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QScrollArea(parent.native); } else { native = new addon.QScrollArea(); } super(native); - this.native = native; - this.setNodeParent(parent); } setAlignment(alignment: AlignmentFlag): void { this.setProperty('alignment', alignment); @@ -59,28 +58,17 @@ export class QScrollArea extends QAbstractScrollArea { ensureVisible(x: number, y: number, xmargin = 50, ymargin = 50): void { this.native.ensureVisible(x, y, xmargin, ymargin); } - ensureWidgetVisible(childWidget: NodeWidget, xmargin = 50, ymargin = 50): void { + ensureWidgetVisible(childWidget: QWidget, xmargin = 50, ymargin = 50): void { this.native.ensureWidgetVisible(childWidget.native, xmargin, ymargin); } - setWidget(widget: NodeWidget): void { - this.contentWidget = widget; + setWidget(widget: QWidget): void { this.native.setWidget(widget.native); } - widget(): NodeWidget | null { - if (this.contentWidget) { - return this.contentWidget; - } - return null; + widget(): QWidget | null { + return wrapperCache.getWrapper(this.native.widget()) as QWidget; } - takeWidget(): NodeWidget | null { - // react:✓ - const contentWidget = this.contentWidget; - this.contentWidget = null; - if (contentWidget) { - this.native.takeWidget(); - return contentWidget; - } - return null; + takeWidget(): QWidget | null { + return wrapperCache.getWrapper(this.native.takeWidget()) as QWidget; } setViewportMargins(left: number, top: number, right: number, bottom: number): void { // Technically part of QAbstractScrollArea, but the C++ side has subclass specific @@ -98,5 +86,6 @@ export class QScrollArea extends QAbstractScrollArea { }; } } +wrapperCache.registerWrapper('QScrollAreaWrap', QScrollArea); export type QScrollAreaSignals = QAbstractScrollAreaSignals; diff --git a/src/lib/QtWidgets/QScrollBar.ts b/src/lib/QtWidgets/QScrollBar.ts index b06fa38e9..d634f6686 100644 --- a/src/lib/QtWidgets/QScrollBar.ts +++ b/src/lib/QtWidgets/QScrollBar.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QAbstractSlider, QAbstractSliderSignals } from './QAbstractSlider'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control scollbar widgets. * **This class is a JS wrapper around Qt's [QScrollBar class](https://doc.qt.io/qt-5/qscrollbar.html)** @@ -20,20 +22,19 @@ const scrollbar = new QScrollBar(); ``` */ export class QScrollBar extends QAbstractSlider { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QScrollBar(parent.native); } else { native = new addon.QScrollBar(); } super(native); - this.native = native; - this.setNodeParent(parent); } } +wrapperCache.registerWrapper('QScrollBarWrap', QScrollBar); export type QScrollBarSignals = QAbstractSliderSignals; diff --git a/src/lib/QtWidgets/QShortcut.ts b/src/lib/QtWidgets/QShortcut.ts index 6e2ddbc4b..df285df23 100644 --- a/src/lib/QtWidgets/QShortcut.ts +++ b/src/lib/QtWidgets/QShortcut.ts @@ -1,12 +1,14 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; import { QKeySequence } from '../QtGui/QKeySequence'; import { ShortcutContext } from '../QtEnums'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; +import { wrapperCache } from '../core/WrapperCache'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > The QShortcut class is used to create keyboard shortcuts. * **This class is a JS wrapper around Qt's [QShortcut class](https://doc.qt.io/qt-5/qshortcut.html)** @@ -29,12 +31,18 @@ global.win = win; global.shortcut = shortcut; ``` */ -export class QShortcut extends NodeObject { - native: NativeElement; - constructor(parent: NodeWidget) { - const native = new addon.QShortcut(parent.native); +export class QShortcut extends QObject { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QShortcut(parent.native); + } else { + native = new addon.QShortcut(); + } super(native); - this.native = native; } setEnabled(enabled: boolean): void { this.native.setEnabled(enabled); @@ -49,6 +57,7 @@ export class QShortcut extends NodeObject { this.native.setContext(shortcutContext); } } +wrapperCache.registerWrapper('QShortcutWrap', QShortcut); export interface QShortcutSignals extends QObjectSignals { activated: () => void; diff --git a/src/lib/QtWidgets/QSlider.ts b/src/lib/QtWidgets/QSlider.ts index f3fde92db..e39431d14 100644 --- a/src/lib/QtWidgets/QSlider.ts +++ b/src/lib/QtWidgets/QSlider.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QAbstractSlider, QAbstractSliderSignals } from './QAbstractSlider'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Create and control slider widgets. * **This class is a JS wrapper around Qt's [QSlider class](https://doc.qt.io/qt-5/qslider.html)** @@ -20,19 +22,17 @@ const slider = new QSlider(); ``` */ export class QSlider extends QAbstractSlider { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QSlider(parent.native); } else { native = new addon.QSlider(); } super(native); - this.native = native; - this.setNodeParent(parent); } setTickInterval(ti: number): void { this.setProperty('tickInterval', ti); @@ -47,6 +47,7 @@ export class QSlider extends QAbstractSlider { return this.property('tickPosition').toInt(); } } +wrapperCache.registerWrapper('QSliderWrap', QSlider); export enum TickPosition { NoTicks, diff --git a/src/lib/QtWidgets/QSpinBox.ts b/src/lib/QtWidgets/QSpinBox.ts index 7cbaee719..d75c66a66 100644 --- a/src/lib/QtWidgets/QSpinBox.ts +++ b/src/lib/QtWidgets/QSpinBox.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; import { QAbstractSpinBox, QAbstractSpinBoxSignals, StepType } from './QAbstractSpinBox'; +import { wrapperCache } from '../core/WrapperCache'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control spin box widgets. * **This class is a JS wrapper around Qt's [QSpinBox class](https://doc.qt.io/qt-5/qspinbox.html)** @@ -20,19 +22,17 @@ const spinBox = new QSpinBox(); ``` */ export class QSpinBox extends QAbstractSpinBox { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QSpinBox(parent.native); } else { native = new addon.QSpinBox(); } super(native); - this.setNodeParent(parent); - this.native = native; } cleanText(): string { return this.property('cleanText').toString(); @@ -89,6 +89,7 @@ export class QSpinBox extends QAbstractSpinBox { this.native.setRange(minimum, maximum); } } +wrapperCache.registerWrapper('QSpinBoxWrap', QSpinBox); export interface QSpinBoxSignals extends QAbstractSpinBoxSignals { valueChanged: (value: number) => void; diff --git a/src/lib/QtWidgets/QSplitter.ts b/src/lib/QtWidgets/QSplitter.ts index 177706c6a..79eb6e410 100644 --- a/src/lib/QtWidgets/QSplitter.ts +++ b/src/lib/QtWidgets/QSplitter.ts @@ -1,8 +1,10 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NodeFrame, QFrameSignals } from './QFrame'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { QFrame, QFrameSignals } from './QFrame'; import { NativeElement } from '../core/Component'; import { Orientation } from '../QtEnums'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -34,22 +36,20 @@ splitterHorizontal.addWidget(right); ``` */ -export class QSplitter extends NodeFrame { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QSplitter extends QFrame { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QSplitter(parent.native); } else { native = new addon.QSplitter(); } super(native); - this.native = native; - this.setNodeParent(parent); } - addWidget(widget: NodeWidget): void { + addWidget(widget: QWidget): void { this.native.addWidget(widget.native); } childrenCollapsible(): boolean { @@ -58,10 +58,10 @@ export class QSplitter extends NodeFrame { count(): number { return this.native.count(); } - indexOf(widget: NodeWidget): number { + indexOf(widget: QWidget): number { return this.native.indexOf(widget.native); } - insertWidget(index: number, widget: NodeWidget): void { + insertWidget(index: number, widget: QWidget): void { this.native.insertWidget(index, widget.native); } isCollapsible(index: number): boolean { @@ -77,6 +77,7 @@ export class QSplitter extends NodeFrame { this.native.setOrientation(orientation); } } +wrapperCache.registerWrapper('QSplitterWrap', QSplitter); export interface QSplitterSignals extends QFrameSignals { splitterMoved: (pos: number, index: number) => void; diff --git a/src/lib/QtWidgets/QStackedWidget.ts b/src/lib/QtWidgets/QStackedWidget.ts index 13e1d2f45..3fbbcdc35 100644 --- a/src/lib/QtWidgets/QStackedWidget.ts +++ b/src/lib/QtWidgets/QStackedWidget.ts @@ -1,7 +1,9 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NodeFrame, QFrameSignals } from './QFrame'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { QFrame, QFrameSignals } from './QFrame'; +import { wrapperCache } from '../core/WrapperCache'; import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -44,26 +46,22 @@ win.show(); ``` */ -export class QStackedWidget extends NodeFrame { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QStackedWidget extends QFrame { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QStackedWidget(parent.native); } else { native = new addon.QStackedWidget(); } super(native); - this.setNodeParent(parent); - this.native = native; } - // *** Public Function *** - addWidget(widget: NodeWidget): void { + addWidget(widget: QWidget): void { this.native.addWidget(widget.native); - this.nodeChildren.add(widget); widget.setFlexNodeSizeControlled(true); } count(): number { @@ -75,9 +73,8 @@ export class QStackedWidget extends NodeFrame { // TODO: QWidget * currentWidget() const // TODO: int indexOf(QWidget *widget) const // TODO: int insertWidget(int index, QWidget *widget) - removeWidget(widget: NodeWidget): void { + removeWidget(widget: QWidget): void { this.native.removeWidget(widget.native); - this.nodeChildren.delete(widget); } // TODO: QWidget * widget(int index) const @@ -85,10 +82,11 @@ export class QStackedWidget extends NodeFrame { setCurrentIndex(index: number): void { this.native.setCurrentIndex(index); } - setCurrentWidget(widget: NodeWidget): void { + setCurrentWidget(widget: QWidget): void { this.native.setCurrentWidget(widget.native); } } +wrapperCache.registerWrapper('QStackedWidgetWrap', QStackedWidget); export interface QStackedWidgetSignals extends QFrameSignals { currentChanged: (index: number) => void; diff --git a/src/lib/QtWidgets/QStandardItem.ts b/src/lib/QtWidgets/QStandardItem.ts index bb9be5d81..c43fcf2be 100644 --- a/src/lib/QtWidgets/QStandardItem.ts +++ b/src/lib/QtWidgets/QStandardItem.ts @@ -5,21 +5,21 @@ import { ItemFlag } from '../QtEnums/ItemFlag'; import { CheckState } from '../QtEnums'; export class QStandardItem extends Component { - native: NativeElement; constructor(); constructor(parent: QStandardItem, text?: string); constructor(native: NativeElement); constructor(parent?: NativeElement | QStandardItem, text?: string) { - super(); + let native: NativeElement; if (checkIfNativeElement(parent)) { - this.native = parent as NativeElement; + native = parent as NativeElement; } else { if (text) { - this.native = new addon.QStandardItem(text); + native = new addon.QStandardItem(text); } else { - this.native = new addon.QStandardItem(); + native = new addon.QStandardItem(); } } + super(native); } setCheckState(state: CheckState): void { this.native.setCheckState(state); diff --git a/src/lib/QtWidgets/QStandardItemModel.ts b/src/lib/QtWidgets/QStandardItemModel.ts index b09db9406..a5c0893e2 100644 --- a/src/lib/QtWidgets/QStandardItemModel.ts +++ b/src/lib/QtWidgets/QStandardItemModel.ts @@ -1,27 +1,26 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement } from '../core/Component'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { QStandardItem } from './QStandardItem'; +import { wrapperCache } from '../core/WrapperCache'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; export interface QStandardItemModelSignals extends QObjectSignals { itemChanged: (item: QStandardItem) => void; } -export class QStandardItemModel extends NodeObject { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QStandardItemModel extends QObject { + constructor(arg?: QObject | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QObject; native = new addon.QStandardItemModel(parent.native); } else { native = new addon.QStandardItemModel(); } super(native); - this.native = native; - parent && parent.nodeChildren.add(this); } item(row: number, column = 0): QStandardItem | void { const item = this.native.item(row, column); @@ -32,3 +31,4 @@ export class QStandardItemModel extends NodeObject { } } } +wrapperCache.registerWrapper('QStandardItemModelWrap', QStandardItemModel); diff --git a/src/lib/QtWidgets/QStatusBar.ts b/src/lib/QtWidgets/QStatusBar.ts index d3571b45a..56a682aa9 100644 --- a/src/lib/QtWidgets/QStatusBar.ts +++ b/src/lib/QtWidgets/QStatusBar.ts @@ -1,13 +1,15 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals, QWidget } from './QWidget'; +import { QWidgetSignals, QWidget } from './QWidget'; import { NativeElement } from '../core/Component'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; export interface QStatusBarSignals extends QWidgetSignals { messageChanged: (message: string) => void; } /** - + > Create and control progress bar widgets. * **This class is a JS wrapper around Qt's [QStatusBar class](https://doc.qt.io/qt-5/qstatusbar.html)** @@ -22,28 +24,19 @@ const { QStatusBar } = require("@nodegui/nodegui"); const progressBar = new QStatusBar(); ``` */ -export class QStatusBar extends NodeWidget { - native: NativeElement; - permanentWidgets: Set; - widgets: Set; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QStatusBar extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QStatusBar(parent.native); } else { native = new addon.QStatusBar(); } - super(native); - this.native = native; - this.setNodeParent(parent); - - this.permanentWidgets = new Set(); - this.widgets = new Set(); } - /** * Adds the given widget permanently to this status bar, reparenting the widget if it isn't already a child of this QStatusBar object. The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. * Permanently means that the widget may not be obscured by temporary messages. It is is located at the far right of the status bar. @@ -52,10 +45,6 @@ export class QStatusBar extends NodeWidget { */ addPermanentWidget(widget: QWidget, stretch = 0): void { this.native.addPermanentWidget(widget.native, stretch); - - if (!this.permanentWidgets.has(widget.native)) { - this.permanentWidgets.add(widget.native); - } } /** @@ -66,10 +55,6 @@ export class QStatusBar extends NodeWidget { */ addWidget(widget: QWidget, stretch = 0): void { this.native.addWidget(widget.native, stretch); - - if (!this.widgets.has(widget.native)) { - this.widgets.add(widget.native); - } } /** @@ -95,12 +80,7 @@ export class QStatusBar extends NodeWidget { * @param stretch Used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. */ insertPermanentWidget(index: number, widget: QWidget, stretch = 0): number { - const insertionIndex = this.native.insertPermanentWidget(index, widget.native, stretch); - if (!this.permanentWidgets.has(widget.native)) { - this.permanentWidgets.add(widget.native); - } - - return insertionIndex; + return this.native.insertPermanentWidget(index, widget.native, stretch); } /** @@ -112,12 +92,7 @@ export class QStatusBar extends NodeWidget { * @param stretch Used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. */ insertWidget(index: number, widget: QWidget, stretch = 0): number { - const insertionIndex = this.native.insertWidget(index, widget.native, stretch); - if (!this.widgets.has(widget.native)) { - this.widgets.add(widget.native); - } - - return insertionIndex; + return this.native.insertWidget(index, widget.native, stretch); } /** @@ -134,8 +109,6 @@ export class QStatusBar extends NodeWidget { */ removeWidget(widget: QWidget): void { this.native.removeWidget(widget.native); - this.widgets.delete(widget.native); - this.permanentWidgets.delete(widget.native); } /** @@ -156,3 +129,4 @@ export class QStatusBar extends NodeWidget { this.native.setSizeGripEnabled(enabled); } } +wrapperCache.registerWrapper('QStatusBarWrap', QStatusBar); diff --git a/src/lib/QtWidgets/QSvgWidget.ts b/src/lib/QtWidgets/QSvgWidget.ts index 6ed0af1f3..02adf2d8f 100644 --- a/src/lib/QtWidgets/QSvgWidget.ts +++ b/src/lib/QtWidgets/QSvgWidget.ts @@ -1,9 +1,11 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Display SVG files in a widget. * **This class is a JS wrapper around Qt's [QSvgWidget class](https://doc.qt.io/qt-5/qsvgwidget.html)** @@ -26,20 +28,18 @@ fs.readFile("icon.svg", (err, buffer) => { ``` */ -export class QSvgWidget extends NodeWidget { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QSvgWidget extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QSvgWidget(parent.native); } else { native = new addon.QSvgWidget(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } load(file: string | Buffer): void { if (file instanceof Buffer) { @@ -49,3 +49,4 @@ export class QSvgWidget extends NodeWidget { } } } +wrapperCache.registerWrapper('QSvgWidgetWrap', QSvgWidget); diff --git a/src/lib/QtWidgets/QSystemTrayIcon.ts b/src/lib/QtWidgets/QSystemTrayIcon.ts index 220027257..18bbbdbae 100644 --- a/src/lib/QtWidgets/QSystemTrayIcon.ts +++ b/src/lib/QtWidgets/QSystemTrayIcon.ts @@ -1,12 +1,14 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; import { QIcon } from '../QtGui/QIcon'; import { QMenu } from './QMenu'; -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; +import { wrapperCache } from '../core/WrapperCache'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control system tray icon. * **This class is a JS wrapper around Qt's [QSystemTrayIcon class](https://doc.qt.io/qt-5/qsystemtrayicon.html)** @@ -28,20 +30,18 @@ tray.show(); global.tray = tray; // prevents garbage collection of tray ``` */ -export class QSystemTrayIcon extends NodeObject { - native: NativeElement; - contextMenu?: QMenu; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QSystemTrayIcon extends QObject { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QSystemTrayIcon(parent.native); } else { native = new addon.QSystemTrayIcon(); } super(native); - this.native = native; } show(): void { this.native.show(); @@ -62,10 +62,10 @@ export class QSystemTrayIcon extends NodeObject { this.native.setToolTip(tooltip); } setContextMenu(menu: QMenu): void { - this.contextMenu = menu; - this.native.setContextMenu(this.contextMenu.native); + this.native.setContextMenu(menu.native); } } +wrapperCache.registerWrapper('QSystemTrayIconWrap', QSystemTrayIcon); export enum QSystemTrayIconActivationReason { Unknown = 0, diff --git a/src/lib/QtWidgets/QTabBar.ts b/src/lib/QtWidgets/QTabBar.ts index d6862175e..546886cd4 100644 --- a/src/lib/QtWidgets/QTabBar.ts +++ b/src/lib/QtWidgets/QTabBar.ts @@ -1,6 +1,5 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; -import { NativeElement } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; import { QIcon } from '../QtGui/QIcon'; import { TextElideMode } from '../QtEnums'; import { QSize } from '../QtCore/QSize'; @@ -8,6 +7,9 @@ import { QVariant } from '../QtCore/QVariant'; import { QColor } from '../QtGui/QColor'; import { QPoint } from '../QtCore/QPoint'; import { QRect } from '../QtCore/QRect'; +import { wrapperCache } from '../core/WrapperCache'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -26,20 +28,18 @@ const tabBar = new QTabBar(); ``` */ -export class QTabBar extends NodeWidget { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QTabBar extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QTabBar(parent.native); } else { native = new addon.QTabBar(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } setAutoHide(hide: boolean): void { this.setProperty('autoHide', hide); @@ -155,7 +155,7 @@ export class QTabBar extends NodeWidget { removeTab(index: number): void { this.native.removeTab(index); } - setTabButton(index: number, position: ButtonPosition, widget: NodeWidget | undefined | null): void { + setTabButton(index: number, position: ButtonPosition, widget: QWidget | undefined | null): void { this.native.setTabButton(index, position, widget == null ? null : widget?.native); } setTabData(index: number, data: QVariant): void { @@ -201,6 +201,7 @@ export class QTabBar extends NodeWidget { return new QRect(this.native.tabRect(index)); } } +wrapperCache.registerWrapper('QTabBarWrap', QTabBar); export enum ButtonPosition { LeftSide = 0, diff --git a/src/lib/QtWidgets/QTabWidget.ts b/src/lib/QtWidgets/QTabWidget.ts index 2ddffe107..e40ccfdcd 100644 --- a/src/lib/QtWidgets/QTabWidget.ts +++ b/src/lib/QtWidgets/QTabWidget.ts @@ -1,16 +1,18 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidgetSignals } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QIcon } from '../QtGui/QIcon'; import { TabPosition } from '../QtEnums'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control a stack of tabbed widgets. * **This class is a JS wrapper around Qt's [QTabWidget class](https://doc.qt.io/qt-5/qtabwidget.html)** -A 'QTabWidget' provides a tab bar and a "page area" that is used to display pages related to each tab. +A 'QTabWidget' provides a tab bar and a "page area" that is used to display pages related to each tab. ### Example @@ -24,38 +26,32 @@ tabWidget.addTab(new QCalendarWidget(), new QIcon(), 'Tab 1'); tabWidget.addTab(new QCalendarWidget(), new QIcon(), 'Tab 2'); ``` */ -export class QTabWidget extends NodeWidget { - native: NativeElement; - tabs: NodeWidget[]; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QTabWidget extends QWidget { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QTabWidget(parent.native); } else { native = new addon.QTabWidget(); } super(native); - this.setNodeParent(parent); - this.tabs = []; - this.native = native; } - addTab(page: NodeWidget, icon: QIcon, label: string): number { + addTab(page: QWidget, icon: QIcon, label: string): number { const index = this.native.addTab(page.native, icon.native, label); - this.tabs.push(page); page.setFlexNodeSizeControlled(true); return index; } - insertTab(index: number, page: NodeWidget, icon: QIcon, label: string): number { + insertTab(index: number, page: QWidget, icon: QIcon, label: string): number { const newIndex = this.native.insertTab(index, page.native, icon.native, label); - this.tabs.splice(index, 0, page); return newIndex; } - indexOf(widget: NodeWidget): number { + indexOf(widget: QWidget): number { return this.native.indexOf(widget.native); } @@ -81,15 +77,19 @@ export class QTabWidget extends NodeWidget { removeTab(index: number): void { this.native.removeTab(index); - const toRemove = this.tabs[index]; + const toRemove = this.widget(index); toRemove.setFlexNodeSizeControlled(false); - this.tabs.splice(index, 1); } setTabsClosable(closeable: boolean): void { this.native.setTabsClosable(closeable); } + + widget(index: number): QWidget { + return wrapperCache.getWrapper(this.native.widget(index)) as QWidget; + } } +wrapperCache.registerWrapper('QTabWidgetWrap', QTabWidget); export interface QTabWidgetSignals extends QWidgetSignals { currentChanged: (index: number) => void; diff --git a/src/lib/QtWidgets/QTableView.ts b/src/lib/QtWidgets/QTableView.ts index d629903c1..9fce94cb7 100644 --- a/src/lib/QtWidgets/QTableView.ts +++ b/src/lib/QtWidgets/QTableView.ts @@ -1,9 +1,11 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; import { SortOrder, PenStyle } from '../QtEnums'; import { QAbstractItemView, QAbstractItemViewSignals } from './QAbstractItemView'; import { QHeaderView } from './QHeaderView'; +import { wrapperCache } from '../core/WrapperCache'; +import { NativeElement } from '../core/Component'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -20,7 +22,19 @@ const tableview = new QTableView(); ``` */ -export abstract class NodeTableView extends QAbstractItemView { +export class QTableView extends QAbstractItemView { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QTableView(parent.native); + } else { + native = new addon.QTableView(); + } + super(native); + } // *** Public Functions *** clearSpans(): void { this.native.clearSpans(); @@ -177,22 +191,6 @@ export abstract class NodeTableView extends Q this.native.sortByColumn(column, order); } } - -export class QTableView extends NodeTableView { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { - native = new addon.QTableView(parent.native); - } else { - native = new addon.QTableView(); - } - super(native); - this.native = native; - parent && this.setNodeParent(parent); - } -} +wrapperCache.registerWrapper('QTableViewWrap', QTableView); export type QTableViewSignals = QAbstractItemViewSignals; diff --git a/src/lib/QtWidgets/QTableWidget.ts b/src/lib/QtWidgets/QTableWidget.ts index b6ff25d0f..5fba3e109 100644 --- a/src/lib/QtWidgets/QTableWidget.ts +++ b/src/lib/QtWidgets/QTableWidget.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidget } from './QWidget'; +import { QWidget } from './QWidget'; import { NativeElement, Component } from '../core/Component'; import { MatchFlag, ScrollHint, SortOrder } from '../QtEnums'; import { QTableWidgetItem } from './QTableWidgetItem'; import { QAbstractScrollArea, QAbstractScrollAreaSignals } from './QAbstractScrollArea'; import { QRect } from '../QtCore/QRect'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** @@ -38,18 +40,24 @@ win.show(); ``` */ export class QTableWidget extends QAbstractScrollArea { - native: NativeElement; items: Set; - constructor(rows: number, columns: number, parent?: NodeWidget) { - let native; - if (parent) { - native = new addon.QTableWidget(rows, columns, parent.native); + constructor(rowsOrNativeOrParent: QWidget | NativeElement | number, columns?: number, parent?: QWidget) { + let native: NativeElement; + if (checkIfNativeElement(rowsOrNativeOrParent)) { + native = rowsOrNativeOrParent as NativeElement; + } else if (typeof rowsOrNativeOrParent == 'number') { + const rows = rowsOrNativeOrParent; + if (parent) { + native = new addon.QTableWidget(rows, columns, parent.native); + } else { + native = new addon.QTableWidget(rows, columns); + } + } else if (rowsOrNativeOrParent != null) { + native = new addon.QTableWidget(rowsOrNativeOrParent.native); } else { - native = new addon.QTableWidget(rows, columns); + native = new addon.QTableWidget(); } super(native); - this.native = native; - this.setNodeParent(parent); this.items = new Set(); } selectedRanges(): Range[] { @@ -61,9 +69,8 @@ export class QTableWidget extends QAbstractScrollArea { editItem(item: Component): void { this.native.editItem(item.native); } - setCellWidget(row: number, column: number, widget: NodeWidget): void { + setCellWidget(row: number, column: number, widget: QWidget): void { this.native.setCellWidget(row, column, widget.native); - this.items.add(widget); } setItem(row: number, column: number, item: QTableWidgetItem): void { this.native.setItem(row, column, item.native); @@ -232,6 +239,7 @@ export class QTableWidget extends QAbstractScrollArea { return this.native.isSortingEnabled(); } } +wrapperCache.registerWrapper('QTableWidgetWrap', QTableWidget); interface Range { topRow: number; diff --git a/src/lib/QtWidgets/QTableWidgetItem.ts b/src/lib/QtWidgets/QTableWidgetItem.ts index 910a4b31a..79137ae49 100644 --- a/src/lib/QtWidgets/QTableWidgetItem.ts +++ b/src/lib/QtWidgets/QTableWidgetItem.ts @@ -41,13 +41,12 @@ win.show(); */ export class QTableWidgetItem extends Component { - native: NativeElement; constructor(); constructor(other: QTableWidgetItem); constructor(native: NativeElement); constructor(text: string); constructor(arg?: QTableWidgetItem | NativeElement | string) { - let native; + let native: NativeElement; if (typeof arg === 'string') { native = new addon.QTableWidgetItem(arg); } else if (checkIfNativeElement(arg)) { @@ -55,8 +54,7 @@ export class QTableWidgetItem extends Component { } else { native = new addon.QTableWidgetItem(); } - super(); - this.native = native; + super(native); } setBackground(brush: QBrush): void { this.native.setBackground(brush.native); diff --git a/src/lib/QtWidgets/QTextBrowser.ts b/src/lib/QtWidgets/QTextBrowser.ts index 35a10ae3e..a64c10311 100644 --- a/src/lib/QtWidgets/QTextBrowser.ts +++ b/src/lib/QtWidgets/QTextBrowser.ts @@ -1,11 +1,13 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QUrl } from '../QtCore/QUrl'; -import { NodeTextEdit, QTextEditSignals } from './QTextEdit'; +import { QTextEdit, QTextEditSignals } from './QTextEdit'; +import { wrapperCache } from '../core/WrapperCache'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Create and control text browser. * **This class is a JS wrapper around Qt's [QTextBrowser class](https://doc.qt.io/qt-5/qtextbrowser.html)** @@ -21,20 +23,18 @@ const textBrowser = new QTextBrowser(); ``` */ -export class QTextBrowser extends NodeTextEdit { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QTextBrowser extends QTextEdit { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QTextBrowser(parent.native); } else { native = new addon.QTextBrowser(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } setOpenExternalLinks(open: boolean): void { this.setProperty('openExternalLinks', open); @@ -89,6 +89,7 @@ export class QTextBrowser extends NodeTextEdit { this.native.reload(); } } +wrapperCache.registerWrapper('QTextBrowserWrap', QTextBrowser); export interface QTextBrowserSignals extends QTextEditSignals { anchorClicked: (link: QUrl) => void; diff --git a/src/lib/QtWidgets/QTextEdit.ts b/src/lib/QtWidgets/QTextEdit.ts index e014bdb51..2dc51447c 100644 --- a/src/lib/QtWidgets/QTextEdit.ts +++ b/src/lib/QtWidgets/QTextEdit.ts @@ -1,13 +1,15 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; import { QAbstractScrollArea, QAbstractScrollAreaSignals } from './QAbstractScrollArea'; import { AlignmentFlag, TextInteractionFlag } from '../QtEnums'; import { QFont } from '../QtGui/QFont'; import { QColor } from '../QtGui/QColor'; +import { checkIfNativeElement } from '../utils/helpers'; +import { NativeElement } from '../core/Component'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Create and control editable text field. * **This class is a JS wrapper around Qt's [QTextEdit class](https://doc.qt.io/qt-5/qtextedit.html)** @@ -23,7 +25,19 @@ const textEdit = new QTextEdit(); ``` */ -export abstract class NodeTextEdit extends QAbstractScrollArea { +export class QTextEdit extends QAbstractScrollArea { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QTextEdit(parent.native); + } else { + native = new addon.QTextEdit(); + } + super(native); + } setAcceptRichText(accept: boolean): void { this.setProperty('acceptRichText', accept); } @@ -217,6 +231,7 @@ export abstract class NodeTextEdit extends QAb this.native.zoomOut(range); } } +wrapperCache.registerWrapper('QTextEditWrap', QTextEdit); export enum AutoFormattingFlag { AutoNone = 0, @@ -239,23 +254,6 @@ export enum WrapMode { WrapAtWordBoundaryOrAnywhere, } -export class QTextEdit extends NodeTextEdit { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { - native = new addon.QTextEdit(parent.native); - } else { - native = new addon.QTextEdit(); - } - super(native); - this.native = native; - parent && this.setNodeParent(parent); - } -} - export interface QTextEditSignals extends QAbstractScrollAreaSignals { copyAvailable: (yes: boolean) => void; cursorPositionChanged: () => void; diff --git a/src/lib/QtWidgets/QTimeEdit.ts b/src/lib/QtWidgets/QTimeEdit.ts index 18d3c97a9..aa63ac1dd 100644 --- a/src/lib/QtWidgets/QTimeEdit.ts +++ b/src/lib/QtWidgets/QTimeEdit.ts @@ -1,10 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { QDateTimeEdit } from './QDateTimeEdit'; +import { wrapperCache } from '../core/WrapperCache'; import { NativeElement } from '../core/Component'; -import { NodeDateTimeEdit } from './QDateTimeEdit'; +import { checkIfNativeElement } from '../utils/helpers'; /** - + > Creates a widget to edit dates with spin box layout. WIP! * **This class is a JS wrapper around Qt's [QTimeEdit class](https://doc.qt.io/qt-5/qtimeedit.html)** @@ -20,19 +22,18 @@ const timeEdit = new QTimeEdit(); // must be implemented ``` */ -export class QTimeEdit extends NodeDateTimeEdit { - native: NativeElement; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { +export class QTimeEdit extends QDateTimeEdit { + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QTimeEdit(parent.native); } else { native = new addon.QTimeEdit(); } super(native); - this.native = native; - this.setNodeParent(parent); } } +wrapperCache.registerWrapper('QTimeEditWrap', QTimeEdit); diff --git a/src/lib/QtWidgets/QToolButton.ts b/src/lib/QtWidgets/QToolButton.ts index 629f76fcd..0d3f8d22d 100644 --- a/src/lib/QtWidgets/QToolButton.ts +++ b/src/lib/QtWidgets/QToolButton.ts @@ -1,15 +1,16 @@ import addon from '../utils/addon'; -import { NodeWidget } from './QWidget'; -import { NativeElement, NativeRawPointer, Component } from '../core/Component'; +import { QWidget, QWidgetSignals } from './QWidget'; +import { NativeElement } from '../core/Component'; import { QAbstractButton, QAbstractButtonSignals } from './QAbstractButton'; import { ToolButtonStyle } from '../QtEnums/ToolButtonStyle'; import { ArrowType } from '../QtEnums/ArrowType'; import { QAction } from '../QtWidgets/QAction'; import { QMenu } from './QMenu'; -import { checkIfNativeElement, checkIfNapiExternal } from '../utils/helpers'; +import { checkIfNativeElement } from '../utils/helpers'; +import { wrapperCache } from '../core/WrapperCache'; /** - + > Create and control buttons to use inside a QToolBar. * **This class is a JS wrapper around Qt's [QToolButton class](https://doc.qt.io/qt-5/qtoolbutton.html)** @@ -26,29 +27,17 @@ tool.setText('Help'); ``` */ export class QToolButton extends QAbstractButton { - native: NativeElement; - private _defaultAction?: QAction | null; - private _menu?: QMenu | null; - constructor(); - constructor(parent: NodeWidget); - constructor(rawPointer: NativeRawPointer, disableNativeDeletion?: boolean); - constructor(arg?: NodeWidget | NativeRawPointer | NativeElement, disableNativeDeletion = true) { - let native; - let parent: Component | undefined; + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; if (checkIfNativeElement(arg)) { native = arg as NativeElement; - } else if (checkIfNapiExternal(arg)) { - native = new addon.QToolButton(arg, disableNativeDeletion); - } else if (arg) { - const parentWidget = arg as NodeWidget; - native = new addon.QToolButton(parentWidget.native); - parent = parentWidget; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QToolButton(parent.native); } else { native = new addon.QToolButton(); } super(native); - this.native = native; - parent && this.setNodeParent(parent); } setArrowType(type: ArrowType): void { this.setProperty('arrowType', type); @@ -75,29 +64,22 @@ export class QToolButton extends QAbstractButton { return this.property('toolButtonStyle').toInt(); } setMenu(menu: QMenu): void { - this._menu = menu; this.native.setMenu(menu.native); } menu(): QMenu | null { - if (this._menu) { - return this._menu; - } - return null; + return wrapperCache.getWrapper(this.native.menu()) as QMenu; } setDefaultAction(action: QAction): void { - this._defaultAction = action; this.native.setDefaultAction(action.native); } defaultAction(): QAction | null { - if (this._defaultAction) { - return this._defaultAction; - } - return null; + return wrapperCache.getWrapper(this.native.defaultAction()) as QAction; } showMenu(): void { this.native.showMenu(); } } +wrapperCache.registerWrapper('QToolButtonWrap', QToolButton); export enum ToolButtonPopupMode { DelayedPopup, diff --git a/src/lib/QtWidgets/QTreeWidget.ts b/src/lib/QtWidgets/QTreeWidget.ts index 4eb5e5d5a..7747187d8 100644 --- a/src/lib/QtWidgets/QTreeWidget.ts +++ b/src/lib/QtWidgets/QTreeWidget.ts @@ -1,13 +1,14 @@ import addon from '../utils/addon'; -import { NodeWidget, QWidget } from './QWidget'; +import { QWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { QAbstractScrollArea, QAbstractScrollAreaSignals } from './QAbstractScrollArea'; import { QTreeWidgetItem } from './QTreeWidgetItem'; -import { MatchFlag } from '../..'; +import { checkIfNativeElement, MatchFlag } from '../..'; +import { wrapperCache } from '../core/WrapperCache'; /** - -> Creates a tree view that uses a predefined tree model. + +> Creates a tree view that uses a predefined tree model. * **This class is a JS wrapper around Qt's [QTreeWidget class](https://doc.qt.io/qt-5/qtreewidget.html)** @@ -49,21 +50,20 @@ win.show(); (global as any).win = win;``` */ export class QTreeWidget extends QAbstractScrollArea { - native: NativeElement; topLevelItems: Set; itemWidgets: Map; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { - let native; - if (parent) { + + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; native = new addon.QTreeWidget(parent.native); } else { native = new addon.QTreeWidget(); } super(native); - this.native = native; - this.setNodeParent(parent); this.topLevelItems = new Set(); this.itemWidgets = new Map(); } @@ -192,6 +192,7 @@ export class QTreeWidget extends QAbstractScrollArea { this.native.clear(); } } +wrapperCache.registerWrapper('QTreeWidgetWrap', QTreeWidget); export interface QTreeWidgetSignals extends QAbstractScrollAreaSignals { itemSelectionChanged: () => void; diff --git a/src/lib/QtWidgets/QTreeWidgetItem.ts b/src/lib/QtWidgets/QTreeWidgetItem.ts index a654951bd..1cb8cd193 100644 --- a/src/lib/QtWidgets/QTreeWidgetItem.ts +++ b/src/lib/QtWidgets/QTreeWidgetItem.ts @@ -8,7 +8,7 @@ import { CheckState, ItemDataRole } from '../QtEnums'; import { QVariantType, QVariant } from '../QtCore/QVariant'; /** - + > Creates an item for QTreeWidget. * **This class is a JS wrapper around Qt's [QTreeWidgetItem class](https://doc.qt.io/qt-5/qtreewidgetitem.html)** @@ -46,7 +46,6 @@ win.show(); ``` */ export class QTreeWidgetItem extends Component { - native: NativeElement; items: Set; constructor(); constructor(parent: QTreeWidgetItem, strings?: string[]); @@ -54,26 +53,26 @@ export class QTreeWidgetItem extends Component { constructor(native: NativeElement); constructor(strings: string[]); constructor(parent?: NativeElement | QTreeWidgetItem | QTreeWidget | string[], strings?: string[]) { - super(); - this.items = new Set(); + let native: NativeElement; if (checkIfNativeElement(parent)) { - this.native = parent as NativeElement; + native = parent as NativeElement; } else { if (parent instanceof QTreeWidgetItem || parent instanceof QTreeWidget) { - this.setNodeParent(parent); const type = parent instanceof QTreeWidgetItem ? 'item' : 'tree'; if (strings) { - this.native = new addon.QTreeWidgetItem(parent.native, strings, type); + native = new addon.QTreeWidgetItem(parent.native, strings, type); } else { - this.native = new addon.QTreeWidgetItem(parent.native, type); + native = new addon.QTreeWidgetItem(parent.native, type); } } else if (Array.isArray(parent)) { const strings = parent; - this.native = new addon.QTreeWidgetItem(strings); + native = new addon.QTreeWidgetItem(strings); } else { - this.native = new addon.QTreeWidgetItem(); + native = new addon.QTreeWidgetItem(); } } + super(native); + this.items = new Set(); } setText(column: number, text: string): void { this.native.setText(column, text); diff --git a/src/lib/QtWidgets/QWidget.ts b/src/lib/QtWidgets/QWidget.ts index 45aa943e5..047e9b33d 100644 --- a/src/lib/QtWidgets/QWidget.ts +++ b/src/lib/QtWidgets/QWidget.ts @@ -1,5 +1,5 @@ import addon from '../utils/addon'; -import { NodeLayout } from './QLayout'; +import { QLayout } from './QLayout'; import { NativeElement } from '../core/Component'; import { FlexLayout } from '../core/FlexLayout'; import { WidgetAttribute, WindowType, ContextMenuPolicy, FocusReason, FocusPolicy } from '../QtEnums'; @@ -24,54 +24,57 @@ import { QStyle } from '../QtGui/QStyle'; import { QWindow } from '../QtGui/QWindow'; /** + > Create and control views. -> Abstract class to add functionalities common to all Widgets. +* **This class is a JS wrapper around Qt's [QWidget class](https://doc.qt.io/qt-5/qwidget.html)** -**This class implements all methods, properties of Qt's [QWidget class](https://doc.qt.io/qt-5/qwidget.html) so that it can be inherited by all widgets** +A `QWidget` can be used to encapsulate other widgets and provide structure. It functions similar to a `div` in the web world. -`NodeWidget` is an abstract class and hence no instances of the same should be created. It exists so that we can add similar functionalities to all widget's easily. Additionally it helps in type checking process. If you wish to create a `div` like widget use [QWidget](QWidget.md) instead. - -**NodeWidget is the base class for all widgets.** ### Example ```javascript -const { - NodeWidget, - QPushButton, - QWidget, - QRadioButton -} = require("@nodegui/nodegui"); +const { QWidget } = require("@nodegui/nodegui"); -// showWidget can accept any widget since it expects NodeWidget -const showWidget = (widget: NodeWidget) => { - widget.show(); -}; - -showWidget(new QPushButton()); -showWidget(new QWidget()); -showWidget(new QRadioButton()); +const view = new QWidget(); +view.setObjectName("container"); //Similar to setting `id` on the web +view.setLayout(new FlexLayout()); ``` -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 { - _layout?: NodeLayout; - _rawInlineStyle = ''; - type = 'widget'; - private _effect?: QGraphicsEffect | null; - constructor(native: NativeElement) { +export class QWidget extends YogaWidget { + _rawInlineStyle: string; + type: string; + + constructor(arg?: QWidget | NativeElement) { + let native: NativeElement; + if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else if (arg != null) { + const parent = arg as QWidget; + native = new addon.QWidget(parent.native); + } else { + native = new addon.QWidget(); + } super(native); + this._rawInlineStyle = ''; + this.type = 'widget'; + this.setStyleSheet = memoizeOne(this.setStyleSheet); this.setInlineStyle = memoizeOne(this.setInlineStyle); this.setObjectName = memoizeOne(this.setObjectName); } - get layout(): NodeLayout | undefined { - return this._layout; + + layout(): QLayout | null { + return wrapperCache.getWrapper(this.native.layout()) as QLayout; } - set layout(l: NodeLayout | undefined) { - this._layout = l; + setLayout(layout: QLayout): void { + this.native.setLayout(layout == null ? null : layout.native); + + const flexLayout = layout as FlexLayout; + if (flexLayout?.setFlexNode) { + //if flex layout set the flexnode + flexLayout.setFlexNode(this.getFlexNode()); + } } // *** Public Functions *** acceptDrops(): boolean { @@ -369,7 +372,6 @@ export abstract class NodeWidget extends YogaWid this.native.setGeometry(x, y, w, h); } setGraphicsEffect(effect: QGraphicsEffect): void { - this._effect = effect; this.native.setGraphicsEffect(effect.native); } // TODO: void setInputMethodHints(Qt::InputMethodHints hints) @@ -382,15 +384,6 @@ export abstract class NodeWidget extends YogaWid this.native.setStyleSheet(style); } } - setLayout(parentLayout: NodeLayout): void { - const flexLayout = parentLayout as FlexLayout; - this.native.setLayout(parentLayout.native); - if (flexLayout.setFlexNode) { - //if flex layout set the flexnode - flexLayout.setFlexNode(this.getFlexNode()); - } - this._layout = parentLayout; - } // TODO: void setLayoutDirection(Qt::LayoutDirection direction) // TODO: void setLocale(const QLocale &locale) // TODO: void setMask(const QBitmap &bitmap) @@ -663,40 +656,4 @@ export interface QWidgetSignals extends QObjectSignals { windowIconChanged: (iconNative: NativeElement) => void; customContextMenuRequested: (pos: { x: number; y: number }) => void; } - -/** - > Create and control views. - -* **This class is a JS wrapper around Qt's [QWidget class](https://doc.qt.io/qt-5/qwidget.html)** - -A `QWidget` can be used to encapsulate other widgets and provide structure. It functions similar to a `div` in the web world. - - -### Example - -```javascript -const { QWidget } = require("@nodegui/nodegui"); - -const view = new QWidget(); -view.setObjectName("container"); //Similar to setting `id` on the web -view.setLayout(new FlexLayout()); -``` - */ -export class QWidget extends NodeWidget { - native: NativeElement; - constructor(arg?: NodeWidget | NativeElement) { - let native; - let parent; - if (checkIfNativeElement(arg)) { - native = arg as NativeElement; - } else if (arg as NodeWidget) { - parent = arg as NodeWidget; - native = new addon.QWidget(parent.native); - } else { - native = new addon.QWidget(); - } - super(native); - this.setNodeParent(parent); - this.native = native; - } -} +wrapperCache.registerWrapper('QWidgetWrap', QWidget); diff --git a/src/lib/core/Component.ts b/src/lib/core/Component.ts index d1ed5e52a..9c62cb5d2 100644 --- a/src/lib/core/Component.ts +++ b/src/lib/core/Component.ts @@ -7,10 +7,10 @@ export type NativeRawPointer = Record; > Abstract class that is root most base class for all widgets and layouts in the NodeGui World. **This class is used to add core properties to all widgets, layouts etc in NodeGui world. - Currently it helps us maintain references to the native C++ instance of the widget or layout. + Currently it helps us maintain references to the native C++ instance of the widget or layout. It also helps in preventing gc of child elements of a layout or widget** -`Component` is an abstract class and hence no instances of the same should be created. +`Component` is an abstract class and hence no instances of the same should be created. It exists so that we can add core functionalities to all widgets and layouts easily. This is an internal class. Its the root base class in NodeGui world. @@ -18,14 +18,8 @@ Its the root base class in NodeGui world. */ export abstract class Component { - nodeChildren: Set; - nodeParent?: Component; - constructor() { - this.nodeChildren = new Set(); + native: NativeElement | null; + constructor(native: NativeElement) { + this.native = native; } - setNodeParent(parent?: Component): void { - this.nodeParent = parent; - parent?.nodeChildren.add(this); - } - abstract native: NativeElement; } diff --git a/src/lib/core/EventWidget.ts b/src/lib/core/EventWidget.ts index b41950104..583a5e5aa 100644 --- a/src/lib/core/EventWidget.ts +++ b/src/lib/core/EventWidget.ts @@ -36,8 +36,9 @@ view.addEventListener(WidgetEventTypes.MouseMove, () => { export abstract class EventWidget extends Component { private emitter: EventEmitter; private _isEventProcessed = false; + constructor(native: NativeElement) { - super(); + super(native); if (native.initNodeEventEmitter == null) { throw new Error('initNodeEventEmitter not implemented on native side'); } diff --git a/src/lib/core/FlexLayout.ts b/src/lib/core/FlexLayout.ts index 6ed19989b..d930489de 100644 --- a/src/lib/core/FlexLayout.ts +++ b/src/lib/core/FlexLayout.ts @@ -1,13 +1,12 @@ import addon from '../utils/addon'; -import { NodeWidget } from '../QtWidgets/QWidget'; -import { NodeLayout, QLayoutSignals } from '../QtWidgets/QLayout'; +import { QWidget } from '../QtWidgets/QWidget'; +import { QLayout, QLayoutSignals } from '../QtWidgets/QLayout'; import { FlexNode } from './YogaWidget'; -import { NativeElement } from './Component'; export type FlexLayoutSignals = QLayoutSignals; /** - + > Custom layout to help layout child widgets using flex layout. * **This class is a JS wrapper around custom Qt layout implemented using [Yoga](https://github.com/facebook/yoga)** @@ -32,13 +31,10 @@ layout.addWidget(label); layout.addWidget(label2); ``` */ -export class FlexLayout extends NodeLayout { - native: NativeElement; +export class FlexLayout extends QLayout { protected flexNode?: FlexNode; - constructor(); - constructor(parent: NodeWidget); - constructor(parent?: NodeWidget) { + constructor(parent?: QWidget) { let native; if (parent) { native = new addon.FlexLayout(parent.native); @@ -46,49 +42,36 @@ export class FlexLayout extends NodeLayout { native = new addon.FlexLayout(); } super(native); - this.native = native; if (parent) { this.setFlexNode(parent.getFlexNode()); } } - addWidget(childWidget: NodeWidget, childFlexNode?: FlexNode): void { + addWidget(childWidget: QWidget, childFlexNode?: FlexNode): void { const childYogaNode = childFlexNode || childWidget.getFlexNode(); - if (this.nodeChildren.has(childWidget)) { + if (this.children().includes(childWidget)) { this.removeWidget(childWidget, childYogaNode); } - this.nodeChildren.add(childWidget); this.native.addWidget(childWidget.native, childYogaNode); } insertChildBefore( - childWidget: NodeWidget, - beforeChildWidget: NodeWidget, + childWidget: QWidget, + beforeChildWidget: QWidget, childFlexNode?: FlexNode, beforeChildFlexNode?: FlexNode, ): void { const childYogaNode = childFlexNode || childWidget.getFlexNode(); - if (this.nodeChildren.has(childWidget)) { + if (this.children().includes(childWidget)) { this.removeWidget(childWidget, childYogaNode); } const beforeChildYogaNode = beforeChildFlexNode || beforeChildWidget.getFlexNode(); - - const widgetArr = Array.from(this.nodeChildren); - const beforeChildIndex = this.getChildIndex(beforeChildWidget); - if (beforeChildIndex !== -1) { - widgetArr.splice(beforeChildIndex, 0, childWidget); - } - this.nodeChildren = new Set(widgetArr); this.native.insertChildBefore(childWidget.native, beforeChildYogaNode, childYogaNode); } - removeWidget(childWidget: NodeWidget, childFlexNode?: FlexNode): void { - if (!this.nodeChildren.has(childWidget)) { - return; - } + removeWidget(childWidget: QWidget, childFlexNode?: FlexNode): void { const childYogaNode = childFlexNode || childWidget.getFlexNode(); this.native.removeWidget(childWidget.native, childYogaNode); - this.nodeChildren.delete(childWidget); } setFlexNode(flexNode: FlexNode): void { @@ -96,16 +79,15 @@ export class FlexLayout extends NodeLayout { this.native.setFlexNode(flexNode); } - getChildIndex(childWidget: NodeWidget): number { - const widgetArr = Array.from(this.nodeChildren); - return widgetArr.indexOf(childWidget); + getChildIndex(childWidget: QWidget): number { + return this.children().indexOf(childWidget); } - getNextSibling(childWidget: NodeWidget): NodeWidget | null { + getNextSibling(childWidget: QWidget): QWidget | null { const childIndex = this.getChildIndex(childWidget); - const widgetArr = Array.from(this.nodeChildren); + const widgetArr = this.children(); if (childIndex !== -1) { - return (widgetArr[childIndex + 1] as NodeWidget) || null; + return (widgetArr[childIndex + 1] as QWidget) || null; } return null; } diff --git a/src/lib/core/Style/StyleSheet.ts b/src/lib/core/Style/StyleSheet.ts index aba56315e..cd9fbb657 100644 --- a/src/lib/core/Style/StyleSheet.ts +++ b/src/lib/core/Style/StyleSheet.ts @@ -1,7 +1,7 @@ import postcss from 'postcss'; import cuid from 'cuid'; import nodeguiAutoPrefixer from 'postcss-nodegui-autoprefixer'; -import { NodeWidget, QWidgetSignals } from '../../QtWidgets/QWidget'; +import { QWidget, QWidgetSignals } from '../../QtWidgets/QWidget'; export class StyleSheet { static create(cssString: string): string { try { @@ -14,7 +14,7 @@ export class StyleSheet { } export function prepareInlineStyleSheet( - widget: NodeWidget, + widget: QWidget, rawStyle: string, ): string { const inlineStyle = StyleSheet.create(rawStyle); diff --git a/src/lib/core/WrapperCache.ts b/src/lib/core/WrapperCache.ts index 13a218d22..abfb82218 100644 --- a/src/lib/core/WrapperCache.ts +++ b/src/lib/core/WrapperCache.ts @@ -1,3 +1,4 @@ +import { QObject } from '../QtCore/QObject'; import addon from '../utils/addon'; import { NativeElement } from './Component'; @@ -13,29 +14,133 @@ import { NativeElement } from './Component'; * wrapper automatically and unexpectedly garbage collected. */ export class WrapperCache { - private _cache = new Map(); + // Cache for wrapper where we always hold onto the wrapper until the C++ + // object is destroyed by Qt. We don't let the V8 GC destroy the wrapper + // first. These wrapper often have signal handlers which are kept alive + // by the wrapper themselves, and not because they are part of a + // `NFooBar` subclass. For example, the `QScreen` attaches signal + // handlers to the C++ `QScreen`. If the wrapper is GC'ed, then the + // signals handlers also stop working. + private _strongCache = new Map(); + + // WeakCache is for normal wrappers around `QObject` based `NFooBar` + // subclasses. + private _weakCache = new Map>(); + + private _wrapperRegistry = new Map(); constructor() { addon.WrapperCache_injectCallback(this._objectDestroyedCallback.bind(this)); } - private _objectDestroyedCallback(objectId: number): void { - if (!this._cache.has(objectId)) { - return; - } - const wrapper = this._cache.get(objectId); - wrapper.native = null; - this._cache.delete(objectId); + logCreateQObject = false; + logDestoryQObject = false; + + // This is only need for testing purposes + _flush(): void { + this._strongCache = new Map(); + this._weakCache = new Map>(); } - get(wrapperConstructor: { new (native: any): T }, native: NativeElement): T { - const id = native.__id__(); - if (this._cache.has(id)) { - return this._cache.get(id) as T; + private _objectDestroyedCallback(objectId: number): void { + if (this._strongCache.has(objectId)) { + const wrapper = this._strongCache.get(objectId); + wrapper.native = null; + this._strongCache.delete(objectId); + + if (this.logDestoryQObject) { + console.log(`NodeGui: Destroyed C++ object with ID: ${objectId}.`); + } + return; + } + + const wrapperRef = this._weakCache.get(objectId); + if (wrapperRef != null) { + const wrapper = wrapperRef.deref(); + if (wrapper != null) { + wrapper.native = null; + this._weakCache.delete(objectId); + if (this.logDestoryQObject) { + console.log(`NodeGui: Destroyed C++ object with ID: ${objectId}.`); + } + } + } + } + + get(wrapperConstructor: { new (native: any): T }, native: NativeElement): T { + const objectId = native.__id__(); + if (this._strongCache.has(objectId)) { + return this._strongCache.get(objectId) as T; } const wrapper = new wrapperConstructor(native); - this._cache.set(id, wrapper); + this._strongCache.set(objectId, wrapper); return wrapper; } + + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + getWrapper(native: any): QObject | null { + if (native == null) { + return null; + } + + const id = native.__id__(); + if (this._strongCache.has(id)) { + return this._strongCache.get(id); + } + + const ref = this._weakCache.get(id); + if (ref != null) { + const wrapper = ref.deref(); + if (wrapper != null) { + return wrapper; + } + } + + if (this._wrapperRegistry.has(native.wrapperType)) { + const wrapper = new (this._wrapperRegistry.get(native.wrapperType))(native); + this.store(wrapper); + return wrapper; + } else { + console.log(`NodeGui: Unable to find JS wrapper for type '${native.wrapperType}'.`); + } + + return null; + } + + registerWrapper(qobjectClassName: string, wrapperConstructor: { new (native: any): QObject }): void { + this._wrapperRegistry.set(qobjectClassName, wrapperConstructor); + } + + store(wrapper: QObject): void { + if (wrapper.native != null) { + const objectId = wrapper.native.__id__(); + this._weakCache.set(objectId, new WeakRef(wrapper)); + + addon.WrapperCache_store(wrapper.native, wrapper.native.__external_qobject__()); + if (this.logCreateQObject) { + console.log(`NodeGui: Created C++ object with ID: ${objectId}.`); + } + } + } } export const wrapperCache = new WrapperCache(); + +/** + * Turn on/off logging when QObjects are created. + * + * @param on When true, logging is written to console when QObjects are + * created by NodeGui. + */ +export function setLogCreateQObject(on: boolean): void { + wrapperCache.logCreateQObject = on; +} + +/** + * Turn on/off logging when QObjects are destoryed. + * + * @param on When true, logging is written to console when QObjects are + * destroyed. + */ +export function setLogDestroyQObject(on: boolean): void { + wrapperCache.logDestoryQObject = on; +} diff --git a/src/lib/core/YogaWidget.ts b/src/lib/core/YogaWidget.ts index c3cb0fad2..f3123c160 100644 --- a/src/lib/core/YogaWidget.ts +++ b/src/lib/core/YogaWidget.ts @@ -1,10 +1,10 @@ -import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QObject, QObjectSignals } from '../QtCore/QObject'; import { NativeRawPointer } from './Component'; export type FlexNode = NativeRawPointer<'YGNodeRef'>; /** - + > Abstract class to add common functionality related to Flex layout to all Widgets. **This class implements methods related to flex layout required to be present in all widgets** @@ -22,7 +22,7 @@ const flexNode = view.getFlexNode(); YogaWidget helps in storing all flex properties of a widget. */ -export abstract class YogaWidget extends NodeObject { +export abstract class YogaWidget extends QObject { getFlexNode(): FlexNode { return this.native.getFlexNode(); } diff --git a/src/lib/core/__test__/CacheTestQObject.ts b/src/lib/core/__test__/CacheTestQObject.ts index 79e42305a..865102699 100644 --- a/src/lib/core/__test__/CacheTestQObject.ts +++ b/src/lib/core/__test__/CacheTestQObject.ts @@ -1,23 +1,21 @@ import addon from '../../utils/addon'; import { NativeElement } from '../Component'; -import { NodeObject, QObjectSignals } from '../../QtCore/QObject'; +import { QObject, QObjectSignals } from '../../QtCore/QObject'; import { wrapperCache } from '../../core/WrapperCache'; -export class CacheTestQObject extends NodeObject { - native: NativeElement; +export class CacheTestQObject extends QObject { constructor(arg?: NativeElement) { let native; - if (native == null) { + if (arg == null) { native = new addon.CacheTestQObject(); } else { native = arg; } super(native); - this.native = native; } foo(): CacheTestQObject { - return wrapperCache.get(CacheTestQObject, this.native.foo()); + return wrapperCache.getWrapper(this.native.foo()) as CacheTestQObject; } clearFoo(): void { @@ -25,6 +23,7 @@ export class CacheTestQObject extends NodeObject { } bar(): CacheTestQObject { - return wrapperCache.get(CacheTestQObject, this.native.bar()); + return wrapperCache.getWrapper(this.native.bar()) as CacheTestQObject; } } +wrapperCache.registerWrapper('CacheTestQObjectWrap', CacheTestQObject); diff --git a/src/lib/core/__test__/WrapperCache.test.ts b/src/lib/core/__test__/WrapperCache.test.ts index fe5cde994..342d3f859 100644 --- a/src/lib/core/__test__/WrapperCache.test.ts +++ b/src/lib/core/__test__/WrapperCache.test.ts @@ -1,11 +1,14 @@ +import { QObject } from '../../QtCore/QObject'; import { QApplication } from '../../QtGui/QApplication'; import { CacheTestQObject } from './CacheTestQObject'; +import { wrapperCache } from '../WrapperCache'; describe('WrapperCache using CacheTestQObject', () => { const qApp = QApplication.instance(); qApp.setQuitOnLastWindowClosed(true); it('Cached foo', () => { + wrapperCache._flush(); const a = new CacheTestQObject(); expect(a).not.toBeNull(); @@ -13,11 +16,12 @@ describe('WrapperCache using CacheTestQObject', () => { expect(foo).not.toBeNull(); const foo2 = a.foo(); - expect(foo).toBe(foo2); expect(foo.native.__id__()).toBe(foo2.native.__id__()); + expect(foo).toBe(foo2); }); it('clearFoo() and wrapper expiration', () => { + wrapperCache._flush(); const a = new CacheTestQObject(); const foo = a.foo(); a.clearFoo(); @@ -25,6 +29,7 @@ describe('WrapperCache using CacheTestQObject', () => { }); it('clearFoo() and new wrapper', () => { + wrapperCache._flush(); const a = new CacheTestQObject(); const foo = a.foo(); const fooId = foo.native.__id__(); @@ -37,11 +42,56 @@ describe('WrapperCache using CacheTestQObject', () => { }); it('Cached foo and bar', () => { + wrapperCache._flush(); const a = new CacheTestQObject(); const foo = a.foo(); const bar = a.bar(); expect(foo).not.toEqual(bar); expect(foo.native.__id__()).not.toEqual(bar.native.__id__()); }); + + it('QObject.parent() can be null', () => { + wrapperCache._flush(); + const a = new QObject(); + expect(a.parent()).toBeNull(); + }); + + it('QObject.parent() === QObject.parent()', () => { + wrapperCache._flush(); + const a = new QObject(); + const b = new QObject(a); + expect(a.native.__id__()).toEqual(b.parent().native.__id__()); + expect(a).toEqual(b.parent()); + (a)['magic'] = true; + expect((b.parent())['magic']).toBe(true); + }); + + it('QObject.delete() clears the native field', () => { + wrapperCache._flush(); + const a = new QObject(); + a.delete(); + expect(a.native).toBeNull(); + }); + + it('QObject.delete() clears chains of QObjects and their native field', () => { + wrapperCache._flush(); + const a = new QObject(); + const b = new QObject(a); + a.delete(); + expect(a.native).toBeNull(); + expect(b.native).toBeNull(); + }); + + it('Object.children()', () => { + wrapperCache._flush(); + const parent = new QObject(); + const kid1 = new QObject(parent); + const kid2 = new QObject(parent); + const allKids = parent.children(); + expect(allKids.length).toBe(2); + expect(allKids[0]).toEqual(kid1); + expect(allKids[1]).toEqual(kid2); + }); + qApp.quit(); }); diff --git a/src/lib/utils/helpers.ts b/src/lib/utils/helpers.ts index b786850c2..8c5022c8a 100644 --- a/src/lib/utils/helpers.ts +++ b/src/lib/utils/helpers.ts @@ -2,9 +2,9 @@ import { NativeElement } from '../core/Component'; import addon from './addon'; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export function checkIfNativeElement(arg: any): boolean { +export function checkIfNativeElement(arg: any): arg is NativeElement { const nativeArg = arg as NativeElement; - return typeof nativeArg === 'object' && nativeArg.type === 'native'; + return nativeArg != null && typeof nativeArg === 'object' && nativeArg.type === 'native'; } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types diff --git a/tsconfig.json b/tsconfig.json index 5d65b5e7a..6abc59d20 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,12 @@ { "compilerOptions": { - "target": "ES2015", + "target": "ES2021", "module": "commonjs", "declaration": true, "sourceMap": false, "outDir": "./dist", "strict": true, + "strictNullChecks": false, "moduleResolution": "node", "esModuleInterop": true, "resolveJsonModule": true diff --git a/website/docs/development/getting-started.md b/website/docs/development/getting-started.md index fbe60e723..a7de3d28d 100644 --- a/website/docs/development/getting-started.md +++ b/website/docs/development/getting-started.md @@ -85,7 +85,7 @@ The idea is : 2. Then we will use NLabel and wrap it using NAPI and export it to JS side. This is what qlabel_wrap does. **NLabel**: Since NLabel has inherited from QLabel we can treat is as QLabel with extra methods and properties. Primary reason to extend QLabel to create NLabel is to add support for Event listeners and CSS styling using Flex. -So if you take a look at NLabel you will see, it inherits from QLabel and NodeWidget. NodeWidget inturn inherits from YogaWidget and EventWidget. Event widget adds event handling support. YogaWidget is a class that contains the magic that enables a regular Qt Widget to have Yoga node. A Yoga node is an instance used by yoga library to calculate a widgets position on the screen. Yoga is a library that will layout the widget on the screen. To do so we will specify the flex properties like alignitems, justify content, margin, paddings etc on the Yoga node of the widget. Apart from adding yoga node, YogaWidget adds support for specifying those yoga properties via Qt's stylesheet. (This is done by using Q_PROPERTY). To make this work we need to use something called as Q_OBJECT inside the class which is a C++ macro. Q_OBJECT will be expanded to relevant code by the compiler. In Qt whenever we add Q_OBJECT to a header file, we need to use a pre compiler called Qt MOC (Meta Object Compiler). The way we use it is +So if you take a look at NLabel you will see, it inherits from QLabel and QWidget. QWidget in turn inherits from YogaWidget and EventWidget. Event widget adds event handling support. YogaWidget is a class that contains the magic that enables a regular Qt Widget to have Yoga node. A Yoga node is an instance used by yoga library to calculate a widgets position on the screen. Yoga is a library that will layout the widget on the screen. To do so we will specify the flex properties like alignitems, justify content, margin, paddings etc on the Yoga node of the widget. Apart from adding yoga node, YogaWidget adds support for specifying those yoga properties via Qt's stylesheet. (This is done by using Q_PROPERTY). To make this work we need to use something called as Q_OBJECT inside the class which is a C++ macro. Q_OBJECT will be expanded to relevant code by the compiler. In Qt whenever we add Q_OBJECT to a header file, we need to use a pre compiler called Qt MOC (Meta Object Compiler). The way we use it is ``` moc headername.h -o headername_moc.cpp --include // example : ../../core/YogaWidget/yogawidget.h diff --git a/website/docs/development/signal_and_event_handling.md b/website/docs/development/signal_and_event_handling.md index 93fe8e4ba..363b00b16 100644 --- a/website/docs/development/signal_and_event_handling.md +++ b/website/docs/development/signal_and_event_handling.md @@ -52,7 +52,7 @@ Example: ```js import addon from '../../core/addon'; -import { NodeWidget } from '../../QtGui/QWidget'; +import { QWidget } from '../../QtGui/QWidget'; import { BaseWidgetEvents } from '../../core/EventWidget'; export const QPushButtonEvents = Object.freeze({ @@ -63,10 +63,9 @@ export const QPushButtonEvents = Object.freeze({ toggled: 'toggled', }); -export class QPushButton extends NodeWidget { - native: NativeElement; - constructor(parent?: NodeWidget) { - let native; +export class QPushButton extends QWidget { + constructor(parent?: QWidget) { + let native: NativeElement; if (parent) { native = new addon.QPushButton(parent.native); } else { @@ -74,7 +73,6 @@ export class QPushButton extends NodeWidget { } super(native); this.parent = parent; - this.native = native; } setText(text: string | number) { diff --git a/website/docs/development/wrapper_cache.drawio.svg b/website/docs/development/wrapper_cache.drawio.svg new file mode 100644 index 000000000..bc6303e04 --- /dev/null +++ b/website/docs/development/wrapper_cache.drawio.svg @@ -0,0 +1,451 @@ + + + + + + + +
+
+
+ + QScreen + +
+ <<cpp>> +
+
+
+
+ + QScreen... + +
+
+ + + + + + +
+
+
+ + QClipboard + +
+ <<ts>> +
+
+
+
+ + QClipboard... + +
+
+ + + + + + +
+
+
+ + QScreen + +
+ <<ts>> +
+
+
+
+ + QScreen... + +
+
+ + + + + + + + +
+
+
+ + WrapperCache + +
+ <<ts>> +
+
+
+
+ + WrapperCache... + +
+
+ + + + +
+
+
+ + WrapperCache + +
+ <<cpp>> +
+
+
+
+ + WrapperCache... + +
+
+ + + + + +
+
+
+ QPointer instance +
+
+
+
+ + QPointer instance + +
+
+ + + + +
+
+
+ + QScreenWrap + +
+ <<Napi>> +
+
+
+
+ + QScreenWrap... + +
+
+ + + + + + +
+
+
+ + JS / TS + +
+
+
+
+ + JS / TS + +
+
+ + + + +
+
+
+ + V8 bindings (C++) + +
+
+
+
+ + V8 bindings (... + +
+
+ + + + +
+
+
+ + Qt + +
+
+
+
+ + Qt + +
+
+ + + + +
+
+
+ + "Wrapper Keep Alive" case (Instance diagram) + +
+
+
+
+ + "Wrapper Keep Alive" case (Insta... + +
+
+ + + + + +
+
+
+ QPointer instance +
+
+
+
+ + QPointer instance + +
+
+ + + + +
+
+
+ + QClipboardWrap + +
+ <<Napi>> +
+
+
+
+ + QClipboardWrap... + +
+
+ + + + +
+
+
+ + QClipboard + +
+ <<cpp>> +
+
+
+
+ + QClipboard... + +
+
+ + + + +
+
+
+ Map QObject +
+ <-> +
+ Cpp Wrapper +
+
+
+
+ + Map QObject... + +
+
+ + + + + + + + +
+
+
+ Map QObject +
+ <-> +
+ Cpp Wrapper +
+
+
+
+ + Map QObject... + +
+
+ + + + + + + + + +
+
+
+ QObject destroyed +
+ signal +
+
+
+
+ + QObject destroyed... + +
+
+ + + + + +
+
+
+ Object destroyed +
+ callback +
+
+
+
+ + Object destroyed... + +
+
+ + + + + +
+
+
+ QObject destroyed +
+ signal +
+
+
+
+ + QObject destroyed... + +
+
+ + + + + + + + +
+
+
+ JS strong reference +
+
+
+
+ + JS strong reference + +
+
+ + + + +
+
+
+ Cpp reference/pointer +
+
+
+
+ + Cpp reference/pointer + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/website/docs/development/wrapper_caching.md b/website/docs/development/wrapper_caching.md new file mode 100644 index 000000000..30a98da9b --- /dev/null +++ b/website/docs/development/wrapper_caching.md @@ -0,0 +1,30 @@ +# Wrapper caching and memory management + + + +## "Wrapper Keep Alive" case + +The goal of the "Wrapper Keep Alive" case is to maintain and "keep alive" wrappers for the life-time of a `QObject` whose life-cycle is under complete control by Qt itself. i.e. Qt may expose the object, but it creates the instance itself and also destroys it later on. This situation applies to `QScreen` and `QClipboard` for example. A common use case for keeping a wrapper alive is to receive and relay signals from a `QObject` back to JS during the object's life-time. + +![Wrapper cache diagram](wrapper_cache.drawio.svg) + +The WrapperCache (C++) holds strong JS references to the Napi wrapper instances. This keeps them alive. When the core QObject is destroyed the "destroyed" signal is emitted and received by WrapperCache (C++). It then removes the Napi wrapper from its cache and uses a callback function to inform the WrapperCache (JS) about the destruction. WrapperCache (JS) can also perform clean up and null out references to the Napi wrappers. If someone then tries to use a JS side wrapper, then will get a JS side null pointer exception with stacktrace. + +**Life-cycle Sequence** + +This sequence diagram shows the events when the application calls `QWindow.screen()` to fetch the `QScreen` for the window. The `QScreen` instance is fully created and managed by Qt. Here you can see how the wrapper creation interacts with the JS and C++ side cache classes. You can also see how the wrappers are gracefully shutdown when the core Qt object is destroyed. Any JS side use of the destroyed wrapper / Qt object results in a neat JS side null pointer exception. This is much better than null pointer segfault on the C++ side. + +![Wrapper keep alive sequence diagram](wrapper_keep_alive_seq.png) + + +## "Wrapper Recycle" case + +A related use case is where we want to "recycle" wrappers and ensure that for a QObject we only have one coresponding JS wrapper active at the same time. For example, repeated calls to `QObject.parent()` should return the same value/object. + +Another goal of this use case is to ensure that the unexpected destruction of the underlying QObject is handled in a more graceful and helpful way than just segfaulting the whole application. This requires the tracking of the QObject via its "destroy" signal and using that to communicate back to JS what has happened. + +The object creation sequence runs quite differently than in the "Wrapper Keep Alive" case because the application initiates the creation of the object and NodeGui then creates the different wrappers on the JS and C++ sides. + +![Wrapper recycle sequence diagram](wrapper_recycle_seq.png) + +The destruction sequence in the case of the C++ object being destroyed is basically the same as the "Wrapper Keep Alive" case. \ No newline at end of file diff --git a/website/docs/development/wrapper_keep_alive_seq.png b/website/docs/development/wrapper_keep_alive_seq.png new file mode 100644 index 000000000..b39fa1600 Binary files /dev/null and b/website/docs/development/wrapper_keep_alive_seq.png differ diff --git a/website/docs/development/wrapper_keep_alive_seq.puml b/website/docs/development/wrapper_keep_alive_seq.puml new file mode 100644 index 000000000..e0711b941 --- /dev/null +++ b/website/docs/development/wrapper_keep_alive_seq.puml @@ -0,0 +1,97 @@ +@startuml + +skinparam object { + backgroundColor White + borderColor Black + arrowColor Black +} + +skinparam note { + backgroundColor LightYellow + borderColor Black +} + +hide footbox + +title Lifecycle of a Wrapper + +participant App as app << ts >> #E3C800 +participant "QWindow.ts" as qwindowts << ts>> #E3C800 +participant "QScreen.ts" as qscreents << ts>> #E3C800 +participant "WrapperCache" as wrappercachets << ts>> #E3C800 +participant "QWindowWrap" as qwindowwrap << cpp >> +participant "QScreenWrap" as qscreenwrap << cpp >> +participant "WrapperCache" as wrappercachecpp << cpp >> +participant "QWindow" as qwindow << cpp >> #D5E8D4 +participant "QScreen" as qscreen << cpp >> #D5E8D4 + + +== Wrapper construction == + +activate qscreen +app --> qwindowts: QWindow.screen() +qwindowts --> qwindowwrap: screen() +qwindowwrap --> qwindow: screen() +qwindow --> qwindowwrap: QScreen instance +qwindowwrap --> wrappercachecpp: get() +note over wrappercachecpp + Checks wrapper cache +end note +wrappercachecpp --> qscreenwrap: new() +activate qscreenwrap +qscreenwrap --> wrappercachecpp: QScreenWrap instance +wrappercachecpp --> qscreen: connect to destroy signal +wrappercachecpp --> qwindowwrap: QScreenWrap instance +qwindowwrap --> qwindowts: QScreenWrap instance +qwindowts --> wrappercachets: get(QScreenWrap instance) +wrappercachets --> qscreents: new(QScreenWrap instance) +activate qscreents +qscreents --> wrappercachets: QScreen.ts instance +wrappercachets --> qwindowts: QScreen.ts instance +qwindowts --> app: QScreen.ts instance + +note across: The app can use the QScreen wrap and run. + +== Qt object destruction == +note over qscreen + Qt decides to destroy + the QScreen instance +end note +qscreen --> wrappercachecpp: destroy signal +destroy qscreen +wrappercachecpp --> wrappercachets: destroy callback + +note over wrappercachecpp + WrapperCache removes + references to the wrapper + and Qt object from cache. +end note + +wrappercachets --> qscreents: null the ref to QScreenWrap +note over wrappercachets + WrapperCache.ts removes + references to the QScreen.ts + instance and QScreenWrap +end note + +destroy qscreenwrap +note over qscreenwrap + V8's GC will destroy + this automatically +end note + +app --> qscreents +note over app + Any use of QScreen.ts + will hit the null reference + and throw a JS exception. +end note +app --> qscreents + +destroy qscreents +note over qscreents + V8's GC will destroy + this automatically +end note + +@enduml diff --git a/website/docs/development/wrapper_recycle_seq.png b/website/docs/development/wrapper_recycle_seq.png new file mode 100644 index 000000000..fbf26d5a3 Binary files /dev/null and b/website/docs/development/wrapper_recycle_seq.png differ diff --git a/website/docs/development/wrapper_recycle_seq.puml b/website/docs/development/wrapper_recycle_seq.puml new file mode 100644 index 000000000..8f789468e --- /dev/null +++ b/website/docs/development/wrapper_recycle_seq.puml @@ -0,0 +1,46 @@ +@startuml + +skinparam object { + backgroundColor White + borderColor Black + arrowColor Black +} + +skinparam note { + backgroundColor LightYellow + borderColor Black +} + +hide footbox + +title Wrapper Recycle + +participant App as app << ts >> #E3C800 +participant "QObject.ts" as qobjectts << ts>> #E3C800 + +participant "WrapperCache" as wrappercachets << ts>> #E3C800 +participant "QObjectWrap" as qobjectwrap << cpp >> +participant "WrapperCache" as wrappercachecpp << cpp >> +participant "QObject" as qobject << cpp >> #D5E8D4 + +== Wrapper construction == + +app --> qobjectts: new() +activate qobjectts +qobjectts --> qobjectwrap: new() +activate qobjectwrap +qobjectwrap --> qobject: new() +activate qobject +qobject --> qobjectwrap: QObject instance + +qobjectwrap --> wrappercachecpp: store(QObject, QObjectWrap) +wrappercachecpp --> qobject: connect to destroy signal + +qobjectwrap --> qobjectts: QOjectWrap instance +qobjectts --> wrappercachets: store(QObject.ts) +qobjectts --> app: QObject instance + + +note across: The app can use the QObject and run. + +@enduml diff --git a/website/docs/guides/understanding-memory.md b/website/docs/guides/understanding-memory.md new file mode 100644 index 000000000..a78344410 --- /dev/null +++ b/website/docs/guides/understanding-memory.md @@ -0,0 +1,81 @@ +--- +sidebar_label: Understanding Memory +title: Understanding Memory +--- + +NodeGui allows you to use a library designed for C++ (Qt) from the JavaScript language. C++ and JavaScript have very different approaches for managing the memory of objects. C++ expects the programmer to manually create objects and later destroy them to free up their memory. It is the job of the programmer to destroy objects at the right time and when it is safe. JavaScript uses a Garbage Collector which detects when an object is longer being used and then automatically destroys it, freeing up its memory. Most of the time NodeGui can coordinate these two systems in a way which is mostly transparent to the JavaScript developer. However, there are situations where it is necessary to have some understanding of what is happening being in the scenes. + +## Qt's QObject Memory Management + +Much of Qt is based around the `QObject` class. It is the base class of many Qt classes including most of the widgets exposed by NodeGui. `QObject` provides many useful features on top of C++ for working with objects. It has support for easier memory management. This system is built around the idea of object ownership and trees of objects which can be freed as a group. Each `QObject` can participate in a tree of objects. Each `QObject` has a list of child objects, and may also have a parent `QObject`. When a `QObject` is destroyed, all of its attached child objects are destroyed with it. + +Trees of objects is a good model for user interfaces which are also structured in hierarchies. Qt's widgets are built on top of `QObject` and its object trees. The same object tree is used by widgets for memory management and UI layout / structuring. + +`QObject` contains a number of methods for examining this tree of objects: + +* `parent()` - Get the parent of an object, or `null`. +* `setParent()` - Set the parent of an object. +* `children()` - Get a list of the child objects belonging to this object. + + +## JavaScript Wrappers + +Node runs on the V8 JavaScript engine. It can't directly work with C++ object like those from Qt. To bridge the gap between V8 and Qt, NodeGui creates "wrapper" C++ and JavaScript objects which can translate between the two worlds. A JavaScript object "wrapper" will wrap a corresponding Qt object in C++. As a JavaScript developer you don't have to pay much attention to this. It happens automatically in the background. + +## QObject End of Life + +There are two ways in which a `QObject` can be destroyed, starting from either the C++ side or JavaScript side. + +The JavaScript side is the most common. If the Garbage Collector discovers that a wrapper is no longer being referenced, then it will destroy the wrapper. When the C++ side of the wrapper is destroyed, it will also destroy its corresponding Qt object *if that object is not part of an object tree*. In other words, if the `QObject` has a parent object set, then it will not be destroyed and NodeGui assumes that the responsibility for destroying and cleaning up that object belongs to the parent object and the Qt `QObject` memory system. + +When a `QObject` is destroyed from the C++ side, then NodeGui detects this and sets the reference to the object itside the JavaScript wrapper object, to `null`. If any attempt is made to use the JS wrapper object, then it will hit the `null` and throw an exception. + +## Problems with Unexpected Object Destruction + +A NodeGui object suddenly throws an exception regarding a `null` if you try to use a method on it. This situation can happen if underlying C++ Qt object is destroyed and afterwards you still try to use it via its wrapper. + +The most common way this can happen is if a `QObject` is part of an object tree and one of its parent objects is destroyed which then also destroys the rest of the tree. If you don't want an object to be destroyed with its parent object, then you need to remove it from the tree before the parent is destroyed. Calling `setParent(null)` on the object is enough to disconnect it from its parent. + +## Debugging + +NodeGui provides some tools to help track the lifecycle of objects and figure out why your application isn't running the way you expect. + +Each `QObject` instance, and subclass also, has an ID to identify it. The method `QObject._id()` can be used to fetch the ID for an object. The ID is valid for the lifetime of the object, although it could be reused once this object is destroyed. + +NodeGui manages the lifecycle of objects and it is possible to turn on logging regarding the creation and destruction of objects. The following functions take a boolean to turn this logging on or off: + +* `setLogDestroyQObject()` - Turn logging of object destruction on/off. +* `setLogCreateQObject()` - Turn logging of object creation on/off. + +These log messages contain the ID of the object as returned from `QObject._id()`. + +A typical usage of this logging would be to narrow down the exact place where an object is being unexpectedly destroyed: + +```js +const { setLogDestroyQObject, setLogCreateQObject } = require('@nodegui/nodegui'); + +setLogCreateQObject(true); +setLogDestroyQObject(true); + +const myWidget = new QWidget(); +console.log(`myWidget has ID: ${myWidget._id()}.`); + +// ... +// ... Code which somehow triggers the destruction of myWidget. +// ... +console.log(`Reached crash point.`); +const size = myWidget.size(); +// ^ line which triggers an exception because the object is destroyed. +``` + +The logging output from the application might be: + +``` +NodeGui: Created C++ object with ID: 17533348349032. +myWidget has ID: 17533348349032. +NodeGui: Destroyed C++ object with ID: 17533348349032. +Reached crash point. +Uncaught TypeError: Cannot read property 'size' of null +``` + +More logging could be added to narrow down which section of code triggers the destruction of the object.