File indexing completed on 2024-05-12 04:04:35

0001 /***************************************************************************
0002  *   Copyright 2010 Stefan Majewsky <majewsky@gmx.net>
0003  *
0004  *   This program is free software; you can redistribute it and/or
0005  *   modify it under the terms of the GNU General Public
0006  *   License as published by the Free Software Foundation; either
0007  *   version 2 of the License, or (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the Free Software
0016  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017  ***************************************************************************/
0018 
0019 #include "utils-animateditem.h"
0020 
0021 #include <QPropertyAnimation>
0022 
0023 Utils::AnimatedItem::AnimatedItem(QGraphicsItem* parent)
0024     : QGraphicsObject(parent)
0025     , m_animationTime(200)
0026     , m_hideWhenInvisible(false)
0027     , m_animator(new QPropertyAnimation(this, "correct_opacity", this))
0028 {
0029 }
0030 
0031 int Utils::AnimatedItem::animationTime() const
0032 {
0033     return m_animationTime;
0034 }
0035 
0036 void Utils::AnimatedItem::setAnimationTime(int time)
0037 {
0038     m_animationTime = time;
0039 }
0040 
0041 bool Utils::AnimatedItem::hideWhenInvisible() const
0042 {
0043     return m_hideWhenInvisible;
0044 }
0045 
0046 void Utils::AnimatedItem::setHideWhenInvisible(bool hideWhenInvisible)
0047 {
0048     m_hideWhenInvisible = hideWhenInvisible;
0049 }
0050 
0051 void Utils::AnimatedItem::setOpacityAnimated(qreal targetOpacity)
0052 {
0053     qreal currentOpacity = opacity();
0054     m_animator->setDuration(m_animationTime);
0055     m_animator->setStartValue(currentOpacity);
0056     m_animator->setEndValue(targetOpacity);
0057     m_animator->start();
0058 }
0059 
0060 void Utils::AnimatedItem::setOpacity(qreal opacity)
0061 {
0062     QGraphicsItem::setOpacity(opacity);
0063     if (m_hideWhenInvisible)
0064         QGraphicsItem::setVisible(!qFuzzyIsNull(opacity));
0065 }
0066 
0067 //empty QGraphicsItem reimplementation
0068 
0069 QRectF Utils::AnimatedItem::boundingRect() const
0070 {
0071     return QRectF();
0072 }
0073 
0074 void Utils::AnimatedItem::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
0075 {
0076 }
0077 
0078 #include "moc_utils-animateditem.cpp"