Commit 1085ee39 authored by François Cartegnie's avatar François 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 )
return r;
}
/* Animated Icon implementation */
AnimatedIcon::AnimatedIcon( QWidget *parent )
: QLabel( parent ), mTimer( this ), mIdleFrame( NULL )
{
mCurrentFrame = mRemainingLoops = 0;
connect( &mTimer, SIGNAL( timeout() ), this, SLOT( onTimerTick() ) );
}
AnimatedIcon::~AnimatedIcon()
PixmapAnimator::PixmapAnimator( QWidget *parent, QList<QString> frames )
: QAbstractAnimation( parent ), current_frame( 0 )
{
// We don't need to destroy the timer, he's our child
delete mIdleFrame;
foreach( QPixmap *frame, mFrames )
delete frame;
}
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() );
foreach( QString name, frames )
pixmaps.append( new QPixmap( name ) );
currentPixmap = pixmaps.at( 0 );
setFps( frames.count() ); /* default to 1 sec loop */
setLoopCount( -1 );
}
// private slot
void AnimatedIcon::onTimerTick()
void PixmapAnimator::updateCurrentTime( int msecs )
{
//assert( !mFrames.isEmpty() );
if( ++mCurrentFrame > mFrames.count() )
int i = msecs / interval;
if ( i >= pixmaps.count() ) i = pixmaps.count() - 1; /* roundings */
if ( i != current_frame )
{
if( mRemainingLoops != -1 )
{
if( --mRemainingLoops == 0 )
{
mTimer.stop();
setPixmap( mIdleFrame ? *mIdleFrame : *mFrames.last() );
return;
}
}
mCurrentFrame = 1;
current_frame = i;
currentPixmap = pixmaps.at( current_frame );
emit pixmapReady( *currentPixmap );
}
//assert( mCurrentFrame >= 1 && mCurrentFrame <= mFrames.count() );
setPixmap( *mFrames.at( mCurrentFrame - 1 ) );
}
/* SpinningIcon implementation */
SpinningIcon::SpinningIcon( QWidget *parent, bool noIdleFrame )
: AnimatedIcon( parent )
/* Animated Icon implementation */
SpinningIcon::SpinningIcon( QWidget *parent ) : QLabel( parent )
{
if( noIdleFrame )
addFrame( QPixmap(), 0 );
else
addFrame( QPixmap( ":/util/wait0" ), 0 );
addFrame( QPixmap( ":/util/wait1" ) );
addFrame( QPixmap( ":/util/wait2" ) );
addFrame( QPixmap( ":/util/wait3" ) );
addFrame( QPixmap( ":/util/wait4" ) );
QList<QString> frames;
frames << ":/util/wait1";
frames << ":/util/wait2";
frames << ":/util/wait3";
frames << ":/util/wait4";
animator = new PixmapAnimator( this, frames );
CONNECT( animator, pixmapReady( const QPixmap & ), this, setPixmap( const QPixmap & ) );
CONNECT( animator, pixmapReady( const QPixmap & ), this, repaint() );
setScaledContents( true );
setFixedSize( 16, 16 );
animator->setCurrentTime( 0 );
}
QToolButtonExt::QToolButtonExt(QWidget *parent, int ms )
......
......@@ -35,6 +35,7 @@
#include <QList>
#include <QTimer>
#include <QToolButton>
#include <QAbstractAnimation>
class QPixmap;
......@@ -99,75 +100,53 @@ protected:
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
* pixmaps. The pixmaps will be stored in memory and should be kept small.
* First, create the widget, add frames and then start playing. Looping
* 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
public:
/** Create an empty AnimatedIcon */
AnimatedIcon( QWidget *parent );
virtual ~AnimatedIcon();
/** Adds a frame to play in the loop.
* @param pixmap The QPixmap to display. Data will be copied internally.
* @param index If -1, append the frame. If 0, replace the idle frame.
* Otherwise, insert the frame at the given position.
**/
void addFrame( const QPixmap &pixmap, int index = -1 );
/** Play the animation (or restart it)
* @param loops Number of times to play the loop. 0 means stop, while -1
* means play forever. When stopped, the frame #0 will be
* displayed until play() is called again.
* @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();
PixmapAnimator( QWidget *parent, QList<QString> _frames );
void setFps( int _fps ) { fps = _fps; interval = 1000.0 / fps; };
virtual int duration() const { return interval * pixmaps.count(); };
virtual ~PixmapAnimator() { while( pixmaps.count() ) pixmaps.erase( pixmaps.end() ); };
QPixmap *getPixmap() { return currentPixmap; }
protected:
virtual void updateCurrentTime ( int msecs );
QList<QPixmap *> pixmaps;
QPixmap *currentPixmap;
int fps;
int interval;
int lastframe_msecs;
int current_frame;
signals:
void pixmapReady( const QPixmap & );
};
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
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 */
......
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