forked from nodegui/nodegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventwidget.cpp
More file actions
52 lines (45 loc) · 1.96 KB
/
eventwidget.cpp
File metadata and controls
52 lines (45 loc) · 1.96 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
#include "eventwidget.h"
#include "deps/spdlog/spdlog.h"
#include <napi.h>
void EventWidget::subscribeToQtEvent(std::string evtString){
try {
int evtType = EventsMap::eventTypes.at(evtString);
this->subscribedEvents.insert({static_cast<QEvent::Type>(evtType), evtString});
spdlog::info("EventWidget: subscribed to {} : {}, size: {}", evtString.c_str(), evtType, subscribedEvents.size());
} catch (...) {
spdlog::info("EventWidget: Couldn't subscribe to qt event {}. If this is a signal you can safely ignore this warning", evtString.c_str());
}
}
void EventWidget::unSubscribeToQtEvent(std::string evtString){
try {
int evtType = EventsMap::eventTypes.at(evtString);
this->subscribedEvents.erase(static_cast<QEvent::Type>(evtType)); // erasing by key
spdlog::info("EventWidget: unsubscribed to {} : {}", evtString.c_str(), evtType);
} catch (...) {
spdlog::info("EventWidget: Couldn't unsubscribe to qt event {}. If this is a signal you can safely ignore this warning", evtString.c_str());
}
}
void EventWidget::event(QEvent* event){
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 };
this->emitOnNode.Call(args);
} catch (...) {
// Do nothing
}
}
}
void EventWidget::connectWidgetSignalsToEventEmitter(){
// 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();
}
}