Commit aea9ebb9 authored by Francois Cartegnie's avatar Francois Cartegnie

Qt: add DelegateAnimationHelper.

Delegates can't request updates themselves.
Repaint needs to be triggered at model or view level.
parent 0f1dbde6
...@@ -19,18 +19,36 @@ ...@@ -19,18 +19,36 @@
*****************************************************************************/ *****************************************************************************/
#include "animators.hpp" #include "animators.hpp"
#include "qt4.hpp"
#include <QWidget> #include <QWidget>
#include <QPixmap> #include <QPixmap>
#include <QAbstractItemView>
PixmapAnimator::PixmapAnimator( QWidget *parent, QList<QString> frames ) BasicAnimator::BasicAnimator( QObject *parent )
: QAbstractAnimation( parent ), current_frame( 0 ) : QAbstractAnimation( parent ), current_frame( 0 )
{
setLoopCount( -1 );
}
void BasicAnimator::updateCurrentTime( int msecs )
{
msecs += (interval / 2);
int i = ( msecs / interval );
if ( i != current_frame )
{
current_frame = i;
emit frameChanged();
}
}
PixmapAnimator::PixmapAnimator( QWidget *parent, QList<QString> frames )
: BasicAnimator( parent )
{ {
foreach( QString name, frames ) foreach( QString name, frames )
pixmaps.append( new QPixmap( name ) ); pixmaps.append( new QPixmap( name ) );
currentPixmap = pixmaps.at( 0 ); currentPixmap = pixmaps.at( 0 );
setFps( frames.count() ); /* default to 1 sec loop */ setFps( frames.count() ); /* default to 1 sec loop */
setLoopCount( -1 );
} }
void PixmapAnimator::updateCurrentTime( int msecs ) void PixmapAnimator::updateCurrentTime( int msecs )
...@@ -45,3 +63,55 @@ void PixmapAnimator::updateCurrentTime( int msecs ) ...@@ -45,3 +63,55 @@ void PixmapAnimator::updateCurrentTime( int msecs )
} }
} }
DelegateAnimationHelper::DelegateAnimationHelper( QAbstractItemView *view_,
BasicAnimator *animator_ )
: QObject( view_ ), view( view_ ), animator( animator_ )
{
if ( !animator )
{
animator = new BasicAnimator( this );
animator->setFps( 15 );
animator->setLoopCount( -1 );
}
setIndex( QModelIndex() );
CONNECT( animator, frameChanged(), this, updateDelegate() );
}
void DelegateAnimationHelper::setIndex( const QModelIndex &index_ )
{
index = QPersistentModelIndex( index_ );
}
void DelegateAnimationHelper::run( bool b_run )
{
if ( b_run )
{
if ( ! isRunning() ) animator->start();
}
else
animator->stop();
}
bool DelegateAnimationHelper::isRunning() const
{
return ( animator->state() == QAbstractAnimation::Running );
}
const QPersistentModelIndex & DelegateAnimationHelper::getIndex() const
{
return index;
}
void DelegateAnimationHelper::updateDelegate()
{
/* Prevent running indefinitively if removed from model */
if ( !index.isValid() )
run( false );
else
{
if ( view->viewport() )
view->viewport()->update();
else
view->update( index );
}
}
...@@ -25,8 +25,31 @@ ...@@ -25,8 +25,31 @@
#include <QList> #include <QList>
#include <QString> #include <QString>
#include <QAbstractAnimation> #include <QAbstractAnimation>
#include <QPersistentModelIndex>
class QWidget; class QWidget;
class QPixmap; class QPixmap;
class QAbstractItemView;
class BasicAnimator : public QAbstractAnimation
{
Q_OBJECT
public:
BasicAnimator( QObject *parent = 0 );
void setFps( int _fps ) { fps = _fps; interval = 1000.0 / fps; }
virtual int duration() const { return 1000; }
signals:
void frameChanged();
protected:
virtual void updateCurrentTime ( int msecs );
int fps;
int interval;
int lastframe_msecs;
int current_frame;
};
/** 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
...@@ -34,13 +57,12 @@ class QPixmap; ...@@ -34,13 +57,12 @@ class QPixmap;
* 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.
**/ **/
class PixmapAnimator : public QAbstractAnimation class PixmapAnimator : public BasicAnimator
{ {
Q_OBJECT Q_OBJECT
public: public:
PixmapAnimator( QWidget *parent, QList<QString> _frames ); 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 int duration() const { return interval * pixmaps.count(); }
virtual ~PixmapAnimator() { qDeleteAll( pixmaps ); } virtual ~PixmapAnimator() { qDeleteAll( pixmaps ); }
QPixmap *getPixmap() { return currentPixmap; } QPixmap *getPixmap() { return currentPixmap; }
...@@ -48,12 +70,30 @@ protected: ...@@ -48,12 +70,30 @@ protected:
virtual void updateCurrentTime ( int msecs ); virtual void updateCurrentTime ( int msecs );
QList<QPixmap *> pixmaps; QList<QPixmap *> pixmaps;
QPixmap *currentPixmap; QPixmap *currentPixmap;
int fps;
int interval;
int lastframe_msecs;
int current_frame;
signals: signals:
void pixmapReady( const QPixmap & ); void pixmapReady( const QPixmap & );
}; };
class DelegateAnimationHelper : public QObject
{
Q_OBJECT
public:
DelegateAnimationHelper( QAbstractItemView *view, BasicAnimator *animator = 0 );
void setIndex( const QModelIndex &index );
bool isRunning() const;
const QPersistentModelIndex & getIndex() const;
public slots:
void run( bool );
protected slots:
void updateDelegate();
private:
BasicAnimator *animator;
QAbstractItemView *view;
QPersistentModelIndex index;
};
#endif // ANIMATORS_HPP #endif // ANIMATORS_HPP
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