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