Add QAbstractItemModel.buddy() support

This commit is contained in:
Simon Edwards 2021-08-15 10:45:22 +02:00
parent e81b5ae658
commit f7a4a4d3d1
6 changed files with 122 additions and 1 deletions

View File

@ -29,7 +29,8 @@
"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_4_headers": "node ./scripts/qode.js dist/examples/modelview_4_headers.js",
"example-modelview_5_edit": "node ./scripts/qode.js dist/examples/modelview_5_edit.js"
"example-modelview_5_edit": "node ./scripts/qode.js dist/examples/modelview_5_edit.js",
"example-modelview_buddy": "node ./scripts/qode.js dist/examples/modelview_buddy.js"
},
"engines": {
"node": ">=14.x.x"

View File

@ -118,4 +118,16 @@ class DLL_EXPORT NAbstractItemModel : public QAbstractItemModel, public EventWid
QModelIndex _protected_createIndex(int row, int column) const {
return createIndex(row, column);
}
QModelIndex buddy(const QModelIndex &index) const override {
Napi::Env env = this->dispatchOnNode.Env();
Napi::HandleScope scope(env);
auto indexWrap = QModelIndexWrap::constructor.New({Napi::External<QModelIndex>::New(env, new QModelIndex(index))});
Napi::Value buddyIndexNapiWrap = this->dispatchOnNode.Call({Napi::String::New(env, "buddy"), indexWrap});
QModelIndexWrap* buddyIndexWrap = Napi::ObjectWrap<QModelIndexWrap>::Unwrap(buddyIndexNapiWrap.As<Napi::Object>());
QModelIndex* buddyIndex = buddyIndexWrap->getInternalInstance();
return *buddyIndex;
}
};

View File

@ -29,4 +29,5 @@ class DLL_EXPORT QAbstractItemModelWrap : public Napi::ObjectWrap<QAbstractItemM
Napi::Value _super_flags(const Napi::CallbackInfo& info);
Napi::Value emitDataChanged(const Napi::CallbackInfo& info);
Napi::Value checkIndex(const Napi::CallbackInfo& info);
Napi::Value _super_buddy(const Napi::CallbackInfo& info);
};

View File

@ -17,6 +17,7 @@ Napi::Object QAbstractItemModelWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("_super_flags", &QAbstractItemModelWrap::_super_flags),
InstanceMethod("emitDataChanged", &QAbstractItemModelWrap::emitDataChanged),
InstanceMethod("checkIndex", &QAbstractItemModelWrap::checkIndex),
InstanceMethod("_super_buddy", &QAbstractItemModelWrap::_super_buddy),
QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QAbstractItemModelWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -109,3 +110,16 @@ Napi::Value QAbstractItemModelWrap::checkIndex(const Napi::CallbackInfo& info) {
auto result = Napi::Value::From(env, static_cast<uint>(this->instance->checkIndex(*index)));
return result;
}
Napi::Value QAbstractItemModelWrap::_super_buddy(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QModelIndexWrap* modelIndexWrap = Napi::ObjectWrap<QModelIndexWrap>::Unwrap(info[0].As<Napi::Object>());
QModelIndex* index = modelIndexWrap->getInternalInstance();
auto resultIndex = this->instance->QAbstractItemModel::buddy(*index);
auto resultModelIndexWrap = QModelIndexWrap::constructor.New({Napi::External<QModelIndex>::New(env, new QModelIndex(resultIndex))});
return resultModelIndexWrap;
}

View File

@ -0,0 +1,80 @@
import { ItemDataRole, ItemFlag, 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;
}
const ROWS = 2;
const COLS = 3;
class MyModel extends QAbstractTableModel {
private _data: string[][] = [];
constructor() {
super();
for (let row = 0; row < ROWS; row++) {
this._data[row] = [];
for (let col = 0; col < COLS; col++) {
this._data[row][col] = '';
}
}
this._data[1][1] = '<- Buddy';
}
rowCount(parent = new QModelIndex()): number {
return ROWS;
}
columnCount(parent = new QModelIndex()): number {
return COLS;
}
buddy(index: QModelIndex): QModelIndex {
if (index.row() == 1 && index.column() == 1) {
return this.createIndex(1, 0);
}
return index;
}
data(index: QModelIndex, role = ItemDataRole.DisplayRole): QVariant {
if (role == ItemDataRole.DisplayRole && this.checkIndex(index)) {
return new QVariant(this._data[index.row()][index.column()]);
}
return new QVariant();
}
flags(index: QModelIndex): ItemFlag {
return ItemFlag.ItemIsEditable | super.flags(index);
}
setData(index: QModelIndex, value: QVariant, role: number): boolean {
if (role == ItemDataRole.EditRole) {
if (!this.checkIndex(index)) {
return false;
}
//save value from editor to member m_gridData
this._data[index.row()][index.column()] = value.toString();
//for presentation purposes only: build and emit a joined string
let result = '';
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
result += this._data[row][col] + ' ';
}
}
console.log(result);
return true;
}
return false;
}
}
main();

View File

@ -89,6 +89,15 @@ export class QAbstractItemModel extends NodeObject<any> {
}
return false;
case 'buddy':
try {
return this.buddy(new QModelIndex(args[0])).native;
} catch (e) {
console.log(`An exception was thrown while dispatching to method 'buddy':`);
console.log(e);
}
return new QModelIndex().native;
default:
return null;
}
@ -96,6 +105,10 @@ export class QAbstractItemModel extends NodeObject<any> {
this.native.initNodeDispatcher(dispatcher);
}
buddy(child: QModelIndex): QModelIndex {
return new QModelIndex(this.native._super_buddy(child.native));
}
index(row: number, column: number, parent = new QModelIndex()): QModelIndex {
return new QModelIndex();
}