Krita Source Code Documentation
Loading...
Searching...
No Matches
KisIconUtils Namespace Reference

Enumerations

enum class  Group {
  NoGroup = -1 , Desktop = 0 , FirstGroup = 0 , Toolbar ,
  MainToolbar , Small , Panel , Dialog ,
  LastGroup , User
}
 
enum  StdSizes {
  SizeSmall = 16 , SizeSmallMedium = 22 , SizeMedium = 32 , SizeLarge = 48 ,
  SizeHuge = 64 , SizeEnormous = 128
}
 

Functions

bool adjustIcon (QIcon *icon)
 
QStringList allUniqueLoadedIconNames ()
 
void clearIconCache ()
 
QIcon loadIcon (const QString &name)
 
void updateIcon (QAbstractButton *button)
 
void updateIcon (QAction *action)
 
void updateIcon (QComboBox *comboBox)
 
void updateIcon (QTabBar *tabBar)
 
void updateIconCommon (QObject *object)
 
bool useDarkIcons ()
 

Variables

static QMap< QString, QIcon > s_cache
 
static QMap< qint64, QString > s_icons
 

Enumeration Type Documentation

◆ Group

enum class KisIconUtils::Group
strong
Enumerator
NoGroup 
Desktop 
FirstGroup 
Toolbar 
MainToolbar 
Small 
Panel 
Dialog 
LastGroup 
User 

Definition at line 31 of file kis_icon_utils.h.

◆ StdSizes

Enumerator
SizeSmall 
SizeSmallMedium 
SizeMedium 
SizeLarge 
SizeHuge 
SizeEnormous 

Definition at line 26 of file kis_icon_utils.h.

Function Documentation

◆ adjustIcon()

bool KisIconUtils::adjustIcon ( QIcon * icon)

Definition at line 118 of file kis_icon_utils.cpp.

119{
120 bool result = false;
121
122 QString iconName = icon->name();
123 if (iconName.isNull()) {
124 if (s_icons.contains(icon->cacheKey())) {
125 iconName = s_icons[icon->cacheKey()];
126 } else {
127 return false;
128 }
129 }
130
131 QString realIconName = iconName;
132
133 if (iconName.startsWith("dark_")) {
134 realIconName = iconName.mid(5);
135 }
136
137 if (iconName.startsWith("light_")) {
138 realIconName = iconName.mid(6);
139 }
140
141 if (!realIconName.isNull()) {
142 *icon = loadIcon(realIconName);
143 result = !icon->isNull();
144 s_icons.insert(icon->cacheKey(), iconName);
145 }
146
147 return result;
148}
QIcon loadIcon(const QString &name)
static QMap< qint64, QString > s_icons

References loadIcon(), and s_icons.

◆ allUniqueLoadedIconNames()

KRITAWIDGETUTILS_EXPORT QStringList KisIconUtils::allUniqueLoadedIconNames ( )

Definition at line 221 of file kis_icon_utils.cpp.

222{
223 // Take a snapshot of current values to avoid modification during iteration
224 const auto valuesCopy = s_icons.values();
225
226 QSet<QString> uniq(valuesCopy.cbegin(), valuesCopy.cend());
227 QStringList list = QStringList(uniq.cbegin(), uniq.cend());
228 list.sort();
229
230 return list;
231}
QList< QString > QStringList

References s_icons.

◆ clearIconCache()

KRITAWIDGETUTILS_EXPORT void KisIconUtils::clearIconCache ( )

Definition at line 177 of file kis_icon_utils.cpp.

177 {
178 s_cache.clear();
179}
static QMap< QString, QIcon > s_cache

References s_cache.

◆ loadIcon()

KRITAWIDGETUTILS_EXPORT QIcon KisIconUtils::loadIcon ( const QString & name)

Load a themed icon using its base name. Use it in Krita instead of previous KisIconUtils::loadIcon()

Definition at line 29 of file kis_icon_utils.cpp.

30{
31 QMap<QString, QIcon>::const_iterator cached = s_cache.constFind(name);
32 if (cached != s_cache.constEnd()) {
33 return cached.value();
34 }
35
36 // try load themed icon
37 const char * const prefix = useDarkIcons() ? "dark_" : "light_";
38
39 QString realName = QLatin1String(prefix) + name;
40
41 // Dark and light, no size specified
42 const QStringList names = { ":/pics/" + realName + ".png",
43 ":/pics/" + realName + ".svg",
44 ":/pics/" + realName + ".svgz",
45 ":/pics/" + name + ".png",
46 ":/pics/" + name + ".svg",
47 ":/pics/" + name + ".svgz",
48 ":/" + realName + ".png",
49 ":/" + realName + ".svg",
50 ":/" + realName + ".svgz",
51 ":/" + name,
52 ":/" + name + ".png",
53 ":/" + name + ".svg",
54 ":/" + name + ".svgz"};
55
56 for (const QString &resname : names) {
57 if (QFile(resname).exists()) {
58 QIcon icon(resname);
59 s_icons.insert(icon.cacheKey(), name);
60 s_cache.insert(name, icon);
61 return icon;
62 }
63 }
64
65 // Now check for icons with sizes
66 QStringList sizes = QStringList() << "16_" << "22_" << "32_" << "48_" << "64_" << "128_" << "256_" << "512_" << "1024_";
68 Q_FOREACH (const QString &size, sizes) {
69 const QStringList names = { ":/pics/" + size + realName + ".png",
70 ":/pics/" + size + realName + ".svg",
71 ":/pics/" + size + realName + ".svgz",
72 ":/pics/" + size + name + ".png",
73 ":/pics/" + size + name + ".svg",
74 ":/pics/" + size + name + ".svgz",
75 ":/" + size + realName + ".png",
76 ":/" + size + realName + ".svg",
77 ":/" + size + realName + ".svgz",
78 ":/" + size + name,
79 ":/" + size + name + ".png",
80 ":/" + size + name + ".svg",
81 ":/" + size + name + ".svgz"};
82
83 for (const QString &resname : names) {
84 if (QFile(resname).exists()) {
85 icons << qMakePair(size, resname);
86 }
87 }
88 }
89
90 if (!icons.isEmpty()) {
91 QIcon icon;
92 Q_FOREACH (auto p, icons) {
93 QString sz = p.first;
94 sz.chop(1);
95 int size = sz.toInt();
96 icon.addFile(p.second, QSize(size, size));
97 }
98 s_icons.insert(icon.cacheKey(), name);
99 s_cache.insert(name, icon);
100 return icon;
101 }
102
103 QIcon icon = QIcon::fromTheme(name);
104 //qDebug() << "falling back on QIcon::FromTheme:" << name;
105 s_icons.insert(icon.cacheKey(), name);
106 s_cache.insert(name, icon);
107 return icon;
108}
const Params2D p
const char * name(StandardAction id)
int size(const Forest< T > &forest)
Definition KisForest.h:1232

References p, s_cache, s_icons, and useDarkIcons().

◆ updateIcon() [1/4]

KRITAWIDGETUTILS_EXPORT void KisIconUtils::updateIcon ( QAbstractButton * button)

Update an icon of button according to the current theme

Definition at line 181 of file kis_icon_utils.cpp.

182{
184
185 QIcon icon = button->icon();
186
187 if (adjustIcon(&icon)) {
188 button->setIcon(icon);
189 }
190}
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
QString button(const QWheelEvent &ev)
bool adjustIcon(QIcon *icon)

References adjustIcon(), button(), and KIS_SAFE_ASSERT_RECOVER_RETURN.

◆ updateIcon() [2/4]

KRITAWIDGETUTILS_EXPORT void KisIconUtils::updateIcon ( QAction * action)

Update an icon of action according to the current theme

Definition at line 202 of file kis_icon_utils.cpp.

203{
204 QIcon icon = action->icon();
205
206 if (adjustIcon(&icon)) {
207 action->setIcon(icon);
208 }
209}

References adjustIcon().

◆ updateIcon() [3/4]

KRITAWIDGETUTILS_EXPORT void KisIconUtils::updateIcon ( QComboBox * comboBox)

Update an icon of comboBox according to the current theme

Definition at line 192 of file kis_icon_utils.cpp.

193{
194 for (int i = 0; i < comboBox->count(); i++) {
195 QIcon icon = comboBox->itemIcon(i);
196 if (adjustIcon(&icon)) {
197 comboBox->setItemIcon(i, icon);
198 }
199 }
200}

References adjustIcon().

◆ updateIcon() [4/4]

KRITAWIDGETUTILS_EXPORT void KisIconUtils::updateIcon ( QTabBar * tabBar)

Update the iconst of the tabBar according to the current theme

Definition at line 211 of file kis_icon_utils.cpp.

212{
213 for (int i = 0; i < tabBar->count(); i++) {
214 QIcon icon = tabBar->tabIcon(i);
215 if (adjustIcon(&icon)) {
216 tabBar->setTabIcon(i, icon);
217 }
218 }
219}

References adjustIcon().

◆ updateIconCommon()

KRITAWIDGETUTILS_EXPORT void KisIconUtils::updateIconCommon ( QObject * object)

This function updates an icon of object depending on its type. See updateIcon() overrides to see the supported types

Definition at line 150 of file kis_icon_utils.cpp.

151{
152 QAbstractButton* button = qobject_cast<QAbstractButton*>(object);
153 if (button) {
155 return;
156 }
157
158 QComboBox* comboBox = qobject_cast<QComboBox*>(object);
159 if (comboBox) {
160 updateIcon(comboBox);
161 return;
162 }
163
164 QAction* action = qobject_cast<QAction*>(object);
165 if (action) {
166 updateIcon(action);
167 return;
168 }
169
170 QTabBar* tabBar = qobject_cast<QTabBar*>(object);
171 if (tabBar) {
172 updateIcon(tabBar);
173 return;
174 }
175}
void updateIcon(QAbstractButton *button)

References button(), and updateIcon().

◆ useDarkIcons()

KRITAWIDGETUTILS_EXPORT bool KisIconUtils::useDarkIcons ( )

Should we use a dark or light themed icon? Useful for images that are loaded dynamically like document templates instead of being in static resource files

Definition at line 113 of file kis_icon_utils.cpp.

113 {
114 QColor background = qApp->palette().window().color();
115 return background.value() > 100;
116}

Variable Documentation

◆ s_cache

QMap<QString, QIcon> KisIconUtils::s_cache
static

Definition at line 26 of file kis_icon_utils.cpp.

◆ s_icons

QMap<qint64, QString> KisIconUtils::s_icons
static

Definition at line 27 of file kis_icon_utils.cpp.