From 6f8db2db1cd11b352d7c82b32b9add2f8910d87b Mon Sep 17 00:00:00 2001 From: Atul R Date: Tue, 21 May 2019 18:46:34 +0200 Subject: [PATCH] Adds basic push button --- binding.gyp | 1 + demo.js | 4 ++ .../QPushButton/qpushbutton_wrap.cpp | 69 +++++++++++++++++++ .../QtWidgets/QPushButton/qpushbutton_wrap.h | 21 ++++++ src/cpp/main.cpp | 2 + src/index.ts | 4 +- src/lib/QtGui/QWidget/index.ts | 3 +- src/lib/QtWidgets/QPushButton/index.ts | 22 ++++++ src/lib/core/Component/index.ts | 2 +- 9 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp create mode 100644 src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h create mode 100644 src/lib/QtWidgets/QPushButton/index.ts diff --git a/binding.gyp b/binding.gyp index 4c69014cc..9a1f27563 100644 --- a/binding.gyp +++ b/binding.gyp @@ -14,6 +14,7 @@ "src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp", "src/cpp/QtWidgets/QLayout/qlayout_wrap.cpp", "src/cpp/QtWidgets/QMainWindow/qmainwindow_wrap.cpp", + "src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp" ], 'conditions': [ ['OS=="mac"', { diff --git a/demo.js b/demo.js index f47331b2d..5eb66002c 100644 --- a/demo.js +++ b/demo.js @@ -14,8 +14,12 @@ const label2 = new QtWidgets.QLabel(); label2.setText("Hello12321"); label2.setStyleSheet("background-color:blue; color:white;"); +const button1 = new QtWidgets.QPushButton(); +button1.setText("Yolo"); + gridLayout.addWidget(label); gridLayout.addWidget(label2); +gridLayout.addWidget(button1); view.setLayout(gridLayout); diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp new file mode 100644 index 000000000..30f738ca7 --- /dev/null +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp @@ -0,0 +1,69 @@ +#include "qpushbutton_wrap.h" +#include "../../QtGui/QWidget/qwidget_wrap.h" +#include "../../Extras/Utils/utils.h" + +Napi::FunctionReference QPushButtonWrap::constructor; + +Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) { + Napi::HandleScope scope(env); + char CLASSNAME[] = "QPushButton"; + Napi::Function func = DefineClass(env, CLASSNAME, { + InstanceMethod("setStyleSheet", &QPushButtonWrap::setStyleSheet), + InstanceMethod("setText", &QPushButtonWrap::setText), + }); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + return exports; +} + +QPushButton* QPushButtonWrap::getInternalInstance() { + return this->instance; +} + +QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info): Napi::ObjectWrap(info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if(info.Length() == 1) { + if(info[0].IsObject()){ + Napi::Object object_parent = info[0].As(); + QWidgetWrap* parent = Napi::ObjectWrap::Unwrap(object_parent); + this->instance = new QPushButton(parent->getInternalInstance()); //this sets the parent to current widget + }else{ + extrautils::throwTypeError(env, "Wrong type of arguments"); + } + }else if (info.Length() == 0){ + this->instance = new QPushButton(); + }else { + extrautils::throwTypeError(env, "Wrong number of arguments"); + } +} + +QPushButtonWrap::~QPushButtonWrap() { + delete this->instance; +} + +Napi::Value QPushButtonWrap::setStyleSheet(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::String text = info[0].As(); + std::string style = text.Utf8Value(); + this->instance->setStyleSheet(style.c_str()); + // this->instance->repaint(); + + return env.Null(); +} + +Napi::Value QPushButtonWrap::setText(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::String napiText = info[0].As(); + std::string text = napiText.Utf8Value(); + this->instance->setText(text.c_str()); + // this->instance->repaint(); + + return env.Null(); +} + diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h new file mode 100644 index 000000000..ae0a456e1 --- /dev/null +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h @@ -0,0 +1,21 @@ +#ifndef QPUSHBUTTON_WRAP_H +#define QPUSHBUTTON_WRAP_H +#include +#include + +class QPushButtonWrap : public Napi::ObjectWrap { + private: + QPushButton* instance; + public: + static Napi::Object init(Napi::Env env, Napi::Object exports); + QPushButtonWrap(const Napi::CallbackInfo& info); + ~QPushButtonWrap(); + QPushButton* getInternalInstance(); + //class constructor + static Napi::FunctionReference constructor; + //wrapped methods + Napi::Value setStyleSheet(const Napi::CallbackInfo& info); + Napi::Value setText(const Napi::CallbackInfo& info); +}; + +#endif diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 0635cb31f..eb5dfb62d 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -4,6 +4,7 @@ #include "QtWidgets/QLayout/qlayout_wrap.h" #include "QtWidgets/QLabel/qlabel_wrap.h" #include "QtWidgets/QMainWindow/qmainwindow_wrap.h" +#include "QtWidgets/QPushButton/qpushbutton_wrap.h" #include @@ -19,6 +20,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { QWidgetWrap::init(env, exports); QGridLayoutWrap::init(env, exports); QMainWindowWrap::init(env,exports); + QPushButtonWrap::init(env, exports); return QLabelWrap::init(env, exports); } diff --git a/src/index.ts b/src/index.ts index b2e844373..872ddf330 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { QGridLayout } from "./lib/QtWidgets/QGridLayout"; import { QLabel } from "./lib/QtWidgets/QLabel"; import { QLayout } from "./lib/QtWidgets/QLayout"; import { QMainWindow } from "./lib/QtWidgets/QMainWindow"; +import { QPushButton } from "./lib/QtWidgets/QPushButton"; export const QtGui = { QApplication, @@ -14,5 +15,6 @@ export const QtWidgets = { QGridLayout, QLabel, QMainWindow, - QLayout + QLayout, + QPushButton }; diff --git a/src/lib/QtGui/QWidget/index.ts b/src/lib/QtGui/QWidget/index.ts index 2178d9815..42aba868a 100644 --- a/src/lib/QtGui/QWidget/index.ts +++ b/src/lib/QtGui/QWidget/index.ts @@ -4,6 +4,7 @@ import { Component } from "../../core/Component"; export class QWidget extends Component { native: any; + layout?: QLayout; constructor(parent?: QWidget) { super(); if (parent) { @@ -24,7 +25,7 @@ export class QWidget extends Component { } setLayout(parentLayout: QLayout) { this.native.setLayout(parentLayout.native); - this.parent = parentLayout; + this.layout = parentLayout; } setStyleSheet(style: string) { this.native.setStyleSheet(style); diff --git a/src/lib/QtWidgets/QPushButton/index.ts b/src/lib/QtWidgets/QPushButton/index.ts new file mode 100644 index 000000000..e2f887492 --- /dev/null +++ b/src/lib/QtWidgets/QPushButton/index.ts @@ -0,0 +1,22 @@ +import addon from "../../core/addon"; +import { QWidget } from "../../QtGui/QWidget"; +import { Component } from "../../core/Component"; + +export class QPushButton extends Component { + native: any; + constructor(parent?: QWidget) { + super(); + if (parent) { + this.native = new addon.QPushButton(parent.native); + this.parent = parent; + } else { + this.native = new addon.QPushButton(); + } + } + setText(text: string) { + this.native.setText(text); + } + setStyleSheet(style: string) { + this.native.setStyleSheet(style); + } +} diff --git a/src/lib/core/Component/index.ts b/src/lib/core/Component/index.ts index 3c2ed89f7..15de30fd3 100644 --- a/src/lib/core/Component/index.ts +++ b/src/lib/core/Component/index.ts @@ -1,5 +1,5 @@ export abstract class Component { - children = new Set(); + children = new Set(); //TODO if react stub these as react will manage the instances from beings gc'ed better. parent: Component | null = null; abstract native: any; }