Turn EntryAttachmentsModel into a QAbstractListModel.

This commit is contained in:
Felix Geyer 2012-05-02 10:30:30 +02:00
parent 93982aa0c9
commit f8f52419c8
3 changed files with 21 additions and 29 deletions

View File

@ -21,7 +21,7 @@
#include "core/Tools.h" #include "core/Tools.h"
EntryAttachmentsModel::EntryAttachmentsModel(QObject* parent) EntryAttachmentsModel::EntryAttachmentsModel(QObject* parent)
: QAbstractTableModel(parent) : QAbstractListModel(parent)
, m_entryAttachments(0) , m_entryAttachments(0)
{ {
} }
@ -63,21 +63,7 @@ int EntryAttachmentsModel::columnCount(const QModelIndex& parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
return 2; return 1;
}
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();
} }
QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const
@ -86,18 +72,24 @@ QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const
return QVariant(); return QVariant();
} }
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole && index.column() == 0) {
QString key = m_entryAttachments->keys().at(index.row()); QString key = keyByIndex(index);
if (index.column() == 0) { return QString("%1 (%2)").arg(key,
return key; Tools::humanReadableFileSize(m_entryAttachments->value(key).size()));
} }
else { else {
return Tools::humanReadableFileSize(m_entryAttachments->value(key).size()); return QVariant();
} }
}
QString EntryAttachmentsModel::keyByIndex(const QModelIndex& index) const
{
if (!index.isValid()) {
return QString();
} }
return QVariant(); return m_entryAttachments->keys().at(index.row());
} }
void EntryAttachmentsModel::attachmentChange(const QString& key) void EntryAttachmentsModel::attachmentChange(const QString& key)

View File

@ -18,11 +18,11 @@
#ifndef KEEPASSX_ENTRYATTACHMENTSMODEL_H #ifndef KEEPASSX_ENTRYATTACHMENTSMODEL_H
#define KEEPASSX_ENTRYATTACHMENTSMODEL_H #define KEEPASSX_ENTRYATTACHMENTSMODEL_H
#include <QtCore/QAbstractTableModel> #include <QtCore/QAbstractListModel>
class EntryAttachments; class EntryAttachments;
class EntryAttachmentsModel : public QAbstractTableModel class EntryAttachmentsModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
@ -31,8 +31,8 @@ public:
void setEntryAttachments(EntryAttachments* entry); void setEntryAttachments(EntryAttachments* entry);
int rowCount(const QModelIndex& parent = QModelIndex()) const; int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(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; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
QString keyByIndex(const QModelIndex& index) const;
private Q_SLOTS: private Q_SLOTS:
void attachmentChange(const QString& key); void attachmentChange(const QString& key);

View File

@ -117,7 +117,7 @@ void TestEntryModel::testAttachmentsModel()
entryAttachments->set("2nd", QByteArray("456")); entryAttachments->set("2nd", QByteArray("456"));
entryAttachments->set("2nd", QByteArray("789")); 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"); entryAttachments->remove("first");