From b4c9c0d51be90c4b1c0acf6b37dc92e5923871ed Mon Sep 17 00:00:00 2001 From: Simon Edwards Date: Sat, 12 Feb 2022 11:42:27 +0100 Subject: [PATCH] Add `QWidget.mapTo()` and `QWidget.mapFrom()` --- .../nodegui/QtWidgets/QWidget/qwidget_macro.h | 33 +++++++++++++++++++ .../nodegui/QtWidgets/QWidget/qwidget_wrap.h | 30 ++++++++--------- src/lib/QtWidgets/QWidget.ts | 8 +++-- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h index 13bd0f449..41cad884f 100644 --- a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h @@ -58,6 +58,19 @@ bool hasClosed = this->instance->close(); \ return Napi::Boolean::New(env, hasClosed); \ } \ + Napi::Value mapFrom(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::Object widgetObject = info[0].As(); \ + NodeWidgetWrap* widgetWrap = \ + Napi::ObjectWrap::Unwrap(widgetObject); \ + Napi::Object posObject = info[1].As(); \ + QPointWrap* posWrap = Napi::ObjectWrap::Unwrap(posObject); \ + QPoint pt = this->instance->mapFrom(widgetWrap->getInternalInstance(), \ + *posWrap->getInternalInstance()); \ + auto instance = QPointWrap::constructor.New( \ + {Napi::External::New(env, new QPoint(pt.x(), pt.y()))}); \ + return instance; \ + } \ Napi::Value mapFromGlobal(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ Napi::Object posObject = info[0].As(); \ @@ -96,6 +109,19 @@ {Napi::External::New(env, new QPoint(pt.x(), pt.y()))}); \ return instance; \ } \ + Napi::Value mapTo(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::Object widgetObject = info[0].As(); \ + NodeWidgetWrap* widgetWrap = \ + Napi::ObjectWrap::Unwrap(widgetObject); \ + Napi::Object posObject = info[1].As(); \ + QPointWrap* posWrap = Napi::ObjectWrap::Unwrap(posObject); \ + QPoint pt = this->instance->mapTo(widgetWrap->getInternalInstance(), \ + *posWrap->getInternalInstance()); \ + auto instance = QPointWrap::constructor.New( \ + {Napi::External::New(env, new QPoint(pt.x(), pt.y()))}); \ + return instance; \ + } \ Napi::Value setLayout(const Napi::CallbackInfo& info) { \ Napi::Env env = info.Env(); \ Napi::Object layoutObject = info[0].As(); \ @@ -550,10 +576,12 @@ InstanceMethod("show", &WidgetWrapName::show), \ InstanceMethod("resize", &WidgetWrapName::resize), \ InstanceMethod("close", &WidgetWrapName::close), \ + InstanceMethod("mapFrom", &WidgetWrapName::mapFrom), \ InstanceMethod("mapFromGlobal", &WidgetWrapName::mapFromGlobal), \ InstanceMethod("mapFromParent", &WidgetWrapName::mapFromParent), \ InstanceMethod("mapToGlobal", &WidgetWrapName::mapToGlobal), \ InstanceMethod("mapToParent", &WidgetWrapName::mapToParent), \ + InstanceMethod("mapTo", &WidgetWrapName::mapTo), \ InstanceMethod("setLayout", &WidgetWrapName::setLayout), \ InstanceMethod("setStyleSheet", &WidgetWrapName::setStyleSheet), \ InstanceMethod("setCursor", &WidgetWrapName::setCursor), \ @@ -661,3 +689,8 @@ }); #endif // QWIDGET_SIGNALS + +#include "QtWidgets/QWidget/qwidget_wrap.h" +// ^ Yes, this is weird due to the mutual dependency between the methods macro +// and `NodeWidgetWrap`. Having this here makes everything work regardless if +// `qwidget_wrap.h` is included first or `qwidget_macro.h`. diff --git a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_wrap.h index 5d370ff11..0befba43a 100644 --- a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_wrap.h @@ -8,21 +8,6 @@ #include "QtWidgets/QWidget/qwidget_macro.h" #include "nwidget.hpp" -class DLL_EXPORT QWidgetWrap : public Napi::ObjectWrap { - QWIDGET_WRAPPED_METHODS_DECLARATION - private: - QPointer instance; - - public: - static Napi::Object init(Napi::Env env, Napi::Object exports); - QWidgetWrap(const Napi::CallbackInfo& info); - ~QWidgetWrap(); - NWidget* getInternalInstance(); - // class constructor - static Napi::FunctionReference constructor; - // wrapped methods -}; - // NodeWidgetWrap is exactly like QWidgetWrap but it is used only to unwrap any // N to QWidget* class DLL_EXPORT NodeWidgetWrap : public Napi::ObjectWrap { @@ -38,3 +23,18 @@ class DLL_EXPORT NodeWidgetWrap : public Napi::ObjectWrap { static Napi::FunctionReference constructor; // wrapped methods }; + +class DLL_EXPORT QWidgetWrap : public Napi::ObjectWrap { + QWIDGET_WRAPPED_METHODS_DECLARATION + private: + QPointer instance; + + public: + static Napi::Object init(Napi::Env env, Napi::Object exports); + QWidgetWrap(const Napi::CallbackInfo& info); + ~QWidgetWrap(); + NWidget* getInternalInstance(); + // class constructor + static Napi::FunctionReference constructor; + // wrapped methods +}; diff --git a/src/lib/QtWidgets/QWidget.ts b/src/lib/QtWidgets/QWidget.ts index 8b7aea320..ecae15647 100644 --- a/src/lib/QtWidgets/QWidget.ts +++ b/src/lib/QtWidgets/QWidget.ts @@ -219,20 +219,24 @@ export abstract class NodeWidget extends YogaWid // TODO: QLayout * layout() const // TODO: Qt::LayoutDirection layoutDirection() const // TODO: QLocale locale() const - // TODO: QPoint mapFrom(const QWidget *parent, const QPoint &pos) const + mapFrom(parent: QWidget, pos: QPoint): QPoint { + return new QPoint(this.native.mapFrom(parent.native, pos.native)); + } mapFromGlobal(pos: QPoint): QPoint { return new QPoint(this.native.mapFromGlobal(pos.native)); } mapFromParent(pos: QPoint): QPoint { return new QPoint(this.native.mapFromParent(pos.native)); } - // TODO: QPoint mapTo(const QWidget *parent, const QPoint &pos) const mapToGlobal(pos: QPoint): QPoint { return new QPoint(this.native.mapToGlobal(pos.native)); } mapToParent(pos: QPoint): QPoint { return new QPoint(this.native.mapToParent(pos.native)); } + mapTo(parent: QWidget, pos: QPoint): QPoint { + return new QPoint(this.native.mapTo(parent.native, pos.native)); + } // TODO: QRegion mask() const maximumHeight(): number { return this.property('maximumHeight').toInt();