forked from linuxdeepin/dtkdeclarative
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdquickdciiconimage.cpp
More file actions
508 lines (418 loc) · 13 KB
/
dquickdciiconimage.cpp
File metadata and controls
508 lines (418 loc) · 13 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// SPDX-FileCopyrightText: 2021 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "dquickdciiconimage_p_p.h"
#include <DIconTheme>
#include <DGuiApplicationHelper>
#include <DPlatformTheme>
#include <QUrlQuery>
DQUICK_BEGIN_NAMESPACE
DGUI_USE_NAMESPACE
static QString appIconThemeName()
{
return DGuiApplicationHelper::instance()->applicationTheme()->iconThemeName();
}
static QString findDciIconPath(const QString &iconName, const QString &themeName)
{
QString iconPath;
auto cached = DIconTheme::cached();
if (cached) {
iconPath = cached->findDciIconFile(iconName, themeName);
} else {
iconPath = DIconTheme::findDciIconFile(iconName, themeName);
}
return iconPath;
}
static DDciIcon::Mode controlState2DciMode(int state)
{
DDciIcon::Mode dcimode = DDciIcon::Normal;
switch (state) {
case DQMLGlobalObject::NormalState:
dcimode = DDciIcon::Normal;
break;
case DQMLGlobalObject::DisabledState:
dcimode = DDciIcon::Disabled;
break;
case DQMLGlobalObject::HoveredState:
dcimode = DDciIcon::Hover;
break;
case DQMLGlobalObject::PressedState:
dcimode = DDciIcon::Pressed;
break;
default:
break;
}
return dcimode;
}
static inline DDciIcon::Theme dciTheme(DGuiApplicationHelper::ColorType type)
{
return type == DGuiApplicationHelper::DarkType ? DDciIcon::Dark : DDciIcon::Light;
}
DQuickDciIconImageItemPrivate::DQuickDciIconImageItemPrivate(DQuickDciIconImagePrivate *pqq)
: parentPriv(pqq)
{
}
void DQuickDciIconImageItemPrivate::maybeUpdateUrl()
{
Q_Q(DQuickIconImage);
if (parentPriv->imageItem->name().isEmpty() || iconType != ThemeIconName) {
return DQuickIconImagePrivate::maybeUpdateUrl();
}
if (!DQMLGlobalObject::hasAnimation()) {
QUrl url;
url.setScheme(QLatin1String("image"));
url.setHost(QLatin1String("dtk.dci.icon"));
url.setQuery(getUrlQuery());
q->setSource(url);
return;
}
QString iconPath = findDciIconPath(parentPriv->imageItem->name(), appIconThemeName());
if (iconPath.isEmpty())
return DQuickIconImagePrivate::maybeUpdateUrl();
updatePlayer();
if (player)
player->setMode(controlState2DciMode(parentPriv->mode));
}
void DQuickDciIconImageItemPrivate::play(int mode)
{
Q_Q(DQuickIconImage);
if (parentPriv->imageItem->name().isEmpty() || iconType != ThemeIconName) {
return;
}
updatePlayer();
if (player)
player->play(controlState2DciMode(mode));
}
QUrlQuery DQuickDciIconImageItemPrivate::getUrlQuery()
{
QUrlQuery query;
query.addQueryItem(QLatin1String("name"), parentPriv->imageItem->name());
query.addQueryItem(QLatin1String("mode"), QString::number(parentPriv->mode));
query.addQueryItem(QLatin1String("theme"), QString::number(parentPriv->theme));
query.addQueryItem(QLatin1String("themeName"), QIcon::themeName());
DDciIconPalette pal = parentPriv->palette;
if (!parentPriv->palette.foreground().isValid() && q_func()->color().isValid()) {
pal.setForeground(q_func()->color());
}
query.addQueryItem(QLatin1String("palette"), DDciIconPalette::convertToString(pal));
query.addQueryItem(QLatin1String("devicePixelRatio"), QString::number(devicePixelRatio));
query.addQueryItem(QLatin1String("fallbackToQIcon"), QString::number(parentPriv->fallbackToQIcon));
return query;
}
void DQuickDciIconImageItemPrivate::updatePlayerIconSize()
{
if (!player)
return;
int boundingSize = qMax(q_func()->sourceSize().width(), q_func()->sourceSize().height());
player->setIconSize(boundingSize);
}
void DQuickDciIconImageItemPrivate::updatePlayer()
{
if (!player) {
Q_Q(DQuickIconImage);
player = new DDciIconPlayer(parentPriv->imageItem);
QObject::connect(player, &DDciIconPlayer::updated, parentPriv->imageItem, [this](){
parentPriv->imageItem->setImage(player->currentImage());
});
QObject::connect(parentPriv->imageItem, &DQuickIconImage::sourceSizeChanged, player, [this](){
updatePlayerIconSize();
});
// 只在初始化和 sourceSizeChanged 时更新图标大小
// 防止出现 dpr > 1.0 时未设置 sourceSize 的 item 在 updatePlayer 时图标一直放大
updatePlayerIconSize();
}
QString iconPath = findDciIconPath(parentPriv->imageItem->name(), appIconThemeName());
// 防止频繁构造 dciicon
if (iconPathCache != iconPath) {
DDciIcon dciIcon(iconPath);
if (!dciIcon.isNull()) {
player->setIcon(dciIcon);
iconPathCache = iconPath;
}
}
player->setTheme(dciTheme(parentPriv->theme));
DDciIconPalette palette = parentPriv->palette;
if (!parentPriv->palette.foreground().isValid() && q_func()->color().isValid())
palette.setForeground(q_func()->color());
player->setPalette(palette);
// treeland下拿qApp的缩放不准确,初始化的时候尝试去更新一次缩放
updateDevicePixelRatio(1.0);
player->setDevicePixelRatio(devicePixelRatio);
}
DQuickDciIconImagePrivate::DQuickDciIconImagePrivate(DQuickDciIconImage *qq)
: DObjectPrivate(qq)
, imageItem(new DQuickIconImage(*new DQuickDciIconImageItemPrivate(this), qq))
{
QObject::connect(imageItem, &DQuickIconImage::nameChanged, qq, &DQuickDciIconImage::nameChanged);
QObject::connect(imageItem, &DQuickIconImage::asynchronousChanged, qq, &DQuickDciIconImage::asynchronousChanged);
QObject::connect(imageItem, &DQuickIconImage::cacheChanged, qq, &DQuickDciIconImage::cacheChanged);
QObject::connect(imageItem, &DQuickIconImage::fillModeChanged, qq, &DQuickDciIconImage::fillModeChanged);
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
QObject::connect(imageItem, &DQuickIconImage::retainWhileLoadingChanged, qq, &DQuickDciIconImage::retainWhileLoadingChanged);
#endif
}
void DQuickDciIconImagePrivate::layout()
{
auto dd = QQuickItemPrivate::get(imageItem);
dd->anchors()->setCenterIn(imageItem->parentItem());
}
void DQuickDciIconImagePrivate::updateImageSourceUrl()
{
imageItem->d_func()->maybeUpdateUrl();
}
void DQuickDciIconImagePrivate::play(DQMLGlobalObject::ControlState mode)
{
imageItem->d_func()->play(mode);
}
DQuickDciIconImage::DQuickDciIconImage(QQuickItem *parent)
: QQuickItem(parent)
, DObject(*new DQuickDciIconImagePrivate(this))
{
D_D(DQuickDciIconImage);
setSmooth(false);
connect(d->imageItem, &QQuickImage::implicitWidthChanged, this, [this, d]() { setImplicitWidth(d->imageItem->implicitWidth()); });
connect(d->imageItem, &QQuickImage::implicitHeightChanged, this, [this, d]() { setImplicitHeight(d->imageItem->implicitHeight()); });
connect(this, &DQuickDciIconImage::smoothChanged, d->imageItem, &QQuickImage::setSmooth);
}
DQuickDciIconImage::~DQuickDciIconImage()
{
}
QString DQuickDciIconImage::name() const
{
D_DC(DQuickDciIconImage);
return d->imageItem->name();
}
void DQuickDciIconImage::setName(const QString &name)
{
D_D(DQuickDciIconImage);
d->imageItem->setName(name);
}
DQMLGlobalObject::ControlState DQuickDciIconImage::mode() const
{
D_DC(DQuickDciIconImage);
return d->mode;
}
void DQuickDciIconImage::setMode(DQMLGlobalObject::ControlState mode)
{
D_D(DQuickDciIconImage);
if (d->mode == mode)
return;
d->mode = mode;
d->updateImageSourceUrl();
Q_EMIT modeChanged();
}
void DQuickDciIconImage::play(DQMLGlobalObject::ControlState mode)
{
D_D(DQuickDciIconImage);
if (d->imageItem)
d->play(mode);
}
DGuiApplicationHelper::ColorType DQuickDciIconImage::theme() const
{
D_DC(DQuickDciIconImage);
return d->theme;
}
void DQuickDciIconImage::setTheme(DGuiApplicationHelper::ColorType theme)
{
D_D(DQuickDciIconImage);
if (d->theme == theme)
return;
d->theme = theme;
d->updateImageSourceUrl();
Q_EMIT themeChanged();
}
DDciIconPalette DQuickDciIconImage::palette() const
{
D_DC(DQuickDciIconImage);
return d->palette;
}
void DQuickDciIconImage::setPalette(const DDciIconPalette &palette)
{
D_D(DQuickDciIconImage);
if (d->palette == palette)
return;
d->palette = palette;
d->updateImageSourceUrl();
Q_EMIT paletteChanged();
}
QSize DQuickDciIconImage::sourceSize() const
{
D_DC(DQuickDciIconImage);
return d->imageItem->sourceSize();
}
void DQuickDciIconImage::setSourceSize(const QSize &size)
{
D_D(DQuickDciIconImage);
d->imageItem->setSourceSize(size);
d->updateImageSourceUrl();
Q_EMIT sourceSizeChanged();
}
void DQuickDciIconImage::setMirror(bool mirror)
{
D_D(DQuickDciIconImage);
d->imageItem->setMirror(mirror);
}
bool DQuickDciIconImage::mirror() const
{
D_DC(DQuickDciIconImage);
return d->imageItem->mirror();
}
bool DQuickDciIconImage::fallbackToQIcon() const
{
D_DC(DQuickDciIconImage);
return d->fallbackToQIcon;
}
void DQuickDciIconImage::setFallbackToQIcon(bool newFallbackToQIcon)
{
D_D(DQuickDciIconImage);
if (d->fallbackToQIcon == newFallbackToQIcon)
return;
d->fallbackToQIcon = newFallbackToQIcon;
Q_EMIT fallbackToQIconChanged();
d->updateImageSourceUrl();
}
bool DQuickDciIconImage::asynchronous() const
{
D_DC(DQuickDciIconImage);
return d->imageItem->asynchronous();
}
void DQuickDciIconImage::setAsynchronous(bool async)
{
D_D(DQuickDciIconImage);
d->imageItem->setAsynchronous(async);
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
bool DQuickDciIconImage::retainWhileLoading() const
{
D_DC(DQuickDciIconImage);
return d->imageItem->retainWhileLoading();
}
void DQuickDciIconImage::setRetainWhileLoading(bool retain)
{
D_D(DQuickDciIconImage);
d->imageItem->setRetainWhileLoading(retain);
}
#endif
void DQuickDciIconImage::setFillMode(QQuickImage::FillMode mode)
{
D_D(DQuickDciIconImage);
d->imageItem->setFillMode(mode);
}
QQuickImage::FillMode DQuickDciIconImage::fillMode() const
{
D_DC(DQuickDciIconImage);
return d->imageItem->fillMode();
}
bool DQuickDciIconImage::cache() const
{
D_DC(DQuickDciIconImage);
return d->imageItem->cache();
}
void DQuickDciIconImage::setCache(bool cache)
{
D_D(DQuickDciIconImage);
d->imageItem->setCache(cache);
}
Dtk::Quick::DQuickIconImage *DQuickDciIconImage::imageItem() const
{
D_DC(DQuickDciIconImage);
return d->imageItem;
}
bool DQuickDciIconImage::isNull(const QString &iconName)
{
return findDciIconPath(iconName, appIconThemeName()).isEmpty();
}
DQuickIconAttached *DQuickDciIconImage::qmlAttachedProperties(QObject *object)
{
auto item = qobject_cast<QQuickItem *>(object);
if (!item)
return nullptr;
return new DQuickIconAttached(item);
}
void DQuickDciIconImage::classBegin()
{
D_D(DQuickDciIconImage);
QQmlEngine::setContextForObject(d->imageItem, QQmlEngine::contextForObject(this));
QQuickItem::classBegin();
d->imageItem->setSmooth(smooth());
}
void DQuickDciIconImage::componentComplete()
{
D_D(DQuickDciIconImage);
d->imageItem->componentComplete();
QQuickItem::componentComplete();
d->layout();
}
class DQuickIconAttachedPrivate : public DCORE_NAMESPACE::DObjectPrivate
{
D_DECLARE_PUBLIC(DQuickIconAttached)
public:
DQuickIconAttachedPrivate(DCORE_NAMESPACE::DObject *qq)
: DObjectPrivate(qq)
{}
DQMLGlobalObject::ControlState mode = DQMLGlobalObject::NormalState;
DGuiApplicationHelper::ColorType theme = DGuiApplicationHelper::ColorType::LightType;
DDciIconPalette palette;
bool fallbackToQIcon = true;
};
DQuickIconAttached::DQuickIconAttached(QQuickItem *parent)
: QObject(parent)
, DObject(*new DQuickIconAttachedPrivate(this))
{
}
DQuickIconAttached::~DQuickIconAttached()
{
}
DQMLGlobalObject::ControlState DQuickIconAttached::mode() const
{
D_DC(DQuickIconAttached);
return d->mode;
}
void DQuickIconAttached::setMode(DQMLGlobalObject::ControlState mode)
{
D_D(DQuickIconAttached);
if (d->mode == mode)
return;
d->mode = mode;
Q_EMIT modeChanged();
}
DGuiApplicationHelper::ColorType DQuickIconAttached::theme() const
{
D_DC(DQuickIconAttached);
return d->theme;
}
void DQuickIconAttached::setTheme(DGuiApplicationHelper::ColorType theme)
{
D_D(DQuickIconAttached);
if (d->theme == theme)
return;
d->theme = theme;
Q_EMIT themeChanged();
}
DDciIconPalette DQuickIconAttached::palette() const
{
D_DC(DQuickIconAttached);
return d->palette;
}
void DQuickIconAttached::setPalette(const DDciIconPalette &palette)
{
D_D(DQuickIconAttached);
if (d->palette == palette)
return;
d->palette = palette;
Q_EMIT paletteChanged();
}
bool DQuickIconAttached::fallbackToQIcon() const
{
D_DC(DQuickIconAttached);
return d->fallbackToQIcon;
}
void DQuickIconAttached::setFallbackToQIcon(bool newFallbackToQIcon)
{
D_D(DQuickIconAttached);
if (d->fallbackToQIcon == newFallbackToQIcon)
return;
d->fallbackToQIcon = newFallbackToQIcon;
Q_EMIT fallbackToQIconChanged();
}
DQUICK_END_NAMESPACE