Commit 001d8f3d authored by Jean-Philippe André's avatar Jean-Philippe André

Qt: add new widgets: AnimatedIcon & SpinningIcon

Very simple, displays a series of pixmaps in a loop animation.
The spinning icon can be used to indicate background activity,
especially designed for Lua SDs and Lua extensions.
parent cd4cd718
......@@ -35,6 +35,7 @@
#include <QRect>
#include <QKeyEvent>
#include <QWheelEvent>
#include <QPixmap>
#include <vlc_keys.h>
QVLCFramelessButton::QVLCFramelessButton( QWidget *parent )
......@@ -308,3 +309,117 @@ QString VLCKeyToString( int val )
return r;
}
/* Animated Icon implementation */
AnimatedIcon::AnimatedIcon( QWidget *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
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.size() ) ? mFrames.size() :
index, copy );
if( !pixmap() )
setPixmap( *copy );
}
void AnimatedIcon::play( int loops, int interval )
{
if( interval < 20 )
{
#ifndef NDEBUG
printf( "AnimatedIcon::play(): interval is too short (%d ms)",
interval );
#endif
interval = 20;
}
if( !mIdleFrame && ( mFrames.empty() | loops != 0 ) )
{
#ifndef NDEBUG
printf( "AnimatedIcon::play(): no frames to display" );
#endif
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 AnimatedIcon::onTimerTick()
{
//assert( !mFrames.empty() );
if( ++mCurrentFrame > mFrames.size() )
{
if( mRemainingLoops != -1 )
{
if( --mRemainingLoops == 0 )
{
mTimer.stop();
setPixmap( mIdleFrame ? *mIdleFrame : *mFrames.last() );
return;
}
}
mCurrentFrame = 1;
}
//assert( mCurrentFrame >= 1 && mCurrentFrame <= mFrames.size() );
setPixmap( *mFrames.at( mCurrentFrame - 1 ) );
}
/* SpinningIcon implementation */
SpinningIcon::SpinningIcon( QWidget *parent, bool noIdleFrame )
: AnimatedIcon( 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" ) );
setScaledContents( true );
setFixedSize( 16, 16 );
}
SpinningIcon::~SpinningIcon()
{
}
......@@ -32,6 +32,9 @@
#include <QLabel>
#include <QStackedWidget>
#include <QSpinBox>
#include <QList>
#include <QTimer>
class QPixmap;
class QVLCFramelessButton : public QPushButton
{
......@@ -79,6 +82,78 @@ protected:
virtual int valueFromText( const QString& ) const { return -1; }
};
class AnimatedIcon : public QLabel
{
/** 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.
**/
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();
};
class SpinningIcon : public AnimatedIcon
{
/** 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 );
virtual ~SpinningIcon();
};
/* VLC Key/Wheel hotkeys interactions */
class QKeyEvent;
......
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