diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp index 7fe128b..fdb16b8 100644 --- a/demo/src/Bindings.cpp +++ b/demo/src/Bindings.cpp @@ -177,6 +177,7 @@ Qt::ItemFlags FibonacciList::flags(const QModelIndex &i) const auto flags = QAbstractItemModel::flags(i); return flags; } + QVariant FibonacciList::result(int row) const { QVariant v; @@ -197,6 +198,7 @@ QVariant FibonacciList::data(const QModelIndex &index, int role) const } return QVariant(); } + QHash FibonacciList::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "result"); @@ -221,12 +223,9 @@ bool FibonacciList::setHeaderData(int section, Qt::Orientation orientation, cons bool FibonacciList::setData(const QModelIndex &index, const QVariant &value, int role) { - bool set = false; - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; + return false; } + extern "C" { FibonacciList::Private* fibonacci_list_new(FibonacciList*, void (*)(const FibonacciList*), @@ -320,6 +319,7 @@ Qt::ItemFlags FileSystemTree::flags(const QModelIndex &i) const auto flags = QAbstractItemModel::flags(i); return flags; } + QVariant FileSystemTree::fileIcon(const QModelIndex& index) const { QVariant v; @@ -328,6 +328,7 @@ QVariant FileSystemTree::fileIcon(const QModelIndex& index) const if (!b.isNull()) v.setValue(b); return v; } + QVariant FileSystemTree::fileName(const QModelIndex& index) const { QVariant v; @@ -336,6 +337,7 @@ QVariant FileSystemTree::fileName(const QModelIndex& index) const if (!s.isNull()) v.setValue(s); return v; } + QVariant FileSystemTree::filePath(const QModelIndex& index) const { QVariant v; @@ -344,18 +346,21 @@ QVariant FileSystemTree::filePath(const QModelIndex& index) const if (!s.isNull()) v.setValue(s); return v; } + QVariant FileSystemTree::filePermissions(const QModelIndex& index) const { QVariant v; v = file_system_tree_data_file_permissions(m_d, index.internalId()); return v; } + QVariant FileSystemTree::fileSize(const QModelIndex& index) const { QVariant v; v = file_system_tree_data_file_size(m_d, index.internalId()); return v; } + QVariant FileSystemTree::fileType(const QModelIndex& index) const { QVariant v; @@ -411,6 +416,7 @@ QVariant FileSystemTree::data(const QModelIndex &index, int role) const } return QVariant(); } + QHash FileSystemTree::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "fileIcon"); @@ -440,12 +446,9 @@ bool FileSystemTree::setHeaderData(int section, Qt::Orientation orientation, con bool FileSystemTree::setData(const QModelIndex &index, const QVariant &value, int role) { - bool set = false; - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; + return false; } + extern "C" { FileSystemTree::Private* file_system_tree_new(FileSystemTree*, void (*)(FileSystemTree*), void (*)(const FileSystemTree*, quintptr, bool), @@ -543,6 +546,7 @@ Qt::ItemFlags Processes::flags(const QModelIndex &i) const auto flags = QAbstractItemModel::flags(i); return flags; } + QVariant Processes::cmd(const QModelIndex& index) const { QVariant v; @@ -551,24 +555,28 @@ QVariant Processes::cmd(const QModelIndex& index) const if (!s.isNull()) v.setValue(s); return v; } + QVariant Processes::cpuPercentage(const QModelIndex& index) const { QVariant v; v = processes_data_cpu_percentage(m_d, index.internalId()); return v; } + QVariant Processes::cpuUsage(const QModelIndex& index) const { QVariant v; v = processes_data_cpu_usage(m_d, index.internalId()); return v; } + QVariant Processes::memory(const QModelIndex& index) const { QVariant v; v = processes_data_memory(m_d, index.internalId()); return v; } + QVariant Processes::name(const QModelIndex& index) const { QVariant v; @@ -577,12 +585,14 @@ QVariant Processes::name(const QModelIndex& index) const if (!s.isNull()) v.setValue(s); return v; } + QVariant Processes::pid(const QModelIndex& index) const { QVariant v; v = processes_data_pid(m_d, index.internalId()); return v; } + QVariant Processes::uid(const QModelIndex& index) const { QVariant v; @@ -628,6 +638,7 @@ QVariant Processes::data(const QModelIndex &index, int role) const } return QVariant(); } + QHash Processes::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "cmd"); @@ -658,12 +669,9 @@ bool Processes::setHeaderData(int section, Qt::Orientation orientation, const QV bool Processes::setData(const QModelIndex &index, const QVariant &value, int role) { - bool set = false; - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; + return false; } + extern "C" { Processes::Private* processes_new(Processes*, void (*)(Processes*), void (*)(const Processes*, quintptr, bool), @@ -745,12 +753,25 @@ Qt::ItemFlags TimeSeries::flags(const QModelIndex &i) const } return flags; } + QVariant TimeSeries::input(int row) const { QVariant v; v = time_series_data_input(m_d, row); return v; } + +bool TimeSeries::setInput(int row, const QVariant& value) +{ + bool set = false; + set = time_series_set_data_input(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + QVariant TimeSeries::result(int row) const { QVariant v; @@ -758,6 +779,17 @@ QVariant TimeSeries::result(int row) const return v; } +bool TimeSeries::setResult(int row, const QVariant& value) +{ + bool set = false; + set = time_series_set_data_result(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + QVariant TimeSeries::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); @@ -781,6 +813,7 @@ QVariant TimeSeries::data(const QModelIndex &index, int role) const } return QVariant(); } + QHash TimeSeries::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "input"); @@ -806,25 +839,22 @@ bool TimeSeries::setHeaderData(int section, Qt::Orientation orientation, const Q bool TimeSeries::setData(const QModelIndex &index, const QVariant &value, int role) { - bool set = false; if (index.column() == 0) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) { - set = time_series_set_data_input(m_d, index.row(), value.value()); + return setInput(index.row(), value); } if (role == Qt::UserRole + 1) { - set = time_series_set_data_result(m_d, index.row(), value.value()); + return setResult(index.row(), value); } } if (index.column() == 1) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 1) { - set = time_series_set_data_result(m_d, index.row(), value.value()); + return setResult(index.row(), value); } } - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; + return false; } + extern "C" { TimeSeries::Private* time_series_new(TimeSeries*, void (*)(const TimeSeries*), @@ -1150,7 +1180,7 @@ FibonacciList::~FibonacciList() { } void FibonacciList::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("result")); - } +} FileSystemTree::FileSystemTree(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), m_d(0), @@ -1225,7 +1255,7 @@ void FileSystemTree::initHeaderData() { m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("filePath")); m_headerData.insert(qMakePair(3, Qt::DisplayRole), QVariant("filePermissions")); m_headerData.insert(qMakePair(4, Qt::DisplayRole), QVariant("fileType")); - } +} QString FileSystemTree::path() const { QString v; @@ -1311,7 +1341,7 @@ void Processes::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("name")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("cpuUsage")); m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("memory")); - } +} bool Processes::active() const { return processes_active_get(m_d); @@ -1372,4 +1402,4 @@ TimeSeries::~TimeSeries() { void TimeSeries::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("input")); m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("result")); - } +} diff --git a/demo/src/Bindings.h b/demo/src/Bindings.h index 9a26abc..0dbf7fd 100644 --- a/demo/src/Bindings.h +++ b/demo/src/Bindings.h @@ -103,13 +103,14 @@ public: QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); + Q_INVOKABLE QVariant result(int row) const; + signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); - QVariant result(int row) const; signals: }; @@ -144,18 +145,19 @@ public: QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); + Q_INVOKABLE QVariant fileIcon(const QModelIndex& index) const; + Q_INVOKABLE QVariant fileName(const QModelIndex& index) const; + Q_INVOKABLE QVariant filePath(const QModelIndex& index) const; + Q_INVOKABLE QVariant filePermissions(const QModelIndex& index) const; + Q_INVOKABLE QVariant fileSize(const QModelIndex& index) const; + Q_INVOKABLE QVariant fileType(const QModelIndex& index) const; + signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); - QVariant fileIcon(const QModelIndex& index) const; - QVariant fileName(const QModelIndex& index) const; - QVariant filePath(const QModelIndex& index) const; - QVariant filePermissions(const QModelIndex& index) const; - QVariant fileSize(const QModelIndex& index) const; - QVariant fileType(const QModelIndex& index) const; signals: void pathChanged(); }; @@ -191,19 +193,20 @@ public: QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); + Q_INVOKABLE QVariant cmd(const QModelIndex& index) const; + Q_INVOKABLE QVariant cpuPercentage(const QModelIndex& index) const; + Q_INVOKABLE QVariant cpuUsage(const QModelIndex& index) const; + Q_INVOKABLE QVariant memory(const QModelIndex& index) const; + Q_INVOKABLE QVariant name(const QModelIndex& index) const; + Q_INVOKABLE QVariant pid(const QModelIndex& index) const; + Q_INVOKABLE QVariant uid(const QModelIndex& index) const; + signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); - QVariant cmd(const QModelIndex& index) const; - QVariant cpuPercentage(const QModelIndex& index) const; - QVariant cpuUsage(const QModelIndex& index) const; - QVariant memory(const QModelIndex& index) const; - QVariant name(const QModelIndex& index) const; - QVariant pid(const QModelIndex& index) const; - QVariant uid(const QModelIndex& index) const; signals: void activeChanged(); }; @@ -236,14 +239,17 @@ public: QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); + Q_INVOKABLE QVariant input(int row) const; + Q_INVOKABLE bool setInput(int row, const QVariant& value); + Q_INVOKABLE QVariant result(int row) const; + Q_INVOKABLE bool setResult(int row, const QVariant& value); + signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); - QVariant input(int row) const; - QVariant result(int row) const; signals: }; #endif // BINDINGS_H diff --git a/src/cpp.cpp b/src/cpp.cpp index 96baaab..428cc3d 100644 --- a/src/cpp.cpp +++ b/src/cpp.cpp @@ -51,26 +51,28 @@ void writeHeaderItemModel(QTextStream& h, const Object& o) { QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); +)"); + for (auto ip: o.itemProperties) { + if (o.type == ObjectType::List) { + h << QString(" Q_INVOKABLE QVariant %1(int row) const;\n").arg(ip.name); + if (ip.write) { + h << QString(" Q_INVOKABLE bool set%1(int row, const QVariant& value);\n").arg(upperInitial(ip.name)); + } + } else { + h << QString(" Q_INVOKABLE QVariant %1(const QModelIndex& index) const;\n").arg(ip.name); + if (ip.write) { + h << QString(" Q_INVOKABLE bool set%1(const QModelIndex& index, const QVariant& value);\n").arg(upperInitial(ip.name)); + } + } + } + h << R"( signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); -)"); - for (auto ip: o.itemProperties) { - if (o.type == ObjectType::List) { - h << QString(" QVariant %1(int row) const;\n").arg(ip.name); - } else { - h << QString(" QVariant %1(const QModelIndex& index) const;\n").arg(ip.name); - } -/* - if (ip.write) { - r << QString(" fn set_%1(&mut self, item: usize, %2) -> bool;\n") - .arg(snakeCase(ip.name), rustType(ip)); - } -*/ - } +)"; } bool isColumnWrite(const Object& o, int col) { @@ -82,6 +84,85 @@ bool isColumnWrite(const Object& o, int col) { return false; } +void writeModelGetterSetter(QTextStream& cpp, const QString& index, + const ItemProperty& ip, const Object& o) { + const QString lcname(snakeCase(o.name)); + QString idx = index; + + // getter + if (o.type == ObjectType::List) { + idx = ", row"; + cpp << QString("QVariant %1::%2(int row) const\n{\n") + .arg(o.name, ip.name); + } else { + cpp << QString("QVariant %1::%2(const QModelIndex& index) const\n{\n") + .arg(o.name, ip.name); + } + cpp << " QVariant v;\n"; + if (ip.type.name == "QString") { + cpp << " QString s;\n"; + cpp << QString(" %1_data_%2(m_d%4, &s, set_%3);\n") + .arg(lcname, snakeCase(ip.name), ip.type.name.toLower(), idx); + cpp << " if (!s.isNull()) v.setValue(s);\n"; + } else if (ip.type.name == "QByteArray") { + cpp << " QByteArray b;\n"; + cpp << QString(" %1_data_%2(m_d%4, &b, set_%3);\n") + .arg(lcname, snakeCase(ip.name), ip.type.name.toLower(), idx); + cpp << " if (!b.isNull()) v.setValue(b);\n"; + } else { + cpp << QString(" v = %1_data_%2(m_d%3);\n") + .arg(lcname, snakeCase(ip.name), idx); + } + cpp << " return v;\n"; + cpp << "}\n\n"; + + if (!ip.write) { + return; + } + + // setter + if (o.type == ObjectType::List) { + idx = ", row"; + cpp << QString("bool %1::set%2(int row, const QVariant& value)\n{\n") + .arg(o.name, upperInitial(ip.name)); + } else { + cpp << QString("bool %1::set%2(const QModelIndex& index, const QVariant& value)\n{\n") + .arg(o.name, upperInitial(ip.name)); + } + cpp << " bool set = false;\n"; + if (ip.optional) { + QString test = "!value.isValid()"; + if (ip.type.isComplex()) { + test += " || value.isNull()"; + } + cpp << " if (" << test << ") {\n"; + cpp << QString(" set = %1_set_data_%2_none(m_d%3);") + .arg(lcname, snakeCase(ip.name), idx) << endl; + cpp << " } else\n"; + } + QString val = QString("value.value<%1>()").arg(ip.type.name); + cpp << QString(" set = %1_set_data_%2(m_d%3, %4);") + .arg(lcname, snakeCase(ip.name), idx, val) << endl; + if (o.type == ObjectType::List) { + cpp << R"( if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +)"; + } else { + cpp << R"( if (set) { + emit dataChanged(index, index); + } + return set; +} + +)"; + } +} + void writeCppModel(QTextStream& cpp, const Object& o) { const QString lcname(snakeCase(o.name)); QString indexDecl = ", int"; @@ -237,37 +318,11 @@ Qt::ItemFlags %1::flags(const QModelIndex &i) const cpp << " flags |= Qt::ItemIsEditable;\n }\n"; } } - cpp << " return flags;\n}\n"; + cpp << " return flags;\n}\n\n"; for (auto ip: o.itemProperties) { - QString idx = index; - if (o.type == ObjectType::List) { - idx = ", row"; - cpp << QString("QVariant %1::%2(int row) const\n{\n") - .arg(o.name, ip.name); - } else { - cpp << QString("QVariant %1::%2(const QModelIndex& index) const\n{\n") - .arg(o.name, ip.name); - } - cpp << " QVariant v;\n"; - if (ip.type.name == "QString") { - cpp << " QString s;\n"; - cpp << QString(" %1_data_%2(m_d%4, &s, set_%3);\n") - .arg(lcname, snakeCase(ip.name), ip.type.name.toLower(), idx); - cpp << " if (!s.isNull()) v.setValue(s);\n"; - } else if (ip.type.name == "QByteArray") { - cpp << " QByteArray b;\n"; - cpp << QString(" %1_data_%2(m_d%4, &b, set_%3);\n") - .arg(lcname, snakeCase(ip.name), ip.type.name.toLower(), idx); - cpp << " if (!b.isNull()) v.setValue(b);\n"; - } else { - cpp << QString(" v = %1_data_%2(m_d%3);\n") - .arg(lcname, snakeCase(ip.name), idx); - } - cpp << " return v;\n"; - cpp << "}\n"; + writeModelGetterSetter(cpp, index, ip, o); } - cpp << QString(R"( -QVariant %1::data(const QModelIndex &index, int role) const + cpp << QString(R"(QVariant %1::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); switch (index.column()) { @@ -295,7 +350,7 @@ QVariant %1::data(const QModelIndex &index, int role) const } cpp << " }\n"; } - cpp << " }\n return QVariant();\n}\n"; + cpp << " }\n return QVariant();\n}\n\n"; cpp << "QHash " << o.name << "::roleNames() const {\n"; cpp << " QHash names = QAbstractItemModel::roleNames();\n"; for (int i = 0; i < o.itemProperties.size(); ++i) { @@ -324,7 +379,6 @@ bool %1::setHeaderData(int section, Qt::Orientation orientation, const QVariant bool %1::setData(const QModelIndex &index, const QVariant &value, int role) { )").arg(o.name); - cpp << " bool set = false;\n"; for (int col = 0; col < o.columnCount; ++col) { if (!isColumnWrite(o, col)) { continue; @@ -344,29 +398,18 @@ bool %1::setData(const QModelIndex &index, const QVariant &value, int role) cpp << QString("role == Qt::%1 || ").arg(metaRoles.valueToKey(role)); } cpp << "role == Qt::UserRole + " << i << ") {\n"; - if (ip.optional) { - QString test = "!value.isValid()"; - if (ip.type.isComplex()) { - test += " || value.isNull()"; - } - cpp << " if (" << test << ") {\n"; - cpp << QString(" set = %1_set_data_%2_none(m_d%3);") - .arg(lcname, snakeCase(ip.name), index) << endl; - cpp << " } else\n"; + if (o.type == ObjectType::List) { + cpp << QString(" return set%1(index.row(), value);\n") + .arg(upperInitial(ip.name)); + } else { + cpp << QString(" return set%1(index, value);\n") + .arg(upperInitial(ip.name)); } - QString val = QString("value.value<%1>()").arg(ip.type.name); - cpp << QString(" set = %1_set_data_%2(m_d%3, %4);") - .arg(lcname, snakeCase(ip.name), index, val) << endl; cpp << " }\n"; } cpp << " }\n"; } - cpp << R"( if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; -} -)"; + cpp << " return false;\n}\n\n"; } void writeHeaderObject(QTextStream& h, const Object& o, const Configuration& conf) { @@ -674,7 +717,7 @@ void writeCppObject(QTextStream& cpp, const Object& o, const Configuration& conf } } } - cpp << " }\n"; + cpp << "}\n"; } for (const Property& p: o.properties) { diff --git a/tests/test_list_rust.cpp b/tests/test_list_rust.cpp index 68fe1c1..f0d8beb 100644 --- a/tests/test_list_rust.cpp +++ b/tests/test_list_rust.cpp @@ -115,6 +115,7 @@ Qt::ItemFlags Persons::flags(const QModelIndex &i) const } return flags; } + QVariant Persons::userName(int row) const { QVariant v; @@ -124,6 +125,17 @@ QVariant Persons::userName(int row) const return v; } +bool Persons::setUserName(int row, const QVariant& value) +{ + bool set = false; + set = persons_set_data_user_name(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + QVariant Persons::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); @@ -138,6 +150,7 @@ QVariant Persons::data(const QModelIndex &index, int role) const } return QVariant(); } + QHash Persons::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "userName"); @@ -162,17 +175,14 @@ bool Persons::setHeaderData(int section, Qt::Orientation orientation, const QVar bool Persons::setData(const QModelIndex &index, const QVariant &value, int role) { - bool set = false; if (index.column() == 0) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) { - set = persons_set_data_user_name(m_d, index.row(), value.value()); + return setUserName(index.row(), value); } } - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; + return false; } + extern "C" { Persons::Private* persons_new(Persons*, void (*)(const Persons*), @@ -238,4 +248,4 @@ Persons::~Persons() { } void Persons::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("userName")); - } +} diff --git a/tests/test_list_rust.h b/tests/test_list_rust.h index 5bf5872..1902464 100644 --- a/tests/test_list_rust.h +++ b/tests/test_list_rust.h @@ -34,13 +34,15 @@ public: QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); + Q_INVOKABLE QVariant userName(int row) const; + Q_INVOKABLE bool setUserName(int row, const QVariant& value); + signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); - QVariant userName(int row) const; signals: }; #endif // TEST_LIST_RUST_H diff --git a/tests/test_tree_rust.cpp b/tests/test_tree_rust.cpp index ec4218d..0c57c56 100644 --- a/tests/test_tree_rust.cpp +++ b/tests/test_tree_rust.cpp @@ -133,6 +133,7 @@ Qt::ItemFlags Persons::flags(const QModelIndex &i) const } return flags; } + QVariant Persons::userName(const QModelIndex& index) const { QVariant v; @@ -142,6 +143,16 @@ QVariant Persons::userName(const QModelIndex& index) const return v; } +bool Persons::setUserName(const QModelIndex& index, const QVariant& value) +{ + bool set = false; + set = persons_set_data_user_name(m_d, index.internalId(), value.value()); + if (set) { + emit dataChanged(index, index); + } + return set; +} + QVariant Persons::data(const QModelIndex &index, int role) const { Q_ASSERT(rowCount(index.parent()) > index.row()); @@ -156,6 +167,7 @@ QVariant Persons::data(const QModelIndex &index, int role) const } return QVariant(); } + QHash Persons::roleNames() const { QHash names = QAbstractItemModel::roleNames(); names.insert(Qt::UserRole + 0, "userName"); @@ -180,17 +192,14 @@ bool Persons::setHeaderData(int section, Qt::Orientation orientation, const QVar bool Persons::setData(const QModelIndex &index, const QVariant &value, int role) { - bool set = false; if (index.column() == 0) { if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) { - set = persons_set_data_user_name(m_d, index.internalId(), value.value()); + return setUserName(index, value); } } - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; + return false; } + extern "C" { Persons::Private* persons_new(Persons*, void (*)(const Persons*, quintptr, bool), @@ -273,4 +282,4 @@ Persons::~Persons() { } void Persons::initHeaderData() { m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("userName")); - } +} diff --git a/tests/test_tree_rust.h b/tests/test_tree_rust.h index b9eab8d..f2180be 100644 --- a/tests/test_tree_rust.h +++ b/tests/test_tree_rust.h @@ -34,13 +34,15 @@ public: QHash roleNames() const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); + Q_INVOKABLE QVariant userName(const QModelIndex& index) const; + Q_INVOKABLE bool setUserName(const QModelIndex& index, const QVariant& value); + signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); - QVariant userName(const QModelIndex& index) const; signals: }; #endif // TEST_TREE_RUST_H