/src/libtomahawk/playlist/dynamic/widgets/LoadingSpinner.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 127 lines · 80 code · 30 blank · 17 comment · 12 complexity · dbce71f98bab59ff6f84e0d32e344d89 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010 Leo Franchi <lfranchi@kde.org>
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "LoadingSpinner.h"
  19. #include <QTimeLine>
  20. #include <QPaintEvent>
  21. #include <QPainter>
  22. #include <QMovie>
  23. #include <QLabel>
  24. #include "utils/tomahawkutils.h"
  25. #include "utils/logger.h"
  26. #define ANIM_LENGTH 300
  27. LoadingSpinner::LoadingSpinner( QWidget* parent )
  28. : QWidget( parent )
  29. , m_showHide( new QTimeLine )
  30. {
  31. m_showHide->setDuration( 300 );
  32. m_showHide->setStartFrame( 0 );
  33. m_showHide->setEndFrame( 100 );
  34. m_showHide->setUpdateInterval( 20 );
  35. connect( m_showHide, SIGNAL( frameChanged( int ) ), this, SLOT( update() ) );
  36. connect( m_showHide, SIGNAL( finished() ), this, SLOT( hideFinished() ) );
  37. m_anim = new QMovie( RESPATH "/images/loading-animation.gif" );
  38. m_anim->jumpToNextFrame();
  39. connect( m_anim, SIGNAL( frameChanged( int ) ), this, SLOT( update() ) );
  40. resize( m_anim->currentPixmap().size() );
  41. setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
  42. hide();
  43. }
  44. LoadingSpinner::~LoadingSpinner()
  45. {
  46. }
  47. void
  48. LoadingSpinner::fadeIn()
  49. {
  50. if ( isVisible() )
  51. return;
  52. show();
  53. m_anim->start();
  54. m_showHide->setDirection( QTimeLine::Forward );
  55. if ( m_showHide->state() != QTimeLine::Running )
  56. m_showHide->start();
  57. }
  58. void
  59. LoadingSpinner::fadeOut()
  60. {
  61. m_showHide->setDirection( QTimeLine::Backward );
  62. if ( m_showHide->state() != QTimeLine::Running )
  63. m_showHide->start();
  64. }
  65. void
  66. LoadingSpinner::hideFinished()
  67. {
  68. if ( m_showHide->direction() == QTimeLine::Backward )
  69. {
  70. hide();
  71. m_anim->stop();
  72. }
  73. }
  74. QSize
  75. LoadingSpinner::sizeHint() const
  76. {
  77. return m_anim->currentPixmap().size();
  78. }
  79. void
  80. LoadingSpinner::paintEvent( QPaintEvent* ev )
  81. {
  82. Q_UNUSED( ev );
  83. if ( !parentWidget() )
  84. return;
  85. QPoint center( ( parentWidget()->width() / 2 ) - ( width() / 2 ), ( parentWidget()->height() / 2 ) - ( height() / 2 ) );
  86. if ( center != pos() )
  87. {
  88. move( center );
  89. return;
  90. }
  91. QPainter p( this );
  92. if ( m_showHide->state() == QTimeLine::Running )
  93. { // showing or hiding
  94. p.setOpacity( (qreal)m_showHide->currentValue() );
  95. }
  96. p.drawPixmap( rect(), m_anim->currentPixmap() );
  97. }