keepassxc

Форк
0
/
EntryAttachmentsModel.cpp 
177 строк · 5.1 Кб
1
/*
2
 *  Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
3
 *
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.
8
 *
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.
13
 *
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/>.
16
 */
17

18
#include "EntryAttachmentsModel.h"
19

20
#include "core/EntryAttachments.h"
21
#include "core/Tools.h"
22

23
EntryAttachmentsModel::EntryAttachmentsModel(QObject* parent)
24
    : QAbstractListModel(parent)
25
    , m_entryAttachments(nullptr)
26
{
27
    m_headers << tr("Name") << tr("Size");
28
}
29

30
void EntryAttachmentsModel::setEntryAttachments(EntryAttachments* entryAttachments)
31
{
32
    beginResetModel();
33

34
    if (m_entryAttachments) {
35
        m_entryAttachments->disconnect(this);
36
    }
37

38
    m_entryAttachments = entryAttachments;
39

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()));
48
    }
49

50
    endResetModel();
51
}
52

53
int EntryAttachmentsModel::rowCount(const QModelIndex& parent) const
54
{
55
    if (!m_entryAttachments || parent.isValid()) {
56
        return 0;
57
    } else {
58
        return m_entryAttachments->keys().size();
59
    }
60
}
61

62
int EntryAttachmentsModel::columnCount(const QModelIndex& parent) const
63
{
64
    Q_UNUSED(parent);
65

66
    return Columns::ColumnsCount;
67
}
68

69
QVariant EntryAttachmentsModel::headerData(int section, Qt::Orientation orientation, int role) const
70
{
71
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
72
        Q_ASSERT(m_headers.size() == columnCount());
73
        return m_headers[section];
74
    }
75

76
    return QAbstractListModel::headerData(section, orientation, role);
77
}
78

79
QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const
80
{
81
    if (!index.isValid()) {
82
        return {};
83
    }
84

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) {
89
            return key;
90
        } else if (column == SizeColumn) {
91
            const int attachmentSize = m_entryAttachments->value(key).size();
92
            if (role == Qt::DisplayRole) {
93
                return Tools::humanReadableFileSize(attachmentSize);
94
            }
95
            return attachmentSize;
96
        }
97
    }
98

99
    return {};
100
}
101

102
bool EntryAttachmentsModel::setData(const QModelIndex& index, const QVariant& value, int role)
103
{
104
    if (!m_readOnly && index.column() == Columns::NameColumn) {
105
        const QString key = value.toString().trimmed();
106
        if (key.isEmpty() || m_entryAttachments->hasKey(key)) {
107
            return false;
108
        }
109
        m_entryAttachments->rename(keyByIndex(index), key);
110
        return true;
111
    }
112
    return QAbstractListModel::setData(index, value, role);
113
}
114

115
Qt::ItemFlags EntryAttachmentsModel::flags(const QModelIndex& index) const
116
{
117
    Qt::ItemFlags flags = QAbstractListModel::flags(index);
118
    if (!m_readOnly && index.column() == Columns::NameColumn) {
119
        flags = flags | Qt::ItemIsEditable;
120
    }
121
    return flags;
122
}
123

124
QString EntryAttachmentsModel::keyByIndex(const QModelIndex& index) const
125
{
126
    if (!index.isValid()) {
127
        return {};
128
    }
129

130
    return m_entryAttachments->keys().at(index.row());
131
}
132

133
void EntryAttachmentsModel::attachmentChange(const QString& key)
134
{
135
    int row = m_entryAttachments->keys().indexOf(key);
136
    emit dataChanged(index(row, 0), index(row, columnCount() - 1));
137
}
138

139
void EntryAttachmentsModel::attachmentAboutToAdd(const QString& key)
140
{
141
    QList<QString> rows = m_entryAttachments->keys();
142
    rows.append(key);
143
    std::sort(rows.begin(), rows.end());
144
    int row = rows.indexOf(key);
145
    beginInsertRows(QModelIndex(), row, row);
146
}
147

148
void EntryAttachmentsModel::attachmentAdd()
149
{
150
    endInsertRows();
151
}
152

153
void EntryAttachmentsModel::attachmentAboutToRemove(const QString& key)
154
{
155
    int row = m_entryAttachments->keys().indexOf(key);
156
    beginRemoveRows(QModelIndex(), row, row);
157
}
158

159
void EntryAttachmentsModel::attachmentRemove()
160
{
161
    endRemoveRows();
162
}
163

164
void EntryAttachmentsModel::aboutToReset()
165
{
166
    beginResetModel();
167
}
168

169
void EntryAttachmentsModel::reset()
170
{
171
    endResetModel();
172
}
173

174
void EntryAttachmentsModel::setReadOnly(bool readOnly)
175
{
176
    m_readOnly = readOnly;
177
}
178

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.