File indexing completed on 2024-05-05 04:47:19

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Pino Toscano <pino@kde.org>                                       *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "AnimatedWidget.h"
0018 
0019 #include <qevent.h>
0020 #include <qpainter.h>
0021 
0022 #include <kiconloader.h>
0023 
0024 AnimatedWidget::AnimatedWidget( const QString& iconName, QWidget *parent )
0025     : QWidget( parent ), m_icon( iconName ), m_frames( 0 ), m_currentFrame( 0 ),
0026     m_size( 0 )
0027 {
0028     setAutoFillBackground( false );
0029 
0030     hide();
0031 }
0032 
0033 AnimatedWidget::~AnimatedWidget()
0034 {
0035 }
0036 
0037 void AnimatedWidget::start()
0038 {
0039     if ( m_timer.isActive() )
0040        return;
0041 
0042     if ( !m_frames )
0043     {
0044         load();
0045         if ( !m_frames )
0046             return;
0047     }
0048 
0049     m_timer.start( 1000 / m_frames, this );
0050     show();
0051 }
0052 
0053 void AnimatedWidget::stop()
0054 {
0055     m_timer.stop();
0056     m_currentFrame = 0;
0057     hide();
0058 }
0059 
0060 void AnimatedWidget::paintEvent( QPaintEvent *event )
0061 {
0062     Q_UNUSED( event );
0063 
0064     const int row_size = m_pixmap.width() / m_size;
0065     const int row = m_currentFrame / row_size;
0066     const int column = m_currentFrame % row_size;
0067 
0068     QPainter p( this );
0069     p.fillRect( rect(), Qt::transparent );
0070     p.drawPixmap( 0, 0, m_pixmap, column * m_size, row * m_size, m_size, m_size );
0071 }
0072 
0073 void AnimatedWidget::resizeEvent( QResizeEvent *event )
0074 {
0075     Q_UNUSED( event );
0076 
0077     m_timer.stop();
0078     load();
0079 }
0080 
0081 void AnimatedWidget::timerEvent( QTimerEvent *event )
0082 {
0083     if ( event->timerId() == m_timer.timerId() )
0084     {
0085         m_currentFrame++;
0086         if ( m_currentFrame == m_frames )
0087             m_currentFrame = 0;
0088         update();
0089     }
0090     QWidget::timerEvent( event );
0091 }
0092 
0093 void AnimatedWidget::load()
0094 {
0095     // FIXME implement a better logic for the animation size
0096     m_size = 22;
0097     const QString path = KIconLoader::global()->iconPath( m_icon, -m_size );
0098     QPixmap pix( path );
0099     if ( pix.isNull() )
0100         return;
0101 
0102     if ( ( pix.width() % m_size != 0 ) || ( pix.height() % m_size != 0 ) )
0103         return;
0104 
0105     m_frames = ( pix.height() / m_size ) * ( pix.width() / m_size );
0106     m_pixmap = pix;
0107     m_currentFrame = 0;
0108 }
0109