1 | /* |
---|
2 | This source file is part of Konsole, a terminal emulator. |
---|
3 | |
---|
4 | Copyright 2007-2008 by Robert Knight <robertknight@gmail.com> |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | This program is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with this program; if not, write to the Free Software |
---|
18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
19 | 02110-1301 USA. |
---|
20 | */ |
---|
21 | |
---|
22 | #ifndef COLORSCHEME_H |
---|
23 | #define COLORSCHEME_H |
---|
24 | |
---|
25 | // Qt |
---|
26 | #include <QHash> |
---|
27 | #include <QList> |
---|
28 | #include <QMetaType> |
---|
29 | #include <QIODevice> |
---|
30 | #include <QSet> |
---|
31 | #include <QSettings> |
---|
32 | |
---|
33 | // Konsole |
---|
34 | #include "CharacterColor.h" |
---|
35 | |
---|
36 | class QIODevice; |
---|
37 | //class KConfig; |
---|
38 | |
---|
39 | namespace Konsole |
---|
40 | { |
---|
41 | |
---|
42 | /** |
---|
43 | * Represents a color scheme for a terminal display. |
---|
44 | * |
---|
45 | * The color scheme includes the palette of colors used to draw the text and character backgrounds |
---|
46 | * in the display and the opacity level of the display background. |
---|
47 | */ |
---|
48 | class ColorScheme |
---|
49 | { |
---|
50 | public: |
---|
51 | /** |
---|
52 | * Constructs a new color scheme which is initialised to the default color set |
---|
53 | * for Konsole. |
---|
54 | */ |
---|
55 | ColorScheme(); |
---|
56 | ColorScheme(const ColorScheme& other); |
---|
57 | ~ColorScheme(); |
---|
58 | |
---|
59 | /** Sets the descriptive name of the color scheme. */ |
---|
60 | void setDescription(const QString& description); |
---|
61 | /** Returns the descriptive name of the color scheme. */ |
---|
62 | QString description() const; |
---|
63 | |
---|
64 | /** Sets the name of the color scheme */ |
---|
65 | void setName(const QString& name); |
---|
66 | /** Returns the name of the color scheme */ |
---|
67 | QString name() const; |
---|
68 | |
---|
69 | #if 0 |
---|
70 | // Implemented upstream - in user apps |
---|
71 | /** Reads the color scheme from the specified configuration source */ |
---|
72 | void read(KConfig& config); |
---|
73 | /** Writes the color scheme to the specified configuration source */ |
---|
74 | void write(KConfig& config) const; |
---|
75 | #endif |
---|
76 | void read(const QString & filename); |
---|
77 | |
---|
78 | /** Sets a single entry within the color palette. */ |
---|
79 | void setColorTableEntry(int index , const ColorEntry& entry); |
---|
80 | |
---|
81 | /** |
---|
82 | * Copies the color entries which form the palette for this color scheme |
---|
83 | * into @p table. @p table should be an array with TABLE_COLORS entries. |
---|
84 | * |
---|
85 | * @param table Array into which the color entries for this color scheme |
---|
86 | * are copied. |
---|
87 | * @param randomSeed Color schemes may allow certain colors in their |
---|
88 | * palette to be randomized. The seed is used to pick the random color. |
---|
89 | */ |
---|
90 | void getColorTable(ColorEntry* table) const; |
---|
91 | |
---|
92 | /** |
---|
93 | * Retrieves a single color entry from the table. |
---|
94 | * |
---|
95 | * See getColorTable() |
---|
96 | */ |
---|
97 | ColorEntry colorEntry(int index) const; |
---|
98 | |
---|
99 | /** |
---|
100 | * Convenience method. Returns the |
---|
101 | * foreground color for this scheme, |
---|
102 | * this is the primary color used to draw the |
---|
103 | * text in this scheme. |
---|
104 | */ |
---|
105 | QColor foregroundColor() const; |
---|
106 | /** |
---|
107 | * Convenience method. Returns the background color for |
---|
108 | * this scheme, this is the primary color used to |
---|
109 | * draw the terminal background in this scheme. |
---|
110 | */ |
---|
111 | QColor backgroundColor() const; |
---|
112 | |
---|
113 | /** |
---|
114 | * Returns true if this color scheme has a dark background. |
---|
115 | * The background color is said to be dark if it has a value of less than 127 |
---|
116 | * in the HSV color space. |
---|
117 | */ |
---|
118 | bool hasDarkBackground() const; |
---|
119 | |
---|
120 | /** |
---|
121 | * Sets the opacity level of the display background. @p opacity ranges |
---|
122 | * between 0 (completely transparent background) and 1 (completely |
---|
123 | * opaque background). |
---|
124 | * |
---|
125 | * Defaults to 1. |
---|
126 | * |
---|
127 | * TODO: More documentation |
---|
128 | */ |
---|
129 | void setOpacity(qreal opacity); |
---|
130 | /** |
---|
131 | * Returns the opacity level for this color scheme, see setOpacity() |
---|
132 | * TODO: More documentation |
---|
133 | */ |
---|
134 | qreal opacity() const; |
---|
135 | |
---|
136 | /** |
---|
137 | * Enables randomization of the background color. This will cause |
---|
138 | * the palette returned by getColorTable() and colorEntry() to |
---|
139 | * be adjusted depending on the value of the random seed argument |
---|
140 | * to them. |
---|
141 | */ |
---|
142 | void setRandomizedBackgroundColor(bool randomize); |
---|
143 | |
---|
144 | /** Returns true if the background color is randomized. */ |
---|
145 | bool randomizedBackgroundColor() const; |
---|
146 | |
---|
147 | static QString colorNameForIndex(int index); |
---|
148 | static QString translatedColorNameForIndex(int index); |
---|
149 | |
---|
150 | private: |
---|
151 | // specifies how much a particular color can be randomized by |
---|
152 | class RandomizationRange |
---|
153 | { |
---|
154 | public: |
---|
155 | RandomizationRange() : hue(0) , saturation(0) , value(0) {} |
---|
156 | |
---|
157 | bool isNull() const |
---|
158 | { |
---|
159 | return ( hue == 0 && saturation == 0 && value == 0 ); |
---|
160 | } |
---|
161 | |
---|
162 | quint16 hue; |
---|
163 | quint8 saturation; |
---|
164 | quint8 value; |
---|
165 | }; |
---|
166 | |
---|
167 | // returns the active color table. if none has been set specifically, |
---|
168 | // this is the default color table. |
---|
169 | const ColorEntry* colorTable() const; |
---|
170 | |
---|
171 | #if 0 |
---|
172 | // implemented upstream - user apps |
---|
173 | // reads a single colour entry from a KConfig source |
---|
174 | // and sets the palette entry at 'index' to the entry read. |
---|
175 | void readColorEntry(KConfig& config , int index); |
---|
176 | // writes a single colour entry to a KConfig source |
---|
177 | void writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry,const RandomizationRange& range) const; |
---|
178 | #endif |
---|
179 | void readColorEntry(QSettings *s, int index); |
---|
180 | |
---|
181 | // sets the amount of randomization allowed for a particular color |
---|
182 | // in the palette. creates the randomization table if |
---|
183 | // it does not already exist |
---|
184 | void setRandomizationRange( int index , quint16 hue , quint8 saturation , quint8 value ); |
---|
185 | |
---|
186 | QString _description; |
---|
187 | QString _name; |
---|
188 | qreal _opacity; |
---|
189 | ColorEntry* _table; // pointer to custom color table or 0 if the default |
---|
190 | // color scheme is being used |
---|
191 | |
---|
192 | |
---|
193 | static const quint16 MAX_HUE = 340; |
---|
194 | |
---|
195 | RandomizationRange* _randomTable; // pointer to randomization table or 0 |
---|
196 | // if no colors in the color scheme support |
---|
197 | // randomization |
---|
198 | |
---|
199 | static const char* const colorNames[TABLE_COLORS]; |
---|
200 | static const char* const translatedColorNames[TABLE_COLORS]; |
---|
201 | |
---|
202 | static const ColorEntry defaultTable[]; // table of default color entries |
---|
203 | }; |
---|
204 | |
---|
205 | /** |
---|
206 | * A color scheme which uses colors from the standard KDE color palette. |
---|
207 | * |
---|
208 | * This is designed primarily for the benefit of users who are using specially |
---|
209 | * designed colors. |
---|
210 | * |
---|
211 | * TODO Implement and make it the default on systems with specialized KDE |
---|
212 | * color schemes. |
---|
213 | */ |
---|
214 | class AccessibleColorScheme : public ColorScheme |
---|
215 | { |
---|
216 | public: |
---|
217 | AccessibleColorScheme(); |
---|
218 | }; |
---|
219 | |
---|
220 | /** |
---|
221 | * Manages the color schemes available for use by terminal displays. |
---|
222 | * See ColorScheme |
---|
223 | */ |
---|
224 | class ColorSchemeManager |
---|
225 | { |
---|
226 | public: |
---|
227 | |
---|
228 | /** |
---|
229 | * Constructs a new ColorSchemeManager and loads the list |
---|
230 | * of available color schemes. |
---|
231 | * |
---|
232 | * The color schemes themselves are not loaded until they are first |
---|
233 | * requested via a call to findColorScheme() |
---|
234 | */ |
---|
235 | ColorSchemeManager(); |
---|
236 | /** |
---|
237 | * Destroys the ColorSchemeManager and saves any modified color schemes to disk. |
---|
238 | */ |
---|
239 | ~ColorSchemeManager(); |
---|
240 | |
---|
241 | /** |
---|
242 | * Returns the default color scheme for Konsole |
---|
243 | */ |
---|
244 | const ColorScheme* defaultColorScheme() const; |
---|
245 | |
---|
246 | /** |
---|
247 | * Returns the color scheme with the given name or 0 if no |
---|
248 | * scheme with that name exists. If @p name is empty, the |
---|
249 | * default color scheme is returned. |
---|
250 | * |
---|
251 | * The first time that a color scheme with a particular name is |
---|
252 | * requested, the configuration information is loaded from disk. |
---|
253 | */ |
---|
254 | const ColorScheme* findColorScheme(const QString& name); |
---|
255 | |
---|
256 | #if 0 |
---|
257 | /** |
---|
258 | * Adds a new color scheme to the manager. If @p scheme has the same name as |
---|
259 | * an existing color scheme, it replaces the existing scheme. |
---|
260 | * |
---|
261 | * TODO - Ensure the old color scheme gets deleted |
---|
262 | */ |
---|
263 | void addColorScheme(ColorScheme* scheme); |
---|
264 | #endif |
---|
265 | /** |
---|
266 | * Deletes a color scheme. Returns true on successful deletion or false otherwise. |
---|
267 | */ |
---|
268 | bool deleteColorScheme(const QString& name); |
---|
269 | |
---|
270 | /** |
---|
271 | * Returns a list of the all the available color schemes. |
---|
272 | * This may be slow when first called because all of the color |
---|
273 | * scheme resources on disk must be located, read and parsed. |
---|
274 | * |
---|
275 | * Subsequent calls will be inexpensive. |
---|
276 | */ |
---|
277 | QList<const ColorScheme*> allColorSchemes(); |
---|
278 | |
---|
279 | /** Returns the global color scheme manager instance. */ |
---|
280 | static ColorSchemeManager* instance(); |
---|
281 | |
---|
282 | /** @brief Loads a custom color scheme under given \em path. |
---|
283 | * |
---|
284 | * The \em path may refer to either KDE 4 .colorscheme or KDE 3 |
---|
285 | * .schema file |
---|
286 | * |
---|
287 | * The loaded color scheme is available under the name equal to |
---|
288 | * the base name of the \em path via the allColorSchemes() and |
---|
289 | * findColorScheme() methods after this call if loaded successfully. |
---|
290 | * |
---|
291 | * @param[in] path The path to KDE 4 .colorscheme or KDE 3 .schema. |
---|
292 | * @return Whether the color scheme is loaded successfully. |
---|
293 | */ |
---|
294 | bool loadCustomColorScheme(const QString& path); |
---|
295 | |
---|
296 | /** |
---|
297 | * @brief Allows to add a custom location of color schemes. |
---|
298 | * |
---|
299 | * @param[in] custom_dir Custom location of color schemes (must end with /). |
---|
300 | */ |
---|
301 | void addCustomColorSchemeDir(const QString& custom_dir); |
---|
302 | |
---|
303 | private: |
---|
304 | // loads a color scheme from a KDE 4+ .colorscheme file |
---|
305 | bool loadColorScheme(const QString& path); |
---|
306 | // returns a list of paths of color schemes in the KDE 4+ .colorscheme file format |
---|
307 | QList<QString> listColorSchemes(); |
---|
308 | // loads all of the color schemes |
---|
309 | void loadAllColorSchemes(); |
---|
310 | // finds the path of a color scheme |
---|
311 | QString findColorSchemePath(const QString& name) const; |
---|
312 | |
---|
313 | QHash<QString,const ColorScheme*> _colorSchemes; |
---|
314 | QSet<ColorScheme*> _modifiedSchemes; |
---|
315 | |
---|
316 | bool _haveLoadedAll; |
---|
317 | |
---|
318 | static const ColorScheme _defaultColorScheme; |
---|
319 | }; |
---|
320 | |
---|
321 | } |
---|
322 | |
---|
323 | Q_DECLARE_METATYPE(const Konsole::ColorScheme*) |
---|
324 | |
---|
325 | #endif //COLORSCHEME_H |
---|