]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistviewanimation.cpp
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() - propertyAnim
->currentTime();
70 const bool block
= propertyAnim
->signalsBlocked();
71 propertyAnim
->blockSignals(true);
74 QPointF endPos
= propertyAnim
->endValue().toPointF();
75 if (m_scrollOrientation
== Qt::Vertical
) {
81 propertyAnim
->setDuration(remainingDuration
);
82 propertyAnim
->setStartValue(currentPos
);
83 propertyAnim
->setEndValue(endPos
);
84 propertyAnim
->start();
85 propertyAnim
->blockSignals(block
);
87 widget
->setPos(currentPos
);
93 qreal
KItemListViewAnimation::scrollOffset() const
95 return m_scrollOffset
;
98 void KItemListViewAnimation::start(QGraphicsWidget
*widget
, AnimationType type
, const QVariant
&endValue
)
102 QPropertyAnimation
*propertyAnim
= nullptr;
103 const int animationDuration
= widget
->style()->styleHint(QStyle::SH_Widget_Animate
) ? 200 : 1;
106 case MovingAnimation
: {
107 const QPointF newPos
= endValue
.toPointF();
108 if (newPos
== widget
->pos()) {
112 propertyAnim
= new QPropertyAnimation(widget
, "pos");
113 propertyAnim
->setDuration(animationDuration
);
114 propertyAnim
->setEndValue(newPos
);
118 case CreateAnimation
: {
119 propertyAnim
= new QPropertyAnimation(widget
, "opacity");
120 propertyAnim
->setEasingCurve(QEasingCurve::InQuart
);
121 propertyAnim
->setDuration(animationDuration
);
122 propertyAnim
->setStartValue(0.0);
123 propertyAnim
->setEndValue(1.0);
127 case DeleteAnimation
: {
128 propertyAnim
= new QPropertyAnimation(widget
, "opacity");
129 propertyAnim
->setEasingCurve(QEasingCurve::OutQuart
);
130 propertyAnim
->setDuration(animationDuration
);
131 propertyAnim
->setStartValue(1.0);
132 propertyAnim
->setEndValue(0.0);
136 case ResizeAnimation
: {
137 const QSizeF newSize
= endValue
.toSizeF();
138 if (newSize
== widget
->size()) {
142 propertyAnim
= new QPropertyAnimation(widget
, "size");
143 propertyAnim
->setDuration(animationDuration
);
144 propertyAnim
->setEndValue(newSize
);
148 case IconResizeAnimation
: {
149 propertyAnim
= new QPropertyAnimation(widget
, QByteArrayLiteral("iconSize"));
150 propertyAnim
->setDuration(animationDuration
);
151 propertyAnim
->setEndValue(endValue
);
160 Q_ASSERT(propertyAnim
);
161 connect(propertyAnim
, &QPropertyAnimation::finished
, this, &KItemListViewAnimation::slotFinished
);
162 m_animation
[type
].insert(widget
, propertyAnim
);
164 propertyAnim
->start();
167 void KItemListViewAnimation::stop(QGraphicsWidget
*widget
, AnimationType type
)
169 QPropertyAnimation
*propertyAnim
= m_animation
[type
].value(widget
);
171 propertyAnim
->stop();
174 case MovingAnimation
:
176 case CreateAnimation
:
177 widget
->setOpacity(1.0);
179 case DeleteAnimation
:
180 widget
->setOpacity(0.0);
182 case ResizeAnimation
:
188 m_animation
[type
].remove(widget
);
191 Q_EMIT
finished(widget
, type
);
195 void KItemListViewAnimation::stop(QGraphicsWidget
*widget
)
197 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
198 stop(widget
, static_cast<AnimationType
>(type
));
202 bool KItemListViewAnimation::isStarted(QGraphicsWidget
*widget
, AnimationType type
) const
204 return m_animation
[type
].value(widget
);
207 bool KItemListViewAnimation::isStarted(QGraphicsWidget
*widget
) const
209 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
210 if (isStarted(widget
, static_cast<AnimationType
>(type
))) {
217 void KItemListViewAnimation::slotFinished()
219 QPropertyAnimation
*finishedAnim
= qobject_cast
<QPropertyAnimation
*>(sender());
220 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
221 QMutableHashIterator
<QGraphicsWidget
*, QPropertyAnimation
*> it(m_animation
[type
]);
222 while (it
.hasNext()) {
224 QPropertyAnimation
*propertyAnim
= it
.value();
225 if (propertyAnim
== finishedAnim
) {
226 QGraphicsWidget
*widget
= it
.key();
228 finishedAnim
->deleteLater();
230 Q_EMIT
finished(widget
, static_cast<AnimationType
>(type
));
238 #include "moc_kitemlistviewanimation.cpp"