1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-09 07:36:48 +02:00

Allow script resolvers to choose icons for their scriptcollections.

This commit is contained in:
Teo Mrnjavac
2013-03-05 10:30:19 +01:00
parent 7f82799a72
commit 5364dfaf40
4 changed files with 79 additions and 7 deletions

View File

@@ -39,6 +39,7 @@
#include "config.h"
#include <QImageReader>
#include <QMessageBox>
#include <QNetworkRequest>
#include <QNetworkReply>
@@ -925,16 +926,71 @@ QtScriptResolver::loadCollections()
sc->setTrackCount( trackCount );
}
if ( collectionInfo.contains( "iconfile" ) )
{
bool ok = false;
QString iconPath = QFileInfo( filePath() ).path() + "/"
+ collectionInfo.value( "iconfile" ).toString();
QPixmap iconPixmap;
ok = iconPixmap.load( iconPath );
if ( ok && !iconPixmap.isNull() )
sc->setIcon( QIcon( iconPixmap ) );
}
Tomahawk::collection_ptr collection( sc );
m_collections.insert( collection->name(), collection );
emit collectionAdded( collection );
if ( collectionInfo.contains( "iconurl" ) )
{
QString iconUrlString = collectionInfo.value( "iconurl" ).toString();
if ( !iconUrlString.isEmpty() )
{
QUrl iconUrl = QUrl::fromEncoded( iconUrlString.toLatin1() );
if ( iconUrl.isValid() )
{
QNetworkRequest req( iconUrl );
tDebug() << "Creating a QNetworkReply with url:" << req.url().toString();
QNetworkReply* reply = TomahawkUtils::nam()->get( req );
reply->setProperty( "collectionName", collection->name() );
connect( reply, SIGNAL( finished() ),
this, SLOT( onCollectionIconFetched() ) );
}
}
}
//TODO: implement multiple collections from a resolver
}
}
void
QtScriptResolver::onCollectionIconFetched()
{
QNetworkReply* reply = qobject_cast< QNetworkReply* >( sender() );
if ( reply != 0 )
{
Tomahawk::collection_ptr collection;
collection = m_collections.value( reply->property( "collectionName" ).toString() );
if ( !collection.isNull() )
{
if ( reply->error() == QNetworkReply::NoError )
{
QImageReader imageReader( reply );
QPixmap collectionIcon = QPixmap::fromImageReader( &imageReader );
if ( !collectionIcon.isNull() )
qobject_cast< Tomahawk::ScriptCollection* >( collection.data() )->setIcon( collectionIcon );
}
}
reply->deleteLater();
}
}
QVariantMap
QtScriptResolver::resolverSettings()
{

View File

@@ -181,6 +181,9 @@ public slots:
signals:
void stopped();
private slots:
void onCollectionIconFetched();
private:
void init();

View File

@@ -27,7 +27,6 @@
#include "resolvers/ScriptCommand_AllAlbums.h"
#include "resolvers/ScriptCommand_AllTracks.h"
#include <QIcon>
#include <QPainter>
using namespace Tomahawk;
@@ -45,6 +44,12 @@ ScriptCollection::ScriptCollection( const source_ptr& source,
m_resolver = resolver;
m_servicePrettyName = m_resolver->name();
ExternalResolverGui* gResolver = qobject_cast< ExternalResolverGui* >( m_resolver );
if ( gResolver )
{
m_icon = gResolver->icon();
}
}
@@ -77,15 +82,18 @@ ScriptCollection::itemName() const
}
void
ScriptCollection::setIcon( const QIcon& icon )
{
m_icon = icon;
emit changed();
}
QIcon
ScriptCollection::icon() const
{
ExternalResolverGui* gResolver = qobject_cast< ExternalResolverGui* >( m_resolver );
if ( gResolver )
{
return gResolver->icon();
}
return QIcon();
return m_icon;
}

View File

@@ -28,6 +28,8 @@
#include "Typedefs.h"
#include "DllMacro.h"
#include <QIcon>
namespace Tomahawk
{
@@ -55,6 +57,8 @@ public:
virtual QString prettyName() const;
virtual QString itemName() const;
virtual BackendType backendType() const { return ScriptCollectionType; }
virtual void setIcon( const QIcon& icon );
virtual QIcon icon() const;
virtual QPixmap bigIcon() const;
@@ -75,6 +79,7 @@ private:
QString m_servicePrettyName;
QString m_description;
int m_trackCount;
QIcon m_icon;
};
} //ns