Add QTimerEvent and QObject.startTimer() and QObject.killTimer()

This commit is contained in:
Simon Edwards 2022-04-19 19:37:26 +02:00
parent 7d2dfc1cf2
commit ac1c118ae3
8 changed files with 113 additions and 1 deletions

View File

@ -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"

View File

@ -74,6 +74,20 @@
Napi::Env env = info.Env(); \
return Napi::External<QObject>::New( \
env, static_cast<QObject*>(this->instance)); \
} \
Napi::Value startTimer(const Napi::CallbackInfo& info) { \
Napi::Env env = info.Env(); \
int interval = info[0].As<Napi::Number>().Int32Value(); \
Qt::TimerType timerType = \
static_cast<Qt::TimerType>(info[1].As<Napi::Number>().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<Napi::Number>().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

View File

@ -0,0 +1,27 @@
#pragma once
#include <napi.h>
#include <QTimerEvent>
#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<QTimerEventWrap> {
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);
};

View File

@ -0,0 +1,45 @@
#include "QtGui/QEvent/QTimerEvent/qtimerevent_wrap.h"
#include <QString>
#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<QTimerEventWrap>(info) {
Napi::Env env = info.Env();
if (info.Length() == 1) {
Napi::External<QTimerEvent> eventObject =
info[0].As<Napi::External<QTimerEvent>>();
this->instance = static_cast<QTimerEvent*>(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());
}

View File

@ -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);

View File

@ -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';

View File

@ -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<Signals extends QObjectSignals> extends EventWidget<Signals> {
inherits(className: string): boolean {
@ -35,6 +36,12 @@ export abstract class NodeObject<Signals extends QObjectSignals> 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 {

View File

@ -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();
}
}