-
-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy patheventwidget.cpp
More file actions
73 lines (62 loc) · 2.23 KB
/
eventwidget.cpp
File metadata and controls
73 lines (62 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "core/Events/eventwidget.h"
#include <napi.h>
#include <QDebug>
void EventWidget::subscribeToQtEvent(std::string evtString) {
try {
int evtType = EventsMap::eventTypes.at(evtString);
this->subscribedEvents.insert(
{static_cast<QEvent::Type>(evtType), evtString});
} catch (...) {
// qDebug() << "EventWidget: Couldn't subscribe to qt event"
// << evtString.c_str()
// << ". If this is a signal you can safely ignore this warning";
}
}
void EventWidget::unSubscribeToQtEvent(std::string evtString) {
try {
int evtType = EventsMap::eventTypes.at(evtString);
this->subscribedEvents.erase(
static_cast<QEvent::Type>(evtType)); // erasing by key
} catch (...) {
// qDebug() << "EventWidget: Couldn't unsubscribe to qt event "
// << evtString.c_str()
// << ". If this is a signal you can safely ignore this warning ";
}
}
bool EventWidget::event(QEvent* event) {
return sendEventToNode(event, false, false);
}
bool EventWidget::eventAfterDefault(QEvent* event, bool baseWidgetResult) {
return sendEventToNode(event, true, baseWidgetResult);
}
bool EventWidget::sendEventToNode(QEvent* event, bool afterBaseWidget,
bool baseWidgetResult) {
if (this->emitOnNode) {
try {
QEvent::Type evtType = event->type();
std::string eventTypeString = subscribedEvents.at(evtType);
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
Napi::Value nativeEvent = Napi::External<QEvent>::New(env, event);
std::vector<napi_value> args = {
Napi::String::New(env, eventTypeString), nativeEvent,
Napi::Boolean::New(env, afterBaseWidget),
Napi::Boolean::New(env, baseWidgetResult)};
Napi::Value returnCode = this->emitOnNode.Call(args);
return returnCode.As<Napi::Boolean>().Value();
} catch (...) {
// Do nothing
}
}
return baseWidgetResult;
}
void EventWidget::connectSignalsToEventEmitter() {
// Do nothing
// This method should be overriden in sub classes to connect all signals to
// event emiiter of node. See Push button
}
EventWidget::~EventWidget() {
if (this->emitOnNode) {
this->emitOnNode.Reset();
}
}