diff --git a/CMakeLists.txt b/CMakeLists.txt index 58d1ee14d..f4649531d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QNativeGestureEvent/qnativegestureevent_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QPaintEvent/qpaintevent_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QTabletEvent/qtabletevent_wrap.cpp" + "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QTimerEvent/qtimerevent_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QDropEvent/qdropevent_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QDragMoveEvent/qdragmoveevent_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QDragLeaveEvent/qdragleaveevent_wrap.cpp" diff --git a/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h b/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h index e50a3b687..83880d5b7 100644 --- a/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h +++ b/src/cpp/include/nodegui/QtCore/QObject/qobject_macro.h @@ -74,6 +74,20 @@ Napi::Env env = info.Env(); \ return Napi::External::New( \ env, static_cast(this->instance)); \ + } \ + Napi::Value startTimer(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + int interval = info[0].As().Int32Value(); \ + Qt::TimerType timerType = \ + static_cast(info[1].As().Int32Value()); \ + int result = this->instance->startTimer(interval, timerType); \ + return Napi::Value::From(env, result); \ + } \ + Napi::Value killTimer(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + int id = info[0].As().Int32Value(); \ + this->instance->killTimer(id); \ + return env.Null(); \ } // Ideally this macro below should go in @@ -115,7 +129,9 @@ InstanceMethod("objectName", &ComponentWrapName::objectName), \ InstanceMethod("dumpObjectTree", &ComponentWrapName::dumpObjectTree), \ InstanceMethod("dumpObjectInfo", &ComponentWrapName::dumpObjectInfo), \ - InstanceMethod("setParent", &ComponentWrapName::setParent), + InstanceMethod("setParent", &ComponentWrapName::setParent), \ + InstanceMethod("startTimer", &ComponentWrapName::startTimer), \ + InstanceMethod("killTimer", &ComponentWrapName::killTimer), #endif // QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE diff --git a/src/cpp/include/nodegui/QtGui/QEvent/QTimerEvent/qtimerevent_wrap.h b/src/cpp/include/nodegui/QtGui/QEvent/QTimerEvent/qtimerevent_wrap.h new file mode 100644 index 000000000..b5f7d2555 --- /dev/null +++ b/src/cpp/include/nodegui/QtGui/QEvent/QTimerEvent/qtimerevent_wrap.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include + +#include "Extras/Export/export.h" +#include "QtGui/QEvent/QEvent/qevent_macro.h" +#include "core/Component/component_macro.h" + +class DLL_EXPORT QTimerEventWrap : public Napi::ObjectWrap { + COMPONENT_WRAPPED_METHODS_DECLARATION + QEVENT_WRAPPED_METHODS_DECLARATION + + private: + QTimerEvent* instance; + + public: + static Napi::Object init(Napi::Env env, Napi::Object exports); + QTimerEventWrap(const Napi::CallbackInfo& info); + ~QTimerEventWrap(); + QTimerEvent* getInternalInstance(); + // class constructor + static Napi::FunctionReference constructor; + // wrapped methods + Napi::Value timerId(const Napi::CallbackInfo& info); +}; diff --git a/src/cpp/lib/QtGui/QEvent/QTimerEvent/qtimerevent_wrap.cpp b/src/cpp/lib/QtGui/QEvent/QTimerEvent/qtimerevent_wrap.cpp new file mode 100644 index 000000000..e2e065d84 --- /dev/null +++ b/src/cpp/lib/QtGui/QEvent/QTimerEvent/qtimerevent_wrap.cpp @@ -0,0 +1,45 @@ +#include "QtGui/QEvent/QTimerEvent/qtimerevent_wrap.h" + +#include + +#include "Extras/Utils/nutils.h" + +Napi::FunctionReference QTimerEventWrap::constructor; + +Napi::Object QTimerEventWrap::init(Napi::Env env, Napi::Object exports) { + Napi::HandleScope scope(env); + char CLASSNAME[] = "QTimerEvent"; + Napi::Function func = + DefineClass(env, CLASSNAME, + {InstanceMethod("timerId", &QTimerEventWrap::timerId), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QTimerEventWrap) + QEVENT_WRAPPED_METHODS_EXPORT_DEFINE(QTimerEventWrap)}); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + return exports; +} + +QTimerEvent* QTimerEventWrap::getInternalInstance() { return this->instance; } + +QTimerEventWrap::QTimerEventWrap(const Napi::CallbackInfo& info) + : Napi::ObjectWrap(info) { + Napi::Env env = info.Env(); + if (info.Length() == 1) { + Napi::External eventObject = + info[0].As>(); + this->instance = static_cast(eventObject.Data()); + } else { + Napi::TypeError::New(env, "Wrong number of arguments") + .ThrowAsJavaScriptException(); + } + this->rawData = extrautils::configureComponent(this->getInternalInstance()); +} + +QTimerEventWrap::~QTimerEventWrap() { + // Do not destroy instance here. It will be done by Qt Event loop. +} + +Napi::Value QTimerEventWrap::timerId(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + return Napi::Value::From(env, this->instance->timerId()); +} diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 817660b77..90b135276 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -34,6 +34,7 @@ #include "QtGui/QEvent/QPaintEvent/qpaintevent_wrap.h" #include "QtGui/QEvent/QResizeEvent/qresizeevent_wrap.h" #include "QtGui/QEvent/QTabletEvent/qtabletevent_wrap.h" +#include "QtGui/QEvent/QTimerEvent/qtimerevent_wrap.h" #include "QtGui/QEvent/QWheelEvent/qwheelevent_wrap.h" #include "QtGui/QFont/qfont_wrap.h" #include "QtGui/QFontDatabase/qfontdatabase_wrap.h" @@ -244,6 +245,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { QScreenWrap::init(env, exports); QWindowWrap::init(env, exports); QResizeEventWrap::init(env, exports); + QTimerEventWrap::init(env, exports); // Test CacheTestQObjectWrap::init(env, exports); diff --git a/src/index.ts b/src/index.ts index 224ef541d..d059c8c60 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,6 +28,7 @@ export { QMoveEvent } from './lib/QtGui/QEvent/QMoveEvent'; export { QWheelEvent } from './lib/QtGui/QEvent/QWheelEvent'; export { QNativeGestureEvent } from './lib/QtGui/QEvent/QNativeGestureEvent'; export { QTabletEvent } from './lib/QtGui/QEvent/QTabletEvent'; +export { QTimerEvent } from './lib/QtGui/QEvent/QTimerEvent'; export { QDrag } from './lib/QtGui/QDrag'; export { QDropEvent } from './lib/QtGui/QEvent/QDropEvent'; export { QDragMoveEvent } from './lib/QtGui/QEvent/QDragMoveEvent'; diff --git a/src/lib/QtCore/QObject.ts b/src/lib/QtCore/QObject.ts index 18ddd8097..e87c9a4b2 100644 --- a/src/lib/QtCore/QObject.ts +++ b/src/lib/QtCore/QObject.ts @@ -3,6 +3,7 @@ import { NativeElement } from '../core/Component'; import { checkIfNativeElement } from '../utils/helpers'; import addon from '../utils/addon'; import { QVariant, QVariantType } from './QVariant'; +import { TimerType } from '../QtEnums/TimerType'; export abstract class NodeObject extends EventWidget { inherits(className: string): boolean { @@ -35,6 +36,12 @@ export abstract class NodeObject extends EventWi this.native.setParent(null); } } + startTimer(intervalMS: number, timerType = TimerType.CoarseTimer): number { + return this.native.startTimer(intervalMS, timerType); + } + killTimer(timerId: number): void { + this.native.killTimer(timerId); + } } export interface QObjectSignals { diff --git a/src/lib/QtGui/QEvent/QTimerEvent.ts b/src/lib/QtGui/QEvent/QTimerEvent.ts new file mode 100644 index 000000000..07abb9a44 --- /dev/null +++ b/src/lib/QtGui/QEvent/QTimerEvent.ts @@ -0,0 +1,13 @@ +import addon from '../../utils/addon'; +import { NativeRawPointer } from '../../core/Component'; +import { QEvent } from './QEvent'; + +export class QTimerEvent extends QEvent { + constructor(event: NativeRawPointer<'QEvent'>) { + super(new addon.QTimerEvent(event)); + } + + timerId(): number { + return this.native.timerId(); + } +}