From 22377c6b3d924d513243b9b203927b2b29628640 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Thu, 27 Sep 2012 19:03:57 -0400 Subject: [PATCH] Remove crappy QPixmap* stored in Result, and replace with per-size cache This avoids users having to do their own scaling + caching. --- src/libtomahawk/Result.cpp | 40 +++++++++++++++---- src/libtomahawk/Result.h | 8 +--- .../resolvers/QtScriptResolver.cpp | 1 - src/libtomahawk/resolvers/ScriptResolver.cpp | 1 - 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/libtomahawk/Result.cpp b/src/libtomahawk/Result.cpp index dd6827903..2db528c8e 100644 --- a/src/libtomahawk/Result.cpp +++ b/src/libtomahawk/Result.cpp @@ -30,12 +30,24 @@ #include "utils/TomahawkUtilsGui.h" #include "utils/Logger.h" +#include "ExternalResolverGui.h" using namespace Tomahawk; static QHash< QString, QWeakPointer< Result > > s_results; static QMutex s_mutex; +typedef QMap< QString, QPixmap > SourceIconCache; +Q_GLOBAL_STATIC( SourceIconCache, sourceIconCache ); +static QMutex s_sourceIconMutex; + +inline QString sourceCacheKey( Resolver* resolver, const QSize& size ) +{ + QString str; + QTextStream stream( &str ); + stream << resolver << size.width() << size.height(); + return str; +} Tomahawk::result_ptr Result::get( const QString& url ) @@ -64,7 +76,6 @@ Result::isCached( const QString& url ) Result::Result( const QString& url ) : QObject() , m_url( url ) - , m_sourceIcon( 0 ) , m_duration( 0 ) , m_bitrate( 0 ) , m_size( 0 ) @@ -82,7 +93,6 @@ Result::Result( const QString& url ) Result::~Result() { - delete m_sourceIcon; } @@ -299,21 +309,37 @@ Result::friendlySource() const QPixmap -Result::sourceIcon() const +Result::sourceIcon( const QSize& desiredSize ) const { if ( collection().isNull() ) { - if ( !m_sourceIcon ) + const ExternalResolverGui* guiResolver = qobject_cast< ExternalResolverGui* >( m_resolvedBy.data() ); + if ( !guiResolver ) return QPixmap(); else - return *m_sourceIcon; + { + QMutexLocker l( &s_sourceIconMutex ); + const QString key = sourceCacheKey( m_resolvedBy.data(), desiredSize ); + if ( !sourceIconCache()->contains( key ) ) + { + QPixmap pixmap = guiResolver->icon(); + if ( !desiredSize.isEmpty() ) + pixmap = pixmap.scaled( desiredSize, Qt::KeepAspectRatio, Qt::SmoothTransformation ); + sourceIconCache()->insert( key, pixmap ); + return pixmap; + } + else + { + return sourceIconCache()->value( key ); + } + } } else { - QPixmap avatar = collection()->source()->avatar( Source::FancyStyle ); + QPixmap avatar = collection()->source()->avatar( Source::FancyStyle, desiredSize ); if ( !avatar ) { - avatar = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultSourceAvatar, TomahawkUtils::AvatarInFrame ); + avatar = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultSourceAvatar, TomahawkUtils::AvatarInFrame, desiredSize ); } return avatar; } diff --git a/src/libtomahawk/Result.h b/src/libtomahawk/Result.h index 38a47eac4..217578324 100644 --- a/src/libtomahawk/Result.h +++ b/src/libtomahawk/Result.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "Typedefs.h" @@ -72,7 +73,7 @@ public: QString url() const { return m_url; } QString mimetype() const { return m_mimetype; } QString friendlySource() const; - QPixmap sourceIcon() const; + QPixmap sourceIcon( const QSize& desiredSize = QSize() ) const; QString purchaseUrl() const { return m_purchaseUrl; } QString linkUrl() const { return m_linkUrl; } @@ -105,9 +106,6 @@ public: void setYear( unsigned int year ) { m_year = year; } void setDiscNumber( unsigned int discnumber ) { m_discnumber = discnumber; } - // Takes ownership of the pixmap - void setSourceIcon( QPixmap* i ) { m_sourceIcon = i; } - QVariantMap attributes() const { return m_attributes; } void setAttributes( const QVariantMap& map ) { m_attributes = map; updateAttributes(); } @@ -149,8 +147,6 @@ private: QString m_mimetype; QString m_friendlySource; - QPixmap* m_sourceIcon; - unsigned int m_duration; unsigned int m_bitrate; unsigned int m_size; diff --git a/src/libtomahawk/resolvers/QtScriptResolver.cpp b/src/libtomahawk/resolvers/QtScriptResolver.cpp index 081b8efc3..5548c0e49 100644 --- a/src/libtomahawk/resolvers/QtScriptResolver.cpp +++ b/src/libtomahawk/resolvers/QtScriptResolver.cpp @@ -432,7 +432,6 @@ QtScriptResolver::parseResultVariantList( const QVariantList& reslist ) rp->setSize( m.value( "size" ).toUInt() ); rp->setRID( uuid() ); rp->setFriendlySource( name() ); - rp->setSourceIcon( new QPixmap( icon() ) ); rp->setPurchaseUrl( m.value( "purchaseUrl" ).toString() ); rp->setLinkUrl( m.value( "linkUrl" ).toString() ); rp->setScore( m.value( "score" ).toFloat() ); diff --git a/src/libtomahawk/resolvers/ScriptResolver.cpp b/src/libtomahawk/resolvers/ScriptResolver.cpp index 88e6573a1..98a2a183b 100644 --- a/src/libtomahawk/resolvers/ScriptResolver.cpp +++ b/src/libtomahawk/resolvers/ScriptResolver.cpp @@ -282,7 +282,6 @@ ScriptResolver::handleMsg( const QByteArray& msg ) rp->setSize( m.value( "size" ).toUInt() ); rp->setRID( uuid() ); rp->setFriendlySource( m_name ); - rp->setSourceIcon( new QPixmap( m_icon ) ); rp->setPurchaseUrl( m.value( "purchaseUrl" ).toString() ); rp->setLinkUrl( m.value( "linkUrl" ).toString() ); rp->setYear( m.value( "year").toUInt() );