From 2943e13edc033381aea3a595672c62e2d4c02892 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 28 Nov 2012 08:01:21 +0100 Subject: [PATCH] * Made a few of PlayableModel's methods virtual. --- src/libtomahawk/playlist/PlayableModel.h | 6 +-- src/libtomahawk/playlist/TreeModel.cpp | 64 ++++++++++++++++++------ src/libtomahawk/playlist/TreeModel.h | 9 ++-- 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/libtomahawk/playlist/PlayableModel.h b/src/libtomahawk/playlist/PlayableModel.h index b2fdd4582..c9dcbf555 100644 --- a/src/libtomahawk/playlist/PlayableModel.h +++ b/src/libtomahawk/playlist/PlayableModel.h @@ -101,9 +101,9 @@ public: virtual void ensureResolved(); - PlayableItem* itemFromIndex( const QModelIndex& index ) const; - PlayableItem* itemFromQuery( const Tomahawk::query_ptr& query ) const; - PlayableItem* itemFromResult( const Tomahawk::result_ptr& result ) const; + virtual PlayableItem* itemFromIndex( const QModelIndex& index ) const; + virtual PlayableItem* itemFromQuery( const Tomahawk::query_ptr& query ) const; + virtual PlayableItem* itemFromResult( const Tomahawk::result_ptr& result ) const; /// Returns a flat list of all tracks in this model QList< Tomahawk::query_ptr > queries() const; diff --git a/src/libtomahawk/playlist/TreeModel.cpp b/src/libtomahawk/playlist/TreeModel.cpp index c77a1b92b..9781eb099 100644 --- a/src/libtomahawk/playlist/TreeModel.cpp +++ b/src/libtomahawk/playlist/TreeModel.cpp @@ -426,27 +426,59 @@ TreeModel::indexFromAlbum( const Tomahawk::album_ptr& album ) const QModelIndex TreeModel::indexFromResult( const Tomahawk::result_ptr& result ) const { - QModelIndex artistIdx = indexFromArtist( result->artist() ); - for ( int i = 0; i < rowCount( artistIdx ); i++ ) + QModelIndex albumIdx = indexFromAlbum( result->album() ); + for ( int i = 0; i < rowCount( albumIdx ); i++ ) { - QModelIndex idx = index( i, 0, artistIdx ); - PlayableItem* albumItem = itemFromIndex( idx ); - if ( albumItem && albumItem->album() == result->album() ) + QModelIndex idx = index( i, 0, albumIdx ); + PlayableItem* item = itemFromIndex( idx ); + tDebug() << item->result()->toString(); + if ( item && item->result() == result ) { - 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; + return idx; } } tDebug() << "Could not find item for result:" << result->toString(); return QModelIndex(); } + + +QModelIndex +TreeModel::indexFromQuery( const Tomahawk::query_ptr& query ) const +{ + Tomahawk::artist_ptr artist = Artist::get( query->artist(), false ); + Tomahawk::album_ptr album = Album::get( artist, query->album(), false ); + + QModelIndex albumIdx = indexFromAlbum( album ); + for ( int i = 0; i < rowCount( albumIdx ); i++ ) + { + QModelIndex idx = index( i, 0, albumIdx ); + PlayableItem* item = itemFromIndex( idx ); + if ( item && item->result() && item->result()->toQuery()->equals( query ) ) + { + return idx; + } + } + + tDebug() << "Could not find item for query:" << query->toString(); + return QModelIndex(); +} + + +PlayableItem* +TreeModel::itemFromResult( const Tomahawk::result_ptr& result ) const +{ + QModelIndex albumIdx = indexFromAlbum( result->album() ); + for ( int i = 0; i < rowCount( albumIdx ); i++ ) + { + QModelIndex idx = index( i, 0, albumIdx ); + 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/TreeModel.h b/src/libtomahawk/playlist/TreeModel.h index bfcf5a583..d477370e3 100644 --- a/src/libtomahawk/playlist/TreeModel.h +++ b/src/libtomahawk/playlist/TreeModel.h @@ -61,9 +61,12 @@ public: void getCover( const QModelIndex& index ); - QModelIndex indexFromArtist( const Tomahawk::artist_ptr& artist ) const; - QModelIndex indexFromAlbum( const Tomahawk::album_ptr& album ) const; - QModelIndex indexFromResult( const Tomahawk::result_ptr& result ) const; + virtual PlayableItem* itemFromResult( const Tomahawk::result_ptr& result ) const; + + virtual QModelIndex indexFromArtist( const Tomahawk::artist_ptr& artist ) const; + virtual QModelIndex indexFromAlbum( const Tomahawk::album_ptr& album ) const; + virtual QModelIndex indexFromResult( const Tomahawk::result_ptr& result ) const; + virtual QModelIndex indexFromQuery( const Tomahawk::query_ptr& query ) const; public slots: void addAlbums( const QModelIndex& parent, const QList& albums );