Skip to content

Commit c9588cd

Browse files
committedApr 12, 2016
Cache theme icons for quick access
1 parent 212b125 commit c9588cd

File tree

3 files changed

+44
-52
lines changed

3 files changed

+44
-52
lines changed
 

‎src/core/qgsapplication.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,22 +412,31 @@ QString QgsApplication::iconPath( const QString& iconFile )
412412

413413
QIcon QgsApplication::getThemeIcon( const QString &theName )
414414
{
415+
QgsApplication* app = static_cast<QgsApplication*>( instance() );
416+
if ( app->mIconCache.contains( theName ) )
417+
return app->mIconCache.value( theName );
418+
419+
QIcon icon;
420+
415421
QString myPreferredPath = activeThemePath() + QDir::separator() + theName;
416422
QString myDefaultPath = defaultThemePath() + QDir::separator() + theName;
417423
if ( QFile::exists( myPreferredPath ) )
418424
{
419-
return QIcon( myPreferredPath );
425+
icon = QIcon( myPreferredPath );
420426
}
421427
else if ( QFile::exists( myDefaultPath ) )
422428
{
423429
//could still return an empty icon if it
424430
//doesnt exist in the default theme either!
425-
return QIcon( myDefaultPath );
431+
icon = QIcon( myDefaultPath );
426432
}
427433
else
428434
{
429-
return QIcon();
435+
icon = QIcon();
430436
}
437+
438+
app->mIconCache.insert( theName, icon );
439+
return icon;
431440
}
432441

433442
// TODO: add some caching mechanism ?

‎src/core/qgsapplication.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ class CORE_EXPORT QgsApplication : public QApplication
407407
static QString sUserName;
408408
static QString sUserFullName;
409409
static QString sPlatformName;
410+
411+
QMap<QString, QIcon> mIconCache;
410412
};
411413

412414
#endif

‎src/core/qgsfield.cpp

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -442,43 +442,43 @@ bool QgsFields::operator==( const QgsFields &other ) const
442442
QgsFields::const_iterator QgsFields::constBegin() const noexcept
443443
{
444444
if ( d->fields.isEmpty() )
445-
return const_iterator();
445+
return const_iterator();
446446

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

450450
QgsFields::const_iterator QgsFields::constEnd() const noexcept
451-
{
452-
if ( d->fields.isEmpty() )
451+
{
452+
if ( d->fields.isEmpty() )
453453
return const_iterator();
454454

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

458-
QgsFields::const_iterator QgsFields::begin() const noexcept
459-
{
460-
if ( d->fields.isEmpty() )
461-
return const_iterator();
458+
QgsFields::const_iterator QgsFields::begin() const noexcept
459+
{
460+
if ( d->fields.isEmpty() )
461+
return const_iterator();
462462

463-
return const_iterator( &d->fields.first() );
464-
}
463+
return const_iterator( &d->fields.first() );
464+
}
465465

466-
QgsFields::const_iterator QgsFields::end() const noexcept
467-
{
468-
if ( d->fields.isEmpty() )
469-
return const_iterator();
466+
QgsFields::const_iterator QgsFields::end() const noexcept
467+
{
468+
if ( d->fields.isEmpty() )
469+
return const_iterator();
470470

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

474-
QgsFields::iterator QgsFields::begin()
475-
{
476-
if ( d->fields.isEmpty() )
477-
return iterator();
474+
QgsFields::iterator QgsFields::begin()
475+
{
476+
if ( d->fields.isEmpty() )
477+
return iterator();
478478

479-
d.detach();
480-
return iterator( &d->fields.first() );
481-
}
479+
d.detach();
480+
return iterator( &d->fields.first() );
481+
}
482482

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

492492
QIcon QgsFields::iconForField( int fieldIdx ) const
493493
{
494-
static QIcon intIcon;
495-
if ( intIcon.isNull() )
496-
intIcon = QgsApplication::getThemeIcon( "/mIconFieldInteger.svg" );
497-
static QIcon floatIcon;
498-
if ( floatIcon.isNull() )
499-
floatIcon = QgsApplication::getThemeIcon( "/mIconFieldFloat.svg" );
500-
static QIcon stringIcon;
501-
if ( stringIcon.isNull() )
502-
stringIcon = QgsApplication::getThemeIcon( "/mIconFieldText.svg" );
503-
static QIcon dateIcon;
504-
if ( dateIcon.isNull() )
505-
dateIcon = QgsApplication::getThemeIcon( "/mIconFieldDate.svg" );
506-
static QIcon dateTimeIcon;
507-
if ( dateTimeIcon.isNull() )
508-
dateTimeIcon = QgsApplication::getThemeIcon( "/mIconFieldDateTime.svg" );
509-
static QIcon timeIcon;
510-
if ( timeIcon.isNull() )
511-
timeIcon = QgsApplication::getThemeIcon( "/mIconFieldTime.svg" );
512-
513494
switch ( d->fields.at( fieldIdx ).field.type() )
514495
{
515496
case QVariant::Int:
516497
case QVariant::UInt:
517498
case QVariant::LongLong:
518499
case QVariant::ULongLong:
519500
{
520-
return intIcon;
501+
return QgsApplication::getThemeIcon( "/mIconFieldInteger.svg" );
521502
}
522503
case QVariant::Double:
523504
{
524-
return floatIcon;
505+
return QgsApplication::getThemeIcon( "/mIconFieldFloat.svg" );
525506
}
526507
case QVariant::String:
527508
{
528-
return stringIcon;
509+
return QgsApplication::getThemeIcon( "/mIconFieldText.svg" );
529510
}
530511
case QVariant::Date:
531512
{
532-
return dateIcon;
513+
return QgsApplication::getThemeIcon( "/mIconFieldDate.svg" );
533514
}
534515
case QVariant::DateTime:
535516
{
536-
return dateTimeIcon;
517+
return QgsApplication::getThemeIcon( "/mIconFieldDateTime.svg" );
537518
}
538519
case QVariant::Time:
539520
{
540-
return timeIcon;
521+
return QgsApplication::getThemeIcon( "/mIconFieldTime.svg" );
541522
}
542523
default:
543524
return QIcon();

0 commit comments

Comments
 (0)
Please sign in to comment.