adds removeEventListener and cleanup

This commit is contained in:
Atul R 2019-07-11 00:29:55 +02:00
parent 3fed29d6fe
commit 790fec5fd6
6 changed files with 45 additions and 11 deletions

View File

@ -6,18 +6,28 @@ 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 {}", evtString.c_str());
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);
spdlog::info("event: {}", eventTypeString);
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);

View File

@ -10,6 +10,7 @@ public:
std::unordered_map<QEvent::Type, std::string> subscribedEvents;
void subscribeToQtEvent(std::string evtString);
void unSubscribeToQtEvent(std::string evtString);
void event(QEvent* event);

View File

@ -25,6 +25,12 @@ Napi::Value subscribeToQtEvent(const Napi::CallbackInfo& info){ \
this->instance->subscribeToQtEvent(eventString.Utf8Value()); \
return env.Null(); \
} \
Napi::Value unSubscribeToQtEvent(const Napi::CallbackInfo& info){ \
Napi::Env env = info.Env(); \
Napi::String eventString = info[0].As<Napi::String>(); \
this->instance->unSubscribeToQtEvent(eventString.Utf8Value()); \
return env.Null(); \
} \
#endif //EVENTWIDGET_WRAPPED_METHODS_DECLARATION
@ -33,6 +39,7 @@ Napi::Value subscribeToQtEvent(const Napi::CallbackInfo& info){ \
\
InstanceMethod("initNodeEventEmitter",&WidgetWrapName::initNodeEventEmitter), \
InstanceMethod("subscribeToQtEvent",&WidgetWrapName::subscribeToQtEvent), \
InstanceMethod("unSubscribeToQtEvent",&WidgetWrapName::unSubscribeToQtEvent), \
#endif // EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE

View File

@ -1,6 +1,6 @@
import addon from "../../core/addon";
import { NodeLayout } from "../../QtWidgets/QLayout";
import { EventWidget } from "../../core/EventWidget";
import { EventWidget, BaseWidgetEvents } from "../../core/EventWidget";
import { NativeElement } from "../../core/Component";
// All Widgets should extend from NodeWidget
@ -46,3 +46,5 @@ export class QWidget extends NodeWidget {
this.native = native;
}
}
export const QWidgetEvents = BaseWidgetEvents;

View File

@ -20,7 +20,21 @@ export abstract class EventWidget extends YogaWidget {
callback: (payload?: NativeEvent | any) => void
) => {
this.native.subscribeToQtEvent(eventType);
this.emitter.on(eventType, callback);
this.emitter.addListener(eventType, callback);
};
removeEventListener = (
eventType: string,
callback?: (payload?: NativeEvent | any) => void
) => {
if (callback) {
this.emitter.removeListener(eventType, callback);
} else {
this.emitter.removeAllListeners(eventType);
}
if (this.emitter.listenerCount(eventType) < 1) {
this.native.unSubscribeToQtEvent(eventType);
}
};
}

View File

@ -1,16 +1,16 @@
export { QApplication } from "./QtGui/QApplication";
export { QWidget } from "./QtGui/QWidget";
export { QWidget, QWidgetEvents } from "./QtGui/QWidget";
// Abstract:
export { NodeWidget } from "./QtGui/QWidget";
export { NodeLayout } from "./QtWidgets/QLayout";
// Widgets:
export { QCheckBox } from "./QtWidgets/QCheckBox";
export { QLabel } from "./QtWidgets/QLabel";
export { QLineEdit } from "./QtWidgets/QLineEdit";
export { QCheckBox, QCheckBoxEvents } from "./QtWidgets/QCheckBox";
export { QLabel, QLabelEvents } from "./QtWidgets/QLabel";
export { QLineEdit, QLineEditEvents } from "./QtWidgets/QLineEdit";
export { QMainWindow } from "./QtWidgets/QMainWindow";
export { QProgressBar } from "./QtWidgets/QProgressBar";
export { QPushButton } from "./QtWidgets/QPushButton";
export { QRadioButton } from "./QtWidgets/QRadioButton";
export { QProgressBar, QProgressBarEvents } from "./QtWidgets/QProgressBar";
export { QPushButton, QPushButtonEvents } from "./QtWidgets/QPushButton";
export { QRadioButton, QRadioButtonEvents } from "./QtWidgets/QRadioButton";
// Layouts:
export { QGridLayout } from "./QtWidgets/QGridLayout";
export { FlexLayout } from "./core/FlexLayout";