1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-08 23:26:40 +02:00

Remove crappy QPixmap* stored in Result, and replace with per-size cache

This avoids users having to do their own scaling + caching.
This commit is contained in:
Leo Franchi
2012-09-27 19:03:57 -04:00
parent cf26463e07
commit 22377c6b3d
4 changed files with 35 additions and 15 deletions

View File

@@ -30,12 +30,24 @@
#include "utils/TomahawkUtilsGui.h" #include "utils/TomahawkUtilsGui.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "ExternalResolverGui.h"
using namespace Tomahawk; using namespace Tomahawk;
static QHash< QString, QWeakPointer< Result > > s_results; static QHash< QString, QWeakPointer< Result > > s_results;
static QMutex s_mutex; 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 Tomahawk::result_ptr
Result::get( const QString& url ) Result::get( const QString& url )
@@ -64,7 +76,6 @@ Result::isCached( const QString& url )
Result::Result( const QString& url ) Result::Result( const QString& url )
: QObject() : QObject()
, m_url( url ) , m_url( url )
, m_sourceIcon( 0 )
, m_duration( 0 ) , m_duration( 0 )
, m_bitrate( 0 ) , m_bitrate( 0 )
, m_size( 0 ) , m_size( 0 )
@@ -82,7 +93,6 @@ Result::Result( const QString& url )
Result::~Result() Result::~Result()
{ {
delete m_sourceIcon;
} }
@@ -299,21 +309,37 @@ Result::friendlySource() const
QPixmap QPixmap
Result::sourceIcon() const Result::sourceIcon( const QSize& desiredSize ) const
{ {
if ( collection().isNull() ) if ( collection().isNull() )
{ {
if ( !m_sourceIcon ) const ExternalResolverGui* guiResolver = qobject_cast< ExternalResolverGui* >( m_resolvedBy.data() );
if ( !guiResolver )
return QPixmap(); return QPixmap();
else 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 else
{ {
QPixmap avatar = collection()->source()->avatar( Source::FancyStyle ); return sourceIconCache()->value( key );
}
}
}
else
{
QPixmap avatar = collection()->source()->avatar( Source::FancyStyle, desiredSize );
if ( !avatar ) if ( !avatar )
{ {
avatar = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultSourceAvatar, TomahawkUtils::AvatarInFrame ); avatar = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultSourceAvatar, TomahawkUtils::AvatarInFrame, desiredSize );
} }
return avatar; return avatar;
} }

View File

@@ -24,6 +24,7 @@
#include <QtGui/QPixmap> #include <QtGui/QPixmap>
#include <QtCore/QSharedPointer> #include <QtCore/QSharedPointer>
#include <QtCore/QVariant> #include <QtCore/QVariant>
#include <QMutex>
#include "Typedefs.h" #include "Typedefs.h"
@@ -72,7 +73,7 @@ public:
QString url() const { return m_url; } QString url() const { return m_url; }
QString mimetype() const { return m_mimetype; } QString mimetype() const { return m_mimetype; }
QString friendlySource() const; QString friendlySource() const;
QPixmap sourceIcon() const; QPixmap sourceIcon( const QSize& desiredSize = QSize() ) const;
QString purchaseUrl() const { return m_purchaseUrl; } QString purchaseUrl() const { return m_purchaseUrl; }
QString linkUrl() const { return m_linkUrl; } QString linkUrl() const { return m_linkUrl; }
@@ -105,9 +106,6 @@ public:
void setYear( unsigned int year ) { m_year = year; } void setYear( unsigned int year ) { m_year = year; }
void setDiscNumber( unsigned int discnumber ) { m_discnumber = discnumber; } 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; } QVariantMap attributes() const { return m_attributes; }
void setAttributes( const QVariantMap& map ) { m_attributes = map; updateAttributes(); } void setAttributes( const QVariantMap& map ) { m_attributes = map; updateAttributes(); }
@@ -149,8 +147,6 @@ private:
QString m_mimetype; QString m_mimetype;
QString m_friendlySource; QString m_friendlySource;
QPixmap* m_sourceIcon;
unsigned int m_duration; unsigned int m_duration;
unsigned int m_bitrate; unsigned int m_bitrate;
unsigned int m_size; unsigned int m_size;

View File

@@ -432,7 +432,6 @@ QtScriptResolver::parseResultVariantList( const QVariantList& reslist )
rp->setSize( m.value( "size" ).toUInt() ); rp->setSize( m.value( "size" ).toUInt() );
rp->setRID( uuid() ); rp->setRID( uuid() );
rp->setFriendlySource( name() ); rp->setFriendlySource( name() );
rp->setSourceIcon( new QPixmap( icon() ) );
rp->setPurchaseUrl( m.value( "purchaseUrl" ).toString() ); rp->setPurchaseUrl( m.value( "purchaseUrl" ).toString() );
rp->setLinkUrl( m.value( "linkUrl" ).toString() ); rp->setLinkUrl( m.value( "linkUrl" ).toString() );
rp->setScore( m.value( "score" ).toFloat() ); rp->setScore( m.value( "score" ).toFloat() );

View File

@@ -282,7 +282,6 @@ ScriptResolver::handleMsg( const QByteArray& msg )
rp->setSize( m.value( "size" ).toUInt() ); rp->setSize( m.value( "size" ).toUInt() );
rp->setRID( uuid() ); rp->setRID( uuid() );
rp->setFriendlySource( m_name ); rp->setFriendlySource( m_name );
rp->setSourceIcon( new QPixmap( m_icon ) );
rp->setPurchaseUrl( m.value( "purchaseUrl" ).toString() ); rp->setPurchaseUrl( m.value( "purchaseUrl" ).toString() );
rp->setLinkUrl( m.value( "linkUrl" ).toString() ); rp->setLinkUrl( m.value( "linkUrl" ).toString() );
rp->setYear( m.value( "year").toUInt() ); rp->setYear( m.value( "year").toUInt() );