| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QICONLOADER_P_H |
| 5 | #define QICONLOADER_P_H |
| 6 | |
| 7 | #include <QtGui/private/qtguiglobal_p.h> |
| 8 | |
| 9 | #ifndef QT_NO_ICON |
| 10 | // |
| 11 | // W A R N I N G |
| 12 | // ------------- |
| 13 | // |
| 14 | // This file is not part of the Qt API. It exists purely as an |
| 15 | // implementation detail. This header file may change from version to |
| 16 | // version without notice, or even be removed. |
| 17 | // |
| 18 | // We mean it. |
| 19 | // |
| 20 | |
| 21 | #include <QtGui/QIcon> |
| 22 | #include <QtGui/QIconEngine> |
| 23 | #include <QtGui/QPixmapCache> |
| 24 | #include <private/qicon_p.h> |
| 25 | #include <private/qiconengine_p.h> |
| 26 | #include <private/qfactoryloader_p.h> |
| 27 | #include <QtCore/QHash> |
| 28 | #include <QtCore/QList> |
| 29 | #include <QtCore/QTypeInfo> |
| 30 | |
| 31 | #include <vector> |
| 32 | #include <memory> |
| 33 | #include <optional> |
| 34 | |
| 35 | QT_BEGIN_NAMESPACE |
| 36 | |
| 37 | class QIconLoader; |
| 38 | |
| 39 | struct QIconDirInfo |
| 40 | { |
| 41 | enum Type { Fixed, Scalable, Threshold, Fallback }; |
| 42 | enum Context { UnknownContext, Applications, MimeTypes }; |
| 43 | QIconDirInfo(const QString &_path = QString()) : |
| 44 | path(_path), |
| 45 | size(0), |
| 46 | maxSize(0), |
| 47 | minSize(0), |
| 48 | threshold(0), |
| 49 | scale(1), |
| 50 | type(Threshold), |
| 51 | context(UnknownContext) {} |
| 52 | QString path; |
| 53 | short size; |
| 54 | short maxSize; |
| 55 | short minSize; |
| 56 | short threshold; |
| 57 | short scale; |
| 58 | Type type; |
| 59 | Context context; |
| 60 | }; |
| 61 | Q_DECLARE_TYPEINFO(QIconDirInfo, Q_RELOCATABLE_TYPE); |
| 62 | |
| 63 | class QIconLoaderEngineEntry |
| 64 | { |
| 65 | public: |
| 66 | virtual ~QIconLoaderEngineEntry() {} |
| 67 | virtual QPixmap pixmap(const QSize &size, |
| 68 | QIcon::Mode mode, |
| 69 | QIcon::State state, |
| 70 | qreal scale) = 0; |
| 71 | QString filename; |
| 72 | QIconDirInfo dir; |
| 73 | }; |
| 74 | |
| 75 | struct ScalableEntry : public QIconLoaderEngineEntry |
| 76 | { |
| 77 | QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) override; |
| 78 | QIcon svgIcon; |
| 79 | }; |
| 80 | |
| 81 | struct PixmapEntry : public QIconLoaderEngineEntry |
| 82 | { |
| 83 | QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) override; |
| 84 | QPixmap basePixmap; |
| 85 | }; |
| 86 | |
| 87 | using QThemeIconEntries = std::vector<std::unique_ptr<QIconLoaderEngineEntry>>; |
| 88 | |
| 89 | struct QThemeIconInfo |
| 90 | { |
| 91 | QThemeIconEntries entries; |
| 92 | QString iconName; |
| 93 | }; |
| 94 | |
| 95 | class QThemeIconEngine : public QProxyIconEngine |
| 96 | { |
| 97 | public: |
| 98 | QThemeIconEngine(const QString& iconName = QString()); |
| 99 | QIconEngine *clone() const override; |
| 100 | bool read(QDataStream &in) override; |
| 101 | bool write(QDataStream &out) const override; |
| 102 | |
| 103 | protected: |
| 104 | QIconEngine *proxiedEngine() const override; |
| 105 | |
| 106 | private: |
| 107 | QThemeIconEngine(const QThemeIconEngine &other); |
| 108 | QString key() const override; |
| 109 | |
| 110 | QString m_iconName; |
| 111 | mutable uint m_themeKey = 0; |
| 112 | |
| 113 | mutable std::unique_ptr<QIconEngine> m_proxiedEngine; |
| 114 | }; |
| 115 | |
| 116 | class QIconLoaderEngine : public QIconEngine |
| 117 | { |
| 118 | public: |
| 119 | QIconLoaderEngine(const QString& iconName = QString()); |
| 120 | ~QIconLoaderEngine(); |
| 121 | |
| 122 | void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override; |
| 123 | QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override; |
| 124 | QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) override; |
| 125 | QIconEngine *clone() const override; |
| 126 | |
| 127 | QString iconName() override; |
| 128 | bool isNull() override; |
| 129 | QPixmap scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) override; |
| 130 | QList<QSize> availableSizes(QIcon::Mode mode, QIcon::State state) override; |
| 131 | |
| 132 | Q_GUI_EXPORT static QIconLoaderEngineEntry *entryForSize(const QThemeIconInfo &info, const QSize &size, int scale = 1); |
| 133 | |
| 134 | private: |
| 135 | Q_DISABLE_COPY(QIconLoaderEngine) |
| 136 | |
| 137 | QString key() const override; |
| 138 | bool hasIcon() const; |
| 139 | |
| 140 | QString m_iconName; |
| 141 | QThemeIconInfo m_info; |
| 142 | |
| 143 | friend class QIconLoader; |
| 144 | }; |
| 145 | |
| 146 | class QIconCacheGtkReader; |
| 147 | |
| 148 | class QIconTheme |
| 149 | { |
| 150 | public: |
| 151 | QIconTheme(const QString &name); |
| 152 | QIconTheme() : m_valid(false) {} |
| 153 | QStringList parents() const; |
| 154 | QList<QIconDirInfo> keyList() { return m_keyList; } |
| 155 | QStringList contentDirs() { return m_contentDirs; } |
| 156 | bool isValid() { return m_valid; } |
| 157 | private: |
| 158 | QStringList m_contentDirs; |
| 159 | QList<QIconDirInfo> m_keyList; |
| 160 | QStringList m_parents; |
| 161 | bool m_valid; |
| 162 | public: |
| 163 | QList<QSharedPointer<QIconCacheGtkReader>> m_gtkCaches; |
| 164 | }; |
| 165 | |
| 166 | class QIconEnginePlugin; |
| 167 | |
| 168 | class Q_GUI_EXPORT QIconLoader |
| 169 | { |
| 170 | public: |
| 171 | QIconLoader(); |
| 172 | QThemeIconInfo loadIcon(const QString &iconName) const; |
| 173 | uint themeKey() const { return m_themeKey; } |
| 174 | |
| 175 | QString themeName() const; |
| 176 | void setThemeName(const QString &themeName); |
| 177 | QString fallbackThemeName() const; |
| 178 | void setFallbackThemeName(const QString &themeName); |
| 179 | QIconTheme theme() { return themeList.value(key: themeName()); } |
| 180 | void setThemeSearchPath(const QStringList &searchPaths); |
| 181 | QStringList themeSearchPaths() const; |
| 182 | void setFallbackSearchPaths(const QStringList &searchPaths); |
| 183 | QStringList fallbackSearchPaths() const; |
| 184 | QIconDirInfo dirInfo(int dirindex); |
| 185 | static QIconLoader *instance(); |
| 186 | void updateSystemTheme(); |
| 187 | void invalidateKey(); |
| 188 | void ensureInitialized(); |
| 189 | bool hasUserTheme() const { return !m_userTheme.isEmpty(); } |
| 190 | |
| 191 | QIconEngine *iconEngine(const QString &iconName) const; |
| 192 | |
| 193 | private: |
| 194 | enum DashRule { FallBack, NoFallBack }; |
| 195 | QThemeIconInfo findIconHelper(const QString &themeName, |
| 196 | const QString &iconName, |
| 197 | QStringList &visited, |
| 198 | DashRule rule) const; |
| 199 | QThemeIconInfo lookupFallbackIcon(const QString &iconName) const; |
| 200 | |
| 201 | uint m_themeKey; |
| 202 | mutable std::optional<QIconEnginePlugin *> m_factory; |
| 203 | bool m_supportsSvg; |
| 204 | bool m_initialized; |
| 205 | |
| 206 | mutable QString m_userTheme; |
| 207 | mutable QString m_userFallbackTheme; |
| 208 | mutable QString m_systemTheme; |
| 209 | mutable QStringList m_iconDirs; |
| 210 | mutable QHash <QString, QIconTheme> themeList; |
| 211 | mutable QStringList m_fallbackDirs; |
| 212 | mutable QString m_iconName; |
| 213 | }; |
| 214 | |
| 215 | QT_END_NAMESPACE |
| 216 | |
| 217 | #endif // QT_NO_ICON |
| 218 | |
| 219 | #endif // QICONLOADER_P_H |
| 220 | |