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