]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistviewanimation.cpp
5d8dade6eed6948289e91e6a41398239dc6b2fc1
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "kitemlistviewanimation.h"
22 #include <kitemviews/kitemlistview.h>
26 #include <QGraphicsWidget>
27 #include <QPropertyAnimation>
29 KItemListViewAnimation::KItemListViewAnimation(QObject
* parent
) :
31 m_scrollOrientation(Qt::Vertical
),
37 KItemListViewAnimation::~KItemListViewAnimation()
39 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
40 qDeleteAll(m_animation
[type
]);
44 void KItemListViewAnimation::setScrollOrientation(Qt::Orientation orientation
)
46 m_scrollOrientation
= orientation
;
49 Qt::Orientation
KItemListViewAnimation::scrollOrientation() const
51 return m_scrollOrientation
;
54 void KItemListViewAnimation::setScrollOffset(qreal offset
)
56 const qreal diff
= m_scrollOffset
- offset
;
57 m_scrollOffset
= offset
;
59 // The change of the offset requires that the position of all
60 // animated QGraphicsWidgets get adjusted. An exception is made
61 // for the delete animation that should just fade away on the
63 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
64 if (type
== DeleteAnimation
) {
68 QHashIterator
<QGraphicsWidget
*, QPropertyAnimation
*> it(m_animation
[type
]);
69 while (it
.hasNext()) {
72 QGraphicsWidget
* widget
= it
.key();
73 QPropertyAnimation
* propertyAnim
= it
.value();
75 QPointF currentPos
= widget
->pos();
76 if (m_scrollOrientation
== Qt::Vertical
) {
77 currentPos
.ry() += diff
;
79 currentPos
.rx() += diff
;
82 if (type
== MovingAnimation
) {
83 // Stop the animation, calculate the moved start- and end-value
84 // and restart the animation for the remaining duration.
85 const int remainingDuration
= propertyAnim
->duration()
86 - propertyAnim
->currentTime();
88 const bool block
= propertyAnim
->signalsBlocked();
89 propertyAnim
->blockSignals(true);
92 QPointF endPos
= propertyAnim
->endValue().toPointF();
93 if (m_scrollOrientation
== Qt::Vertical
) {
99 propertyAnim
->setDuration(remainingDuration
);
100 propertyAnim
->setStartValue(currentPos
);
101 propertyAnim
->setEndValue(endPos
);
102 propertyAnim
->start();
103 propertyAnim
->blockSignals(block
);
105 widget
->setPos(currentPos
);
111 qreal
KItemListViewAnimation::scrollOffset() const
113 return m_scrollOffset
;
116 void KItemListViewAnimation::start(QGraphicsWidget
* widget
, AnimationType type
, const QVariant
& endValue
)
120 QPropertyAnimation
* propertyAnim
= 0;
121 const int animationDuration
= widget
->style()->styleHint(QStyle::SH_Widget_Animate
) ? 200 : 1;
124 case MovingAnimation
: {
125 const QPointF newPos
= endValue
.toPointF();
126 if (newPos
== widget
->pos()) {
130 propertyAnim
= new QPropertyAnimation(widget
, "pos");
131 propertyAnim
->setDuration(animationDuration
);
132 propertyAnim
->setEndValue(newPos
);
136 case CreateAnimation
: {
137 propertyAnim
= new QPropertyAnimation(widget
, "opacity");
138 propertyAnim
->setEasingCurve(QEasingCurve::InQuart
);
139 propertyAnim
->setDuration(animationDuration
);
140 propertyAnim
->setStartValue(0.0);
141 propertyAnim
->setEndValue(1.0);
145 case DeleteAnimation
: {
146 propertyAnim
= new QPropertyAnimation(widget
, "opacity");
147 propertyAnim
->setEasingCurve(QEasingCurve::OutQuart
);
148 propertyAnim
->setDuration(animationDuration
);
149 propertyAnim
->setStartValue(1.0);
150 propertyAnim
->setEndValue(0.0);
154 case ResizeAnimation
: {
155 const QSizeF newSize
= endValue
.toSizeF();
156 if (newSize
== widget
->size()) {
160 propertyAnim
= new QPropertyAnimation(widget
, "size");
161 propertyAnim
->setDuration(animationDuration
);
162 propertyAnim
->setEndValue(newSize
);
170 Q_ASSERT(propertyAnim
);
171 connect(propertyAnim
, &QPropertyAnimation::finished
, this, &KItemListViewAnimation::slotFinished
);
172 m_animation
[type
].insert(widget
, propertyAnim
);
174 propertyAnim
->start();
177 void KItemListViewAnimation::stop(QGraphicsWidget
* widget
, AnimationType type
)
179 QPropertyAnimation
* propertyAnim
= m_animation
[type
].value(widget
);
181 propertyAnim
->stop();
184 case MovingAnimation
: break;
185 case CreateAnimation
: widget
->setOpacity(1.0); break;
186 case DeleteAnimation
: widget
->setOpacity(0.0); break;
187 case ResizeAnimation
: break;
191 m_animation
[type
].remove(widget
);
194 emit
finished(widget
, type
);
198 void KItemListViewAnimation::stop(QGraphicsWidget
* widget
)
200 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
201 stop(widget
, static_cast<AnimationType
>(type
));
205 bool KItemListViewAnimation::isStarted(QGraphicsWidget
*widget
, AnimationType type
) const
207 return m_animation
[type
].value(widget
);
210 bool KItemListViewAnimation::isStarted(QGraphicsWidget
* widget
) const
212 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
213 if (isStarted(widget
, static_cast<AnimationType
>(type
))) {
220 void KItemListViewAnimation::slotFinished()
222 QPropertyAnimation
* finishedAnim
= qobject_cast
<QPropertyAnimation
*>(sender());
223 for (int type
= 0; type
< AnimationTypeCount
; ++type
) {
224 QMutableHashIterator
<QGraphicsWidget
*, QPropertyAnimation
*> it(m_animation
[type
]);
225 while (it
.hasNext()) {
227 QPropertyAnimation
* propertyAnim
= it
.value();
228 if (propertyAnim
== finishedAnim
) {
229 QGraphicsWidget
* widget
= it
.key();
231 finishedAnim
->deleteLater();
233 emit
finished(widget
, static_cast<AnimationType
>(type
));