keepassxc

Форк
0
/
Icons.cpp 
314 строк · 9.5 Кб
1
/*
2
 *  Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
3
 *  Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation, either version 2 or (at your option)
8
 *  version 3 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
#include "Icons.h"
20

21
#include <QBuffer>
22
#include <QIconEngine>
23
#include <QImageReader>
24
#include <QPaintDevice>
25
#include <QPainter>
26

27
#include "config-keepassx.h"
28
#include "core/Config.h"
29
#include "core/Database.h"
30
#include "gui/DatabaseIcons.h"
31
#include "gui/MainWindow.h"
32
#include "gui/osutils/OSUtils.h"
33

34
#ifdef WITH_XC_KEESHARE
35
#include "keeshare/KeeShare.h"
36
#endif
37

38
class AdaptiveIconEngine : public QIconEngine
39
{
40
public:
41
    explicit AdaptiveIconEngine(QIcon baseIcon, QColor overrideColor = {});
42
    void paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state) override;
43
    QPixmap pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state) override;
44
    QIconEngine* clone() const override;
45

46
private:
47
    QIcon m_baseIcon;
48
    QColor m_overrideColor;
49
};
50

51
Icons* Icons::m_instance(nullptr);
52

53
Icons::Icons() = default;
54

55
QString Icons::applicationIconName()
56
{
57
#ifdef KEEPASSXC_DIST_FLATPAK
58
    return "org.keepassxc.KeePassXC";
59
#else
60
    return "keepassxc";
61
#endif
62
}
63

64
QIcon Icons::applicationIcon()
65
{
66
    return icon(applicationIconName(), false);
67
}
68

69
QString Icons::trayIconAppearance() const
70
{
71
    auto iconAppearance = config()->get(Config::GUI_TrayIconAppearance).toString();
72
    if (iconAppearance.isNull()) {
73
#ifdef Q_OS_MACOS
74
        iconAppearance = osUtils->isDarkMode() ? "monochrome-light" : "monochrome-dark";
75
#else
76
        iconAppearance = "monochrome-light";
77
#endif
78
    }
79
    return iconAppearance;
80
}
81

82
QIcon Icons::trayIcon(bool unlocked)
83
{
84
    QString suffix;
85
    if (!unlocked) {
86
        suffix = "-locked";
87
    }
88

89
    auto iconApperance = trayIconAppearance();
90
    if (!iconApperance.startsWith("monochrome")) {
91
        return icon(QString("%1%2").arg(applicationIconName(), suffix), false);
92
    }
93

94
    QIcon i;
95
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
96
    if (osUtils->isStatusBarDark()) {
97
        i = icon(QString("keepassxc-monochrome-light%1").arg(suffix), false);
98
    } else {
99
        i = icon(QString("keepassxc-monochrome-dark%1").arg(suffix), false);
100
    }
101
#else
102
    i = icon(QString("%1-%2%3").arg(applicationIconName(), iconApperance, suffix), false);
103
#endif
104
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
105
    // Set as mask to allow the operating system to recolour the tray icon. This may look weird
106
    // if we failed to detect the status bar background colour correctly, but it is certainly
107
    // better than a barely visible icon and even if we did guess correctly, it allows for better
108
    // integration should the system's preferred colours not be 100% black or white.
109
    i.setIsMask(true);
110
#endif
111
    return i;
112
}
113

114
AdaptiveIconEngine::AdaptiveIconEngine(QIcon baseIcon, QColor overrideColor)
115
    : QIconEngine()
116
    , m_baseIcon(std::move(baseIcon))
117
    , m_overrideColor(overrideColor)
118
{
119
}
120

121
void AdaptiveIconEngine::paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state)
122
{
123
    // Temporary image canvas to ensure that the background is transparent and alpha blending works.
124
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
125
    auto scale = painter->device()->devicePixelRatioF();
126
#else
127
    auto scale = painter->device()->devicePixelRatio();
128
#endif
129
    QImage img(rect.size() * scale, QImage::Format_ARGB32_Premultiplied);
130
    img.fill(0);
131
    QPainter p(&img);
132

133
    m_baseIcon.paint(&p, img.rect(), Qt::AlignCenter, mode, state);
134

135
    if (m_overrideColor.isValid()) {
136
        p.setCompositionMode(QPainter::CompositionMode_SourceIn);
137
        p.fillRect(img.rect(), m_overrideColor);
138
    } else if (getMainWindow()) {
139
        QPalette palette = getMainWindow()->palette();
140
        p.setCompositionMode(QPainter::CompositionMode_SourceIn);
141

142
        if (mode == QIcon::Active) {
143
            p.fillRect(img.rect(), palette.color(QPalette::Active, QPalette::ButtonText));
144
        } else if (mode == QIcon::Selected) {
145
            p.fillRect(img.rect(), palette.color(QPalette::Active, QPalette::HighlightedText));
146
        } else if (mode == QIcon::Disabled) {
147
            p.fillRect(img.rect(), palette.color(QPalette::Disabled, QPalette::WindowText));
148
        } else {
149
            p.fillRect(img.rect(), palette.color(QPalette::Normal, QPalette::WindowText));
150
        }
151
    }
152

153
    painter->drawImage(rect, img);
154
}
155

156
QPixmap AdaptiveIconEngine::pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state)
157
{
158
    QImage img(size, QImage::Format_ARGB32_Premultiplied);
159
    img.fill(0);
160
    QPainter painter(&img);
161
    paint(&painter, QRect(0, 0, size.width(), size.height()), mode, state);
162
    return QPixmap::fromImage(img, Qt::ImageConversionFlag::NoFormatConversion);
163
}
164

165
QIconEngine* AdaptiveIconEngine::clone() const
166
{
167
    return new AdaptiveIconEngine(m_baseIcon);
168
}
169

170
QIcon Icons::icon(const QString& name, bool recolor, const QColor& overrideColor)
171
{
172
#ifdef Q_OS_LINUX
173
    // Resetting the application theme name before calling QIcon::fromTheme() is required for hacky
174
    // QPA platform themes such as qt5ct, which randomly mess with the configured icon theme.
175
    // If we do not reset the theme name here, it will become empty at some point, causing
176
    // Qt to look for icons at the user-level and global default locations.
177
    //
178
    // See issue #4963: https://github.com/keepassxreboot/keepassxc/issues/4963
179
    // and qt5ct issue #80: https://sourceforge.net/p/qt5ct/tickets/80/
180
    QIcon::setThemeName("application");
181
#endif
182

183
    QString cacheName =
184
        QString("%1:%2:%3").arg(recolor ? "1" : "0", overrideColor.isValid() ? overrideColor.name() : "#", name);
185
    QIcon icon = m_iconCache.value(cacheName);
186

187
    if (!icon.isNull() && !overrideColor.isValid()) {
188
        return icon;
189
    }
190

191
    icon = QIcon::fromTheme(name);
192
    if (recolor) {
193
        icon = QIcon(new AdaptiveIconEngine(icon, overrideColor));
194
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
195
        icon.setIsMask(true);
196
#endif
197
    }
198

199
    m_iconCache.insert(cacheName, icon);
200
    return icon;
201
}
202

203
QIcon Icons::onOffIcon(const QString& name, bool on, bool recolor)
204
{
205
    return icon(name + (on ? "-on" : "-off"), recolor);
206
}
207

208
Icons* Icons::instance()
209
{
210
    if (!m_instance) {
211
        m_instance = new Icons();
212

213
        Q_INIT_RESOURCE(icons);
214
        QIcon::setThemeSearchPaths(QStringList{":/icons"} << QIcon::themeSearchPaths());
215
        QIcon::setThemeName("application");
216
    }
217

218
    return m_instance;
219
}
220

221
QPixmap Icons::customIconPixmap(const Database* db, const QUuid& uuid, IconSize size)
222
{
223
    if (!db->metadata()->hasCustomIcon(uuid)) {
224
        return {};
225
    }
226
    // Generate QIcon with pre-baked resolutions
227
    auto icon = QImage::fromData(db->metadata()->customIcon(uuid).data);
228
    auto basePixmap = QPixmap::fromImage(icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
229
    return QIcon(basePixmap).pixmap(databaseIcons()->iconSize(size));
230
}
231

232
QHash<QUuid, QPixmap> Icons::customIconsPixmaps(const Database* db, IconSize size)
233
{
234
    QHash<QUuid, QPixmap> result;
235

236
    for (const QUuid& uuid : db->metadata()->customIconsOrder()) {
237
        result.insert(uuid, Icons::customIconPixmap(db, uuid, size));
238
    }
239

240
    return result;
241
}
242

243
QPixmap Icons::entryIconPixmap(const Entry* entry, IconSize size)
244
{
245
    QPixmap icon(size, size);
246
    if (entry->iconUuid().isNull()) {
247
        icon = databaseIcons()->icon(entry->iconNumber(), size);
248
    } else {
249
        if (entry->database()) {
250
            icon = Icons::customIconPixmap(entry->database(), entry->iconUuid(), size);
251
        }
252
    }
253

254
    if (entry->isExpired()) {
255
        icon = databaseIcons()->applyBadge(icon, DatabaseIcons::Badges::Expired);
256
    }
257

258
    return icon;
259
}
260

261
QPixmap Icons::groupIconPixmap(const Group* group, IconSize size)
262
{
263
    QPixmap icon(size, size);
264
    if (group->iconUuid().isNull()) {
265
        icon = databaseIcons()->icon(group->iconNumber(), size);
266
    } else {
267
        if (group->database()) {
268
            icon = Icons::customIconPixmap(group->database(), group->iconUuid(), size);
269
        }
270
    }
271

272
    if (group->isExpired()) {
273
        icon = databaseIcons()->applyBadge(icon, DatabaseIcons::Badges::Expired);
274
    }
275
#ifdef WITH_XC_KEESHARE
276
    else if (KeeShare::isShared(group)) {
277
        icon = KeeShare::indicatorBadge(group, icon);
278
    }
279
#endif
280

281
    return icon;
282
}
283

284
QString Icons::imageFormatsFilter()
285
{
286
    const QList<QByteArray> formats = QImageReader::supportedImageFormats();
287
    QStringList formatsStringList;
288

289
    for (const QByteArray& format : formats) {
290
        bool codePointClean = true;
291
        for (char codePoint : format) {
292
            if (!QChar(codePoint).isLetterOrNumber()) {
293
                codePointClean = false;
294
                break;
295
            }
296
        }
297
        if (codePointClean) {
298
            formatsStringList.append("*." + QString::fromLatin1(format).toLower());
299
        }
300
    }
301

302
    return formatsStringList.join(" ");
303
}
304

305
QByteArray Icons::saveToBytes(const QImage& image)
306
{
307
    QByteArray ba;
308
    QBuffer buffer(&ba);
309
    buffer.open(QIODevice::WriteOnly);
310
    // TODO: check !icon.save()
311
    image.save(&buffer, "PNG");
312
    buffer.close();
313
    return ba;
314
}
315

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

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

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

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