Skip to content
Permalink
Browse files

Cache theme icons for quick access

  • Loading branch information
m-kuhn committed Apr 12, 2016
1 parent 212b125 commit c9588cd56ea2de9aa004d975a19db3b6aec4516d
Showing with 44 additions and 52 deletions.
  1. +12 −3 src/core/qgsapplication.cpp
  2. +2 −0 src/core/qgsapplication.h
  3. +30 −49 src/core/qgsfield.cpp
@@ -412,22 +412,31 @@ QString QgsApplication::iconPath( const QString& iconFile )

QIcon QgsApplication::getThemeIcon( const QString &theName )
{
QgsApplication* app = static_cast<QgsApplication*>( instance() );
if ( app->mIconCache.contains( theName ) )
return app->mIconCache.value( theName );

QIcon icon;

QString myPreferredPath = activeThemePath() + QDir::separator() + theName;
QString myDefaultPath = defaultThemePath() + QDir::separator() + theName;
if ( QFile::exists( myPreferredPath ) )
{
return QIcon( myPreferredPath );
icon = QIcon( myPreferredPath );
}
else if ( QFile::exists( myDefaultPath ) )
{
//could still return an empty icon if it
//doesnt exist in the default theme either!
return QIcon( myDefaultPath );
icon = QIcon( myDefaultPath );
}
else
{
return QIcon();
icon = QIcon();
}

app->mIconCache.insert( theName, icon );
return icon;
}

// TODO: add some caching mechanism ?
@@ -407,6 +407,8 @@ class CORE_EXPORT QgsApplication : public QApplication
static QString sUserName;
static QString sUserFullName;
static QString sPlatformName;

QMap<QString, QIcon> mIconCache;
};

#endif
@@ -442,43 +442,43 @@ bool QgsFields::operator==( const QgsFields &other ) const
QgsFields::const_iterator QgsFields::constBegin() const noexcept
{
if ( d->fields.isEmpty() )
return const_iterator();
return const_iterator();

return const_iterator( &d->fields.first() );
}

QgsFields::const_iterator QgsFields::constEnd() const noexcept
{
if ( d->fields.isEmpty() )
{
if ( d->fields.isEmpty() )
return const_iterator();

return const_iterator( &d->fields.last() + 1 );
}
return const_iterator( &d->fields.last() + 1 );
}

QgsFields::const_iterator QgsFields::begin() const noexcept
{
if ( d->fields.isEmpty() )
return const_iterator();
QgsFields::const_iterator QgsFields::begin() const noexcept
{
if ( d->fields.isEmpty() )
return const_iterator();

return const_iterator( &d->fields.first() );
}
return const_iterator( &d->fields.first() );
}

QgsFields::const_iterator QgsFields::end() const noexcept
{
if ( d->fields.isEmpty() )
return const_iterator();
QgsFields::const_iterator QgsFields::end() const noexcept
{
if ( d->fields.isEmpty() )
return const_iterator();

return const_iterator( &d->fields.last() + 1 );
}
return const_iterator( &d->fields.last() + 1 );
}

QgsFields::iterator QgsFields::begin()
{
if ( d->fields.isEmpty() )
return iterator();
QgsFields::iterator QgsFields::begin()
{
if ( d->fields.isEmpty() )
return iterator();

d.detach();
return iterator( &d->fields.first() );
}
d.detach();
return iterator( &d->fields.first() );
}

QgsFields::iterator QgsFields::end()
{
@@ -491,53 +491,34 @@ QgsFields::iterator QgsFields::end()

QIcon QgsFields::iconForField( int fieldIdx ) const
{
static QIcon intIcon;
if ( intIcon.isNull() )
intIcon = QgsApplication::getThemeIcon( "/mIconFieldInteger.svg" );
static QIcon floatIcon;
if ( floatIcon.isNull() )
floatIcon = QgsApplication::getThemeIcon( "/mIconFieldFloat.svg" );
static QIcon stringIcon;
if ( stringIcon.isNull() )
stringIcon = QgsApplication::getThemeIcon( "/mIconFieldText.svg" );
static QIcon dateIcon;
if ( dateIcon.isNull() )
dateIcon = QgsApplication::getThemeIcon( "/mIconFieldDate.svg" );
static QIcon dateTimeIcon;
if ( dateTimeIcon.isNull() )
dateTimeIcon = QgsApplication::getThemeIcon( "/mIconFieldDateTime.svg" );
static QIcon timeIcon;
if ( timeIcon.isNull() )
timeIcon = QgsApplication::getThemeIcon( "/mIconFieldTime.svg" );

switch ( d->fields.at( fieldIdx ).field.type() )
{
case QVariant::Int:
case QVariant::UInt:
case QVariant::LongLong:
case QVariant::ULongLong:
{
return intIcon;
return QgsApplication::getThemeIcon( "/mIconFieldInteger.svg" );
}
case QVariant::Double:
{
return floatIcon;
return QgsApplication::getThemeIcon( "/mIconFieldFloat.svg" );
}
case QVariant::String:
{
return stringIcon;
return QgsApplication::getThemeIcon( "/mIconFieldText.svg" );
}
case QVariant::Date:
{
return dateIcon;
return QgsApplication::getThemeIcon( "/mIconFieldDate.svg" );
}
case QVariant::DateTime:
{
return dateTimeIcon;
return QgsApplication::getThemeIcon( "/mIconFieldDateTime.svg" );
}
case QVariant::Time:
{
return timeIcon;
return QgsApplication::getThemeIcon( "/mIconFieldTime.svg" );
}
default:
return QIcon();

0 comments on commit c9588cd

Please sign in to comment.
You can’t perform that action at this time.