diff --git a/src/libtomahawk/collection/Collection.cpp b/src/libtomahawk/collection/Collection.cpp index 2409661e5..f0ec020c6 100644 --- a/src/libtomahawk/collection/Collection.cpp +++ b/src/libtomahawk/collection/Collection.cpp @@ -108,6 +108,13 @@ source_ptr& Collection::source() const } +int +Collection::trackCount() const +{ + return -1; +} + + void Collection::addPlaylist( const Tomahawk::playlist_ptr& p ) { diff --git a/src/libtomahawk/collection/Collection.h b/src/libtomahawk/collection/Collection.h index 8759fc421..574dce637 100644 --- a/src/libtomahawk/collection/Collection.h +++ b/src/libtomahawk/collection/Collection.h @@ -98,6 +98,8 @@ public: const source_ptr& source() const; unsigned int lastmodified() const { return m_lastmodified; } + virtual int trackCount() const; + signals: void tracksAdded( const QList& fileids ); void tracksRemoved( const QList& fileids ); diff --git a/src/libtomahawk/database/DatabaseCollection.cpp b/src/libtomahawk/database/DatabaseCollection.cpp index 9df267b00..4ceff587a 100644 --- a/src/libtomahawk/database/DatabaseCollection.cpp +++ b/src/libtomahawk/database/DatabaseCollection.cpp @@ -177,6 +177,13 @@ DatabaseCollection::requestTracks( const Tomahawk::album_ptr& album ) } +int +DatabaseCollection::trackCount() const +{ + return source()->trackCount(); +} + + void DatabaseCollection::autoPlaylistCreated( const source_ptr& source, const QVariantList& data ) { diff --git a/src/libtomahawk/database/DatabaseCollection.h b/src/libtomahawk/database/DatabaseCollection.h index a292ba475..4c41c8c08 100644 --- a/src/libtomahawk/database/DatabaseCollection.h +++ b/src/libtomahawk/database/DatabaseCollection.h @@ -54,6 +54,8 @@ public: virtual Tomahawk::AlbumsRequest* requestAlbums( const Tomahawk::artist_ptr& artist ); virtual Tomahawk::TracksRequest* requestTracks( const Tomahawk::album_ptr& album ); + virtual int trackCount() const; + public slots: virtual void addTracks( const QList& newitems ); virtual void removeTracks( const QDir& dir ); diff --git a/src/libtomahawk/resolvers/QtScriptResolver.cpp b/src/libtomahawk/resolvers/QtScriptResolver.cpp index 54d46370a..6a634f2e4 100644 --- a/src/libtomahawk/resolvers/QtScriptResolver.cpp +++ b/src/libtomahawk/resolvers/QtScriptResolver.cpp @@ -914,6 +914,15 @@ QtScriptResolver::loadCollections() // at this point we assume that all the tracks browsable through a resolver belong to the local source Tomahawk::ScriptCollection* sc = new Tomahawk::ScriptCollection( SourceList::instance()->getLocal(), this ); sc->setDescription( desc ); + + if ( collectionInfo.contains( "trackcount" ) ) //a resolver might not expose this + { + bool ok = false; + int trackCount = collectionInfo.value( "trackcount" ).toInt( &ok ); + if ( ok ) + sc->setTrackCount( trackCount ); + } + Tomahawk::collection_ptr collection( sc ); m_collections.insert( collection->name(), collection ); diff --git a/src/libtomahawk/resolvers/ScriptCollection.cpp b/src/libtomahawk/resolvers/ScriptCollection.cpp index bcf798602..d76ce2f49 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.cpp +++ b/src/libtomahawk/resolvers/ScriptCollection.cpp @@ -37,6 +37,7 @@ ScriptCollection::ScriptCollection( const source_ptr& source, ExternalResolver* resolver, QObject* parent ) : Collection( source, QString( "scriptcollection:" + resolver->name() + ":" + uuid() ), parent ) + , m_trackCount( -1 ) //null value { Q_ASSERT( resolver != 0 ); qDebug() << Q_FUNC_INFO << resolver->name() << name(); @@ -155,3 +156,17 @@ ScriptCollection::requestTracks( const Tomahawk::album_ptr& album ) return cmd; } + + +void +ScriptCollection::setTrackCount( int count ) +{ + m_trackCount = count; +} + + +int +ScriptCollection::trackCount() const +{ + return m_trackCount; +} diff --git a/src/libtomahawk/resolvers/ScriptCollection.h b/src/libtomahawk/resolvers/ScriptCollection.h index 9ff3543b8..8f17d2d9c 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.h +++ b/src/libtomahawk/resolvers/ScriptCollection.h @@ -57,9 +57,13 @@ public: virtual Tomahawk::AlbumsRequest* requestAlbums( const Tomahawk::artist_ptr& artist ); virtual Tomahawk::TracksRequest* requestTracks( const Tomahawk::album_ptr& album ); + virtual void setTrackCount( int count ); + virtual int trackCount() const; + private: ExternalResolver* m_resolver; QString m_description; + int m_trackCount; }; } //ns diff --git a/src/sourcetree/SourceDelegate.cpp b/src/sourcetree/SourceDelegate.cpp index 735c8d9b1..76139af47 100644 --- a/src/sourcetree/SourceDelegate.cpp +++ b/src/sourcetree/SourceDelegate.cpp @@ -204,6 +204,12 @@ SourceDelegate::paintCollection( QPainter* painter, const QStyleOptionViewItem& if ( !scItem->collection().isNull() ) { + int trackCount = scItem->collection()->trackCount(); + if ( trackCount >= 0 ) + { + tracks = QString::number( trackCount ); + figWidth = QFontMetrics( figFont ).width( tracks ); + } name = scItem->collection()->itemName(); } @@ -311,6 +317,7 @@ SourceDelegate::paintCollection( QPainter* painter, const QStyleOptionViewItem& painter->setPen( descColor ); painter->drawText( textRect, text, to ); + bool shouldPaintTrackCount = false; if ( type == SourcesModel::Collection ) { SourceItem* colItem = qobject_cast< SourceItem* >( item ); @@ -321,23 +328,30 @@ SourceDelegate::paintCollection( QPainter* painter, const QStyleOptionViewItem& m_trackRects[ index ] = textRect.adjusted( 0, 0, -textRect.width() + painter->fontMetrics().width( text ), 0 ); else m_trackRects.remove( index ); + if ( status && !tracks.isEmpty() ) + shouldPaintTrackCount = true; + } + else if ( type == SourcesModel::ScriptCollection ) + { + if ( !tracks.isEmpty() ) + shouldPaintTrackCount = true; + } - if ( status ) - { - painter->setRenderHint( QPainter::Antialiasing ); + if ( shouldPaintTrackCount ) + { + painter->setRenderHint( QPainter::Antialiasing ); - QRect figRect = option.rect.adjusted( option.rect.width() - figWidth - 13, 0, -14, -option.rect.height() + option.fontMetrics.height() * 1.1 ); - int hd = ( option.rect.height() - figRect.height() ) / 2; - figRect.adjust( 0, hd, 0, hd ); + QRect figRect = option.rect.adjusted( option.rect.width() - figWidth - 13, 0, -14, -option.rect.height() + option.fontMetrics.height() * 1.1 ); + int hd = ( option.rect.height() - figRect.height() ) / 2; + figRect.adjust( 0, hd, 0, hd ); - painter->setFont( figFont ); + painter->setFont( figFont ); - QColor figColor( 167, 183, 211 ); - painter->setPen( figColor ); - painter->setBrush( figColor ); + QColor figColor( 167, 183, 211 ); + painter->setPen( figColor ); + painter->setBrush( figColor ); - TomahawkUtils::drawBackgroundAndNumbers( painter, tracks, figRect ); - } + TomahawkUtils::drawBackgroundAndNumbers( painter, tracks, figRect ); } painter->restore();