Skip to content

Commit

Permalink
Use QDir search paths and QImageReader in getIconPixmap
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-w committed Mar 21, 2015
1 parent 5e0d218 commit 9175928
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cmake/modules/BuildPlugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ MACRO(BUILD_PLUGIN PLUGIN_NAME)

LIST(LENGTH PLUGIN_EMBEDDED_RESOURCES ER_LEN)
IF(ER_LEN)
ADD_GEN_QRC(RCC_OUT "${PLUGIN_NAME}.qrc" PREFIX ${PLUGIN_NAME} ${PLUGIN_EMBEDDED_RESOURCES})
ADD_GEN_QRC(RCC_OUT "${PLUGIN_NAME}.qrc" PREFIX artwork/${PLUGIN_NAME} ${PLUGIN_EMBEDDED_RESOURCES})
ENDIF(ER_LEN)

IF(QT5)
Expand Down
6 changes: 6 additions & 0 deletions src/gui/GuiApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "AutomationEditor.h"
#include "BBEditor.h"
#include "ConfigManager.h"
#include "ControllerRackView.h"
#include "FxMixerView.h"
#include "InstrumentTrack.h"
Expand All @@ -40,6 +41,7 @@
#include "SongEditor.h"

#include <QApplication>
#include <QDir>
#include <QSplashScreen>

GuiApplication* GuiApplication::s_instance = nullptr;
Expand All @@ -52,6 +54,10 @@ GuiApplication* GuiApplication::instance()
GuiApplication::GuiApplication()
{
// Init style and palette
QDir::addSearchPath("artwork", ConfigManager::inst()->artworkDir());
QDir::addSearchPath("artwork", ConfigManager::inst()->defaultArtworkDir());
QDir::addSearchPath("artwork", ":/artwork");

LmmsStyle* lmmsstyle = new LmmsStyle();
QApplication::setStyle(lmmsstyle);

Expand Down
81 changes: 26 additions & 55 deletions src/gui/embed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
*/


#include <QDebug>
#include <QImage>
#include <QHash>
#include <QImageReader>
Expand All @@ -46,67 +46,38 @@ namespace

QPixmap getIconPixmap( const char * pixmapName, int width, int height )
{
if( width == -1 || height == -1 )
// Return cached pixmap
QPixmap cached = s_pixmapCache.value( pixmapName );
if( !cached.isNull() )
{
// Return cached pixmap
QPixmap cached = s_pixmapCache.value( pixmapName );
if( !cached.isNull() )
{
return cached;
}

// Or try to load it
QList<QByteArray> formats = QImageReader::supportedImageFormats();
QList<QString> candidates;
QPixmap pixmap;
QString name;
int i;

for ( i = 0; i < formats.size() && pixmap.isNull(); ++i )
{
candidates << QString( pixmapName ) + "." + formats.at( i ).data();
}
return cached;
}

#ifdef PLUGIN_NAME
for ( i = 0; i < candidates.size() && pixmap.isNull(); ++i ) {
name = candidates.at( i );
pixmap = QPixmap( ConfigManager::inst()->artworkDir() + "plugins/" +
STRINGIFY( PLUGIN_NAME ) + "_" + name );
}
#endif
for ( i = 0; i < candidates.size() && pixmap.isNull(); ++i ) {
name = candidates.at( i );
pixmap = QPixmap( ConfigManager::inst()->artworkDir() + name );
}

// nothing found, so look in default-artwork-dir
for ( i = 0; i < candidates.size() && pixmap.isNull(); ++i ) {
name = candidates.at( i );
pixmap = QPixmap( ConfigManager::inst()->defaultArtworkDir() + name );
}

for ( i = 0; i < candidates.size() && pixmap.isNull(); ++i ) {
name = candidates.at( i );
#ifdef PLUGIN_NAME
pixmap = QPixmap( ":/" STRINGIFY( PLUGIN_NAME ) "/" + name );
QString name(QString("artwork:%1/%2").arg(STRINGIFY(PLUGIN_NAME), pixmapName));
#else
pixmap = QPixmap( ":/" + name );
QString name(QString("artwork:%1").arg(pixmapName));
#endif
}

// Fallback
if( pixmap.isNull() )
{
pixmap = QPixmap( 1, 1 );
}
// Save to cache and return
s_pixmapCache.insert( pixmapName, pixmap );
return pixmap;
QImageReader reader(name);

if (width > 0 && height > 0)
{
reader.setScaledSize(QSize(width, height));
}

return getIconPixmap( pixmapName ).
scaled( width, height, Qt::IgnoreAspectRatio,
Qt::SmoothTransformation );
QImage image = reader.read();
if (image.isNull())
{
qWarning().nospace() << "Error loading icon pixmap " << name << ": " <<
reader.errorString().toLocal8Bit().data();
return QPixmap(1,1);
}

QPixmap pixmap = QPixmap::fromImage(image);

// Save to cache and return
s_pixmapCache.insert( pixmapName, pixmap );
return pixmap;
}


Expand Down

0 comments on commit 9175928

Please sign in to comment.