Skip to content

Commit

Permalink
Better error handling for missing icons
Browse files Browse the repository at this point in the history
  • Loading branch information
shundhammer committed Nov 6, 2018
1 parent ceee025 commit 289677f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
48 changes: 39 additions & 9 deletions src/YQIconPool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#define YUILogComponent "qt-ui"
#include "YUILog.h"
#include "utf8.h"

#include "YQIconPool.h"

using std::endl;



YQIconPool * YQIconPool::_iconPool = 0;
Expand Down Expand Up @@ -113,19 +116,46 @@ YQIconPool::cachedIcon( const QString icon_name, const bool enabled )

if ( !iconPixmap )
{
if ( QIcon::hasThemeIcon(icon_name) )
{
QIcon icon = QIcon::fromTheme(icon_name, QIcon( ":/" + icon_name ));
iconPixmap = icon.pixmap(QSize(16,16), enabled ? QIcon::Normal : QIcon::Disabled);
}
else
iconPixmap = loadIcon( icon_name, enabled );

if ( !iconPixmap )
{
QIcon icon = QIcon( ":/" + icon_name );
iconPixmap = icon.pixmap(QSize(16,16), enabled ? QIcon::Normal : QIcon::Disabled);
// Create an icon for the cache to avoid more than one complaint
// and to have a clearly visible error icon (a small red square)
iconPixmap = QPixmap( 8, 8 );
iconPixmap.fill( Qt::red );
}
}

_iconCache.insert( icon_name + enabled, iconPixmap);
_iconCache.insert( icon_name + enabled, iconPixmap );

return iconPixmap;
}


QPixmap
YQIconPool::loadIcon( const QString icon_name, const bool enabled )
{
QPixmap iconPixmap = _iconCache[ icon_name + enabled ];

if ( QIcon::hasThemeIcon( icon_name ) )
{
yuiDebug() << "Loading theme icon " << icon_name << endl;

QIcon icon = QIcon::fromTheme( icon_name, QIcon( ":/" + icon_name ) );
iconPixmap = icon.pixmap( QSize( 16, 16 ), enabled ? QIcon::Normal : QIcon::Disabled );
}
else
{
yuiDebug() << "Loading built-in icon " << icon_name << endl;

QIcon icon = QIcon( ":/" + icon_name );
iconPixmap = icon.pixmap( QSize( 16, 16 ), enabled ? QIcon::Normal : QIcon::Disabled );
}

if ( !iconPixmap )
yuiError() << "Could not load icon " << icon_name << endl;

return iconPixmap;
}

20 changes: 14 additions & 6 deletions src/YQIconPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ class YQIconPool
static QPixmap disabledPkgProtected();
static QPixmap disabledPkgTaboo();
static QPixmap disabledPkgUpdate();

static QPixmap normalPkgConflict();

static QPixmap treePlus();
static QPixmap treeMinus();

static QPixmap warningSign();
static QPixmap pkgSatisfied();

Expand All @@ -92,12 +92,20 @@ class YQIconPool
static YQIconPool * iconPool();

/**
* Return the cached icon for 'xpm_data' ( an included XPM file ).
* If the icon isn't in the cache yet, create it and store it in the
* cache.
* Return the cached icon for 'icon_name'. If the icon isn't in the cache
* yet, load it and store it in the cache.
*
* Return a red square as an error icon if there is no icon by that name.
**/
QPixmap cachedIcon(const QString icon_name, const bool enabled );

/**
* Load the icon for 'icon_name' from the icon theme or, if that fails,
* from the compiled-in icons (using the Qt resource system). Return a null
* pixmap if there is no such icon.
**/
QPixmap loadIcon( const QString icon_name, const bool enabled );

private:

/**
Expand All @@ -111,7 +119,7 @@ class YQIconPool
**/
virtual ~YQIconPool();


//
// Data members
//
Expand Down

0 comments on commit 289677f

Please sign in to comment.