comparison widgets/IconLoader.cpp @ 1609:705d1d979ae4

It turns out icons are loaded repeatedly when recreating menus, causing an actually appreciable slowdown when some cases. Provide a cache
author Chris Cannam
date Thu, 14 May 2020 16:38:05 +0100
parents 8b7f797bca86
children
comparison
equal deleted inserted replaced
1608:6616e1899daa 1609:705d1d979ae4
20 #include <QPainter> 20 #include <QPainter>
21 #include <QPalette> 21 #include <QPalette>
22 #include <QFile> 22 #include <QFile>
23 #include <QSvgRenderer> 23 #include <QSvgRenderer>
24 #include <QSettings> 24 #include <QSettings>
25 #include <QMutex>
26 #include <QMutexLocker>
25 27
26 #include <vector> 28 #include <vector>
27 #include <set> 29 #include <set>
30 #include <map>
28 31
29 #include "base/Debug.h" 32 #include "base/Debug.h"
33 #include "base/Profiler.h"
30 34
31 using namespace std; 35 using namespace std;
32 36
33 static set<QString> autoInvertExceptions { 37 static set<QString> autoInvertExceptions {
34 // These are the icons that look OK in their default colours, even 38 // These are the icons that look OK in their default colours, even
56 "zoom-in", 60 "zoom-in",
57 "zoom-out", 61 "zoom-out",
58 "zoom" 62 "zoom"
59 }; 63 };
60 64
61 static vector<int> sizes { 0, 16, 22, 24, 32, 48, 64, 128 };
62
63 QIcon 65 QIcon
64 IconLoader::load(QString name) 66 IconLoader::load(QString name)
65 { 67 {
68 Profiler profiler("IconLoader::load");
69
70 static QMutex mutex;
71 static map<QString, QIcon> icons;
72 static const vector<int> sizes { 0, 16, 22, 24, 32, 48, 64, 128 };
73
74 QMutexLocker locker(&mutex);
75
76 if (icons.find(name) != icons.end()) {
77 return icons.at(name);
78 }
79
66 QIcon icon; 80 QIcon icon;
67 for (int sz: sizes) { 81 for (int sz: sizes) {
68 QPixmap pmap(loadPixmap(name, sz)); 82 QPixmap pmap(loadPixmap(name, sz));
69 if (!pmap.isNull()) icon.addPixmap(pmap); 83 if (!pmap.isNull()) icon.addPixmap(pmap);
70 } 84 }
85
86 icons[name] = icon;
87
71 return icon; 88 return icon;
72 } 89 }
73 90
74 bool 91 bool
75 IconLoader::shouldInvert() const 92 IconLoader::shouldInvert() const