/src/sourcetree/animationhelper.cpp

http://github.com/tomahawk-player/tomahawk · 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. #include "AnimationHelper.h"
  20. #include <QDebug>
  21. AnimationHelper::AnimationHelper( const QModelIndex& index, QObject *parent )
  22. :QObject( parent )
  23. , m_index( index )
  24. , m_fullyExpanded( false )
  25. , m_expandAnimation( 0 )
  26. {
  27. m_expandTimer.setSingleShot( true );
  28. m_expandTimer.setInterval( 600 );
  29. connect( &m_expandTimer, SIGNAL( timeout() ), SLOT(expandTimeout() ) );
  30. m_collapseTimer.setSingleShot( true );
  31. m_collapseTimer.setInterval( 600 );
  32. connect( &m_collapseTimer, SIGNAL( timeout() ), SLOT(collapseTimeout() ) );
  33. }
  34. bool
  35. AnimationHelper::initialized() const
  36. {
  37. return m_expandAnimation != 0;
  38. }
  39. void
  40. AnimationHelper::initialize( const QSize& startValue, const QSize& endValue, int duration )
  41. {
  42. m_size = startValue;
  43. m_targetSize = endValue;
  44. m_startSize = startValue;
  45. m_expandAnimation = new QPropertyAnimation( this, "size", this );
  46. m_expandAnimation->setStartValue( startValue );
  47. m_expandAnimation->setEndValue( endValue );
  48. m_expandAnimation->setDuration( duration );
  49. m_expandAnimation->setEasingCurve( QEasingCurve::OutExpo );
  50. qDebug() << "starting animation" << startValue << endValue << duration;
  51. connect( m_expandAnimation, SIGNAL( finished() ), SLOT( expandAnimationFinished() ) );
  52. m_collapseAnimation= new QPropertyAnimation( this, "size", this );
  53. m_collapseAnimation->setStartValue( endValue );
  54. m_collapseAnimation->setEndValue( startValue );
  55. m_collapseAnimation->setDuration( duration );
  56. m_collapseAnimation->setEasingCurve( QEasingCurve::InExpo );
  57. connect( m_collapseAnimation, SIGNAL( finished() ), SLOT( collapseAnimationFinished() ) );
  58. }
  59. void
  60. AnimationHelper::setSize( const QSize& size )
  61. {
  62. m_size = size;
  63. emit sizeChanged();
  64. //qDebug() << "animaton setting size to" << size;
  65. }
  66. void
  67. AnimationHelper::expand()
  68. {
  69. m_collapseTimer.stop();
  70. m_expandTimer.start();
  71. }
  72. void
  73. AnimationHelper::collapse( bool immediately )
  74. {
  75. if ( m_expandTimer.isActive() )
  76. {
  77. m_expandTimer.stop();
  78. emit finished( m_index );
  79. return;
  80. }
  81. if ( immediately )
  82. {
  83. m_fullyExpanded = false;
  84. m_collapseAnimation->start();
  85. }
  86. else
  87. m_collapseTimer.start();
  88. }
  89. bool
  90. AnimationHelper::partlyExpanded()
  91. {
  92. return m_size != m_startSize;
  93. // return m_fullyExpanded
  94. // || ( m_expandAnimation->state() == QPropertyAnimation::Running && m_expandAnimation->currentTime() > 250 )
  95. // || ( m_collapseAnimation->state() == QPropertyAnimation::Running && m_collapseAnimation->currentTime() < 100 );
  96. }
  97. bool
  98. AnimationHelper::fullyExpanded()
  99. {
  100. return m_fullyExpanded;
  101. }
  102. void
  103. AnimationHelper::expandTimeout()
  104. {
  105. m_expandAnimation->start();
  106. }
  107. void
  108. AnimationHelper::collapseTimeout()
  109. {
  110. m_fullyExpanded = false;
  111. m_collapseAnimation->start();
  112. }
  113. void
  114. AnimationHelper::expandAnimationFinished()
  115. {
  116. m_fullyExpanded = true;
  117. }
  118. void
  119. AnimationHelper::collapseAnimationFinished()
  120. {
  121. emit finished( m_index );
  122. }
  123. QSize
  124. AnimationHelper::originalSize() const
  125. {
  126. return m_startSize;
  127. }
  128. QSize
  129. AnimationHelper::size() const
  130. {
  131. return m_size;
  132. }