From 588093519abbc313eda6f6406cbd7a23920c2fca Mon Sep 17 00:00:00 2001 From: Simon Edwards Date: Sat, 14 Aug 2021 21:20:46 +0200 Subject: [PATCH] Add header model view tutorial example --- package.json | 3 +- .../QAbstractItemModel/nabstractitemmodel.hpp | 15 +++++++ src/examples/modelview_4_headers.ts | 44 +++++++++++++++++++ src/lib/QtCore/QAbstractItemModel.ts | 15 ++++++- 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/examples/modelview_4_headers.ts diff --git a/package.json b/package.json index 87c8cc51d..ca93501ed 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "prepublishOnly": "cross-env npm run build", "example-modelview_1_readonly": "node ./scripts/qode.js dist/examples/modelview_1_readonly.js", "example-modelview_2_formatting": "node ./scripts/qode.js dist/examples/modelview_2_formatting.js", - "example-modelview_3_changingmodel": "node ./scripts/qode.js dist/examples/modelview_3_changingmodel.js" + "example-modelview_3_changingmodel": "node ./scripts/qode.js dist/examples/modelview_3_changingmodel.js", + "example-modelview_4_headers": "node ./scripts/qode.js dist/examples/modelview_4_headers.js" }, "engines": { "node": ">=14.x.x" diff --git a/src/cpp/include/nodegui/QtCore/QAbstractItemModel/nabstractitemmodel.hpp b/src/cpp/include/nodegui/QtCore/QAbstractItemModel/nabstractitemmodel.hpp index 4b1f0c538..505702f51 100644 --- a/src/cpp/include/nodegui/QtCore/QAbstractItemModel/nabstractitemmodel.hpp +++ b/src/cpp/include/nodegui/QtCore/QAbstractItemModel/nabstractitemmodel.hpp @@ -87,6 +87,21 @@ class DLL_EXPORT NAbstractItemModel : public QAbstractItemModel, public EventWid return result; } + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override { + Napi::Env env = this->dispatchOnNode.Env(); + Napi::HandleScope scope(env); + + auto sectionValue = Napi::Value::From(env, static_cast(section)); + auto orientationValue = Napi::Value::From(env, static_cast(orientation)); + auto roleValue = Napi::Value::From(env, static_cast(role)); + + Napi::Value variantJsObject = this->dispatchOnNode.Call({Napi::String::New(env, "headerData"), sectionValue, orientationValue, roleValue}); + + QVariantWrap* variantWrap = Napi::ObjectWrap::Unwrap(variantJsObject.As()); + QVariant* variant = variantWrap->getInternalInstance(); + return *variant; + } + QModelIndex _protected_createIndex(int row, int column) const { return createIndex(row, column); } diff --git a/src/examples/modelview_4_headers.ts b/src/examples/modelview_4_headers.ts new file mode 100644 index 000000000..30ac581b7 --- /dev/null +++ b/src/examples/modelview_4_headers.ts @@ -0,0 +1,44 @@ +import { ItemDataRole, Orientation, QAbstractTableModel, QModelIndex, QTableView, QVariant } from '..'; + +function main(): void { + const tableView = new QTableView(); + const model = new MyModel(); + tableView.setModel(model); + + tableView.show(); + + (global as any).win = tableView; +} + +class MyModel extends QAbstractTableModel { + rowCount(parent = new QModelIndex()): number { + return 2; + } + + columnCount(parent = new QModelIndex()): number { + return 3; + } + + data(index: QModelIndex, role = ItemDataRole.DisplayRole): QVariant { + if (role === ItemDataRole.DisplayRole) { + return new QVariant(`Row${index.row() + 1}, Column${index.column() + 1}`); + } + return new QVariant(); + } + + headerData(section: number, orientation: Orientation, role: number): QVariant { + if (role == ItemDataRole.DisplayRole && orientation == Orientation.Horizontal) { + switch (section) { + case 0: + return new QVariant('first'); + case 1: + return new QVariant('second'); + case 2: + return new QVariant('third'); + } + } + return new QVariant(); + } +} + +main(); diff --git a/src/lib/QtCore/QAbstractItemModel.ts b/src/lib/QtCore/QAbstractItemModel.ts index fed614d30..66102e0f0 100644 --- a/src/lib/QtCore/QAbstractItemModel.ts +++ b/src/lib/QtCore/QAbstractItemModel.ts @@ -3,7 +3,7 @@ import { NativeElement } from '../core/Component'; import { NodeObject, QObjectSignals } from '../QtCore/QObject'; import { QModelIndex } from './QModelIndex'; import { QVariant } from './QVariant'; -import { ItemDataRole, ItemFlag } from '../QtEnums'; +import { ItemDataRole, ItemFlag, Orientation } from '../QtEnums'; export interface QAbstractItemSignals extends QObjectSignals { // itemChanged: (item: QStandardItem) => void; @@ -71,6 +71,15 @@ export class QAbstractItemModel extends NodeObject { } return ItemFlag.NoItemFlags; + case 'headerData': + try { + return this.headerData(args[0], args[1], args[2]).native; + } catch (e) { + console.log(`An exception was thrown while dispatching to method 'headerData':`); + console.log(e); + } + return new QVariant().native; + default: return null; } @@ -114,4 +123,8 @@ export class QAbstractItemModel extends NodeObject { emitDataChanged(topLeft: QModelIndex, bottomRight: QModelIndex, roles: ItemDataRole[]): void { this.native.emitDataChanged(topLeft.native, bottomRight.native, roles); } + + headerData(section: number, orientation: Orientation, role: number): QVariant { + return new QVariant(); + } }