Make use of models in QML more convenient

This commit is contained in:
Marius Kittler 2018-05-20 01:48:31 +02:00
parent 51d9190199
commit 22fb061686
4 changed files with 49 additions and 23 deletions

View File

@ -1,7 +1,7 @@
#include "./entrymodel.h"
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
#include "./gui/undocommands.h"
#include "../gui/undocommands.h"
#endif
#include <passwordfile/io/entry.h>
@ -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

View File

@ -2,7 +2,7 @@
#define ENTRYMODEL_H
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
#include "gui/stacksupport.h"
#include "../gui/stacksupport.h"
#endif
#include <c++utilities/application/global.h>
@ -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<int, QByteArray> roleNames() const;
Io::NodeEntry *rootEntry();
void setRootEntry(Io::NodeEntry *entry);
Io::Entry *entry(const QModelIndex &index);
QList<Io::Entry *> takeEntries(int row, int count, const QModelIndex &parent);
bool insertEntries(int row, const QModelIndex &parent, const QList<Io::Entry *> &entries);
Q_INVOKABLE Io::Entry *entry(const QModelIndex &index);
Q_INVOKABLE QList<Io::Entry *> takeEntries(int row, int count, const QModelIndex &parent);
Q_INVOKABLE bool insertEntries(int row, const QModelIndex &parent, const QList<Io::Entry *> &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<int, QVariant> itemData(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &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

View File

@ -1,7 +1,7 @@
#include "./fieldmodel.h"
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
#include "./gui/undocommands.h"
#include "../gui/undocommands.h"
#endif
#include <passwordfile/io/field.h>
@ -52,9 +52,9 @@ FieldModel::FieldModel(QUndoStack *undoStack, QObject *parent)
QHash<int, QByteArray> FieldModel::roleNames() const
{
static const QHash<int, QByteArray> 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<size_t>(row + count) > m_fields->size()) {
if (parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row) > m_fields->size()) {
return false;
}
beginInsertRows(parent, row, row + count - 1);

View File

@ -2,7 +2,7 @@
#define FIELDMODEL_H
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
#include "gui/stacksupport.h"
#include "../gui/stacksupport.h"
#endif
#include <passwordfile/io/entry.h>
@ -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);