From 493a61de1b23c1fd20eb31a5a7470e7ca402aa37 Mon Sep 17 00:00:00 2001 From: Atul R Date: Sun, 11 Aug 2019 20:39:17 +0200 Subject: [PATCH] Adds QApplication methods --- docs/README.md | 1 + docs/api/QApplication.md | 56 +++++++++++++++++++ .../QtGui/QApplication/qapplication_wrap.cpp | 39 ++++++++++++- .../QtGui/QApplication/qapplication_wrap.h | 5 ++ src/cpp/core/YogaWidget/yogawidget.cpp | 8 +-- src/lib/QtGui/QApplication/index.ts | 28 ++++++++-- src/lib/QtGui/QPixmap/index.ts | 7 +-- src/lib/utils.ts | 6 ++ 8 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 docs/api/QApplication.md create mode 100644 src/lib/utils.ts diff --git a/docs/README.md b/docs/README.md index 6bf3ea78d..c99d5b6cb 100644 --- a/docs/README.md +++ b/docs/README.md @@ -40,6 +40,7 @@ ### Modules from NodeGui: +- [QApplication (Application)](api/QApplication.md) - [QMainWindow (Window)](api/QMainWindow.md) - [QWidget (View)](api/QWidget.md) - [QLabel (Text/Image)](api/QLabel.md) diff --git a/docs/api/QApplication.md b/docs/api/QApplication.md new file mode 100644 index 000000000..907325bf1 --- /dev/null +++ b/docs/api/QApplication.md @@ -0,0 +1,56 @@ +## Class: QApplication + +> QApplication is the root object for the entire application. It manages app level settings. + +**This class is a JS wrapper around Qt's [QApplication class](https://doc.qt.io/qt-5/qapplication.html)** + +The QApplication class manages the GUI application's control flow and main settings. In NodeGui you will never create an instance of it manually. NodeGui's internal runtime `Qode` does it for you on app start. You can access the initialised QApplication though if needed. + +**QApplication inherits from [Component](api/Component.md)** + +### Example + +```javascript +const { QApplication } = require("@nodegui/nodegui"); + +const qApp = QApplication.instance(); +qApp.quit(); +``` + +### Static Methods + +QApplication can access all the static methods defined in [Component](api/Component.md). Additionally it also has the following static methods. + +#### `qApp.instance()` + +Returns the already initialised QApplication instance. It calls the native method [QApplication: instance](https://doc.qt.io/qt-5/qcoreapplication.html#instance). + +### Instance Properties + +QApplication can access all the instance properties defined in [Component](api/Component.md) + +### Instance Methods + +QApplication can access all the instance methods defined in [Component](api/Component.md). Additionally it also has the following instance methods: + +#### `qApp.quit()` + +Quits the entire app. It calls the native method [QApplication: quit](https://doc.qt.io/qt-5/qcoreapplication.html#quit). + +#### `qApp.exit(returnCode)` + +Tells the application to exit with a return code. It calls the native method [QApplication: exit](https://doc.qt.io/qt-5/qcoreapplication.html#exit). + +- `returnCode` number - The exit code while quitting the app. + +#### `qApp.processEvents()` + +Processes all pending events for the calling thread . It calls the native method [QApplication: processEvents](https://doc.qt.io/qt-5/qcoreapplication.html#processEvents). + +#### `qApp.exec()` + +> We will never call this method in NodeGui, since Qode will execute this function for us. It exists for experiments only. + +Enters the main event loop and waits until exit() is called. Returns the value that was passed to exit() (which is 0 if exit() is called via quit()). It calls the native method [QApplication: exec](https://doc.qt.io/qt-5/qcoreapplication.html#exec). + +Returns the exit code after app exits. diff --git a/src/cpp/QtGui/QApplication/qapplication_wrap.cpp b/src/cpp/QtGui/QApplication/qapplication_wrap.cpp index 2b65eff69..da3879008 100644 --- a/src/cpp/QtGui/QApplication/qapplication_wrap.cpp +++ b/src/cpp/QtGui/QApplication/qapplication_wrap.cpp @@ -1,5 +1,6 @@ #include "qapplication_wrap.h" #include "src/cpp/core/Component/component_macro.h" +#include "src/cpp/Extras/Utils/nutils.h" Napi::FunctionReference QApplicationWrap::constructor; int QApplicationWrap::argc = 0; @@ -12,6 +13,8 @@ Napi::Object QApplicationWrap::init(Napi::Env env, Napi::Object exports) Napi::Function func = DefineClass(env, CLASSNAME, { InstanceMethod("processEvents", &QApplicationWrap::processEvents), InstanceMethod("exec", &QApplicationWrap::exec), + InstanceMethod("quit", &QApplicationWrap::quit), + StaticMethod("instance", &StaticQApplicationWrapMethods::instance), COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE }); constructor = Napi::Persistent(func); @@ -24,7 +27,13 @@ QApplicationWrap::QApplicationWrap(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); - this->instance = new QApplication(this->argc, this->argv); + if(info.Length() == 1) { + this->instance = info[0].As>().Data(); + } else if (info.Length() == 0){ + this->instance = new QApplication(this->argc, this->argv); + } else { + extrautils::throwTypeError(env, "Wrong number of arguments"); + } } QApplicationWrap::~QApplicationWrap() @@ -49,6 +58,32 @@ Napi::Value QApplicationWrap::exec(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); - this->instance->exec(); + int exitCode = this->instance->exec(); + return Napi::Number::New(env, exitCode); +} + +Napi::Value QApplicationWrap::quit(const Napi::CallbackInfo& info) +{ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + this->instance->quit(); return env.Null(); } + +Napi::Value QApplicationWrap::exit(const Napi::CallbackInfo& info) +{ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + Napi::Number exitCode = info[0].As(); + this->instance->exit(exitCode.Int32Value()); + return env.Null(); +} + +Napi::Value StaticQApplicationWrapMethods::instance(const Napi::CallbackInfo& info) +{ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QApplication* app = static_cast(QCoreApplication::instance()); + Napi::Object instance = QApplicationWrap::constructor.New({ Napi::External::New(env, app) }); + return instance; +} diff --git a/src/cpp/QtGui/QApplication/qapplication_wrap.h b/src/cpp/QtGui/QApplication/qapplication_wrap.h index ad291a76b..19e5224fe 100644 --- a/src/cpp/QtGui/QApplication/qapplication_wrap.h +++ b/src/cpp/QtGui/QApplication/qapplication_wrap.h @@ -19,5 +19,10 @@ public: // Wrapped methods Napi::Value processEvents(const Napi::CallbackInfo& info); Napi::Value exec(const Napi::CallbackInfo& info); + Napi::Value quit(const Napi::CallbackInfo& info); + Napi::Value exit(const Napi::CallbackInfo& info); }; +namespace StaticQApplicationWrapMethods { + Napi::Value instance(const Napi::CallbackInfo& info); +} \ No newline at end of file diff --git a/src/cpp/core/YogaWidget/yogawidget.cpp b/src/cpp/core/YogaWidget/yogawidget.cpp index aed73f784..fde00f369 100644 --- a/src/cpp/core/YogaWidget/yogawidget.cpp +++ b/src/cpp/core/YogaWidget/yogawidget.cpp @@ -247,9 +247,9 @@ void YogaWidget::setYWidth(QString rawValue){ ? YGNodeStyleSetWidthPercent(this->getFlexNode(), measurement.value) : YGNodeStyleSetWidth(this->getFlexNode(), measurement.value); this->_yWidth = rawValue; - spdlog::info("set qWidth: {}", rawValue.toStdString()); + spdlog::info("set yWidth: {}", rawValue.toStdString()); }catch(...){ - spdlog::warn("Invalid value: qWidth: {}",rawValue.toStdString()); + spdlog::warn("Invalid value: yWidth: {}",rawValue.toStdString()); } } void YogaWidget::setYHeight(QString rawValue){ @@ -259,9 +259,9 @@ void YogaWidget::setYHeight(QString rawValue){ ? YGNodeStyleSetHeightPercent(this->getFlexNode(), measurement.value) : YGNodeStyleSetHeight(this->getFlexNode(), measurement.value); this->_yHeight = rawValue; - spdlog::info("set qHeight: {}", rawValue.toStdString()); + spdlog::info("set yHeight: {}", rawValue.toStdString()); }catch(...){ - spdlog::warn("Invalid value: qHeight: {}",rawValue.toStdString()); + spdlog::warn("Invalid value: yHeight: {}",rawValue.toStdString()); } } void YogaWidget::setYMaxWidth(QString rawValue){ diff --git a/src/lib/QtGui/QApplication/index.ts b/src/lib/QtGui/QApplication/index.ts index 518d96d3c..67ed6a616 100644 --- a/src/lib/QtGui/QApplication/index.ts +++ b/src/lib/QtGui/QApplication/index.ts @@ -1,12 +1,32 @@ import addon from "../../core/addon"; -import { Component } from "../../core/Component"; +import { Component, NativeElement } from "../../core/Component"; +import { checkIfNativeElement } from "../../utils"; +type arg = NativeElement; export class QApplication extends Component { - native = new addon.QApplication(); + native: NativeElement; + constructor(arg?: arg) { + super(); + if (checkIfNativeElement(arg)) { + this.native = arg as NativeElement; + } else { + this.native = new addon.QApplication(); + } + } processEvents = () => { this.native.processEvents(); }; - exec = () => { - this.native.exec(); + exec = (): Number => { + return this.native.exec(); + }; + static instance = (): QApplication => { + const nativeQApp = addon.QApplication.instance(); + return new QApplication(nativeQApp); + }; + quit = () => { + return this.native.quit(); + }; + exit = (exitCode: number) => { + return this.native.exit(exitCode); }; } diff --git a/src/lib/QtGui/QPixmap/index.ts b/src/lib/QtGui/QPixmap/index.ts index 3d28f372a..7d7699fcf 100644 --- a/src/lib/QtGui/QPixmap/index.ts +++ b/src/lib/QtGui/QPixmap/index.ts @@ -1,12 +1,9 @@ import addon from "../../core/addon"; import { Component, NativeElement } from "../../core/Component"; import { AspectRatioMode } from "../../QtEnums"; -type arg = string | NativeElement; +import { checkIfNativeElement } from "../../utils"; -const checkIfNativeElement = (arg: any) => { - const nativeArg = arg as NativeElement; - return typeof nativeArg === "object" && nativeArg.type === "native"; -}; +type arg = string | NativeElement; export class QPixmap extends Component { native: NativeElement; constructor(arg?: arg) { diff --git a/src/lib/utils.ts b/src/lib/utils.ts new file mode 100644 index 000000000..00f059216 --- /dev/null +++ b/src/lib/utils.ts @@ -0,0 +1,6 @@ +import { NativeElement } from "./core/Component"; + +export const checkIfNativeElement = (arg: any) => { + const nativeArg = arg as NativeElement; + return typeof nativeArg === "object" && nativeArg.type === "native"; +};