mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2024-10-01 01:26:01 -04:00
Turn EntryAttachmentsModel into a QAbstractListModel.
This commit is contained in:
parent
93982aa0c9
commit
f8f52419c8
@ -21,7 +21,7 @@
|
||||
#include "core/Tools.h"
|
||||
|
||||
EntryAttachmentsModel::EntryAttachmentsModel(QObject* parent)
|
||||
: QAbstractTableModel(parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_entryAttachments(0)
|
||||
{
|
||||
}
|
||||
@ -63,21 +63,7 @@ int EntryAttachmentsModel::columnCount(const QModelIndex& parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
QVariant EntryAttachmentsModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)) {
|
||||
switch (section) {
|
||||
case 0:
|
||||
return tr("Name");
|
||||
case 1:
|
||||
return tr("Size");
|
||||
}
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
return 1;
|
||||
}
|
||||
|
||||
QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const
|
||||
@ -86,18 +72,24 @@ QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
if (role == Qt::DisplayRole) {
|
||||
QString key = m_entryAttachments->keys().at(index.row());
|
||||
if (role == Qt::DisplayRole && index.column() == 0) {
|
||||
QString key = keyByIndex(index);
|
||||
|
||||
if (index.column() == 0) {
|
||||
return key;
|
||||
return QString("%1 (%2)").arg(key,
|
||||
Tools::humanReadableFileSize(m_entryAttachments->value(key).size()));
|
||||
}
|
||||
else {
|
||||
return Tools::humanReadableFileSize(m_entryAttachments->value(key).size());
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
QString EntryAttachmentsModel::keyByIndex(const QModelIndex& index) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
return m_entryAttachments->keys().at(index.row());
|
||||
}
|
||||
|
||||
void EntryAttachmentsModel::attachmentChange(const QString& key)
|
||||
|
@ -18,11 +18,11 @@
|
||||
#ifndef KEEPASSX_ENTRYATTACHMENTSMODEL_H
|
||||
#define KEEPASSX_ENTRYATTACHMENTSMODEL_H
|
||||
|
||||
#include <QtCore/QAbstractTableModel>
|
||||
#include <QtCore/QAbstractListModel>
|
||||
|
||||
class EntryAttachments;
|
||||
|
||||
class EntryAttachmentsModel : public QAbstractTableModel
|
||||
class EntryAttachmentsModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@ -31,8 +31,8 @@ public:
|
||||
void setEntryAttachments(EntryAttachments* entry);
|
||||
int rowCount(const QModelIndex& parent = QModelIndex()) const;
|
||||
int columnCount(const QModelIndex& parent = QModelIndex()) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
|
||||
QString keyByIndex(const QModelIndex& index) const;
|
||||
|
||||
private Q_SLOTS:
|
||||
void attachmentChange(const QString& key);
|
||||
|
@ -117,7 +117,7 @@ void TestEntryModel::testAttachmentsModel()
|
||||
entryAttachments->set("2nd", QByteArray("456"));
|
||||
entryAttachments->set("2nd", QByteArray("789"));
|
||||
|
||||
QCOMPARE(model->data(model->index(0, 0)).toString(), QString("2nd"));
|
||||
QCOMPARE(model->data(model->index(0, 0)).toString().left(4), QString("2nd "));
|
||||
|
||||
entryAttachments->remove("first");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user