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

source code of qtbase/src/gui/image/qiconloader_p.h