| 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 |
// Own |
| 23 |
#include "ColorScheme.h" |
| 24 |
#include "tools.h" |
| 25 |
|
| 26 |
// Qt |
| 27 |
#include <QBrush> |
| 28 |
#include <QFile> |
| 29 |
#include <QFileInfo> |
| 30 |
#include <QtDebug> |
| 31 |
#include <QSettings> |
| 32 |
#include <QDir> |
| 33 |
#include <QRegularExpression> |
| 34 |
|
| 35 |
|
| 36 |
// KDE |
| 37 |
//#include <KColorScheme> |
| 38 |
//#include <KConfig> |
| 39 |
//#include <KLocale> |
| 40 |
//#include <KDebug> |
| 41 |
//#include <KConfigGroup> |
| 42 |
//#include <KStandardDirs> |
| 43 |
|
| 44 |
using namespace Konsole; |
| 45 |
|
| 46 |
const ColorEntry ColorScheme::defaultTable[TABLE_COLORS] = |
| 47 |
// The following are almost IBM standard color codes, with some slight |
| 48 |
// gamma correction for the dim colors to compensate for bright X screens. |
| 49 |
// It contains the 8 ansiterm/xterm colors in 2 intensities. |
| 50 |
{ |
| 51 |
ColorEntry( QColor(0x00,0x00,0x00), false), ColorEntry( |
| 52 |
QColor(0xFF,0xFF,0xFF), true), // Dfore, Dback |
| 53 |
ColorEntry( QColor(0x00,0x00,0x00), false), ColorEntry( |
| 54 |
QColor(0xB2,0x18,0x18), false), // Black, Red |
| 55 |
ColorEntry( QColor(0x18,0xB2,0x18), false), ColorEntry( |
| 56 |
QColor(0xB2,0x68,0x18), false), // Green, Yellow |
| 57 |
ColorEntry( QColor(0x18,0x18,0xB2), false), ColorEntry( |
| 58 |
QColor(0xB2,0x18,0xB2), false), // Blue, Magenta |
| 59 |
ColorEntry( QColor(0x18,0xB2,0xB2), false), ColorEntry( |
| 60 |
QColor(0xB2,0xB2,0xB2), false), // Cyan, White |
| 61 |
// intensive |
| 62 |
ColorEntry( QColor(0x00,0x00,0x00), false), ColorEntry( |
| 63 |
QColor(0xFF,0xFF,0xFF), true), |
| 64 |
ColorEntry( QColor(0x68,0x68,0x68), false), ColorEntry( |
| 65 |
QColor(0xFF,0x54,0x54), false), |
| 66 |
ColorEntry( QColor(0x54,0xFF,0x54), false), ColorEntry( |
| 67 |
QColor(0xFF,0xFF,0x54), false), |
| 68 |
ColorEntry( QColor(0x54,0x54,0xFF), false), ColorEntry( |
| 69 |
QColor(0xFF,0x54,0xFF), false), |
| 70 |
ColorEntry( QColor(0x54,0xFF,0xFF), false), ColorEntry( |
| 71 |
QColor(0xFF,0xFF,0xFF), false) |
| 72 |
}; |
| 73 |
|
| 74 |
const char* const ColorScheme::colorNames[TABLE_COLORS] = |
| 75 |
{ |
| 76 |
"Foreground", |
| 77 |
"Background", |
| 78 |
"Color0", |
| 79 |
"Color1", |
| 80 |
"Color2", |
| 81 |
"Color3", |
| 82 |
"Color4", |
| 83 |
"Color5", |
| 84 |
"Color6", |
| 85 |
"Color7", |
| 86 |
"ForegroundIntense", |
| 87 |
"BackgroundIntense", |
| 88 |
"Color0Intense", |
| 89 |
"Color1Intense", |
| 90 |
"Color2Intense", |
| 91 |
"Color3Intense", |
| 92 |
"Color4Intense", |
| 93 |
"Color5Intense", |
| 94 |
"Color6Intense", |
| 95 |
"Color7Intense" |
| 96 |
}; |
| 97 |
// dummy silently comment out the tr_NOOP |
| 98 |
#define tr_NOOP |
| 99 |
const char* const ColorScheme::translatedColorNames[TABLE_COLORS] = |
| 100 |
{ |
| 101 |
tr_NOOP("Foreground"), |
| 102 |
tr_NOOP("Background"), |
| 103 |
tr_NOOP("Color 1"), |
| 104 |
tr_NOOP("Color 2"), |
| 105 |
tr_NOOP("Color 3"), |
| 106 |
tr_NOOP("Color 4"), |
| 107 |
tr_NOOP("Color 5"), |
| 108 |
tr_NOOP("Color 6"), |
| 109 |
tr_NOOP("Color 7"), |
| 110 |
tr_NOOP("Color 8"), |
| 111 |
tr_NOOP("Foreground (Intense)"), |
| 112 |
tr_NOOP("Background (Intense)"), |
| 113 |
tr_NOOP("Color 1 (Intense)"), |
| 114 |
tr_NOOP("Color 2 (Intense)"), |
| 115 |
tr_NOOP("Color 3 (Intense)"), |
| 116 |
tr_NOOP("Color 4 (Intense)"), |
| 117 |
tr_NOOP("Color 5 (Intense)"), |
| 118 |
tr_NOOP("Color 6 (Intense)"), |
| 119 |
tr_NOOP("Color 7 (Intense)"), |
| 120 |
tr_NOOP("Color 8 (Intense)") |
| 121 |
}; |
| 122 |
|
| 123 |
ColorScheme::ColorScheme() |
| 124 |
{ |
| 125 |
_table = nullptr; |
| 126 |
_randomTable = nullptr; |
| 127 |
_opacity = 1.0; |
| 128 |
} |
| 129 |
ColorScheme::ColorScheme(const ColorScheme& other) |
| 130 |
: _opacity(other._opacity) |
| 131 |
,_table(nullptr) |
| 132 |
,_randomTable(nullptr) |
| 133 |
{ |
| 134 |
setName(other.name()); |
| 135 |
setDescription(other.description()); |
| 136 |
|
| 137 |
if ( other._table != nullptr ) |
| 138 |
{ |
| 139 |
for ( int i = 0 ; i < TABLE_COLORS ; i++ ) |
| 140 |
setColorTableEntry(i,other._table[i]); |
| 141 |
} |
| 142 |
|
| 143 |
if ( other._randomTable != nullptr ) |
| 144 |
{ |
| 145 |
for ( int i = 0 ; i < TABLE_COLORS ; i++ ) |
| 146 |
{ |
| 147 |
const RandomizationRange& range = other._randomTable[i]; |
| 148 |
setRandomizationRange(i,range.hue,range.saturation,range.value); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
ColorScheme::~ColorScheme() |
| 153 |
{ |
| 154 |
delete[] _table; |
| 155 |
delete[] _randomTable; |
| 156 |
} |
| 157 |
|
| 158 |
void ColorScheme::setDescription(const QString& description) { _description = description; } |
| 159 |
QString ColorScheme::description() const { return _description; } |
| 160 |
|
| 161 |
void ColorScheme::setName(const QString& name) { _name = name; } |
| 162 |
QString ColorScheme::name() const { return _name; } |
| 163 |
|
| 164 |
void ColorScheme::setColorTableEntry(int index , const ColorEntry& entry) |
| 165 |
{ |
| 166 |
Q_ASSERT( index >= 0 && index < TABLE_COLORS ); |
| 167 |
|
| 168 |
if ( !_table ) |
| 169 |
{ |
| 170 |
_table = new ColorEntry[TABLE_COLORS]; |
| 171 |
|
| 172 |
for (int i=0;i<TABLE_COLORS;i++) |
| 173 |
_table[i] = defaultTable[i]; |
| 174 |
} |
| 175 |
|
| 176 |
_table[index] = entry; |
| 177 |
} |
| 178 |
ColorEntry ColorScheme::colorEntry(int index , uint randomSeed) const |
| 179 |
{ |
| 180 |
Q_ASSERT( index >= 0 && index < TABLE_COLORS ); |
| 181 |
|
| 182 |
if ( randomSeed != 0 ) |
| 183 |
qsrand(randomSeed); |
| 184 |
|
| 185 |
ColorEntry entry = colorTable()[index]; |
| 186 |
|
| 187 |
if ( randomSeed != 0 && |
| 188 |
_randomTable != nullptr && |
| 189 |
!_randomTable[index].isNull() ) |
| 190 |
{ |
| 191 |
const RandomizationRange& range = _randomTable[index]; |
| 192 |
|
| 193 |
|
| 194 |
int hueDifference = range.hue ? (qrand() % range.hue) - range.hue/2 : 0; |
| 195 |
int saturationDifference = range.saturation ? (qrand() % range.saturation) - range.saturation/2 : 0; |
| 196 |
int valueDifference = range.value ? (qrand() % range.value) - range.value/2 : 0; |
| 197 |
|
| 198 |
QColor& color = entry.color; |
| 199 |
|
| 200 |
int newHue = qAbs( (color.hue() + hueDifference) % MAX_HUE ); |
| 201 |
int newValue = qMin( qAbs(color.value() + valueDifference) , 255 ); |
| 202 |
int newSaturation = qMin( qAbs(color.saturation() + saturationDifference) , 255 ); |
| 203 |
|
| 204 |
color.setHsv(newHue,newSaturation,newValue); |
| 205 |
} |
| 206 |
|
| 207 |
return entry; |
| 208 |
} |
| 209 |
void ColorScheme::getColorTable(ColorEntry* table , uint randomSeed) const |
| 210 |
{ |
| 211 |
for ( int i = 0 ; i < TABLE_COLORS ; i++ ) |
| 212 |
table[i] = colorEntry(i,randomSeed); |
| 213 |
} |
| 214 |
bool ColorScheme::randomizedBackgroundColor() const |
| 215 |
{ |
| 216 |
return _randomTable == nullptr ? false : !_randomTable[1].isNull(); |
| 217 |
} |
| 218 |
void ColorScheme::setRandomizedBackgroundColor(bool randomize) |
| 219 |
{ |
| 220 |
// the hue of the background colour is allowed to be randomly |
| 221 |
// adjusted as much as possible. |
| 222 |
// |
| 223 |
// the value and saturation are left alone to maintain read-ability |
| 224 |
if ( randomize ) |
| 225 |
{ |
| 226 |
setRandomizationRange( 1 /* background color index */ , MAX_HUE , 255 , 0 ); |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
if ( _randomTable ) |
| 231 |
setRandomizationRange( 1 /* background color index */ , 0 , 0 , 0 ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
void ColorScheme::setRandomizationRange( int index , quint16 hue , quint8 saturation , |
| 236 |
quint8 value ) |
| 237 |
{ |
| 238 |
Q_ASSERT( hue <= MAX_HUE ); |
| 239 |
Q_ASSERT( index >= 0 && index < TABLE_COLORS ); |
| 240 |
|
| 241 |
if ( _randomTable == nullptr ) |
| 242 |
_randomTable = new RandomizationRange[TABLE_COLORS]; |
| 243 |
|
| 244 |
_randomTable[index].hue = hue; |
| 245 |
_randomTable[index].value = value; |
| 246 |
_randomTable[index].saturation = saturation; |
| 247 |
} |
| 248 |
|
| 249 |
const ColorEntry* ColorScheme::colorTable() const |
| 250 |
{ |
| 251 |
if ( _table ) |
| 252 |
return _table; |
| 253 |
else |
| 254 |
return defaultTable; |
| 255 |
} |
| 256 |
QColor ColorScheme::foregroundColor() const |
| 257 |
{ |
| 258 |
return colorTable()[0].color; |
| 259 |
} |
| 260 |
QColor ColorScheme::backgroundColor() const |
| 261 |
{ |
| 262 |
return colorTable()[1].color; |
| 263 |
} |
| 264 |
bool ColorScheme::hasDarkBackground() const |
| 265 |
{ |
| 266 |
// value can range from 0 - 255, with larger values indicating higher brightness. |
| 267 |
// so 127 is in the middle, anything less is deemed 'dark' |
| 268 |
return backgroundColor().value() < 127; |
| 269 |
} |
| 270 |
void ColorScheme::setOpacity(qreal opacity) { _opacity = opacity; } |
| 271 |
qreal ColorScheme::opacity() const { return _opacity; } |
| 272 |
|
| 273 |
void ColorScheme::read(const QString & fileName) |
| 274 |
{ |
| 275 |
QSettings s(fileName, QSettings::IniFormat); |
| 276 |
s.beginGroup(QLatin1String("General")); |
| 277 |
|
| 278 |
_description = s.value(QLatin1String("Description"), QObject::tr("Un-named Color Scheme")).toString(); |
| 279 |
_opacity = s.value(QLatin1String("Opacity"),qreal(1.0)).toDouble(); |
| 280 |
s.endGroup(); |
| 281 |
|
| 282 |
for (int i=0 ; i < TABLE_COLORS ; i++) |
| 283 |
{ |
| 284 |
readColorEntry(&s, i); |
| 285 |
} |
| 286 |
} |
| 287 |
#if 0 |
| 288 |
// implemented upstream - user apps |
| 289 |
void ColorScheme::read(KConfig& config) |
| 290 |
{ |
| 291 |
KConfigGroup configGroup = config.group("General"); |
| 292 |
|
| 293 |
QString description = configGroup.readEntry("Description", QObject::tr("Un-named Color Scheme")); |
| 294 |
|
| 295 |
_description = tr(description.toUtf8()); |
| 296 |
_opacity = configGroup.readEntry("Opacity",qreal(1.0)); |
| 297 |
|
| 298 |
for (int i=0 ; i < TABLE_COLORS ; i++) |
| 299 |
{ |
| 300 |
readColorEntry(config,i); |
| 301 |
} |
| 302 |
} |
| 303 |
void ColorScheme::write(KConfig& config) const |
| 304 |
{ |
| 305 |
KConfigGroup configGroup = config.group("General"); |
| 306 |
|
| 307 |
configGroup.writeEntry("Description",_description); |
| 308 |
configGroup.writeEntry("Opacity",_opacity); |
| 309 |
|
| 310 |
for (int i=0 ; i < TABLE_COLORS ; i++) |
| 311 |
{ |
| 312 |
RandomizationRange random = _randomTable != 0 ? _randomTable[i] : RandomizationRange(); |
| 313 |
writeColorEntry(config,colorNameForIndex(i),colorTable()[i],random); |
| 314 |
} |
| 315 |
} |
| 316 |
#endif |
| 317 |
|
| 318 |
QString ColorScheme::colorNameForIndex(int index) |
| 319 |
{ |
| 320 |
Q_ASSERT( index >= 0 && index < TABLE_COLORS ); |
| 321 |
|
| 322 |
return QString::fromLatin1(colorNames[index]); |
| 323 |
} |
| 324 |
QString ColorScheme::translatedColorNameForIndex(int index) |
| 325 |
{ |
| 326 |
Q_ASSERT( index >= 0 && index < TABLE_COLORS ); |
| 327 |
|
| 328 |
return QString::fromLatin1(translatedColorNames[index]); |
| 329 |
} |
| 330 |
|
| 331 |
void ColorScheme::readColorEntry(QSettings * s , int index) |
| 332 |
{ |
| 333 |
QString colorName = colorNameForIndex(index); |
| 334 |
|
| 335 |
s->beginGroup(colorName); |
| 336 |
|
| 337 |
ColorEntry entry; |
| 338 |
|
| 339 |
QVariant colorValue = s->value(QLatin1String("Color")); |
| 340 |
QString colorStr; |
| 341 |
int r, g, b; |
| 342 |
bool ok = false; |
| 343 |
// XXX: Undocumented(?) QSettings behavior: values with commas are parsed |
| 344 |
// as QStringList and others QString |
| 345 |
if (colorValue.type() == QVariant::StringList) |
| 346 |
{ |
| 347 |
QStringList rgbList = colorValue.toStringList(); |
| 348 |
colorStr = rgbList.join(QLatin1Char(',')); |
| 349 |
if (rgbList.count() == 3) |
| 350 |
{ |
| 351 |
bool parse_ok; |
| 352 |
|
| 353 |
ok = true; |
| 354 |
r = rgbList[0].toInt(&parse_ok); |
| 355 |
ok = ok && parse_ok && (r >= 0 && r <= 0xff); |
| 356 |
g = rgbList[1].toInt(&parse_ok); |
| 357 |
ok = ok && parse_ok && (g >= 0 && g <= 0xff); |
| 358 |
b = rgbList[2].toInt(&parse_ok); |
| 359 |
ok = ok && parse_ok && (b >= 0 && b <= 0xff); |
| 360 |
} |
| 361 |
} |
| 362 |
else |
| 363 |
{ |
| 364 |
colorStr = colorValue.toString(); |
| 365 |
QRegularExpression hexColorPattern(QLatin1String("^#[0-9a-f]{6}$"), |
| 366 |
QRegularExpression::CaseInsensitiveOption); |
| 367 |
if (hexColorPattern.match(colorStr).hasMatch()) |
| 368 |
{ |
| 369 |
// Parsing is always ok as already matched by the regexp |
| 370 |
r = colorStr.midRef(1, 2).toInt(nullptr, 16); |
| 371 |
g = colorStr.midRef(3, 2).toInt(nullptr, 16); |
| 372 |
b = colorStr.midRef(5, 2).toInt(nullptr, 16); |
| 373 |
ok = true; |
| 374 |
} |
| 375 |
} |
| 376 |
if (!ok) |
| 377 |
{ |
| 378 |
qWarning().nospace() << "Invalid color value " << colorStr |
| 379 |
<< " for " << colorName << ". Fallback to black."; |
| 380 |
r = g = b = 0; |
| 381 |
} |
| 382 |
entry.color = QColor(r, g, b); |
| 383 |
|
| 384 |
entry.transparent = s->value(QLatin1String("Transparent"),false).toBool(); |
| 385 |
|
| 386 |
// Deprecated key from KDE 4.0 which set 'Bold' to true to force |
| 387 |
// a color to be bold or false to use the current format |
| 388 |
// |
| 389 |
// TODO - Add a new tri-state key which allows for bold, normal or |
| 390 |
// current format |
| 391 |
if (s->contains(QLatin1String("Bold"))) |
| 392 |
entry.fontWeight = s->value(QLatin1String("Bold"),false).toBool() ? ColorEntry::Bold : |
| 393 |
ColorEntry::UseCurrentFormat; |
| 394 |
|
| 395 |
quint16 hue = s->value(QLatin1String("MaxRandomHue"),0).toInt(); |
| 396 |
quint8 value = s->value(QLatin1String("MaxRandomValue"),0).toInt(); |
| 397 |
quint8 saturation = s->value(QLatin1String("MaxRandomSaturation"),0).toInt(); |
| 398 |
|
| 399 |
setColorTableEntry( index , entry ); |
| 400 |
|
| 401 |
if ( hue != 0 || value != 0 || saturation != 0 ) |
| 402 |
setRandomizationRange( index , hue , saturation , value ); |
| 403 |
|
| 404 |
s->endGroup(); |
| 405 |
} |
| 406 |
#if 0 |
| 407 |
// implemented upstream - user apps |
| 408 |
void ColorScheme::writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry , const RandomizationRange& random) const |
| 409 |
{ |
| 410 |
KConfigGroup configGroup(&config,colorName); |
| 411 |
|
| 412 |
configGroup.writeEntry("Color",entry.color); |
| 413 |
configGroup.writeEntry("Transparency",(bool)entry.transparent); |
| 414 |
if (entry.fontWeight != ColorEntry::UseCurrentFormat) |
| 415 |
{ |
| 416 |
configGroup.writeEntry("Bold",entry.fontWeight == ColorEntry::Bold); |
| 417 |
} |
| 418 |
|
| 419 |
// record randomization if this color has randomization or |
| 420 |
// if one of the keys already exists |
| 421 |
if ( !random.isNull() || configGroup.hasKey("MaxRandomHue") ) |
| 422 |
{ |
| 423 |
configGroup.writeEntry("MaxRandomHue",(int)random.hue); |
| 424 |
configGroup.writeEntry("MaxRandomValue",(int)random.value); |
| 425 |
configGroup.writeEntry("MaxRandomSaturation",(int)random.saturation); |
| 426 |
} |
| 427 |
} |
| 428 |
#endif |
| 429 |
|
| 430 |
// |
| 431 |
// Work In Progress - A color scheme for use on KDE setups for users |
| 432 |
// with visual disabilities which means that they may have trouble |
| 433 |
// reading text with the supplied color schemes. |
| 434 |
// |
| 435 |
// This color scheme uses only the 'safe' colors defined by the |
| 436 |
// KColorScheme class. |
| 437 |
// |
| 438 |
// A complication this introduces is that each color provided by |
| 439 |
// KColorScheme is defined as a 'background' or 'foreground' color. |
| 440 |
// Only foreground colors are allowed to be used to render text and |
| 441 |
// only background colors are allowed to be used for backgrounds. |
| 442 |
// |
| 443 |
// The ColorEntry and TerminalDisplay classes do not currently |
| 444 |
// support this restriction. |
| 445 |
// |
| 446 |
// Requirements: |
| 447 |
// - A color scheme which uses only colors from the KColorScheme class |
| 448 |
// - Ability to restrict which colors the TerminalDisplay widget |
| 449 |
// uses as foreground and background color |
| 450 |
// - Make use of KGlobalSettings::allowDefaultBackgroundImages() as |
| 451 |
// a hint to determine whether this accessible color scheme should |
| 452 |
// be used by default. |
| 453 |
// |
| 454 |
// |
| 455 |
// -- Robert Knight <[email protected]> 21/07/2007 |
| 456 |
// |
| 457 |
AccessibleColorScheme::AccessibleColorScheme() |
| 458 |
: ColorScheme() |
| 459 |
{ |
| 460 |
#if 0 |
| 461 |
// It's not finished in konsole and it breaks Qt4 compilation as well |
| 462 |
// basic attributes |
| 463 |
setName("accessible"); |
| 464 |
setDescription(QObject::tr("Accessible Color Scheme")); |
| 465 |
|
| 466 |
// setup colors |
| 467 |
const int ColorRoleCount = 8; |
| 468 |
|
| 469 |
const KColorScheme colorScheme(QPalette::Active); |
| 470 |
|
| 471 |
QBrush colors[ColorRoleCount] = |
| 472 |
{ |
| 473 |
colorScheme.foreground( colorScheme.NormalText ), |
| 474 |
colorScheme.background( colorScheme.NormalBackground ), |
| 475 |
|
| 476 |
colorScheme.foreground( colorScheme.InactiveText ), |
| 477 |
colorScheme.foreground( colorScheme.ActiveText ), |
| 478 |
colorScheme.foreground( colorScheme.LinkText ), |
| 479 |
colorScheme.foreground( colorScheme.VisitedText ), |
| 480 |
colorScheme.foreground( colorScheme.NegativeText ), |
| 481 |
colorScheme.foreground( colorScheme.NeutralText ) |
| 482 |
}; |
| 483 |
|
| 484 |
for ( int i = 0 ; i < TABLE_COLORS ; i++ ) |
| 485 |
{ |
| 486 |
ColorEntry entry; |
| 487 |
entry.color = colors[ i % ColorRoleCount ].color(); |
| 488 |
|
| 489 |
setColorTableEntry( i , entry ); |
| 490 |
} |
| 491 |
#endif |
| 492 |
} |
| 493 |
|
| 494 |
ColorSchemeManager::ColorSchemeManager() |
| 495 |
: _haveLoadedAll(false) |
| 496 |
{ |
| 497 |
} |
| 498 |
ColorSchemeManager::~ColorSchemeManager() |
| 499 |
{ |
| 500 |
QHashIterator<QString,const ColorScheme*> iter(_colorSchemes); |
| 501 |
while (iter.hasNext()) |
| 502 |
{ |
| 503 |
iter.next(); |
| 504 |
delete iter.value(); |
| 505 |
} |
| 506 |
} |
| 507 |
void ColorSchemeManager::loadAllColorSchemes() |
| 508 |
{ |
| 509 |
//qDebug() << "loadAllColorSchemes"; |
| 510 |
int failed = 0; |
| 511 |
|
| 512 |
QList<QString> nativeColorSchemes = listColorSchemes(); |
| 513 |
QListIterator<QString> nativeIter(nativeColorSchemes); |
| 514 |
while ( nativeIter.hasNext() ) |
| 515 |
{ |
| 516 |
if ( !loadColorScheme( nativeIter.next() ) ) |
| 517 |
failed++; |
| 518 |
} |
| 519 |
|
| 520 |
/*if ( failed > 0 ) |
| 521 |
qDebug() << "failed to load " << failed << " color schemes.";*/ |
| 522 |
|
| 523 |
_haveLoadedAll = true; |
| 524 |
} |
| 525 |
QList<const ColorScheme*> ColorSchemeManager::allColorSchemes() |
| 526 |
{ |
| 527 |
if ( !_haveLoadedAll ) |
| 528 |
{ |
| 529 |
loadAllColorSchemes(); |
| 530 |
} |
| 531 |
|
| 532 |
return _colorSchemes.values(); |
| 533 |
} |
| 534 |
#if 0 |
| 535 |
void ColorSchemeManager::addColorScheme(ColorScheme* scheme) |
| 536 |
{ |
| 537 |
_colorSchemes.insert(scheme->name(),scheme); |
| 538 |
|
| 539 |
// save changes to disk |
| 540 |
QString path = KGlobal::dirs()->saveLocation("data","konsole/") + scheme->name() + ".colorscheme"; |
| 541 |
KConfig config(path , KConfig::NoGlobals); |
| 542 |
|
| 543 |
scheme->write(config); |
| 544 |
} |
| 545 |
#endif |
| 546 |
|
| 547 |
bool ColorSchemeManager::loadCustomColorScheme(const QString& path) |
| 548 |
{ |
| 549 |
if (path.endsWith(QLatin1String(".colorscheme"))) |
| 550 |
return loadColorScheme(path); |
| 551 |
|
| 552 |
return false; |
| 553 |
} |
| 554 |
|
| 555 |
void ColorSchemeManager::addCustomColorSchemeDir(const QString& custom_dir) |
| 556 |
{ |
| 557 |
add_custom_color_scheme_dir(custom_dir); |
| 558 |
} |
| 559 |
|
| 560 |
bool ColorSchemeManager::loadColorScheme(const QString& filePath) |
| 561 |
{ |
| 562 |
if ( !filePath.endsWith(QLatin1String(".colorscheme")) || !QFile::exists(filePath) ) |
| 563 |
return false; |
| 564 |
|
| 565 |
QFileInfo info(filePath); |
| 566 |
|
| 567 |
const QString& schemeName = info.baseName(); |
| 568 |
|
| 569 |
ColorScheme* scheme = new ColorScheme(); |
| 570 |
scheme->setName(schemeName); |
| 571 |
scheme->read(filePath); |
| 572 |
|
| 573 |
if (scheme->name().isEmpty()) |
| 574 |
{ |
| 575 |
//qDebug() << "Color scheme in" << filePath << "does not have a valid name and was not loaded."; |
| 576 |
delete scheme; |
| 577 |
return false; |
| 578 |
} |
| 579 |
|
| 580 |
if ( !_colorSchemes.contains(schemeName) ) |
| 581 |
{ |
| 582 |
_colorSchemes.insert(schemeName,scheme); |
| 583 |
} |
| 584 |
else |
| 585 |
{ |
| 586 |
/*qDebug() << "color scheme with name" << schemeName << "has already been" << |
| 587 |
"found, ignoring.";*/ |
| 588 |
|
| 589 |
delete scheme; |
| 590 |
} |
| 591 |
|
| 592 |
return true; |
| 593 |
} |
| 594 |
QList<QString> ColorSchemeManager::listColorSchemes() |
| 595 |
{ |
| 596 |
QList<QString> ret; |
| 597 |
for (const QString &scheme_dir : get_color_schemes_dirs()) |
| 598 |
{ |
| 599 |
const QString dname(scheme_dir); |
| 600 |
QDir dir(dname); |
| 601 |
QStringList filters; |
| 602 |
filters << QLatin1String("*.colorscheme"); |
| 603 |
dir.setNameFilters(filters); |
| 604 |
const QStringList list = dir.entryList(filters); |
| 605 |
for (const QString &i : list) |
| 606 |
ret << dname + QLatin1Char('/') + i; |
| 607 |
} |
| 608 |
return ret; |
| 609 |
// return KGlobal::dirs()->findAllResources("data", |
| 610 |
// "konsole/*.colorscheme", |
| 611 |
// KStandardDirs::NoDuplicates); |
| 612 |
} |
| 613 |
const ColorScheme ColorSchemeManager::_defaultColorScheme; |
| 614 |
const ColorScheme* ColorSchemeManager::defaultColorScheme() const |
| 615 |
{ |
| 616 |
return &_defaultColorScheme; |
| 617 |
} |
| 618 |
bool ColorSchemeManager::deleteColorScheme(const QString& name) |
| 619 |
{ |
| 620 |
Q_ASSERT( _colorSchemes.contains(name) ); |
| 621 |
|
| 622 |
// lookup the path and delete |
| 623 |
QString path = findColorSchemePath(name); |
| 624 |
if ( QFile::remove(path) ) |
| 625 |
{ |
| 626 |
_colorSchemes.remove(name); |
| 627 |
return true; |
| 628 |
} |
| 629 |
else |
| 630 |
{ |
| 631 |
//qDebug() << "Failed to remove color scheme -" << path; |
| 632 |
return false; |
| 633 |
} |
| 634 |
} |
| 635 |
QString ColorSchemeManager::findColorSchemePath(const QString& name) const |
| 636 |
{ |
| 637 |
// QString path = KStandardDirs::locate("data","konsole/"+name+".colorscheme"); |
| 638 |
const QStringList dirs = get_color_schemes_dirs(); |
| 639 |
if ( dirs.isEmpty() ) |
| 640 |
return QString(); |
| 641 |
|
| 642 |
const QString dir = dirs.first(); |
| 643 |
QString path(dir + QLatin1Char('/')+ name + QLatin1String(".colorscheme")); |
| 644 |
if ( !path.isEmpty() ) |
| 645 |
return path; |
| 646 |
|
| 647 |
//path = KStandardDirs::locate("data","konsole/"+name+".schema"); |
| 648 |
path = dir + QLatin1Char('/')+ name + QLatin1String(".schema"); |
| 649 |
|
| 650 |
return path; |
| 651 |
} |
| 652 |
const ColorScheme* ColorSchemeManager::findColorScheme(const QString& name) |
| 653 |
{ |
| 654 |
if ( name.isEmpty() ) |
| 655 |
return defaultColorScheme(); |
| 656 |
|
| 657 |
if ( _colorSchemes.contains(name) ) |
| 658 |
return _colorSchemes[name]; |
| 659 |
else |
| 660 |
{ |
| 661 |
// look for this color scheme |
| 662 |
QString path = findColorSchemePath(name); |
| 663 |
if ( !path.isEmpty() && loadColorScheme(path) ) |
| 664 |
{ |
| 665 |
return findColorScheme(name); |
| 666 |
} |
| 667 |
|
| 668 |
//qDebug() << "Could not find color scheme - " << name; |
| 669 |
|
| 670 |
return nullptr; |
| 671 |
} |
| 672 |
} |
| 673 |
Q_GLOBAL_STATIC(ColorSchemeManager, theColorSchemeManager) |
| 674 |
ColorSchemeManager* ColorSchemeManager::instance() |
| 675 |
{ |
| 676 |
return theColorSchemeManager; |
| 677 |
} |