From fe58726b69561c3911f685a056eed03ad333e918 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 18 May 2012 12:09:58 +0200 Subject: [PATCH] * Directly query artist / albums objects for their data. --- src/libtomahawk/playlist/TreeModel.cpp | 79 +++++++++++++------------- src/libtomahawk/playlist/TreeModel.h | 3 +- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/libtomahawk/playlist/TreeModel.cpp b/src/libtomahawk/playlist/TreeModel.cpp index e4bb13fb2..e142c6b0e 100644 --- a/src/libtomahawk/playlist/TreeModel.cpp +++ b/src/libtomahawk/playlist/TreeModel.cpp @@ -29,6 +29,7 @@ #include "database/DatabaseCommand_AllAlbums.h" #include "database/DatabaseCommand_AllTracks.h" #include "database/Database.h" +#include "AlbumPlaylistInterface.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" @@ -55,6 +56,9 @@ TreeModel::TreeModel( QObject* parent ) TreeModel::~TreeModel() { + tDebug() << Q_FUNC_INFO; + + delete m_rootItem; } @@ -619,11 +623,15 @@ TreeModel::onAlbumsFound( const QList& albums, ModelMode mo if ( m_mode != mode ) return; - const artist_ptr artist = qobject_cast< Artist* >( sender() )->weakRef().toStrongRef(); - disconnect( artist.data(), SIGNAL( albumsAdded( QList, Tomahawk::ModelMode ) ), - this, SLOT( onAlbumsFound( QList, Tomahawk::ModelMode ) ) ); + Artist* artist = qobject_cast< Artist* >( sender() ); + if ( !artist ) + return; - const QModelIndex parent = indexFromArtist( artist ); + const artist_ptr artistp = artist->weakRef().toStrongRef(); + disconnect( artist, SIGNAL( albumsAdded( QList, Tomahawk::ModelMode ) ), + this, SLOT( onAlbumsFound( QList, Tomahawk::ModelMode ) ) ); + + const QModelIndex parent = indexFromArtist( artistp ); addAlbums( parent, albums ); } @@ -663,40 +671,10 @@ TreeModel::addTracks( const album_ptr& album, const QModelIndex& parent, bool au { emit loadingStarted(); - QList< QVariant > rows; - rows << parent.row(); - rows << parent.parent().row(); + connect( album.data(), SIGNAL( tracksAdded( QList, Tomahawk::ModelMode, Tomahawk::collection_ptr ) ), + SLOT( onTracksFound( QList, Tomahawk::ModelMode, Tomahawk::collection_ptr ) ) ); - if ( m_mode == DatabaseMode ) - { - DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_collection ); - cmd->setAlbum( album ); - cmd->setData( QVariant( rows ) ); - - connect( cmd, SIGNAL( tracks( QList, QVariant ) ), - SLOT( onTracksFound( QList, QVariant ) ) ); - - Database::instance()->enqueue( QSharedPointer( cmd ) ); - } - else if ( m_mode == InfoSystemMode ) - { - Tomahawk::InfoSystem::InfoStringHash artistInfo; - artistInfo["artist"] = album->artist()->name(); - artistInfo["album"] = album->name(); - - m_receivedInfoData.removeAll( artistInfo ); - Tomahawk::InfoSystem::InfoRequestData requestData; - requestData.caller = m_infoId; - requestData.customData["rows"] = rows; - requestData.customData["refetch"] = autoRefetch; - requestData.input = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( artistInfo ); - requestData.type = Tomahawk::InfoSystem::InfoAlbumSongs; - requestData.timeoutMillis = 0; - requestData.allSources = true; - Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData ); - } - else - Q_ASSERT( false ); + onTracksAdded( album->tracks( m_mode, m_collection ), parent ); } @@ -835,11 +813,14 @@ TreeModel::onTracksAdded( const QList& tracks, const QModel void -TreeModel::onTracksFound( const QList& tracks, const QVariant& variant ) +TreeModel::onTracksFound( const QList& tracks, Tomahawk::ModelMode mode, Tomahawk::collection_ptr collection ) { - QList< QVariant > rows = variant.toList(); - QModelIndex idx = index( rows.first().toUInt(), 0, index( rows.at( 1 ).toUInt(), 0, QModelIndex() ) ); + if ( mode != m_mode || collection != m_collection ) + return; + Album* album = qobject_cast( sender() ); + + QModelIndex idx = indexFromAlbum( album->weakRef().toStrongRef() ); onTracksAdded( tracks, idx ); } @@ -977,3 +958,21 @@ TreeModel::indexFromArtist( const Tomahawk::artist_ptr& artist ) const return QModelIndex(); } + + +QModelIndex +TreeModel::indexFromAlbum( const Tomahawk::album_ptr& album ) const +{ + QModelIndex artistIdx = indexFromArtist( album->artist() ); + for ( int i = 0; i < rowCount( artistIdx ); i++ ) + { + QModelIndex idx = index( i, 0, artistIdx ); + TreeModelItem* item = itemFromIndex( idx ); + if ( item && item->album() == album ) + { + return idx; + } + } + + return QModelIndex(); +} diff --git a/src/libtomahawk/playlist/TreeModel.h b/src/libtomahawk/playlist/TreeModel.h index 94d3b9a9a..30b0885fa 100644 --- a/src/libtomahawk/playlist/TreeModel.h +++ b/src/libtomahawk/playlist/TreeModel.h @@ -112,6 +112,7 @@ public: virtual void setIcon( const QPixmap& pixmap ) { m_icon = pixmap; } QModelIndex indexFromArtist( const Tomahawk::artist_ptr& artist ) const; + QModelIndex indexFromAlbum( const Tomahawk::album_ptr& album ) const; TreeModelItem* itemFromIndex( const QModelIndex& index ) const { if ( index.isValid() ) @@ -150,7 +151,7 @@ private slots: void onArtistsAdded( const QList& artists ); void onAlbumsFound( const QList& albums, Tomahawk::ModelMode mode ); void onTracksAdded( const QList& tracks, const QModelIndex& index ); - void onTracksFound( const QList& tracks, const QVariant& variant ); + void onTracksFound( const QList& tracks, Tomahawk::ModelMode mode, Tomahawk::collection_ptr collection ); void infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output );