From 61169486b704697fb2fc3cb6c0f5ca74055ca25c Mon Sep 17 00:00:00 2001 From: Jos van den Oever Date: Fri, 1 Sep 2017 12:03:40 +0200 Subject: [PATCH] Only override setData when the are writable item properties in the model --- demo/src/Bindings.cpp | 15 --------- demo/src/Bindings.h | 5 +-- src/cpp.cpp | 69 ++++++++++++++++++++++++------------------ tests/test_list_rust.h | 2 +- tests/test_tree_rust.h | 2 +- 5 files changed, 43 insertions(+), 50 deletions(-) diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp index fdb16b8..a0142f0 100644 --- a/demo/src/Bindings.cpp +++ b/demo/src/Bindings.cpp @@ -221,11 +221,6 @@ bool FibonacciList::setHeaderData(int section, Qt::Orientation orientation, cons return true; } -bool FibonacciList::setData(const QModelIndex &index, const QVariant &value, int role) -{ - return false; -} - extern "C" { FibonacciList::Private* fibonacci_list_new(FibonacciList*, void (*)(const FibonacciList*), @@ -444,11 +439,6 @@ bool FileSystemTree::setHeaderData(int section, Qt::Orientation orientation, con return true; } -bool FileSystemTree::setData(const QModelIndex &index, const QVariant &value, int role) -{ - return false; -} - extern "C" { FileSystemTree::Private* file_system_tree_new(FileSystemTree*, void (*)(FileSystemTree*), void (*)(const FileSystemTree*, quintptr, bool), @@ -667,11 +657,6 @@ bool Processes::setHeaderData(int section, Qt::Orientation orientation, const QV return true; } -bool Processes::setData(const QModelIndex &index, const QVariant &value, int role) -{ - return false; -} - extern "C" { Processes::Private* processes_new(Processes*, void (*)(Processes*), void (*)(const Processes*, quintptr, bool), diff --git a/demo/src/Bindings.h b/demo/src/Bindings.h index 0dbf7fd..847d956 100644 --- a/demo/src/Bindings.h +++ b/demo/src/Bindings.h @@ -91,7 +91,6 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; @@ -133,7 +132,6 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; @@ -181,7 +179,6 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; @@ -227,7 +224,6 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; @@ -239,6 +235,7 @@ 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); + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Q_INVOKABLE QVariant input(int row) const; Q_INVOKABLE bool setInput(int row, const QVariant& value); Q_INVOKABLE QVariant result(int row) const; diff --git a/src/cpp.cpp b/src/cpp.cpp index 428cc3d..6bb01d1 100644 --- a/src/cpp.cpp +++ b/src/cpp.cpp @@ -35,11 +35,18 @@ QString cGetType(const BindingTypeProperties& type) { return type.name + "*, " + type.name.toLower() + "_set"; } +bool modelIsWritable(const Object& o) { + bool write = false; + for (auto ip: o.itemProperties) { + write |= ip.write; + } + return write; +} + void writeHeaderItemModel(QTextStream& h, const Object& o) { h << QString(R"( int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; @@ -52,6 +59,9 @@ void writeHeaderItemModel(QTextStream& h, const Object& o) { 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); )"); + if (modelIsWritable(o)) { + h << " bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;\n"; + } for (auto ip: o.itemProperties) { if (o.type == ObjectType::List) { h << QString(" Q_INVOKABLE QVariant %1(int row) const;\n").arg(ip.name); @@ -376,40 +386,41 @@ bool %1::setHeaderData(int section, Qt::Orientation orientation, const QVariant return true; } -bool %1::setData(const QModelIndex &index, const QVariant &value, int role) -{ )").arg(o.name); - for (int col = 0; col < o.columnCount; ++col) { - if (!isColumnWrite(o, col)) { - continue; - } - cpp << " if (index.column() == " << col << ") {\n"; - for (int i = 0; i < o.itemProperties.size(); ++i) { - auto ip = o.itemProperties[i]; - if (!ip.write) { + if (modelIsWritable(o)) { + cpp << QString("bool %1::setData(const QModelIndex &index, const QVariant &value, int role)\n{\n").arg(o.name); + for (int col = 0; col < o.columnCount; ++col) { + if (!isColumnWrite(o, col)) { continue; } - auto roles = ip.roles.value(col); - if (col > 0 && roles.size() == 0) { - continue; + cpp << " if (index.column() == " << col << ") {\n"; + for (int i = 0; i < o.itemProperties.size(); ++i) { + auto ip = o.itemProperties[i]; + if (!ip.write) { + continue; + } + auto roles = ip.roles.value(col); + if (col > 0 && roles.size() == 0) { + continue; + } + cpp << " if ("; + for (auto role: roles) { + cpp << QString("role == Qt::%1 || ").arg(metaRoles.valueToKey(role)); + } + cpp << "role == Qt::UserRole + " << i << ") {\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)); + } + cpp << " }\n"; } - cpp << " if ("; - for (auto role: roles) { - cpp << QString("role == Qt::%1 || ").arg(metaRoles.valueToKey(role)); - } - cpp << "role == Qt::UserRole + " << i << ") {\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)); - } - cpp << " }\n"; + cpp << " }\n"; } - cpp << " }\n"; + cpp << " return false;\n}\n\n"; } - cpp << " return false;\n}\n\n"; } void writeHeaderObject(QTextStream& h, const Object& o, const Configuration& conf) { diff --git a/tests/test_list_rust.h b/tests/test_list_rust.h index 1902464..afcaba5 100644 --- a/tests/test_list_rust.h +++ b/tests/test_list_rust.h @@ -22,7 +22,6 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; @@ -34,6 +33,7 @@ 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); + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Q_INVOKABLE QVariant userName(int row) const; Q_INVOKABLE bool setUserName(int row, const QVariant& value); diff --git a/tests/test_tree_rust.h b/tests/test_tree_rust.h index f2180be..4499b59 100644 --- a/tests/test_tree_rust.h +++ b/tests/test_tree_rust.h @@ -22,7 +22,6 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; @@ -34,6 +33,7 @@ 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); + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Q_INVOKABLE QVariant userName(const QModelIndex& index) const; Q_INVOKABLE bool setUserName(const QModelIndex& index, const QVariant& value);