Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DatabaseIcons.cpp « gui « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f83ce9ac435947d981424cf77fd4dda8c99b5c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 *  Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "DatabaseIcons.h"

#include "core/Config.h"
#include "core/Global.h"

#include <QDir>
#include <QImageReader>
#include <QPainter>
#include <QPixmapCache>

DatabaseIcons* DatabaseIcons::m_instance(nullptr);

namespace
{
    const QString iconDir = QStringLiteral(":/icons/database/");
    QStringList iconList;

    const QString badgeDir = QStringLiteral(":/icons/badges/");
    QStringList badgeList;
} // namespace

DatabaseIcons::DatabaseIcons()
{
    iconList = QDir(iconDir).entryList(QDir::NoFilter, QDir::Name);
    badgeList = QDir(badgeDir).entryList(QDir::NoFilter, QDir::Name);

    // Set this early and once to ensure consistent icon size until app restart
    m_compactMode = config()->get(Config::GUI_CompactMode).toBool();
}

DatabaseIcons* DatabaseIcons::instance()
{
    if (!m_instance) {
        m_instance = new DatabaseIcons();
    }

    return m_instance;
}

QPixmap DatabaseIcons::icon(int index, IconSize size)
{
    if (index < 0 || index >= count()) {
        qWarning("DatabaseIcons::icon: invalid icon index %d, using 0 instead", index);
        index = 0;
        Q_ASSERT_X(false, "DatabaseIcons::icon", "invalid icon index %d");
    }

    auto cacheKey = QString::number(index);
    auto icon = m_iconCache.value(cacheKey);
    if (icon.isNull()) {
        icon.addFile(iconDir + iconList[index]);
        icon.addPixmap(icon.pixmap(64));
        m_iconCache.insert(cacheKey, icon);
    }

    return icon.pixmap(iconSize(size));
}

QPixmap DatabaseIcons::applyBadge(const QPixmap& basePixmap, Badges badgeIndex)
{
    const auto cacheKey = QStringLiteral("badgedicon-%1-%2").arg(basePixmap.cacheKey()).arg(badgeIndex);
    QPixmap pixmap = basePixmap;
    if (badgeIndex < 0 || badgeIndex >= badgeList.size()) {
        qWarning("DatabaseIcons: Out-of-range badge index given to applyBadge: %d", badgeIndex);
    } else if (!QPixmapCache::find(cacheKey, &pixmap)) {
        int baseSize = basePixmap.width();
        int badgeSize =
            baseSize <= iconSize(IconSize::Default) * basePixmap.devicePixelRatio() ? baseSize * 0.6 : baseSize * 0.5;
        QPoint badgePos(baseSize - badgeSize, baseSize - badgeSize);
        badgePos /= basePixmap.devicePixelRatio();

        QImageReader reader(badgeDir + badgeList[badgeIndex]);
        reader.setScaledSize({badgeSize, badgeSize});
        auto badge = QPixmap::fromImageReader(&reader);
        badge.setDevicePixelRatio(basePixmap.devicePixelRatio());

        QPainter painter(&pixmap);
        painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
        painter.drawPixmap(badgePos, badge);

        QPixmapCache::insert(cacheKey, pixmap);
    }

    return pixmap;
}

int DatabaseIcons::count()
{
    return iconList.size();
}

int DatabaseIcons::iconSize(IconSize size)
{
    switch (size) {
    case Medium:
        return m_compactMode ? 26 : 30;
    case Large:
        return m_compactMode ? 30 : 36;
    default:
        return m_compactMode ? 16 : 22;
    }
}