/src/sourcetree/animationhelper.cpp
C++ | 166 lines | 110 code | 34 blank | 22 comment | 4 complexity | f8f9c9dc4ce458958c7f71a4c1df1e12 MD5 | raw file
1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === 2 * 3 * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org> 4 * Copyright 2011, Michael Zanetti <mzanetti@kde.org> 5 * 6 * Tomahawk is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * Tomahawk is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20#include "AnimationHelper.h" 21 22#include <QDebug> 23 24AnimationHelper::AnimationHelper( const QModelIndex& index, QObject *parent ) 25 :QObject( parent ) 26 , m_index( index ) 27 , m_fullyExpanded( false ) 28 , m_expandAnimation( 0 ) 29{ 30 m_expandTimer.setSingleShot( true ); 31 m_expandTimer.setInterval( 600 ); 32 connect( &m_expandTimer, SIGNAL( timeout() ), SLOT(expandTimeout() ) ); 33 34 m_collapseTimer.setSingleShot( true ); 35 m_collapseTimer.setInterval( 600 ); 36 connect( &m_collapseTimer, SIGNAL( timeout() ), SLOT(collapseTimeout() ) ); 37} 38 39 40bool 41AnimationHelper::initialized() const 42{ 43 return m_expandAnimation != 0; 44} 45 46 47void 48AnimationHelper::initialize( const QSize& startValue, const QSize& endValue, int duration ) 49{ 50 m_size = startValue; 51 m_targetSize = endValue; 52 m_startSize = startValue; 53 54 m_expandAnimation = new QPropertyAnimation( this, "size", this ); 55 m_expandAnimation->setStartValue( startValue ); 56 m_expandAnimation->setEndValue( endValue ); 57 m_expandAnimation->setDuration( duration ); 58 m_expandAnimation->setEasingCurve( QEasingCurve::OutExpo ); 59 qDebug() << "starting animation" << startValue << endValue << duration; 60 connect( m_expandAnimation, SIGNAL( finished() ), SLOT( expandAnimationFinished() ) ); 61 62 m_collapseAnimation= new QPropertyAnimation( this, "size", this ); 63 m_collapseAnimation->setStartValue( endValue ); 64 m_collapseAnimation->setEndValue( startValue ); 65 m_collapseAnimation->setDuration( duration ); 66 m_collapseAnimation->setEasingCurve( QEasingCurve::InExpo ); 67 connect( m_collapseAnimation, SIGNAL( finished() ), SLOT( collapseAnimationFinished() ) ); 68 69} 70 71 72void 73AnimationHelper::setSize( const QSize& size ) 74{ 75 m_size = size; 76 emit sizeChanged(); 77 //qDebug() << "animaton setting size to" << size; 78} 79 80 81void 82AnimationHelper::expand() 83{ 84 m_collapseTimer.stop(); 85 m_expandTimer.start(); 86} 87 88 89void 90AnimationHelper::collapse( bool immediately ) 91{ 92 if ( m_expandTimer.isActive() ) 93 { 94 m_expandTimer.stop(); 95 emit finished( m_index ); 96 return; 97 } 98 99 if ( immediately ) 100 { 101 m_fullyExpanded = false; 102 m_collapseAnimation->start(); 103 } 104 else 105 m_collapseTimer.start(); 106} 107 108 109bool 110AnimationHelper::partlyExpanded() 111{ 112 return m_size != m_startSize; 113// return m_fullyExpanded 114// || ( m_expandAnimation->state() == QPropertyAnimation::Running && m_expandAnimation->currentTime() > 250 ) 115// || ( m_collapseAnimation->state() == QPropertyAnimation::Running && m_collapseAnimation->currentTime() < 100 ); 116} 117 118 119bool 120AnimationHelper::fullyExpanded() 121{ 122 return m_fullyExpanded; 123} 124 125 126void 127AnimationHelper::expandTimeout() 128{ 129 m_expandAnimation->start(); 130} 131 132 133void 134AnimationHelper::collapseTimeout() 135{ 136 m_fullyExpanded = false; 137 m_collapseAnimation->start(); 138} 139 140 141void 142AnimationHelper::expandAnimationFinished() 143{ 144 m_fullyExpanded = true; 145} 146 147 148void 149AnimationHelper::collapseAnimationFinished() 150{ 151 emit finished( m_index ); 152} 153 154 155QSize 156AnimationHelper::originalSize() const 157{ 158 return m_startSize; 159} 160 161 162QSize 163AnimationHelper::size() const 164{ 165 return m_size; 166}