File indexing completed on 2024-04-28 17:06:20

0001 /*
0002     SPDX-FileCopyrightText: 2000-2002 Shie Erlich <erlich@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000-2002 Rafi Yanai <yanai@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef KRCOLORCACHE_H
0010 #define KRCOLORCACHE_H
0011 
0012 // QtCore
0013 #include <QList>
0014 #include <QObject>
0015 // QtGui
0016 #include <QColor>
0017 #include <QPalette>
0018 
0019 /**
0020  * Design goals: Color calculation is done on one place only.
0021  * Configuration through krConfig OR through local settings.
0022  * Calculation must be fast through caching.
0023  *
0024  * This implementation exposes 3 classes:
0025  * KrColorSettings: holds the color settings from krConfig,
0026  *                  which can be changed locally
0027  * KrColorItemType: specifies the colors to be calculated
0028  * KrColorCache: performs the color calculation and caches the result.
0029  *               Uses KrColorSettings for the calculation
0030  *
0031  * Copies all used color settings from krConfig into a local cache on creation.
0032  * It contains 3 types of properties:
0033  * color, numeric (int) and boolean.
0034  * Color properties can have string or color values.
0035  * Property values can be changed.
0036  * These changes does not go into krConfig!
0037  *
0038  * is*Valid checks, if a property name is valid
0039  * get*Names returns a list of all allowed property names
0040  * set*Value overwrites a property with a new value
0041  * get*Value returns the current value
0042  *
0043  * For colors the value can be returned as text or as color.
0044  * If a text representation is not a valid color, setColorValue(QColor())
0045  * should be called.
0046  */
0047 class KrColorSettings
0048 {
0049     class KrColorSettingsImpl *m_impl;
0050 
0051 public:
0052     KrColorSettings();
0053     KrColorSettings(const KrColorSettings &);
0054     ~KrColorSettings();
0055     const KrColorSettings &operator=(const KrColorSettings &);
0056 
0057     static bool isColorNameValid(const QString &settingName);
0058     static QList<QString> getColorNames();
0059     bool setColorValue(const QString &settingName, const QColor &color);
0060     QColor getColorValue(const QString &settingName) const;
0061     bool setColorTextValue(const QString &settingName, const QString &colorText);
0062     QString getColorTextValue(const QString &settingName) const;
0063 
0064     static bool isNumNameValid(const QString &settingName);
0065     static QList<QString> getNumNames();
0066     bool setNumValue(const QString &settingName, int value);
0067     int getNumValue(const QString &settingName, int defaultValue = 0) const;
0068 
0069     static bool isBoolNameValid(const QString &settingName);
0070     static QList<QString> getBoolNames();
0071     bool setBoolValue(const QString &settingName, bool value);
0072     int getBoolValue(const QString &settingName, bool defaultValue = false) const;
0073 };
0074 
0075 /**
0076  * A collection of properties which describe the color group to be calculated
0077  */
0078 class KrColorItemType
0079 {
0080 public:
0081     enum FileType { File, InvalidSymlink, Symlink, Directory, Executable };
0082     FileType m_fileType;
0083     bool m_alternateBackgroundColor, m_activePanel, m_currentItem, m_selectedItem;
0084     KrColorItemType();
0085     KrColorItemType(FileType type, bool alternateBackgroundColor, bool activePanel, bool currentItem, bool selectedItem);
0086     KrColorItemType(const KrColorItemType &);
0087     const KrColorItemType &operator=(const KrColorItemType &);
0088 };
0089 
0090 /**
0091  * The color calculation. It bases on an internal KrColorSettings instance.
0092  * Via setColors it can be changed.
0093  * getColors does the color calculation.
0094  * It sets the colors Base, Background, Text, HighlightedText and Highlight.
0095  * All calculated values are cached.
0096  * The cache is deleted on refreshColors and setColors, which also trigger
0097  * colorsRefreshed.
0098  * getColorCache returns a static color cached for painting the panels.
0099  * On the color cache setColors should NEVER be called!
0100  */
0101 class KrColorGroup
0102 {
0103 public:
0104     inline KrColorGroup()
0105         : _textColor()
0106         , _backgroundColor()
0107         , _highlightedTextColor()
0108         , _highlightedBackgroundColor()
0109     {
0110     }
0111 
0112     inline const QColor &text() const
0113     {
0114         return _textColor;
0115     }
0116     inline const QColor &background() const
0117     {
0118         return _backgroundColor;
0119     }
0120     inline const QColor &highlight() const
0121     {
0122         return _highlightedBackgroundColor;
0123     }
0124     inline const QColor &highlightedText() const
0125     {
0126         return _highlightedTextColor;
0127     }
0128 
0129     inline void setText(const QColor &c)
0130     {
0131         _textColor = c;
0132     }
0133     inline void setBackground(const QColor &c)
0134     {
0135         _backgroundColor = c;
0136     }
0137     inline void setHighlight(const QColor &c)
0138     {
0139         _highlightedBackgroundColor = c;
0140     }
0141     inline void setHighlightedText(const QColor &c)
0142     {
0143         _highlightedTextColor = c;
0144     }
0145 
0146 protected:
0147     QColor _textColor;
0148     QColor _backgroundColor;
0149     QColor _highlightedTextColor;
0150     QColor _highlightedBackgroundColor;
0151 };
0152 
0153 class KrColorCache : public QObject
0154 {
0155     Q_OBJECT
0156     static KrColorCache *m_instance;
0157     class KrColorCacheImpl *m_impl;
0158     KrColorCache(const KrColorCache &);
0159     const KrColorCache &operator=(const KrColorCache &);
0160 
0161 public:
0162     KrColorCache();
0163     ~KrColorCache() override;
0164     static KrColorCache &getColorCache();
0165     void getColors(KrColorGroup &result, const KrColorItemType &type) const;
0166     bool getDimSettings(QColor &dimColor, int &dimFactor);
0167     static QColor dimColor(const QColor &color, int dim, const QColor &targetColor);
0168 public slots:
0169     void refreshColors();
0170     void setColors(const KrColorSettings &);
0171 signals:
0172     void colorsRefreshed();
0173 };
0174 
0175 #endif