Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/image/qiconloader.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
16 | ** | - | ||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | #ifndef QT_NO_ICON | - | ||||||||||||||||||
34 | #include <private/qiconloader_p.h> | - | ||||||||||||||||||
35 | - | |||||||||||||||||||
36 | #include <private/qguiapplication_p.h> | - | ||||||||||||||||||
37 | #include <private/qicon_p.h> | - | ||||||||||||||||||
38 | - | |||||||||||||||||||
39 | #include <QtGui/QIconEnginePlugin> | - | ||||||||||||||||||
40 | #include <QtGui/QPixmapCache> | - | ||||||||||||||||||
41 | #include <qpa/qplatformtheme.h> | - | ||||||||||||||||||
42 | #include <QtGui/QIconEngine> | - | ||||||||||||||||||
43 | #include <QtGui/QPalette> | - | ||||||||||||||||||
44 | #include <QtCore/QList> | - | ||||||||||||||||||
45 | #include <QtCore/QDir> | - | ||||||||||||||||||
46 | #include <QtCore/QSettings> | - | ||||||||||||||||||
47 | #include <QtGui/QPainter> | - | ||||||||||||||||||
48 | - | |||||||||||||||||||
49 | #include <private/qhexstring_p.h> | - | ||||||||||||||||||
50 | - | |||||||||||||||||||
51 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
52 | - | |||||||||||||||||||
53 | Q_GLOBAL_STATIC(QIconLoader, iconLoaderInstance) never executed: end of block never executed: guard.store(QtGlobalStatic::Destroyed); never executed: return &holder.value;
| 0 | ||||||||||||||||||
54 | - | |||||||||||||||||||
55 | /* Theme to use in last resort, if the theme does not have the icon, neither the parents */ | - | ||||||||||||||||||
56 | static QString fallbackTheme() | - | ||||||||||||||||||
57 | { | - | ||||||||||||||||||
58 | if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
| 0 | ||||||||||||||||||
59 | const QVariant themeHint = theme->themeHint(QPlatformTheme::SystemIconFallbackThemeName); | - | ||||||||||||||||||
60 | if (themeHint.isValid())
| 0 | ||||||||||||||||||
61 | return themeHint.toString(); never executed: return themeHint.toString(); | 0 | ||||||||||||||||||
62 | } never executed: end of block | 0 | ||||||||||||||||||
63 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
64 | } | - | ||||||||||||||||||
65 | - | |||||||||||||||||||
66 | QIconLoader::QIconLoader() : | - | ||||||||||||||||||
67 | m_themeKey(1), m_supportsSvg(false), m_initialized(false) | - | ||||||||||||||||||
68 | { | - | ||||||||||||||||||
69 | } never executed: end of block | 0 | ||||||||||||||||||
70 | - | |||||||||||||||||||
71 | static inline QString systemThemeName() | - | ||||||||||||||||||
72 | { | - | ||||||||||||||||||
73 | if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
| 0 | ||||||||||||||||||
74 | const QVariant themeHint = theme->themeHint(QPlatformTheme::SystemIconThemeName); | - | ||||||||||||||||||
75 | if (themeHint.isValid())
| 0 | ||||||||||||||||||
76 | return themeHint.toString(); never executed: return themeHint.toString(); | 0 | ||||||||||||||||||
77 | } never executed: end of block | 0 | ||||||||||||||||||
78 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
79 | } | - | ||||||||||||||||||
80 | - | |||||||||||||||||||
81 | static inline QStringList systemIconSearchPaths() | - | ||||||||||||||||||
82 | { | - | ||||||||||||||||||
83 | if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
| 0 | ||||||||||||||||||
84 | const QVariant themeHint = theme->themeHint(QPlatformTheme::IconThemeSearchPaths); | - | ||||||||||||||||||
85 | if (themeHint.isValid())
| 0 | ||||||||||||||||||
86 | return themeHint.toStringList(); never executed: return themeHint.toStringList(); | 0 | ||||||||||||||||||
87 | } never executed: end of block | 0 | ||||||||||||||||||
88 | return QStringList(); never executed: return QStringList(); | 0 | ||||||||||||||||||
89 | } | - | ||||||||||||||||||
90 | - | |||||||||||||||||||
91 | #ifndef QT_NO_LIBRARY | - | ||||||||||||||||||
92 | extern QFactoryLoader *qt_iconEngineFactoryLoader(); // qicon.cpp | - | ||||||||||||||||||
93 | #endif | - | ||||||||||||||||||
94 | - | |||||||||||||||||||
95 | void QIconLoader::ensureInitialized() | - | ||||||||||||||||||
96 | { | - | ||||||||||||||||||
97 | if (!m_initialized) {
| 0 | ||||||||||||||||||
98 | m_initialized = true; | - | ||||||||||||||||||
99 | - | |||||||||||||||||||
100 | Q_ASSERT(qApp); | - | ||||||||||||||||||
101 | - | |||||||||||||||||||
102 | m_systemTheme = systemThemeName(); | - | ||||||||||||||||||
103 | - | |||||||||||||||||||
104 | if (m_systemTheme.isEmpty())
| 0 | ||||||||||||||||||
105 | m_systemTheme = fallbackTheme(); never executed: m_systemTheme = fallbackTheme(); | 0 | ||||||||||||||||||
106 | #ifndef QT_NO_LIBRARY | - | ||||||||||||||||||
107 | if (qt_iconEngineFactoryLoader()->keyMap().key(QLatin1String("svg"), -1) != -1)
| 0 | ||||||||||||||||||
108 | m_supportsSvg = true; never executed: m_supportsSvg = true; | 0 | ||||||||||||||||||
109 | #endif //QT_NO_LIBRARY | - | ||||||||||||||||||
110 | } never executed: end of block | 0 | ||||||||||||||||||
111 | } never executed: end of block | 0 | ||||||||||||||||||
112 | - | |||||||||||||||||||
113 | QIconLoader *QIconLoader::instance() | - | ||||||||||||||||||
114 | { | - | ||||||||||||||||||
115 | iconLoaderInstance()->ensureInitialized(); | - | ||||||||||||||||||
116 | return iconLoaderInstance(); never executed: return iconLoaderInstance(); | 0 | ||||||||||||||||||
117 | } | - | ||||||||||||||||||
118 | - | |||||||||||||||||||
119 | // Queries the system theme and invalidates existing | - | ||||||||||||||||||
120 | // icons if the theme has changed. | - | ||||||||||||||||||
121 | void QIconLoader::updateSystemTheme() | - | ||||||||||||||||||
122 | { | - | ||||||||||||||||||
123 | // Only change if this is not explicitly set by the user | - | ||||||||||||||||||
124 | if (m_userTheme.isEmpty()) {
| 0 | ||||||||||||||||||
125 | QString theme = systemThemeName(); | - | ||||||||||||||||||
126 | if (theme.isEmpty())
| 0 | ||||||||||||||||||
127 | theme = fallbackTheme(); never executed: theme = fallbackTheme(); | 0 | ||||||||||||||||||
128 | if (theme != m_systemTheme) {
| 0 | ||||||||||||||||||
129 | m_systemTheme = theme; | - | ||||||||||||||||||
130 | invalidateKey(); | - | ||||||||||||||||||
131 | } never executed: end of block | 0 | ||||||||||||||||||
132 | } never executed: end of block | 0 | ||||||||||||||||||
133 | } never executed: end of block | 0 | ||||||||||||||||||
134 | - | |||||||||||||||||||
135 | void QIconLoader::setThemeName(const QString &themeName) | - | ||||||||||||||||||
136 | { | - | ||||||||||||||||||
137 | m_userTheme = themeName; | - | ||||||||||||||||||
138 | invalidateKey(); | - | ||||||||||||||||||
139 | } never executed: end of block | 0 | ||||||||||||||||||
140 | - | |||||||||||||||||||
141 | void QIconLoader::setThemeSearchPath(const QStringList &searchPaths) | - | ||||||||||||||||||
142 | { | - | ||||||||||||||||||
143 | m_iconDirs = searchPaths; | - | ||||||||||||||||||
144 | themeList.clear(); | - | ||||||||||||||||||
145 | invalidateKey(); | - | ||||||||||||||||||
146 | } never executed: end of block | 0 | ||||||||||||||||||
147 | - | |||||||||||||||||||
148 | QStringList QIconLoader::themeSearchPaths() const | - | ||||||||||||||||||
149 | { | - | ||||||||||||||||||
150 | if (m_iconDirs.isEmpty()) {
| 0 | ||||||||||||||||||
151 | m_iconDirs = systemIconSearchPaths(); | - | ||||||||||||||||||
152 | // Always add resource directory as search path | - | ||||||||||||||||||
153 | m_iconDirs.append(QLatin1String(":/icons")); | - | ||||||||||||||||||
154 | } never executed: end of block | 0 | ||||||||||||||||||
155 | return m_iconDirs; never executed: return m_iconDirs; | 0 | ||||||||||||||||||
156 | } | - | ||||||||||||||||||
157 | - | |||||||||||||||||||
158 | QIconTheme::QIconTheme(const QString &themeName) | - | ||||||||||||||||||
159 | : m_valid(false) | - | ||||||||||||||||||
160 | { | - | ||||||||||||||||||
161 | QFile themeIndex; | - | ||||||||||||||||||
162 | - | |||||||||||||||||||
163 | QStringList iconDirs = QIcon::themeSearchPaths(); | - | ||||||||||||||||||
164 | for ( int i = 0 ; i < iconDirs.size() ; ++i) {
| 0 | ||||||||||||||||||
165 | QDir iconDir(iconDirs[i]); | - | ||||||||||||||||||
166 | QString themeDir = iconDir.path() + QLatin1Char('/') + themeName; | - | ||||||||||||||||||
167 | QFileInfo themeDirInfo(themeDir); | - | ||||||||||||||||||
168 | - | |||||||||||||||||||
169 | if (themeDirInfo.isDir())
| 0 | ||||||||||||||||||
170 | m_contentDirs << themeDir; never executed: m_contentDirs << themeDir; | 0 | ||||||||||||||||||
171 | - | |||||||||||||||||||
172 | if (!m_valid) {
| 0 | ||||||||||||||||||
173 | themeIndex.setFileName(themeDir + QLatin1String("/index.theme")); | - | ||||||||||||||||||
174 | if (themeIndex.exists())
| 0 | ||||||||||||||||||
175 | m_valid = true; never executed: m_valid = true; | 0 | ||||||||||||||||||
176 | } never executed: end of block | 0 | ||||||||||||||||||
177 | } never executed: end of block | 0 | ||||||||||||||||||
178 | #ifndef QT_NO_SETTINGS | - | ||||||||||||||||||
179 | if (themeIndex.exists()) {
| 0 | ||||||||||||||||||
180 | const QSettings indexReader(themeIndex.fileName(), QSettings::IniFormat); | - | ||||||||||||||||||
181 | QStringListIterator keyIterator(indexReader.allKeys()); | - | ||||||||||||||||||
182 | while (keyIterator.hasNext()) {
| 0 | ||||||||||||||||||
183 | - | |||||||||||||||||||
184 | const QString key = keyIterator.next(); | - | ||||||||||||||||||
185 | if (key.endsWith(QLatin1String("/Size"))) {
| 0 | ||||||||||||||||||
186 | // Note the QSettings ini-format does not accept | - | ||||||||||||||||||
187 | // slashes in key names, hence we have to cheat | - | ||||||||||||||||||
188 | if (int size = indexReader.value(key).toInt()) {
| 0 | ||||||||||||||||||
189 | QString directoryKey = key.left(key.size() - 5); | - | ||||||||||||||||||
190 | QIconDirInfo dirInfo(directoryKey); | - | ||||||||||||||||||
191 | dirInfo.size = size; | - | ||||||||||||||||||
192 | QString type = indexReader.value(directoryKey + | - | ||||||||||||||||||
193 | QLatin1String("/Type") | - | ||||||||||||||||||
194 | ).toString(); | - | ||||||||||||||||||
195 | - | |||||||||||||||||||
196 | if (type == QLatin1String("Fixed"))
| 0 | ||||||||||||||||||
197 | dirInfo.type = QIconDirInfo::Fixed; never executed: dirInfo.type = QIconDirInfo::Fixed; | 0 | ||||||||||||||||||
198 | else if (type == QLatin1String("Scalable"))
| 0 | ||||||||||||||||||
199 | dirInfo.type = QIconDirInfo::Scalable; never executed: dirInfo.type = QIconDirInfo::Scalable; | 0 | ||||||||||||||||||
200 | else | - | ||||||||||||||||||
201 | dirInfo.type = QIconDirInfo::Threshold; never executed: dirInfo.type = QIconDirInfo::Threshold; | 0 | ||||||||||||||||||
202 | - | |||||||||||||||||||
203 | dirInfo.threshold = indexReader.value(directoryKey + | - | ||||||||||||||||||
204 | QLatin1String("/Threshold"), | - | ||||||||||||||||||
205 | 2).toInt(); | - | ||||||||||||||||||
206 | - | |||||||||||||||||||
207 | dirInfo.minSize = indexReader.value(directoryKey + | - | ||||||||||||||||||
208 | QLatin1String("/MinSize"), | - | ||||||||||||||||||
209 | size).toInt(); | - | ||||||||||||||||||
210 | - | |||||||||||||||||||
211 | dirInfo.maxSize = indexReader.value(directoryKey + | - | ||||||||||||||||||
212 | QLatin1String("/MaxSize"), | - | ||||||||||||||||||
213 | size).toInt(); | - | ||||||||||||||||||
214 | m_keyList.append(dirInfo); | - | ||||||||||||||||||
215 | } never executed: end of block | 0 | ||||||||||||||||||
216 | } never executed: end of block | 0 | ||||||||||||||||||
217 | } never executed: end of block | 0 | ||||||||||||||||||
218 | - | |||||||||||||||||||
219 | // Parent themes provide fallbacks for missing icons | - | ||||||||||||||||||
220 | m_parents = indexReader.value( | - | ||||||||||||||||||
221 | QLatin1String("Icon Theme/Inherits")).toStringList(); | - | ||||||||||||||||||
222 | m_parents.removeAll(QString()); | - | ||||||||||||||||||
223 | - | |||||||||||||||||||
224 | // Ensure a default platform fallback for all themes | - | ||||||||||||||||||
225 | if (m_parents.isEmpty()) {
| 0 | ||||||||||||||||||
226 | const QString fallback = fallbackTheme(); | - | ||||||||||||||||||
227 | if (!fallback.isEmpty())
| 0 | ||||||||||||||||||
228 | m_parents.append(fallback); never executed: m_parents.append(fallback); | 0 | ||||||||||||||||||
229 | } never executed: end of block | 0 | ||||||||||||||||||
230 | - | |||||||||||||||||||
231 | // Ensure that all themes fall back to hicolor | - | ||||||||||||||||||
232 | if (!m_parents.contains(QLatin1String("hicolor")))
| 0 | ||||||||||||||||||
233 | m_parents.append(QLatin1String("hicolor")); never executed: m_parents.append(QLatin1String("hicolor")); | 0 | ||||||||||||||||||
234 | } never executed: end of block | 0 | ||||||||||||||||||
235 | #endif //QT_NO_SETTINGS | - | ||||||||||||||||||
236 | } never executed: end of block | 0 | ||||||||||||||||||
237 | - | |||||||||||||||||||
238 | QThemeIconInfo QIconLoader::findIconHelper(const QString &themeName, | - | ||||||||||||||||||
239 | const QString &iconName, | - | ||||||||||||||||||
240 | QStringList &visited) const | - | ||||||||||||||||||
241 | { | - | ||||||||||||||||||
242 | QThemeIconInfo info; | - | ||||||||||||||||||
243 | Q_ASSERT(!themeName.isEmpty()); | - | ||||||||||||||||||
244 | - | |||||||||||||||||||
245 | // Used to protect against potential recursions | - | ||||||||||||||||||
246 | visited << themeName; | - | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | QIconTheme theme = themeList.value(themeName); | - | ||||||||||||||||||
249 | if (!theme.isValid()) {
| 0 | ||||||||||||||||||
250 | theme = QIconTheme(themeName); | - | ||||||||||||||||||
251 | if (!theme.isValid())
| 0 | ||||||||||||||||||
252 | theme = QIconTheme(fallbackTheme()); never executed: theme = QIconTheme(fallbackTheme()); | 0 | ||||||||||||||||||
253 | - | |||||||||||||||||||
254 | themeList.insert(themeName, theme); | - | ||||||||||||||||||
255 | } never executed: end of block | 0 | ||||||||||||||||||
256 | - | |||||||||||||||||||
257 | const QStringList contentDirs = theme.contentDirs(); | - | ||||||||||||||||||
258 | const QVector<QIconDirInfo> subDirs = theme.keyList(); | - | ||||||||||||||||||
259 | - | |||||||||||||||||||
260 | QString iconNameFallback = iconName; | - | ||||||||||||||||||
261 | - | |||||||||||||||||||
262 | // Iterate through all icon's fallbacks in current theme | - | ||||||||||||||||||
263 | while (info.entries.isEmpty()) {
| 0 | ||||||||||||||||||
264 | const QString svgIconName = iconNameFallback + QLatin1String(".svg"); | - | ||||||||||||||||||
265 | const QString pngIconName = iconNameFallback + QLatin1String(".png"); | - | ||||||||||||||||||
266 | - | |||||||||||||||||||
267 | // Add all relevant files | - | ||||||||||||||||||
268 | for (int i = 0; i < contentDirs.size(); ++i) {
| 0 | ||||||||||||||||||
269 | QString contentDir = contentDirs.at(i) + QLatin1Char('/'); | - | ||||||||||||||||||
270 | for (int j = 0; j < subDirs.size() ; ++j) {
| 0 | ||||||||||||||||||
271 | const QIconDirInfo &dirInfo = subDirs.at(j); | - | ||||||||||||||||||
272 | QString subdir = dirInfo.path; | - | ||||||||||||||||||
273 | QDir currentDir(contentDir + subdir); | - | ||||||||||||||||||
274 | if (currentDir.exists(pngIconName)) {
| 0 | ||||||||||||||||||
275 | PixmapEntry *iconEntry = new PixmapEntry; | - | ||||||||||||||||||
276 | iconEntry->dir = dirInfo; | - | ||||||||||||||||||
277 | iconEntry->filename = currentDir.filePath(pngIconName); | - | ||||||||||||||||||
278 | // Notice we ensure that pixmap entries always come before | - | ||||||||||||||||||
279 | // scalable to preserve search order afterwards | - | ||||||||||||||||||
280 | info.entries.prepend(iconEntry); | - | ||||||||||||||||||
281 | } else if (m_supportsSvg && never executed: end of block
| 0 | ||||||||||||||||||
282 | currentDir.exists(svgIconName)) {
| 0 | ||||||||||||||||||
283 | ScalableEntry *iconEntry = new ScalableEntry; | - | ||||||||||||||||||
284 | iconEntry->dir = dirInfo; | - | ||||||||||||||||||
285 | iconEntry->filename = currentDir.filePath(svgIconName); | - | ||||||||||||||||||
286 | info.entries.append(iconEntry); | - | ||||||||||||||||||
287 | } never executed: end of block | 0 | ||||||||||||||||||
288 | } never executed: end of block | 0 | ||||||||||||||||||
289 | } never executed: end of block | 0 | ||||||||||||||||||
290 | - | |||||||||||||||||||
291 | if (!info.entries.isEmpty()) {
| 0 | ||||||||||||||||||
292 | info.iconName = iconNameFallback; | - | ||||||||||||||||||
293 | break; never executed: break; | 0 | ||||||||||||||||||
294 | } | - | ||||||||||||||||||
295 | - | |||||||||||||||||||
296 | // If it's possible - find next fallback for the icon | - | ||||||||||||||||||
297 | const int indexOfDash = iconNameFallback.lastIndexOf(QLatin1Char('-')); | - | ||||||||||||||||||
298 | if (indexOfDash == -1)
| 0 | ||||||||||||||||||
299 | break; never executed: break; | 0 | ||||||||||||||||||
300 | - | |||||||||||||||||||
301 | iconNameFallback.truncate(indexOfDash); | - | ||||||||||||||||||
302 | } never executed: end of block | 0 | ||||||||||||||||||
303 | - | |||||||||||||||||||
304 | if (info.entries.isEmpty()) {
| 0 | ||||||||||||||||||
305 | const QStringList parents = theme.parents(); | - | ||||||||||||||||||
306 | // Search recursively through inherited themes | - | ||||||||||||||||||
307 | for (int i = 0 ; i < parents.size() ; ++i) {
| 0 | ||||||||||||||||||
308 | - | |||||||||||||||||||
309 | const QString parentTheme = parents.at(i).trimmed(); | - | ||||||||||||||||||
310 | - | |||||||||||||||||||
311 | if (!visited.contains(parentTheme)) // guard against recursion
| 0 | ||||||||||||||||||
312 | info = findIconHelper(parentTheme, iconName, visited); never executed: info = findIconHelper(parentTheme, iconName, visited); | 0 | ||||||||||||||||||
313 | - | |||||||||||||||||||
314 | if (!info.entries.isEmpty()) // success
| 0 | ||||||||||||||||||
315 | break; never executed: break; | 0 | ||||||||||||||||||
316 | } never executed: end of block | 0 | ||||||||||||||||||
317 | } never executed: end of block | 0 | ||||||||||||||||||
318 | return info; never executed: return info; | 0 | ||||||||||||||||||
319 | } | - | ||||||||||||||||||
320 | - | |||||||||||||||||||
321 | QThemeIconInfo QIconLoader::loadIcon(const QString &name) const | - | ||||||||||||||||||
322 | { | - | ||||||||||||||||||
323 | if (!themeName().isEmpty()) {
| 0 | ||||||||||||||||||
324 | QStringList visited; | - | ||||||||||||||||||
325 | return findIconHelper(themeName(), name, visited); never executed: return findIconHelper(themeName(), name, visited); | 0 | ||||||||||||||||||
326 | } | - | ||||||||||||||||||
327 | - | |||||||||||||||||||
328 | return QThemeIconInfo(); never executed: return QThemeIconInfo(); | 0 | ||||||||||||||||||
329 | } | - | ||||||||||||||||||
330 | - | |||||||||||||||||||
331 | - | |||||||||||||||||||
332 | // -------- Icon Loader Engine -------- // | - | ||||||||||||||||||
333 | - | |||||||||||||||||||
334 | - | |||||||||||||||||||
335 | QIconLoaderEngine::QIconLoaderEngine(const QString& iconName) | - | ||||||||||||||||||
336 | : m_iconName(iconName), m_key(0) | - | ||||||||||||||||||
337 | { | - | ||||||||||||||||||
338 | } never executed: end of block | 0 | ||||||||||||||||||
339 | - | |||||||||||||||||||
340 | QIconLoaderEngine::~QIconLoaderEngine() | - | ||||||||||||||||||
341 | { | - | ||||||||||||||||||
342 | qDeleteAll(m_info.entries); | - | ||||||||||||||||||
343 | } never executed: end of block | 0 | ||||||||||||||||||
344 | - | |||||||||||||||||||
345 | QIconLoaderEngine::QIconLoaderEngine(const QIconLoaderEngine &other) | - | ||||||||||||||||||
346 | : QIconEngine(other), | - | ||||||||||||||||||
347 | m_iconName(other.m_iconName), | - | ||||||||||||||||||
348 | m_key(0) | - | ||||||||||||||||||
349 | { | - | ||||||||||||||||||
350 | } never executed: end of block | 0 | ||||||||||||||||||
351 | - | |||||||||||||||||||
352 | QIconEngine *QIconLoaderEngine::clone() const | - | ||||||||||||||||||
353 | { | - | ||||||||||||||||||
354 | return new QIconLoaderEngine(*this); never executed: return new QIconLoaderEngine(*this); | 0 | ||||||||||||||||||
355 | } | - | ||||||||||||||||||
356 | - | |||||||||||||||||||
357 | bool QIconLoaderEngine::read(QDataStream &in) { | - | ||||||||||||||||||
358 | in >> m_iconName; | - | ||||||||||||||||||
359 | return true; never executed: return true; | 0 | ||||||||||||||||||
360 | } | - | ||||||||||||||||||
361 | - | |||||||||||||||||||
362 | bool QIconLoaderEngine::write(QDataStream &out) const | - | ||||||||||||||||||
363 | { | - | ||||||||||||||||||
364 | out << m_iconName; | - | ||||||||||||||||||
365 | return true; never executed: return true; | 0 | ||||||||||||||||||
366 | } | - | ||||||||||||||||||
367 | - | |||||||||||||||||||
368 | bool QIconLoaderEngine::hasIcon() const | - | ||||||||||||||||||
369 | { | - | ||||||||||||||||||
370 | return !(m_info.entries.isEmpty()); never executed: return !(m_info.entries.isEmpty()); | 0 | ||||||||||||||||||
371 | } | - | ||||||||||||||||||
372 | - | |||||||||||||||||||
373 | // Lazily load the icon | - | ||||||||||||||||||
374 | void QIconLoaderEngine::ensureLoaded() | - | ||||||||||||||||||
375 | { | - | ||||||||||||||||||
376 | if (!(QIconLoader::instance()->themeKey() == m_key)) {
| 0 | ||||||||||||||||||
377 | qDeleteAll(m_info.entries); | - | ||||||||||||||||||
378 | m_info.entries.clear(); | - | ||||||||||||||||||
379 | m_info.iconName.clear(); | - | ||||||||||||||||||
380 | - | |||||||||||||||||||
381 | Q_ASSERT(m_info.entries.size() == 0); | - | ||||||||||||||||||
382 | m_info = QIconLoader::instance()->loadIcon(m_iconName); | - | ||||||||||||||||||
383 | m_key = QIconLoader::instance()->themeKey(); | - | ||||||||||||||||||
384 | } never executed: end of block | 0 | ||||||||||||||||||
385 | } never executed: end of block | 0 | ||||||||||||||||||
386 | - | |||||||||||||||||||
387 | void QIconLoaderEngine::paint(QPainter *painter, const QRect &rect, | - | ||||||||||||||||||
388 | QIcon::Mode mode, QIcon::State state) | - | ||||||||||||||||||
389 | { | - | ||||||||||||||||||
390 | QSize pixmapSize = rect.size(); | - | ||||||||||||||||||
391 | painter->drawPixmap(rect, pixmap(pixmapSize, mode, state)); | - | ||||||||||||||||||
392 | } never executed: end of block | 0 | ||||||||||||||||||
393 | - | |||||||||||||||||||
394 | /* | - | ||||||||||||||||||
395 | * This algorithm is defined by the freedesktop spec: | - | ||||||||||||||||||
396 | * http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html | - | ||||||||||||||||||
397 | */ | - | ||||||||||||||||||
398 | static bool directoryMatchesSize(const QIconDirInfo &dir, int iconsize) | - | ||||||||||||||||||
399 | { | - | ||||||||||||||||||
400 | if (dir.type == QIconDirInfo::Fixed) {
| 0 | ||||||||||||||||||
401 | return dir.size == iconsize; never executed: return dir.size == iconsize; | 0 | ||||||||||||||||||
402 | - | |||||||||||||||||||
403 | } else if (dir.type == QIconDirInfo::Scalable) {
| 0 | ||||||||||||||||||
404 | return iconsize <= dir.maxSize && never executed: return iconsize <= dir.maxSize && iconsize >= dir.minSize;
| 0 | ||||||||||||||||||
405 | iconsize >= dir.minSize; never executed: return iconsize <= dir.maxSize && iconsize >= dir.minSize;
| 0 | ||||||||||||||||||
406 | - | |||||||||||||||||||
407 | } else if (dir.type == QIconDirInfo::Threshold) {
| 0 | ||||||||||||||||||
408 | return iconsize >= dir.size - dir.threshold && never executed: return iconsize >= dir.size - dir.threshold && iconsize <= dir.size + dir.threshold;
| 0 | ||||||||||||||||||
409 | iconsize <= dir.size + dir.threshold; never executed: return iconsize >= dir.size - dir.threshold && iconsize <= dir.size + dir.threshold;
| 0 | ||||||||||||||||||
410 | } | - | ||||||||||||||||||
411 | - | |||||||||||||||||||
412 | Q_ASSERT(1); // Not a valid value | - | ||||||||||||||||||
413 | return false; never executed: return false; | 0 | ||||||||||||||||||
414 | } | - | ||||||||||||||||||
415 | - | |||||||||||||||||||
416 | /* | - | ||||||||||||||||||
417 | * This algorithm is defined by the freedesktop spec: | - | ||||||||||||||||||
418 | * http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html | - | ||||||||||||||||||
419 | */ | - | ||||||||||||||||||
420 | static int directorySizeDistance(const QIconDirInfo &dir, int iconsize) | - | ||||||||||||||||||
421 | { | - | ||||||||||||||||||
422 | if (dir.type == QIconDirInfo::Fixed) {
| 0 | ||||||||||||||||||
423 | return qAbs(dir.size - iconsize); never executed: return qAbs(dir.size - iconsize); | 0 | ||||||||||||||||||
424 | - | |||||||||||||||||||
425 | } else if (dir.type == QIconDirInfo::Scalable) {
| 0 | ||||||||||||||||||
426 | if (iconsize < dir.minSize)
| 0 | ||||||||||||||||||
427 | return dir.minSize - iconsize; never executed: return dir.minSize - iconsize; | 0 | ||||||||||||||||||
428 | else if (iconsize > dir.maxSize)
| 0 | ||||||||||||||||||
429 | return iconsize - dir.maxSize; never executed: return iconsize - dir.maxSize; | 0 | ||||||||||||||||||
430 | else | - | ||||||||||||||||||
431 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
432 | - | |||||||||||||||||||
433 | } else if (dir.type == QIconDirInfo::Threshold) {
| 0 | ||||||||||||||||||
434 | if (iconsize < dir.size - dir.threshold)
| 0 | ||||||||||||||||||
435 | return dir.minSize - iconsize; never executed: return dir.minSize - iconsize; | 0 | ||||||||||||||||||
436 | else if (iconsize > dir.size + dir.threshold)
| 0 | ||||||||||||||||||
437 | return iconsize - dir.maxSize; never executed: return iconsize - dir.maxSize; | 0 | ||||||||||||||||||
438 | else return 0; never executed: return 0; | 0 | ||||||||||||||||||
439 | } | - | ||||||||||||||||||
440 | - | |||||||||||||||||||
441 | Q_ASSERT(1); // Not a valid value | - | ||||||||||||||||||
442 | return INT_MAX; never executed: return 2147483647; | 0 | ||||||||||||||||||
443 | } | - | ||||||||||||||||||
444 | - | |||||||||||||||||||
445 | QIconLoaderEngineEntry *QIconLoaderEngine::entryForSize(const QSize &size) | - | ||||||||||||||||||
446 | { | - | ||||||||||||||||||
447 | int iconsize = qMin(size.width(), size.height()); | - | ||||||||||||||||||
448 | - | |||||||||||||||||||
449 | // Note that m_info.entries are sorted so that png-files | - | ||||||||||||||||||
450 | // come first | - | ||||||||||||||||||
451 | - | |||||||||||||||||||
452 | const int numEntries = m_info.entries.size(); | - | ||||||||||||||||||
453 | - | |||||||||||||||||||
454 | // Search for exact matches first | - | ||||||||||||||||||
455 | for (int i = 0; i < numEntries; ++i) {
| 0 | ||||||||||||||||||
456 | QIconLoaderEngineEntry *entry = m_info.entries.at(i); | - | ||||||||||||||||||
457 | if (directoryMatchesSize(entry->dir, iconsize)) {
| 0 | ||||||||||||||||||
458 | return entry; never executed: return entry; | 0 | ||||||||||||||||||
459 | } | - | ||||||||||||||||||
460 | } never executed: end of block | 0 | ||||||||||||||||||
461 | - | |||||||||||||||||||
462 | // Find the minimum distance icon | - | ||||||||||||||||||
463 | int minimalSize = INT_MAX; | - | ||||||||||||||||||
464 | QIconLoaderEngineEntry *closestMatch = 0; | - | ||||||||||||||||||
465 | for (int i = 0; i < numEntries; ++i) {
| 0 | ||||||||||||||||||
466 | QIconLoaderEngineEntry *entry = m_info.entries.at(i); | - | ||||||||||||||||||
467 | int distance = directorySizeDistance(entry->dir, iconsize); | - | ||||||||||||||||||
468 | if (distance < minimalSize) {
| 0 | ||||||||||||||||||
469 | minimalSize = distance; | - | ||||||||||||||||||
470 | closestMatch = entry; | - | ||||||||||||||||||
471 | } never executed: end of block | 0 | ||||||||||||||||||
472 | } never executed: end of block | 0 | ||||||||||||||||||
473 | return closestMatch; never executed: return closestMatch; | 0 | ||||||||||||||||||
474 | } | - | ||||||||||||||||||
475 | - | |||||||||||||||||||
476 | /* | - | ||||||||||||||||||
477 | * Returns the actual icon size. For scalable svg's this is equivalent | - | ||||||||||||||||||
478 | * to the requested size. Otherwise the closest match is returned but | - | ||||||||||||||||||
479 | * we can never return a bigger size than the requested size. | - | ||||||||||||||||||
480 | * | - | ||||||||||||||||||
481 | */ | - | ||||||||||||||||||
482 | QSize QIconLoaderEngine::actualSize(const QSize &size, QIcon::Mode mode, | - | ||||||||||||||||||
483 | QIcon::State state) | - | ||||||||||||||||||
484 | { | - | ||||||||||||||||||
485 | ensureLoaded(); | - | ||||||||||||||||||
486 | - | |||||||||||||||||||
487 | QIconLoaderEngineEntry *entry = entryForSize(size); | - | ||||||||||||||||||
488 | if (entry) {
| 0 | ||||||||||||||||||
489 | const QIconDirInfo &dir = entry->dir; | - | ||||||||||||||||||
490 | if (dir.type == QIconDirInfo::Scalable)
| 0 | ||||||||||||||||||
491 | return size; never executed: return size; | 0 | ||||||||||||||||||
492 | else { | - | ||||||||||||||||||
493 | int result = qMin<int>(dir.size, qMin(size.width(), size.height())); | - | ||||||||||||||||||
494 | return QSize(result, result); never executed: return QSize(result, result); | 0 | ||||||||||||||||||
495 | } | - | ||||||||||||||||||
496 | } | - | ||||||||||||||||||
497 | return QIconEngine::actualSize(size, mode, state); never executed: return QIconEngine::actualSize(size, mode, state); | 0 | ||||||||||||||||||
498 | } | - | ||||||||||||||||||
499 | - | |||||||||||||||||||
500 | QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) | - | ||||||||||||||||||
501 | { | - | ||||||||||||||||||
502 | Q_UNUSED(state); | - | ||||||||||||||||||
503 | - | |||||||||||||||||||
504 | // Ensure that basePixmap is lazily initialized before generating the | - | ||||||||||||||||||
505 | // key, otherwise the cache key is not unique | - | ||||||||||||||||||
506 | if (basePixmap.isNull())
| 0 | ||||||||||||||||||
507 | basePixmap.load(filename); never executed: basePixmap.load(filename); | 0 | ||||||||||||||||||
508 | - | |||||||||||||||||||
509 | QSize actualSize = basePixmap.size(); | - | ||||||||||||||||||
510 | if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
| 0 | ||||||||||||||||||
511 | actualSize.scale(size, Qt::KeepAspectRatio); never executed: actualSize.scale(size, Qt::KeepAspectRatio); | 0 | ||||||||||||||||||
512 | - | |||||||||||||||||||
513 | QString key = QLatin1String("$qt_theme_") | - | ||||||||||||||||||
514 | % HexString<qint64>(basePixmap.cacheKey()) | - | ||||||||||||||||||
515 | % HexString<int>(mode) | - | ||||||||||||||||||
516 | % HexString<qint64>(QGuiApplication::palette().cacheKey()) | - | ||||||||||||||||||
517 | % HexString<int>(actualSize.width()) | - | ||||||||||||||||||
518 | % HexString<int>(actualSize.height()); | - | ||||||||||||||||||
519 | - | |||||||||||||||||||
520 | QPixmap cachedPixmap; | - | ||||||||||||||||||
521 | if (QPixmapCache::find(key, &cachedPixmap)) {
| 0 | ||||||||||||||||||
522 | return cachedPixmap; never executed: return cachedPixmap; | 0 | ||||||||||||||||||
523 | } else { | - | ||||||||||||||||||
524 | if (basePixmap.size() != actualSize)
| 0 | ||||||||||||||||||
525 | cachedPixmap = basePixmap.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); never executed: cachedPixmap = basePixmap.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); | 0 | ||||||||||||||||||
526 | else | - | ||||||||||||||||||
527 | cachedPixmap = basePixmap; never executed: cachedPixmap = basePixmap; | 0 | ||||||||||||||||||
528 | if (QGuiApplication *guiApp = qobject_cast<QGuiApplication *>(qApp))
| 0 | ||||||||||||||||||
529 | cachedPixmap = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(mode, cachedPixmap); never executed: cachedPixmap = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(mode, cachedPixmap); | 0 | ||||||||||||||||||
530 | QPixmapCache::insert(key, cachedPixmap); | - | ||||||||||||||||||
531 | } never executed: end of block | 0 | ||||||||||||||||||
532 | return cachedPixmap; never executed: return cachedPixmap; | 0 | ||||||||||||||||||
533 | } | - | ||||||||||||||||||
534 | - | |||||||||||||||||||
535 | QPixmap ScalableEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) | - | ||||||||||||||||||
536 | { | - | ||||||||||||||||||
537 | if (svgIcon.isNull())
| 0 | ||||||||||||||||||
538 | svgIcon = QIcon(filename); never executed: svgIcon = QIcon(filename); | 0 | ||||||||||||||||||
539 | - | |||||||||||||||||||
540 | // Simply reuse svg icon engine | - | ||||||||||||||||||
541 | return svgIcon.pixmap(size, mode, state); never executed: return svgIcon.pixmap(size, mode, state); | 0 | ||||||||||||||||||
542 | } | - | ||||||||||||||||||
543 | - | |||||||||||||||||||
544 | QPixmap QIconLoaderEngine::pixmap(const QSize &size, QIcon::Mode mode, | - | ||||||||||||||||||
545 | QIcon::State state) | - | ||||||||||||||||||
546 | { | - | ||||||||||||||||||
547 | ensureLoaded(); | - | ||||||||||||||||||
548 | - | |||||||||||||||||||
549 | QIconLoaderEngineEntry *entry = entryForSize(size); | - | ||||||||||||||||||
550 | if (entry)
| 0 | ||||||||||||||||||
551 | return entry->pixmap(size, mode, state); never executed: return entry->pixmap(size, mode, state); | 0 | ||||||||||||||||||
552 | - | |||||||||||||||||||
553 | return QPixmap(); never executed: return QPixmap(); | 0 | ||||||||||||||||||
554 | } | - | ||||||||||||||||||
555 | - | |||||||||||||||||||
556 | QString QIconLoaderEngine::key() const | - | ||||||||||||||||||
557 | { | - | ||||||||||||||||||
558 | return QLatin1String("QIconLoaderEngine"); never executed: return QLatin1String("QIconLoaderEngine"); | 0 | ||||||||||||||||||
559 | } | - | ||||||||||||||||||
560 | - | |||||||||||||||||||
561 | void QIconLoaderEngine::virtual_hook(int id, void *data) | - | ||||||||||||||||||
562 | { | - | ||||||||||||||||||
563 | ensureLoaded(); | - | ||||||||||||||||||
564 | - | |||||||||||||||||||
565 | switch (id) { | - | ||||||||||||||||||
566 | case QIconEngine::AvailableSizesHook: never executed: case QIconEngine::AvailableSizesHook: | 0 | ||||||||||||||||||
567 | { | - | ||||||||||||||||||
568 | QIconEngine::AvailableSizesArgument &arg | - | ||||||||||||||||||
569 | = *reinterpret_cast<QIconEngine::AvailableSizesArgument*>(data); | - | ||||||||||||||||||
570 | const int N = m_info.entries.size(); | - | ||||||||||||||||||
571 | QList<QSize> sizes; | - | ||||||||||||||||||
572 | sizes.reserve(N); | - | ||||||||||||||||||
573 | - | |||||||||||||||||||
574 | // Gets all sizes from the DirectoryInfo entries | - | ||||||||||||||||||
575 | for (int i = 0; i < N; ++i) {
| 0 | ||||||||||||||||||
576 | int size = m_info.entries.at(i)->dir.size; | - | ||||||||||||||||||
577 | sizes.append(QSize(size, size)); | - | ||||||||||||||||||
578 | } never executed: end of block | 0 | ||||||||||||||||||
579 | arg.sizes.swap(sizes); // commit | - | ||||||||||||||||||
580 | } | - | ||||||||||||||||||
581 | break; never executed: break; | 0 | ||||||||||||||||||
582 | case QIconEngine::IconNameHook: never executed: case QIconEngine::IconNameHook: | 0 | ||||||||||||||||||
583 | { | - | ||||||||||||||||||
584 | QString &name = *reinterpret_cast<QString*>(data); | - | ||||||||||||||||||
585 | name = m_info.iconName; | - | ||||||||||||||||||
586 | } | - | ||||||||||||||||||
587 | break; never executed: break; | 0 | ||||||||||||||||||
588 | default: never executed: default: | 0 | ||||||||||||||||||
589 | QIconEngine::virtual_hook(id, data); | - | ||||||||||||||||||
590 | } never executed: end of block | 0 | ||||||||||||||||||
591 | } | - | ||||||||||||||||||
592 | - | |||||||||||||||||||
593 | QT_END_NAMESPACE | - | ||||||||||||||||||
594 | - | |||||||||||||||||||
595 | #endif //QT_NO_ICON | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |