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