Make models ready for use with Qt Quick

This commit is contained in:
Martchus 2018-04-22 21:00:39 +02:00
parent 65c8797079
commit 51d9190199
4 changed files with 57 additions and 8 deletions

View File

@ -54,6 +54,15 @@ EntryModel::EntryModel(QUndoStack *undoStack, QObject *parent)
}
#endif
QHash<int, QByteArray> EntryModel::roleNames() const
{
static const QHash<int, QByteArray> roles{
{ Qt::DisplayRole, "name" },
{ EntryModelRoles::DefaultExpandedRole, "isDefaultExpanded" },
};
return roles;
}
/*!
* \brief Returns the Entry for the specified \a index of nullptr if the \a index is invalid.
*

View File

@ -22,7 +22,7 @@ namespace QtGui {
*/
enum EntryModelRoles {
SerializedRole = Qt::UserRole + 1, /**< the entry (including descendants) in serialized from (QByteArray) */
DefaultExpandedRole = Qt::UserRole + 2 /**< whether the entry should be expanded by default */
DefaultExpandedRole, /**< whether the entry should be expanded by default */
};
class EntryModel : public QAbstractItemModel
@ -38,6 +38,7 @@ public:
explicit EntryModel(QUndoStack *undoStack, QObject *parent = nullptr);
#endif
QHash<int, QByteArray> roleNames() const;
Io::NodeEntry *rootEntry();
void setRootEntry(Io::NodeEntry *entry);
Io::Entry *entry(const QModelIndex &index);
@ -45,7 +46,6 @@ public:
bool insertEntries(int row, const QModelIndex &parent, const QList<Io::Entry *> &entries);
Io::EntryType insertType() const;
void setInsertType(Io::EntryType type);
bool hidePasswords() const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex index(Io::Entry *entry) const;
QModelIndex parent(const QModelIndex &child) const;

View File

@ -49,6 +49,17 @@ FieldModel::FieldModel(QUndoStack *undoStack, QObject *parent)
}
#endif
QHash<int, QByteArray> FieldModel::roleNames() const
{
static const QHash<int, QByteArray> roles{
{ FieldModelRoles::FieldTypeRole, "key" },
{ FieldModelRoles::Key, "key" },
{ FieldModelRoles::Value, "key" },
{ FieldModelRoles::IsPassword, "isPassword" },
};
return roles;
}
/*!
* \brief Sets the account entry. Causes a model reset.
*
@ -92,10 +103,20 @@ QVariant FieldModel::data(const QModelIndex &index, int role) const
break;
case FieldTypeRole:
return static_cast<int>((*m_fields)[static_cast<size_t>(index.row())].type());
case Key:
return QString::fromStdString((*m_fields)[static_cast<size_t>(index.row())].name());
case Value:
return (m_passwordVisibility == PasswordVisibility::Always || role == Qt::EditRole
|| (*m_fields)[static_cast<size_t>(index.row())].type() != FieldType::Password)
? QString::fromStdString((*m_fields)[static_cast<size_t>(index.row())].value())
: QString((*m_fields)[static_cast<size_t>(index.row())].value().size(), QChar(0x2022));
case IsPassword:
return (*m_fields)[static_cast<size_t>(index.row())].type() == FieldType::Password;
default:;
}
// return data for empty field at the end which enables the user to append fields
} else if (static_cast<size_t>(index.row()) == m_fields->size()) {
// return data for empty field at the end which enables the user to append fields
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
@ -146,11 +167,11 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
switch (index.column()) {
case 0:
m_fields->at(index.row()).setName(value.toString().toStdString());
roles << role;
roles << Qt::EditRole << Key;
break;
case 1:
m_fields->at(index.row()).setValue(value.toString().toStdString());
roles << role;
roles << Qt::EditRole << Value;
break;
default:;
}
@ -159,11 +180,23 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
bool ok;
int fieldType = value.toInt(&ok);
if (ok && Field::isValidType(fieldType)) {
roles << role;
m_fields->at(index.row()).setType(static_cast<FieldType>(fieldType));
roles << FieldTypeRole << IsPassword;
}
break;
}
case Key:
m_fields->at(index.row()).setName(value.toString().toStdString());
roles << Qt::EditRole << Key;
break;
case Value:
m_fields->at(index.row()).setValue(value.toString().toStdString());
roles << Qt::EditRole << Value;
break;
case IsPassword:
m_fields->at(index.row()).setType(value.toBool() ? FieldType::Password : FieldType::Normal);
roles << FieldTypeRole << IsPassword;
break;
default:;
}
// remove last field if empty, showing an empty field at the end to enabled appending new rows is provided by the data method
@ -205,10 +238,13 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
// some roles affect other roles
switch (role) {
case Qt::EditRole:
case Key:
case Value:
roles << Qt::DisplayRole;
break;
case FieldTypeRole:
roles << Qt::DisplayRole << Qt::EditRole;
case IsPassword:
roles << Qt::DisplayRole << Qt::EditRole << Key;
break;
default:;
}

View File

@ -21,7 +21,10 @@ namespace QtGui {
* \brief The FieldModelRoles enum defines custom roles for the FieldModel class.
*/
enum FieldModelRoles {
FieldTypeRole = Qt::UserRole + 1 /**< the field type */
FieldTypeRole = Qt::UserRole + 1, /**< the field type */
Key,
Value,
IsPassword,
};
/*!
@ -46,6 +49,7 @@ public:
explicit FieldModel(QUndoStack *undoStack, QObject *parent = nullptr);
#endif
QHash<int, QByteArray> roleNames() const;
Io::AccountEntry *accountEntry();
const Io::AccountEntry *accountEntry() const;
void setAccountEntry(Io::AccountEntry *entry);