events support

This commit is contained in:
Atul R 2019-06-15 18:29:16 +02:00
parent 3fe22cdc12
commit 229ecddeb1
8 changed files with 206 additions and 10 deletions

View File

@ -13,6 +13,7 @@
"../src/cpp/core/FlexLayout/flexlayout.cpp",
"../src/cpp/core/FlexLayout/flexitem.cpp",
"../src/cpp/core/YogaWidget/nodestyle.cpp",
"../src/cpp/core/Events/eventsmap.cpp",
"../src/cpp/core/YogaWidget/yogawidget.cpp",
# wrapped cpps
"../src/cpp/QtGui/QApplication/qapplication_wrap.cpp",

View File

@ -71,7 +71,7 @@ const testFlexLayout = () => {
const win = new QMainWindow();
win.setObjectName("win");
win.resize(300,300);
win.resize(300, 300);
const rootView = new QWidget();
rootView.setStyleSheet(`
* {
@ -127,6 +127,10 @@ const testFlexLayout = () => {
const button = new QPushButton(view2);
button.setObjectName("button");
button.setText("Hululu");
button.native.subscribeToEvent("MouseButtonPress");
button.setSignalListener("MouseButtonPress", () => {
console.log("MouseButtonPress");
});
flayout2.addWidget(button, button.getFlexNode());
rootLayout.addWidget(view, view.getFlexNode());

View File

@ -4,13 +4,42 @@
#include <QPushButton>
#include "src/cpp/core/YogaWidget/yogawidget.h"
#include "napi.h"
#include <napi-thread-safe-callback.hpp>
#include <QEvent>
#include "src/cpp/core/Events/eventsmap.h"
#include <QDebug>
class NPushButton: public QPushButton, public YogaWidget
{
public:
std::unique_ptr<ThreadSafeCallback> emitOnNode;
SET_YOGA_WIDGET_Q_PROPERTIES
using QPushButton::QPushButton; //inherit all constructors of QPushButton
Q_OBJECT
public:
std::unordered_map<QEvent::Type, std::string> subscribedEvents;
void subscribeToEvent(std::string evtString){
try {
int evtType = EventsMap::events.at(evtString);
this->subscribedEvents.insert({static_cast<QEvent::Type>(evtType), evtString});
} catch (...) {
qDebug()<< "Coudn't subscribe to event "<< evtString.c_str();
}
}
bool event(QEvent* event){
try {
QEvent::Type e = event->type();
std::string eventTypeString = subscribedEvents.at(e);
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::String::New(env, eventTypeString) };
});
} catch (...) {}
return QWidget::event(event);
}
~NPushButton(){
this->emitOnNode.release(); //cleanup instance->emitOnNode
}
};

View File

@ -10,6 +10,7 @@ Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("setText", &QPushButtonWrap::setText),
InstanceMethod("setupSignalListeners",&QPushButtonWrap::setupSignalListeners),
InstanceMethod("subscribeToEvent",&QPushButtonWrap::subscribeToEvent),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPushButtonWrap)
});
constructor = Napi::Persistent(func);
@ -39,37 +40,42 @@ QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info): Napi::ObjectWr
}
QPushButtonWrap::~QPushButtonWrap() {
this->emitOnNode.release(); //cleanup emitOnNode
delete this->instance;
}
Napi::Value QPushButtonWrap::setupSignalListeners(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
this->emitOnNode = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
this->instance->emitOnNode = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
// Qt Connects: Implement all signal connects here
QObject::connect(this->instance, &QPushButton::clicked, [=](bool checked) {
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
this->instance->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::String::New(env, "clicked"), Napi::Value::From(env, checked) };
});
});
QObject::connect(this->instance, &QPushButton::released, [=]() {
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
this->instance->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::String::New(env, "released") };
});
});
QObject::connect(this->instance, &QPushButton::pressed, [=]() {
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
this->instance->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::String::New(env, "pressed") };
});
});
QObject::connect(this->instance, &QPushButton::toggled, [=](bool checked) {
this->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
this->instance->emitOnNode->call([=](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::String::New(env, "toggled"), Napi::Value::From(env, checked) };
});
});
return env.Null();
}
Napi::Value QPushButtonWrap::subscribeToEvent(const Napi::CallbackInfo& info){
Napi::Env env = info.Env();
Napi::String eventString = info[0].As<Napi::String>();
this->instance->subscribeToEvent(eventString.Utf8Value());
return env.Null();
}
Napi::Value QPushButtonWrap::setText(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

View File

@ -1,7 +1,6 @@
#pragma once
#include <napi.h>
#include <napi-thread-safe-callback.hpp>
#include "npushbutton.h"
#include "src/cpp/QtGui/QWidget/qwidget_macro.h"
@ -10,7 +9,7 @@
class QPushButtonWrap : public Napi::ObjectWrap<QPushButtonWrap> {
private:
NPushButton* instance;
std::unique_ptr<ThreadSafeCallback> emitOnNode;
// std::unique_ptr<ThreadSafeCallback> emitOnNode;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QPushButtonWrap(const Napi::CallbackInfo& info);
@ -21,6 +20,7 @@ class QPushButtonWrap : public Napi::ObjectWrap<QPushButtonWrap> {
static Napi::FunctionReference constructor;
//wrapped methods
Napi::Value setText(const Napi::CallbackInfo& info);
Napi::Value subscribeToEvent(const Napi::CallbackInfo& info);
QWIDGET_WRAPPED_METHODS_DECLARATION

View File

@ -0,0 +1,149 @@
#include "eventsmap.h"
std::unordered_map<std::string, int> EventsMap::events {
{ "None", QEvent::None },
{ "ActionAdded", QEvent::ActionAdded },
{ "ActionChanged", QEvent::ActionChanged },
{ "ActionRemoved", QEvent::ActionRemoved },
{ "ActivationChange", QEvent::ActivationChange },
{ "ApplicationActivate", QEvent::ApplicationActivate },
{ "ApplicationActivated", QEvent::ApplicationActivated },
{ "ApplicationDeactivate", QEvent::ApplicationDeactivate },
{ "ApplicationFontChange", QEvent::ApplicationFontChange },
{ "ApplicationLayoutDirectionChange", QEvent::ApplicationLayoutDirectionChange },
{ "ApplicationPaletteChange", QEvent::ApplicationPaletteChange },
{ "ApplicationStateChange", QEvent::ApplicationStateChange },
{ "ApplicationWindowIconChange", QEvent::ApplicationWindowIconChange },
{ "ChildAdded", QEvent::ChildAdded },
{ "ChildPolished", QEvent::ChildPolished },
{ "ChildRemoved", QEvent::ChildRemoved },
{ "Clipboard", QEvent::Clipboard },
{ "Close", QEvent::Close },
{ "CloseSoftwareInputPanel", QEvent::CloseSoftwareInputPanel },
{ "ContentsRectChange", QEvent::ContentsRectChange },
{ "ContextMenu", QEvent::ContextMenu },
{ "CursorChange", QEvent::CursorChange },
{ "DeferredDelete", QEvent::DeferredDelete },
{ "DragEnter", QEvent::DragEnter },
{ "DragLeave", QEvent::DragLeave },
{ "DragMove", QEvent::DragMove },
{ "Drop", QEvent::Drop },
{ "DynamicPropertyChange", QEvent::DynamicPropertyChange },
{ "EnabledChange", QEvent::EnabledChange },
{ "Enter", QEvent::Enter },
{ "EnterWhatsThisMode", QEvent::EnterWhatsThisMode },
{ "Expose", QEvent::Expose },
{ "FileOpen", QEvent::FileOpen },
{ "FocusIn", QEvent::FocusIn },
{ "FocusOut", QEvent::FocusOut },
{ "FocusAboutToChange", QEvent::FocusAboutToChange },
{ "FontChange", QEvent::FontChange },
{ "Gesture", QEvent::Gesture },
{ "GestureOverride", QEvent::GestureOverride },
{ "GrabKeyboard", QEvent::GrabKeyboard },
{ "GrabMouse", QEvent::GrabMouse },
{ "GraphicsSceneContextMenu", QEvent::GraphicsSceneContextMenu },
{ "GraphicsSceneDragEnter", QEvent::GraphicsSceneDragEnter },
{ "GraphicsSceneDragLeave", QEvent::GraphicsSceneDragLeave },
{ "GraphicsSceneDragMove", QEvent::GraphicsSceneDragMove },
{ "GraphicsSceneDrop", QEvent::GraphicsSceneDrop },
{ "GraphicsSceneHelp", QEvent::GraphicsSceneHelp },
{ "GraphicsSceneHoverEnter", QEvent::GraphicsSceneHoverEnter },
{ "GraphicsSceneHoverLeave", QEvent::GraphicsSceneHoverLeave },
{ "GraphicsSceneHoverMove", QEvent::GraphicsSceneHoverMove },
{ "GraphicsSceneMouseDoubleClick", QEvent::GraphicsSceneMouseDoubleClick },
{ "GraphicsSceneMouseMove", QEvent::GraphicsSceneMouseMove },
{ "GraphicsSceneMousePress", QEvent::GraphicsSceneMousePress },
{ "GraphicsSceneMouseRelease", QEvent::GraphicsSceneMouseRelease },
{ "GraphicsSceneMove", QEvent::GraphicsSceneMove },
{ "GraphicsSceneResize", QEvent::GraphicsSceneResize },
{ "GraphicsSceneWheel", QEvent::GraphicsSceneWheel },
{ "Hide", QEvent::Hide },
{ "HideToParent", QEvent::HideToParent },
{ "HoverEnter", QEvent::HoverEnter },
{ "HoverLeave", QEvent::HoverLeave },
{ "HoverMove", QEvent::HoverMove },
{ "IconDrag", QEvent::IconDrag },
{ "IconTextChange", QEvent::IconTextChange },
{ "InputMethod", QEvent::InputMethod },
{ "InputMethodQuery", QEvent::InputMethodQuery },
{ "KeyboardLayoutChange", QEvent::KeyboardLayoutChange },
{ "KeyPress", QEvent::KeyPress },
{ "KeyRelease", QEvent::KeyRelease },
{ "LanguageChange", QEvent::LanguageChange },
{ "LayoutDirectionChange", QEvent::LayoutDirectionChange },
{ "LayoutRequest", QEvent::LayoutRequest },
{ "Leave", QEvent::Leave },
{ "LeaveWhatsThisMode", QEvent::LeaveWhatsThisMode },
{ "LocaleChange", QEvent::LocaleChange },
{ "NonClientAreaMouseButtonDblClick", QEvent::NonClientAreaMouseButtonDblClick },
{ "NonClientAreaMouseButtonPress", QEvent::NonClientAreaMouseButtonPress },
{ "NonClientAreaMouseButtonRelease", QEvent::NonClientAreaMouseButtonRelease },
{ "NonClientAreaMouseMove", QEvent::NonClientAreaMouseMove },
{ "MacSizeChange", QEvent::MacSizeChange },
{ "MetaCall", QEvent::MetaCall },
{ "ModifiedChange", QEvent::ModifiedChange },
{ "MouseButtonDblClick", QEvent::MouseButtonDblClick },
{ "MouseButtonPress", QEvent::MouseButtonPress },
{ "MouseButtonRelease", QEvent::MouseButtonRelease },
{ "MouseMove", QEvent::MouseMove },
{ "MouseTrackingChange", QEvent::MouseTrackingChange },
{ "Move", QEvent::Move },
{ "NativeGesture", QEvent::NativeGesture },
{ "OrientationChange", QEvent::OrientationChange },
{ "Paint", QEvent::Paint },
{ "PaletteChange", QEvent::PaletteChange },
{ "ParentAboutToChange", QEvent::ParentAboutToChange },
{ "ParentChange", QEvent::ParentChange },
{ "PlatformPanel", QEvent::PlatformPanel },
{ "PlatformSurface", QEvent::PlatformSurface },
{ "Polish", QEvent::Polish },
{ "PolishRequest", QEvent::PolishRequest },
{ "QueryWhatsThis", QEvent::QueryWhatsThis },
{ "ReadOnlyChange", QEvent::ReadOnlyChange },
{ "RequestSoftwareInputPanel", QEvent::RequestSoftwareInputPanel },
{ "Resize", QEvent::Resize },
{ "ScrollPrepare", QEvent::ScrollPrepare },
{ "Scroll", QEvent::Scroll },
{ "Shortcut", QEvent::Shortcut },
{ "ShortcutOverride", QEvent::ShortcutOverride },
{ "Show", QEvent::Show },
{ "ShowToParent", QEvent::ShowToParent },
{ "SockAct", QEvent::SockAct },
{ "StateMachineSignal", QEvent::StateMachineSignal },
{ "StateMachineWrapped", QEvent::StateMachineWrapped },
{ "StatusTip", QEvent::StatusTip },
{ "StyleChange", QEvent::StyleChange },
{ "TabletMove", QEvent::TabletMove },
{ "TabletPress", QEvent::TabletPress },
{ "TabletRelease", QEvent::TabletRelease },
{ "TabletEnterProximity", QEvent::TabletEnterProximity },
{ "TabletLeaveProximity", QEvent::TabletLeaveProximity },
{ "TabletTrackingChange", QEvent::TabletTrackingChange },
{ "ThreadChange", QEvent::ThreadChange },
{ "Timer", QEvent::Timer },
{ "ToolBarChange", QEvent::ToolBarChange },
{ "ToolTip", QEvent::ToolTip },
{ "ToolTipChange", QEvent::ToolTipChange },
{ "TouchBegin", QEvent::TouchBegin },
{ "TouchCancel", QEvent::TouchCancel },
{ "TouchEnd", QEvent::TouchEnd },
{ "TouchUpdate", QEvent::TouchUpdate },
{ "UngrabKeyboard", QEvent::UngrabKeyboard },
{ "UngrabMouse", QEvent::UngrabMouse },
{ "UpdateLater", QEvent::UpdateLater },
{ "UpdateRequest", QEvent::UpdateRequest },
{ "WhatsThis", QEvent::WhatsThis },
{ "WhatsThisClicked", QEvent::WhatsThisClicked },
{ "Wheel", QEvent::Wheel },
{ "WinEventAct", QEvent::WinEventAct },
{ "WindowActivate", QEvent::WindowActivate },
{ "WindowBlocked", QEvent::WindowBlocked },
{ "WindowDeactivate", QEvent::WindowDeactivate },
{ "WindowIconChange", QEvent::WindowIconChange },
{ "WindowStateChange", QEvent::WindowStateChange },
{ "WindowTitleChange", QEvent::WindowTitleChange },
{ "WindowUnblocked", QEvent::WindowUnblocked },
{ "WinIdChange", QEvent::WinIdChange },
{ "ZOrderChange", QEvent::ZOrderChange },
};

View File

@ -0,0 +1,8 @@
#pragma once
#include <unordered_map>
#include <QEvent>
class EventsMap {
public:
static std::unordered_map<std::string, int> events;
};

View File

@ -11,7 +11,6 @@
*/
struct NodeValueUnit{
YGUnit unit;
float value;