mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-04-07 17:42:35 +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:
parent
cf26463e07
commit
22377c6b3d
@ -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;
|
||||
}
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <QtGui/QPixmap>
|
||||
#include <QtCore/QSharedPointer>
|
||||
#include <QtCore/QVariant>
|
||||
#include <QMutex>
|
||||
|
||||
#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;
|
||||
|
@ -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() );
|
||||
|
@ -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() );
|
||||
|
Loading…
x
Reference in New Issue
Block a user