Adds QApplication methods

This commit is contained in:
Atul R 2019-08-11 20:39:17 +02:00
parent 7ad7c389c3
commit 493a61de1b
8 changed files with 135 additions and 15 deletions

View File

@ -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)

56
docs/api/QApplication.md Normal file
View File

@ -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.

View File

@ -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<Napi::External<QApplication>>().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<Napi::Number>();
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<QApplication *>(QCoreApplication::instance());
Napi::Object instance = QApplicationWrap::constructor.New({ Napi::External<QApplication>::New(env, app) });
return instance;
}

View File

@ -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);
}

View File

@ -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){

View File

@ -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);
};
}

View File

@ -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) {

6
src/lib/utils.ts Normal file
View File

@ -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";
};