-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathdquickiconimage.cpp
More file actions
391 lines (321 loc) · 9.75 KB
/
Copy pathdquickiconimage.cpp
File metadata and controls
391 lines (321 loc) · 9.75 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "dquickiconimage_p.h"
#include "dquickiconimage_p_p.h"
#include <DGuiApplicationHelper>
#include <DPlatformTheme>
#include <QTimer>
#include <QUrlQuery>
#include <QQmlFile>
DGUI_USE_NAMESPACE
DQUICK_BEGIN_NAMESPACE
bool DQuickIconImagePrivate::updateDevicePixelRatio(qreal targetDevicePixelRatio)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if (!qApp->testAttribute(Qt::AA_UseHighDpiPixmaps)) {
devicePixelRatio = 1.0;
return true;
}
#endif
auto lastRatio = devicePixelRatio;
if (targetDevicePixelRatio > 1.0) {
devicePixelRatio = targetDevicePixelRatio;
} else {
devicePixelRatio = calculateDevicePixelRatio();
}
return lastRatio != devicePixelRatio;
}
void DQuickIconImagePrivate::updateBase64Image()
{
Q_ASSERT(iconType == Base64Data);
D_Q(DQuickIconImage);
QImage image = requestImageFromBase64(name, q->sourceSize(), devicePixelRatio);
setImage(image);
}
QImage DQuickIconImagePrivate::requestImageFromBase64(const QString &name, const QSize &requestedSize, qreal devicePixelRatio)
{
const QString flag("base64,");
const auto index = name.indexOf(flag);
if (index < 0)
return QImage();
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const QString &imgData(name.sliced(index + flag.size()));
#else
const QString &imgData(name.left(index + flag.size()));
#endif
QImage image = QImage::fromData(QByteArray::fromBase64(imgData.toLatin1()));
QSize icon_size = requestedSize;
if (icon_size.isEmpty()) {
icon_size = image.size();
}
image = image.scaled(icon_size * devicePixelRatio, Qt::KeepAspectRatio, Qt::SmoothTransformation);
return image;
}
void DQuickIconImagePrivate::init()
{
D_Q(DQuickIconImage);
if (iconType == ThemeIconName) {
// 强制确保图标的缩放比例正确
updateDevicePixelRatio(1.0);
QObject::connect(DGuiApplicationHelper::instance()->applicationTheme(), SIGNAL(iconThemeNameChanged(QByteArray)),
q, SLOT(maybeUpdateUrl()));
// dtk build-in 类型的图标支持区分窗口主题色, 此处需在主题类型变化时更新图标
QObject::connect(DGuiApplicationHelper::instance(), SIGNAL(themeTypeChanged(ColorType)),
q, SLOT(maybeUpdateUrl()));
} else {
QObject::disconnect(DGuiApplicationHelper::instance()->applicationTheme(), SIGNAL(iconThemeNameChanged(QByteArray)),
q, SLOT(maybeUpdateUrl()));
QObject::disconnect(DGuiApplicationHelper::instance(), SIGNAL(themeTypeChanged(ColorType)),
q, SLOT(maybeUpdateUrl()));
}
// 更新url地址
maybeUpdateUrl();
}
void DQuickIconImagePrivate::maybeUpdateUrl()
{
D_Q(DQuickIconImage);
// 不要为非主题中的图标更新url地址
if (iconType != ThemeIconName) {
if (iconType == Base64Data)
updateBase64Image();
return;
}
// 当图标名为空视为清理图片内容
if (name.isEmpty()) {
if (fallbackSource.isValid())
q->setSource(fallbackSource);
return;
}
QUrl url;
url.setScheme("image");
url.setHost("dtk.icon");
url.setQuery(getUrlQuery());
q->setSource(url);
}
void DQuickIconImagePrivate::play(int mode)
{
Q_UNUSED(mode)
}
QUrlQuery DQuickIconImagePrivate::getUrlQuery()
{
QUrlQuery query;
query.addQueryItem("name", name);
query.addQueryItem("themeName", QIcon::themeName());
query.addQueryItem("themeType", QString::number(int(DGuiApplicationHelper::instance()->themeType())));
query.addQueryItem("state", QString::number(int(state)));
query.addQueryItem("mode", QString::number(int(getIconMode())));
if (color.isValid())
query.addQueryItem("color", color.name(QColor::HexArgb));
query.addQueryItem("devicePixelRatio", QString::number(devicePixelRatio));
return query;
}
DQuickIconImage::Mode DQuickIconImagePrivate::getIconMode() const
{
D_QC(DQuickIconImage);
if (mode != DQuickIconImage::Mode::Invalid)
return mode;
if (!q->isEnabled()) {
return DQuickIconImage::Mode::Disabled;
}
return DQuickIconImage::Mode::Normal;
}
qreal DQuickIconImagePrivate::calculateDevicePixelRatio() const
{
Q_Q(const DQuickIconImage);
return q->window() ? q->window()->effectiveDevicePixelRatio() : qApp->devicePixelRatio();
}
DQuickIconImage::DQuickIconImage(QQuickItem *parent)
: QQuickImage(*(new DQuickIconImagePrivate), parent)
{
setAsynchronous(true); // asynchronous by default
}
DQuickIconImage::~DQuickIconImage()
{
}
void DQuickIconImage::componentComplete()
{
QQuickImage::componentComplete();
D_D(DQuickIconImage);
// 初始化信号链接和url地址
d->init();
connect(this, &QQuickItem::windowChanged, this, [this](){
D_D(DQuickIconImage);
if (d->updateDevicePixelRatio(1.0)) {
d->maybeUpdateUrl();
}
});
}
/**
* @brief DQuickIconImage::name
* @return Returns the name of theme icon used in QML Application.
*/
QString DQuickIconImage::name() const
{
D_DC(DQuickIconImage);
return d->name;
}
/**
* @brief DQuickIconImage::setName
* @param name can set to show the icon used in QML Application.
* Name can be the icon's theme name, icon's base64 data, or icon's local file,
* theme name: it's icon name, and using DIconTheme to find.
* base64: name starts with "data:image/".
* local file: it's a local file, and have either a file: or qrc: scheme.
*/
void DQuickIconImage::setName(const QString &name)
{
D_D(DQuickIconImage);
if (name == d->name) {
return;
}
d->name = name;
Q_EMIT nameChanged();
// 分析icon的类型
d->iconType = DQuickIconImagePrivate::ThemeIconName;
if (name.startsWith("data:image/")) {
d->iconType = DQuickIconImagePrivate::Base64Data;
} else if (QQmlFile::isLocalFile(name)) {
QUrl url(name);
// 如果name指定的是一个url,则直接将其当作url使用
if (url.isValid()) {
d->iconType = DQuickIconImagePrivate::FileUrl;
setSource(url);
}
}
if (isComponentComplete()) {
d->maybeUpdateUrl();
}
}
/**
* @brief DQuickIconImage::state
* @return Returns the state of theme icon used in QML Application.
*/
DQuickIconImage::State DQuickIconImage::state() const
{
D_DC(DQuickIconImage);
return d->state;
}
/**
* @brief DQuickIconImage::setState
* @param state can set to show the icon used in QML Application.
*/
void DQuickIconImage::setState(State state)
{
D_D(DQuickIconImage);
if (d->state == state)
return;
d->state = state;
Q_EMIT stateChanged();
// 尝试重设图标的url地址
d->maybeUpdateUrl();
}
/**
* @brief DQuickIconImage::mode
* @return Returns the mode of theme icon used in QML Application.
*/
DQuickIconImage::Mode DQuickIconImage::mode() const
{
D_DC(DQuickIconImage);
return d->mode;
}
/**
* @brief DQuickIconImage::setMode
* @param mode can set to show the icon used in QML Application.
*/
void DQuickIconImage::setMode(Mode mode)
{
D_D(DQuickIconImage);
if (d->mode == mode)
return;
d->mode = mode;
Q_EMIT modeChanged();
// 尝试重设图标的url地址
d->maybeUpdateUrl();
}
/**
* @brief DQuickIconImage::color
* @return Returns the color of theme icon used in QML Application.
*/
QColor DQuickIconImage::color() const
{
D_DC(DQuickIconImage);
return d->color;
}
/**
* @brief DQuickIconImage::setColor
* @param color can set to show the icon used in QML Application.
*/
void DQuickIconImage::setColor(const QColor &color)
{
D_D(DQuickIconImage);
if (d->color == color)
return;
d->color = color;
Q_EMIT colorChanged();
// 尝试重设图标的url地址
d->maybeUpdateUrl();
}
const QUrl &DQuickIconImage::fallbackSource() const
{
D_DC(DQuickIconImage);
return d->fallbackSource;
}
void DQuickIconImage::setFallbackSource(const QUrl &newSource)
{
D_D(DQuickIconImage);
if (d->fallbackSource == newSource)
return;
d->fallbackSource = newSource;
Q_EMIT fallbackSourceChanged();
// 尝试重设图标的url地址
d->maybeUpdateUrl();
}
void DQuickIconImage::setImage(const QImage &img)
{
D_D(DQuickIconImage);
d->setImage(img);
}
DQuickIconImage::DQuickIconImage(DQuickIconImagePrivate &dd, QQuickItem *parent)
: QQuickImage(dd, parent)
{
setAsynchronous(true);
}
void DQuickIconImage::itemChange(ItemChange change, const ItemChangeData &value)
{
Q_D(DQuickIconImage);
switch (change) {
case ItemDevicePixelRatioHasChanged: Q_FALLTHROUGH();
case ItemEnabledHasChanged: {
// ###!(Chen Bin): When the program exits, it will be called
// again, but the engine has been freed, causing subsequent
// functions to crash when loading the image provider registered
// in engine.
if (!qmlEngine(this))
break;
d->maybeUpdateUrl();
}
break;
default:
break;
}
QQuickImage::itemChange(change, value);
}
void DQuickIconImage::pixmapChange()
{
// QQuickImage中只会在设置了souceSize的前提下才会计算图片自身的缩放比例
// 此处强制确保图标的缩放比例正确
D_D(DQuickIconImage);
if (d->iconType == DQuickIconImagePrivate::ThemeIconName) {
if (d->updateDevicePixelRatio(1.0)) {
d->maybeUpdateUrl();
}
}
QQuickImage::pixmapChange();
if (status() == Error && d->fallbackSource.isValid()) {
// fallback to source property
setSource(d->fallbackSource);
}
}
DQUICK_END_NAMESPACE
#include "moc_dquickiconimage_p.cpp"