From 3694215fcbd1ed72a75139fa6ab40c7b96b3b206 Mon Sep 17 00:00:00 2001 From: Atul R Date: Sun, 4 Aug 2019 08:49:33 +0200 Subject: [PATCH] Adds type = native property to all wrapped elements in the nodegui world. Also fixes scaled QPixmap --- src/cpp/QtGui/QApplication/qapplication_wrap.cpp | 11 ++++++----- src/cpp/QtGui/QEvent/QKeyEvent/qkeyevent_wrap.cpp | 5 +++-- src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp | 2 ++ src/cpp/QtGui/QPixmap/qpixmap_wrap.h | 1 + src/cpp/QtGui/QWidget/qwidget_macro.h | 2 ++ src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.cpp | 4 +++- src/cpp/QtWidgets/QLayout/qlayout_wrap.cpp | 5 ++++- src/cpp/core/Component/component_macro.h | 7 +++++++ src/cpp/core/FlexLayout/flexlayout_wrap.cpp | 2 ++ src/lib/QtGui/QPixmap/index.ts | 13 +++++++++---- src/lib/core/Component/index.ts | 2 +- 11 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 src/cpp/core/Component/component_macro.h diff --git a/src/cpp/QtGui/QApplication/qapplication_wrap.cpp b/src/cpp/QtGui/QApplication/qapplication_wrap.cpp index 2f5c26a9f..2b65eff69 100644 --- a/src/cpp/QtGui/QApplication/qapplication_wrap.cpp +++ b/src/cpp/QtGui/QApplication/qapplication_wrap.cpp @@ -1,4 +1,5 @@ #include "qapplication_wrap.h" +#include "src/cpp/core/Component/component_macro.h" Napi::FunctionReference QApplicationWrap::constructor; int QApplicationWrap::argc = 0; @@ -8,11 +9,11 @@ Napi::Object QApplicationWrap::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); char CLASSNAME[] = "QApplication"; - Napi::Function func = - DefineClass(env, CLASSNAME, - {InstanceMethod("processEvents", &QApplicationWrap::processEvents), - InstanceMethod("exec", &QApplicationWrap::exec)}); - + Napi::Function func = DefineClass(env, CLASSNAME, { + InstanceMethod("processEvents", &QApplicationWrap::processEvents), + InstanceMethod("exec", &QApplicationWrap::exec), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE + }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); return exports; diff --git a/src/cpp/QtGui/QEvent/QKeyEvent/qkeyevent_wrap.cpp b/src/cpp/QtGui/QEvent/QKeyEvent/qkeyevent_wrap.cpp index b8b2452c6..06fec648b 100644 --- a/src/cpp/QtGui/QEvent/QKeyEvent/qkeyevent_wrap.cpp +++ b/src/cpp/QtGui/QEvent/QKeyEvent/qkeyevent_wrap.cpp @@ -2,7 +2,7 @@ #include "src/cpp/Extras/Utils/nutils.h" #include #include "deps/spdlog/spdlog.h" - +#include "src/cpp/core/Component/component_macro.h" Napi::FunctionReference QKeyEventWrap::constructor; @@ -10,7 +10,8 @@ Napi::Object QKeyEventWrap::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); char CLASSNAME[] = "QKeyEvent"; Napi::Function func = DefineClass(env, CLASSNAME, { - InstanceMethod("text", &QKeyEventWrap::text), + InstanceMethod("text", &QKeyEventWrap::text), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp b/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp index 92c93f5ee..1354c21a2 100644 --- a/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp +++ b/src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp @@ -1,5 +1,6 @@ #include "qpixmap_wrap.h" #include "src/cpp/Extras/Utils/nutils.h" +#include "deps/spdlog/spdlog.h" Napi::FunctionReference QPixmapWrap::constructor; @@ -10,6 +11,7 @@ Napi::Object QPixmapWrap::init(Napi::Env env, Napi::Object exports) Napi::Function func = DefineClass(env, CLASSNAME,{ InstanceMethod("load", &QPixmapWrap::load), InstanceMethod("scaled",&QPixmapWrap::scaled), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/cpp/QtGui/QPixmap/qpixmap_wrap.h b/src/cpp/QtGui/QPixmap/qpixmap_wrap.h index dee7982e1..c8ff9efeb 100644 --- a/src/cpp/QtGui/QPixmap/qpixmap_wrap.h +++ b/src/cpp/QtGui/QPixmap/qpixmap_wrap.h @@ -2,6 +2,7 @@ #include #include +#include "src/cpp/core/Component/component_macro.h" class QPixmapWrap : public Napi::ObjectWrap { private: diff --git a/src/cpp/QtGui/QWidget/qwidget_macro.h b/src/cpp/QtGui/QWidget/qwidget_macro.h index 2b581ff58..e009dbd65 100644 --- a/src/cpp/QtGui/QWidget/qwidget_macro.h +++ b/src/cpp/QtGui/QWidget/qwidget_macro.h @@ -3,6 +3,7 @@ #include "src/cpp/QtWidgets/QLayout/qlayout_wrap.h" #include "src/cpp/core/YogaWidget/yogawidget_macro.h" #include "src/cpp/core/Events/eventwidget_macro.h" +#include "src/cpp/core/Component/component_macro.h" /* This macro adds common QWidgets exported methods @@ -128,6 +129,7 @@ Napi::Value update(const Napi::CallbackInfo& info){ \ \ YOGAWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE \ InstanceMethod("show", &WidgetWrapName::show), \ InstanceMethod("resize",&WidgetWrapName::resize), \ InstanceMethod("close",&WidgetWrapName::close), \ diff --git a/src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.cpp b/src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.cpp index 3b2b03580..75841aff5 100644 --- a/src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.cpp +++ b/src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.cpp @@ -1,6 +1,7 @@ #include "qgridlayout_wrap.h" #include "src/cpp/QtGui/QWidget/qwidget_wrap.h" #include "src/cpp/Extras/Utils/nutils.h" +#include "src/cpp/core/Component/component_macro.h" Napi::FunctionReference QGridLayoutWrap::constructor; @@ -8,7 +9,8 @@ Napi::Object QGridLayoutWrap::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); char CLASSNAME[] = "QGridLayout"; Napi::Function func = DefineClass(env, CLASSNAME, { - InstanceMethod("addWidget", &QGridLayoutWrap::addWidget) + InstanceMethod("addWidget", &QGridLayoutWrap::addWidget), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/cpp/QtWidgets/QLayout/qlayout_wrap.cpp b/src/cpp/QtWidgets/QLayout/qlayout_wrap.cpp index c453439ef..e18f2f1df 100644 --- a/src/cpp/QtWidgets/QLayout/qlayout_wrap.cpp +++ b/src/cpp/QtWidgets/QLayout/qlayout_wrap.cpp @@ -1,10 +1,13 @@ #include "qlayout_wrap.h" +#include "src/cpp/core/Component/component_macro.h" Napi::FunctionReference QLayoutWrap::constructor; void QLayoutWrap::init(Napi::Env env) { char CLASSNAME[] = "QLayout"; - Napi::Function func = DefineClass(env, CLASSNAME, {}); + Napi::Function func = DefineClass(env, CLASSNAME, { + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE + }); constructor = Napi::Persistent(func); } diff --git a/src/cpp/core/Component/component_macro.h b/src/cpp/core/Component/component_macro.h new file mode 100644 index 000000000..1c641f69f --- /dev/null +++ b/src/cpp/core/Component/component_macro.h @@ -0,0 +1,7 @@ + +#ifndef COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE +#define COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE \ +\ + InstanceValue("type", Napi::String::New(env, "native")), \ + +#endif // COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE diff --git a/src/cpp/core/FlexLayout/flexlayout_wrap.cpp b/src/cpp/core/FlexLayout/flexlayout_wrap.cpp index 6c14d0d99..580adf562 100644 --- a/src/cpp/core/FlexLayout/flexlayout_wrap.cpp +++ b/src/cpp/core/FlexLayout/flexlayout_wrap.cpp @@ -1,6 +1,7 @@ #include "flexlayout_wrap.h" #include "src/cpp/QtGui/QWidget/qwidget_wrap.h" #include "src/cpp/Extras/Utils/nutils.h" +#include "src/cpp/core/Component/component_macro.h" Napi::FunctionReference FlexLayoutWrap::constructor; @@ -12,6 +13,7 @@ Napi::Object FlexLayoutWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("insertChildBefore", &FlexLayoutWrap::insertChildBefore), InstanceMethod("removeWidget", &FlexLayoutWrap::removeWidget), InstanceMethod("setFlexNode", &FlexLayoutWrap::setFlexNode), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); diff --git a/src/lib/QtGui/QPixmap/index.ts b/src/lib/QtGui/QPixmap/index.ts index 2b18362c7..a89432669 100644 --- a/src/lib/QtGui/QPixmap/index.ts +++ b/src/lib/QtGui/QPixmap/index.ts @@ -1,12 +1,16 @@ import addon from "../../core/addon"; import { Component, NativeElement } from "../../core/Component"; +type arg = string | NativeElement; export class QPixmap extends Component { native: NativeElement; - constructor(imageUrl?: string) { + constructor(arg?: arg) { super(); - if (imageUrl) { + if (typeof arg === "string") { + const imageUrl = arg; this.native = new addon.QPixmap(imageUrl); + } else if ((arg as NativeElement).type === "native") { + this.native = arg as NativeElement; } else { this.native = new addon.QPixmap(); } @@ -14,7 +18,8 @@ export class QPixmap extends Component { load = (imageUrl: string) => { return this.native.load(imageUrl); }; - scaled = (width: number, height: number) => { - return this.native.scaled(width, height); + scaled = (width: number, height: number): QPixmap => { + const nativePixmap = this.native.scaled(width, height); + return new QPixmap(nativePixmap); }; } diff --git a/src/lib/core/Component/index.ts b/src/lib/core/Component/index.ts index 2f16c8b14..4d3683cee 100644 --- a/src/lib/core/Component/index.ts +++ b/src/lib/core/Component/index.ts @@ -1,4 +1,4 @@ -export type NativeElement = any; +export type NativeElement = { type: "native"; [key: string]: any }; export abstract class Component { protected children = new Set(); protected parent?: Component;