From ad7263477ff70c543ab447138701df68273ca73a Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 28 Nov 2012 04:52:07 +0100 Subject: [PATCH] * Updated Playable- & TreeModel to handle new PlaylistInterfaces. --- src/libtomahawk/playlist/PlayableModel.cpp | 60 ++++++++++++++++++- src/libtomahawk/playlist/PlayableModel.h | 7 ++- .../playlist/PlayableProxyModel.cpp | 47 +++++++++++---- src/libtomahawk/playlist/PlayableProxyModel.h | 12 +++- src/libtomahawk/playlist/TreeModel.cpp | 29 +++++++++ src/libtomahawk/playlist/TreeModel.h | 1 + src/libtomahawk/playlist/TreeProxyModel.cpp | 26 +++++--- src/libtomahawk/playlist/TreeProxyModel.h | 8 +-- 8 files changed, 160 insertions(+), 30 deletions(-) diff --git a/src/libtomahawk/playlist/PlayableModel.cpp b/src/libtomahawk/playlist/PlayableModel.cpp index c5b5ce415..5912404fa 100644 --- a/src/libtomahawk/playlist/PlayableModel.cpp +++ b/src/libtomahawk/playlist/PlayableModel.cpp @@ -56,6 +56,18 @@ PlayableModel::~PlayableModel() } +QModelIndex +PlayableModel::createIndex( int row, int column, PlayableItem* item ) const +{ + if ( item->query() ) + { + connect( item->query().data(), SIGNAL( playableStateChanged( bool ) ), SLOT( onQueryBecamePlayable( bool ) ), Qt::UniqueConnection ); + } + + return QAbstractItemModel::createIndex( row, column, item ); +} + + QModelIndex PlayableModel::index( int row, int column, const QModelIndex& parent ) const { @@ -192,7 +204,7 @@ PlayableModel::queryData( const query_ptr& query, int column, int role ) const if ( query->albumpos() != 0 ) { tPos = QString::number( query->albumpos() ); - if( query->discnumber() == 0 ) + if ( query->discnumber() == 0 ) return tPos; else return QString( "%1.%2" ).arg( QString::number( query->discnumber() ) ) @@ -542,7 +554,7 @@ PlayableModel::insertInternal( const QList< T >& items, int row ) int i = 0; PlayableItem* plitem; - foreach( const T& item, items ) + foreach ( const T& item, items ) { plitem = new PlayableItem( item, m_rootItem, row + i ); plitem->index = createIndex( row + i, 0, plitem ); @@ -846,9 +858,32 @@ PlayableModel::setIcon( const QPixmap& pixmap ) } +void +PlayableModel::onQueryBecamePlayable( bool playable ) +{ + Tomahawk::Query* q = qobject_cast< Query* >( sender() ); + if ( !q ) + { + // Track has been removed from the playlist by now + return; + } + + Tomahawk::query_ptr query = q->weakRef().toStrongRef(); + PlayableItem* item = itemFromQuery( query ); + + if ( item ) + { + emit indexPlayable( item->index ); + } +} + + PlayableItem* PlayableModel::itemFromQuery( const Tomahawk::query_ptr& query ) const { + if ( !query ) + return 0; + for ( int i = 0; i < rowCount( QModelIndex() ); i++ ) { QModelIndex idx = index( i, 0, QModelIndex() ); @@ -862,3 +897,24 @@ PlayableModel::itemFromQuery( const Tomahawk::query_ptr& query ) const tDebug() << "Could not find item for query:" << query->toString(); return 0; } + + +PlayableItem* +PlayableModel::itemFromResult( const Tomahawk::result_ptr& result ) const +{ + if ( !result ) + return 0; + + for ( int i = 0; i < rowCount( QModelIndex() ); i++ ) + { + QModelIndex idx = index( i, 0, QModelIndex() ); + PlayableItem* item = itemFromIndex( idx ); + if ( item && item->result() == result ) + { + return item; + } + } + + tDebug() << "Could not find item for result:" << result->toString(); + return 0; +} diff --git a/src/libtomahawk/playlist/PlayableModel.h b/src/libtomahawk/playlist/PlayableModel.h index 950448b28..b2fdd4582 100644 --- a/src/libtomahawk/playlist/PlayableModel.h +++ b/src/libtomahawk/playlist/PlayableModel.h @@ -38,7 +38,8 @@ class DLLEXPORT PlayableModel : public QAbstractItemModel Q_OBJECT public: - enum Columns { + enum Columns + { Artist = 0, Track = 1, Composer = 2, @@ -102,6 +103,7 @@ public: PlayableItem* itemFromIndex( const QModelIndex& index ) const; PlayableItem* itemFromQuery( const Tomahawk::query_ptr& query ) const; + PlayableItem* itemFromResult( const Tomahawk::result_ptr& result ) const; /// Returns a flat list of all tracks in this model QList< Tomahawk::query_ptr > queries() const; @@ -119,6 +121,7 @@ signals: void loadingStarted(); void loadingFinished(); + void indexPlayable( const QModelIndex& index ); void changed(); public slots: @@ -150,9 +153,11 @@ public slots: protected: PlayableItem* rootItem() const { return m_rootItem; } + QModelIndex createIndex( int row, int column, PlayableItem* item = 0 ) const; private slots: void onDataChanged(); + void onQueryBecamePlayable( bool playable ); void onPlaybackStarted( const Tomahawk::result_ptr& result ); void onPlaybackStopped(); diff --git a/src/libtomahawk/playlist/PlayableProxyModel.cpp b/src/libtomahawk/playlist/PlayableProxyModel.cpp index 48f681011..541a9d5e0 100644 --- a/src/libtomahawk/playlist/PlayableProxyModel.cpp +++ b/src/libtomahawk/playlist/PlayableProxyModel.cpp @@ -38,6 +38,8 @@ PlayableProxyModel::PlayableProxyModel( QObject* parent ) , m_maxVisibleItems( -1 ) , m_style( Detailed ) { + m_playlistInterface = Tomahawk::playlistinterface_ptr( new Tomahawk::PlayableProxyModelPlaylistInterface( this ) ); + setFilterCaseSensitivity( Qt::CaseInsensitive ); setSortCaseSensitivity( Qt::CaseInsensitive ); setDynamicSortFilter( true ); @@ -50,6 +52,20 @@ PlayableProxyModel::PlayableProxyModel( QObject* parent ) } +Tomahawk::playlistinterface_ptr +PlayableProxyModel::playlistInterface() const +{ + return m_playlistInterface; +} + + +void +PlayableProxyModel::setPlaylistInterface( const Tomahawk::playlistinterface_ptr& playlistInterface ) +{ + m_playlistInterface = playlistInterface; +} + + QString PlayableProxyModel::guid() const { @@ -90,6 +106,7 @@ PlayableProxyModel::setSourcePlayableModel( PlayableModel* sourceModel ) { disconnect( m_model, SIGNAL( loadingStarted() ), this, SIGNAL( loadingStarted() ) ); disconnect( m_model, SIGNAL( loadingFinished() ), this, SIGNAL( loadingFinished() ) ); + disconnect( m_model, SIGNAL( indexPlayable( QModelIndex ) ), this, SLOT( onIndexPlayable( QModelIndex ) ) ); } m_model = sourceModel; @@ -98,6 +115,7 @@ PlayableProxyModel::setSourcePlayableModel( PlayableModel* sourceModel ) { connect( m_model, SIGNAL( loadingStarted() ), SIGNAL( loadingStarted() ) ); connect( m_model, SIGNAL( loadingFinished() ), SIGNAL( loadingFinished() ) ); + connect( m_model, SIGNAL( indexPlayable( QModelIndex ) ), SLOT( onIndexPlayable( QModelIndex ) ) ); } QSortFilterProxyModel::setSourceModel( m_model ); @@ -482,18 +500,6 @@ PlayableProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right } -Tomahawk::playlistinterface_ptr -PlayableProxyModel::playlistInterface() -{ - if ( m_playlistInterface.isNull() ) - { - m_playlistInterface = Tomahawk::playlistinterface_ptr( new Tomahawk::PlayableProxyModelPlaylistInterface( this ) ); - } - - return m_playlistInterface; -} - - int PlayableProxyModel::columnCount( const QModelIndex& parent ) const { @@ -616,3 +622,20 @@ PlayableProxyModel::setFilter( const QString& pattern ) emit filterChanged( pattern ); } } + + +void +PlayableProxyModel::setCurrentIndex( const QModelIndex& index ) +{ + tDebug() << Q_FUNC_INFO << QThread::currentThread(); + m_model->setCurrentItem( mapToSource( index ) ); + emit currentIndexChanged(); + tDebug() << Q_FUNC_INFO << QThread::currentThread() << "Finished"; +} + + +void +PlayableProxyModel::onIndexPlayable( const QModelIndex& index ) +{ + emit indexPlayable( mapFromSource( index ) ); +} diff --git a/src/libtomahawk/playlist/PlayableProxyModel.h b/src/libtomahawk/playlist/PlayableProxyModel.h index 36e21cf31..88d7c033c 100644 --- a/src/libtomahawk/playlist/PlayableProxyModel.h +++ b/src/libtomahawk/playlist/PlayableProxyModel.h @@ -53,7 +53,7 @@ public: void setStyle( PlayableProxyModel::PlayableItemStyle style ) { m_style = style; } virtual QPersistentModelIndex currentIndex() const { return mapFromSource( m_model->currentItem() ); } - virtual void setCurrentIndex( const QModelIndex& index ) { m_model->setCurrentItem( mapToSource( index ) ); } + virtual void setCurrentIndex( const QModelIndex& index ); virtual void removeIndex( const QModelIndex& index ); virtual void removeIndexes( const QModelIndexList& indexes ); @@ -70,8 +70,10 @@ public: virtual PlayableItem* itemFromIndex( const QModelIndex& index ) const { return sourceModel()->itemFromIndex( index ); } virtual PlayableItem* itemFromQuery( const Tomahawk::query_ptr& query ) const { return sourceModel()->itemFromQuery( query ); } + virtual PlayableItem* itemFromResult( const Tomahawk::result_ptr& result ) const { return sourceModel()->itemFromResult( result ); } - virtual Tomahawk::playlistinterface_ptr playlistInterface(); + virtual Tomahawk::playlistinterface_ptr playlistInterface() const; + void setPlaylistInterface( const Tomahawk::playlistinterface_ptr& playlistInterface ); QList< double > columnWeights() const; @@ -91,12 +93,18 @@ signals: void loadingStarted(); void loadingFinished(); + void indexPlayable( const QModelIndex& index ); + void currentIndexChanged(); + protected: virtual bool filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const; virtual bool lessThan( const QModelIndex& left, const QModelIndex& right ) const; Tomahawk::playlistinterface_ptr m_playlistInterface; +private slots: + void onIndexPlayable( const QModelIndex& index ); + private: virtual bool lessThan( int column, const Tomahawk::query_ptr& left, const Tomahawk::query_ptr& right ) const; diff --git a/src/libtomahawk/playlist/TreeModel.cpp b/src/libtomahawk/playlist/TreeModel.cpp index 063bf2804..c77a1b92b 100644 --- a/src/libtomahawk/playlist/TreeModel.cpp +++ b/src/libtomahawk/playlist/TreeModel.cpp @@ -421,3 +421,32 @@ TreeModel::indexFromAlbum( const Tomahawk::album_ptr& album ) const tDebug() << "Could not find item for album:" << album->name() << album->artist()->name(); return QModelIndex(); } + + +QModelIndex +TreeModel::indexFromResult( const Tomahawk::result_ptr& result ) const +{ + QModelIndex artistIdx = indexFromArtist( result->artist() ); + for ( int i = 0; i < rowCount( artistIdx ); i++ ) + { + QModelIndex idx = index( i, 0, artistIdx ); + PlayableItem* albumItem = itemFromIndex( idx ); + if ( albumItem && albumItem->album() == result->album() ) + { + for ( int j = 0; j < rowCount( idx ); j++ ) + { + QModelIndex subidx = index( j, 0, idx ); + PlayableItem* item = itemFromIndex( subidx ); + if ( item && item->result() == result ) + { + return subidx; + } + } + + break; + } + } + + tDebug() << "Could not find item for result:" << result->toString(); + return QModelIndex(); +} diff --git a/src/libtomahawk/playlist/TreeModel.h b/src/libtomahawk/playlist/TreeModel.h index 6fb91d3c3..bfcf5a583 100644 --- a/src/libtomahawk/playlist/TreeModel.h +++ b/src/libtomahawk/playlist/TreeModel.h @@ -63,6 +63,7 @@ public: QModelIndex indexFromArtist( const Tomahawk::artist_ptr& artist ) const; QModelIndex indexFromAlbum( const Tomahawk::album_ptr& album ) const; + QModelIndex indexFromResult( const Tomahawk::result_ptr& result ) const; public slots: void addAlbums( const QModelIndex& parent, const QList& albums ); diff --git a/src/libtomahawk/playlist/TreeProxyModel.cpp b/src/libtomahawk/playlist/TreeProxyModel.cpp index 5cf9d2a1f..a565d2bd9 100644 --- a/src/libtomahawk/playlist/TreeProxyModel.cpp +++ b/src/libtomahawk/playlist/TreeProxyModel.cpp @@ -36,6 +36,7 @@ TreeProxyModel::TreeProxyModel( QObject* parent ) , m_artistsFilterCmd( 0 ) , m_model( 0 ) { + setPlaylistInterface( Tomahawk::playlistinterface_ptr( new Tomahawk::TreeProxyModelPlaylistInterface( this ) ) ); } @@ -347,13 +348,22 @@ TreeProxyModel::textForItem( PlayableItem* item ) const } -Tomahawk::playlistinterface_ptr -TreeProxyModel::playlistInterface() +QModelIndex +TreeProxyModel::indexFromArtist( const Tomahawk::artist_ptr& artist ) const { - if ( m_playlistInterface.isNull() ) - { - m_playlistInterface = Tomahawk::playlistinterface_ptr( new Tomahawk::TreeProxyModelPlaylistInterface( this ) ); - } - - return m_playlistInterface; + return mapFromSource( m_model->indexFromArtist( artist ) ); +} + + +QModelIndex +TreeProxyModel::indexFromAlbum( const Tomahawk::album_ptr& album ) const +{ + return mapFromSource( m_model->indexFromAlbum( album ) ); +} + + +QModelIndex +TreeProxyModel::indexFromResult( const Tomahawk::result_ptr& result ) const +{ + return mapFromSource( m_model->indexFromResult( result ) ); } diff --git a/src/libtomahawk/playlist/TreeProxyModel.h b/src/libtomahawk/playlist/TreeProxyModel.h index 0578d6ac5..801dad821 100644 --- a/src/libtomahawk/playlist/TreeProxyModel.h +++ b/src/libtomahawk/playlist/TreeProxyModel.h @@ -45,11 +45,11 @@ public: // workaround overloaded-virtual warning virtual void setSourcePlayableModel( PlayableModel* ) { Q_ASSERT( false ); } - virtual Tomahawk::playlistinterface_ptr playlistInterface(); - virtual void setFilter( const QString& pattern ); -signals: + QModelIndex indexFromArtist( const Tomahawk::artist_ptr& artist ) const; + QModelIndex indexFromAlbum( const Tomahawk::album_ptr& album ) const; + QModelIndex indexFromResult( const Tomahawk::result_ptr& result ) const; protected: bool filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const; @@ -78,8 +78,6 @@ private: QString m_filter; TreeModel* m_model; - - Tomahawk::playlistinterface_ptr m_playlistInterface; }; #endif // TREEPROXYMODEL_H