From 7b7872a38eca6b97f463854b727b217c12cf2cfc Mon Sep 17 00:00:00 2001 From: Atul R Date: Sat, 28 Mar 2020 20:33:29 +0100 Subject: [PATCH] Adds docs for handling events --- src/demo.ts | 57 +++-------------- src/lib/QtGui/QEvent/QKeyEvent.ts | 4 +- website/docs/guides/handle-events.md | 96 +++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 52 deletions(-) diff --git a/src/demo.ts b/src/demo.ts index 09495475e..1a15d48a9 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -1,57 +1,16 @@ -import { QMainWindow, QWidget, FlexLayout, QTreeWidgetItem, QTreeWidget, QLineEdit, MatchFlag } from './index'; +import { QMainWindow, QLabel, WidgetEventTypes, QMouseEvent } from './index'; const win = new QMainWindow(); -const center = new QWidget(); -center.setLayout(new FlexLayout()); -win.setCentralWidget(center); +const label = new QLabel(); +label.setText('Move your move here'); +label.setMouseTracking(true); -const fruitTree = new QTreeWidget(); -fruitTree.setSortingEnabled(true); - -fruitTree.setHeaderLabels(['Fruit', 'Price']); - -const fruitObj = [ - { - fruit: 'Banana', - price: '2.5', - }, - { - fruit: 'Apple', - price: '1.0', - }, - { - fruit: 'Strawberry', - price: '2.5', - }, - { - fruit: 'Orange', - price: '1.5', - }, -]; - -const items = []; - -for (const element of fruitObj) { - const fruitItem = new QTreeWidgetItem(fruitTree, [element.fruit, element.price]); - items.push(fruitItem); -} - -fruitTree.addTopLevelItems(items); - -const filterLineEdit = new QLineEdit(); -filterLineEdit.setPlaceholderText('Filter...'); -filterLineEdit.addEventListener('returnPressed', () => { - const filterText = filterLineEdit.text(); - const foundItems = fruitTree - .findItems(filterText, MatchFlag.MatchContains, 0) - .concat(fruitTree.findItems(filterText, MatchFlag.MatchContains, 1)); - fruitTree.topLevelItems.forEach(item => item.setHidden(true)); - foundItems.forEach(item => item.setHidden(false)); +label.addEventListener(WidgetEventTypes.MouseMove, nativeEvt => { + const mouseEvt = new QMouseEvent(nativeEvt as any); + console.log('mouseMoved at: ', { x: mouseEvt.x(), y: mouseEvt.y() }); }); -center.layout?.addWidget(filterLineEdit); -center.layout?.addWidget(fruitTree); - +win.setCentralWidget(label); win.show(); (global as any).win = win; diff --git a/src/lib/QtGui/QEvent/QKeyEvent.ts b/src/lib/QtGui/QEvent/QKeyEvent.ts index 663219c71..2f4834974 100644 --- a/src/lib/QtGui/QEvent/QKeyEvent.ts +++ b/src/lib/QtGui/QEvent/QKeyEvent.ts @@ -1,9 +1,9 @@ import addon from '../../utils/addon'; -import { NativeElement } from '../../core/Component'; +import { NativeElement, NativeRawPointer } from '../../core/Component'; export class QKeyEvent { native: NativeElement; - constructor(event: NativeElement) { + constructor(event: NativeRawPointer<'QEvent'>) { this.native = new addon.QKeyEvent(event); } text(): string { diff --git a/website/docs/guides/handle-events.md b/website/docs/guides/handle-events.md index 5135d61d5..0d4177e9d 100644 --- a/website/docs/guides/handle-events.md +++ b/website/docs/guides/handle-events.md @@ -3,4 +3,98 @@ sidebar_label: Handle Events title: Handle Events --- -WIP +NodeGui allows you to listen to various events that might originate from the underlying Qt widgets. These events can either be a simple button click or a text change on a lineedit or even something like window being hidden and shown. + +In order to do this we need to attach an event listener to the respective widget. + +Technically, the event listener is a NodeJs [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) instance that listens to events from the underlying Qt widget. The native Qt widget would send all the events to the event emitter in NodeGui world and the user can essentially subscribe to it. + +Lets see an example to see how this looks in practice. + +## Event handling + +The following example demonstrates how to add an event listener to a button widget. + +event example + +```javascript +const { QMainWindow, QPushButton } = require('@nodegui/nodegui'); + +const win = new QMainWindow(); + +const button = new QPushButton(); +button.setText('Click me'); +button.addEventListener('clicked', () => { + console.log('the button was clicked'); +}); + +win.setCentralWidget(button); +win.show(); +global.win = win; +``` + +The addEventListener function accepts an event type, followed by a handler for the event. You can register multiple handlers for the same event by calling addEventListener multiple times. + +Internally, Qt widgets in nodegui has two types of events.: + +- Signals: In short these are basically different for different widgets. So a button maybe have `clicked`, `pressed` signal, while a linedit may have `textChanged` signal. +- QEvents: These are common set of events for all the widgets/qobjects in NodeGui world. These are also helpful at times but typically you would end up using signals more than these common events. + +In NodeGui you can listen to both Signals and QEvents using the same addEventListener function. + +### How do I know which events are supported ? + +In order to find all the supported events for a widget you can take a look at + +#### All Signals for the widgets: + +- (https://docs.nodegui.org/docs/api/generated/globals/#interfaces)[https://docs.nodegui.org/docs/api/generated/globals/#interfaces] +- (https://docs.nodegui.org/docs/api/generated/globals/#type-aliases)[https://docs.nodegui.org/docs/api/generated/globals/#type-aliases] + +You can subscribe to a signal like so: + +```javascript +const { QMainWindow, QCheckBox } = require('@nodegui/nodegui'); + +const win = new QMainWindow(); + +const checkbox = new QCheckBox(); +checkbox.setText('Check/Uncheck me'); +checkbox.addEventListener('clicked', checked => { + console.log('was checked', checked); +}); + +win.setCentralWidget(checkbox); +win.show(); +global.win = win; +``` + +#### All common QEvents for the widgets + +In nodegui all these common QEvents are represented under an enum type: (WidgetEventTypes)[https://docs.nodegui.org/docs/api/generated/enums/widgeteventtypes] + +You can subscribe to a QEvent like so: + +```javascript +const { QMainWindow, QLabel, WidgetEventTypes, QMouseEvent } = require('@nodegui/nodegui'); + +const win = new QMainWindow(); + +const label = new QLabel(); +label.setText('Move your move here'); +label.setMouseTracking(true); + +label.addEventListener(WidgetEventTypes.MouseMove, nativeEvt => { + const mouseEvt = new QMouseEvent(nativeEvt); + console.log('mouseMoved at: ', { x: mouseEvt.x(), y: mouseEvt.y() }); +}); + +win.setCentralWidget(label); +win.show(); +global.win = win; +``` + +mouse event listener + +Note here that every QEvent handler gives a reference to native QEvent in the handler callback. +Not all native QEvent wrappers are implemented yet and we might need your help regarding those. Feel free to jump in and contribute to the nodegui core.