1 | /* |
2 | This file is part of the KDE project, module kdecore. |
3 | SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org> |
4 | SPDX-FileCopyrightText: 2000 Antonio Larrosa <larrosa@kde.org> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-only |
7 | */ |
8 | |
9 | #ifndef KICONLOADER_P_H |
10 | #define KICONLOADER_P_H |
11 | |
12 | #include <QCache> |
13 | #include <QElapsedTimer> |
14 | #include <QPixmap> |
15 | #include <QSize> |
16 | #include <QString> |
17 | #include <QStringList> |
18 | |
19 | #include "kiconcolors.h" |
20 | #include "kiconeffect.h" |
21 | #include "kiconloader.h" |
22 | |
23 | class KIconThemeNode; |
24 | |
25 | /*** KIconGroup: Icon type description. ***/ |
26 | |
27 | struct KIconGroup { |
28 | int size; |
29 | }; |
30 | |
31 | /** |
32 | * Holds a QPixmap for this process, along with its associated path on disk. |
33 | */ |
34 | struct PixmapWithPath { |
35 | QPixmap pixmap; |
36 | QString path; |
37 | }; |
38 | |
39 | /*** d pointer for KIconLoader. ***/ |
40 | class KIconLoaderPrivate |
41 | { |
42 | public: |
43 | KIconLoaderPrivate(const QString &_appname, const QStringList &, KIconLoader *qq); |
44 | ~KIconLoaderPrivate(); |
45 | |
46 | static KIconLoaderPrivate *get(KIconLoader *loader); |
47 | |
48 | void clear(); |
49 | |
50 | /** |
51 | * @internal |
52 | */ |
53 | void init(const QString &_appname, const QStringList & = QStringList()); |
54 | |
55 | /** |
56 | * @internal |
57 | */ |
58 | void initIconThemes(); |
59 | |
60 | /** |
61 | * @internal |
62 | * tries to find an icon with the name. It tries some extension and |
63 | * match strategies |
64 | */ |
65 | QString findMatchingIcon(const QString &name, int size, qreal scale) const; |
66 | |
67 | /** |
68 | * @internal |
69 | * tries to find an icon with the name. |
70 | * This is one layer above findMatchingIcon -- it also implements generic fallbacks |
71 | * such as generic icons for mimetypes. |
72 | */ |
73 | QString findMatchingIconWithGenericFallbacks(const QString &name, int size, qreal scale) const; |
74 | |
75 | /** |
76 | * @internal |
77 | * returns the preferred icon path for an icon with the name. |
78 | * Can be used for a quick "hasIcon" check since it caches |
79 | * that an icon was not found. |
80 | */ |
81 | QString preferredIconPath(const QString &name); |
82 | |
83 | /** |
84 | * @internal |
85 | * Adds themes installed in the application's directory. |
86 | **/ |
87 | void addAppThemes(const QString &appname, const QString &themeBaseDir = QString()); |
88 | |
89 | /** |
90 | * @internal |
91 | * Adds all themes that are part of this node and the themes |
92 | * below (the fallbacks of the theme) into the tree. |
93 | */ |
94 | void addBaseThemes(KIconThemeNode *node, const QString &appname); |
95 | |
96 | /** |
97 | * @internal |
98 | * Recursively adds all themes that are specified in the "Inherits" |
99 | * property of the given theme into the tree. |
100 | */ |
101 | void addInheritedThemes(KIconThemeNode *node, const QString &appname); |
102 | |
103 | /** |
104 | * @internal |
105 | * Creates a KIconThemeNode out of a theme name, and adds this theme |
106 | * as well as all its inherited themes into the tree. Themes that already |
107 | * exist in the tree will be ignored and not added twice. |
108 | */ |
109 | void addThemeByName(const QString &themename, const QString &appname); |
110 | |
111 | /** |
112 | * Adds all the default themes from other desktops at the end of |
113 | * the list of icon themes. |
114 | */ |
115 | void (); |
116 | |
117 | /** |
118 | * @internal |
119 | * return the path for the unknown icon in that size |
120 | */ |
121 | QString unknownIconPath(int size, qreal scale) const; |
122 | |
123 | /** |
124 | * Checks if name ends in one of the supported icon formats (i.e. .png) |
125 | * and returns the name without the extension if it does. |
126 | */ |
127 | QString removeIconExtension(const QString &name) const; |
128 | |
129 | /** |
130 | * @internal |
131 | * Used with KIconLoader::loadIcon to convert the given name, size, group, |
132 | * and icon state information to valid states. All parameters except the |
133 | * name can be modified as well to be valid. |
134 | */ |
135 | void normalizeIconMetadata(KIconLoader::Group &group, QSize &size, int &state) const; |
136 | |
137 | /** |
138 | * @internal |
139 | * Used with KIconLoader::loadIcon to get a base key name from the given |
140 | * icon metadata. Ensure the metadata is normalized first. |
141 | */ |
142 | QString makeCacheKey(const QString &name, |
143 | KIconLoader::Group group, |
144 | const QStringList &overlays, |
145 | const QSize &size, |
146 | qreal scale, |
147 | int state, |
148 | const KIconColors &colors) const; |
149 | |
150 | /** |
151 | * @internal |
152 | * If the icon is an SVG file, process it generating a stylesheet |
153 | * following the current color scheme. in this case the icon can use named colors |
154 | * as text color, background color, highlight color, positive/neutral/negative color |
155 | * @see KColorScheme |
156 | */ |
157 | QByteArray processSvg(const QString &path, KIconLoader::States state, const KIconColors &colors) const; |
158 | |
159 | /** |
160 | * @internal |
161 | * Creates the QImage for @p path, using SVG rendering as appropriate. |
162 | * @p size is only used for scalable images, but if non-zero non-scalable |
163 | * images will be resized anyways. |
164 | */ |
165 | QImage createIconImage(const QString &path, const QSize &size, qreal scale, KIconLoader::States state, const KIconColors &colors); |
166 | |
167 | /** |
168 | * @internal |
169 | * Adds an QPixmap with its associated path to the shared icon cache. |
170 | */ |
171 | void insertCachedPixmapWithPath(const QString &key, const QPixmap &data, const QString &path); |
172 | |
173 | /** |
174 | * @internal |
175 | * Retrieves the path and pixmap of the given key from the shared |
176 | * icon cache. |
177 | */ |
178 | bool findCachedPixmapWithPath(const QString &key, QPixmap &data, QString &path); |
179 | |
180 | /** |
181 | * Find the given file in the search paths. |
182 | */ |
183 | QString locate(const QString &fileName); |
184 | |
185 | /** |
186 | * @internal |
187 | * React to a global icon theme change |
188 | */ |
189 | void _k_refreshIcons(int group); |
190 | |
191 | bool shouldCheckForUnknownIcons(); |
192 | |
193 | KIconLoader *const q; |
194 | |
195 | QStringList mThemesInTree; |
196 | KIconGroup *mpGroups = nullptr; |
197 | KIconThemeNode *mpThemeRoot = nullptr; |
198 | QStringList searchPaths; |
199 | KIconEffect mpEffect; |
200 | QList<KIconThemeNode *> links; |
201 | |
202 | // This caches rendered QPixmaps in just this process. |
203 | QCache<QString, PixmapWithPath> mPixmapCache; |
204 | |
205 | bool : 1; |
206 | // lazy loading: initIconThemes() is only needed when the "links" list is needed |
207 | // mIconThemeInited is used inside initIconThemes() to init only once |
208 | bool mIconThemeInited : 1; |
209 | QString m_appname; |
210 | |
211 | void drawOverlays(const KIconLoader *loader, KIconLoader::Group group, int state, QPixmap &pix, const QStringList &overlays); |
212 | |
213 | QHash<QString, QString> mIconAvailability; // icon name -> actual icon name (not null if known to be available) |
214 | QElapsedTimer mLastUnknownIconCheck; // recheck for unknown icons after kiconloader_ms_between_checks |
215 | // the colors used to recolor svg icons stylesheets |
216 | KIconColors mColors; |
217 | QPalette mPalette; |
218 | // to keep track if we are using a custom palette or just falling back to qApp; |
219 | bool mCustomColors = false; |
220 | }; |
221 | |
222 | #endif // KICONLOADER_P_H |
223 | |