Commit 1085ee39 authored by Francois Cartegnie's avatar Francois Cartegnie

Qt: PixmapAnimator: replace AnimatedIcon.

As Qt >= 4.6.0 provides AbstractAnimation we no longer need QTimer
based animators.
PixmapAnimator allows to be used by Q*Painter.
Unused still frame support is removed, but can easily be implemented.
parent 95da9d5a
...@@ -298,108 +298,42 @@ QString VLCKeyToString( unsigned val ) ...@@ -298,108 +298,42 @@ QString VLCKeyToString( unsigned val )
return r; return r;
} }
PixmapAnimator::PixmapAnimator( QWidget *parent, QList<QString> frames )
/* Animated Icon implementation */ : QAbstractAnimation( parent ), current_frame( 0 )
AnimatedIcon::AnimatedIcon( QWidget *parent )
: QLabel( parent ), mTimer( this ), mIdleFrame( NULL )
{
mCurrentFrame = mRemainingLoops = 0;
connect( &mTimer, SIGNAL( timeout() ), this, SLOT( onTimerTick() ) );
}
AnimatedIcon::~AnimatedIcon()
{ {
// We don't need to destroy the timer, he's our child foreach( QString name, frames )
delete mIdleFrame; pixmaps.append( new QPixmap( name ) );
foreach( QPixmap *frame, mFrames ) currentPixmap = pixmaps.at( 0 );
delete frame; setFps( frames.count() ); /* default to 1 sec loop */
} setLoopCount( -1 );
void AnimatedIcon::addFrame( const QPixmap &pxm, int index )
{
if( index == 0 )
{
// Replace idle frame
delete mIdleFrame;
mIdleFrame = new QPixmap( pxm );
setPixmap( *mIdleFrame );
return;
}
QPixmap *copy = new QPixmap( pxm );
mFrames.insert( ( index < 0 || index > mFrames.count() ) ? mFrames.count() :
index, copy );
if( !pixmap() )
setPixmap( *copy );
}
void AnimatedIcon::play( int loops, int interval )
{
if( interval < 20 )
{
interval = 20;
}
if( !mIdleFrame && (mFrames.isEmpty() || loops != 0 ) )
{
return;
}
if( loops == 0 )
{
// Stop playback
mCurrentFrame = mRemainingLoops = 0;
mTimer.stop();
setPixmap( mIdleFrame != NULL ? *mIdleFrame : *mFrames.last() );
return;
}
if( loops <= -1 )
loops = -1;
mCurrentFrame = 1;
mRemainingLoops = loops;
mTimer.start( interval );
setPixmap( *mFrames.first() );
} }
// private slot void PixmapAnimator::updateCurrentTime( int msecs )
void AnimatedIcon::onTimerTick()
{ {
//assert( !mFrames.isEmpty() ); int i = msecs / interval;
if( ++mCurrentFrame > mFrames.count() ) if ( i >= pixmaps.count() ) i = pixmaps.count() - 1; /* roundings */
if ( i != current_frame )
{ {
if( mRemainingLoops != -1 ) current_frame = i;
{ currentPixmap = pixmaps.at( current_frame );
if( --mRemainingLoops == 0 ) emit pixmapReady( *currentPixmap );
{
mTimer.stop();
setPixmap( mIdleFrame ? *mIdleFrame : *mFrames.last() );
return;
}
}
mCurrentFrame = 1;
} }
//assert( mCurrentFrame >= 1 && mCurrentFrame <= mFrames.count() );
setPixmap( *mFrames.at( mCurrentFrame - 1 ) );
} }
/* Animated Icon implementation */
/* SpinningIcon implementation */ SpinningIcon::SpinningIcon( QWidget *parent ) : QLabel( parent )
SpinningIcon::SpinningIcon( QWidget *parent, bool noIdleFrame )
: AnimatedIcon( parent )
{ {
if( noIdleFrame ) QList<QString> frames;
addFrame( QPixmap(), 0 ); frames << ":/util/wait1";
else frames << ":/util/wait2";
addFrame( QPixmap( ":/util/wait0" ), 0 ); frames << ":/util/wait3";
addFrame( QPixmap( ":/util/wait1" ) ); frames << ":/util/wait4";
addFrame( QPixmap( ":/util/wait2" ) ); animator = new PixmapAnimator( this, frames );
addFrame( QPixmap( ":/util/wait3" ) ); CONNECT( animator, pixmapReady( const QPixmap & ), this, setPixmap( const QPixmap & ) );
addFrame( QPixmap( ":/util/wait4" ) ); CONNECT( animator, pixmapReady( const QPixmap & ), this, repaint() );
setScaledContents( true ); setScaledContents( true );
setFixedSize( 16, 16 ); setFixedSize( 16, 16 );
animator->setCurrentTime( 0 );
} }
QToolButtonExt::QToolButtonExt(QWidget *parent, int ms ) QToolButtonExt::QToolButtonExt(QWidget *parent, int ms )
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <QList> #include <QList>
#include <QTimer> #include <QTimer>
#include <QToolButton> #include <QToolButton>
#include <QAbstractAnimation>
class QPixmap; class QPixmap;
...@@ -99,75 +100,53 @@ protected: ...@@ -99,75 +100,53 @@ protected:
virtual int valueFromText( const QString& ) const { return -1; } virtual int valueFromText( const QString& ) const { return -1; }
}; };
class AnimatedIcon : public QLabel /** An animated pixmap
{
/** An animated pixmap
* Use this widget to display an animated icon based on a series of * Use this widget to display an animated icon based on a series of
* pixmaps. The pixmaps will be stored in memory and should be kept small. * pixmaps. The pixmaps will be stored in memory and should be kept small.
* First, create the widget, add frames and then start playing. Looping * First, create the widget, add frames and then start playing. Looping
* is supported. * is supported.
* Frames #1 to #n are displayed at regular intervals when playing.
* Frame #0 is the idle frame, displayed when the icon is not animated.
* If not #0 frame has been specified, the last frame will be shown when
* idle.
**/ **/
class PixmapAnimator : public QAbstractAnimation
{
Q_OBJECT Q_OBJECT
public: public:
/** Create an empty AnimatedIcon */ PixmapAnimator( QWidget *parent, QList<QString> _frames );
AnimatedIcon( QWidget *parent ); void setFps( int _fps ) { fps = _fps; interval = 1000.0 / fps; };
virtual ~AnimatedIcon(); virtual int duration() const { return interval * pixmaps.count(); };
virtual ~PixmapAnimator() { while( pixmaps.count() ) pixmaps.erase( pixmaps.end() ); };
/** Adds a frame to play in the loop. QPixmap *getPixmap() { return currentPixmap; }
* @param pixmap The QPixmap to display. Data will be copied internally. protected:
* @param index If -1, append the frame. If 0, replace the idle frame. virtual void updateCurrentTime ( int msecs );
* Otherwise, insert the frame at the given position. QList<QPixmap *> pixmaps;
**/ QPixmap *currentPixmap;
void addFrame( const QPixmap &pixmap, int index = -1 ); int fps;
int interval;
/** Play the animation (or restart it) int lastframe_msecs;
* @param loops Number of times to play the loop. 0 means stop, while -1 int current_frame;
* means play forever. When stopped, the frame #0 will be signals:
* displayed until play() is called again. void pixmapReady( const QPixmap & );
* @param interval Delay between frames, in milliseconds (minimum 20ms)
* @note If isPlaying() is true, then restart the animation from frame #1
**/
void play( int loops = 1, int interval = 200 );
/** Stop playback. Same as play(0). */
inline void stop()
{
play( 0 );
}
/** Is the animation currently running? */
inline bool isPlaying()
{
return mTimer.isActive();
}
private:
QTimer mTimer;
QPixmap *mIdleFrame;
QList<QPixmap*> mFrames; // Keeps deep copies of all the frames
int mCurrentFrame, mRemainingLoops;
private slots:
/** Slot connected to the timeout() signal of our internal timer */
void onTimerTick();
}; };
class SpinningIcon : public AnimatedIcon /** This spinning icon, to the colors of the VLC cone, will show
* that there is some background activity running
**/
class SpinningIcon : public QLabel
{ {
/** This spinning icon, to the colors of the VLC cone, will show
* that there is some background activity running
**/
Q_OBJECT Q_OBJECT
public: public:
SpinningIcon( QWidget *parent, bool noIdleFrame = false ); SpinningIcon( QWidget *parent );
void play( int loops = -1, int fps = 0 )
{
animator->setLoopCount( loops );
if ( fps ) animator->setFps( fps );
animator->start();
}
void stop() { animator->stop(); }
bool isPlaying() { return animator->state() == PixmapAnimator::Running; }
private:
PixmapAnimator *animator;
}; };
/* VLC Key/Wheel hotkeys interactions */ /* VLC Key/Wheel hotkeys interactions */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment