From 32780f79a0b16debff46b0e879b4908911b54222 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Tue, 22 Jan 2013 16:08:38 +0100 Subject: [PATCH] Added support for showing multiple collections in a SourceItem. --- src/libtomahawk/Collection.cpp | 7 ++ src/libtomahawk/Collection.h | 1 + src/libtomahawk/Source.cpp | 4 +- src/libtomahawk/Source.h | 5 +- .../resolvers/ScriptCollection.cpp | 7 ++ src/libtomahawk/resolvers/ScriptCollection.h | 1 + src/sourcetree/items/SourceItem.cpp | 70 ++++++++++++++++--- src/sourcetree/items/SourceItem.h | 14 ++-- 8 files changed, 90 insertions(+), 19 deletions(-) diff --git a/src/libtomahawk/Collection.cpp b/src/libtomahawk/Collection.cpp index b10d0b63a..6f08a5389 100644 --- a/src/libtomahawk/Collection.cpp +++ b/src/libtomahawk/Collection.cpp @@ -56,6 +56,13 @@ Collection::name() const } +QString +Collection::prettyName() const +{ + return tr( "Collection" ); +} + + const source_ptr& Collection::source() const { diff --git a/src/libtomahawk/Collection.h b/src/libtomahawk/Collection.h index 1ebdd28f1..197e18250 100644 --- a/src/libtomahawk/Collection.h +++ b/src/libtomahawk/Collection.h @@ -48,6 +48,7 @@ public: virtual ~Collection(); virtual QString name() const; + virtual QString prettyName() const; virtual QString type() const { return QString(); } virtual void loadPlaylists() { qDebug() << Q_FUNC_INFO; } diff --git a/src/libtomahawk/Source.cpp b/src/libtomahawk/Source.cpp index 4c9784a72..bcd693117 100644 --- a/src/libtomahawk/Source.cpp +++ b/src/libtomahawk/Source.cpp @@ -210,7 +210,7 @@ Source::setDbFriendlyName( const QString& dbFriendlyName ) void Source::addCollection( const collection_ptr& c ) { - Q_ASSERT( m_collections.length() == 0 ); // only 1 source supported atm + //Q_ASSERT( m_collections.length() == 0 ); // only 1 source supported atm m_collections.append( c ); emit collectionAdded( c ); } @@ -219,7 +219,7 @@ Source::addCollection( const collection_ptr& c ) void Source::removeCollection( const collection_ptr& c ) { - Q_ASSERT( m_collections.length() == 1 && m_collections.first() == c ); // only 1 source supported atm + //Q_ASSERT( m_collections.length() == 1 && m_collections.first() == c ); // only 1 source supported atm m_collections.removeAll( c ); emit collectionRemoved( c ); } diff --git a/src/libtomahawk/Source.h b/src/libtomahawk/Source.h index de83e3116..7878c5c37 100644 --- a/src/libtomahawk/Source.h +++ b/src/libtomahawk/Source.h @@ -78,6 +78,7 @@ public: #endif collection_ptr dbCollection() const; + QList< Tomahawk::collection_ptr > collections() const { return m_collections; } void addCollection( const Tomahawk::collection_ptr& c ); void removeCollection( const Tomahawk::collection_ptr& c ); @@ -105,8 +106,8 @@ signals: void online(); void offline(); - void collectionAdded( const collection_ptr& collection ); - void collectionRemoved( const collection_ptr& collection ); + void collectionAdded( const Tomahawk::collection_ptr& collection ); + void collectionRemoved( const Tomahawk::collection_ptr& collection ); void stats( const QVariantMap& ); diff --git a/src/libtomahawk/resolvers/ScriptCollection.cpp b/src/libtomahawk/resolvers/ScriptCollection.cpp index 91131c65f..e9e062c5c 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.cpp +++ b/src/libtomahawk/resolvers/ScriptCollection.cpp @@ -40,3 +40,10 @@ ScriptCollection::~ScriptCollection() { } + + +QString +ScriptCollection::prettyName() const +{ + return QString(); +} diff --git a/src/libtomahawk/resolvers/ScriptCollection.h b/src/libtomahawk/resolvers/ScriptCollection.h index 10bbcd14e..b4162872f 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.h +++ b/src/libtomahawk/resolvers/ScriptCollection.h @@ -40,6 +40,7 @@ public: QObject* parent = 0 ); virtual ~ScriptCollection(); + virtual QString prettyName() const; virtual QString type() const { return "scriptcollection"; } private: diff --git a/src/sourcetree/items/SourceItem.cpp b/src/sourcetree/items/SourceItem.cpp index 30d98243e..4cf96fcb7 100644 --- a/src/sourcetree/items/SourceItem.cpp +++ b/src/sourcetree/items/SourceItem.cpp @@ -52,7 +52,6 @@ SourceItem::SourceItem( SourcesModel* mdl, SourceTreeItem* parent, const Tomahaw , m_latchedOn( false ) , m_sourceInfoItem( 0 ) , m_coolPlaylistsItem( 0 ) - , m_collectionPage( 0 ) , m_sourceInfoPage( 0 ) , m_coolPlaylistsPage( 0 ) , m_latestAdditionsPage( 0 ) @@ -64,9 +63,15 @@ SourceItem::SourceItem( SourcesModel* mdl, SourceTreeItem* parent, const Tomahaw return; } - m_collectionItem = new GenericPageItem( model(), this, tr( "Collection" ), ImageRegistry::instance()->icon( RESPATH "images/collection.svg" ), //FIXME different icon - boost::bind( &SourceItem::collectionClicked, this ), - boost::bind( &SourceItem::getCollectionPage, this ) ); + connect( source.data(), SIGNAL( collectionAdded( Tomahawk::collection_ptr ) ), + SLOT( onCollectionAdded( Tomahawk::collection_ptr ) ) ); + connect( source.data(), SIGNAL( collectionRemoved( Tomahawk::collection_ptr ) ), + SLOT( onCollectionRemoved( Tomahawk::collection_ptr ) ) ); + + foreach ( const Tomahawk::collection_ptr& collection, source->collections() ) + { + performAddCollectionItem( collection ); + } /* m_sourceInfoItem = new GenericPageItem( model(), this, tr( "New Additions" ), QIcon( RESPATH "images/new-additions.png" ), boost::bind( &SourceItem::sourceInfoClicked, this ), @@ -82,7 +87,6 @@ SourceItem::SourceItem( SourcesModel* mdl, SourceTreeItem* parent, const Tomahaw new LovedTracksItem( model(), this ); - m_collectionItem->setSortValue( -350 ); // m_sourceInfoItem->setSortValue( -300 ); m_latestAdditionsItem->setSortValue( -250 ); m_recentPlaysItem->setSortValue( -200 ); @@ -114,6 +118,7 @@ SourceItem::SourceItem( SourcesModel* mdl, SourceTreeItem* parent, const Tomahaw connect( source.data(), SIGNAL( stateChanged() ), SIGNAL( updated() ) ); connect( source.data(), SIGNAL( offline() ), SIGNAL( updated() ) ); connect( source.data(), SIGNAL( online() ), SIGNAL( updated() ) ); + connect( SourceList::instance(), SIGNAL( sourceLatchedOn( Tomahawk::source_ptr, Tomahawk::source_ptr ) ), SLOT( latchedOn( Tomahawk::source_ptr, Tomahawk::source_ptr ) ) ); connect( SourceList::instance(), SIGNAL( sourceLatchedOff( Tomahawk::source_ptr, Tomahawk::source_ptr ) ), SLOT( latchedOff( Tomahawk::source_ptr, Tomahawk::source_ptr ) ) ); @@ -294,6 +299,30 @@ SourceItem::latchModeChanged( Tomahawk::PlaylistModes::LatchMode mode ) } +void +SourceItem::onCollectionAdded( const collection_ptr& collection ) +{ + if ( m_collectionItems.contains( collection ) ) + return; + + beginRowsAdded( model()->rowCount( model()->indexFromItem( this ) ), + model()->rowCount( model()->indexFromItem( this ) ) ); + performAddCollectionItem( collection ); + endRowsAdded(); +} + + +void +SourceItem::onCollectionRemoved( const collection_ptr& collection ) +{ + delete m_collectionPages.value( collection, 0 ); + m_collectionPages.remove( collection ); + + m_collectionItems.value( collection )->deleteLater(); + m_collectionItems.remove( collection ); +} + + void SourceItem::playlistsAddedInternal( SourceTreeItem* parent, const QList< dynplaylist_ptr >& playlists ) { @@ -332,6 +361,25 @@ SourceItem::playlistsAddedInternal( SourceTreeItem* parent, const QList< dynplay } +void +SourceItem::performAddCollectionItem( const collection_ptr& collection ) +{ + GenericPageItem* item = new GenericPageItem( model(), + this, + collection->prettyName(), + ImageRegistry::instance()->icon( RESPATH "images/collection.svg" ), //FIXME different icon + boost::bind( &SourceItem::collectionClicked, this, collection ), + boost::bind( &SourceItem::getCollectionPage, this, collection ) ); + + if ( collection->type() == "databasecollection" ) + item->setSortValue( -350 ); + else + item->setSortValue( -340 ); + + m_collectionItems.insert( collection, item ); +} + + template< typename T > void SourceItem::playlistDeletedInternal( SourceTreeItem* parent, const T& p ) @@ -498,20 +546,20 @@ SourceItem::getSourceInfoPage() const ViewPage* -SourceItem::collectionClicked() +SourceItem::collectionClicked( const Tomahawk::collection_ptr& collection ) { if ( m_source.isNull() ) return 0; - m_collectionPage = ViewManager::instance()->show( m_source->dbCollection() ); - return m_collectionPage; + m_collectionPages[ collection ] = ViewManager::instance()->show( collection ); + return m_collectionPages[ collection ]; } ViewPage* -SourceItem::getCollectionPage() const -{ - return m_collectionPage;; +SourceItem::getCollectionPage( const Tomahawk::collection_ptr& collection ) const +{ + return m_collectionPages[ collection ]; } diff --git a/src/sourcetree/items/SourceItem.h b/src/sourcetree/items/SourceItem.h index 2e9238623..9501bcf76 100644 --- a/src/sourcetree/items/SourceItem.h +++ b/src/sourcetree/items/SourceItem.h @@ -1,6 +1,7 @@ /* * Copyright 2010-2011, Leo Franchi * Copyright 2010-2012, Jeff Mitchell + * Copyright 2013, Teo Mrnjavac * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -69,13 +70,16 @@ private slots: void latchedOff( const Tomahawk::source_ptr&, const Tomahawk::source_ptr& ); void latchModeChanged( Tomahawk::PlaylistModes::LatchMode mode ); + void onCollectionAdded( const Tomahawk::collection_ptr& ); //never call from ctor because of begin/endRowsAdded! + void onCollectionRemoved( const Tomahawk::collection_ptr& ); + void requestExpanding(); Tomahawk::ViewPage* sourceInfoClicked(); Tomahawk::ViewPage* getSourceInfoPage() const; - Tomahawk::ViewPage* collectionClicked(); - Tomahawk::ViewPage* getCollectionPage() const; + Tomahawk::ViewPage* collectionClicked( const Tomahawk::collection_ptr& collection ); + Tomahawk::ViewPage* getCollectionPage( const Tomahawk::collection_ptr& collection ) const; Tomahawk::ViewPage* coolPlaylistsClicked(); Tomahawk::ViewPage* getCoolPlaylistsPage() const; @@ -90,6 +94,7 @@ private: void playlistsAddedInternal( SourceTreeItem* parent, const QList< Tomahawk::dynplaylist_ptr >& playlists ); template< typename T > void playlistDeletedInternal( SourceTreeItem* parent, const T& playlists ); + void performAddCollectionItem( const Tomahawk::collection_ptr& collection ); Tomahawk::source_ptr m_source; CategoryItem* m_playlists; @@ -98,13 +103,14 @@ private: bool m_latchedOn; Tomahawk::source_ptr m_latchedOnTo; - GenericPageItem* m_collectionItem; + QMap< Tomahawk::collection_ptr, GenericPageItem* > m_collectionItems; + QMap< Tomahawk::collection_ptr, Tomahawk::ViewPage* > m_collectionPages; + GenericPageItem* m_sourceInfoItem; GenericPageItem* m_coolPlaylistsItem; GenericPageItem* m_latestAdditionsItem; GenericPageItem* m_recentPlaysItem; - Tomahawk::ViewPage* m_collectionPage; Tomahawk::ViewPage* m_sourceInfoPage; Tomahawk::ViewPage* m_coolPlaylistsPage; Tomahawk::ViewPage* m_latestAdditionsPage;