PageRenderTime 72ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 20ms app.codeStats 0ms

/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
 19#include "LoadingSpinner.h"
 20
 21#include <QTimeLine>
 22#include <QPaintEvent>
 23#include <QPainter>
 24#include <QMovie>
 25#include <QLabel>
 26
 27#include "utils/tomahawkutils.h"
 28#include "utils/logger.h"
 29
 30#define ANIM_LENGTH 300
 31
 32
 33LoadingSpinner::LoadingSpinner( QWidget* parent )
 34    : QWidget( parent )
 35    , m_showHide( new QTimeLine )
 36{
 37    m_showHide->setDuration( 300 );
 38    m_showHide->setStartFrame( 0 );
 39    m_showHide->setEndFrame( 100 );
 40    m_showHide->setUpdateInterval( 20 );
 41    connect( m_showHide, SIGNAL( frameChanged( int ) ), this, SLOT( update() ) );
 42    connect( m_showHide, SIGNAL( finished() ), this, SLOT( hideFinished() ) );
 43
 44    m_anim = new QMovie( RESPATH "/images/loading-animation.gif" );
 45    m_anim->jumpToNextFrame();
 46
 47    connect( m_anim, SIGNAL( frameChanged( int ) ), this, SLOT( update() ) );
 48
 49    resize( m_anim->currentPixmap().size() );
 50    setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
 51    hide();
 52}
 53
 54
 55LoadingSpinner::~LoadingSpinner()
 56{
 57}
 58
 59
 60void
 61LoadingSpinner::fadeIn()
 62{
 63    if ( isVisible() )
 64        return;
 65
 66    show();
 67
 68    m_anim->start();
 69    m_showHide->setDirection( QTimeLine::Forward );
 70
 71    if ( m_showHide->state() != QTimeLine::Running )
 72        m_showHide->start();
 73}
 74
 75
 76void
 77LoadingSpinner::fadeOut()
 78{
 79    m_showHide->setDirection( QTimeLine::Backward );
 80
 81    if ( m_showHide->state() != QTimeLine::Running )
 82        m_showHide->start();
 83}
 84
 85
 86void
 87LoadingSpinner::hideFinished()
 88{
 89    if ( m_showHide->direction() == QTimeLine::Backward )
 90    {
 91        hide();
 92        m_anim->stop();
 93    }
 94}
 95
 96
 97QSize
 98LoadingSpinner::sizeHint() const
 99{
100    return m_anim->currentPixmap().size();
101}
102
103
104void
105LoadingSpinner::paintEvent( QPaintEvent* ev )
106{
107    Q_UNUSED( ev );
108
109    if ( !parentWidget() )
110        return;
111
112    QPoint center( ( parentWidget()->width() / 2 ) - ( width() / 2 ), ( parentWidget()->height() / 2 ) - ( height() / 2 ) );
113    if ( center != pos() )
114    {
115        move( center );
116        return;
117    }
118
119    QPainter p( this );
120
121    if ( m_showHide->state() == QTimeLine::Running )
122    { // showing or hiding
123        p.setOpacity( (qreal)m_showHide->currentValue() );
124    }
125
126    p.drawPixmap( rect(), m_anim->currentPixmap() );
127}