Debian Codegraph @

src/util/private/dciiconengine.cpp

raw
1 // SPDX-FileCopyrightText: 2022 - 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dciiconengine_p.h"
6 #include "dguiapplicationhelper.h"
7 #include "dplatformtheme.h"
8
9 #include <QPainter>
10 #include <QPixmap>
11
12 #include <private/qhexstring_p.h>
13 #include <private/qiconloader_p.h>
14
15 DGUI_BEGIN_NAMESPACE
16
17 static inline DDciIcon::Theme dciTheme()
18 {
19 auto theme = DGuiApplicationHelper::instance()->themeType();
20 return theme == DGuiApplicationHelper::DarkType ? DDciIcon::Dark : DDciIcon::Light;
21 }
22
23 static inline DDciIcon::Mode dciMode(QIcon::Mode mode)
24 {
25 // QIcon only support DDciIcon::Disabled and DDciIcon::Normal
26 return mode == QIcon::Disabled ? DDciIcon::Disabled : DDciIcon::Normal;
27 }
28
29 static inline DDciIconPalette dciPalettle(QPaintDevice *paintDevice = nullptr)
30 {
31 QPalette pa;
32 if (!paintDevice || paintDevice->devType() != QInternal::Widget) {
33 pa = qApp->palette();
34 } else if (QObject *obj = dynamic_cast<QObject *>(paintDevice)) {
35 pa = qvariant_cast<QPalette>(obj->property("palette"));
36 } else {
37 pa = qApp->palette();
38 }
39
40 return DDciIconPalette(pa.windowText().color(), pa.window().color(),
41 pa.highlight().color(), pa.highlightedText().color());
42 }
43
44 static inline qreal deviceRadio(QPaintDevice *paintDevice = nullptr)
45 {
46 qreal scale = 1.0;
47 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
48 if (qApp->testAttribute(Qt::AA_UseHighDpiPixmaps))
49 scale = paintDevice ? paintDevice->devicePixelRatioF() : qApp->devicePixelRatio();
50 #else
51 scale = paintDevice ? paintDevice->devicePixelRatioF() : qApp->devicePixelRatio();
52 #endif
53
54 return scale;
55 }
56
57 DDciIconEngine::DDciIconEngine(const QString &iconName)
58 : m_iconName(iconName)
59 , m_iconThemeName(DGuiApplicationHelper::instance()->applicationTheme()->iconThemeName())
60 , m_dciIcon(DDciIcon::fromTheme(iconName))
61 {
62
63 }
64
65 DDciIconEngine::DDciIconEngine(const DDciIconEngine &other)
66 : QIconEngine(other)
67 , m_iconName(other.m_iconName)
68 , m_iconThemeName(other.m_iconThemeName)
69 , m_dciIcon(other.m_dciIcon)
70 {
71
72 }
73
74 DDciIconEngine::~DDciIconEngine()
75 {
76
77 }
78
79 QSize DDciIconEngine::actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
80 {
81 Q_UNUSED(state);
82 ensureIconTheme();
83 int s = m_dciIcon.actualSize(qMin(size.width(), size.height()), dciTheme(), dciMode(mode));
84 return QSize(s, s).boundedTo(size);
85 }
86
87 QPixmap DDciIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
88 {
89 // QIcon::pixmap has already been multiplied by the screen scaling factor
90 // so there is no need to do it again here. set radio 1.0 .
91 return pixmap(size, mode, state, 1.0);
92 }
93
94 QPixmap DDciIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal radio)
95 {
96 Q_UNUSED(state);
97
98 const int s = qMin(size.width(), size.height());
99 const DDciIcon::Theme theme = dciTheme();
100 const DDciIconPalette pa = dciPalettle();
101
102 QString key = QLatin1String("dci_") + m_iconName + m_iconThemeName +
103 DDciIconPalette::convertToString(pa)
104 % HexString<uint>(mode)
105 % HexString<int>(theme)
106 % HexString<int>(s)
107 % HexString<uint>(uint(radio * 100));
108
109 QPixmap pix;
110 if (QPixmapCache::find(key, &pix))
111 return pix;
112
113 ensureIconTheme();
114 pix = m_dciIcon.pixmap(radio, s, theme, dciMode(mode), pa);
115 if (!pix.isNull())
116 QPixmapCache::insert(key, pix);
117
118 return pix;
119 }
120
121 void DDciIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
122 {
123 Q_UNUSED(state);
124 ensureIconTheme();
125 m_dciIcon.paint(painter, rect, deviceRadio(painter->device()), dciTheme(),
126 dciMode(mode), Qt::AlignCenter, dciPalettle(painter->device()));
127 }
128
129 QString DDciIconEngine::key() const
130 {
131 return QLatin1String("DDciIconEngine");
132 }
133
134 QIconEngine *DDciIconEngine::clone() const
135 {
136 return new DDciIconEngine(*this);
137 }
138
139 bool DDciIconEngine::read(QDataStream &in)
140 {
141 ensureIconTheme();
142 in >> m_iconThemeName >> m_iconName >> m_dciIcon;
143 return true;
144 }
145
146 bool DDciIconEngine::write(QDataStream &out) const
147 {
148 const_cast<DDciIconEngine *>(this)->ensureIconTheme();
149 out << m_iconThemeName << m_iconName << m_dciIcon;
150 return true;
151 }
152
153 QString DDciIconEngine::iconName()
154 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
155 const
156 #endif
157 {
158 return m_iconName;
159 }
160
161 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
162 QList<QSize> DDciIconEngine::availableSizes(QIcon::Mode mode, QIcon::State state)
163 {
164 ensureIconTheme();
165
166 const auto availableSizes = m_dciIcon.availableSizes(dciTheme(), DDciIcon::Normal);
167 QList<QSize> sizes;
168 sizes.reserve(availableSizes.size());
169
170 for (int size : availableSizes)
171 sizes.append(QSize(size, size));
172
173 return sizes;
174 }
175 #endif
176
177 void DDciIconEngine::virtual_hook(int id, void *data)
178 {
179 ensureIconTheme();
180 switch (id) {
181 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
182 case QIconEngine::AvailableSizesHook:
183 {
184 auto &arg = *reinterpret_cast<QIconEngine::AvailableSizesArgument*>(data);
185 auto availableSizes = m_dciIcon.availableSizes(dciTheme(), DDciIcon::Normal);
186 QList<QSize> sizes;
187 sizes.reserve(availableSizes.size());
188
189 for (int size : availableSizes)
190 sizes.append(QSize(size, size));
191
192 arg.sizes.swap(sizes); // commit
193 }
194 break;
195 case QIconEngine::IconNameHook:
196 {
197 QString &name = *reinterpret_cast<QString*>(data);
198 name = iconName();
199 }
200 break;
201 #endif
202 case QIconEngine::IsNullHook:
203 {
204 *reinterpret_cast<bool*>(data) = m_dciIcon.isNull();
205 }
206 break;
207 case QIconEngine::ScaledPixmapHook:
208 {
209 QIconEngine::ScaledPixmapArgument &arg = *reinterpret_cast<QIconEngine::ScaledPixmapArgument*>(data);
210 arg.pixmap = pixmap(arg.size, arg.mode, arg.state);
211 }
212 break;
213 default:
214 QIconEngine::virtual_hook(id, data);
215 }
216 }
217
218 void DDciIconEngine::ensureIconTheme()
219 {
220 QString iconThemeName = DGuiApplicationHelper::instance()->applicationTheme()->iconThemeName();
221 if (m_iconThemeName != iconThemeName) {
222 m_iconThemeName = iconThemeName;
223 // update dci icon when icon theme name changed.
224 m_dciIcon = DDciIcon::fromTheme(m_iconName);
225 }
226 }
227
228 DGUI_END_NAMESPACE