From d0ef8472a3083e32478339bd5e206b91859198cd Mon Sep 17 00:00:00 2001 From: Atul R Date: Sat, 8 Jun 2019 17:41:23 +0200 Subject: [PATCH] Adds all button event listeners --- demo.ts | 15 ++++++--- .../QPushButton/qpushbutton_wrap.cpp | 4 +-- .../QtWidgets/QPushButton/qpushbutton_wrap.h | 31 ++++++++++++++++--- src/lib/QtWidgets/QPushButton/index.ts | 13 +++----- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/demo.ts b/demo.ts index 971601277..545198feb 100644 --- a/demo.ts +++ b/demo.ts @@ -25,12 +25,19 @@ const testGridLayout = () => { label.setStyleSheet("background-color:blue; color:white;"); const button1 = new QPushButton(); - button1.setEventListener(QPushButtonEvents.clicked, () => { - console.log("THIS IS Yolo from JS"); + button1.setEventListener(QPushButtonEvents.clicked, (...args) => { + console.log("clicked", ...args); }); - button1.setEventListener(QPushButtonEvents.pressed, () => { - console.log("THIS IS PRESED FROM JS"); + button1.setEventListener(QPushButtonEvents.pressed, (...args) => { + console.log("pressed", ...args); }); + button1.setEventListener(QPushButtonEvents.released, (...args) => { + console.log("released", ...args); + }); + button1.setEventListener(QPushButtonEvents.toggled, (...args) => { + console.log("toggled", ...args); + }); + button1.setText("Yolo"); const checkbox = new QCheckBox(); diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp index d738bae35..33ed5f819 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp @@ -9,7 +9,7 @@ Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QPushButton"; Napi::Function func = DefineClass(env, CLASSNAME, { InstanceMethod("setText", &QPushButtonWrap::setText), - InstanceMethod("setNodeEventEmiiter",&QPushButtonWrap::setNodeEventEmiiter), + InstanceMethod("setupEventListeners",&QPushButtonWrap::setupEventListeners), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPushButtonWrap) }); constructor = Napi::Persistent(func); @@ -37,7 +37,7 @@ QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info): Napi::ObjectWr } QPushButtonWrap::~QPushButtonWrap() { - this->emitterEmit.release(); + this->nodeEmit.release(); delete this->instance; } diff --git a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h index 3aace90cc..c661ad115 100644 --- a/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h +++ b/src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.h @@ -8,7 +8,7 @@ class QPushButtonWrap : public Napi::ObjectWrap { private: NPushButton* instance; - std::unique_ptr emitterEmit; + std::unique_ptr nodeEmit; public: static Napi::Object init(Napi::Env env, Napi::Object exports); QPushButtonWrap(const Napi::CallbackInfo& info); @@ -19,17 +19,38 @@ class QPushButtonWrap : public Napi::ObjectWrap { //wrapped methods Napi::Value setText(const Napi::CallbackInfo& info); - Napi::Value setNodeEventEmiiter(const Napi::CallbackInfo& info) { + Napi::Value setupEventListeners(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); - this->emitterEmit = std::make_unique(info[0].As()); + this->nodeEmit = std::make_unique(info[0].As()); NPushButton::connect(this->instance, &QPushButton::clicked, [=](bool val) { - this->emitterEmit->call([=](Napi::Env env, std::vector& args) { - args = { Napi::String::New(env, "clicked"), Napi::Boolean::New(env, val) }; + this->nodeEmit->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, "clicked"), Napi::Value::From(env,val) }; }); } ); + NPushButton::connect(this->instance, &QPushButton::released, + [=]() { + this->nodeEmit->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, "released") }; + }); + } + ); + NPushButton::connect(this->instance, &QPushButton::pressed, + [=]() { + this->nodeEmit->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, "pressed") }; + }); + } + ); + NPushButton::connect(this->instance, &QPushButton::toggled, + [=](bool val) { + this->nodeEmit->call([=](Napi::Env env, std::vector& args) { + args = { Napi::String::New(env, "toggled"), Napi::Value::From(env,val) }; + }); + } + ); return env.Null(); } diff --git a/src/lib/QtWidgets/QPushButton/index.ts b/src/lib/QtWidgets/QPushButton/index.ts index 8e5d9a523..e206752ec 100644 --- a/src/lib/QtWidgets/QPushButton/index.ts +++ b/src/lib/QtWidgets/QPushButton/index.ts @@ -12,7 +12,7 @@ export enum QPushButtonEvents { type EventCallback = (payload?: any) => void; export class QPushButton extends NodeWidget { native: any; - private eventEmiiter: EventEmitter; + private emitter: EventEmitter; constructor(parent?: NodeWidget) { super(); if (parent) { @@ -21,17 +21,14 @@ export class QPushButton extends NodeWidget { } else { this.native = new addon.QPushButton(); } - this.eventEmiiter = this.initEventEmitter(this.native); - } - private initEventEmitter(native: any): EventEmitter { - const emitter = new EventEmitter(); - native.setNodeEventEmiiter(emitter.emit.bind(emitter)); - return emitter; + this.emitter = new EventEmitter(); + this.native.setupEventListeners(this.emitter.emit.bind(this.emitter)); } + setText(text: string) { this.native.setText(text); } setEventListener(eventType: QPushButtonEvents, callback: EventCallback) { - this.eventEmiiter.on(eventType, callback); + this.emitter.on(eventType, callback); } }