File indexing completed on 2024-10-06 09:38:49

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