From 72528ab08964885548d8026f0d11c1b7db48f6cc Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Thu, 31 May 2012 21:31:43 +0200
Subject: [PATCH] * LoadingSpinner inherits AnimatedSpinner and automatically
 connects to a QAbstractItemView.

---
 src/libtomahawk/utils/AnimatedSpinner.cpp | 28 +++++++++++++++++++++--
 src/libtomahawk/utils/AnimatedSpinner.h   | 21 +++++++++++++++--
 2 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/src/libtomahawk/utils/AnimatedSpinner.cpp b/src/libtomahawk/utils/AnimatedSpinner.cpp
index c3cffd547..2d66e7273 100644
--- a/src/libtomahawk/utils/AnimatedSpinner.cpp
+++ b/src/libtomahawk/utils/AnimatedSpinner.cpp
@@ -31,7 +31,7 @@
 #include "utils/Logger.h"
 
 
-AnimatedSpinner::AnimatedSpinner( QWidget *parent )
+AnimatedSpinner::AnimatedSpinner( QWidget* parent )
     : QWidget( parent )
     , m_showHide( new QTimeLine )
     , m_animation( new QTimeLine )
@@ -41,7 +41,7 @@ AnimatedSpinner::AnimatedSpinner( QWidget *parent )
 }
 
 
-AnimatedSpinner::AnimatedSpinner( const QSize size, bool autoStart )
+AnimatedSpinner::AnimatedSpinner( const QSize& size, bool autoStart )
     : QWidget()
     , m_showHide( new QTimeLine )
     , m_animation( new QTimeLine )
@@ -260,3 +260,27 @@ AnimatedSpinner::segmentCount() const
 {
     return 11;
 }
+
+
+LoadingSpinner::LoadingSpinner( QAbstractItemView* parent )
+    : AnimatedSpinner( parent )
+    , m_parent( parent )
+{
+    if ( m_parent->model() )
+    {
+        connect( m_parent->model(), SIGNAL( loadingStarted() ), SLOT( fadeIn() ), Qt::UniqueConnection );
+        connect( m_parent->model(), SIGNAL( loadingFinished() ), SLOT( fadeOut() ), Qt::UniqueConnection );
+    }
+    connect( m_parent, SIGNAL( modelChanged() ), SLOT( onViewModelChanged() ) );
+}
+
+
+void
+LoadingSpinner::onViewModelChanged()
+{
+    if ( m_parent->model() )
+    {
+        connect( m_parent->model(), SIGNAL( loadingStarted() ), SLOT( fadeIn() ), Qt::UniqueConnection );
+        connect( m_parent->model(), SIGNAL( loadingFinished() ), SLOT( fadeOut() ), Qt::UniqueConnection );
+    }
+}
diff --git a/src/libtomahawk/utils/AnimatedSpinner.h b/src/libtomahawk/utils/AnimatedSpinner.h
index 0bf502246..19ec3090e 100644
--- a/src/libtomahawk/utils/AnimatedSpinner.h
+++ b/src/libtomahawk/utils/AnimatedSpinner.h
@@ -26,6 +26,7 @@
 #include <QtGui/QColor>
 #include <QtGui/QPixmap>
 #include <QPainter>
+#include <QAbstractItemView>
 
 class QTimeLine;
 class QHideEvent;
@@ -42,9 +43,10 @@ class QTimerEvent;
 class DLLEXPORT AnimatedSpinner : public QWidget
 {
     Q_OBJECT
+
 public:
-    explicit AnimatedSpinner( QWidget *parent = 0 ); // widget mode
-    AnimatedSpinner( const QSize size, bool autoStart ); // pixmap mode
+    explicit AnimatedSpinner( QWidget* parent = 0 ); // widget mode
+    AnimatedSpinner( const QSize& size, bool autoStart ); // pixmap mode
 
     QSize sizeHint() const;
 
@@ -87,4 +89,19 @@ private:
     bool m_autoCenter;
 };
 
+
+class LoadingSpinner : public AnimatedSpinner
+{
+    Q_OBJECT
+
+public:
+    explicit LoadingSpinner( QAbstractItemView* parent = 0 ); // widget mode
+    
+private slots:
+    void onViewModelChanged();
+
+private:
+    QAbstractItemView* m_parent;
+};
+
 #endif