From 5d322c12cbdc4bd8fefdaecf98eeec4f5b4e81f4 Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Tue, 6 Jan 2015 03:23:32 +0100 Subject: [PATCH] Add ResultProvider baseclass for Collections and Resolvers --- src/libtomahawk/CMakeLists.txt | 1 + src/libtomahawk/Result.cpp | 4 +- src/libtomahawk/ResultProvider.cpp | 24 +++++++++++ src/libtomahawk/ResultProvider.h | 42 +++++++++++++++++++ src/libtomahawk/accounts/ResolverAccount.cpp | 2 +- src/libtomahawk/collection/Collection.cpp | 8 ++-- src/libtomahawk/collection/Collection.h | 9 ++-- src/libtomahawk/database/DatabaseResolver.cpp | 2 +- src/libtomahawk/database/DatabaseResolver.h | 2 +- src/libtomahawk/resolvers/ExternalResolver.h | 1 - src/libtomahawk/resolvers/JSResolver.cpp | 11 +++-- src/libtomahawk/resolvers/JSResolver.h | 4 +- src/libtomahawk/resolvers/Resolver.cpp | 9 +++- src/libtomahawk/resolvers/Resolver.h | 11 ++--- .../resolvers/ScriptCollection.cpp | 14 +++---- src/libtomahawk/resolvers/ScriptCollection.h | 8 ++-- src/libtomahawk/resolvers/ScriptResolver.cpp | 10 +++++ src/libtomahawk/resolvers/ScriptResolver.h | 4 +- .../sourcetree/items/ScriptCollectionItem.cpp | 2 +- src/tomahawk/sourcetree/items/SourceItem.cpp | 2 +- 20 files changed, 127 insertions(+), 43 deletions(-) create mode 100644 src/libtomahawk/ResultProvider.cpp create mode 100644 src/libtomahawk/ResultProvider.h diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index a4d64b576..c4d6eb5a3 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -197,6 +197,7 @@ list(APPEND libSources MetaPlaylistInterface.cpp Query.cpp Result.cpp + ResultProvider.cpp Source.cpp Track.cpp TrackData.cpp diff --git a/src/libtomahawk/Result.cpp b/src/libtomahawk/Result.cpp index 361c09cdb..c658b8b92 100644 --- a/src/libtomahawk/Result.cpp +++ b/src/libtomahawk/Result.cpp @@ -421,9 +421,7 @@ Result::sourceIcon( TomahawkUtils::ImageMode style, const QSize& desiredSize ) c const QString key = sourceCacheKey( m_resolvedBy.data(), desiredSize, style ); if ( !sourceIconCache()->contains( key ) ) { - QPixmap pixmap = resolver->icon(); - if ( !desiredSize.isEmpty() ) - pixmap = pixmap.scaled( desiredSize, Qt::KeepAspectRatio, Qt::SmoothTransformation ); + QPixmap pixmap = resolver->icon( desiredSize ); switch ( style ) { diff --git a/src/libtomahawk/ResultProvider.cpp b/src/libtomahawk/ResultProvider.cpp new file mode 100644 index 000000000..55c1ae08f --- /dev/null +++ b/src/libtomahawk/ResultProvider.cpp @@ -0,0 +1,24 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright (C) 2015 Dominik Schmidt + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ +#include "ResultProvider.h" + +using namespace Tomahawk; + +ResultProvider::~ResultProvider() +{ +} diff --git a/src/libtomahawk/ResultProvider.h b/src/libtomahawk/ResultProvider.h new file mode 100644 index 000000000..a32d2575d --- /dev/null +++ b/src/libtomahawk/ResultProvider.h @@ -0,0 +1,42 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright (C) 2015 Dominik Schmidt + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ +#pragma once +#ifndef TOMAHAWK_RESULTPROVIDER_H +#define TOMAHAWK_RESULTPROVIDER_H + +#include "DllMacro.h" + +class QPixmap; +class QString; +class QSize; + +namespace Tomahawk +{ + +class DLLEXPORT ResultProvider +{ +public: + virtual ~ResultProvider(); + + virtual const QString name() const = 0; + virtual const QPixmap icon( const QSize& size ) const = 0; +}; + +} + +#endif // TOMAHAWK_RESULTPROVIDER_H diff --git a/src/libtomahawk/accounts/ResolverAccount.cpp b/src/libtomahawk/accounts/ResolverAccount.cpp index 5865ee410..d057961b5 100644 --- a/src/libtomahawk/accounts/ResolverAccount.cpp +++ b/src/libtomahawk/accounts/ResolverAccount.cpp @@ -472,7 +472,7 @@ ResolverAccount::icon() const if ( m_resolver.isNull() ) return QPixmap(); - return m_resolver->icon(); + return m_resolver->icon( QSize( 0, 0 ) ); } diff --git a/src/libtomahawk/collection/Collection.cpp b/src/libtomahawk/collection/Collection.cpp index 87802c151..de9c2d2a7 100644 --- a/src/libtomahawk/collection/Collection.cpp +++ b/src/libtomahawk/collection/Collection.cpp @@ -67,7 +67,7 @@ Collection::weakRef() const } -QString +const QString Collection::name() const { return m_name; @@ -95,10 +95,10 @@ Collection::itemName() const } -QIcon -Collection::icon() const +const QPixmap +Collection::icon( const QSize& size ) const { - return ImageRegistry::instance()->icon( RESPATH "images/collection.svg" ); + return ImageRegistry::instance()->pixmap( RESPATH "images/collection.svg", size ); } diff --git a/src/libtomahawk/collection/Collection.h b/src/libtomahawk/collection/Collection.h index 0fee6e62a..c83b4e685 100644 --- a/src/libtomahawk/collection/Collection.h +++ b/src/libtomahawk/collection/Collection.h @@ -37,15 +37,14 @@ #include "collection/ArtistsRequest.h" #include "collection/AlbumsRequest.h" #include "collection/TracksRequest.h" +#include "../ResultProvider.h" #include "DllMacro.h" -class QIcon; - namespace Tomahawk { -class DLLEXPORT Collection : public QObject +class DLLEXPORT Collection : public QObject, public ResultProvider { Q_OBJECT @@ -63,12 +62,12 @@ public: ScriptCollectionType //performs operations through a resolver }; - virtual QString name() const; + virtual const QString name() const override; virtual QString prettyName() const; virtual QString description() const; virtual QString itemName() const; virtual BackendType backendType() const { return NullCollectionType; } - virtual QIcon icon() const; + virtual const QPixmap icon( const QSize& size ) const override; virtual QPixmap bigIcon() const; //for the ViewPage header virtual void loadPlaylists(); diff --git a/src/libtomahawk/database/DatabaseResolver.cpp b/src/libtomahawk/database/DatabaseResolver.cpp index 8e455a5f9..7dd9f5828 100644 --- a/src/libtomahawk/database/DatabaseResolver.cpp +++ b/src/libtomahawk/database/DatabaseResolver.cpp @@ -77,7 +77,7 @@ DatabaseResolver::gotArtists( const Tomahawk::QID qid, QList< Tomahawk::artist_p } -QString +const QString DatabaseResolver::name() const { return QString( "DatabaseResolver" ); diff --git a/src/libtomahawk/database/DatabaseResolver.h b/src/libtomahawk/database/DatabaseResolver.h index dcfd40744..fc860c0f0 100644 --- a/src/libtomahawk/database/DatabaseResolver.h +++ b/src/libtomahawk/database/DatabaseResolver.h @@ -33,7 +33,7 @@ Q_OBJECT public: explicit DatabaseResolver( int weight ); - virtual QString name() const; + const QString name() const override; virtual unsigned int weight() const { return m_weight; } virtual unsigned int timeout() const { return 0; } diff --git a/src/libtomahawk/resolvers/ExternalResolver.h b/src/libtomahawk/resolvers/ExternalResolver.h index 6c8ff4199..ea1865324 100644 --- a/src/libtomahawk/resolvers/ExternalResolver.h +++ b/src/libtomahawk/resolvers/ExternalResolver.h @@ -88,7 +88,6 @@ public: { m_filePath = filePath; } QString filePath() const { return m_filePath; } - virtual QPixmap icon() const { return QPixmap(); } virtual void setIcon( const QPixmap& ) {} virtual void saveConfig() = 0; diff --git a/src/libtomahawk/resolvers/JSResolver.cpp b/src/libtomahawk/resolvers/JSResolver.cpp index efea147fb..4d6dd43d3 100644 --- a/src/libtomahawk/resolvers/JSResolver.cpp +++ b/src/libtomahawk/resolvers/JSResolver.cpp @@ -116,7 +116,7 @@ JSResolver::capabilities() const } -QString +const QString JSResolver::name() const { Q_D( const JSResolver ); @@ -125,11 +125,14 @@ JSResolver::name() const } -QPixmap -JSResolver::icon() const +const QPixmap +JSResolver::icon( const QSize& size ) const { Q_D( const JSResolver ); + if ( !size.isEmpty() ) + return d->icon.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation ); + return d->icon; } @@ -805,7 +808,7 @@ JSResolver::loadCollections() QPixmap iconPixmap; bool ok = iconPixmap.load( iconPath ); if ( ok && !iconPixmap.isNull() ) - sc->setIcon( QIcon( iconPixmap ) ); + sc->setIcon( iconPixmap ); } m_collections.insert( collection->name(), collection ); diff --git a/src/libtomahawk/resolvers/JSResolver.h b/src/libtomahawk/resolvers/JSResolver.h index b46f438d6..c4fac1920 100644 --- a/src/libtomahawk/resolvers/JSResolver.h +++ b/src/libtomahawk/resolvers/JSResolver.h @@ -55,8 +55,8 @@ public: Capabilities capabilities() const override; - QString name() const override; - QPixmap icon() const override; + const QString name() const override; + const QPixmap icon( const QSize& size ) const override; unsigned int weight() const override; unsigned int timeout() const override; diff --git a/src/libtomahawk/resolvers/Resolver.cpp b/src/libtomahawk/resolvers/Resolver.cpp index 6311a8d53..46c16ba97 100644 --- a/src/libtomahawk/resolvers/Resolver.cpp +++ b/src/libtomahawk/resolvers/Resolver.cpp @@ -15,5 +15,12 @@ * You should have received a copy of the GNU General Public License * along with Tomahawk. If not, see . */ - #include "Resolver.h" + +#include + +const QPixmap +Tomahawk::Resolver::icon( const QSize& ) const +{ + return QPixmap(); +} diff --git a/src/libtomahawk/resolvers/Resolver.h b/src/libtomahawk/resolvers/Resolver.h index 5ce76d05b..e090331b8 100644 --- a/src/libtomahawk/resolvers/Resolver.h +++ b/src/libtomahawk/resolvers/Resolver.h @@ -19,11 +19,11 @@ #ifndef RESOLVER_H #define RESOLVER_H -#include - -#include "DllMacro.h" +#include "../ResultProvider.h" #include "Typedefs.h" +#include "DllMacro.h" +#include // implement this if you can resolve queries to content @@ -37,17 +37,18 @@ namespace Tomahawk { -class DLLEXPORT Resolver : public QObject +class DLLEXPORT Resolver : public QObject, public ResultProvider { Q_OBJECT public: Resolver() {} - virtual QString name() const = 0; virtual unsigned int weight() const = 0; virtual unsigned int timeout() const = 0; + virtual const QPixmap icon( const QSize& size ) const override; + public slots: virtual void resolve( const Tomahawk::query_ptr& query ) = 0; }; diff --git a/src/libtomahawk/resolvers/ScriptCollection.cpp b/src/libtomahawk/resolvers/ScriptCollection.cpp index c9cc57f49..8dc1b8bee 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.cpp +++ b/src/libtomahawk/resolvers/ScriptCollection.cpp @@ -86,20 +86,20 @@ ScriptCollection::itemName() const void -ScriptCollection::setIcon( const QIcon& icon ) +ScriptCollection::setIcon( const QPixmap& icon ) { m_icon = icon; emit changed(); } -QIcon -ScriptCollection::icon() const +const QPixmap +ScriptCollection::icon( const QSize& size ) const { - if( !m_icon.isNull() ) - return m_icon; + if ( !size.isEmpty() && !m_icon.isNull() ) + return m_icon.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation ); - return m_resolver->icon(); + return m_icon; } @@ -107,7 +107,7 @@ QPixmap ScriptCollection::bigIcon() const { QPixmap big = Collection::bigIcon(); - QPixmap base = icon().pixmap( big.size() ); + QPixmap base = icon( big.size() ); if ( !source()->isLocal() ) { diff --git a/src/libtomahawk/resolvers/ScriptCollection.h b/src/libtomahawk/resolvers/ScriptCollection.h index 8843e4aa3..ed7b6af8a 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.h +++ b/src/libtomahawk/resolvers/ScriptCollection.h @@ -29,7 +29,7 @@ #include "Typedefs.h" #include "DllMacro.h" -#include +#include namespace Tomahawk @@ -62,8 +62,8 @@ public: QString itemName() const override; BackendType backendType() const override { return ScriptCollectionType; } - void setIcon( const QIcon& icon ); - QIcon icon() const override; + void setIcon( const QPixmap& icon ); + const QPixmap icon( const QSize& size ) const override; QPixmap bigIcon() const override; void setDescription( const QString& text ); @@ -84,7 +84,7 @@ private: QString m_servicePrettyName; QString m_description; int m_trackCount; - QIcon m_icon; + QPixmap m_icon; }; } //ns diff --git a/src/libtomahawk/resolvers/ScriptResolver.cpp b/src/libtomahawk/resolvers/ScriptResolver.cpp index 6d23587dd..718ea906c 100644 --- a/src/libtomahawk/resolvers/ScriptResolver.cpp +++ b/src/libtomahawk/resolvers/ScriptResolver.cpp @@ -555,6 +555,16 @@ ScriptResolver::setIcon( const QPixmap& icon ) } +const QPixmap +ScriptResolver::icon( const QSize& size ) const +{ + if ( !size.isEmpty() ) + return m_icon.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation ); + + return m_icon; +} + + AccountConfigWidget* ScriptResolver::configUI() const { diff --git a/src/libtomahawk/resolvers/ScriptResolver.h b/src/libtomahawk/resolvers/ScriptResolver.h index 403cbcf89..54230717c 100644 --- a/src/libtomahawk/resolvers/ScriptResolver.h +++ b/src/libtomahawk/resolvers/ScriptResolver.h @@ -44,8 +44,8 @@ public: virtual ~ScriptResolver(); static ExternalResolver* factory( const QString& accountId, const QString& exe, const QStringList& ); - QString name() const Q_DECL_OVERRIDE { return m_name; } - QPixmap icon() const Q_DECL_OVERRIDE { return m_icon; } + const QString name() const Q_DECL_OVERRIDE { return m_name; } + const QPixmap icon( const QSize& size ) const Q_DECL_OVERRIDE; unsigned int weight() const Q_DECL_OVERRIDE { return m_weight; } virtual unsigned int preference() const { return m_preference; } unsigned int timeout() const Q_DECL_OVERRIDE { return m_timeout; } diff --git a/src/tomahawk/sourcetree/items/ScriptCollectionItem.cpp b/src/tomahawk/sourcetree/items/ScriptCollectionItem.cpp index 920b01ca5..0c319db84 100644 --- a/src/tomahawk/sourcetree/items/ScriptCollectionItem.cpp +++ b/src/tomahawk/sourcetree/items/ScriptCollectionItem.cpp @@ -62,7 +62,7 @@ ScriptCollectionItem::tooltip() const QIcon ScriptCollectionItem::icon() const { - return m_collection->icon(); + return m_collection->icon( QSize( 0, 0 ) ); } diff --git a/src/tomahawk/sourcetree/items/SourceItem.cpp b/src/tomahawk/sourcetree/items/SourceItem.cpp index 0ecd33180..1bdc90dbc 100644 --- a/src/tomahawk/sourcetree/items/SourceItem.cpp +++ b/src/tomahawk/sourcetree/items/SourceItem.cpp @@ -378,7 +378,7 @@ SourceItem::performAddCollectionItem( const collection_ptr& collection ) GenericPageItem* i = new GenericPageItem( model(), this, collection->itemName(), - collection->icon(), + collection->icon( QSize( 0, 0 ) ), std::bind( &SourceItem::collectionClicked, this, collection ), std::bind( &SourceItem::getCollectionPage, this, collection ) ); i->setSortValue( -340 );