This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
palette_icon_engine.cpp
157 lines (130 loc) · 4.56 KB
/
palette_icon_engine.cpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
custom icon engine Qt plugin
Copyright (C) 2017-2023 Nick Korotysh <[email protected]>
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 3 of the License, or
(at your option) any later version.
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 "palette_icon_engine.hpp"
#include <QFile>
#include <QPainter>
#include <QPalette>
#include <QPixmapCache>
#include <QSvgRenderer>
static QString actualFilename(const QString& filename)
{
if (QFile::exists(filename))
return filename;
QString fn = filename.mid(0, filename.lastIndexOf('.'));
if (QFile::exists(fn))
return fn;
return fn + ".svg";
}
static QColor getIconColor(QIcon::Mode mode, QIcon::State state)
{
Q_UNUSED(state);
QPalette::ColorGroup color_group = QPalette::Active;
if (mode == QIcon::Disabled) color_group = QPalette::Disabled;
return QPalette().color(color_group, QPalette::WindowText);
}
static QPixmap renderIcon(QSvgRenderer* renderer, const QSize& size, const QBrush& brush)
{
QPixmap output(size);
output.fill(Qt::transparent);
QPainter p(&output);
renderer->render(&p);
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
p.setPen(Qt::NoPen);
p.setBrush(brush);
p.drawRect(output.rect());
return output;
}
PaletteIconEngine::PaletteIconEngine()
{
renderer_.reset(new QSvgRenderer());
}
PaletteIconEngine::PaletteIconEngine(const PaletteIconEngine& other) : QIconEngine(other)
{
src_file_ = other.src_file_;
renderer_.reset(new QSvgRenderer());
if (other.renderer_->isValid())
renderer_->load(other.src_file_);
}
PaletteIconEngine::~PaletteIconEngine() = default;
void PaletteIconEngine::paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state)
{
// "direct rendereng" using given painter is not possible
// because colorization logic modifies already painted area
// such behavior is not acceptable, so render icon to pixmap first
QSize size = rect.size() * painter->device()->devicePixelRatioF();
QPixmap pxm = pixmap(size, mode, state);
// set device pixel ratio exactly before painting,
// this will allow to reuse the same cached pixmap
pxm.setDevicePixelRatio(painter->device()->devicePixelRatioF());
painter->drawPixmap(rect, pxm);
}
QPixmap PaletteIconEngine::pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state)
{
// unfortunately, default implementation (call paint() on newly created QPixmap)
// doesn't initialize (fill) QPixmap with transparent color, so artifacts may happen
// so, it is better to implement pixmap() function and use it in paint() implementation
QColor color = getIconColor(mode, state);
QString pmckey = QString("pie_%1:%2x%3:%4")
.arg(src_file_)
.arg(size.width()).arg(size.height())
.arg(color.name(QColor::HexArgb));
QPixmap pxm;
if (!QPixmapCache::find(pmckey, &pxm)) {
pxm = renderIcon(renderer_.data(), size, color);
QPixmapCache::insert(pmckey, pxm);
}
return pxm;
}
void PaletteIconEngine::addFile(const QString& fileName, const QSize& size, QIcon::Mode mode, QIcon::State state)
{
Q_UNUSED(size);
Q_UNUSED(mode);
Q_UNUSED(state);
QString filename = actualFilename(fileName);
if (filename == src_file_)
return;
if (renderer_->load(filename))
src_file_ = filename;
}
QString PaletteIconEngine::key() const
{
return QLatin1String("palette");
}
QIconEngine* PaletteIconEngine::clone() const
{
return new PaletteIconEngine(*this);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QList<QSize> PaletteIconEngine::availableSizes(QIcon::Mode mode, QIcon::State state) const
#else
QList<QSize> PaletteIconEngine::availableSizes(QIcon::Mode mode, QIcon::State state)
#endif
{
Q_UNUSED(mode);
Q_UNUSED(state);
QList<QSize> sizes;
sizes << QSize(512, 512); // just workaround to make tray icon visible on KDE
return sizes;
}
void PaletteIconEngine::virtual_hook(int id, void* data)
{
switch (id) {
case QIconEngine::IsNullHook:
*reinterpret_cast<bool*>(data) = (!renderer_ || !renderer_->isValid());
break;
default:
QIconEngine::virtual_hook(id, data);
}
}