Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_icon_utils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "kis_icon_utils.h"
8
9#include <QApplication>
10#include <QAction>
11#include <QAbstractButton>
12#include <QComboBox>
13#include <QTabBar>
14#include <QTabWidget>
15#include <QIcon>
16#include <QFile>
17#include <QPair>
18#include <QDebug>
19
20#include <KoIcon.h>
21#include "kis_debug.h"
22
23namespace KisIconUtils
24{
25
26static QMap<QString, QIcon> s_cache;
27static QMap<qint64, QString> s_icons;
28
29QIcon loadIcon(const QString &name)
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}
109
110
111
112
114 QColor background = qApp->palette().window().color();
115 return background.value() > 100;
116}
117
118bool adjustIcon(QIcon *icon)
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}
149
150void updateIconCommon(QObject *object)
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}
176
178 s_cache.clear();
179}
180
181void updateIcon(QAbstractButton *button)
182{
184
185 QIcon icon = button->icon();
186
187 if (adjustIcon(&icon)) {
188 button->setIcon(icon);
189 }
190}
191
192void updateIcon(QComboBox *comboBox)
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}
201
202void updateIcon(QAction *action)
203{
204 QIcon icon = action->icon();
205
206 if (adjustIcon(&icon)) {
207 action->setIcon(icon);
208 }
209}
210
211void updateIcon(QTabBar *tabBar)
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}
220
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}
232
233}
const Params2D p
QList< QString > QStringList
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
QString button(const QWheelEvent &ev)
QIcon loadIcon(const QString &name)
void updateIconCommon(QObject *object)
static QMap< qint64, QString > s_icons
void updateIcon(QAbstractButton *button)
bool adjustIcon(QIcon *icon)
QStringList allUniqueLoadedIconNames()
static QMap< QString, QIcon > s_cache