From 22fb061686d02ad3c74ce5b710ecb5fdaa1e3be6 Mon Sep 17 00:00:00 2001 From: Marius Kittler Date: Sun, 20 May 2018 01:48:31 +0200 Subject: [PATCH] Make use of models in QML more convenient --- model/entrymodel.cpp | 21 +++++++++++++++++++-- model/entrymodel.h | 28 +++++++++++++++++----------- model/fieldmodel.cpp | 8 ++++---- model/fieldmodel.h | 15 +++++++++------ 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/model/entrymodel.cpp b/model/entrymodel.cpp index d871663..50b8b1b 100644 --- a/model/entrymodel.cpp +++ b/model/entrymodel.cpp @@ -1,7 +1,7 @@ #include "./entrymodel.h" #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS -#include "./gui/undocommands.h" +#include "../gui/undocommands.h" #endif #include @@ -19,7 +19,6 @@ using namespace std; using namespace Io; namespace QtGui { - /*! * \class EntryModel * \brief The EntryModel class provides a model interface for a hierarchy of Entry instances. @@ -567,4 +566,22 @@ Qt::DropActions EntryModel::supportedDropActions() const return Qt::MoveAction; } +/*! + * \brief Sets the insert type to node. + * \remarks Intended to prevent the offort of exposing EntryType to QML. + */ +void EntryModel::setInsertTypeToNode() +{ + setInsertType(Io::EntryType::Node); +} + +/*! + * \brief Sets the insert type to account. + * \remarks Intended to prevent the offort of exposing EntryType to QML. + */ +void EntryModel::setInsertTypeToAccount() +{ + setInsertType(Io::EntryType::Account); +} + } // namespace QtGui diff --git a/model/entrymodel.h b/model/entrymodel.h index cf43d22..c5210ce 100644 --- a/model/entrymodel.h +++ b/model/entrymodel.h @@ -2,7 +2,7 @@ #define ENTRYMODEL_H #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS -#include "gui/stacksupport.h" +#include "../gui/stacksupport.h" #endif #include @@ -12,7 +12,7 @@ namespace Io { class Entry; class NodeEntry; -DECLARE_ENUM_CLASS(EntryType, int); +enum class EntryType; } // namespace Io namespace QtGui { @@ -32,6 +32,9 @@ class EntryModel : public QAbstractItemModel #endif { Q_OBJECT + Q_PROPERTY(Io::NodeEntry *rootEntry READ rootEntry WRITE setRootEntry) + Q_PROPERTY(Io::EntryType insertType READ insertType WRITE setInsertType) + public: explicit EntryModel(QObject *parent = nullptr); #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS @@ -41,31 +44,33 @@ public: QHash roleNames() const; Io::NodeEntry *rootEntry(); void setRootEntry(Io::NodeEntry *entry); - Io::Entry *entry(const QModelIndex &index); - QList takeEntries(int row, int count, const QModelIndex &parent); - bool insertEntries(int row, const QModelIndex &parent, const QList &entries); + Q_INVOKABLE Io::Entry *entry(const QModelIndex &index); + Q_INVOKABLE QList takeEntries(int row, int count, const QModelIndex &parent); + Q_INVOKABLE bool insertEntries(int row, const QModelIndex &parent, const QList &entries); Io::EntryType insertType() const; void setInsertType(Io::EntryType type); QModelIndex index(int row, int column, const QModelIndex &parent) const; QModelIndex index(Io::Entry *entry) const; QModelIndex parent(const QModelIndex &child) const; bool hasChildren(const QModelIndex &parent) const; - bool isNode(const QModelIndex &parent) const; + Q_INVOKABLE bool isNode(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; QMap itemData(const QModelIndex &index) const; bool setData(const QModelIndex &index, const QVariant &value, int role); bool setItemData(const QModelIndex &index, const QMap &roles); Qt::ItemFlags flags(const QModelIndex &index) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; - int rowCount(const QModelIndex &parent) const; - int columnCount(const QModelIndex &parent) const; - bool insertRows(int row, int count, const QModelIndex &parent); - bool removeRows(int row, int count, const QModelIndex &parent); - bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild); + Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const; + Q_INVOKABLE int columnCount(const QModelIndex &parent = QModelIndex()) const; + Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()); + Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); + Q_INVOKABLE bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild); QStringList mimeTypes() const; QMimeData *mimeData(const QModelIndexList &indexes) const; bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); Qt::DropActions supportedDropActions() const; + Q_INVOKABLE void setInsertTypeToNode(); + Q_INVOKABLE void setInsertTypeToAccount(); public Q_SLOTS: void reset(); @@ -122,6 +127,7 @@ inline void EntryModel::setInsertType(Io::EntryType type) { m_insertType = type; } + } // namespace QtGui #endif // ENTRYMODEL_H diff --git a/model/fieldmodel.cpp b/model/fieldmodel.cpp index a24ea1e..325e991 100644 --- a/model/fieldmodel.cpp +++ b/model/fieldmodel.cpp @@ -1,7 +1,7 @@ #include "./fieldmodel.h" #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS -#include "./gui/undocommands.h" +#include "../gui/undocommands.h" #endif #include @@ -52,9 +52,9 @@ FieldModel::FieldModel(QUndoStack *undoStack, QObject *parent) QHash FieldModel::roleNames() const { static const QHash roles{ - { FieldModelRoles::FieldTypeRole, "key" }, + { FieldModelRoles::FieldTypeRole, "fieldType" }, { FieldModelRoles::Key, "key" }, - { FieldModelRoles::Value, "key" }, + { FieldModelRoles::Value, "value" }, { FieldModelRoles::IsPassword, "isPassword" }, }; return roles; @@ -297,7 +297,7 @@ bool FieldModel::insertRows(int row, int count, const QModelIndex &parent) return push(new FieldModelInsertRowsCommand(this, row, count)); } #endif - if (parent.isValid() || row < 0 || count <= 0 || static_cast(row + count) > m_fields->size()) { + if (parent.isValid() || row < 0 || count <= 0 || static_cast(row) > m_fields->size()) { return false; } beginInsertRows(parent, row, row + count - 1); diff --git a/model/fieldmodel.h b/model/fieldmodel.h index d2d2f73..75b1d3f 100644 --- a/model/fieldmodel.h +++ b/model/fieldmodel.h @@ -2,7 +2,7 @@ #define FIELDMODEL_H #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS -#include "gui/stacksupport.h" +#include "../gui/stacksupport.h" #endif #include @@ -43,6 +43,9 @@ class FieldModel : public QAbstractTableModel #endif { Q_OBJECT + Q_PROPERTY(Io::AccountEntry *accountEntry READ accountEntry WRITE setAccountEntry) + Q_PROPERTY(PasswordVisibility passwordVisibility READ passwordVisibility WRITE setPasswordVisibility) + public: explicit FieldModel(QObject *parent = nullptr); #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS @@ -60,14 +63,14 @@ public: bool setData(const QModelIndex &index, const QVariant &value, int role); Qt::ItemFlags flags(const QModelIndex &index) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; - int rowCount(const QModelIndex &parent = QModelIndex()) const; - int columnCount(const QModelIndex &parent = QModelIndex()) const; - bool insertRows(int row, int count, const QModelIndex &parent); - bool removeRows(int row, int count, const QModelIndex &parent); + Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const; + Q_INVOKABLE int columnCount(const QModelIndex &parent = QModelIndex()) const; + Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()); + Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); QStringList mimeTypes() const; QMimeData *mimeData(const QModelIndexList &indexes) const; - const Io::Field *field(std::size_t row) const; + Q_INVOKABLE const Io::Field *field(std::size_t row) const; public Q_SLOTS: void setPasswordVisibility(PasswordVisibility passwordVisibility);