| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QICONLOADER_P_H |
| 41 | #define QICONLOADER_P_H |
| 42 | |
| 43 | #include <QtGui/private/qtguiglobal_p.h> |
| 44 | |
| 45 | #ifndef QT_NO_ICON |
| 46 | // |
| 47 | // W A R N I N G |
| 48 | // ------------- |
| 49 | // |
| 50 | // This file is not part of the Qt API. It exists purely as an |
| 51 | // implementation detail. This header file may change from version to |
| 52 | // version without notice, or even be removed. |
| 53 | // |
| 54 | // We mean it. |
| 55 | // |
| 56 | |
| 57 | #include <QtGui/QIcon> |
| 58 | #include <QtGui/QIconEngine> |
| 59 | #include <QtGui/QPixmapCache> |
| 60 | #include <private/qicon_p.h> |
| 61 | #include <private/qfactoryloader_p.h> |
| 62 | #include <QtCore/QHash> |
| 63 | #include <QtCore/QList> |
| 64 | #include <QtCore/QTypeInfo> |
| 65 | |
| 66 | QT_BEGIN_NAMESPACE |
| 67 | |
| 68 | class QIconLoader; |
| 69 | |
| 70 | struct QIconDirInfo |
| 71 | { |
| 72 | enum Type { Fixed, Scalable, Threshold, Fallback }; |
| 73 | QIconDirInfo(const QString &_path = QString()) : |
| 74 | path(_path), |
| 75 | size(0), |
| 76 | maxSize(0), |
| 77 | minSize(0), |
| 78 | threshold(0), |
| 79 | scale(1), |
| 80 | type(Threshold) {} |
| 81 | QString path; |
| 82 | short size; |
| 83 | short maxSize; |
| 84 | short minSize; |
| 85 | short threshold; |
| 86 | short scale; |
| 87 | Type type; |
| 88 | }; |
| 89 | Q_DECLARE_TYPEINFO(QIconDirInfo, Q_MOVABLE_TYPE); |
| 90 | |
| 91 | class QIconLoaderEngineEntry |
| 92 | { |
| 93 | public: |
| 94 | virtual ~QIconLoaderEngineEntry() {} |
| 95 | virtual QPixmap pixmap(const QSize &size, |
| 96 | QIcon::Mode mode, |
| 97 | QIcon::State state) = 0; |
| 98 | QString filename; |
| 99 | QIconDirInfo dir; |
| 100 | }; |
| 101 | |
| 102 | struct ScalableEntry : public QIconLoaderEngineEntry |
| 103 | { |
| 104 | QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override; |
| 105 | QIcon svgIcon; |
| 106 | }; |
| 107 | |
| 108 | struct PixmapEntry : public QIconLoaderEngineEntry |
| 109 | { |
| 110 | QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override; |
| 111 | QPixmap basePixmap; |
| 112 | }; |
| 113 | |
| 114 | typedef QList<QIconLoaderEngineEntry*> QThemeIconEntries; |
| 115 | |
| 116 | struct QThemeIconInfo |
| 117 | { |
| 118 | QThemeIconEntries entries; |
| 119 | QString iconName; |
| 120 | }; |
| 121 | |
| 122 | class QIconLoaderEngine : public QIconEngine |
| 123 | { |
| 124 | public: |
| 125 | QIconLoaderEngine(const QString& iconName = QString()); |
| 126 | ~QIconLoaderEngine(); |
| 127 | |
| 128 | void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override; |
| 129 | QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override; |
| 130 | QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) override; |
| 131 | QIconEngine *clone() const override; |
| 132 | bool read(QDataStream &in) override; |
| 133 | bool write(QDataStream &out) const override; |
| 134 | |
| 135 | QString iconName() override; |
| 136 | bool isNull() override; |
| 137 | QPixmap scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) override; |
| 138 | QList<QSize> availableSizes(QIcon::Mode mode, QIcon::State state) override; |
| 139 | |
| 140 | Q_GUI_EXPORT static QIconLoaderEngineEntry *entryForSize(const QThemeIconInfo &info, const QSize &size, int scale = 1); |
| 141 | |
| 142 | private: |
| 143 | QString key() const override; |
| 144 | bool hasIcon() const; |
| 145 | void ensureLoaded(); |
| 146 | |
| 147 | QIconLoaderEngine(const QIconLoaderEngine &other); |
| 148 | QThemeIconInfo m_info; |
| 149 | QString m_iconName; |
| 150 | uint m_key; |
| 151 | |
| 152 | friend class QIconLoader; |
| 153 | }; |
| 154 | |
| 155 | class QIconCacheGtkReader; |
| 156 | |
| 157 | class QIconTheme |
| 158 | { |
| 159 | public: |
| 160 | QIconTheme(const QString &name); |
| 161 | QIconTheme() : m_valid(false) {} |
| 162 | QStringList parents() { return m_parents; } |
| 163 | QList<QIconDirInfo> keyList() { return m_keyList; } |
| 164 | QStringList contentDirs() { return m_contentDirs; } |
| 165 | bool isValid() { return m_valid; } |
| 166 | private: |
| 167 | QStringList m_contentDirs; |
| 168 | QList<QIconDirInfo> m_keyList; |
| 169 | QStringList m_parents; |
| 170 | bool m_valid; |
| 171 | public: |
| 172 | QList<QSharedPointer<QIconCacheGtkReader>> m_gtkCaches; |
| 173 | }; |
| 174 | |
| 175 | class Q_GUI_EXPORT QIconLoader |
| 176 | { |
| 177 | public: |
| 178 | QIconLoader(); |
| 179 | QThemeIconInfo loadIcon(const QString &iconName) const; |
| 180 | uint themeKey() const { return m_themeKey; } |
| 181 | |
| 182 | QString themeName() const { return m_userTheme.isEmpty() ? m_systemTheme : m_userTheme; } |
| 183 | void setThemeName(const QString &themeName); |
| 184 | QString fallbackThemeName() const; |
| 185 | void setFallbackThemeName(const QString &themeName); |
| 186 | QIconTheme theme() { return themeList.value(themeName()); } |
| 187 | void setThemeSearchPath(const QStringList &searchPaths); |
| 188 | QStringList themeSearchPaths() const; |
| 189 | void setFallbackSearchPaths(const QStringList &searchPaths); |
| 190 | QStringList fallbackSearchPaths() const; |
| 191 | QIconDirInfo dirInfo(int dirindex); |
| 192 | static QIconLoader *instance(); |
| 193 | void updateSystemTheme(); |
| 194 | void invalidateKey() { m_themeKey++; } |
| 195 | void ensureInitialized(); |
| 196 | bool hasUserTheme() const { return !m_userTheme.isEmpty(); } |
| 197 | |
| 198 | private: |
| 199 | QThemeIconInfo findIconHelper(const QString &themeName, |
| 200 | const QString &iconName, |
| 201 | QStringList &visited) const; |
| 202 | QThemeIconInfo lookupFallbackIcon(const QString &iconName) const; |
| 203 | |
| 204 | uint m_themeKey; |
| 205 | bool m_supportsSvg; |
| 206 | bool m_initialized; |
| 207 | |
| 208 | mutable QString m_userTheme; |
| 209 | mutable QString m_userFallbackTheme; |
| 210 | mutable QString m_systemTheme; |
| 211 | mutable QStringList m_iconDirs; |
| 212 | mutable QHash <QString, QIconTheme> themeList; |
| 213 | mutable QStringList m_fallbackDirs; |
| 214 | }; |
| 215 | |
| 216 | QT_END_NAMESPACE |
| 217 | |
| 218 | #endif // QT_NO_ICON |
| 219 | |
| 220 | #endif // QICONLOADER_P_H |
| 221 | |