[vlc-commits] Qt: add DelegateAnimationHelper.

Francois Cartegnie git at videolan.org
Sun Feb 9 21:27:03 CET 2014


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Sat Feb  8 19:26:08 2014 +0100| [aea9ebb9fdd4251ba384c6a68c9b35dafd99d2b8] | committer: Francois Cartegnie

Qt: add DelegateAnimationHelper.

Delegates can't request updates themselves.
Repaint needs to be triggered at model or view level.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=aea9ebb9fdd4251ba384c6a68c9b35dafd99d2b8
---

 modules/gui/qt4/util/animators.cpp |   74 +++++++++++++++++++++++++++++++++++-
 modules/gui/qt4/util/animators.hpp |   52 ++++++++++++++++++++++---
 2 files changed, 118 insertions(+), 8 deletions(-)

diff --git a/modules/gui/qt4/util/animators.cpp b/modules/gui/qt4/util/animators.cpp
index 9bed3a6..9d9f23d 100644
--- a/modules/gui/qt4/util/animators.cpp
+++ b/modules/gui/qt4/util/animators.cpp
@@ -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 );
+    }
+}
diff --git a/modules/gui/qt4/util/animators.hpp b/modules/gui/qt4/util/animators.hpp
index 87cb160..1b8f10a 100644
--- a/modules/gui/qt4/util/animators.hpp
+++ b/modules/gui/qt4/util/animators.hpp
@@ -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



More information about the vlc-commits mailing list