keepassxc

Форк
0
/
IconModels.cpp 
106 строк · 2.4 Кб
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 "IconModels.h"
19

20
#include <QUuid>
21

22
#include "gui/DatabaseIcons.h"
23

24
DefaultIconModel::DefaultIconModel(QObject* parent)
25
    : QAbstractListModel(parent)
26
{
27
}
28

29
int DefaultIconModel::rowCount(const QModelIndex& parent) const
30
{
31
    if (!parent.isValid()) {
32
        return databaseIcons()->count();
33
    } else {
34
        return 0;
35
    }
36
}
37

38
QVariant DefaultIconModel::data(const QModelIndex& index, int role) const
39
{
40
    if (!index.isValid()) {
41
        return {};
42
    }
43

44
    Q_ASSERT(index.row() < databaseIcons()->count());
45

46
    if (role == Qt::DecorationRole) {
47
        return databaseIcons()->icon(index.row(), IconSize::Medium);
48
    }
49

50
    return {};
51
}
52

53
CustomIconModel::CustomIconModel(QObject* parent)
54
    : QAbstractListModel(parent)
55
{
56
}
57

58
void CustomIconModel::setIcons(const QHash<QUuid, QPixmap>& icons, const QList<QUuid>& iconsOrder)
59
{
60
    beginResetModel();
61

62
    m_icons = icons;
63
    m_iconsOrder = iconsOrder;
64
    Q_ASSERT(m_icons.count() == m_iconsOrder.count());
65

66
    endResetModel();
67
}
68

69
int CustomIconModel::rowCount(const QModelIndex& parent) const
70
{
71
    if (!parent.isValid()) {
72
        return m_icons.size();
73
    } else {
74
        return 0;
75
    }
76
}
77

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

84
    if (role == Qt::DecorationRole) {
85
        QUuid uuid = uuidFromIndex(index);
86
        return m_icons.value(uuid);
87
    }
88

89
    return {};
90
}
91

92
QUuid CustomIconModel::uuidFromIndex(const QModelIndex& index) const
93
{
94
    Q_ASSERT(index.isValid());
95

96
    return m_iconsOrder.value(index.row());
97
}
98

99
QModelIndex CustomIconModel::indexFromUuid(const QUuid& uuid) const
100
{
101
    int idx = m_iconsOrder.indexOf(uuid);
102
    if (idx > -1) {
103
        return index(idx, 0);
104
    }
105
    return {};
106
}
107

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

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

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

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