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 | class KIconLoaderPrivate |
40 | { |
41 | public: |
42 | KIconLoaderPrivate(const QString &_appname, const QStringList &, KIconLoader *qq); |
43 | ~KIconLoaderPrivate(); |
44 | |
45 | static KIconLoaderPrivate *get(KIconLoader *loader); |
46 | |
47 | void clear(); |
48 | |
49 | void init(const QString &_appname, const QStringList & = QStringList()); |
50 | |
51 | void initIconThemes(); |
52 | |
53 | /* |
54 | * tries to find an icon with the name. It tries some extension and |
55 | * match strategies |
56 | */ |
57 | QString findMatchingIcon(const QString &name, int size, qreal scale) const; |
58 | |
59 | /* |
60 | * tries to find an icon with the name. |
61 | * This is one layer above findMatchingIcon -- it also implements generic fallbacks |
62 | * such as generic icons for mimetypes. |
63 | */ |
64 | QString findMatchingIconWithGenericFallbacks(const QString &name, int size, qreal scale) const; |
65 | |
66 | /* |
67 | * returns the preferred icon path for an icon with the name. |
68 | * Can be used for a quick "hasIcon" check since it caches |
69 | * that an icon was not found. |
70 | */ |
71 | QString preferredIconPath(const QString &name); |
72 | |
73 | /* |
74 | * Adds themes installed in the application's directory. |
75 | **/ |
76 | void addAppThemes(const QString &appname, const QString &themeBaseDir = QString()); |
77 | |
78 | /* |
79 | * Adds all themes that are part of this node and the themes |
80 | * below (the fallbacks of the theme) into the tree. |
81 | */ |
82 | void addBaseThemes(KIconThemeNode *node, const QString &appname); |
83 | |
84 | /* |
85 | * Recursively adds all themes that are specified in the "Inherits" |
86 | * property of the given theme into the tree. |
87 | */ |
88 | void addInheritedThemes(KIconThemeNode *node, const QString &appname); |
89 | |
90 | /* |
91 | * Creates a KIconThemeNode out of a theme name, and adds this theme |
92 | * as well as all its inherited themes into the tree. Themes that already |
93 | * exist in the tree will be ignored and not added twice. |
94 | */ |
95 | void addThemeByName(const QString &themename, const QString &appname); |
96 | |
97 | /* |
98 | * Adds all the default themes from other desktops at the end of |
99 | * the list of icon themes. |
100 | */ |
101 | void (); |
102 | |
103 | /* |
104 | * return the path for the unknown icon in that size |
105 | */ |
106 | QString unknownIconPath(int size, qreal scale) const; |
107 | |
108 | /* |
109 | * Used with KIconLoader::loadIcon to convert the given name, size, group, |
110 | * and icon state information to valid states. All parameters except the |
111 | * name can be modified as well to be valid. |
112 | */ |
113 | void normalizeIconMetadata(KIconLoader::Group &group, QSize &size, int &state) const; |
114 | |
115 | /* |
116 | * Used with KIconLoader::loadIcon to get a base key name from the given |
117 | * icon metadata. Ensure the metadata is normalized first. |
118 | */ |
119 | QString makeCacheKey(const QString &name, |
120 | KIconLoader::Group group, |
121 | const QStringList &overlays, |
122 | const QSize &size, |
123 | qreal scale, |
124 | int state, |
125 | const KIconColors &colors) const; |
126 | |
127 | /* |
128 | * If the icon is an SVG file, process it generating a stylesheet |
129 | * following the current color scheme. in this case the icon can use named colors |
130 | * as text color, background color, highlight color, positive/neutral/negative color |
131 | * \sa KColorScheme |
132 | */ |
133 | QByteArray processSvg(const QString &path, KIconLoader::States state, const KIconColors &colors) const; |
134 | |
135 | /* |
136 | * Creates the QImage for \apath, using SVG rendering as appropriate. |
137 | * \a size is only used for scalable images, but if non-zero non-scalable |
138 | * images will be resized anyways. |
139 | */ |
140 | QImage createIconImage(const QString &path, const QSize &size, qreal scale, KIconLoader::States state, const KIconColors &colors); |
141 | |
142 | /* |
143 | * Adds an QPixmap with its associated path to the shared icon cache. |
144 | */ |
145 | void insertCachedPixmapWithPath(const QString &key, const QPixmap &data, const QString &path); |
146 | |
147 | /* |
148 | * Retrieves the path and pixmap of the given key from the shared |
149 | * icon cache. |
150 | */ |
151 | bool findCachedPixmapWithPath(const QString &key, QPixmap &data, QString &path); |
152 | |
153 | /* |
154 | * Find the given file in the search paths. |
155 | */ |
156 | QString locate(const QString &fileName); |
157 | |
158 | /* |
159 | * React to a global icon theme change |
160 | */ |
161 | void _k_refreshIcons(int group); |
162 | |
163 | bool shouldCheckForUnknownIcons(); |
164 | |
165 | KIconLoader *const q; |
166 | |
167 | QStringList mThemesInTree; |
168 | std::vector<KIconGroup> mpGroups; |
169 | KIconThemeNode *mpThemeRoot = nullptr; |
170 | QStringList searchPaths; |
171 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
172 | KIconEffect mpEffect; |
173 | #endif |
174 | QList<KIconThemeNode *> links; |
175 | |
176 | // This caches rendered QPixmaps in just this process. |
177 | QCache<QString, PixmapWithPath> mPixmapCache; |
178 | |
179 | bool : 1; |
180 | // lazy loading: initIconThemes() is only needed when the "links" list is needed |
181 | // mIconThemeInited is used inside initIconThemes() to init only once |
182 | bool mIconThemeInited : 1; |
183 | QString m_appname; |
184 | |
185 | void drawOverlays(const KIconLoader *loader, KIconLoader::Group group, int state, QPixmap &pix, const QStringList &overlays); |
186 | |
187 | QHash<QString, QString> mIconAvailability; // icon name -> actual icon name (not null if known to be available) |
188 | QElapsedTimer mLastUnknownIconCheck; // recheck for unknown icons after kiconloader_ms_between_checks |
189 | // the colors used to recolor svg icons stylesheets |
190 | KIconColors mColors; |
191 | QPalette mPalette; |
192 | // to keep track if we are using a custom palette or just falling back to qApp; |
193 | bool mCustomColors = false; |
194 | }; |
195 | |
196 | #endif // KICONLOADER_P_H |
197 | |