1
/****************************************************************************
2
**
3
** Copyright (C) 2011 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
#ifndef QT_NO_ICON
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
#endif
65
66
QT_BEGIN_NAMESPACE
67
68
Q_GLOBAL_STATIC(QIconLoader, iconLoaderInstance)
69
70
/* Theme to use in last resort, if the theme does not have the icon, neither the parents  */
71
static QString fallbackTheme()
72
{
73
#ifdef Q_WS_X11
74
    if (X11->desktopEnvironment == DE_GNOME) {
75
        return QLatin1String("gnome");
76
    } else if (X11->desktopEnvironment == DE_KDE) {
77
        return X11->desktopVersion >= 4
78
            ? QString::fromLatin1("oxygen")
79
            : QString::fromLatin1("crystalsvg");
80
    } else {
81
        return QLatin1String("hicolor");
82
    }
83
#endif
84
    return QString();
85
}
86
87
QIconLoader::QIconLoader() :
88
        m_themeKey(1), m_supportsSvg(false), m_initialized(false)
89
{
90
}
91
92
// We lazily initialize the loader to make static icons
93
// work. Though we do not officially support this.
94
void QIconLoader::ensureInitialized()
95
{
96
    if (!m_initialized) {
97
        m_initialized = true;
98
99
        Q_ASSERT(qApp);
100
101
        m_systemTheme = qt_guiPlatformPlugin()->systemIconThemeName();
102
        if (m_systemTheme.isEmpty())
103
            m_systemTheme = fallbackTheme();
104
#ifndef QT_NO_LIBRARY
105
        QFactoryLoader iconFactoryLoader(QIconEngineFactoryInterfaceV2_iid,
106
                                         QLatin1String("/iconengines"),
107
                                         Qt::CaseInsensitive);
108
        if (iconFactoryLoader.keys().contains(QLatin1String("svg")))
109
            m_supportsSvg = true;
110
#endif //QT_NO_LIBRARY
111
    }
112
}
113
114
QIconLoader *QIconLoader::instance()
115
{
116
   return iconLoaderInstance();
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()) {
125
        QString theme = qt_guiPlatformPlugin()->systemIconThemeName();
126
        if (theme.isEmpty())
127
            theme = fallbackTheme();
128
        if (theme != m_systemTheme) {
129
            m_systemTheme = theme;
130
            invalidateKey();
131
        }
132
    }
133
}
134
135
void QIconLoader::setThemeName(const QString &themeName)
136
{
137
    m_userTheme = themeName;
138
    invalidateKey();
139
}
140
141
void QIconLoader::setThemeSearchPath(const QStringList &searchPaths)
142
{
143
    m_iconDirs = searchPaths;
144
    themeList.clear();
145
    invalidateKey();
146
}
147
148
QStringList QIconLoader::themeSearchPaths() const
149
{
150
    if (m_iconDirs.isEmpty()) {
151
        m_iconDirs = qt_guiPlatformPlugin()->iconThemeSearchPaths();
152
        // Allways add resource directory as search path
153
        m_iconDirs.append(QLatin1String(":/icons"));
154
    }
155
    return m_iconDirs;
156
}
157
158
QIconTheme::QIconTheme(const QString &themeName)
159
        : m_valid(false)
160
{
161
    QFile themeIndex;
162
163
    QList <QIconDirInfo> keyList;
164
    QStringList iconDirs = QIcon::themeSearchPaths();
165
    for ( int i = 0 ; i < iconDirs.size() ; ++i) {
166
        QDir iconDir(iconDirs[i]);
167
        QString themeDir = iconDir.path() + QLatin1Char('/') + themeName;
168
        themeIndex.setFileName(themeDir + QLatin1String("/index.theme"));
169
        if (themeIndex.exists()) {
170
            m_contentDir = themeDir;
171
            m_valid = true;
172
            break;
173
        }
174
    }
175
#ifndef QT_NO_SETTINGS
176
    if (themeIndex.exists()) {
177
        const QSettings indexReader(themeIndex.fileName(), QSettings::IniFormat);
178
        QStringListIterator keyIterator(indexReader.allKeys());
179
        while (keyIterator.hasNext()) {
180
181
            const QString key = keyIterator.next();
182
            if (key.endsWith(QLatin1String("/Size"))) {
183
                // Note the QSettings ini-format does not accept
184
                // slashes in key names, hence we have to cheat
185
                if (int size = indexReader.value(key).toInt()) {
186
                    QString directoryKey = key.left(key.size() - 5);
187
                    QIconDirInfo dirInfo(directoryKey);
188
                    dirInfo.size = size;
189
                    QString type = indexReader.value(directoryKey +
190
                                                     QLatin1String("/Type")
191
                                                     ).toString();
192
193
                    if (type == QLatin1String("Fixed"))
194
                        dirInfo.type = QIconDirInfo::Fixed;
195
                    else if (type == QLatin1String("Scalable"))
196
                        dirInfo.type = QIconDirInfo::Scalable;
197
                    else
198
                        dirInfo.type = QIconDirInfo::Threshold;
199
200
                    dirInfo.threshold = indexReader.value(directoryKey +
201
                                                        QLatin1String("/Threshold"),
202
                                                        2).toInt();
203
204
                    dirInfo.minSize = indexReader.value(directoryKey +
205
                                                         QLatin1String("/MinSize"),
206
                                                         size).toInt();
207
208
                    dirInfo.maxSize = indexReader.value(directoryKey +
209
                                                        QLatin1String("/MaxSize"),
210
                                                        size).toInt();
211
                    m_keyList.append(dirInfo);
212
                }
213
            }
214
        }
215
216
        // Parent themes provide fallbacks for missing icons
217
        m_parents = indexReader.value(
218
                QLatin1String("Icon Theme/Inherits")).toStringList();
219
220
        // Ensure a default platform fallback for all themes
221
        if (m_parents.isEmpty())
222
            m_parents.append(fallbackTheme());
223
224
        // Ensure that all themes fall back to hicolor
225
        if (!m_parents.contains(QLatin1String("hicolor")))
226
            m_parents.append(QLatin1String("hicolor"));
227
    }
228
#endif //QT_NO_SETTINGS
229
}
230
231
QThemeIconEntries QIconLoader::findIconHelper(const QString &themeName,
232
                                 const QString &iconName,
233
                                 QStringList &visited) const
234
{
235
    QThemeIconEntries entries;
236
    Q_ASSERT(!themeName.isEmpty());
237
238
    QPixmap pixmap;
239
240
    // Used to protect against potential recursions
241
    visited << themeName;
242
243
    QIconTheme theme = themeList.value(themeName);
244
    if (!theme.isValid()) {
245
        theme = QIconTheme(themeName);
246
        if (!theme.isValid())
247
            theme = QIconTheme(fallbackTheme());
248
249
        themeList.insert(themeName, theme);
250
    }
251
252
    QString contentDir = theme.contentDir() + QLatin1Char('/');
253
    QList<QIconDirInfo> subDirs = theme.keyList();
254
255
    const QString svgext(QLatin1String(".svg"));
256
    const QString pngext(QLatin1String(".png"));
257
258
    // Add all relevant files
259
    for (int i = 0; i < subDirs.size() ; ++i) {
260
        const QIconDirInfo &dirInfo = subDirs.at(i);
261
        QString subdir = dirInfo.path;
262
        QDir currentDir(contentDir + subdir);
263
        if (currentDir.exists(iconName + pngext)) {
264
            PixmapEntry *iconEntry = new PixmapEntry;
265
            iconEntry->dir = dirInfo;
266
            iconEntry->filename = currentDir.filePath(iconName + pngext);
267
            // Notice we ensure that pixmap entries allways come before
268
            // scalable to preserve search order afterwards
269
            entries.prepend(iconEntry);
270
        } else if (m_supportsSvg &&
271
            currentDir.exists(iconName + svgext)) {
272
            ScalableEntry *iconEntry = new ScalableEntry;
273
            iconEntry->dir = dirInfo;
274
            iconEntry->filename = currentDir.filePath(iconName + svgext);
275
            entries.append(iconEntry);
276
        }
277
    }
278
279
    if (entries.isEmpty()) {
280
        const QStringList parents = theme.parents();
281
        // Search recursively through inherited themes
282
        for (int i = 0 ; i < parents.size() ; ++i) {
283
284
            const QString parentTheme = parents.at(i).trimmed();
285
286
            if (!visited.contains(parentTheme)) // guard against recursion
287
                entries = findIconHelper(parentTheme, iconName, visited);
288
289
            if (!entries.isEmpty()) // success
290
                break;
291
        }
292
    }
293
    return entries;
294
}
295
296
QThemeIconEntries QIconLoader::loadIcon(const QString &name) const
297
{
298
    if (!themeName().isEmpty()) {
299
        QStringList visited;
300
        return findIconHelper(themeName(), name, visited);
301
    }
302
303
    return QThemeIconEntries();
304
}
305
306
307
// -------- Icon Loader Engine -------- //
308
309
310
QIconLoaderEngine::QIconLoaderEngine(const QString& iconName)
311
        : m_iconName(iconName), m_key(0)
312
{
313
}
314
315
QIconLoaderEngine::~QIconLoaderEngine()
316
{
317
    while (!m_entries.isEmpty())
318
        delete m_entries.takeLast();
319
    Q_ASSERT(m_entries.size() == 0);
320
}
321
322
QIconLoaderEngine::QIconLoaderEngine(const QIconLoaderEngine &other)
323
        : QIconEngineV2(other),
324
        m_iconName(other.m_iconName),
325
        m_key(0)
326
{
327
}
328
329
QIconEngineV2 *QIconLoaderEngine::clone() const
330
{
331
    return new QIconLoaderEngine(*this);
332
}
333
334
bool QIconLoaderEngine::read(QDataStream &in) {
335
    in >> m_iconName;
336
    return true;
337
}
338
339
bool QIconLoaderEngine::write(QDataStream &out) const
340
{
341
    out << m_iconName;
342
    return true;
343
}
344
345
bool QIconLoaderEngine::hasIcon() const
346
{
347
    return !(m_entries.isEmpty());
348
}
349
350
// Lazily load the icon
351
void QIconLoaderEngine::ensureLoaded()
352
{
353
354
    iconLoaderInstance()->ensureInitialized();
355
356
    if (!(iconLoaderInstance()->themeKey() == m_key)) {
357
358
        while (!m_entries.isEmpty())
359
            delete m_entries.takeLast();
360
361
        Q_ASSERT(m_entries.size() == 0);
362
        m_entries = iconLoaderInstance()->loadIcon(m_iconName);
363
        m_key = iconLoaderInstance()->themeKey();
364
    }
365
}
366
367
void QIconLoaderEngine::paint(QPainter *painter, const QRect &rect,
368
                             QIcon::Mode mode, QIcon::State state)
369
{
370
    QSize pixmapSize = rect.size();
371
#if defined(Q_WS_MAC)
372
    pixmapSize *= qt_mac_get_scalefactor();
373
#endif
374
    painter->drawPixmap(rect, pixmap(pixmapSize, mode, state));
375
}
376
377
/*
378
 * This algorithm is defined by the freedesktop spec:
379
 * http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
380
 */
381
static bool directoryMatchesSize(const QIconDirInfo &dir, int iconsize)
382
{
383
    if (dir.type == QIconDirInfo::Fixed) {
384
        return dir.size == iconsize;
385
386
    } else if (dir.type == QIconDirInfo::Scalable) {
387
        return dir.size <= dir.maxSize &&
388
                iconsize >= dir.minSize;
389
390
    } else if (dir.type == QIconDirInfo::Threshold) {
391
        return iconsize >= dir.size - dir.threshold &&
392
                iconsize <= dir.size + dir.threshold;
393
    }
394
395
    Q_ASSERT(1); // Not a valid value
396
    return false;
397
}
398
399
/*
400
 * This algorithm is defined by the freedesktop spec:
401
 * http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
402
 */
403
static int directorySizeDistance(const QIconDirInfo &dir, int iconsize)
404
{
405
    if (dir.type == QIconDirInfo::Fixed) {
406
        return qAbs(dir.size - iconsize);
407
408
    } else if (dir.type == QIconDirInfo::Scalable) {
409
        if (iconsize < dir.minSize)
410
            return dir.minSize - iconsize;
411
        else if (iconsize > dir.maxSize)
412
            return iconsize - dir.maxSize;
413
        else
414
            return 0;
415
416
    } else if (dir.type == QIconDirInfo::Threshold) {
417
        if (iconsize < dir.size - dir.threshold)
418
            return dir.minSize - iconsize;
419
        else if (iconsize > dir.size + dir.threshold)
420
            return iconsize - dir.maxSize;
421
        else return 0;
422
    }
423
424
    Q_ASSERT(1); // Not a valid value
425
    return INT_MAX;
426
}
427
428
QIconLoaderEngineEntry *QIconLoaderEngine::entryForSize(const QSize &size)
429
{
430
    int iconsize = qMin(size.width(), size.height());
431
432
    // Note that m_entries are sorted so that png-files
433
    // come first
434
435
    // Search for exact matches first
436
    for (int i = 0; i < m_entries.count(); ++i) {
437
        QIconLoaderEngineEntry *entry = m_entries.at(i);
438
        if (directoryMatchesSize(entry->dir, iconsize)) {
439
            return entry;
440
        }
441
    }
442
443
    // Find the minimum distance icon
444
    int minimalSize = INT_MAX;
445
    QIconLoaderEngineEntry *closestMatch = 0;
446
    for (int i = 0; i < m_entries.count(); ++i) {
447
        QIconLoaderEngineEntry *entry = m_entries.at(i);
448
        int distance = directorySizeDistance(entry->dir, iconsize);
449
        if (distance < minimalSize) {
450
            minimalSize  = distance;
451
            closestMatch = entry;
452
        }
453
    }
454
    return closestMatch;
455
}
456
457
/*
458
 * Returns the actual icon size. For scalable svg's this is equivalent
459
 * to the requested size. Otherwise the closest match is returned but
460
 * we can never return a bigger size than the requested size.
461
 *
462
 */
463
QSize QIconLoaderEngine::actualSize(const QSize &size, QIcon::Mode mode,
464
                                   QIcon::State state)
465
{
466
    ensureLoaded();
467
468
    QIconLoaderEngineEntry *entry = entryForSize(size);
469
    if (entry) {
470
        const QIconDirInfo &dir = entry->dir;
471
        if (dir.type == QIconDirInfo::Scalable)
472
            return size;
473
        else {
474
            int result = qMin<int>(dir.size, qMin(size.width(), size.height()));
475
            return QSize(result, result);
476
        }
477
    }
478
    return QIconEngineV2::actualSize(size, mode, state);
479
}
480
481
QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
482
{
483
    Q_UNUSED(state);
484
485
    // Ensure that basePixmap is lazily initialized before generating the
486
    // key, otherwise the cache key is not unique
487
    if (basePixmap.isNull())
488
        basePixmap.load(filename);
489
490
    int actualSize = qMin(size.width(), size.height());
491
    QString key = QLatin1String("$qt_theme_")
492
                  + QString::number(basePixmap.cacheKey(), 16)
493
                  + QLatin1Char('_')
494
                  + QString::number(mode)
495
                  + QLatin1Char('_')
496
                  + QString::number(qApp->palette().cacheKey(), 16)
497
                  + QLatin1Char('_')
498
                  + QString::number(actualSize);
499
500
    QPixmap cachedPixmap;
501
    if (QPixmapCache::find(key, &cachedPixmap)) {
502
        return cachedPixmap;
503
    } else {
504
        QStyleOption opt(0);
505
        opt.palette = qApp->palette();
506
        cachedPixmap = qApp->style()->generatedIconPixmap(mode, basePixmap, &opt);
507
        QPixmapCache::insert(key, cachedPixmap);
508
    }
509
    return cachedPixmap;
510
}
511
512
QPixmap ScalableEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
513
{
514
    if (svgIcon.isNull())
515
        svgIcon = QIcon(filename);
516
517
    // Simply reuse svg icon engine
518
    return svgIcon.pixmap(size, mode, state);
519
}
520
521
QPixmap QIconLoaderEngine::pixmap(const QSize &size, QIcon::Mode mode,
522
                                 QIcon::State state)
523
{
524
    ensureLoaded();
525
526
    QIconLoaderEngineEntry *entry = entryForSize(size);
527
    if (entry)
528
        return entry->pixmap(size, mode, state);
529
530
    return QPixmap();
531
}
532
533
QString QIconLoaderEngine::key() const
534
{
535
    return QLatin1String("QIconLoaderEngine");
536
}
537
538
void QIconLoaderEngine::virtual_hook(int id, void *data)
539
{
540
    ensureLoaded();
541
542
    switch (id) {
543
    case QIconEngineV2::AvailableSizesHook:
544
        {
545
            QIconEngineV2::AvailableSizesArgument &arg
546
                    = *reinterpret_cast<QIconEngineV2::AvailableSizesArgument*>(data);
547
            const QList<QIconDirInfo> directoryKey = iconLoaderInstance()->theme().keyList();
548
            arg.sizes.clear();
549
550
            // Gets all sizes from the DirectoryInfo entries
551
            for (int i = 0 ; i < m_entries.size() ; ++i) {
552
                int size = m_entries.at(i)->dir.size;
553
                arg.sizes.append(QSize(size, size));
554
            }
555
        }
556
        break;
557
    default:
558
        QIconEngineV2::virtual_hook(id, data);
559
    }
560
}
561
562
QT_END_NAMESPACE
563
564
#endif //QT_NO_ICON