]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistviewanimation.cpp
0c16c8b0a00cc2c81bb2edfff7fc1213a7ca70ca
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kitemlistviewanimation.h"
8 #include "kitemviews/kitemlistview.h"
10 #include <QPropertyAnimation>
12 KItemListViewAnimation::KItemListViewAnimation(QObject
* parent
) :
14 m_scrollOrientation(Qt::Vertical
),
20 KItemListViewAnimation::~KItemListViewAnimation()
22 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
23 qDeleteAll(m_animation
[type
]);
27 void KItemListViewAnimation::setScrollOrientation(Qt::Orientation orientation
)
29 m_scrollOrientation
= orientation
;
32 Qt::Orientation
KItemListViewAnimation::scrollOrientation() const
34 return m_scrollOrientation
;
37 void KItemListViewAnimation::setScrollOffset(qreal offset
)
39 const qreal diff
= m_scrollOffset
- offset
;
40 m_scrollOffset
= offset
;
42 // The change of the offset requires that the position of all
43 // animated QGraphicsWidgets get adjusted. An exception is made
44 // for the delete animation that should just fade away on the
46 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
47 if (type
== DeleteAnimation
) {
51 QHashIterator
<QGraphicsWidget
*, QPropertyAnimation
*> it(m_animation
[type
]);
52 while (it
.hasNext()) {
55 QGraphicsWidget
* widget
= it
.key();
56 QPropertyAnimation
* propertyAnim
= it
.value();
58 QPointF currentPos
= widget
->pos();
59 if (m_scrollOrientation
== Qt::Vertical
) {
60 currentPos
.ry() += diff
;
62 currentPos
.rx() += diff
;
65 if (type
== MovingAnimation
) {
66 // Stop the animation, calculate the moved start- and end-value
67 // and restart the animation for the remaining duration.
68 const int remainingDuration
= propertyAnim
->duration()
69 - propertyAnim
->currentTime();
71 const bool block
= propertyAnim
->signalsBlocked();
72 propertyAnim
->blockSignals(true);
75 QPointF endPos
= propertyAnim
->endValue().toPointF();
76 if (m_scrollOrientation
== Qt::Vertical
) {
82 propertyAnim
->setDuration(remainingDuration
);
83 propertyAnim
->setStartValue(currentPos
);
84 propertyAnim
->setEndValue(endPos
);
85 propertyAnim
->start();
86 propertyAnim
->blockSignals(block
);
88 widget
->setPos(currentPos
);
94 qreal
KItemListViewAnimation::scrollOffset() const
96 return m_scrollOffset
;
99 void KItemListViewAnimation::start(QGraphicsWidget
* widget
, AnimationType type
, const QVariant
& endValue
)
103 QPropertyAnimation
* propertyAnim
= nullptr;
104 const int animationDuration
= widget
->style()->styleHint(QStyle::SH_Widget_Animate
) ? 200 : 1;
107 case MovingAnimation
: {
108 const QPointF newPos
= endValue
.toPointF();
109 if (newPos
== widget
->pos()) {
113 propertyAnim
= new QPropertyAnimation(widget
, "pos");
114 propertyAnim
->setDuration(animationDuration
);
115 propertyAnim
->setEndValue(newPos
);
119 case CreateAnimation
: {
120 propertyAnim
= new QPropertyAnimation(widget
, "opacity");
121 propertyAnim
->setEasingCurve(QEasingCurve::InQuart
);
122 propertyAnim
->setDuration(animationDuration
);
123 propertyAnim
->setStartValue(0.0);
124 propertyAnim
->setEndValue(1.0);
128 case DeleteAnimation
: {
129 propertyAnim
= new QPropertyAnimation(widget
, "opacity");
130 propertyAnim
->setEasingCurve(QEasingCurve::OutQuart
);
131 propertyAnim
->setDuration(animationDuration
);
132 propertyAnim
->setStartValue(1.0);
133 propertyAnim
->setEndValue(0.0);
137 case ResizeAnimation
: {
138 const QSizeF newSize
= endValue
.toSizeF();
139 if (newSize
== widget
->size()) {
143 propertyAnim
= new QPropertyAnimation(widget
, "size");
144 propertyAnim
->setDuration(animationDuration
);
145 propertyAnim
->setEndValue(newSize
);
149 case IconResizeAnimation
: {
150 propertyAnim
= new QPropertyAnimation(widget
, QByteArrayLiteral("iconSize"));
151 propertyAnim
->setDuration(animationDuration
);
152 propertyAnim
->setEndValue(endValue
);
161 Q_ASSERT(propertyAnim
);
162 connect(propertyAnim
, &QPropertyAnimation::finished
, this, &KItemListViewAnimation::slotFinished
);
163 m_animation
[type
].insert(widget
, propertyAnim
);
165 propertyAnim
->start();
168 void KItemListViewAnimation::stop(QGraphicsWidget
* widget
, AnimationType type
)
170 QPropertyAnimation
* propertyAnim
= m_animation
[type
].value(widget
);
172 propertyAnim
->stop();
175 case MovingAnimation
: break;
176 case CreateAnimation
: widget
->setOpacity(1.0); break;
177 case DeleteAnimation
: widget
->setOpacity(0.0); break;
178 case ResizeAnimation
: break;
182 m_animation
[type
].remove(widget
);
185 Q_EMIT
finished(widget
, type
);
189 void KItemListViewAnimation::stop(QGraphicsWidget
* widget
)
191 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
192 stop(widget
, static_cast<AnimationType
>(type
));
196 bool KItemListViewAnimation::isStarted(QGraphicsWidget
*widget
, AnimationType type
) const
198 return m_animation
[type
].value(widget
);
201 bool KItemListViewAnimation::isStarted(QGraphicsWidget
* widget
) const
203 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
204 if (isStarted(widget
, static_cast<AnimationType
>(type
))) {
211 void KItemListViewAnimation::slotFinished()
213 QPropertyAnimation
* finishedAnim
= qobject_cast
<QPropertyAnimation
*>(sender());
214 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
215 QMutableHashIterator
<QGraphicsWidget
*, QPropertyAnimation
*> it(m_animation
[type
]);
216 while (it
.hasNext()) {
218 QPropertyAnimation
* propertyAnim
= it
.value();
219 if (propertyAnim
== finishedAnim
) {
220 QGraphicsWidget
* widget
= it
.key();
222 finishedAnim
->deleteLater();
224 Q_EMIT
finished(widget
, static_cast<AnimationType
>(type
));