1 /*****************************************************************************
2  * Copyright (C) 2000-2002 Shie Erlich <erlich@users.sourceforge.net>        *
3  * Copyright (C) 2000-2002 Rafi Yanai <yanai@users.sourceforge.net>          *
4  * Copyright (C) 2004-2019 Krusader Krew [https://krusader.org]              *
5  *                                                                           *
6  * This file is part of Krusader [https://krusader.org].                     *
7  *                                                                           *
8  * Krusader is free software: you can redistribute it and/or modify          *
9  * it under the terms of the GNU General Public License as published by      *
10  * the Free Software Foundation, either version 2 of the License, or         *
11  * (at your option) any later version.                                       *
12  *                                                                           *
13  * Krusader is distributed in the hope that it will be useful,               *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
16  * GNU General Public License for more details.                              *
17  *                                                                           *
18  * You should have received a copy of the GNU General Public License         *
19  * along with Krusader.  If not, see [http://www.gnu.org/licenses/].         *
20  *****************************************************************************/
21 
22 #ifndef KRCOLORCACHE_H
23 #define KRCOLORCACHE_H
24 
25 // QtCore
26 #include <QObject>
27 #include <QList>
28 // QtGui
29 #include <QColor>
30 #include <QPalette>
31 
32 /**
33  * Design goals: Color calculation is done on one place only.
34  * Configuration through krConfig OR through local settings.
35  * Calculation must be fast through caching.
36  *
37  * This implementation exposes 3 classes:
38  * KrColorSettings: holds the color settings from krConfig,
39  *                  which can be changed locally
40  * KrColorItemType: specifies the colors to be calculated
41  * KrColorCache: perfomes the color calculation and caches the result.
42  *               Uses KrColorSettings for the calculation
43  *
44  * Copies all used color settings from krConfig into a local cache on creation.
45  * It contains 3 types of properties:
46  * color, numeric (int) and boolean.
47  * Color properties can have string or color values.
48  * Property values can be changed.
49  * These changes does not go into krConfig!
50  *
51  * is*Valid checks, if a protery name is valid
52  * get*Names returns a list of all allowed property names
53  * set*Value overwrites a property with a new value
54  * get*Value retunrs the current value
55  *
56  * For colors the value can be returned as text or as color.
57  * If a text representation is not a valid color, setColorValue(QColor())
58  * should be called.
59  */
60 class KrColorSettings
61 {
62     class KrColorSettingsImpl * m_impl;
63 public:
64     KrColorSettings();
65     KrColorSettings(const KrColorSettings &);
66     ~KrColorSettings();
67     const KrColorSettings & operator= (const KrColorSettings &);
68 
69     static bool isColorNameValid(const QString & settingName);
70     static QList<QString> getColorNames();
71     bool setColorValue(const QString & settingName, const QColor & color);
72     QColor getColorValue(const QString & settingName) const;
73     bool setColorTextValue(const QString & settingName, const QString & colorText);
74     QString getColorTextValue(const QString & settingName) const;
75 
76     static bool isNumNameValid(const QString & settingName);
77     static QList<QString> getNumNames();
78     bool setNumValue(const QString & settingName, int value);
79     int getNumValue(const QString & settingName, int defaultValue = 0) const;
80 
81     static bool isBoolNameValid(const QString & settingName);
82     static QList<QString> getBoolNames();
83     bool setBoolValue(const QString & settingName, bool value);
84     int getBoolValue(const QString & settingName, bool defaultValue = false) const;
85 };
86 
87 /**
88  * A colletcion of properties which describe the color group to be calculated
89  */
90 class KrColorItemType
91 {
92 public:
93     enum FileType {File, InvalidSymlink, Symlink, Directory, Executable};
94     FileType m_fileType;
95     bool m_alternateBackgroundColor, m_activePanel, m_currentItem, m_selectedItem;
96     KrColorItemType();
97     KrColorItemType(FileType type, bool alternateBackgroundColor, bool activePanel, bool currentItem, bool selectedItem);
98     KrColorItemType(const KrColorItemType &);
99     const KrColorItemType & operator= (const KrColorItemType &);
100 };
101 
102 /**
103  * The color calculation. It bases on an internal KrColorSettings instance.
104  * Via setColors it can be changed.
105  * getColors does the color calculation.
106  * It sets the colors Base, Background, Text, HighlightedText and Highlight.
107  * All calculated values are cached.
108  * The cache is deleted on refreshColors and setColors, which also trigger
109  * colorsRefreshed.
110  * getColorCache returns a static color cached for painting the panels.
111  * On the color cache setColors should NEVER be called!
112  */
113 class KrColorGroup
114 {
115 public:
KrColorGroup()116     inline KrColorGroup() : _textColor(), _backgroundColor(), _highlightedTextColor(),
117             _highlightedBackgroundColor() {}
118 
text()119     inline const QColor & text() const            {
120         return _textColor;
121     }
background()122     inline const QColor & background() const      {
123         return _backgroundColor;
124     }
highlight()125     inline const QColor & highlight() const       {
126         return _highlightedBackgroundColor;
127     }
highlightedText()128     inline const QColor & highlightedText() const {
129         return _highlightedTextColor;
130     }
131 
setText(const QColor & c)132     inline void setText(const QColor & c)               {
133         _textColor = c;
134     }
setBackground(const QColor & c)135     inline void setBackground(const QColor & c)          {
136         _backgroundColor = c;
137     }
setHighlight(const QColor & c)138     inline void setHighlight(const QColor & c)          {
139         _highlightedBackgroundColor = c;
140     }
setHighlightedText(const QColor & c)141     inline void setHighlightedText(const QColor & c)    {
142         _highlightedTextColor = c;
143     }
144 
145 protected:
146     QColor _textColor;
147     QColor _backgroundColor;
148     QColor _highlightedTextColor;
149     QColor _highlightedBackgroundColor;
150 };
151 
152 class KrColorCache : public QObject
153 {
154     Q_OBJECT
155     static KrColorCache * m_instance;
156     class KrColorCacheImpl * m_impl;
157     KrColorCache(const KrColorCache &);
158     const KrColorCache & operator= (const KrColorCache &);
159 public:
160     KrColorCache();
161     ~KrColorCache();
162     static KrColorCache & getColorCache();
163     void getColors(KrColorGroup & result, const KrColorItemType & type) const;
164     bool getDimSettings(QColor & dimColor, int & dimFactor);
165     static QColor dimColor(const QColor & color, int dim, const QColor & targetColor);
166 public slots:
167     void refreshColors();
168     void setColors(const KrColorSettings &);
169 signals:
170     void colorsRefreshed();
171 };
172 
173 #endif
174