1 | /* |
2 | This file is part of the KDE project |
3 | SPDX-FileCopyrightText: 2007 Fredrik Höglund <fredrik@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #ifndef DELEGATEANIMATIONHANDLER_P_H |
9 | #define DELEGATEANIMATIONHANDLER_P_H |
10 | |
11 | #include <QBasicTimer> |
12 | #include <QElapsedTimer> |
13 | #include <QMap> |
14 | #include <QPersistentModelIndex> |
15 | #include <QStyle> |
16 | #include <QTime> |
17 | #include <QTimeLine> |
18 | #include <QTimer> |
19 | |
20 | class QAbstractItemView; |
21 | |
22 | namespace KIO |
23 | { |
24 | class CachedRendering : public QObject |
25 | { |
26 | Q_OBJECT |
27 | public: |
28 | CachedRendering(QStyle::State state, const QSize &size, const QModelIndex &validityIndex, qreal devicePixelRatio = 1.0); |
29 | bool checkValidity(QStyle::State current) const |
30 | { |
31 | return state == current && valid; |
32 | } |
33 | |
34 | QStyle::State state; |
35 | QPixmap regular; |
36 | QPixmap hover; |
37 | |
38 | bool valid; |
39 | QPersistentModelIndex validityIndex; |
40 | private Q_SLOTS: |
41 | void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
42 | void modelReset(); |
43 | }; |
44 | |
45 | class AnimationState |
46 | { |
47 | public: |
48 | ~AnimationState(); |
49 | AnimationState(const AnimationState &) = delete; |
50 | AnimationState &operator=(const AnimationState &) = delete; |
51 | |
52 | // Progress of the mouse hovering animation |
53 | qreal hoverProgress() const; |
54 | // Progress of the icon fading animation |
55 | qreal fadeProgress() const; |
56 | // Angle of the painter, to paint the animation for a file job on an item |
57 | qreal jobAnimationAngle() const; |
58 | |
59 | void setJobAnimation(bool value); |
60 | bool hasJobAnimation() const; |
61 | |
62 | CachedRendering *cachedRendering() const |
63 | { |
64 | return renderCache; |
65 | } |
66 | // The previous render-cache is deleted, if there was one |
67 | void setCachedRendering(CachedRendering *rendering) |
68 | { |
69 | delete renderCache; |
70 | renderCache = rendering; |
71 | } |
72 | |
73 | // Returns current cached rendering, and removes it from this state. |
74 | // The caller has the ownership. |
75 | CachedRendering *takeCachedRendering() |
76 | { |
77 | CachedRendering *ret = renderCache; |
78 | renderCache = nullptr; |
79 | return ret; |
80 | } |
81 | |
82 | CachedRendering *cachedRenderingFadeFrom() const |
83 | { |
84 | return fadeFromRenderCache; |
85 | } |
86 | // The previous render-cache is deleted, if there was one |
87 | void setCachedRenderingFadeFrom(CachedRendering *rendering) |
88 | { |
89 | delete fadeFromRenderCache; |
90 | fadeFromRenderCache = rendering; |
91 | if (rendering) { |
92 | m_fadeProgress = 0; |
93 | } else { |
94 | m_fadeProgress = 1; |
95 | } |
96 | } |
97 | |
98 | private: |
99 | explicit AnimationState(const QModelIndex &index); |
100 | bool update(); |
101 | |
102 | QPersistentModelIndex index; |
103 | QTimeLine::Direction direction; |
104 | bool animating; |
105 | bool jobAnimation; |
106 | qreal progress; |
107 | qreal m_fadeProgress; |
108 | qreal m_jobAnimationAngle; |
109 | QElapsedTimer time; |
110 | QElapsedTimer creationTime; |
111 | CachedRendering *renderCache; |
112 | CachedRendering *fadeFromRenderCache; |
113 | |
114 | friend class DelegateAnimationHandler; |
115 | }; |
116 | |
117 | class DelegateAnimationHandler : public QObject |
118 | { |
119 | Q_OBJECT |
120 | |
121 | typedef QList<AnimationState *> AnimationList; |
122 | typedef QMapIterator<const QAbstractItemView *, AnimationList *> AnimationListsIterator; |
123 | typedef QMutableMapIterator<const QAbstractItemView *, AnimationList *> MutableAnimationListsIterator; |
124 | |
125 | public: |
126 | explicit DelegateAnimationHandler(QObject *parent = nullptr); |
127 | ~DelegateAnimationHandler() override; |
128 | |
129 | AnimationState *animationState(const QStyleOption &option, const QModelIndex &index, const QAbstractItemView *view); |
130 | |
131 | void restartAnimation(AnimationState *state); |
132 | |
133 | void gotNewIcon(const QModelIndex &index); |
134 | |
135 | private Q_SLOTS: |
136 | void viewDeleted(QObject *view); |
137 | void sequenceTimerTimeout(); |
138 | |
139 | private: |
140 | void eventuallyStartIteration(const QModelIndex &index); |
141 | AnimationState *findAnimationState(const QAbstractItemView *view, const QModelIndex &index) const; |
142 | void addAnimationState(AnimationState *state, const QAbstractItemView *view); |
143 | void startAnimation(AnimationState *state); |
144 | int runAnimations(AnimationList *list, const QAbstractItemView *view); |
145 | void timerEvent(QTimerEvent *event) override; |
146 | void setSequenceIndex(int arg1); |
147 | |
148 | private: |
149 | QMap<const QAbstractItemView *, AnimationList *> animationLists; |
150 | QElapsedTimer fadeInAddTime; |
151 | QBasicTimer timer; |
152 | // Icon sequence handling: |
153 | QPersistentModelIndex sequenceModelIndex; |
154 | QTimer iconSequenceTimer; |
155 | int currentSequenceIndex; |
156 | }; |
157 | |
158 | } |
159 | |
160 | #endif |
161 | |