1/***************************************************************************
2 * Copyright 2010 Stefan Majewsky <majewsky@gmx.net>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ***************************************************************************/
18
19#include "utils-animateditem.h"
20
21#include <QPropertyAnimation>
22
23Utils::AnimatedItem::AnimatedItem(QGraphicsItem* parent)
24 : QGraphicsObject(parent)
25 , m_animationTime(200)
26 , m_hideWhenInvisible(false)
27 , m_animator(new QPropertyAnimation(this, "correct_opacity", this))
28{
29}
30
31int Utils::AnimatedItem::animationTime() const
32{
33 return m_animationTime;
34}
35
36void Utils::AnimatedItem::setAnimationTime(int time)
37{
38 m_animationTime = time;
39}
40
41bool Utils::AnimatedItem::hideWhenInvisible() const
42{
43 return m_hideWhenInvisible;
44}
45
46void Utils::AnimatedItem::setHideWhenInvisible(bool hideWhenInvisible)
47{
48 m_hideWhenInvisible = hideWhenInvisible;
49}
50
51void Utils::AnimatedItem::setOpacityAnimated(qreal targetOpacity)
52{
53 qreal currentOpacity = opacity();
54 m_animator->setDuration(m_animationTime);
55 m_animator->setStartValue(currentOpacity);
56 m_animator->setEndValue(targetOpacity);
57 m_animator->start();
58}
59
60void Utils::AnimatedItem::setOpacity(qreal opacity)
61{
62 QGraphicsItem::setOpacity(opacity);
63 if (m_hideWhenInvisible)
64 QGraphicsItem::setVisible(!qFuzzyIsNull(opacity));
65}
66
67//empty QGraphicsItem reimplementation
68
69QRectF Utils::AnimatedItem::boundingRect() const
70{
71 return QRectF();
72}
73
74void Utils::AnimatedItem::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
75{
76}
77
78#include "utils-animateditem.moc"
79