Debian Codegraph @

src/util/ddciiconplayer.cpp

raw
1 // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "ddciiconplayer.h"
6 #include "ddciicon.h"
7
8 #include <DObjectPrivate>
9 #include <QTimerEvent>
10 #include <QCoreApplication>
11 #include <QLoggingCategory>
12 #include <QDebug>
13
14 DCORE_USE_NAMESPACE
15 DGUI_BEGIN_NAMESPACE
16
17 #ifdef QT_DEBUG
18 Q_LOGGING_CATEGORY(diPlayer, "dtk.dciicon.player")
19 #else
20 Q_LOGGING_CATEGORY(diPlayer, "dtk.dciicon.player", QtInfoMsg)
21 #endif
22
23 class DDciIconImagePlayerPrivate : DObjectPrivate
24 {
25 public:
26 DDciIconImagePlayerPrivate(DDciIconImagePlayer *qq)
27 : DObjectPrivate(qq) {}
28
29 bool initCurrent();
30 bool ensureCurrent();
31 void clearCache();
32 void setState(DDciIconImagePlayer::State newState);
33
34 inline bool reversed() const {
35 return flags.testFlag(DDciIconImagePlayer::InvertedOrder);
36 }
37 inline bool hasCache(int imageIndex, int frameNumber) const {
38 if (imageIndex < 0 || frameNumber < 0)
39 return false;
40 if (cachedFrames.size() <= imageIndex)
41 return false;
42 const auto &i = cachedFrames.at(imageIndex);
43 if (i.size() <= frameNumber)
44 return false;
45 return true;
46 }
47 inline bool currentHasCache() const {
48 return current >= 0 && currentFrameNumber >= 0 && hasCache(current, currentFrameNumber);
49 }
50
51 D_DECLARE_PUBLIC(DDciIconImagePlayer)
52 QVector<DDciIconImage> images;
53 DDciIconPalette palette;
54 inline DDciIconImage &currentImage() {
55 return images[current];
56 }
57
58 DDciIconImagePlayer::State state = DDciIconImagePlayer::NotRunning;
59 DDciIconImagePlayer::Flags flags;
60 float speed = 1.0;
61 // loop count for all images sequential animation
62 int userLoopCount = 1;
63
64 struct Frame {
65 QImage image;
66 int duration;
67 };
68 QVector<QVector<Frame>> cachedFrames;
69 inline QVector<Frame> &currentCache() {
70 return cachedFrames[current];
71 }
72
73 int timerId = 0;
74 // loop count for all images sequential animation, init from "userLoopCount" when start
75 int loopCount = 1;
76 int current = 0;
77 int currentLoopCount = 0;
78 int currentFrameNumber = 0;
79 };
80
81 inline static bool jumpImageTo(DDciIconImage &image, int number)
82 {
83 if (number < 0)
84 return false;
85
86 if (image.currentImageNumber() > number)
87 image.reset();
88 for (int i = image.currentImageNumber(); i < number; ++i)
89 if (!image.jumpToNextImage())
90 return false;;
91 return true;
92 }
93
94 bool DDciIconImagePlayerPrivate::initCurrent()
95 {
96 if (!currentImage().supportsAnimation())
97 return false;
98
99 if (reversed()) {
100 if (currentFrameNumber < 0)
101 currentFrameNumber = currentCache().size() - 1;
102 } else {
103 if (currentFrameNumber < 0)
104 currentFrameNumber = 0;
105 }
106
107 if (!currentHasCache()) {
108 if (!jumpImageTo(currentImage(), currentFrameNumber))
109 return false;
110 }
111
112 if ((flags & DDciIconImagePlayer::CacheAll) && cachedFrames.size() <= current) {
113 cachedFrames.append(QVector<DDciIconImagePlayerPrivate::Frame>());
114 }
115
116 if (current == images.size() - 1) {
117 if (flags.testFlag(DDciIconImagePlayer::IgnoreLastImageLoop)) {
118 currentLoopCount = 1;
119 } else {
120 currentLoopCount = currentImage().loopCount();
121 }
122 } else if (flags.testFlag(DDciIconImagePlayer::AllowNonLastImageLoop)) {
123 currentLoopCount = currentImage().loopCount();
124 } else {
125 currentLoopCount = 1;
126 }
127
128 if (currentLoopCount == 0)
129 currentLoopCount = 1;
130
131 return true;
132 }
133
134 bool DDciIconImagePlayerPrivate::ensureCurrent()
135 {
136 while (current >= 0 && current < images.size()) {
137 if (initCurrent())
138 return true;
139 current += reversed() ? -1 : 1;
140 currentFrameNumber = -1;
141 }
142
143 return false;
144 }
145
146 void DDciIconImagePlayerPrivate::clearCache()
147 {
148 cachedFrames.clear();
149 }
150
151 void DDciIconImagePlayerPrivate::setState(DDciIconImagePlayer::State newState)
152 {
153 if (state == newState)
154 return;
155 state = newState;
156 Q_EMIT q_func()->stateChanged();
157 }
158
159 DDciIconImagePlayer::DDciIconImagePlayer(QObject *parent)
160 : QObject(parent)
161 , DObject(*new DDciIconImagePlayerPrivate(this))
162 {
163
164 }
165
166 void DDciIconImagePlayer::setImages(const QVector<DDciIconImage> &images)
167 {
168 D_D(DDciIconImagePlayer);
169 if (d->images == images)
170 return;
171
172 if (d->state != NotRunning)
173 stop();
174 Q_ASSERT(d->state == NotRunning);
175 d->images = images;
176 d->current = -1;
177 d->currentFrameNumber = -1;
178 d->clearCache();
179 }
180
181 QVector<DDciIconImage> DDciIconImagePlayer::images() const
182 {
183 D_DC(DDciIconImagePlayer);
184 return d->images;
185 }
186
187 const DDciIconImage &DDciIconImagePlayer::currentImage() const
188 {
189 D_DC(DDciIconImagePlayer);
190 static DDciIconImage nullImage;
191 if (d->state == NotRunning)
192 return nullImage;
193 return d->images.at(d->current);
194 }
195
196 bool DDciIconImagePlayer::currentLoopForever() const
197 {
198 D_DC(DDciIconImagePlayer);
199 return d->state != NotRunning && d->currentLoopCount < 0;
200 }
201
202 bool DDciIconImagePlayer::setPalette(const DDciIconPalette &palette)
203 {
204 D_D(DDciIconImagePlayer);
205 if (d->palette == palette)
206 return false;
207 d->palette = palette;
208
209 bool hasPalette = false;
210 for (const auto &i : qAsConst(d->images))
211 if (i.hasPalette())
212 hasPalette = true;
213
214 if (!hasPalette)
215 return true;
216
217 if (d->state == NotRunning) {
218 d->clearCache();
219 } else {
220 d->flags |= ClearCacheOnStop;
221 }
222
223 return true;
224 }
225
226 DDciIconPalette DDciIconImagePlayer::palette() const
227 {
228 D_DC(DDciIconImagePlayer);
229 return d->palette;
230 }
231
232 void DDciIconImagePlayer::setLoopCount(int count)
233 {
234 D_D(DDciIconImagePlayer);
235 if (count <= 0)
236 return;
237 if (d->state != NotRunning) {
238 d->loopCount += (count - d->userLoopCount);
239 }
240 d->userLoopCount = count;
241 }
242
243 int DDciIconImagePlayer::loopCount() const
244 {
245 D_DC(DDciIconImagePlayer);
246 return d->userLoopCount;
247 }
248
249 void DDciIconImagePlayer::abortLoop()
250 {
251 D_D(DDciIconImagePlayer);
252
253 if (d->state == NotRunning)
254 return;
255
256 d->flags = (d->flags | IgnoreLastImageLoop) & (~AllowNonLastImageLoop);
257 d->currentLoopCount = 0;
258 d->loopCount = 0;
259 }
260
261 QImage DDciIconImagePlayer::readImage()
262 {
263 D_D(DDciIconImagePlayer);
264
265 if (d->state != WaitingRead)
266 return QImage();
267
268 QImage image;
269 int timerIntervel = 0;
270
271 if (d->currentHasCache()) {
272 image = d->currentCache().at(d->currentFrameNumber).image;
273 timerIntervel = qRound(d->currentCache().at(d->currentFrameNumber).duration / d->speed);
274 } else {
275 Q_ASSERT(!d->reversed());
276 Q_ASSERT(d->currentImage().currentImageNumber() == d->currentFrameNumber);
277 image = d->currentImage().toImage(d->palette);
278 if (d->flags & CacheAll) {
279 Q_ASSERT(d->currentImage().currentImageNumber() == d->currentFrameNumber);
280 Q_ASSERT(d->currentCache().size() == d->currentFrameNumber);
281 d->currentCache().append({image, d->currentImage().currentImageDuration()});
282 }
283 timerIntervel = qRound(d->currentImage().currentImageDuration() / d->speed);
284 }
285
286 d->timerId = startTimer(timerIntervel < 0 ? 0 : timerIntervel);
287 if (d->timerId == 0) {
288 qCWarning(diPlayer, "Can't start timer, will abort the animations.");
289 stop();
290 Q_EMIT finished();
291 return image;
292 }
293
294 d->setState(Running);
295
296 return image;
297 }
298
299 DDciIconImagePlayer::State DDciIconImagePlayer::state() const
300 {
301 D_DC(DDciIconImagePlayer);
302 return d->state;
303 }
304
305 void DDciIconImagePlayer::clearCache()
306 {
307 D_D(DDciIconImagePlayer);
308 d->clearCache();
309 }
310
311 bool DDciIconImagePlayer::start(float speed, Flags flags)
312 {
313 D_D(DDciIconImagePlayer);
314
315 if (d->state != NotRunning)
316 return false;
317
318 if (d->images.isEmpty())
319 return false;
320
321 d->flags = flags;
322 if (!d->flags.testFlag(Continue) || d->current < 0) {
323 d->current = d->reversed() ? d->images.size() - 1 : 0;
324 }
325
326 if (d->reversed()) {
327 d->flags |= CacheAll;
328
329 if (d->cachedFrames.isEmpty()) {
330 d->cachedFrames.resize(d->images.size());
331 for (int i = 0; i <= d->current; ++i) {
332 DDciIconImage &image = d->images[i];
333 if (!image.supportsAnimation())
334 continue;
335 image.reset();
336
337 do {
338 d->cachedFrames[i].append({image.toImage(d->palette), image.currentImageDuration()});
339 } while (image.jumpToNextImage());
340 }
341 } else if (!flags.testFlag(Continue)) {
342 auto &image = d->images[d->cachedFrames.size() - 1];
343 if (image.supportsAnimation()
344 && jumpImageTo(image, d->cachedFrames.last().size())) {
345 do {
346 d->cachedFrames.last().append({image.toImage(d->palette), image.currentImageDuration()});
347 } while (image.jumpToNextImage());
348 }
349
350 for (int i = d->cachedFrames.size(); i < d->images.size(); ++i) {
351 d->cachedFrames.append(QVector<DDciIconImagePlayerPrivate::Frame>());
352 auto &image = d->images[i];
353 if (!image.supportsAnimation())
354 continue;
355
356 image.reset();
357 do {
358 d->cachedFrames[i].append({image.toImage(d->palette), image.currentImageDuration()});
359 } while (image.jumpToNextImage());
360 }
361
362 d->currentFrameNumber = d->currentCache().size() - 1;
363 }
364 } else {
365 if (flags.testFlag(Continue)) {
366 if (d->current < 0)
367 d->current = 0;
368 } else {
369 d->current = 0;
370 d->currentFrameNumber = 0;
371 }
372
373 // Init cache
374 if (flags.testFlag(CacheAll) && d->cachedFrames.isEmpty()
375 && (d->current > 0 || d->currentFrameNumber > 0)) {
376 d->cachedFrames.resize(d->images.size());
377 DDciIconImage &image = d->images[d->current];
378 if (image.supportsAnimation()) {
379 image.reset();
380
381 do {
382 d->cachedFrames[d->current].append({image.toImage(d->palette), image.currentImageDuration()});
383 } while (image.jumpToNextImage());
384 }
385 }
386 }
387
388 d->speed = speed > 0.0 ? speed : 1.0;
389 d->loopCount = d->userLoopCount;
390
391 if (!d->ensureCurrent())
392 return false;
393 d->setState(WaitingRead);
394 Q_EMIT started();
395 Q_EMIT updated();
396
397 return true;
398 }
399
400 void DDciIconImagePlayer::stop()
401 {
402 D_D(DDciIconImagePlayer);
403 if (d->state == NotRunning)
404 return;
405 if (d->timerId > 0) {
406 killTimer(d->timerId);
407 d->timerId = 0;
408 }
409
410 if (d->flags & ClearCacheOnStop)
411 d->clearCache();
412
413 d->setState(NotRunning);
414 }
415
416 void DDciIconImagePlayer::timerEvent(QTimerEvent *event)
417 {
418 D_D(DDciIconImagePlayer);
419 if (event->timerId() != d->timerId)
420 return QObject::timerEvent(event);
421
422 killTimer(d->timerId);
423 d->timerId = 0;
424
425 bool finished = false;
426 const int newFrameNumber = d->currentFrameNumber + (d->reversed() ? -1 : 1);
427 if (!d->hasCache(d->current, newFrameNumber)) {
428 if (d->reversed()) {
429 finished = true;
430 } else {
431 finished = !jumpImageTo(d->currentImage(), newFrameNumber);
432 }
433 }
434
435 #define IGNORE_LOOP qEnvironmentVariableIsSet("D_DTK_DCI_PLAYER_IGNORE_ANIMATION_LOOP")
436
437 if (finished) {
438 if (d->currentLoopCount != 0 && --d->currentLoopCount && !IGNORE_LOOP) {
439 // reset value
440 d->currentFrameNumber = -1;
441 // replay current image
442 bool ok = d->initCurrent();
443 Q_ASSERT(ok);
444 finished = false;
445 } else {
446 const int newCurrent = d->current + (d->reversed() ? -1 : 1);
447 if (newCurrent >= 0 && newCurrent < d->images.size()) {
448 d->current = newCurrent;
449 d->currentFrameNumber = -1;
450 if (d->ensureCurrent())
451 finished = false;
452 }
453 }
454 } else {
455 d->currentFrameNumber = newFrameNumber;
456 }
457
458 if (finished && d->loopCount != 0 && (--d->loopCount) && !IGNORE_LOOP) {
459 d->current = d->reversed() ? d->images.size() - 1 : 0;
460 d->currentFrameNumber = -1;
461 if (d->ensureCurrent())
462 finished = false;
463 }
464
465 if (finished) {
466 stop();
467 Q_EMIT this->finished();
468 } else {
469 d->setState(WaitingRead);
470 Q_EMIT updated();
471 }
472 }
473
474 static QString iconModeToString(DDciIcon::Mode mode)
475 {
476 switch (mode) {
477 case DDciIcon::Normal:
478 return QStringLiteral("Normal");
479 case DDciIcon::Hover:
480 return QStringLiteral("Hover");
481 case DDciIcon::Pressed:
482 return QStringLiteral("Pressed");
483 case DDciIcon::Disabled:
484 return QStringLiteral("Disabled");
485 default:
486 break;
487 }
488
489 return nullptr;
490 }
491
492 class DDciIconPlayerPrivate : public DObjectPrivate
493 {
494 public:
495 DDciIconPlayerPrivate(DDciIconPlayer *qq)
496 : DObjectPrivate(qq) {}
497
498 void ensureInit();
499 void initPlayer();
500 bool ensureHoverModeLastImage();
501 void play(DDciIcon::Mode mode, DDciIconImagePlayer::Flags flags);
502 bool play(DDciIcon::Mode fromMode, DDciIcon::Mode toMode,
503 DDciIconImagePlayer::Flags extraflags = {});
504 inline void _q_playFromQueue(int extraflags = 0) {
505 if (player && player->state() != DDciIconImagePlayer::NotRunning)
506 return;
507
508 if (animationJobs.isEmpty())
509 return;
510
511 const auto &job = animationJobs.first();
512 bool ok = play(static_cast<DDciIcon::Mode>(job.from), static_cast<DDciIcon::Mode>(job.to),
513 DDciIconImagePlayer::Flags(static_cast<DDciIconImagePlayer::Flag>(extraflags)));
514 if (!ok) {
515 qCDebug(diPlayer, "Don't play any animations, from mode is \"%s\", to mode is \"%s\"",
516 qPrintable(iconModeToString(job.from)), qPrintable(iconModeToString(job.to)));
517 animationJobs.removeFirst();
518 } // else: the job will remove on animation finished
519 }
520 void playToQueue();
521 inline bool start(DDciIcon::Mode forMode, qreal speed = 1.0, DDciIconImagePlayer::Flags flags = {}) {
522 qCDebug(diPlayer) << "Start animation for" << iconModeToString(forMode);
523 const bool ok = player->start(speed, flags);
524 if (ok && forMode == DDciIcon::Hover && !flags.testFlag(DDciIconImagePlayer::InvertedOrder)) {
525 // When from Pressed mode to Hover mode, needs stop at the hover mode animation last frame.
526 saveImageForHoverModeOnFinished = true;
527 }
528
529 if (ok) {
530 setState(DDciIconPlayer::Busy);
531 } else if (diPlayer().isDebugEnabled()) {
532 qCDebug(diPlayer, "Failed on start animation for \"%s\"", qPrintable(iconModeToString(forMode)));
533 }
534
535 return ok;
536 }
537
538 inline void reset() {
539 if (player && player->state() != DDciIconImagePlayer::NotRunning) {
540 player->stop();
541 }
542 normal = DDciIconImage();
543 hover = DDciIconImage();
544 pressed = DDciIconImage();
545 disabled = DDciIconImage();
546 hoverModeLastImage = QImage();
547 }
548 inline bool isValid() const {
549 return !normal.isNull();
550 }
551 inline void setImage(const QImage &image) {
552 this->image = image;
553 Q_EMIT q_func()->updated();
554 }
555 inline void setImage(DDciIcon::Mode mode) {
556 auto &image = getImage(mode);
557 if (image.atBegin()) {
558 setImage(image.toImage(player->palette()));
559 } else {
560 setImage(icon.pixmap(devicePixelRatio, iconSize, theme, mode, player->palette()).toImage());
561 }
562 }
563 inline void setFinishedImage(DDciIcon::Mode mode) {
564 finishedImage = icon.pixmap(devicePixelRatio, iconSize, theme, mode, player->palette()).toImage();
565 }
566 inline const DDciIconImage &getImage(DDciIcon::Mode mode) const {
567 if (mode == DDciIcon::Hover) {
568 return hover;
569 } else if (mode == DDciIcon::Pressed) {
570 return pressed;
571 } else if (mode == DDciIcon::Disabled) {
572 return disabled;
573 }
574
575 Q_ASSERT(mode == DDciIcon::Normal);
576 return normal;
577 }
578
579 inline void setState(DDciIconPlayer::State newState) {
580 if (state == newState)
581 return;
582 state = newState;
583 Q_EMIT q_func()->stateChanged();
584 }
585
586 DDciIconPlayer::State state = DDciIconPlayer::Idle;
587
588 DDciIcon icon;
589 DDciIcon::Theme theme = DDciIcon::Light;
590 DDciIcon::Mode mode = DDciIcon::Normal;
591 DDciIcon::Mode lastMode = DDciIcon::Normal;
592 int iconSize = -1;
593 qreal devicePixelRatio = 1.0;
594
595 DDciIconImage normal;
596 DDciIconImage hover;
597 DDciIconImage pressed;
598 DDciIconImage disabled;
599 DDciIconImagePlayer *player = nullptr;
600
601 struct Animation {
602 DDciIcon::Mode from;
603 DDciIcon::Mode to;
604 };
605 QVector<Animation> animationJobs;
606
607 bool saveImageForHoverModeOnFinished = false;
608
609 QImage image;
610 QImage finishedImage;
611 QImage hoverModeLastImage;
612
613 D_DECLARE_PUBLIC(DDciIconPlayer)
614 };
615
616 void DDciIconPlayerPrivate::ensureInit()
617 {
618 initPlayer();
619
620 if (!normal.isNull())
621 return;
622 if (icon.isNull())
623 return;
624 normal = icon.image(icon.matchIcon(iconSize, theme, DDciIcon::Normal, DDciIcon::DontFallbackMode), iconSize, devicePixelRatio);
625 hover = icon.image(icon.matchIcon(iconSize, theme, DDciIcon::Hover, DDciIcon::DontFallbackMode), iconSize, devicePixelRatio);
626 pressed = icon.image(icon.matchIcon(iconSize, theme, DDciIcon::Pressed, DDciIcon::DontFallbackMode), iconSize, devicePixelRatio);
627 disabled = icon.image(icon.matchIcon(iconSize, theme, DDciIcon::Disabled, DDciIcon::DontFallbackMode), iconSize, devicePixelRatio);
628 }
629
630 void DDciIconPlayerPrivate::initPlayer()
631 {
632 if (player)
633 return;
634 player = new DDciIconImagePlayer(q_func());
635
636 D_Q(DDciIconPlayer);
637 QObject::connect(player, &DDciIconImagePlayer::updated, q, [this]() {
638 setImage(player->readImage());
639 });
640 QObject::connect(player, &DDciIconImagePlayer::finished, q, [q, this]() {
641 qCDebug(diPlayer, "Current animation finished!");
642
643 if (saveImageForHoverModeOnFinished) {
644 saveImageForHoverModeOnFinished = false;
645 hoverModeLastImage = image;
646 }
647
648 if (!animationJobs.isEmpty()) {
649 // Remove the finished animation
650 animationJobs.removeFirst();
651
652 qCDebug(diPlayer, "Number of animations remaining is %i", animationJobs.size());
653 if (!animationJobs.isEmpty()) {
654 _q_playFromQueue();
655 return;
656 }
657 }
658 if (!finishedImage.isNull()) {
659 setImage(finishedImage);
660 finishedImage = QImage();
661 }
662
663 if (mode == DDciIcon::Normal || mode == DDciIcon::Disabled) {
664 player->clearCache();
665 }
666
667 setState(DDciIconPlayer::Idle);
668 });
669 }
670
671 bool DDciIconPlayerPrivate::ensureHoverModeLastImage()
672 {
673 if (!hoverModeLastImage.isNull())
674 return true;
675 if (!hover.supportsAnimation())
676 return false;
677 DDciIconImage image;
678 const bool currentHoverImageIsUsable = hover.atEnd() || !player
679 || player->state() != DDciIconImagePlayer::NotRunning;
680 if (!currentHoverImageIsUsable) {
681 if (icon.isNull())
682 return false;
683 image = icon.image(icon.matchIcon(iconSize, theme, DDciIcon::Hover, DDciIcon::DontFallbackMode),
684 iconSize, devicePixelRatio);
685 }
686 if (image.isNull())
687 return false;
688
689 while (!image.atEnd()) {
690 if (!image.jumpToNextImage())
691 break;
692 }
693 if (!image.atEnd())
694 return false;
695 Q_ASSERT(player);
696 hoverModeLastImage = image.toImage(player->palette());
697 return !hoverModeLastImage.isNull();
698 }
699
700 void DDciIconPlayerPrivate::play(DDciIcon::Mode mode, DDciIconImagePlayer::Flags flags)
701 {
702 if (diPlayer().isDebugEnabled()) {
703 qCDebug(diPlayer) << "Immediate play animation for" << iconModeToString(mode);
704 }
705
706 ensureInit();
707 if (normal.isNull())
708 return;
709
710 // Ensure the old animations clean
711 animationJobs.clear();
712 player->stop();
713 finishedImage = QImage();
714
715 const DDciIconImage &image = getImage(mode);
716 if (!image.supportsAnimation())
717 return;
718
719 player->setImages({image});
720 start(mode, 1.0, flags);
721 }
722
723 bool DDciIconPlayerPrivate::play(DDciIcon::Mode fromMode, DDciIcon::Mode toMode,
724 DDciIconImagePlayer::Flags extraflags)
725 {
726 ensureInit();
727 finishedImage = QImage();
728
729 if (normal.isNull()) {
730 setImage(QImage());
731 return false;
732 }
733
734 if (fromMode == DDciIcon::Normal) {
735 if (toMode == DDciIcon::Normal) {
736 setImage(DDciIcon::Normal);
737 } else if (toMode == DDciIcon::Hover) {
738 if (hover.isNull()) {
739 return false;
740 }
741
742 if (hover.supportsAnimation()) {
743 player->setImages({hover});
744 return start(toMode, 1.0, DDciIconImagePlayer::CacheAll | extraflags);
745 } else {
746 setImage(DDciIcon::Hover);
747 }
748 } else if (toMode == DDciIcon::Pressed) {
749 if (pressed.isNull()) {
750 return false;
751 }
752
753 if (pressed.supportsAnimation()) {
754 if (hover.supportsAnimation()) {
755 player->setImages({hover, pressed});
756 return start(toMode, 2.0, DDciIconImagePlayer::CacheAll | extraflags);
757 } else {
758 player->setImages({pressed});
759 return start(toMode, 1.0, DDciIconImagePlayer::CacheAll | extraflags);
760 }
761 } else {
762 setImage(DDciIcon::Pressed);
763 }
764 } else if (toMode == DDciIcon::Disabled) {
765 if (disabled.isNull())
766 return false;
767
768 if (disabled.supportsAnimation()) {
769 player->setImages({disabled});
770 return start(toMode, 1.0, DDciIconImagePlayer::IgnoreLastImageLoop | extraflags);
771 } else {
772 setImage(DDciIcon::Disabled);
773 }
774 }
775 } else if (fromMode == DDciIcon::Hover) {
776 Q_ASSERT(toMode != DDciIcon::Hover);
777
778 if (toMode == DDciIcon::Normal) {
779 if (hover.supportsAnimation()) {
780 setFinishedImage(DDciIcon::Normal);
781 player->setImages({hover});
782 return start(toMode, 1.0, DDciIconImagePlayer::InvertedOrder
783 | DDciIconImagePlayer::IgnoreLastImageLoop
784 | DDciIconImagePlayer::ClearCacheOnStop
785 | extraflags);
786 } else {
787 setImage(DDciIcon::Normal);
788 }
789 } else if (toMode == DDciIcon::Hover) {
790 } else if (toMode == DDciIcon::Pressed) {
791 if (pressed.isNull()) {
792 if (hover.supportsAnimation()) {
793 player->setImages({hover});
794 return start(toMode, 1.0, DDciIconImagePlayer::InvertedOrder
795 | DDciIconImagePlayer::IgnoreLastImageLoop
796 | extraflags);
797 } else {
798 setImage(DDciIcon::Normal);
799 }
800 return false;
801 }
802
803 if (pressed.supportsAnimation()) {
804 player->setImages({pressed});
805 return start(toMode, 1.0, DDciIconImagePlayer::CacheAll | extraflags);
806 } else {
807 setImage(DDciIcon::Pressed);
808 }
809 } else if (toMode == DDciIcon::Disabled) {
810 if (disabled.isNull()) {
811 setImage(DDciIcon::Normal);
812 return false;
813 }
814
815 if (disabled.supportsAnimation()) {
816 player->setImages({disabled});
817 return start(toMode, 1.0, DDciIconImagePlayer::IgnoreLastImageLoop | extraflags);
818 } else {
819 setImage(DDciIcon::Disabled);
820 }
821 }
822 } else if (fromMode == DDciIcon::Pressed) {
823 Q_ASSERT(toMode != DDciIcon::Pressed);
824
825 if (toMode == DDciIcon::Normal) {
826 if (!pressed.supportsAnimation()) {
827 setImage(DDciIcon::Normal);
828 return false;
829 }
830
831 setFinishedImage(DDciIcon::Normal);
832 if (hover.supportsAnimation()) {
833 player->setImages({hover, pressed});
834 return start(toMode, 2.0, DDciIconImagePlayer::InvertedOrder
835 | DDciIconImagePlayer::IgnoreLastImageLoop
836 | extraflags);
837 } else {
838 player->setImages({pressed});
839 return start(toMode, 1.0, DDciIconImagePlayer::InvertedOrder
840 | DDciIconImagePlayer::IgnoreLastImageLoop
841 | extraflags);
842 }
843 } else if (toMode == DDciIcon::Hover) {
844 if (pressed.isNull()) {
845 if (hover.supportsAnimation()) {
846 player->setImages({hover});
847 return start(toMode, 1.0, DDciIconImagePlayer::CacheAll | extraflags);
848 } else {
849 setImage(DDciIcon::Hover);
850 }
851 return false;
852 }
853
854 if (pressed.supportsAnimation()) {
855 ensureHoverModeLastImage();
856 finishedImage = hoverModeLastImage;
857 player->setImages({pressed});
858 return start(toMode, 1.0, DDciIconImagePlayer::InvertedOrder
859 | DDciIconImagePlayer::IgnoreLastImageLoop
860 | extraflags);
861 } else {
862 setImage(DDciIcon::Hover);
863 }
864 } else if (toMode == DDciIcon::Pressed) {
865 } else if (toMode == DDciIcon::Disabled) {
866 if (disabled.isNull()) {
867 setImage(DDciIcon::Normal);
868 return false;
869 }
870
871 if (disabled.supportsAnimation()) {
872 player->setImages({disabled});
873 return start(toMode, 1.0, DDciIconImagePlayer::IgnoreLastImageLoop | extraflags);
874 } else {
875 setImage(DDciIcon::Disabled);
876 }
877 }
878 } else if (fromMode == DDciIcon::Disabled) {
879 Q_ASSERT(toMode != DDciIcon::Disabled);
880
881 if (disabled.supportsAnimation()) {
882 setFinishedImage(DDciIcon::Normal);
883 player->setImages({disabled});
884 return start(toMode, 1.0, DDciIconImagePlayer::InvertedOrder
885 | DDciIconImagePlayer::IgnoreLastImageLoop
886 | extraflags);
887 } else {
888 setImage(DDciIcon::Normal);
889 }
890 }
891
892 return false;
893 }
894
895 void DDciIconPlayerPrivate::playToQueue()
896 {
897 DDciIconImagePlayer::Flags extraFlags = {};
898
899 if (diPlayer().isDebugEnabled()) {
900 qCDebug(diPlayer, "Request play animation in queue, from mode is \"%s\", to mode is \"%s\"",
901 qPrintable(iconModeToString(lastMode)), qPrintable(iconModeToString(mode)));
902 }
903
904 if (!animationJobs.isEmpty()) {
905 if (diPlayer().isDebugEnabled()) {
906 qCDebug(diPlayer, "Old Animation Queue:");
907 for (int i = 0; i < animationJobs.count(); ++i) {
908 const Animation &job = animationJobs.at(i);
909 qCDebug(diPlayer, " %d. from mode is \"%s\", to mode is \"%s\"", i + 1,
910 qPrintable(iconModeToString(job.from)), qPrintable(iconModeToString(job.to)));
911 }
912 }
913
914 // Ignore the same request
915 if (animationJobs.last().from == lastMode && animationJobs.last().to == mode) {
916 qCDebug(diPlayer, "Same as the last animation, ignores this request");
917 return;
918 }
919
920 if (animationJobs.last().from == mode && animationJobs.last().to == lastMode) {
921 if (animationJobs.size() > 1) {
922 animationJobs.removeLast();
923 qCDebug(diPlayer, "Offsets the last unplayed animation, discards the animation, and ignores this request");
924 return;
925 } else {
926 if (player && player->state() != DDciIconImagePlayer::NotRunning) {
927 extraFlags |= DDciIconImagePlayer::Continue;
928 player->stop();
929 animationJobs.removeFirst();
930 }
931 }
932 }
933 } else {
934 if (player)
935 player->stop();
936 qCDebug(diPlayer, "Old Animation queue is empty");
937 }
938
939 animationJobs.append({lastMode, mode});
940
941 if (player && player->state() != DDciIconImagePlayer::NotRunning) {
942 player->abortLoop();
943 Q_ASSERT(!player->currentLoopForever());
944
945 // Don't stop current animation, wating play to end.
946 // Ensure the continuity of the animation when multiple states change continuously.
947 // An example is changing from the Normal state to the Hover state and then to the
948 // Pressed state in a short period of time. At this time, the animation in the
949 // hover state may not have finished playing.
950 qCDebug(diPlayer, "Wait the current animation finished to continue play the new animation.");
951 return;
952 }
953
954 QMetaObject::invokeMethod(q_func(), "_q_playFromQueue", Qt::QueuedConnection, Q_ARG(int, extraFlags));
955 }
956
957 DDciIconPlayer::DDciIconPlayer(QObject *parent)
958 : QObject(parent)
959 , DObject(*new DDciIconPlayerPrivate(this))
960 {
961
962 }
963
964 DDciIconPlayer::State DDciIconPlayer::state() const
965 {
966 D_DC(DDciIconPlayer);
967 return d->state;
968 }
969
970 void DDciIconPlayer::setIcon(const DDciIcon &icon)
971 {
972 D_D(DDciIconPlayer);
973 d->icon = icon;
974 d->reset();
975 d->playToQueue();
976 }
977
978 DDciIcon DDciIconPlayer::icon() const
979 {
980 D_DC(DDciIconPlayer);
981 return d->icon;
982 }
983
984 void DDciIconPlayer::setTheme(DDciIcon::Theme theme)
985 {
986 D_D(DDciIconPlayer);
987 if (d->theme == theme)
988 return;
989 d->theme = theme;
990 d->reset();
991 d->playToQueue();
992 }
993
994 DDciIcon::Theme DDciIconPlayer::theme() const
995 {
996 D_DC(DDciIconPlayer);
997 return d->theme;
998 }
999
1000 void DDciIconPlayer::setMode(DDciIcon::Mode mode)
1001 {
1002 D_D(DDciIconPlayer);
1003 if (d->mode == mode)
1004 return;
1005 d->lastMode = d->mode;
1006 d->mode = mode;
1007 Q_EMIT modeChanged(d->lastMode, mode);
1008
1009 if (diPlayer().isDebugEnabled()) {
1010 qCDebug(diPlayer) << this
1011 << "Old Mode:" << iconModeToString(d->lastMode)
1012 << "New Mode" << iconModeToString(d->mode);
1013 }
1014
1015 if (mode == DDciIcon::Disabled)
1016 abort();
1017 d->playToQueue();
1018 }
1019
1020 DDciIcon::Mode DDciIconPlayer::mode() const
1021 {
1022 D_DC(DDciIconPlayer);
1023 return d->mode;
1024 }
1025
1026 void DDciIconPlayer::setIconSize(int size)
1027 {
1028 D_D(DDciIconPlayer);
1029 if (d->iconSize == size)
1030 return;
1031 d->iconSize = size;
1032 d->reset();
1033 d->playToQueue();
1034 }
1035
1036 int DDciIconPlayer::iconSize() const
1037 {
1038 D_DC(DDciIconPlayer);
1039 return d->iconSize;
1040 }
1041
1042 void DDciIconPlayer::setDevicePixelRatio(qreal devicePixelRatio)
1043 {
1044 D_D(DDciIconPlayer);
1045 if (qFuzzyCompare(d->devicePixelRatio, devicePixelRatio))
1046 return;
1047 d->devicePixelRatio = devicePixelRatio;
1048 d->reset();
1049 d->playToQueue();
1050 }
1051
1052 qreal DDciIconPlayer::devicePixelRatio() const
1053 {
1054 D_DC(DDciIconPlayer);
1055 return d->devicePixelRatio;
1056 }
1057
1058 void DDciIconPlayer::setPalette(const DDciIconPalette &palette)
1059 {
1060 D_D(DDciIconPlayer);
1061 d->initPlayer();
1062 if (d->player->setPalette(palette)) {
1063 if (d->hover.hasPalette())
1064 d->hoverModeLastImage = QImage();
1065
1066 const auto &image = d->getImage(d->mode);
1067 if (image.hasPalette())
1068 d->playToQueue();
1069 }
1070 }
1071
1072 QImage DDciIconPlayer::currentImage() const
1073 {
1074 D_DC(DDciIconPlayer);
1075 return d->image;
1076 }
1077
1078 void DDciIconPlayer::play(DDciIcon::Mode mode)
1079 {
1080 D_D(DDciIconPlayer);
1081 d->play(mode, DDciIconImagePlayer::IgnoreLastImageLoop);
1082 }
1083
1084 void DDciIconPlayer::stop()
1085 {
1086 D_D(DDciIconPlayer);
1087 if (d->player)
1088 d->player->stop();
1089 d->setState(Idle);
1090 }
1091
1092 void DDciIconPlayer::abort()
1093 {
1094 D_D(DDciIconPlayer);
1095 d->animationJobs.clear();
1096 if (d->player)
1097 d->player->stop();
1098 d->setState(Idle);
1099 }
1100
1101 DGUI_END_NAMESPACE
1102
1103 #include "moc_ddciiconplayer.cpp"