From 339710c86ac4da118bf0d832a891ac599ee4d9f9 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Mon, 8 Aug 2011 23:23:52 -0400 Subject: [PATCH] Find playlist/dynplaylist item to activate if opened from non-sidebar. TWK-300. Ugh. --- .../playlist/dynamic/widgets/DynamicWidget.h | 1 + src/sourcetree/items/playlistitems.cpp | 29 +++++++++++++++++ src/sourcetree/items/playlistitems.h | 3 ++ src/sourcetree/sourcesmodel.cpp | 31 ++++++++++++++++++- src/sourcetree/sourcesmodel.h | 1 + 5 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/libtomahawk/playlist/dynamic/widgets/DynamicWidget.h b/src/libtomahawk/playlist/dynamic/widgets/DynamicWidget.h index 4b6e7a5b3..31f1040a3 100644 --- a/src/libtomahawk/playlist/dynamic/widgets/DynamicWidget.h +++ b/src/libtomahawk/playlist/dynamic/widgets/DynamicWidget.h @@ -63,6 +63,7 @@ public: virtual ~DynamicWidget(); void loadDynamicPlaylist( const dynplaylist_ptr& playlist ); + dynplaylist_ptr playlist() { return m_playlist; } virtual PlaylistInterface* playlistInterface() const; diff --git a/src/sourcetree/items/playlistitems.cpp b/src/sourcetree/items/playlistitems.cpp index 2bef4cce8..2146c9f37 100644 --- a/src/sourcetree/items/playlistitems.cpp +++ b/src/sourcetree/items/playlistitems.cpp @@ -180,6 +180,20 @@ PlaylistItem::setData( const QVariant& v, bool role ) return false; } +bool +PlaylistItem::activateCurrent() +{ + if( ViewManager::instance()->pageForPlaylist( m_playlist ) == ViewManager::instance()->currentPage() ) + { + model()->linkSourceItemToPage( this, ViewManager::instance()->currentPage() ); + emit selectRequest( this ); + + return true; + } + + return false; +} + DynamicPlaylistItem::DynamicPlaylistItem( SourcesModel* mdl, SourceTreeItem* parent, const dynplaylist_ptr& pl, int index ) : PlaylistItem( mdl, parent, pl.staticCast< Playlist >(), index ) @@ -312,3 +326,18 @@ DynamicPlaylistItem::icon() const return QIcon( RESPATH "images/automatic-playlist.png" ); } } + +bool +DynamicPlaylistItem::activateCurrent() +{ + if( ViewManager::instance()->pageForDynPlaylist( m_dynplaylist ) == ViewManager::instance()->currentPage() ) + { + model()->linkSourceItemToPage( this, ViewManager::instance()->currentPage() ); + emit selectRequest( this ); + + return true; + } + + return false; +} + diff --git a/src/sourcetree/items/playlistitems.h b/src/sourcetree/items/playlistitems.h index 42ce35e00..1c3e9d463 100644 --- a/src/sourcetree/items/playlistitems.h +++ b/src/sourcetree/items/playlistitems.h @@ -37,6 +37,8 @@ public: virtual bool setData(const QVariant& v, bool role); virtual int peerSortValue() const; + virtual bool activateCurrent(); + protected: void setLoaded( bool loaded ); @@ -65,6 +67,7 @@ public: virtual int peerSortValue() const; virtual QIcon icon() const; + virtual bool activateCurrent(); private slots: void onDynamicPlaylistLoaded( Tomahawk::DynamicPlaylistRevision revision ); diff --git a/src/sourcetree/sourcesmodel.cpp b/src/sourcetree/sourcesmodel.cpp index 8b9858625..5c5254624 100644 --- a/src/sourcetree/sourcesmodel.cpp +++ b/src/sourcetree/sourcesmodel.cpp @@ -33,6 +33,7 @@ #include "utils/logger.h" #include "globalactionmanager.h" +#include "items/playlistitems.h" using namespace Tomahawk; @@ -281,10 +282,38 @@ SourcesModel::viewPageActivated( Tomahawk::ViewPage* page ) } else { - m_viewPageDelayedCacheItem = page; + // HACK + // try to find it if it is a playlist. not pretty at all.... but this happens when ViewManager loads a playlist or dynplaylist NOT from the sidebar but from somewhere else + // we don't know which sourcetreeitem is related to it, so we have to find it. we also don't know if this page is a playlist or dynplaylist or not, but we can't check as we can't + // include DynamicWidget.h here (so can't dynamic_cast). + // this could also be fixed by keeping a master list of playlists/sourcetreeitems... but that's even uglier i think. this is only called the first time a certain viewpage is clicked from external + // sources. + if( activatePlaylistPage( page, m_rootItem ) ) + m_viewPageDelayedCacheItem = page; } } +bool +SourcesModel::activatePlaylistPage( ViewPage* p, SourceTreeItem* i ) +{ + if( !i ) + return false; + + qDebug() << "Doing an activation check on:" << p << i; + if( qobject_cast< PlaylistItem* >( i ) && + qobject_cast< PlaylistItem* >( i )->activateCurrent() ) + return true; + + bool ret = false; + for( int k = 0; k < i->children().size(); k++ ) + { + if( activatePlaylistPage( p, i->children().at( k ) ) ) + ret = true; + } + + return ret; +} + void SourcesModel::loadSources() diff --git a/src/sourcetree/sourcesmodel.h b/src/sourcetree/sourcesmodel.h index c702b4b4b..94ca4008c 100644 --- a/src/sourcetree/sourcesmodel.h +++ b/src/sourcetree/sourcesmodel.h @@ -113,6 +113,7 @@ private slots: private: SourceTreeItem* itemFromIndex( const QModelIndex& idx ) const; int rowForItem( SourceTreeItem* item ) const; + bool activatePlaylistPage( Tomahawk::ViewPage* p, SourceTreeItem* i ); SourceTreeItem* m_rootItem;