|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <private/qiconloader_p.h> |
|
43 |
|
44 #include <private/qapplication_p.h> |
|
45 #include <private/qicon_p.h> |
|
46 #include <private/qguiplatformplugin_p.h> |
|
47 |
|
48 #include <QtGui/QIconEnginePlugin> |
|
49 #include <QtGui/QPixmapCache> |
|
50 #include <QtGui/QIconEngine> |
|
51 #include <QtGui/QStyleOption> |
|
52 #include <QtCore/QList> |
|
53 #include <QtCore/QHash> |
|
54 #include <QtCore/QDir> |
|
55 #include <QtCore/QSettings> |
|
56 #include <QtGui/QPainter> |
|
57 |
|
58 #ifdef Q_WS_MAC |
|
59 #include <private/qt_cocoa_helpers_mac_p.h> |
|
60 #endif |
|
61 |
|
62 #ifdef Q_WS_X11 |
|
63 #include <private/qt_x11_p.h> |
|
64 #include <private/gtksymbols_p.h> |
|
65 #endif |
|
66 |
|
67 QT_BEGIN_NAMESPACE |
|
68 |
|
69 Q_GLOBAL_STATIC(QIconLoader, iconLoaderInstance) |
|
70 |
|
71 /* Theme to use in last resort, if the theme does not have the icon, neither the parents */ |
|
72 static QString fallbackTheme() |
|
73 { |
|
74 #ifdef Q_WS_X11 |
|
75 if (X11->desktopEnvironment == DE_GNOME) { |
|
76 return QLatin1String("gnome"); |
|
77 } else if (X11->desktopEnvironment == DE_KDE) { |
|
78 return X11->desktopVersion >= 4 |
|
79 ? QString::fromLatin1("oxygen") |
|
80 : QString::fromLatin1("crystalsvg"); |
|
81 } else { |
|
82 return QLatin1String("hicolor"); |
|
83 } |
|
84 #endif |
|
85 return QString(); |
|
86 } |
|
87 |
|
88 QIconLoader::QIconLoader() : |
|
89 m_themeKey(1), m_supportsSvg(false) |
|
90 { |
|
91 m_systemTheme = qt_guiPlatformPlugin()->systemIconThemeName(); |
|
92 if (m_systemTheme.isEmpty()) |
|
93 m_systemTheme = fallbackTheme(); |
|
94 |
|
95 QFactoryLoader iconFactoryLoader(QIconEngineFactoryInterfaceV2_iid, |
|
96 QLatin1String("/iconengines"), |
|
97 Qt::CaseInsensitive); |
|
98 if (iconFactoryLoader.keys().contains(QLatin1String("svg"))) |
|
99 m_supportsSvg = true; |
|
100 } |
|
101 |
|
102 QIconLoader *QIconLoader::instance() |
|
103 { |
|
104 return iconLoaderInstance(); |
|
105 } |
|
106 |
|
107 // Queries the system theme and invalidates existing |
|
108 // icons if the theme has changed. |
|
109 void QIconLoader::updateSystemTheme() |
|
110 { |
|
111 // Only change if this is not explicitly set by the user |
|
112 if (m_userTheme.isEmpty()) { |
|
113 QString theme = qt_guiPlatformPlugin()->systemIconThemeName(); |
|
114 if (theme.isEmpty()) |
|
115 theme = fallbackTheme(); |
|
116 if (theme != m_systemTheme) { |
|
117 m_systemTheme = theme; |
|
118 invalidateKey(); |
|
119 } |
|
120 } |
|
121 } |
|
122 |
|
123 void QIconLoader::setThemeName(const QString &themeName) |
|
124 { |
|
125 m_userTheme = themeName; |
|
126 invalidateKey(); |
|
127 } |
|
128 |
|
129 void QIconLoader::setThemeSearchPath(const QStringList &searchPaths) |
|
130 { |
|
131 m_iconDirs = searchPaths; |
|
132 themeList.clear(); |
|
133 invalidateKey(); |
|
134 } |
|
135 |
|
136 QStringList QIconLoader::themeSearchPaths() const |
|
137 { |
|
138 if (m_iconDirs.isEmpty()) { |
|
139 m_iconDirs = qt_guiPlatformPlugin()->iconThemeSearchPaths(); |
|
140 // Allways add resource directory as search path |
|
141 m_iconDirs.append(QLatin1String(":/icons")); |
|
142 } |
|
143 return m_iconDirs; |
|
144 } |
|
145 |
|
146 QIconTheme::QIconTheme(const QString &themeName) |
|
147 : m_valid(false) |
|
148 { |
|
149 QFile themeIndex; |
|
150 |
|
151 QList <QIconDirInfo> keyList; |
|
152 QStringList iconDirs = QIcon::themeSearchPaths(); |
|
153 for ( int i = 0 ; i < iconDirs.size() ; ++i) { |
|
154 QDir iconDir(iconDirs[i]); |
|
155 QString themeDir = iconDir.path() + QLatin1Char('/') + themeName; |
|
156 themeIndex.setFileName(themeDir + QLatin1String("/index.theme")); |
|
157 if (themeIndex.exists()) { |
|
158 m_contentDir = themeDir; |
|
159 m_valid = true; |
|
160 break; |
|
161 } |
|
162 } |
|
163 |
|
164 if (themeIndex.exists()) { |
|
165 const QSettings indexReader(themeIndex.fileName(), QSettings::IniFormat); |
|
166 QStringListIterator keyIterator(indexReader.allKeys()); |
|
167 while (keyIterator.hasNext()) { |
|
168 |
|
169 const QString key = keyIterator.next(); |
|
170 if (key.endsWith(QLatin1String("/Size"))) { |
|
171 // Note the QSettings ini-format does not accept |
|
172 // slashes in key names, hence we have to cheat |
|
173 if (int size = indexReader.value(key).toInt()) { |
|
174 QString directoryKey = key.left(key.size() - 5); |
|
175 QIconDirInfo dirInfo(directoryKey); |
|
176 dirInfo.size = size; |
|
177 QString type = indexReader.value(directoryKey + |
|
178 QLatin1String("/Type") |
|
179 ).toString(); |
|
180 |
|
181 if (type == QLatin1String("Fixed")) |
|
182 dirInfo.type = QIconDirInfo::Fixed; |
|
183 else if (type == QLatin1String("Scalable")) |
|
184 dirInfo.type = QIconDirInfo::Scalable; |
|
185 else |
|
186 dirInfo.type = QIconDirInfo::Threshold; |
|
187 |
|
188 dirInfo.threshold = indexReader.value(directoryKey + |
|
189 QLatin1String("/Threshold"), |
|
190 2).toInt(); |
|
191 |
|
192 dirInfo.minSize = indexReader.value(directoryKey + |
|
193 QLatin1String("/MinSize"), |
|
194 size).toInt(); |
|
195 |
|
196 dirInfo.maxSize = indexReader.value(directoryKey + |
|
197 QLatin1String("/MaxSize"), |
|
198 size).toInt(); |
|
199 m_keyList.append(dirInfo); |
|
200 } |
|
201 } |
|
202 } |
|
203 |
|
204 // Parent themes provide fallbacks for missing icons |
|
205 m_parents = indexReader.value( |
|
206 QLatin1String("Icon Theme/Inherits")).toStringList(); |
|
207 |
|
208 // Ensure a default platform fallback for all themes |
|
209 if (m_parents.isEmpty()) |
|
210 m_parents.append(fallbackTheme()); |
|
211 |
|
212 // Ensure that all themes fall back to hicolor |
|
213 if (!m_parents.contains(QLatin1String("hicolor"))) |
|
214 m_parents.append(QLatin1String("hicolor")); |
|
215 } |
|
216 } |
|
217 |
|
218 QThemeIconEntries QIconLoader::findIconHelper(const QString &themeName, |
|
219 const QString &iconName, |
|
220 QStringList &visited) const |
|
221 { |
|
222 QThemeIconEntries entries; |
|
223 Q_ASSERT(!themeName.isEmpty()); |
|
224 |
|
225 QPixmap pixmap; |
|
226 |
|
227 // Used to protect against potential recursions |
|
228 visited << themeName; |
|
229 |
|
230 QIconTheme theme = themeList.value(themeName); |
|
231 if (!theme.isValid()) { |
|
232 theme = QIconTheme(themeName); |
|
233 if (!theme.isValid()) |
|
234 theme = QIconTheme(fallbackTheme()); |
|
235 |
|
236 themeList.insert(themeName, theme); |
|
237 } |
|
238 |
|
239 QString contentDir = theme.contentDir() + QLatin1Char('/'); |
|
240 QList<QIconDirInfo> subDirs = theme.keyList(); |
|
241 |
|
242 const QString svgext(QLatin1String(".svg")); |
|
243 const QString pngext(QLatin1String(".png")); |
|
244 |
|
245 // Add all relevant files |
|
246 for (int i = 0; i < subDirs.size() ; ++i) { |
|
247 const QIconDirInfo &dirInfo = subDirs.at(i); |
|
248 QString subdir = dirInfo.path; |
|
249 QDir currentDir(contentDir + subdir); |
|
250 |
|
251 if (dirInfo.type == QIconDirInfo::Scalable && m_supportsSvg && |
|
252 currentDir.exists(iconName + svgext)) { |
|
253 ScalableEntry *iconEntry = new ScalableEntry; |
|
254 iconEntry->dir = dirInfo; |
|
255 iconEntry->filename = currentDir.filePath(iconName + svgext); |
|
256 entries.append(iconEntry); |
|
257 |
|
258 } else if (currentDir.exists(iconName + pngext)) { |
|
259 PixmapEntry *iconEntry = new PixmapEntry; |
|
260 iconEntry->dir = dirInfo; |
|
261 iconEntry->filename = currentDir.filePath(iconName + pngext); |
|
262 // Notice we ensure that pixmap entries allways come before |
|
263 // scalable to preserve search order afterwards |
|
264 entries.prepend(iconEntry); |
|
265 } |
|
266 } |
|
267 |
|
268 if (entries.isEmpty()) { |
|
269 const QStringList parents = theme.parents(); |
|
270 // Search recursively through inherited themes |
|
271 for (int i = 0 ; i < parents.size() ; ++i) { |
|
272 |
|
273 const QString parentTheme = parents.at(i).trimmed(); |
|
274 |
|
275 if (!visited.contains(parentTheme)) // guard against recursion |
|
276 entries = findIconHelper(parentTheme, iconName, visited); |
|
277 |
|
278 if (!entries.isEmpty()) // success |
|
279 break; |
|
280 } |
|
281 } |
|
282 return entries; |
|
283 } |
|
284 |
|
285 QThemeIconEntries QIconLoader::loadIcon(const QString &name) const |
|
286 { |
|
287 if (!themeName().isEmpty()) { |
|
288 QStringList visited; |
|
289 return findIconHelper(themeName(), name, visited); |
|
290 } |
|
291 |
|
292 return QThemeIconEntries(); |
|
293 } |
|
294 |
|
295 |
|
296 // -------- Icon Loader Engine -------- // |
|
297 |
|
298 |
|
299 QIconLoaderEngine::QIconLoaderEngine(const QString& iconName) |
|
300 : m_iconName(iconName), m_key(0) |
|
301 { |
|
302 } |
|
303 |
|
304 QIconLoaderEngine::~QIconLoaderEngine() |
|
305 { |
|
306 while (!m_entries.isEmpty()) |
|
307 delete m_entries.takeLast(); |
|
308 Q_ASSERT(m_entries.size() == 0); |
|
309 } |
|
310 |
|
311 QIconLoaderEngine::QIconLoaderEngine(const QIconLoaderEngine &other) |
|
312 : QIconEngineV2(other), |
|
313 m_iconName(other.m_iconName), |
|
314 m_key(0) |
|
315 { |
|
316 } |
|
317 |
|
318 QIconEngineV2 *QIconLoaderEngine::clone() const |
|
319 { |
|
320 return new QIconLoaderEngine(*this); |
|
321 } |
|
322 |
|
323 bool QIconLoaderEngine::read(QDataStream &in) { |
|
324 in >> m_iconName; |
|
325 return true; |
|
326 } |
|
327 |
|
328 bool QIconLoaderEngine::write(QDataStream &out) const |
|
329 { |
|
330 out << m_iconName; |
|
331 return true; |
|
332 } |
|
333 |
|
334 bool QIconLoaderEngine::hasIcon() const |
|
335 { |
|
336 return !(m_entries.isEmpty()); |
|
337 } |
|
338 |
|
339 // Lazily load the icon |
|
340 void QIconLoaderEngine::ensureLoaded() |
|
341 { |
|
342 if (!(iconLoaderInstance()->themeKey() == m_key)) { |
|
343 |
|
344 while (!m_entries.isEmpty()) |
|
345 delete m_entries.takeLast(); |
|
346 |
|
347 Q_ASSERT(m_entries.size() == 0); |
|
348 m_entries = iconLoaderInstance()->loadIcon(m_iconName); |
|
349 m_key = iconLoaderInstance()->themeKey(); |
|
350 } |
|
351 } |
|
352 |
|
353 void QIconLoaderEngine::paint(QPainter *painter, const QRect &rect, |
|
354 QIcon::Mode mode, QIcon::State state) |
|
355 { |
|
356 QSize pixmapSize = rect.size(); |
|
357 #if defined(Q_WS_MAC) |
|
358 pixmapSize *= qt_mac_get_scalefactor(); |
|
359 #endif |
|
360 painter->drawPixmap(rect, pixmap(pixmapSize, mode, state)); |
|
361 } |
|
362 |
|
363 /* |
|
364 * This algorithm is defined by the freedesktop spec: |
|
365 * http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html |
|
366 */ |
|
367 static bool directoryMatchesSize(const QIconDirInfo &dir, int iconsize) |
|
368 { |
|
369 if (dir.type == QIconDirInfo::Fixed) { |
|
370 return dir.size == iconsize; |
|
371 |
|
372 } else if (dir.type == QIconDirInfo::Scalable) { |
|
373 return dir.size <= dir.maxSize && |
|
374 iconsize >= dir.minSize; |
|
375 |
|
376 } else if (dir.type == QIconDirInfo::Threshold) { |
|
377 return iconsize >= dir.size - dir.threshold && |
|
378 iconsize <= dir.size + dir.threshold; |
|
379 } |
|
380 |
|
381 Q_ASSERT(1); // Not a valid value |
|
382 return false; |
|
383 } |
|
384 |
|
385 /* |
|
386 * This algorithm is defined by the freedesktop spec: |
|
387 * http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html |
|
388 */ |
|
389 static int directorySizeDistance(const QIconDirInfo &dir, int iconsize) |
|
390 { |
|
391 if (dir.type == QIconDirInfo::Fixed) { |
|
392 return qAbs(dir.size - iconsize); |
|
393 |
|
394 } else if (dir.type == QIconDirInfo::Scalable) { |
|
395 if (iconsize < dir.minSize) |
|
396 return dir.minSize - iconsize; |
|
397 else if (iconsize > dir.maxSize) |
|
398 return iconsize - dir.maxSize; |
|
399 else |
|
400 return 0; |
|
401 |
|
402 } else if (dir.type == QIconDirInfo::Threshold) { |
|
403 if (iconsize < dir.size - dir.threshold) |
|
404 return dir.minSize - iconsize; |
|
405 else if (iconsize > dir.size + dir.threshold) |
|
406 return iconsize - dir.maxSize; |
|
407 else return 0; |
|
408 } |
|
409 |
|
410 Q_ASSERT(1); // Not a valid value |
|
411 return INT_MAX; |
|
412 } |
|
413 |
|
414 QIconLoaderEngineEntry *QIconLoaderEngine::entryForSize(const QSize &size) |
|
415 { |
|
416 int iconsize = qMin(size.width(), size.height()); |
|
417 |
|
418 // Note that m_entries are sorted so that png-files |
|
419 // come first |
|
420 |
|
421 // Search for exact matches first |
|
422 for (int i = 0; i < m_entries.count(); ++i) { |
|
423 QIconLoaderEngineEntry *entry = m_entries.at(i); |
|
424 if (directoryMatchesSize(entry->dir, iconsize)) { |
|
425 return entry; |
|
426 } |
|
427 } |
|
428 |
|
429 // Find the minimum distance icon |
|
430 int minimalSize = INT_MAX; |
|
431 QIconLoaderEngineEntry *closestMatch = 0; |
|
432 for (int i = 0; i < m_entries.count(); ++i) { |
|
433 QIconLoaderEngineEntry *entry = m_entries.at(i); |
|
434 int distance = directorySizeDistance(entry->dir, iconsize); |
|
435 if (distance < minimalSize) { |
|
436 minimalSize = distance; |
|
437 closestMatch = entry; |
|
438 } |
|
439 } |
|
440 return closestMatch; |
|
441 } |
|
442 |
|
443 /* |
|
444 * Returns the actual icon size. For scalable svg's this is equivalent |
|
445 * to the requested size. Otherwise the closest match is returned. |
|
446 * |
|
447 * todo: the spec is a bit fuzzy in this area, but we should probably |
|
448 * allow scaling down pixmap icons as well. |
|
449 * |
|
450 */ |
|
451 QSize QIconLoaderEngine::actualSize(const QSize &size, QIcon::Mode mode, |
|
452 QIcon::State state) |
|
453 { |
|
454 ensureLoaded(); |
|
455 |
|
456 QIconLoaderEngineEntry *entry = entryForSize(size); |
|
457 if (entry) { |
|
458 const QIconDirInfo &dir = entry->dir; |
|
459 if (dir.type == QIconDirInfo::Scalable) |
|
460 return size; |
|
461 else |
|
462 return QSize(dir.size, dir.size); |
|
463 } |
|
464 return QIconEngineV2::actualSize(size, mode, state); |
|
465 } |
|
466 |
|
467 QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) |
|
468 { |
|
469 Q_UNUSED(state); |
|
470 |
|
471 // Ensure that basePixmap is lazily initialized before generating the |
|
472 // key, otherwise the cache key is not unique |
|
473 if (basePixmap.isNull()) |
|
474 basePixmap.load(filename); |
|
475 |
|
476 int actualSize = qMin(size.width(), size.height()); |
|
477 QString key = QLatin1String("$qt_theme_") |
|
478 + QString::number(basePixmap.cacheKey(), 16) |
|
479 + QLatin1Char('_') |
|
480 + QString::number(mode) |
|
481 + QLatin1Char('_') |
|
482 + QString::number(qApp->palette().cacheKey(), 16) |
|
483 + QLatin1Char('_') |
|
484 + QString::number(actualSize); |
|
485 |
|
486 QPixmap cachedPixmap; |
|
487 if (QPixmapCache::find(key, &cachedPixmap)) { |
|
488 return cachedPixmap; |
|
489 } else { |
|
490 QStyleOption opt(0); |
|
491 opt.palette = qApp->palette(); |
|
492 cachedPixmap = qApp->style()->generatedIconPixmap(mode, basePixmap, &opt); |
|
493 QPixmapCache::insert(key, cachedPixmap); |
|
494 } |
|
495 return cachedPixmap; |
|
496 } |
|
497 |
|
498 QPixmap ScalableEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) |
|
499 { |
|
500 if (svgIcon.isNull()) |
|
501 svgIcon = QIcon(filename); |
|
502 |
|
503 // Simply reuse svg icon engine |
|
504 return svgIcon.pixmap(size, mode, state); |
|
505 } |
|
506 |
|
507 QPixmap QIconLoaderEngine::pixmap(const QSize &size, QIcon::Mode mode, |
|
508 QIcon::State state) |
|
509 { |
|
510 ensureLoaded(); |
|
511 |
|
512 QIconLoaderEngineEntry *entry = entryForSize(size); |
|
513 if (entry) |
|
514 return entry->pixmap(size, mode, state); |
|
515 |
|
516 return QPixmap(); |
|
517 } |
|
518 |
|
519 QString QIconLoaderEngine::key() const |
|
520 { |
|
521 return QLatin1String("QIconLoaderEngine"); |
|
522 } |
|
523 |
|
524 void QIconLoaderEngine::virtual_hook(int id, void *data) |
|
525 { |
|
526 ensureLoaded(); |
|
527 |
|
528 switch (id) { |
|
529 case QIconEngineV2::AvailableSizesHook: |
|
530 { |
|
531 QIconEngineV2::AvailableSizesArgument &arg |
|
532 = *reinterpret_cast<QIconEngineV2::AvailableSizesArgument*>(data); |
|
533 const QList<QIconDirInfo> directoryKey = iconLoaderInstance()->theme().keyList(); |
|
534 arg.sizes.clear(); |
|
535 |
|
536 // Gets all sizes from the DirectoryInfo entries |
|
537 for (int i = 0 ; i < m_entries.size() ; ++i) { |
|
538 int size = m_entries.at(i)->dir.size; |
|
539 arg.sizes.append(QSize(size, size)); |
|
540 } |
|
541 } |
|
542 break; |
|
543 default: |
|
544 QIconEngineV2::virtual_hook(id, data); |
|
545 } |
|
546 } |
|
547 |
|
548 QT_END_NAMESPACE |