From 812bfd7ca50e5f3bb5867d2c02e86d98856687f7 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Sat, 28 Apr 2012 16:29:21 -0400 Subject: [PATCH] Show spinner for an account while it's connecting --- src/AccountDelegate.cpp | 63 ++++++++++++++++++++- src/AccountDelegate.h | 6 ++ src/libtomahawk/accounts/Account.h | 1 + src/libtomahawk/accounts/AccountManager.cpp | 15 ++++- src/libtomahawk/accounts/AccountManager.h | 6 +- 5 files changed, 86 insertions(+), 5 deletions(-) diff --git a/src/AccountDelegate.cpp b/src/AccountDelegate.cpp index a7cd9f8c1..95e5ce418 100644 --- a/src/AccountDelegate.cpp +++ b/src/AccountDelegate.cpp @@ -24,6 +24,7 @@ #include "accounts/AccountModel.h" #include "accounts/Account.h" +#include "accounts/AccountManager.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" @@ -55,6 +56,7 @@ using namespace Accounts; AccountDelegate::AccountDelegate( QObject* parent ) : QStyledItemDelegate ( parent ) , m_accountRowHeight( -1 ) + , m_model( 0 ) { m_defaultCover.load( RESPATH "images/sipplugin-online.png" ); @@ -128,6 +130,9 @@ AccountDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option, painter->setRenderHint( QPainter::Antialiasing ); + if ( m_model == 0 || m_model != index.model() ) + m_model = const_cast( index.model() ); + QFont titleFont = opt.font; titleFont.setBold( true ); titleFont.setPointSize( titleFont.pointSize() + 2 ); @@ -596,7 +601,7 @@ AccountDelegate::drawStatus( QPainter* painter, const QPointF& rightTopEdge, Acc { QPixmap p; QString statusText; - Account::ConnectionState state = acct->connectionState(); + const Account::ConnectionState state = acct->connectionState(); if ( state == Account::Connected ) { p = m_onlineIcon; @@ -615,7 +620,29 @@ AccountDelegate::drawStatus( QPainter* painter, const QPointF& rightTopEdge, Acc const int yPos = rightTopEdge.y(); const QRect connectIconRect( rightTopEdge.x() - STATUS_ICON_SIZE, yPos, STATUS_ICON_SIZE, STATUS_ICON_SIZE ); - painter->drawPixmap( connectIconRect, p ); + + if ( state == Account::Connecting ) + { + if ( !m_connectingSpinners.contains( acct ) ) + { + AnimatedSpinner* anim = new AnimatedSpinner( connectIconRect.size(), true ); + _detail::Closure* closure = new _detail::Closure( anim, SIGNAL( requestUpdate() ), const_cast(this), SLOT( doUpdateIndexWithAccount( Tomahawk::Accounts::Account* ) ), C_ARG( Tomahawk::Accounts::Account*, acct ) ); + closure->setAutoDelete( false ); + + m_connectingSpinners[ acct ] = anim; + } + + const QPixmap pm = m_connectingSpinners[acct]->pixmap(); + painter->drawPixmap( connectIconRect, pm ); + } + else + { + if ( m_connectingSpinners.contains( acct ) ) + delete m_connectingSpinners.take( acct ); + + painter->drawPixmap( connectIconRect, p ); + } + int leftEdge = connectIconRect.x(); if ( drawText ) @@ -713,3 +740,35 @@ AccountDelegate::doUpdateIndex( const QPersistentModelIndex& idx ) emit update( idx ); } + +void +AccountDelegate::doUpdateIndexWithAccount( Account* account ) +{ + // Urgh, have to go through the list and check based on the type + for ( int i = 0; i < m_model->rowCount(); i++ ) + { + const QModelIndex index = m_model->index( i, 0, QModelIndex() ); + const AccountModel::RowType rowType = static_cast< AccountModel::RowType >( index.data( AccountModel::RowTypeRole ).toInt() ); + if ( rowType == AccountModel::TopLevelAccount || + rowType == AccountModel::CustomAccount ) + { + Account* acct = qobject_cast< Account* >( index.data( AccountModel::AccountData ).value< QObject* >() ); + + if ( account == acct ) + { + emit update( index ); + return; + } + } + else if ( rowType == AccountModel::TopLevelFactory || rowType == AccountModel::UniqueFactory ) + { + const QList< Account* > accts = index.data( AccountModel::ChildrenOfFactoryRole ).value< QList< Tomahawk::Accounts::Account* > >(); + if ( accts.contains( account ) ) + { + emit update( index ); + return; + } + } + } +} + diff --git a/src/AccountDelegate.h b/src/AccountDelegate.h index e0d4112bc..2028eb09b 100644 --- a/src/AccountDelegate.h +++ b/src/AccountDelegate.h @@ -54,6 +54,9 @@ signals: void openConfig( Tomahawk::Accounts::Account* ); void openConfig( Tomahawk::Accounts::AccountFactory* ); +private slots: + void doUpdateIndexWithAccount( Tomahawk::Accounts::Account* account ); + private: void drawRoundedButton( QPainter* painter, const QRect& buttonRect, bool red = false ) const; // Returns new left edge @@ -73,7 +76,10 @@ private: mutable QHash< QPersistentModelIndex, QRect > m_cachedConfigRects; mutable QHash< QPersistentModelIndex, QSize > m_sizeHints; mutable QHash< QPersistentModelIndex, AnimatedSpinner* > m_loadingSpinners; + mutable QHash< Account*, AnimatedSpinner* > m_connectingSpinners; mutable int m_accountRowHeight; + + mutable QAbstractItemModel* m_model; }; } diff --git a/src/libtomahawk/accounts/Account.h b/src/libtomahawk/accounts/Account.h index 8378e14c3..98afb3750 100644 --- a/src/libtomahawk/accounts/Account.h +++ b/src/libtomahawk/accounts/Account.h @@ -188,6 +188,7 @@ public: Q_DECLARE_INTERFACE( Tomahawk::Accounts::AccountFactory, "tomahawk.AccountFactory/1.0" ) +Q_DECLARE_METATYPE( Tomahawk::Accounts::Account* ) Q_DECLARE_METATYPE( QList< Tomahawk::Accounts::Account* > ) Q_DECLARE_METATYPE( Tomahawk::Accounts::AccountTypes ) #endif diff --git a/src/libtomahawk/accounts/AccountManager.cpp b/src/libtomahawk/accounts/AccountManager.cpp index 66da9ce39..0c9e3d190 100644 --- a/src/libtomahawk/accounts/AccountManager.cpp +++ b/src/libtomahawk/accounts/AccountManager.cpp @@ -74,7 +74,7 @@ AccountManager::init() QTimer::singleShot( 0, this, SLOT( init() ) ); return; } - + connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( onSettingsChanged() ) ); loadPluginFactories( findPluginFactories() ); @@ -347,6 +347,19 @@ AccountManager::removeAccount( Account* account ) } +QList< Account* > +AccountManager::accountsFromFactory( AccountFactory* factory ) const +{ + QList< Account* > accts; + foreach ( Account* acct, m_accounts ) + { + if ( factoryForAccount( acct ) == factory ) + accts << acct; + } + return accts; +} + + Account* AccountManager::accountFromPath( const QString& accountPath ) { diff --git a/src/libtomahawk/accounts/AccountManager.h b/src/libtomahawk/accounts/AccountManager.h index 01881791a..02a54efcb 100644 --- a/src/libtomahawk/accounts/AccountManager.h +++ b/src/libtomahawk/accounts/AccountManager.h @@ -43,7 +43,7 @@ public: explicit AccountManager( QObject *parent ); virtual ~AccountManager(); - + void loadFromConfig(); void initSIP(); @@ -61,6 +61,8 @@ public: QList< Account* > accounts() const { return m_accounts; }; QList< Account* > accounts( Tomahawk::Accounts::AccountType type ) const { return m_accountsByAccountType[ type ]; } + QList< Account* > accountsFromFactory( Tomahawk::Accounts::AccountFactory* factory ) const; + /** * Returns a new Account for a certain path on disk. This will go through all on-disk resolver account providers * to find the most specific account that matches this. @@ -85,7 +87,7 @@ public slots: signals: void ready(); - + void added( Tomahawk::Accounts::Account* ); void removed( Tomahawk::Accounts::Account* );