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