File indexing completed on 2025-01-19 04:23:25
0001 /* 0002 This source file is part of Konsole, a terminal emulator. 0003 0004 Copyright 2007-2008 by Robert Knight <robertknight@gmail.com> 0005 0006 This program is free software; you can redistribute it and/or modify 0007 it under the terms of the GNU General Public License as published by 0008 the Free Software Foundation; either version 2 of the License, or 0009 (at your option) any later version. 0010 0011 This program is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program; if not, write to the Free Software 0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0019 02110-1301 USA. 0020 */ 0021 0022 #ifndef COLORSCHEME_H 0023 #define COLORSCHEME_H 0024 0025 // Qt 0026 #include <QHash> 0027 #include <QList> 0028 #include <QMetaType> 0029 #include <QIODevice> 0030 #include <QSet> 0031 #include <QSettings> 0032 0033 // Konsole 0034 #include "CharacterColor.h" 0035 0036 class QIODevice; 0037 class KConfig; 0038 0039 namespace Konsole 0040 { 0041 0042 /** 0043 * Represents a color scheme for a terminal display. 0044 * 0045 * The color scheme includes the palette of colors used to draw the text and character backgrounds 0046 * in the display and the opacity level of the display background. 0047 */ 0048 class ColorScheme : public QObject 0049 { 0050 Q_OBJECT 0051 0052 public: 0053 /** 0054 * Constructs a new color scheme which is initialised to the default color set 0055 * for Konsole. 0056 */ 0057 ColorScheme(QObject * parent = nullptr); 0058 ~ColorScheme(); 0059 0060 /** Sets the descriptive name of the color scheme. */ 0061 void setDescription(const QString& description); 0062 /** Returns the descriptive name of the color scheme. */ 0063 QString description() const; 0064 0065 /** Sets the name of the color scheme */ 0066 void setName(const QString& name); 0067 /** Returns the name of the color scheme */ 0068 QString name() const; 0069 0070 #if 0 0071 // Implemented upstream - in user apps 0072 /** Reads the color scheme from the specified configuration source */ 0073 void read(KConfig& config); 0074 0075 #endif 0076 /** Writes the color scheme to the specified configuration source */ 0077 void write(KConfig& config) const; 0078 void read(const QString & filename); 0079 0080 /** Sets a single entry within the color palette. */ 0081 void setColorTableEntry(int index , const ColorEntry& entry); 0082 void setColor(int index, QColor color); 0083 0084 /** 0085 * Copies the color entries which form the palette for this color scheme 0086 * into @p table. @p table should be an array with TABLE_COLORS entries. 0087 * 0088 * @param table Array into which the color entries for this color scheme 0089 * are copied. 0090 * @param randomSeed Color schemes may allow certain colors in their 0091 * palette to be randomized. The seed is used to pick the random color. 0092 */ 0093 void getColorTable(ColorEntry* table, uint randomSeed = 0) const; 0094 0095 /** 0096 * Retrieves a single color entry from the table. 0097 * 0098 * See getColorTable() 0099 */ 0100 ColorEntry colorEntry(int index , uint randomSeed = 0) const; 0101 0102 /** 0103 * Convenience method. Returns the 0104 * foreground color for this scheme, 0105 * this is the primary color used to draw the 0106 * text in this scheme. 0107 */ 0108 QColor foregroundColor() const; 0109 /** 0110 * Convenience method. Returns the background color for 0111 * this scheme, this is the primary color used to 0112 * draw the terminal background in this scheme. 0113 */ 0114 QColor backgroundColor() const; 0115 0116 /** 0117 * Returns true if this color scheme has a dark background. 0118 * The background color is said to be dark if it has a value of less than 127 0119 * in the HSV color space. 0120 */ 0121 bool hasDarkBackground() const; 0122 0123 /** 0124 * Sets the opacity level of the display background. @p opacity ranges 0125 * between 0 (completely transparent background) and 1 (completely 0126 * opaque background). 0127 * 0128 * Defaults to 1. 0129 * 0130 * TODO: More documentation 0131 */ 0132 void setOpacity(qreal opacity); 0133 /** 0134 * Returns the opacity level for this color scheme, see setOpacity() 0135 * TODO: More documentation 0136 */ 0137 qreal opacity() const; 0138 0139 /** 0140 * Enables randomization of the background color. This will cause 0141 * the palette returned by getColorTable() and colorEntry() to 0142 * be adjusted depending on the value of the random seed argument 0143 * to them. 0144 */ 0145 void setRandomizedBackgroundColor(bool randomize); 0146 0147 /** Returns true if the background color is randomized. */ 0148 bool randomizedBackgroundColor() const; 0149 0150 static QString colorNameForIndex(int index); 0151 static QString translatedColorNameForIndex(int index); 0152 0153 private: 0154 // specifies how much a particular color can be randomized by 0155 class RandomizationRange 0156 { 0157 public: 0158 RandomizationRange() : hue(0) , saturation(0) , value(0) {} 0159 0160 bool isNull() const 0161 { 0162 return ( hue == 0 && saturation == 0 && value == 0 ); 0163 } 0164 0165 quint16 hue; 0166 quint8 saturation; 0167 quint8 value; 0168 }; 0169 0170 // returns the active color table. if none has been set specifically, 0171 // this is the default color table. 0172 const ColorEntry* colorTable() const; 0173 0174 #if 0 0175 // implemented upstream - user apps 0176 // reads a single colour entry from a KConfig source 0177 // and sets the palette entry at 'index' to the entry read. 0178 void readColorEntry(KConfig& config , int index); 0179 // writes a single colour entry to a KConfig source 0180 #endif 0181 void writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry,const RandomizationRange& range) const; 0182 0183 void readColorEntry(QSettings *s, int index); 0184 0185 // sets the amount of randomization allowed for a particular color 0186 // in the palette. creates the randomization table if 0187 // it does not already exist 0188 void setRandomizationRange( int index , quint16 hue , quint8 saturation , quint8 value ); 0189 0190 QString _description; 0191 QString _name; 0192 qreal _opacity; 0193 ColorEntry* _table; // pointer to custom color table or 0 if the default 0194 // color scheme is being used 0195 0196 0197 static const quint16 MAX_HUE = 340; 0198 0199 RandomizationRange* _randomTable; // pointer to randomization table or 0 0200 // if no colors in the color scheme support 0201 // randomization 0202 0203 static const char* const colorNames[TABLE_COLORS]; 0204 static const char* const translatedColorNames[TABLE_COLORS]; 0205 0206 static const ColorEntry defaultTable[]; // table of default color entries 0207 0208 Q_SIGNALS: 0209 void colorChanged(int index); 0210 }; 0211 0212 /** 0213 * A color scheme which uses colors from the standard KDE color palette. 0214 * 0215 * This is designed primarily for the benefit of users who are using specially 0216 * designed colors. 0217 * 0218 * TODO Implement and make it the default on systems with specialized KDE 0219 * color schemes. 0220 */ 0221 class AccessibleColorScheme : public ColorScheme 0222 { 0223 public: 0224 AccessibleColorScheme(); 0225 }; 0226 0227 /** 0228 * Reads a color scheme stored in the .schema format used in the KDE 3 incarnation 0229 * of Konsole 0230 * 0231 * Only the basic essentials ( title and color palette entries ) are currently 0232 * supported. Additional options such as background image and background 0233 * blend colors are ignored. 0234 */ 0235 class KDE3ColorSchemeReader 0236 { 0237 public: 0238 /** 0239 * Constructs a new reader which reads from the specified device. 0240 * The device should be open in read-only mode. 0241 */ 0242 KDE3ColorSchemeReader( QIODevice* device ); 0243 0244 /** 0245 * Reads and parses the contents of the .schema file from the input 0246 * device and returns the ColorScheme defined within it. 0247 * 0248 * Returns a null pointer if an error occurs whilst parsing 0249 * the contents of the file. 0250 */ 0251 ColorScheme* read(); 0252 0253 private: 0254 // reads a line from the file specifying a colour palette entry 0255 // format is: color [index] [red] [green] [blue] [transparent] [bold] 0256 bool readColorLine(const QString& line , ColorScheme* scheme); 0257 bool readTitleLine(const QString& line , ColorScheme* scheme); 0258 0259 QIODevice* _device; 0260 }; 0261 0262 /** 0263 * Manages the color schemes available for use by terminal displays. 0264 * See ColorScheme 0265 */ 0266 class ColorSchemeManager 0267 { 0268 public: 0269 0270 /** 0271 * Constructs a new ColorSchemeManager and loads the list 0272 * of available color schemes. 0273 * 0274 * The color schemes themselves are not loaded until they are first 0275 * requested via a call to findColorScheme() 0276 */ 0277 ColorSchemeManager(); 0278 /** 0279 * Destroys the ColorSchemeManager and saves any modified color schemes to disk. 0280 */ 0281 ~ColorSchemeManager(); 0282 0283 /** 0284 * Returns the default color scheme for Konsole 0285 */ 0286 const ColorScheme *defaultColorScheme() const; 0287 0288 /** 0289 * Returns the color scheme with the given name or 0 if no 0290 * scheme with that name exists. If @p name is empty, the 0291 * default color scheme is returned. 0292 * 0293 * The first time that a color scheme with a particular name is 0294 * requested, the configuration information is loaded from disk. 0295 */ 0296 const ColorScheme* findColorScheme(const QString& name); 0297 0298 #if 0 0299 /** 0300 * Adds a new color scheme to the manager. If @p scheme has the same name as 0301 * an existing color scheme, it replaces the existing scheme. 0302 * 0303 * TODO - Ensure the old color scheme gets deleted 0304 */ 0305 void addColorScheme(ColorScheme* scheme); 0306 #endif 0307 /** 0308 * Deletes a color scheme. Returns true on successful deletion or false otherwise. 0309 */ 0310 bool deleteColorScheme(const QString& name); 0311 0312 /** 0313 * Returns a list of the all the available color schemes. 0314 * This may be slow when first called because all of the color 0315 * scheme resources on disk must be located, read and parsed. 0316 * 0317 * Subsequent calls will be inexpensive. 0318 */ 0319 QList<const ColorScheme*> allColorSchemes(); 0320 0321 /** Returns the global color scheme manager instance. */ 0322 static ColorSchemeManager* instance(); 0323 0324 /** @brief Loads a custom color scheme under given \em path. 0325 * 0326 * The \em path may refer to either KDE 4 .colorscheme or KDE 3 0327 * .schema file 0328 * 0329 * The loaded color scheme is available under the name equal to 0330 * the base name of the \em path via the allColorSchemes() and 0331 * findColorScheme() methods after this call if loaded successfully. 0332 * 0333 * @param[in] path The path to KDE 4 .colorscheme or KDE 3 .schema. 0334 * @return Whether the color scheme is loaded successfully. 0335 */ 0336 bool loadCustomColorScheme(const QString& path); 0337 0338 /** 0339 * @brief Allows to add a custom location of color schemes. 0340 * 0341 * @param[in] custom_dir Custom location of color schemes (must end with /). 0342 */ 0343 void addCustomColorSchemeDir(const QString& custom_dir); 0344 0345 private: 0346 // loads a color scheme from a KDE 4+ .colorscheme file 0347 bool loadColorScheme(const QString& path); 0348 // loads a color scheme from a KDE 3 .schema file 0349 bool loadKDE3ColorScheme(const QString& path); 0350 // returns a list of paths of color schemes in the KDE 4+ .colorscheme file format 0351 QList<QString> listColorSchemes(); 0352 // returns a list of paths of color schemes in the .schema file format 0353 // used in KDE 3 0354 QList<QString> listKDE3ColorSchemes(); 0355 // loads all of the color schemes 0356 void loadAllColorSchemes(); 0357 // finds the path of a color scheme 0358 QString findColorSchemePath(const QString& name) const; 0359 0360 QHash<QString,const ColorScheme*> _colorSchemes; 0361 QSet<ColorScheme*> _modifiedSchemes; 0362 0363 bool _haveLoadedAll; 0364 }; 0365 0366 } 0367 0368 Q_DECLARE_METATYPE(const Konsole::ColorScheme*) 0369 0370 #endif //COLORSCHEME_H