~ubuntu-branches/ubuntu/saucy/konsole/saucy-proposed

« back to all changes in this revision

Viewing changes to src/ColorScheme.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-06-06 14:29:24 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20120606142924-1rekqv6j25lw2k41
Tags: 4:4.8.80-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
// Qt
26
26
#include <QtGui/QBrush>
27
 
#include <QtCore/QFile>
28
 
#include <QtCore/QFileInfo>
29
27
#include <QtGui/QPainter>
30
28
 
31
29
// KDE
32
30
#include <KColorScheme>
33
31
#include <KConfig>
34
 
#include <KLocale>
35
 
#include <KDebug>
 
32
#include <KLocalizedString>
36
33
#include <KConfigGroup>
37
 
#include <KStandardDirs>
 
34
 
 
35
namespace {
 
36
const int FGCOLOR_INDEX = 0;
 
37
const int BGCOLOR_INDEX = 1;
 
38
}
38
39
 
39
40
using namespace Konsole;
40
41
 
 
42
// The following are almost IBM standard color codes, with some slight
 
43
// gamma correction for the dim colors to compensate for bright X screens.
 
44
// It contains the 8 ansiterm/xterm colors in 2 intensities.
41
45
const ColorEntry ColorScheme::defaultTable[TABLE_COLORS] =
42
 
 // The following are almost IBM standard color codes, with some slight
43
 
 // gamma correction for the dim colors to compensate for bright X screens.
44
 
 // It contains the 8 ansiterm/xterm colors in 2 intensities.
45
46
{
46
 
    ColorEntry( QColor(0x00,0x00,0x00), 0), ColorEntry(
47
 
QColor(0xFF,0xFF,0xFF), 1), // Dfore, Dback
48
 
    ColorEntry( QColor(0x00,0x00,0x00), 0), ColorEntry(
49
 
QColor(0xB2,0x18,0x18), 0), // Black, Red
50
 
    ColorEntry( QColor(0x18,0xB2,0x18), 0), ColorEntry(
51
 
QColor(0xB2,0x68,0x18), 0), // Green, Yellow
52
 
    ColorEntry( QColor(0x18,0x18,0xB2), 0), ColorEntry(
53
 
QColor(0xB2,0x18,0xB2), 0), // Blue, Magenta
54
 
    ColorEntry( QColor(0x18,0xB2,0xB2), 0), ColorEntry(
55
 
QColor(0xB2,0xB2,0xB2), 0), // Cyan, White
56
 
    // intensive
57
 
    ColorEntry( QColor(0x00,0x00,0x00), 0), ColorEntry(
58
 
QColor(0xFF,0xFF,0xFF), 1),
59
 
    ColorEntry( QColor(0x68,0x68,0x68), 0), ColorEntry(
60
 
QColor(0xFF,0x54,0x54), 0),
61
 
    ColorEntry( QColor(0x54,0xFF,0x54), 0), ColorEntry(
62
 
QColor(0xFF,0xFF,0x54), 0),
63
 
    ColorEntry( QColor(0x54,0x54,0xFF), 0), ColorEntry(
64
 
QColor(0xFF,0x54,0xFF), 0),
65
 
    ColorEntry( QColor(0x54,0xFF,0xFF), 0), ColorEntry(
66
 
QColor(0xFF,0xFF,0xFF), 0)
 
47
    ColorEntry(QColor(0x00, 0x00, 0x00)), // Dfore
 
48
    ColorEntry(QColor(0xFF, 0xFF, 0xFF)), // Dback
 
49
    ColorEntry(QColor(0x00, 0x00, 0x00)), // Black
 
50
    ColorEntry(QColor(0xB2, 0x18, 0x18)), // Red
 
51
    ColorEntry(QColor(0x18, 0xB2, 0x18)), // Green
 
52
    ColorEntry(QColor(0xB2, 0x68, 0x18)), // Yellow
 
53
    ColorEntry(QColor(0x18, 0x18, 0xB2)), // Blue
 
54
    ColorEntry(QColor(0xB2, 0x18, 0xB2)), // Magenta
 
55
    ColorEntry(QColor(0x18, 0xB2, 0xB2)), // Cyan
 
56
    ColorEntry(QColor(0xB2, 0xB2, 0xB2)), // White
 
57
    // intensive versions
 
58
    ColorEntry(QColor(0x00, 0x00, 0x00)),
 
59
    ColorEntry(QColor(0xFF, 0xFF, 0xFF)),
 
60
    ColorEntry(QColor(0x68, 0x68, 0x68)),
 
61
    ColorEntry(QColor(0xFF, 0x54, 0x54)),
 
62
    ColorEntry(QColor(0x54, 0xFF, 0x54)),
 
63
    ColorEntry(QColor(0xFF, 0xFF, 0x54)),
 
64
    ColorEntry(QColor(0x54, 0x54, 0xFF)),
 
65
    ColorEntry(QColor(0xFF, 0x54, 0xFF)),
 
66
    ColorEntry(QColor(0x54, 0xFF, 0xFF)),
 
67
    ColorEntry(QColor(0xFF, 0xFF, 0xFF))
67
68
};
68
69
 
69
 
const char* const ColorScheme::colorNames[TABLE_COLORS] =
70
 
{
71
 
  "Foreground",
72
 
  "Background",
73
 
  "Color0",
74
 
  "Color1",
75
 
  "Color2",
76
 
  "Color3",
77
 
  "Color4",
78
 
  "Color5",
79
 
  "Color6",
80
 
  "Color7",
81
 
  "ForegroundIntense",
82
 
  "BackgroundIntense",
83
 
  "Color0Intense",
84
 
  "Color1Intense",
85
 
  "Color2Intense",
86
 
  "Color3Intense",
87
 
  "Color4Intense",
88
 
  "Color5Intense",
89
 
  "Color6Intense",
90
 
  "Color7Intense"
 
70
const char* const ColorScheme::colorNames[TABLE_COLORS] = {
 
71
    "Foreground",
 
72
    "Background",
 
73
    "Color0",
 
74
    "Color1",
 
75
    "Color2",
 
76
    "Color3",
 
77
    "Color4",
 
78
    "Color5",
 
79
    "Color6",
 
80
    "Color7",
 
81
    "ForegroundIntense",
 
82
    "BackgroundIntense",
 
83
    "Color0Intense",
 
84
    "Color1Intense",
 
85
    "Color2Intense",
 
86
    "Color3Intense",
 
87
    "Color4Intense",
 
88
    "Color5Intense",
 
89
    "Color6Intense",
 
90
    "Color7Intense"
91
91
};
92
 
const char* const ColorScheme::translatedColorNames[TABLE_COLORS] =
93
 
{
 
92
const char* const ColorScheme::translatedColorNames[TABLE_COLORS] = {
94
93
    I18N_NOOP2("@item:intable palette", "Foreground"),
95
94
    I18N_NOOP2("@item:intable palette", "Background"),
96
95
    I18N_NOOP2("@item:intable palette", "Color 1"),
113
112
    I18N_NOOP2("@item:intable palette", "Color 8 (Intense)")
114
113
};
115
114
 
 
115
QString ColorScheme::colorNameForIndex(int index)
 
116
{
 
117
    Q_ASSERT(index >= 0 && index < TABLE_COLORS);
 
118
 
 
119
    return QString(colorNames[index]);
 
120
}
 
121
 
 
122
QString ColorScheme::translatedColorNameForIndex(int index)
 
123
{
 
124
    Q_ASSERT(index >= 0 && index < TABLE_COLORS);
 
125
 
 
126
    return i18nc("@item:intable palette", translatedColorNames[index]);
 
127
}
 
128
 
116
129
ColorScheme::ColorScheme()
117
130
{
118
131
    _table = 0;
122
135
}
123
136
 
124
137
ColorScheme::ColorScheme(const ColorScheme& other)
125
 
      : _opacity(other._opacity)
126
 
       ,_table(0)
127
 
       ,_randomTable(0)
128
 
       ,_wallpaper(other._wallpaper)
 
138
    : _table(0)
 
139
    , _randomTable(0)
 
140
    , _opacity(other._opacity)
 
141
    , _wallpaper(other._wallpaper)
129
142
{
130
143
    setName(other.name());
131
144
    setDescription(other.description());
132
145
 
133
 
    if ( other._table != 0 )
134
 
    {
135
 
        for ( int i = 0 ; i < TABLE_COLORS ; i++ )
136
 
            setColorTableEntry(i,other._table[i]);
 
146
    if (other._table != 0) {
 
147
        for (int i = 0 ; i < TABLE_COLORS ; i++)
 
148
            setColorTableEntry(i, other._table[i]);
137
149
    }
138
150
 
139
 
    if ( other._randomTable != 0 )
140
 
    {
141
 
        for ( int i = 0 ; i < TABLE_COLORS ; i++ )
142
 
        {
 
151
    if (other._randomTable != 0) {
 
152
        for (int i = 0 ; i < TABLE_COLORS ; i++) {
143
153
            const RandomizationRange& range = other._randomTable[i];
144
 
            setRandomizationRange(i,range.hue,range.saturation,range.value);
 
154
            setRandomizationRange(i, range.hue, range.saturation, range.value);
145
155
        }
146
156
    }
147
157
}
151
161
    delete[] _randomTable;
152
162
}
153
163
 
154
 
void ColorScheme::setDescription(const QString& description) { _description = description; }
155
 
QString ColorScheme::description() const { return _description; }
 
164
void ColorScheme::setDescription(const QString& aDescription)
 
165
{
 
166
    _description = aDescription;
 
167
}
 
168
QString ColorScheme::description() const
 
169
{
 
170
    return _description;
 
171
}
156
172
 
157
 
void ColorScheme::setName(const QString& name) { _name = name; }
158
 
QString ColorScheme::name() const { return _name; }
 
173
void ColorScheme::setName(const QString& aName)
 
174
{
 
175
    _name = aName;
 
176
}
 
177
QString ColorScheme::name() const
 
178
{
 
179
    return _name;
 
180
}
159
181
 
160
182
void ColorScheme::setColorTableEntry(int index , const ColorEntry& entry)
161
183
{
162
 
    Q_ASSERT( index >= 0 && index < TABLE_COLORS );
 
184
    Q_ASSERT(index >= 0 && index < TABLE_COLORS);
163
185
 
164
 
    if ( !_table ) 
165
 
    {
 
186
    if (!_table) {
166
187
        _table = new ColorEntry[TABLE_COLORS];
167
188
 
168
 
        for (int i=0;i<TABLE_COLORS;i++)
 
189
        for (int i = 0; i < TABLE_COLORS; i++)
169
190
            _table[i] = defaultTable[i];
170
191
    }
171
192
 
172
 
    _table[index] = entry; 
 
193
    _table[index] = entry;
173
194
}
174
195
ColorEntry ColorScheme::colorEntry(int index , uint randomSeed) const
175
196
{
176
 
    Q_ASSERT( index >= 0 && index < TABLE_COLORS );
 
197
    Q_ASSERT(index >= 0 && index < TABLE_COLORS);
177
198
 
178
 
    if ( randomSeed != 0 )
 
199
    if (randomSeed != 0)
179
200
        qsrand(randomSeed);
180
201
 
181
202
    ColorEntry entry = colorTable()[index];
182
203
 
183
 
    if ( randomSeed != 0 && 
184
 
        _randomTable != 0 && 
185
 
        !_randomTable[index].isNull() )
186
 
    {
 
204
    if (randomSeed != 0 &&
 
205
            _randomTable != 0 &&
 
206
            !_randomTable[index].isNull()) {
187
207
        const RandomizationRange& range = _randomTable[index];
188
208
 
189
 
 
190
 
        int hueDifference = range.hue ? (qrand() % range.hue) - range.hue/2 : 0;
191
 
        int saturationDifference = range.saturation ? (qrand() % range.saturation) - range.saturation/2 : 0;
192
 
        int  valueDifference = range.value ? (qrand() % range.value) - range.value/2 : 0;
 
209
        int hueDifference = range.hue ? (qrand() % range.hue) - range.hue / 2 : 0;
 
210
        int saturationDifference = range.saturation ? (qrand() % range.saturation) - range.saturation / 2 : 0;
 
211
        int  valueDifference = range.value ? (qrand() % range.value) - range.value / 2 : 0;
193
212
 
194
213
        QColor& color = entry.color;
195
214
 
196
 
        int newHue = qAbs( (color.hue() + hueDifference) % MAX_HUE );
197
 
        int newValue = qMin( qAbs(color.value() + valueDifference) , 255 );
198
 
        int newSaturation = qMin( qAbs(color.saturation() + saturationDifference) , 255 );
 
215
        int newHue = qAbs((color.hue() + hueDifference) % MAX_HUE);
 
216
        int newValue = qMin(qAbs(color.value() + valueDifference) , 255);
 
217
        int newSaturation = qMin(qAbs(color.saturation() + saturationDifference) , 255);
199
218
 
200
 
        color.setHsv(newHue,newSaturation,newValue);
 
219
        color.setHsv(newHue, newSaturation, newValue);
201
220
    }
202
221
 
203
222
    return entry;
204
223
}
205
224
void ColorScheme::getColorTable(ColorEntry* table , uint randomSeed) const
206
225
{
207
 
    for ( int i = 0 ; i < TABLE_COLORS ; i++ )
208
 
        table[i] = colorEntry(i,randomSeed);
 
226
    for (int i = 0 ; i < TABLE_COLORS ; i++)
 
227
        table[i] = colorEntry(i, randomSeed);
209
228
}
210
229
bool ColorScheme::randomizedBackgroundColor() const
211
230
{
212
 
    return _randomTable == 0 ? false : !_randomTable[1].isNull();
 
231
    return _randomTable == 0 ? false : !_randomTable[BGCOLOR_INDEX].isNull();
213
232
}
214
233
void ColorScheme::setRandomizedBackgroundColor(bool randomize)
215
234
{
217
236
    // adjusted as much as possible.
218
237
    //
219
238
    // the value and saturation are left alone to maintain read-ability
220
 
    if ( randomize )
221
 
    {
222
 
        setRandomizationRange( 1 /* background color index */ , MAX_HUE , 255 , 0 ); 
223
 
    }
224
 
    else
225
 
    {
226
 
        if ( _randomTable )
227
 
            setRandomizationRange( 1 /* background color index */ , 0 , 0 , 0 );
 
239
    if (randomize) {
 
240
        setRandomizationRange(BGCOLOR_INDEX , MAX_HUE , 255 , 0);
 
241
    } else {
 
242
        if (_randomTable)
 
243
            setRandomizationRange(BGCOLOR_INDEX , 0 , 0 , 0);
228
244
    }
229
245
}
230
246
 
231
 
void ColorScheme::setRandomizationRange( int index , quint16 hue , quint8 saturation ,
232
 
                                         quint8 value )
 
247
void ColorScheme::setRandomizationRange(int index , quint16 hue , quint8 saturation ,
 
248
                                        quint8 value)
233
249
{
234
 
    Q_ASSERT( hue <= MAX_HUE );
235
 
    Q_ASSERT( index >= 0 && index < TABLE_COLORS );
 
250
    Q_ASSERT(hue <= MAX_HUE);
 
251
    Q_ASSERT(index >= 0 && index < TABLE_COLORS);
236
252
 
237
 
    if ( _randomTable == 0 )
 
253
    if (_randomTable == 0)
238
254
        _randomTable = new RandomizationRange[TABLE_COLORS];
239
255
 
240
256
    _randomTable[index].hue = hue;
244
260
 
245
261
const ColorEntry* ColorScheme::colorTable() const
246
262
{
247
 
    if ( _table )
 
263
    if (_table)
248
264
        return _table;
249
265
    else
250
266
        return defaultTable;
251
267
}
252
268
QColor ColorScheme::foregroundColor() const
253
269
{
254
 
    return colorTable()[0].color;
 
270
    return colorTable()[FGCOLOR_INDEX].color;
255
271
}
256
272
QColor ColorScheme::backgroundColor() const
257
273
{
258
 
    return colorTable()[1].color;
 
274
    return colorTable()[BGCOLOR_INDEX].color;
259
275
}
260
276
bool ColorScheme::hasDarkBackground() const
261
277
{
263
279
    // so 127 is in the middle, anything less is deemed 'dark'
264
280
    return backgroundColor().value() < 127;
265
281
}
266
 
void ColorScheme::setOpacity(qreal opacity) { _opacity = opacity; }
267
 
qreal ColorScheme::opacity() const { return _opacity; }
 
282
void ColorScheme::setOpacity(qreal aOpacity)
 
283
{
 
284
    _opacity = aOpacity;
 
285
}
 
286
qreal ColorScheme::opacity() const
 
287
{
 
288
    return _opacity;
 
289
}
268
290
 
269
 
void ColorScheme::read(KConfig& config)
 
291
void ColorScheme::read(const KConfig& config)
270
292
{
271
293
    KConfigGroup configGroup = config.group("General");
272
294
 
273
 
    QString description = configGroup.readEntry("Description", I18N_NOOP("Un-named Color Scheme"));
 
295
    const QString schemeDescription = configGroup.readEntry("Description", I18N_NOOP("Un-named Color Scheme"));
274
296
 
275
 
    _description = i18n(description.toUtf8());
276
 
    _opacity = configGroup.readEntry("Opacity",qreal(1.0));
 
297
    _description = i18n(schemeDescription.toUtf8());
 
298
    _opacity = configGroup.readEntry("Opacity", qreal(1.0));
277
299
    setWallpaper(configGroup.readEntry("Wallpaper", QString()));
278
300
 
279
 
    for (int i=0 ; i < TABLE_COLORS ; i++)
280
 
    {
281
 
        readColorEntry(config,i);
282
 
    }
283
 
}
284
 
void ColorScheme::write(KConfig& config) const
285
 
{
286
 
    KConfigGroup configGroup = config.group("General");
287
 
 
288
 
    configGroup.writeEntry("Description",_description);
289
 
    configGroup.writeEntry("Opacity",_opacity);
290
 
    configGroup.writeEntry("Wallpaper", _wallpaper->path());
291
 
 
292
 
    for (int i=0 ; i < TABLE_COLORS ; i++)
293
 
    {
294
 
        RandomizationRange random = _randomTable != 0 ? _randomTable[i] : RandomizationRange();
295
 
        writeColorEntry(config,colorNameForIndex(i),colorTable()[i],random);
296
 
    }
297
 
}
298
 
 
299
 
QString ColorScheme::colorNameForIndex(int index) 
300
 
{
301
 
    Q_ASSERT( index >= 0 && index < TABLE_COLORS );
302
 
 
303
 
    return QString(colorNames[index]);
304
 
}
305
 
QString ColorScheme::translatedColorNameForIndex(int index) 
306
 
{
307
 
    Q_ASSERT( index >= 0 && index < TABLE_COLORS );
308
 
 
309
 
    return i18nc("@item:intable palette", translatedColorNames[index]);
310
 
}
311
 
void ColorScheme::readColorEntry(KConfig& config , int index)
312
 
{
313
 
    KConfigGroup configGroup(&config,colorNameForIndex(index));
 
301
    for (int i = 0 ; i < TABLE_COLORS ; i++) {
 
302
        readColorEntry(config, i);
 
303
    }
 
304
}
 
305
 
 
306
void ColorScheme::readColorEntry(const KConfig& config , int index)
 
307
{
 
308
    KConfigGroup configGroup = config.group(colorNameForIndex(index));
314
309
 
315
310
    ColorEntry entry;
316
311
 
317
 
    entry.color = configGroup.readEntry("Color",QColor());
318
 
    entry.transparent = configGroup.readEntry("Transparent",false);
 
312
    entry.color = configGroup.readEntry("Color", QColor());
319
313
 
320
314
    // Deprecated key from KDE 4.0 which set 'Bold' to true to force
321
315
    // a color to be bold or false to use the current format
323
317
    // TODO - Add a new tri-state key which allows for bold, normal or
324
318
    // current format
325
319
    if (configGroup.hasKey("Bold"))
326
 
        entry.fontWeight = configGroup.readEntry("Bold",false) ? ColorEntry::Bold :
327
 
                                                                 ColorEntry::UseCurrentFormat;
328
 
 
329
 
    quint16 hue = configGroup.readEntry("MaxRandomHue",0);
330
 
    quint8 value = configGroup.readEntry("MaxRandomValue",0);
331
 
    quint8 saturation = configGroup.readEntry("MaxRandomSaturation",0);
332
 
 
333
 
    setColorTableEntry( index , entry );
334
 
 
335
 
    if ( hue != 0 || value != 0 || saturation != 0 )
336
 
       setRandomizationRange( index , hue , saturation , value ); 
337
 
}
338
 
void ColorScheme::writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry , const RandomizationRange& random) const
339
 
{
340
 
    KConfigGroup configGroup(&config,colorName);
341
 
 
342
 
    configGroup.writeEntry("Color",entry.color);
343
 
    configGroup.writeEntry("Transparency",(bool)entry.transparent);
344
 
    if (entry.fontWeight != ColorEntry::UseCurrentFormat)
345
 
    {
346
 
        configGroup.writeEntry("Bold",entry.fontWeight == ColorEntry::Bold);
347
 
    }
348
 
 
349
 
    // record randomization if this color has randomization or 
350
 
    // if one of the keys already exists 
351
 
    if ( !random.isNull() || configGroup.hasKey("MaxRandomHue") )
352
 
    {
353
 
        configGroup.writeEntry("MaxRandomHue",(int)random.hue);
354
 
        configGroup.writeEntry("MaxRandomValue",(int)random.value);
355
 
        configGroup.writeEntry("MaxRandomSaturation",(int)random.saturation);
 
320
        entry.fontWeight = configGroup.readEntry("Bold", false) ? ColorEntry::Bold :
 
321
                           ColorEntry::UseCurrentFormat;
 
322
 
 
323
    setColorTableEntry(index , entry);
 
324
 
 
325
    const quint16 hue = configGroup.readEntry("MaxRandomHue", 0);
 
326
    const quint8 value = configGroup.readEntry("MaxRandomValue", 0);
 
327
    const quint8 saturation = configGroup.readEntry("MaxRandomSaturation", 0);
 
328
 
 
329
    if (hue != 0 || value != 0 || saturation != 0)
 
330
        setRandomizationRange(index , hue , saturation , value);
 
331
}
 
332
 
 
333
void ColorScheme::write(KConfig& config) const
 
334
{
 
335
    KConfigGroup configGroup = config.group("General");
 
336
 
 
337
    configGroup.writeEntry("Description", _description);
 
338
    configGroup.writeEntry("Opacity", _opacity);
 
339
    configGroup.writeEntry("Wallpaper", _wallpaper->path());
 
340
 
 
341
    for (int i = 0 ; i < TABLE_COLORS ; i++) {
 
342
        writeColorEntry(config, i);
 
343
    }
 
344
}
 
345
 
 
346
void ColorScheme::writeColorEntry(KConfig& config , int index) const
 
347
{
 
348
    KConfigGroup configGroup = config.group(colorNameForIndex(index));
 
349
 
 
350
    const ColorEntry& entry = colorTable()[index];
 
351
 
 
352
    configGroup.writeEntry("Color", entry.color);
 
353
    if ( configGroup.hasKey("Transparent") ) {
 
354
        configGroup.deleteEntry("Transparent");
 
355
    }
 
356
    if (entry.fontWeight != ColorEntry::UseCurrentFormat) {
 
357
        configGroup.writeEntry("Bold", entry.fontWeight == ColorEntry::Bold);
 
358
    }
 
359
 
 
360
    RandomizationRange random = _randomTable != 0 ? _randomTable[index] : RandomizationRange();
 
361
 
 
362
    // record randomization if this color has randomization or
 
363
    // if one of the keys already exists
 
364
    if (!random.isNull() || configGroup.hasKey("MaxRandomHue")) {
 
365
        configGroup.writeEntry("MaxRandomHue", (int)random.hue);
 
366
        configGroup.writeEntry("MaxRandomValue", (int)random.value);
 
367
        configGroup.writeEntry("MaxRandomSaturation", (int)random.saturation);
356
368
    }
357
369
}
358
370
 
359
371
void ColorScheme::setWallpaper(const QString& path)
360
372
{
361
 
    _wallpaper.attach(new ColorSchemeWallpaper(path));
 
373
    _wallpaper = new ColorSchemeWallpaper(path);
362
374
}
363
375
 
364
376
ColorSchemeWallpaper::Ptr ColorScheme::wallpaper() const
366
378
    return _wallpaper;
367
379
}
368
380
 
369
 
ColorSchemeWallpaper::ColorSchemeWallpaper(const QString& path)
370
 
    : _path(path),
 
381
ColorSchemeWallpaper::ColorSchemeWallpaper(const QString& aPath)
 
382
    : _path(aPath),
371
383
      _picture(0)
372
384
{
373
385
}
409
421
    return _path;
410
422
}
411
423
 
412
 
// 
 
424
//
413
425
// Work In Progress - A color scheme for use on KDE setups for users
414
426
// with visual disabilities which means that they may have trouble
415
427
// reading text with the supplied color schemes.
416
428
//
417
429
// This color scheme uses only the 'safe' colors defined by the
418
 
// KColorScheme class.  
 
430
// KColorScheme class.
419
431
//
420
 
// A complication this introduces is that each color provided by 
 
432
// A complication this introduces is that each color provided by
421
433
// KColorScheme is defined as a 'background' or 'foreground' color.
422
 
// Only foreground colors are allowed to be used to render text and 
 
434
// Only foreground colors are allowed to be used to render text and
423
435
// only background colors are allowed to be used for backgrounds.
424
436
//
425
437
// The ColorEntry and TerminalDisplay classes do not currently
426
 
// support this restriction.  
 
438
// support this restriction.
427
439
//
428
440
// Requirements:
429
441
//  - A color scheme which uses only colors from the KColorScheme class
430
 
//  - Ability to restrict which colors the TerminalDisplay widget 
 
442
//  - Ability to restrict which colors the TerminalDisplay widget
431
443
//    uses as foreground and background color
432
444
//  - Make use of KGlobalSettings::allowDefaultBackgroundImages() as
433
 
//    a hint to determine whether this accessible color scheme should 
 
445
//    a hint to determine whether this accessible color scheme should
434
446
//    be used by default.
435
447
//
436
448
//
448
460
 
449
461
    const KColorScheme colorScheme(QPalette::Active);
450
462
 
451
 
    QBrush colors[ColorRoleCount] =
452
 
    {
453
 
        colorScheme.foreground( colorScheme.NormalText ),
454
 
        colorScheme.background( colorScheme.NormalBackground ),
 
463
    QBrush colors[ColorRoleCount] = {
 
464
        colorScheme.foreground(colorScheme.NormalText),
 
465
        colorScheme.background(colorScheme.NormalBackground),
455
466
 
456
 
        colorScheme.foreground( colorScheme.InactiveText ),
457
 
        colorScheme.foreground( colorScheme.ActiveText ),
458
 
        colorScheme.foreground( colorScheme.LinkText ),
459
 
        colorScheme.foreground( colorScheme.VisitedText ),
460
 
        colorScheme.foreground( colorScheme.NegativeText ),
461
 
        colorScheme.foreground( colorScheme.NeutralText )
 
467
        colorScheme.foreground(colorScheme.InactiveText),
 
468
        colorScheme.foreground(colorScheme.ActiveText),
 
469
        colorScheme.foreground(colorScheme.LinkText),
 
470
        colorScheme.foreground(colorScheme.VisitedText),
 
471
        colorScheme.foreground(colorScheme.NegativeText),
 
472
        colorScheme.foreground(colorScheme.NeutralText)
462
473
    };
463
474
 
464
 
    for ( int i = 0 ; i < TABLE_COLORS ; i++ ) 
465
 
    {
 
475
    for (int i = 0 ; i < TABLE_COLORS ; i++) {
466
476
        ColorEntry entry;
467
477
        entry.color = colors[ i % ColorRoleCount ].color();
468
478
 
469
 
        setColorTableEntry( i , entry ); 
470
 
    }   
471
 
}
472
 
 
473
 
KDE3ColorSchemeReader::KDE3ColorSchemeReader( QIODevice* device ) :
474
 
    _device(device)
475
 
{
476
 
}
477
 
ColorScheme* KDE3ColorSchemeReader::read() 
478
 
{
479
 
    Q_ASSERT( _device->openMode() == QIODevice::ReadOnly ||
480
 
              _device->openMode() == QIODevice::ReadWrite  );
481
 
 
482
 
    ColorScheme* scheme = new ColorScheme();
483
 
 
484
 
    QRegExp comment("#.*$");
485
 
    while ( !_device->atEnd() )
486
 
    {
487
 
        QString line(_device->readLine());
488
 
        line.remove(comment);
489
 
        line = line.simplified();
490
 
 
491
 
        if ( line.isEmpty() )
492
 
            continue;
493
 
 
494
 
        if ( line.startsWith(QLatin1String("color")) )
495
 
        {
496
 
            if (!readColorLine(line,scheme))
497
 
                kWarning() << "Failed to read KDE 3 color scheme line" << line;
498
 
        }
499
 
        else if ( line.startsWith(QLatin1String("title")) )
500
 
        {
501
 
            if (!readTitleLine(line,scheme))
502
 
                kWarning() << "Failed to read KDE 3 color scheme title line" << line;
503
 
        }
504
 
        else
505
 
        {
506
 
            kWarning() << "KDE 3 color scheme contains an unsupported feature, '" <<
507
 
                line << "'";
508
 
        } 
509
 
    }
510
 
 
511
 
    return scheme;
512
 
}
513
 
bool KDE3ColorSchemeReader::readColorLine(const QString& line,ColorScheme* scheme)
514
 
{
515
 
    QStringList list = line.split(QChar(' '));
516
 
 
517
 
    if (list.count() != 7)
518
 
        return false;
519
 
    if (list.first() != "color")
520
 
        return false;
521
 
 
522
 
    int index = list[1].toInt();
523
 
    int red = list[2].toInt();
524
 
    int green = list[3].toInt();
525
 
    int blue = list[4].toInt();
526
 
    int transparent = list[5].toInt();
527
 
    int bold = list[6].toInt();
528
 
 
529
 
    const int MAX_COLOR_VALUE = 255;
530
 
 
531
 
    if(     (index < 0 || index >= TABLE_COLORS )
532
 
        ||  (red < 0 || red > MAX_COLOR_VALUE )
533
 
        ||  (blue < 0 || blue > MAX_COLOR_VALUE )
534
 
        ||  (green < 0 || green > MAX_COLOR_VALUE )
535
 
        ||  (transparent != 0 && transparent != 1 )
536
 
        ||  (bold != 0 && bold != 1)    )
537
 
        return false;
538
 
 
539
 
    ColorEntry entry;
540
 
    entry.color = QColor(red,green,blue);
541
 
    entry.transparent = ( transparent != 0 );
542
 
    entry.fontWeight = ( bold != 0 ) ? ColorEntry::Bold : ColorEntry::UseCurrentFormat;
543
 
 
544
 
    scheme->setColorTableEntry(index,entry);
545
 
    return true;
546
 
}
547
 
bool KDE3ColorSchemeReader::readTitleLine(const QString& line,ColorScheme* scheme)
548
 
{
549
 
    if( !line.startsWith(QLatin1String("title")) )
550
 
        return false;
551
 
 
552
 
    int spacePos = line.indexOf(' ');
553
 
    if( spacePos == -1 )
554
 
        return false;
555
 
 
556
 
    QString description = line.mid(spacePos+1);
557
 
 
558
 
    scheme->setDescription(i18n(description.toUtf8()));
559
 
    return true;
560
 
}
561
 
ColorSchemeManager::ColorSchemeManager()
562
 
    : _haveLoadedAll(false)
563
 
{
564
 
}
565
 
ColorSchemeManager::~ColorSchemeManager()
566
 
{
567
 
    QHashIterator<QString,const ColorScheme*> iter(_colorSchemes);
568
 
    while (iter.hasNext())
569
 
    {
570
 
        iter.next();
571
 
        delete iter.value();
572
 
    }
573
 
}
574
 
void ColorSchemeManager::loadAllColorSchemes()
575
 
{
576
 
    int success = 0;
577
 
    int failed = 0;
578
 
 
579
 
    QList<QString> nativeColorSchemes = listColorSchemes();
580
 
 
581
 
    QListIterator<QString> nativeIter(nativeColorSchemes);
582
 
    while ( nativeIter.hasNext() )
583
 
    {
584
 
        if ( loadColorScheme( nativeIter.next() ) )
585
 
            success++;
586
 
        else
587
 
            failed++;
588
 
    }
589
 
 
590
 
    QList<QString> kde3ColorSchemes = listKDE3ColorSchemes();
591
 
    QListIterator<QString> kde3Iter(kde3ColorSchemes);
592
 
    while ( kde3Iter.hasNext() )
593
 
    {
594
 
        if ( loadKDE3ColorScheme( kde3Iter.next() ) )
595
 
            success++;
596
 
        else
597
 
            failed++;
598
 
    }
599
 
 
600
 
    if ( failed > 0 )
601
 
        kWarning() << "failed to load " << failed << " color schemes.";
602
 
 
603
 
    _haveLoadedAll = true;
604
 
}
605
 
QList<const ColorScheme*> ColorSchemeManager::allColorSchemes()
606
 
{
607
 
    if ( !_haveLoadedAll )
608
 
    {
609
 
        loadAllColorSchemes();
610
 
    }
611
 
 
612
 
    return _colorSchemes.values();
613
 
}
614
 
bool ColorSchemeManager::loadKDE3ColorScheme(const QString& filePath)
615
 
{
616
 
    QFile file(filePath);
617
 
    if (!filePath.endsWith(QLatin1String(".schema")) || !file.open(QIODevice::ReadOnly))
618
 
        return false;
619
 
 
620
 
    KDE3ColorSchemeReader reader(&file);
621
 
    ColorScheme* scheme = reader.read();
622
 
    scheme->setName(QFileInfo(file).baseName());
623
 
    file.close();
624
 
 
625
 
    if (scheme->name().isEmpty())
626
 
    {
627
 
        kWarning() << "color scheme name is not valid.";
628
 
        delete scheme;
629
 
        return false;
630
 
    }
631
 
 
632
 
    QFileInfo info(filePath);
633
 
 
634
 
    if ( !_colorSchemes.contains(info.baseName()) )
635
 
        _colorSchemes.insert(scheme->name(),scheme);
636
 
    else
637
 
    {
638
 
        kWarning() << "color scheme with name" << scheme->name() << "has already been" <<
639
 
            "found, ignoring.";
640
 
        delete scheme;
641
 
    }
642
 
 
643
 
    return true;
644
 
}
645
 
void ColorSchemeManager::addColorScheme(ColorScheme* scheme) 
646
 
{
647
 
    _colorSchemes.insert(scheme->name(),scheme);
648
 
 
649
 
    // save changes to disk
650
 
    QString path = KGlobal::dirs()->saveLocation("data","konsole/") + scheme->name() + ".colorscheme";
651
 
    KConfig config(path , KConfig::NoGlobals);
652
 
 
653
 
    scheme->write(config);
654
 
}
655
 
bool ColorSchemeManager::loadColorScheme(const QString& filePath)
656
 
{
657
 
    if ( !filePath.endsWith(QLatin1String(".colorscheme")) || !QFile::exists(filePath) )
658
 
        return false;
659
 
 
660
 
    QFileInfo info(filePath);
661
 
 
662
 
    KConfig config(filePath , KConfig::NoGlobals);
663
 
    ColorScheme* scheme = new ColorScheme();
664
 
    scheme->setName(info.baseName());
665
 
    scheme->read(config);
666
 
 
667
 
    if (scheme->name().isEmpty()) 
668
 
    {
669
 
        kWarning() << "Color scheme in" << filePath << "does not have a valid name and was not loaded.";
670
 
        delete scheme;
671
 
        return false;
672
 
    }    
673
 
 
674
 
    if ( !_colorSchemes.contains(info.baseName()) )
675
 
    {
676
 
        _colorSchemes.insert(scheme->name(),scheme);
677
 
    }
678
 
    else
679
 
    {
680
 
        kDebug() << "color scheme with name" << scheme->name() << "has already been" <<
681
 
            "found, ignoring.";
682
 
 
683
 
        delete scheme;
684
 
    }
685
 
 
686
 
    return true; 
687
 
}
688
 
QList<QString> ColorSchemeManager::listKDE3ColorSchemes()
689
 
{
690
 
    return KGlobal::dirs()->findAllResources("data",
691
 
                                             "konsole/*.schema",
692
 
                                              KStandardDirs::NoDuplicates);
693
 
 
694
 
}
695
 
QList<QString> ColorSchemeManager::listColorSchemes()
696
 
{
697
 
    return KGlobal::dirs()->findAllResources("data",
698
 
                                             "konsole/*.colorscheme",
699
 
                                             KStandardDirs::NoDuplicates);
700
 
}
701
 
const ColorScheme ColorSchemeManager::_defaultColorScheme;
702
 
const ColorScheme* ColorSchemeManager::defaultColorScheme() const
703
 
{
704
 
    return &_defaultColorScheme;
705
 
}
706
 
bool ColorSchemeManager::deleteColorScheme(const QString& name)
707
 
{
708
 
    Q_ASSERT( _colorSchemes.contains(name) );
709
 
 
710
 
    // lookup the path and delete 
711
 
    QString path = findColorSchemePath(name);
712
 
    if ( QFile::remove(path) )
713
 
    {
714
 
        _colorSchemes.remove(name);
715
 
        return true;
716
 
    }
717
 
    else
718
 
    {
719
 
        kWarning() << "Failed to remove color scheme -" << path;
720
 
        return false;
721
 
    }
722
 
}
723
 
QString ColorSchemeManager::findColorSchemePath(const QString& name) const
724
 
{
725
 
    QString path = KStandardDirs::locate("data","konsole/"+name+".colorscheme");
726
 
 
727
 
    if ( !path.isEmpty() )
728
 
       return path; 
729
 
 
730
 
    path = KStandardDirs::locate("data","konsole/"+name+".schema");
731
 
 
732
 
    return path;
733
 
}
734
 
const ColorScheme* ColorSchemeManager::findColorScheme(const QString& name) 
735
 
{
736
 
    if ( name.isEmpty() )
737
 
        return defaultColorScheme();
738
 
 
739
 
    if ( _colorSchemes.contains(name) )
740
 
        return _colorSchemes[name];
741
 
    else
742
 
    {
743
 
        // look for this color scheme
744
 
        QString path = findColorSchemePath(name); 
745
 
        if ( !path.isEmpty() && loadColorScheme(path) )
746
 
        {
747
 
            return findColorScheme(name); 
748
 
        } 
749
 
        else 
750
 
        {
751
 
            if (!path.isEmpty() && loadKDE3ColorScheme(path))
752
 
                return findColorScheme(name);
753
 
        }
754
 
 
755
 
        kWarning() << "Could not find color scheme - " << name;
756
 
 
757
 
        return 0; 
758
 
    }
759
 
}
760
 
K_GLOBAL_STATIC( ColorSchemeManager , theColorSchemeManager )
761
 
ColorSchemeManager* ColorSchemeManager::instance()
762
 
{
763
 
    return theColorSchemeManager;
764
 
}
 
479
        setColorTableEntry(i , entry);
 
480
    }
 
481
}
 
482