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 @@
*****************************************************************************/
#include "animators.hpp"
#include "qt4.hpp"
#include <QWidget>
#include <QPixmap>
#include <QAbstractItemView>
PixmapAnimator::PixmapAnimator( QWidget *parent, QList<QString> frames )
BasicAnimator::BasicAnimator( QObject *parent )
: 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 )
pixmaps.append( new QPixmap( name ) );
currentPixmap = pixmaps.at( 0 );
setFps( frames.count() ); /* default to 1 sec loop */
setLoopCount( -1 );
}
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 @@
#include <QList>
#include <QString>
#include <QAbstractAnimation>
#include <QPersistentModelIndex>
class QWidget;
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
* Use this widget to display an animated icon based on a series of
......@@ -34,13 +57,12 @@ class QPixmap;
* First, create the widget, add frames and then start playing. Looping
* is supported.
**/
class PixmapAnimator : public QAbstractAnimation
class PixmapAnimator : public BasicAnimator
{
Q_OBJECT
public:
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() { qDeleteAll( pixmaps ); }
QPixmap *getPixmap() { return currentPixmap; }
......@@ -48,12 +70,30 @@ 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 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
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