File indexing completed on 2024-10-06 06:40:38

0001 /*
0002     This file is part of the KDE project, module kdecore.
0003     SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org>
0004     SPDX-FileCopyrightText: 2000 Antonio Larrosa <larrosa@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #ifndef KICONLOADER_P_H
0010 #define KICONLOADER_P_H
0011 
0012 #include <QCache>
0013 #include <QElapsedTimer>
0014 #include <QPixmap>
0015 #include <QSize>
0016 #include <QString>
0017 #include <QStringList>
0018 
0019 #include "kiconcolors.h"
0020 #include "kiconeffect.h"
0021 #include "kiconloader.h"
0022 
0023 class KIconThemeNode;
0024 
0025 /*** KIconGroup: Icon type description. ***/
0026 
0027 struct KIconGroup {
0028     int size;
0029 };
0030 
0031 /**
0032  * Holds a QPixmap for this process, along with its associated path on disk.
0033  */
0034 struct PixmapWithPath {
0035     QPixmap pixmap;
0036     QString path;
0037 };
0038 
0039 /*** d pointer for KIconLoader. ***/
0040 class KIconLoaderPrivate
0041 {
0042 public:
0043     KIconLoaderPrivate(const QString &_appname, const QStringList &extraSearchPaths, KIconLoader *qq);
0044     ~KIconLoaderPrivate();
0045 
0046     static KIconLoaderPrivate *get(KIconLoader *loader);
0047 
0048     void clear();
0049 
0050     /**
0051      * @internal
0052      */
0053     void init(const QString &_appname, const QStringList &extraSearchPaths = QStringList());
0054 
0055     /**
0056      * @internal
0057      */
0058     void initIconThemes();
0059 
0060     /**
0061      * @internal
0062      * tries to find an icon with the name. It tries some extension and
0063      * match strategies
0064      */
0065     QString findMatchingIcon(const QString &name, int size, qreal scale) const;
0066 
0067     /**
0068      * @internal
0069      * tries to find an icon with the name.
0070      * This is one layer above findMatchingIcon -- it also implements generic fallbacks
0071      * such as generic icons for mimetypes.
0072      */
0073     QString findMatchingIconWithGenericFallbacks(const QString &name, int size, qreal scale) const;
0074 
0075     /**
0076      * @internal
0077      * returns the preferred icon path for an icon with the name.
0078      * Can be used for a quick "hasIcon" check since it caches
0079      * that an icon was not found.
0080      */
0081     QString preferredIconPath(const QString &name);
0082 
0083     /**
0084      * @internal
0085      * Adds themes installed in the application's directory.
0086      **/
0087     void addAppThemes(const QString &appname, const QString &themeBaseDir = QString());
0088 
0089     /**
0090      * @internal
0091      * Adds all themes that are part of this node and the themes
0092      * below (the fallbacks of the theme) into the tree.
0093      */
0094     void addBaseThemes(KIconThemeNode *node, const QString &appname);
0095 
0096     /**
0097      * @internal
0098      * Recursively adds all themes that are specified in the "Inherits"
0099      * property of the given theme into the tree.
0100      */
0101     void addInheritedThemes(KIconThemeNode *node, const QString &appname);
0102 
0103     /**
0104      * @internal
0105      * Creates a KIconThemeNode out of a theme name, and adds this theme
0106      * as well as all its inherited themes into the tree. Themes that already
0107      * exist in the tree will be ignored and not added twice.
0108      */
0109     void addThemeByName(const QString &themename, const QString &appname);
0110 
0111     /**
0112      * Adds all the default themes from other desktops at the end of
0113      * the list of icon themes.
0114      */
0115     void addExtraDesktopThemes();
0116 
0117     /**
0118      * @internal
0119      * return the path for the unknown icon in that size
0120      */
0121     QString unknownIconPath(int size, qreal scale) const;
0122 
0123     /**
0124      * Checks if name ends in one of the supported icon formats (i.e. .png)
0125      * and returns the name without the extension if it does.
0126      */
0127     QString removeIconExtension(const QString &name) const;
0128 
0129     /**
0130      * @internal
0131      * Used with KIconLoader::loadIcon to convert the given name, size, group,
0132      * and icon state information to valid states. All parameters except the
0133      * name can be modified as well to be valid.
0134      */
0135     void normalizeIconMetadata(KIconLoader::Group &group, QSize &size, int &state) const;
0136 
0137     /**
0138      * @internal
0139      * Used with KIconLoader::loadIcon to get a base key name from the given
0140      * icon metadata. Ensure the metadata is normalized first.
0141      */
0142     QString makeCacheKey(const QString &name,
0143                          KIconLoader::Group group,
0144                          const QStringList &overlays,
0145                          const QSize &size,
0146                          qreal scale,
0147                          int state,
0148                          const KIconColors &colors) const;
0149 
0150     /**
0151      * @internal
0152      * If the icon is an SVG file, process it generating a stylesheet
0153      * following the current color scheme. in this case the icon can use named colors
0154      * as text color, background color, highlight color, positive/neutral/negative color
0155      * @see KColorScheme
0156      */
0157     QByteArray processSvg(const QString &path, KIconLoader::States state, const KIconColors &colors) const;
0158 
0159     /**
0160      * @internal
0161      * Creates the QImage for @p path, using SVG rendering as appropriate.
0162      * @p size is only used for scalable images, but if non-zero non-scalable
0163      * images will be resized anyways.
0164      */
0165     QImage createIconImage(const QString &path, const QSize &size, qreal scale, KIconLoader::States state, const KIconColors &colors);
0166 
0167     /**
0168      * @internal
0169      * Adds an QPixmap with its associated path to the shared icon cache.
0170      */
0171     void insertCachedPixmapWithPath(const QString &key, const QPixmap &data, const QString &path);
0172 
0173     /**
0174      * @internal
0175      * Retrieves the path and pixmap of the given key from the shared
0176      * icon cache.
0177      */
0178     bool findCachedPixmapWithPath(const QString &key, QPixmap &data, QString &path);
0179 
0180     /**
0181      * Find the given file in the search paths.
0182      */
0183     QString locate(const QString &fileName);
0184 
0185     /**
0186      * @internal
0187      * React to a global icon theme change
0188      */
0189     void _k_refreshIcons(int group);
0190 
0191     bool shouldCheckForUnknownIcons();
0192 
0193     KIconLoader *const q;
0194 
0195     QStringList mThemesInTree;
0196     KIconGroup *mpGroups = nullptr;
0197     KIconThemeNode *mpThemeRoot = nullptr;
0198     QStringList searchPaths;
0199     KIconEffect mpEffect;
0200     QList<KIconThemeNode *> links;
0201 
0202     // This caches rendered QPixmaps in just this process.
0203     QCache<QString, PixmapWithPath> mPixmapCache;
0204 
0205     bool extraDesktopIconsLoaded : 1;
0206     // lazy loading: initIconThemes() is only needed when the "links" list is needed
0207     // mIconThemeInited is used inside initIconThemes() to init only once
0208     bool mIconThemeInited : 1;
0209     QString m_appname;
0210 
0211     void drawOverlays(const KIconLoader *loader, KIconLoader::Group group, int state, QPixmap &pix, const QStringList &overlays);
0212 
0213     QHash<QString, QString> mIconAvailability; // icon name -> actual icon name (not null if known to be available)
0214     QElapsedTimer mLastUnknownIconCheck; // recheck for unknown icons after kiconloader_ms_between_checks
0215     // the colors used to recolor svg icons stylesheets
0216     KIconColors mColors;
0217     QPalette mPalette;
0218     // to keep track if we are using a custom palette or just falling back to qApp;
0219     bool mCustomColors = false;
0220 };
0221 
0222 #endif // KICONLOADER_P_H