2
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "EntryAttachmentsModel.h"
20
#include "core/EntryAttachments.h"
21
#include "core/Tools.h"
23
EntryAttachmentsModel::EntryAttachmentsModel(QObject* parent)
24
: QAbstractListModel(parent)
25
, m_entryAttachments(nullptr)
27
m_headers << tr("Name") << tr("Size");
30
void EntryAttachmentsModel::setEntryAttachments(EntryAttachments* entryAttachments)
34
if (m_entryAttachments) {
35
m_entryAttachments->disconnect(this);
38
m_entryAttachments = entryAttachments;
40
if (m_entryAttachments) {
41
connect(m_entryAttachments, SIGNAL(keyModified(QString)), SLOT(attachmentChange(QString)));
42
connect(m_entryAttachments, SIGNAL(aboutToBeAdded(QString)), SLOT(attachmentAboutToAdd(QString)));
43
connect(m_entryAttachments, SIGNAL(added(QString)), SLOT(attachmentAdd()));
44
connect(m_entryAttachments, SIGNAL(aboutToBeRemoved(QString)), SLOT(attachmentAboutToRemove(QString)));
45
connect(m_entryAttachments, SIGNAL(removed(QString)), SLOT(attachmentRemove()));
46
connect(m_entryAttachments, SIGNAL(aboutToBeReset()), SLOT(aboutToReset()));
47
connect(m_entryAttachments, SIGNAL(reset()), SLOT(reset()));
53
int EntryAttachmentsModel::rowCount(const QModelIndex& parent) const
55
if (!m_entryAttachments || parent.isValid()) {
58
return m_entryAttachments->keys().size();
62
int EntryAttachmentsModel::columnCount(const QModelIndex& parent) const
66
return Columns::ColumnsCount;
69
QVariant EntryAttachmentsModel::headerData(int section, Qt::Orientation orientation, int role) const
71
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
72
Q_ASSERT(m_headers.size() == columnCount());
73
return m_headers[section];
76
return QAbstractListModel::headerData(section, orientation, role);
79
QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const
81
if (!index.isValid()) {
85
if (role == Qt::DisplayRole || role == Qt::EditRole) {
86
const QString key = keyByIndex(index);
87
const int column = index.column();
88
if (column == Columns::NameColumn) {
90
} else if (column == SizeColumn) {
91
const int attachmentSize = m_entryAttachments->value(key).size();
92
if (role == Qt::DisplayRole) {
93
return Tools::humanReadableFileSize(attachmentSize);
95
return attachmentSize;
102
bool EntryAttachmentsModel::setData(const QModelIndex& index, const QVariant& value, int role)
104
if (!m_readOnly && index.column() == Columns::NameColumn) {
105
const QString key = value.toString().trimmed();
106
if (key.isEmpty() || m_entryAttachments->hasKey(key)) {
109
m_entryAttachments->rename(keyByIndex(index), key);
112
return QAbstractListModel::setData(index, value, role);
115
Qt::ItemFlags EntryAttachmentsModel::flags(const QModelIndex& index) const
117
Qt::ItemFlags flags = QAbstractListModel::flags(index);
118
if (!m_readOnly && index.column() == Columns::NameColumn) {
119
flags = flags | Qt::ItemIsEditable;
124
QString EntryAttachmentsModel::keyByIndex(const QModelIndex& index) const
126
if (!index.isValid()) {
130
return m_entryAttachments->keys().at(index.row());
133
void EntryAttachmentsModel::attachmentChange(const QString& key)
135
int row = m_entryAttachments->keys().indexOf(key);
136
emit dataChanged(index(row, 0), index(row, columnCount() - 1));
139
void EntryAttachmentsModel::attachmentAboutToAdd(const QString& key)
141
QList<QString> rows = m_entryAttachments->keys();
143
std::sort(rows.begin(), rows.end());
144
int row = rows.indexOf(key);
145
beginInsertRows(QModelIndex(), row, row);
148
void EntryAttachmentsModel::attachmentAdd()
153
void EntryAttachmentsModel::attachmentAboutToRemove(const QString& key)
155
int row = m_entryAttachments->keys().indexOf(key);
156
beginRemoveRows(QModelIndex(), row, row);
159
void EntryAttachmentsModel::attachmentRemove()
164
void EntryAttachmentsModel::aboutToReset()
169
void EntryAttachmentsModel::reset()
174
void EntryAttachmentsModel::setReadOnly(bool readOnly)
176
m_readOnly = readOnly;