Scale icons returned by QIcon::fromTheme.
[profile/ivi/qtbase.git] / src / gui / image / qiconloader.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #ifndef QT_NO_ICON
42 #include <private/qiconloader_p.h>
43
44 #include <private/qguiapplication_p.h>
45 #include <private/qicon_p.h>
46
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>
57
58 #ifdef Q_WS_MAC
59 #include <private/qt_cocoa_helpers_mac_p.h>
60 #endif
61
62 #include <private/qhexstring_p.h>
63
64 QT_BEGIN_NAMESPACE
65
66 Q_GLOBAL_STATIC(QIconLoader, iconLoaderInstance)
67
68 /* Theme to use in last resort, if the theme does not have the icon, neither the parents  */
69 static QString fallbackTheme()
70 {
71     if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
72         const QVariant themeHint = theme->themeHint(QPlatformTheme::SystemIconThemeName);
73         if (themeHint.isValid())
74             return themeHint.toString();
75     }
76     return QString();
77 }
78
79 QIconLoader::QIconLoader() :
80         m_themeKey(1), m_supportsSvg(false), m_initialized(false)
81 {
82 }
83
84 // We lazily initialize the loader to make static icons
85 // work. Though we do not officially support this.
86
87 static inline QString systemThemeName()
88 {
89     if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
90         const QVariant themeHint = theme->themeHint(QPlatformTheme::SystemIconThemeName);
91         if (themeHint.isValid())
92             return themeHint.toString();
93     }
94     return QString();
95 }
96
97 static inline QStringList systemIconSearchPaths()
98 {
99     if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
100         const QVariant themeHint = theme->themeHint(QPlatformTheme::IconThemeSearchPaths);
101         if (themeHint.isValid())
102             return themeHint.toStringList();
103     }
104     return QStringList();
105 }
106
107 void QIconLoader::ensureInitialized()
108 {
109     if (!m_initialized) {
110         m_initialized = true;
111
112         Q_ASSERT(qApp);
113
114         m_systemTheme = systemThemeName();
115
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
125     }
126 }
127
128 QIconLoader *QIconLoader::instance()
129 {
130    return iconLoaderInstance();
131 }
132
133 // Queries the system theme and invalidates existing
134 // icons if the theme has changed.
135 void QIconLoader::updateSystemTheme()
136 {
137     // Only change if this is not explicitly set by the user
138     if (m_userTheme.isEmpty()) {
139         QString theme = systemThemeName();
140         if (theme.isEmpty())
141             theme = fallbackTheme();
142         if (theme != m_systemTheme) {
143             m_systemTheme = theme;
144             invalidateKey();
145         }
146     }
147 }
148
149 void QIconLoader::setThemeName(const QString &themeName)
150 {
151     m_userTheme = themeName;
152     invalidateKey();
153 }
154
155 void QIconLoader::setThemeSearchPath(const QStringList &searchPaths)
156 {
157     m_iconDirs = searchPaths;
158     themeList.clear();
159     invalidateKey();
160 }
161
162 QStringList QIconLoader::themeSearchPaths() const
163 {
164     if (m_iconDirs.isEmpty()) {
165         m_iconDirs = systemIconSearchPaths();
166         // Always add resource directory as search path
167         m_iconDirs.append(QLatin1String(":/icons"));
168     }
169     return m_iconDirs;
170 }
171
172 QIconTheme::QIconTheme(const QString &themeName)
173         : m_valid(false)
174 {
175     QFile themeIndex;
176
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;
185             m_valid = true;
186             break;
187         }
188     }
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()) {
194
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);
202                     dirInfo.size = size;
203                     QString type = indexReader.value(directoryKey +
204                                                      QLatin1String("/Type")
205                                                      ).toString();
206
207                     if (type == QLatin1String("Fixed"))
208                         dirInfo.type = QIconDirInfo::Fixed;
209                     else if (type == QLatin1String("Scalable"))
210                         dirInfo.type = QIconDirInfo::Scalable;
211                     else
212                         dirInfo.type = QIconDirInfo::Threshold;
213
214                     dirInfo.threshold = indexReader.value(directoryKey +
215                                                         QLatin1String("/Threshold"),
216                                                         2).toInt();
217
218                     dirInfo.minSize = indexReader.value(directoryKey +
219                                                          QLatin1String("/MinSize"),
220                                                          size).toInt();
221
222                     dirInfo.maxSize = indexReader.value(directoryKey +
223                                                         QLatin1String("/MaxSize"),
224                                                         size).toInt();
225                     m_keyList.append(dirInfo);
226                 }
227             }
228         }
229
230         // Parent themes provide fallbacks for missing icons
231         m_parents = indexReader.value(
232                 QLatin1String("Icon Theme/Inherits")).toStringList();
233
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);
239         }
240
241         // Ensure that all themes fall back to hicolor
242         if (!m_parents.contains(QLatin1String("hicolor")))
243             m_parents.append(QLatin1String("hicolor"));
244     }
245 #endif //QT_NO_SETTINGS
246 }
247
248 QThemeIconEntries QIconLoader::findIconHelper(const QString &themeName,
249                                  const QString &iconName,
250                                  QStringList &visited) const
251 {
252     QThemeIconEntries entries;
253     Q_ASSERT(!themeName.isEmpty());
254
255     QPixmap pixmap;
256
257     // Used to protect against potential recursions
258     visited << themeName;
259
260     QIconTheme theme = themeList.value(themeName);
261     if (!theme.isValid()) {
262         theme = QIconTheme(themeName);
263         if (!theme.isValid())
264             theme = QIconTheme(fallbackTheme());
265
266         themeList.insert(themeName, theme);
267     }
268
269     QString contentDir = theme.contentDir() + QLatin1Char('/');
270     QList<QIconDirInfo> subDirs = theme.keyList();
271
272     const QString svgext(QLatin1String(".svg"));
273     const QString pngext(QLatin1String(".png"));
274
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);
293         }
294     }
295
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) {
300
301             const QString parentTheme = parents.at(i).trimmed();
302
303             if (!visited.contains(parentTheme)) // guard against recursion
304                 entries = findIconHelper(parentTheme, iconName, visited);
305
306             if (!entries.isEmpty()) // success
307                 break;
308         }
309     }
310     return entries;
311 }
312
313 QThemeIconEntries QIconLoader::loadIcon(const QString &name) const
314 {
315     if (!themeName().isEmpty()) {
316         QStringList visited;
317         return findIconHelper(themeName(), name, visited);
318     }
319
320     return QThemeIconEntries();
321 }
322
323
324 // -------- Icon Loader Engine -------- //
325
326
327 QIconLoaderEngine::QIconLoaderEngine(const QString& iconName)
328         : m_iconName(iconName), m_key(0)
329 {
330 }
331
332 QIconLoaderEngine::~QIconLoaderEngine()
333 {
334     while (!m_entries.isEmpty())
335         delete m_entries.takeLast();
336     Q_ASSERT(m_entries.size() == 0);
337 }
338
339 QIconLoaderEngine::QIconLoaderEngine(const QIconLoaderEngine &other)
340         : QIconEngine(other),
341         m_iconName(other.m_iconName),
342         m_key(0)
343 {
344 }
345
346 QIconEngine *QIconLoaderEngine::clone() const
347 {
348     return new QIconLoaderEngine(*this);
349 }
350
351 bool QIconLoaderEngine::read(QDataStream &in) {
352     in >> m_iconName;
353     return true;
354 }
355
356 bool QIconLoaderEngine::write(QDataStream &out) const
357 {
358     out << m_iconName;
359     return true;
360 }
361
362 bool QIconLoaderEngine::hasIcon() const
363 {
364     return !(m_entries.isEmpty());
365 }
366
367 // Lazily load the icon
368 void QIconLoaderEngine::ensureLoaded()
369 {
370
371     iconLoaderInstance()->ensureInitialized();
372
373     if (!(iconLoaderInstance()->themeKey() == m_key)) {
374
375         while (!m_entries.isEmpty())
376             delete m_entries.takeLast();
377
378         Q_ASSERT(m_entries.size() == 0);
379         m_entries = iconLoaderInstance()->loadIcon(m_iconName);
380         m_key = iconLoaderInstance()->themeKey();
381     }
382 }
383
384 void QIconLoaderEngine::paint(QPainter *painter, const QRect &rect,
385                              QIcon::Mode mode, QIcon::State state)
386 {
387     QSize pixmapSize = rect.size();
388 #if defined(Q_WS_MAC)
389     pixmapSize *= qt_mac_get_scalefactor();
390 #endif
391     painter->drawPixmap(rect, pixmap(pixmapSize, mode, state));
392 }
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) {
401         return dir.size == iconsize;
402
403     } else if (dir.type == QIconDirInfo::Scalable) {
404         return dir.size <= dir.maxSize &&
405                 iconsize >= dir.minSize;
406
407     } else if (dir.type == QIconDirInfo::Threshold) {
408         return iconsize >= dir.size - dir.threshold &&
409                 iconsize <= dir.size + dir.threshold;
410     }
411
412     Q_ASSERT(1); // Not a valid value
413     return false;
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) {
423         return qAbs(dir.size - iconsize);
424
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;
430         else
431             return 0;
432
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;
438         else return 0;
439     }
440
441     Q_ASSERT(1); // Not a valid value
442     return INT_MAX;
443 }
444
445 QIconLoaderEngineEntry *QIconLoaderEngine::entryForSize(const QSize &size)
446 {
447     int iconsize = qMin(size.width(), size.height());
448
449     // Note that m_entries are sorted so that png-files
450     // come first
451
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)) {
456             return entry;
457         }
458     }
459
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;
469         }
470     }
471     return closestMatch;
472 }
473
474 /*
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.
478  *
479  */
480 QSize QIconLoaderEngine::actualSize(const QSize &size, QIcon::Mode mode,
481                                    QIcon::State state)
482 {
483     ensureLoaded();
484
485     QIconLoaderEngineEntry *entry = entryForSize(size);
486     if (entry) {
487         const QIconDirInfo &dir = entry->dir;
488         if (dir.type == QIconDirInfo::Scalable)
489             return size;
490         else {
491             int result = qMin<int>(dir.size, qMin(size.width(), size.height()));
492             return QSize(result, result);
493         }
494     }
495     return QIconEngine::actualSize(size, mode, state);
496 }
497
498 QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
499 {
500     Q_UNUSED(state);
501
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);
506
507     QSize actualSize = basePixmap.size();
508     if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
509         actualSize.scale(size, Qt::KeepAspectRatio);
510
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());
517
518     QPixmap cachedPixmap;
519     if (QPixmapCache::find(key, &cachedPixmap)) {
520         return cachedPixmap;
521     } else {
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);
528     }
529     return cachedPixmap;
530 }
531
532 QPixmap ScalableEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
533 {
534     if (svgIcon.isNull())
535         svgIcon = QIcon(filename);
536
537     // Simply reuse svg icon engine
538     return svgIcon.pixmap(size, mode, state);
539 }
540
541 QPixmap QIconLoaderEngine::pixmap(const QSize &size, QIcon::Mode mode,
542                                  QIcon::State state)
543 {
544     ensureLoaded();
545
546     QIconLoaderEngineEntry *entry = entryForSize(size);
547     if (entry)
548         return entry->pixmap(size, mode, state);
549
550     return QPixmap();
551 }
552
553 QString QIconLoaderEngine::key() const
554 {
555     return QLatin1String("QIconLoaderEngine");
556 }
557
558 void QIconLoaderEngine::virtual_hook(int id, void *data)
559 {
560     ensureLoaded();
561
562     switch (id) {
563     case QIconEngine::AvailableSizesHook:
564         {
565             QIconEngine::AvailableSizesArgument &arg
566                     = *reinterpret_cast<QIconEngine::AvailableSizesArgument*>(data);
567             const QList<QIconDirInfo> directoryKey = iconLoaderInstance()->theme().keyList();
568             arg.sizes.clear();
569
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));
574             }
575         }
576         break;
577     case QIconEngine::IconNameHook:
578         {
579             QString &name = *reinterpret_cast<QString*>(data);
580             name = m_iconName;
581         }
582         break;
583     default:
584         QIconEngine::virtual_hook(id, data);
585     }
586 }
587
588 QT_END_NAMESPACE
589
590 #endif //QT_NO_ICON