Add QEvent methods to all subclasses + some random methods (#848)

* Add `clearFocus()` to `QWidget`

* Add `Alignment` param to `QBoxLayout.addWidget()`

* Add `setViewportMargins()` & `viewportMargins()` to `QScrollArea`

* Add `QEvent` methods to all event related subclasses

* Tell C++ that our 0 is zero and not a null pointer
This commit is contained in:
Simon Edwards 2021-07-08 22:45:41 +02:00 committed by GitHub
parent 1d2a0a6f47
commit 1bc346d22c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 192 additions and 163 deletions

View File

@ -11,6 +11,7 @@
class DLL_EXPORT QDragLeaveEventWrap
: public Napi::ObjectWrap<QDragLeaveEventWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
QEVENT_WRAPPED_METHODS_DECLARATION
private:
QDragLeaveEvent* instance;
@ -23,6 +24,4 @@ class DLL_EXPORT QDragLeaveEventWrap
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods (none)
// Methods from QEvent
QEVENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -7,11 +7,6 @@
#include "Extras/Export/export.h"
#include "core/Component/component_macro.h"
/*
NOTE : QDragMoveEvent inherits from QDropEvent
- Is it possible to inherit from QDropEventWrap directly?
*/
class DLL_EXPORT QDragMoveEventWrap
: public Napi::ObjectWrap<QDragMoveEventWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
@ -51,4 +46,4 @@ class DLL_EXPORT QDragMoveEventWrap
Napi::Value setAccepted(const Napi::CallbackInfo& info);
Napi::Value spontaneous(const Napi::CallbackInfo& info);
Napi::Value _type(const Napi::CallbackInfo& info);
};
};

View File

@ -5,10 +5,12 @@
#include <QKeyEvent>
#include "Extras/Export/export.h"
#include "QtGui/QEvent/QEvent/qevent_macro.h"
#include "core/Component/component_macro.h"
class DLL_EXPORT QKeyEventWrap : public Napi::ObjectWrap<QKeyEventWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
QEVENT_WRAPPED_METHODS_DECLARATION
private:
QKeyEvent* instance;
@ -26,4 +28,4 @@ class DLL_EXPORT QKeyEventWrap : public Napi::ObjectWrap<QKeyEventWrap> {
Napi::Value modifiers(const Napi::CallbackInfo& info);
Napi::Value count(const Napi::CallbackInfo& info);
Napi::Value isAutoRepeat(const Napi::CallbackInfo& info);
};
};

View File

@ -5,10 +5,12 @@
#include <QMouseEvent>
#include "Extras/Export/export.h"
#include "QtGui/QEvent/QEvent/qevent_macro.h"
#include "core/Component/component_macro.h"
class DLL_EXPORT QMouseEventWrap : public Napi::ObjectWrap<QMouseEventWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
QEVENT_WRAPPED_METHODS_DECLARATION
private:
QMouseEvent* instance;

View File

@ -5,10 +5,12 @@
#include <QTabletEvent>
#include "Extras/Export/export.h"
#include "QtGui/QEvent/QEvent/qevent_macro.h"
#include "core/Component/component_macro.h"
class DLL_EXPORT QTabletEventWrap : public Napi::ObjectWrap<QTabletEventWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
QEVENT_WRAPPED_METHODS_DECLARATION
private:
QTabletEvent* instance;

View File

@ -13,4 +13,10 @@ class DLL_EXPORT NScrollArea : public QScrollArea, public NodeWidget {
using QScrollArea::QScrollArea; // inherit all constructors of QScrollArea
void connectSignalsToEventEmitter() { QABSTRACTSCROLLAREA_SIGNALS }
// By-pass `protected` access and expose it.
void setViewportMargins(int left, int top, int right, int bottom) {
QScrollArea::setViewportMargins(left, top, right, bottom);
}
QMargins viewportMargins() const { return QScrollArea::viewportMargins(); }
};

View File

@ -26,4 +26,6 @@ class DLL_EXPORT QScrollAreaWrap : public Napi::ObjectWrap<QScrollAreaWrap> {
Napi::Value ensureWidgetVisible(const Napi::CallbackInfo& info);
Napi::Value setWidget(const Napi::CallbackInfo& info);
Napi::Value takeWidget(const Napi::CallbackInfo& info);
Napi::Value setViewportMargins(const Napi::CallbackInfo& info);
Napi::Value viewportMargins(const Napi::CallbackInfo& info);
};

View File

@ -439,6 +439,12 @@
int reason = info[0].As<Napi::Number>().Int32Value(); \
this->instance->setFocus(static_cast<Qt::FocusReason>(reason)); \
return env.Null(); \
} \
Napi::Value clearFocus(const Napi::CallbackInfo& info) { \
Napi::Env env = info.Env(); \
Napi::HandleScope scope(env); \
this->instance->clearFocus(); \
return env.Null(); \
}
#endif // QWIDGET_WRAPPED_METHODS_DECLARATION
@ -501,7 +507,8 @@
InstanceMethod("setGraphicsEffect", &WidgetWrapName::setGraphicsEffect), \
InstanceMethod("acceptDrops", &WidgetWrapName::acceptDrops), \
InstanceMethod("setAcceptDrops", &WidgetWrapName::setAcceptDrops), \
InstanceMethod("setFocus", &WidgetWrapName::setFocus),
InstanceMethod("setFocus", &WidgetWrapName::setFocus), \
InstanceMethod("clearFocus", &WidgetWrapName::clearFocus),
#endif // QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE

View File

@ -16,7 +16,8 @@ Napi::Object QKeyEventWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("modifiers", &QKeyEventWrap::modifiers),
InstanceMethod("count", &QKeyEventWrap::count),
InstanceMethod("isAutoRepeat", &QKeyEventWrap::isAutoRepeat),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QKeyEventWrap)});
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QKeyEventWrap)
QEVENT_WRAPPED_METHODS_EXPORT_DEFINE(QKeyEventWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;

View File

@ -18,7 +18,8 @@ Napi::Object QMouseEventWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("globalX", &QMouseEventWrap::globalX),
InstanceMethod("globalY", &QMouseEventWrap::globalY),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QMouseEventWrap)});
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QMouseEventWrap)
QEVENT_WRAPPED_METHODS_EXPORT_DEFINE(QMouseEventWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;

View File

@ -34,7 +34,8 @@ Napi::Object QTabletEventWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("yTilt", &QTabletEventWrap::yTilt),
InstanceMethod("z", &QTabletEventWrap::z),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QTabletEventWrap)});
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QTabletEventWrap)
QEVENT_WRAPPED_METHODS_EXPORT_DEFINE(QTabletEventWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;

View File

@ -99,8 +99,10 @@ Napi::Value QBoxLayoutWrap::addWidget(const Napi::CallbackInfo& info) {
NodeWidgetWrap* widget =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(qwidgetObject);
int stretch = info[1].As<Napi::Number>().Int32Value();
Qt::Alignment alignment =
static_cast<Qt::Alignment>(info[2].As<Napi::Number>().Int32Value());
this->instance->addWidget(widget->getInternalInstance(), stretch);
this->instance->addWidget(widget->getInternalInstance(), stretch, alignment);
return env.Null();
}

View File

@ -15,6 +15,9 @@ Napi::Object QScrollAreaWrap::init(Napi::Env env, Napi::Object exports) {
&QScrollAreaWrap::ensureWidgetVisible),
InstanceMethod("setWidget", &QScrollAreaWrap::setWidget),
InstanceMethod("takeWidget", &QScrollAreaWrap::takeWidget),
InstanceMethod("setViewportMargins",
&QScrollAreaWrap::setViewportMargins),
InstanceMethod("viewportMargins", &QScrollAreaWrap::viewportMargins),
QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(QScrollAreaWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -100,3 +103,38 @@ Napi::Value QScrollAreaWrap::takeWidget(const Napi::CallbackInfo& info) {
// We will not return the value here since we are doing it in js side anyway
return env.Null();
}
Napi::Value QScrollAreaWrap::setViewportMargins(
const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
NScrollArea* nScrollArea = qobject_cast<NScrollArea*>(this->instance);
if (nScrollArea != nullptr) {
int left = info[0].As<Napi::Number>().Int32Value();
int top = info[1].As<Napi::Number>().Int32Value();
int right = info[2].As<Napi::Number>().Int32Value();
int bottom = info[3].As<Napi::Number>().Int32Value();
nScrollArea->setViewportMargins(left, top, right, bottom);
}
return env.Null();
}
Napi::Value QScrollAreaWrap::viewportMargins(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
NScrollArea* nScrollArea = qobject_cast<NScrollArea*>(this->instance);
QMargins margins;
if (nScrollArea != nullptr) {
margins = nScrollArea->viewportMargins();
}
Napi::Array resultNapi = Napi::Array::New(env, 4);
resultNapi[uint32_t(0)] = Napi::Number::From(env, margins.left());
resultNapi[1] = Napi::Value::From(env, margins.top());
resultNapi[2] = Napi::Value::From(env, margins.right());
resultNapi[3] = Napi::Value::From(env, margins.bottom());
return resultNapi;
}

View File

@ -150,3 +150,4 @@ export { FlexLayout, FlexLayoutSignals } from './lib/core/FlexLayout';
export { StyleSheet } from './lib/core/Style/StyleSheet';
export { NativeElement, Component } from './lib/core/Component';
export { checkIfNativeElement, checkIfNapiExternal } from './lib/utils/helpers';
export { Margins } from './lib/utils/Margins';

View File

@ -1,49 +1,9 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { NativeRawPointer } from '../../core/Component';
import { QEvent } from './QEvent';
export class QDragLeaveEvent {
native: NativeElement;
export class QDragLeaveEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QDragLeaveEvent(event);
}
//Methods from QEvent ---------------------------
/**
* Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
* Setting the accept parameter indicates that the event receiver wants the event. Unwanted events might be propagated to the parent widget
*/
accept(): void {
return this.native.accept();
}
/**
* Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
* Clearing the accept parameter indicates that the event receiver does not want the event.
* Unwanted events might be propagated to the parent widget.
*/
ignore(): void {
return this.native.ignore();
}
isAccepted(): boolean {
return this.native.isAccepted();
}
/**
* Sets the accept flag of the event object
*/
setAccepted(accepted: boolean): void {
return this.native.setAccepted(accepted);
}
/**
* Returns true if the event originated outside the application (a system event); otherwise returns false.
*
* The return value of this function is not defined for paint events.
*/
spontaneous(): boolean {
return this.native.spontaneous();
}
/**
* Returns the event type
* @returns {Number} This is QEvent::Type
*/
type(): number {
return this.native._type();
super(new addon.QDragLeaveEvent(event));
}
}

View File

@ -1,12 +1,12 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { NativeRawPointer } from '../../core/Component';
import { DropAction } from '../../QtEnums';
import { QMimeData } from '../../QtCore/QMimeData';
import { QEvent } from './QEvent';
export class QDragMoveEvent {
native: NativeElement;
export class QDragMoveEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QDragMoveEvent(event);
super(new addon.QDragMoveEvent(event));
}
/**
@ -114,30 +114,4 @@ export class QDragMoveEvent {
// source(): QObject {
// return this.native.source();
// }
//Methods from QEvent ---------------------------
isAccepted(): boolean {
return this.native.isAccepted();
}
/**
* Sets the accept flag of the event object
*/
setAccepted(accepted: boolean): void {
return this.native.setAccepted(accepted);
}
/**
* Returns true if the event originated outside the application (a system event); otherwise returns false.
*
* The return value of this function is not defined for paint events.
*/
spontaneous(): boolean {
return this.native.spontaneous();
}
/**
* Returns the event type
* @returns {Number} This is QEvent::Type
*/
type(): number {
return this.native._type();
}
}

View File

@ -1,12 +1,12 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { NativeRawPointer } from '../../core/Component';
import { DropAction } from '../../QtEnums';
import { QMimeData } from '../../QtCore/QMimeData';
import { QEvent } from './QEvent';
export class QDropEvent {
native: NativeElement;
export class QDropEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QDropEvent(event);
super(new addon.QDropEvent(event));
}
/** Sets the drop action to be the proposed action */
@ -82,45 +82,4 @@ export class QDropEvent {
// source(): QObject {
// return this.native.source();
// }
//Methods from QEvent ---------------------------
/**
* Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
* Setting the accept parameter indicates that the event receiver wants the event. Unwanted events might be propagated to the parent widget
*/
accept(): void {
return this.native.accept();
}
/**
* Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
* Clearing the accept parameter indicates that the event receiver does not want the event.
* Unwanted events might be propagated to the parent widget.
*/
ignore(): void {
return this.native.ignore();
}
isAccepted(): boolean {
return this.native.isAccepted();
}
/**
* Sets the accept flag of the event object
*/
setAccepted(accepted: boolean): void {
return this.native.setAccepted(accepted);
}
/**
* Returns true if the event originated outside the application (a system event); otherwise returns false.
*
* The return value of this function is not defined for paint events.
*/
spontaneous(): boolean {
return this.native.spontaneous();
}
/**
* Returns the event type
* @returns {Number} This is QEvent::Type
*/
type(): number {
return this.native._type();
}
}

View File

@ -0,0 +1,51 @@
import { NativeElement } from '../../core/Component';
export abstract class QEvent {
native: NativeElement;
constructor(native: NativeElement) {
this.native = native;
}
//Methods from QEvent ---------------------------
/**
* Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
* Setting the accept parameter indicates that the event receiver wants the event. Unwanted events might be propagated to the parent widget
*/
accept(): void {
return this.native.accept();
}
/**
* Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
* Clearing the accept parameter indicates that the event receiver does not want the event.
* Unwanted events might be propagated to the parent widget.
*/
ignore(): void {
return this.native.ignore();
}
isAccepted(): boolean {
return this.native.isAccepted();
}
/**
* Sets the accept flag of the event object
*/
setAccepted(accepted: boolean): void {
return this.native.setAccepted(accepted);
}
/**
* Returns true if the event originated outside the application (a system event); otherwise returns false.
*
* The return value of this function is not defined for paint events.
*/
spontaneous(): boolean {
return this.native.spontaneous();
}
/**
* Returns the event type
* @returns {Number} This is QEvent::Type
*/
type(): number {
return this.native._type();
}
}

View File

@ -1,10 +1,10 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { NativeRawPointer } from '../../core/Component';
import { QEvent } from './QEvent';
export class QKeyEvent {
native: NativeElement;
export class QKeyEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QKeyEvent(event);
super(new addon.QKeyEvent(event));
}
text(): string {
return this.native.text();

View File

@ -1,10 +1,10 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { NativeRawPointer } from '../../core/Component';
import { QEvent } from './QEvent';
export class QMouseEvent {
native: NativeElement;
export class QMouseEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QMouseEvent(event);
super(new addon.QMouseEvent(event));
}
button(): number {
return this.native.button();

View File

@ -1,11 +1,11 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { NativeRawPointer } from '../../core/Component';
import { NativeGestureType } from '../../QtEnums';
import { QEvent } from './QEvent';
export class QNativeGestureEvent {
native: NativeElement;
export class QNativeGestureEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QNativeGestureEvent(event);
super(new addon.QNativeGestureEvent(event));
}
//Needs QTouchDevice to implement this

View File

@ -1,11 +1,11 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { NativeRawPointer } from '../../core/Component';
import { QRect } from '../../..';
import { QEvent } from './QEvent';
export class QPaintEvent {
native: NativeElement;
export class QPaintEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QPaintEvent(event);
super(new addon.QPaintEvent(event));
}
rect(): QRect {
return this.native.rect();

View File

@ -1,5 +1,6 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { NativeRawPointer } from '../../core/Component';
import { QEvent } from './QEvent';
enum PointerType {
/** An unknown device */
@ -28,14 +29,14 @@ enum TabletDevice {
/**
* The QTabletEvent class contains parameters that describe a Tablet event
*/
export class QTabletEvent {
export class QTabletEvent extends QEvent {
static readonly PointerType = PointerType;
static readonly TabletDevice = TabletDevice;
readonly PointerType = PointerType;
readonly TabletDevice = TabletDevice;
native: NativeElement;
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QTabletEvent(event);
super(new addon.QTabletEvent(event));
}
/**
* Returns the button that caused the event.

View File

@ -1,11 +1,11 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { NativeRawPointer } from '../../core/Component';
import { ScrollPhase } from '../../QtEnums';
import { QEvent } from './QEvent';
export class QWheelEvent {
native: NativeElement;
export class QWheelEvent extends QEvent {
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QWheelEvent(event);
super(new addon.QWheelEvent(event));
}
/**
* Returns the relative amount that the wheel was rotated, in eighths of a degree.

View File

@ -2,10 +2,10 @@ import addon from '../utils/addon';
import { NodeWidget } from './QWidget';
import { NodeLayout, QLayoutSignals } from './QLayout';
import { NativeElement } from '../core/Component';
import { Direction } from '../QtEnums';
import { AlignmentFlag, Direction } from '../QtEnums';
/**
> Lines up child widgets horizontally or vertically.
* **This class is a JS wrapper around Qt's [QBoxLayout class](https://doc.qt.io/qt-5/qboxlayout.html)**
@ -55,8 +55,8 @@ export class QBoxLayout extends NodeLayout<QBoxLayoutSignals> {
addStrut(size: number): void {
this.native.addStrut(size);
}
addWidget(widget: NodeWidget<any>, stretch = 0): void {
this.native.addWidget(widget.native, stretch);
addWidget(widget: NodeWidget<any>, stretch = 0, alignment: AlignmentFlag = 0): void {
this.native.addWidget(widget.native, stretch, alignment);
this.nodeChildren.add(widget);
}
insertWidget(index: number, widget: NodeWidget<any>, stretch = 0): void {

View File

@ -3,9 +3,10 @@ import { NodeWidget } from './QWidget';
import { NativeElement } from '../core/Component';
import { QAbstractScrollArea, QAbstractScrollAreaSignals } from './QAbstractScrollArea';
import { AlignmentFlag } from '../QtEnums';
import { Margins } from '../utils/Margins';
/**
> A `QScrollArea` provides a scrolling view onto another widget.
* **This class is a JS wrapper around Qt's [QScrollArea class](https://doc.qt.io/qt-5/qscrollarea.html)**
@ -81,6 +82,21 @@ export class QScrollArea extends QAbstractScrollArea<QScrollAreaSignals> {
}
return null;
}
setViewportMargins(left: number, top: number, right: number, bottom: number): void {
// Technically part of QAbstractScrollArea, but the C++ side has subclass specific
// code needed, and setViewportMargins() isn't implemented yet for all of the
// subclasses.
this.native.setViewportMargins(left, top, right, bottom);
}
viewportMargins(): Margins {
const marginsArray = this.native.viewportMargins();
return {
left: marginsArray[0],
top: marginsArray[1],
right: marginsArray[2],
bottom: marginsArray[3],
};
}
}
export type QScrollAreaSignals = QAbstractScrollAreaSignals;

View File

@ -289,9 +289,12 @@ export abstract class NodeWidget<Signals extends QWidgetSignals> extends YogaWid
acceptDrops(): boolean {
return this.native.acceptDrops();
}
setFocus(reason: FocusReason): void {
setFocus(reason = FocusReason.OtherFocusReason): void {
this.native.setFocus(reason);
}
clearFocus(): void {
this.native.clearFocus();
}
}
export interface QWidgetSignals extends QObjectSignals {

6
src/lib/utils/Margins.ts Normal file
View File

@ -0,0 +1,6 @@
export interface Margins {
left: number;
top: number;
right: number;
bottom: number;
}