1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include <private/qiconloader_p.h>
44 #include <private/qguiapplication_p.h>
45 #include <private/qicon_p.h>
47 #include <QtGui/QIconEnginePlugin>
48 #include <QtGui/QPixmapCache>
49 #include <qpa/qplatformtheme.h>
50 #include <QtGui/QIconEngine>
51 #include <QtGui/QPalette>
52 #include <QtCore/QList>
53 #include <QtCore/QHash>
54 #include <QtCore/QDir>
55 #include <QtCore/QSettings>
56 #include <QtGui/QPainter>
59 #include <private/qt_cocoa_helpers_mac_p.h>
62 #include <private/qhexstring_p.h>
66 Q_GLOBAL_STATIC(QIconLoader, iconLoaderInstance)
68 /* Theme to use in last resort, if the theme does not have the icon, neither the parents */
69 static QString fallbackTheme()
71 if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
72 const QVariant themeHint = theme->themeHint(QPlatformTheme::SystemIconThemeName);
73 if (themeHint.isValid())
74 return themeHint.toString();
79 QIconLoader::QIconLoader() :
80 m_themeKey(1), m_supportsSvg(false), m_initialized(false)
84 // We lazily initialize the loader to make static icons
85 // work. Though we do not officially support this.
87 static inline QString systemThemeName()
89 if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
90 const QVariant themeHint = theme->themeHint(QPlatformTheme::SystemIconThemeName);
91 if (themeHint.isValid())
92 return themeHint.toString();
97 static inline QStringList systemIconSearchPaths()
99 if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
100 const QVariant themeHint = theme->themeHint(QPlatformTheme::IconThemeSearchPaths);
101 if (themeHint.isValid())
102 return themeHint.toStringList();
104 return QStringList();
107 void QIconLoader::ensureInitialized()
109 if (!m_initialized) {
110 m_initialized = true;
114 m_systemTheme = systemThemeName();
116 if (m_systemTheme.isEmpty())
117 m_systemTheme = fallbackTheme();
118 #ifndef QT_NO_LIBRARY
119 QFactoryLoader iconFactoryLoader(QIconEngineFactoryInterface_iid,
120 QLatin1String("/iconengines"),
121 Qt::CaseInsensitive);
122 if (iconFactoryLoader.keyMap().key(QLatin1String("svg"), -1) != -1)
123 m_supportsSvg = true;
124 #endif //QT_NO_LIBRARY
128 QIconLoader *QIconLoader::instance()
130 return iconLoaderInstance();
133 // Queries the system theme and invalidates existing
134 // icons if the theme has changed.
135 void QIconLoader::updateSystemTheme()
137 // Only change if this is not explicitly set by the user
138 if (m_userTheme.isEmpty()) {
139 QString theme = systemThemeName();
141 theme = fallbackTheme();
142 if (theme != m_systemTheme) {
143 m_systemTheme = theme;
149 void QIconLoader::setThemeName(const QString &themeName)
151 m_userTheme = themeName;
155 void QIconLoader::setThemeSearchPath(const QStringList &searchPaths)
157 m_iconDirs = searchPaths;
162 QStringList QIconLoader::themeSearchPaths() const
164 if (m_iconDirs.isEmpty()) {
165 m_iconDirs = systemIconSearchPaths();
166 // Always add resource directory as search path
167 m_iconDirs.append(QLatin1String(":/icons"));
172 QIconTheme::QIconTheme(const QString &themeName)
177 QList <QIconDirInfo> keyList;
178 QStringList iconDirs = QIcon::themeSearchPaths();
179 for ( int i = 0 ; i < iconDirs.size() ; ++i) {
180 QDir iconDir(iconDirs[i]);
181 QString themeDir = iconDir.path() + QLatin1Char('/') + themeName;
182 themeIndex.setFileName(themeDir + QLatin1String("/index.theme"));
183 if (themeIndex.exists()) {
184 m_contentDir = themeDir;
189 #ifndef QT_NO_SETTINGS
190 if (themeIndex.exists()) {
191 const QSettings indexReader(themeIndex.fileName(), QSettings::IniFormat);
192 QStringListIterator keyIterator(indexReader.allKeys());
193 while (keyIterator.hasNext()) {
195 const QString key = keyIterator.next();
196 if (key.endsWith(QLatin1String("/Size"))) {
197 // Note the QSettings ini-format does not accept
198 // slashes in key names, hence we have to cheat
199 if (int size = indexReader.value(key).toInt()) {
200 QString directoryKey = key.left(key.size() - 5);
201 QIconDirInfo dirInfo(directoryKey);
203 QString type = indexReader.value(directoryKey +
204 QLatin1String("/Type")
207 if (type == QLatin1String("Fixed"))
208 dirInfo.type = QIconDirInfo::Fixed;
209 else if (type == QLatin1String("Scalable"))
210 dirInfo.type = QIconDirInfo::Scalable;
212 dirInfo.type = QIconDirInfo::Threshold;
214 dirInfo.threshold = indexReader.value(directoryKey +
215 QLatin1String("/Threshold"),
218 dirInfo.minSize = indexReader.value(directoryKey +
219 QLatin1String("/MinSize"),
222 dirInfo.maxSize = indexReader.value(directoryKey +
223 QLatin1String("/MaxSize"),
225 m_keyList.append(dirInfo);
230 // Parent themes provide fallbacks for missing icons
231 m_parents = indexReader.value(
232 QLatin1String("Icon Theme/Inherits")).toStringList();
234 // Ensure a default platform fallback for all themes
235 if (m_parents.isEmpty()) {
236 const QString fallback = fallbackTheme();
237 if (!fallback.isEmpty())
238 m_parents.append(fallback);
241 // Ensure that all themes fall back to hicolor
242 if (!m_parents.contains(QLatin1String("hicolor")))
243 m_parents.append(QLatin1String("hicolor"));
245 #endif //QT_NO_SETTINGS
248 QThemeIconEntries QIconLoader::findIconHelper(const QString &themeName,
249 const QString &iconName,
250 QStringList &visited) const
252 QThemeIconEntries entries;
253 Q_ASSERT(!themeName.isEmpty());
257 // Used to protect against potential recursions
258 visited << themeName;
260 QIconTheme theme = themeList.value(themeName);
261 if (!theme.isValid()) {
262 theme = QIconTheme(themeName);
263 if (!theme.isValid())
264 theme = QIconTheme(fallbackTheme());
266 themeList.insert(themeName, theme);
269 QString contentDir = theme.contentDir() + QLatin1Char('/');
270 QList<QIconDirInfo> subDirs = theme.keyList();
272 const QString svgext(QLatin1String(".svg"));
273 const QString pngext(QLatin1String(".png"));
275 // Add all relevant files
276 for (int i = 0; i < subDirs.size() ; ++i) {
277 const QIconDirInfo &dirInfo = subDirs.at(i);
278 QString subdir = dirInfo.path;
279 QDir currentDir(contentDir + subdir);
280 if (currentDir.exists(iconName + pngext)) {
281 PixmapEntry *iconEntry = new PixmapEntry;
282 iconEntry->dir = dirInfo;
283 iconEntry->filename = currentDir.filePath(iconName + pngext);
284 // Notice we ensure that pixmap entries always come before
285 // scalable to preserve search order afterwards
286 entries.prepend(iconEntry);
287 } else if (m_supportsSvg &&
288 currentDir.exists(iconName + svgext)) {
289 ScalableEntry *iconEntry = new ScalableEntry;
290 iconEntry->dir = dirInfo;
291 iconEntry->filename = currentDir.filePath(iconName + svgext);
292 entries.append(iconEntry);
296 if (entries.isEmpty()) {
297 const QStringList parents = theme.parents();
298 // Search recursively through inherited themes
299 for (int i = 0 ; i < parents.size() ; ++i) {
301 const QString parentTheme = parents.at(i).trimmed();
303 if (!visited.contains(parentTheme)) // guard against recursion
304 entries = findIconHelper(parentTheme, iconName, visited);
306 if (!entries.isEmpty()) // success
313 QThemeIconEntries QIconLoader::loadIcon(const QString &name) const
315 if (!themeName().isEmpty()) {
317 return findIconHelper(themeName(), name, visited);
320 return QThemeIconEntries();
324 // -------- Icon Loader Engine -------- //
327 QIconLoaderEngine::QIconLoaderEngine(const QString& iconName)
328 : m_iconName(iconName), m_key(0)
332 QIconLoaderEngine::~QIconLoaderEngine()
334 while (!m_entries.isEmpty())
335 delete m_entries.takeLast();
336 Q_ASSERT(m_entries.size() == 0);
339 QIconLoaderEngine::QIconLoaderEngine(const QIconLoaderEngine &other)
340 : QIconEngine(other),
341 m_iconName(other.m_iconName),
346 QIconEngine *QIconLoaderEngine::clone() const
348 return new QIconLoaderEngine(*this);
351 bool QIconLoaderEngine::read(QDataStream &in) {
356 bool QIconLoaderEngine::write(QDataStream &out) const
362 bool QIconLoaderEngine::hasIcon() const
364 return !(m_entries.isEmpty());
367 // Lazily load the icon
368 void QIconLoaderEngine::ensureLoaded()
371 iconLoaderInstance()->ensureInitialized();
373 if (!(iconLoaderInstance()->themeKey() == m_key)) {
375 while (!m_entries.isEmpty())
376 delete m_entries.takeLast();
378 Q_ASSERT(m_entries.size() == 0);
379 m_entries = iconLoaderInstance()->loadIcon(m_iconName);
380 m_key = iconLoaderInstance()->themeKey();
384 void QIconLoaderEngine::paint(QPainter *painter, const QRect &rect,
385 QIcon::Mode mode, QIcon::State state)
387 QSize pixmapSize = rect.size();
388 #if defined(Q_WS_MAC)
389 pixmapSize *= qt_mac_get_scalefactor();
391 painter->drawPixmap(rect, pixmap(pixmapSize, mode, state));
395 * This algorithm is defined by the freedesktop spec:
396 * http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
398 static bool directoryMatchesSize(const QIconDirInfo &dir, int iconsize)
400 if (dir.type == QIconDirInfo::Fixed) {
401 return dir.size == iconsize;
403 } else if (dir.type == QIconDirInfo::Scalable) {
404 return dir.size <= dir.maxSize &&
405 iconsize >= dir.minSize;
407 } else if (dir.type == QIconDirInfo::Threshold) {
408 return iconsize >= dir.size - dir.threshold &&
409 iconsize <= dir.size + dir.threshold;
412 Q_ASSERT(1); // Not a valid value
417 * This algorithm is defined by the freedesktop spec:
418 * http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
420 static int directorySizeDistance(const QIconDirInfo &dir, int iconsize)
422 if (dir.type == QIconDirInfo::Fixed) {
423 return qAbs(dir.size - iconsize);
425 } else if (dir.type == QIconDirInfo::Scalable) {
426 if (iconsize < dir.minSize)
427 return dir.minSize - iconsize;
428 else if (iconsize > dir.maxSize)
429 return iconsize - dir.maxSize;
433 } else if (dir.type == QIconDirInfo::Threshold) {
434 if (iconsize < dir.size - dir.threshold)
435 return dir.minSize - iconsize;
436 else if (iconsize > dir.size + dir.threshold)
437 return iconsize - dir.maxSize;
441 Q_ASSERT(1); // Not a valid value
445 QIconLoaderEngineEntry *QIconLoaderEngine::entryForSize(const QSize &size)
447 int iconsize = qMin(size.width(), size.height());
449 // Note that m_entries are sorted so that png-files
452 // Search for exact matches first
453 for (int i = 0; i < m_entries.count(); ++i) {
454 QIconLoaderEngineEntry *entry = m_entries.at(i);
455 if (directoryMatchesSize(entry->dir, iconsize)) {
460 // Find the minimum distance icon
461 int minimalSize = INT_MAX;
462 QIconLoaderEngineEntry *closestMatch = 0;
463 for (int i = 0; i < m_entries.count(); ++i) {
464 QIconLoaderEngineEntry *entry = m_entries.at(i);
465 int distance = directorySizeDistance(entry->dir, iconsize);
466 if (distance < minimalSize) {
467 minimalSize = distance;
468 closestMatch = entry;
475 * Returns the actual icon size. For scalable svg's this is equivalent
476 * to the requested size. Otherwise the closest match is returned but
477 * we can never return a bigger size than the requested size.
480 QSize QIconLoaderEngine::actualSize(const QSize &size, QIcon::Mode mode,
485 QIconLoaderEngineEntry *entry = entryForSize(size);
487 const QIconDirInfo &dir = entry->dir;
488 if (dir.type == QIconDirInfo::Scalable)
491 int result = qMin<int>(dir.size, qMin(size.width(), size.height()));
492 return QSize(result, result);
495 return QIconEngine::actualSize(size, mode, state);
498 QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
502 // Ensure that basePixmap is lazily initialized before generating the
503 // key, otherwise the cache key is not unique
504 if (basePixmap.isNull())
505 basePixmap.load(filename);
507 QSize actualSize = basePixmap.size();
508 if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
509 actualSize.scale(size, Qt::KeepAspectRatio);
511 QString key = QLatin1String("$qt_theme_")
512 % HexString<qint64>(basePixmap.cacheKey())
513 % HexString<int>(mode)
514 % HexString<qint64>(QGuiApplication::palette().cacheKey())
515 % HexString<int>(actualSize.width())
516 % HexString<int>(actualSize.height());
518 QPixmap cachedPixmap;
519 if (QPixmapCache::find(key, &cachedPixmap)) {
522 if (basePixmap.size() != actualSize)
523 basePixmap = basePixmap.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
524 cachedPixmap = basePixmap;
525 if (QGuiApplication *guiApp = qobject_cast<QGuiApplication *>(qApp))
526 cachedPixmap = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(mode, basePixmap);
527 QPixmapCache::insert(key, cachedPixmap);
532 QPixmap ScalableEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
534 if (svgIcon.isNull())
535 svgIcon = QIcon(filename);
537 // Simply reuse svg icon engine
538 return svgIcon.pixmap(size, mode, state);
541 QPixmap QIconLoaderEngine::pixmap(const QSize &size, QIcon::Mode mode,
546 QIconLoaderEngineEntry *entry = entryForSize(size);
548 return entry->pixmap(size, mode, state);
553 QString QIconLoaderEngine::key() const
555 return QLatin1String("QIconLoaderEngine");
558 void QIconLoaderEngine::virtual_hook(int id, void *data)
563 case QIconEngine::AvailableSizesHook:
565 QIconEngine::AvailableSizesArgument &arg
566 = *reinterpret_cast<QIconEngine::AvailableSizesArgument*>(data);
567 const QList<QIconDirInfo> directoryKey = iconLoaderInstance()->theme().keyList();
570 // Gets all sizes from the DirectoryInfo entries
571 for (int i = 0 ; i < m_entries.size() ; ++i) {
572 int size = m_entries.at(i)->dir.size;
573 arg.sizes.append(QSize(size, size));
577 case QIconEngine::IconNameHook:
579 QString &name = *reinterpret_cast<QString*>(data);
584 QIconEngine::virtual_hook(id, data);