~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to kcontrol/colors/colorscm.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// KDE Display color scheme setup module
 
2
//
 
3
// Copyright (c)  Mark Donohoe 1997
 
4
//
 
5
// Converted to a kcc module by Matthias Hoelzer 1997
 
6
// Ported to Qt-2.0 by Matthias Ettrich 1999
 
7
// Ported to kcontrol2 by Geert Jansen 1999
 
8
// Made maintainable by Waldo Bastian 2000
 
9
 
 
10
#include <assert.h>
 
11
#include <config-workspace.h>
 
12
#include <stdlib.h>
 
13
#include <unistd.h>
 
14
 
 
15
#include <QCheckBox>
 
16
#include <QComboBox>
 
17
#include <QDir>
 
18
#include <QLabel>
 
19
#include <QLayout>
 
20
#include <QPainter>
 
21
#include <QSlider>
 
22
#include <QSplitter>
 
23
#include <Qt3Support/Q3GroupBox>
 
24
#include <Q3PtrList>
 
25
 
 
26
#include <kcolorbutton.h>
 
27
#include <kcursor.h>
 
28
#include <kfiledialog.h>
 
29
#include <kgenericfactory.h>
 
30
#include <kglobalsettings.h>
 
31
#include <kinputdialog.h>
 
32
#include <kio/netaccess.h>
 
33
#include <kmessagebox.h>
 
34
#include <kstandarddirs.h>
 
35
#include <kaboutdata.h>
 
36
#include <klistwidget.h>
 
37
#include <kvbox.h>
 
38
#include "../krdb/krdb.h"
 
39
 
 
40
#include "colorscm.h"
 
41
#include "kcolortreewidget.h"
 
42
 
 
43
#if defined Q_WS_X11
 
44
#include <QX11Info>
 
45
#include <X11/Xlib.h>
 
46
#include <X11/Xatom.h>
 
47
#endif
 
48
 
 
49
K_PLUGIN_FACTORY(KolorFactory,
 
50
        registerPlugin<KColorScheme>();
 
51
        )
 
52
K_EXPORT_PLUGIN(KolorFactory("kcmcolors"))
 
53
 
 
54
class KColorSchemeEntry {
 
55
public:
 
56
    KColorSchemeEntry(const QString &_path, const QString &_name, bool _local)
 
57
        : path(_path), name(_name), local(_local) { }
 
58
 
 
59
    QString path;
 
60
    QString name;
 
61
    bool local;
 
62
};
 
63
 
 
64
class KColorSchemeList : public Q3PtrList<KColorSchemeEntry> {
 
65
public:
 
66
    KColorSchemeList()
 
67
        { setAutoDelete(true); }
 
68
 
 
69
    int compareItems(Q3PtrCollection::Item item1, Q3PtrCollection::Item item2)
 
70
        {
 
71
           KColorSchemeEntry *i1 = (KColorSchemeEntry*)item1;
 
72
           KColorSchemeEntry *i2 = (KColorSchemeEntry*)item2;
 
73
           if (i1->local != i2->local)
 
74
              return i1->local ? -1 : 1;
 
75
           return i1->name.localeAwareCompare(i2->name);
 
76
        }
 
77
};
 
78
 
 
79
#define SIZE 8
 
80
 
 
81
// make a 24 * 8 pixmap with the main colors in a scheme
 
82
QPixmap mkColorPreview(const WidgetCanvas *cs)
 
83
{
 
84
   QPixmap group(SIZE*3,SIZE);
 
85
   QPixmap block(SIZE,SIZE);
 
86
   group.fill(QColor(0,0,0));
 
87
   block.fill(cs->back);   bitBlt(&group,0*SIZE,0,&block,0,0,SIZE,SIZE);
 
88
   block.fill(cs->window); bitBlt(&group,1*SIZE,0,&block,0,0,SIZE,SIZE);
 
89
   block.fill(cs->aTitle); bitBlt(&group,2*SIZE,0,&block,0,0,SIZE,SIZE);
 
90
   QPainter p(&group);
 
91
   p.drawRect(0,0,3*SIZE,SIZE);
 
92
   return group;
 
93
}
 
94
 
 
95
/**** KColorScheme ****/
 
96
 
 
97
KColorScheme::KColorScheme(QWidget *parent, const QVariantList &)
 
98
    : KCModule(KolorFactory::componentData(), parent)
 
99
{
 
100
    nSysSchemes = 2;
 
101
 
 
102
    setQuickHelp( i18n("<p><h1>Colors</h1> This module allows you to choose"
 
103
       " the color scheme used for the KDE desktop. The different"
 
104
       " elements of the desktop, such as title bars, menu text, etc.,"
 
105
       " are called \"widgets\". You can choose the widget whose"
 
106
       " color you want to change by selecting it from a list, or by"
 
107
       " clicking on a graphical representation of the desktop.</p><p>"
 
108
       " You can save color settings as complete color schemes,"
 
109
       " which can also be modified or deleted. KDE comes with several"
 
110
       " predefined color schemes on which you can base your own.</p><p>"
 
111
       " All KDE applications will obey the selected color scheme."
 
112
       " Non-KDE applications may also obey some or all of the color"
 
113
       " settings, if this option is enabled.</p>"));
 
114
 
 
115
    KConfigGroup cfg(KSharedConfig::openConfig("kcmdisplayrc"), "X11");
 
116
    useRM = cfg.readEntry("useResourceManager", true);
 
117
 
 
118
    cs = new WidgetCanvas( this );
 
119
    cs->setCursor( Qt::PointingHandCursor );
 
120
 
 
121
 
 
122
    cs->setFixedHeight(160);
 
123
    cs->setMinimumWidth(440);
 
124
    cs->setWhatsThis( i18n("This is a preview of the color settings which"
 
125
       " will be applied if you click \"Apply\" or \"OK\". You can click on"
 
126
       " different parts of this preview image. The widget name in the"
 
127
       " \"Widget color\" box will change to reflect the part of the preview"
 
128
       " image you clicked.") );
 
129
 
 
130
    connect( cs, SIGNAL( colorDropped( int, const QColor&)),
 
131
         SLOT( slotColorForWidget( int, const QColor&)));
 
132
 
 
133
    QSplitter *splitter = new QSplitter(this);
 
134
 
 
135
    Q3GroupBox *group = new Q3GroupBox( i18n("Color Scheme"), splitter );
 
136
    group->setOrientation( Qt::Horizontal );
 
137
    group->setColumns( 1 );
 
138
 
 
139
 
 
140
    sList = new KListWidget( group );
 
141
    mSchemeList = new KColorSchemeList();
 
142
    connect(sList, SIGNAL(currentRowChanged(int)), SLOT(slotPreviewScheme(int)));
 
143
 
 
144
    sList->setWhatsThis( i18n("<p>This is a list of predefined color schemes,"
 
145
       " including any that you may have created. You can preview an existing"
 
146
       " color scheme by selecting it from the list. The current scheme will"
 
147
       " be replaced by the selected color scheme.</p><p>"
 
148
       " Warning: if you have not yet applied any changes you may have made"
 
149
       " to the current scheme, those changes will be lost if you select"
 
150
       " another color scheme.</p>") );
 
151
 
 
152
    addBt = new QPushButton(i18n("&Save Scheme..."), group);
 
153
    connect(addBt, SIGNAL(clicked()), SLOT(slotAdd()));
 
154
 
 
155
    addBt->setWhatsThis( i18n("Press this button if you want to save"
 
156
       " the current color settings as a color scheme. You will be"
 
157
       " prompted for a name.") );
 
158
 
 
159
    removeBt = new QPushButton(i18n("R&emove Scheme"), group);
 
160
    removeBt->setEnabled(false);
 
161
    connect(removeBt, SIGNAL(clicked()), SLOT(slotRemove()));
 
162
 
 
163
    removeBt->setWhatsThis( i18n("Press this button to remove the selected"
 
164
       " color scheme. Note that this button is disabled if you do not have"
 
165
       " permission to delete the color scheme.") );
 
166
 
 
167
        importBt = new QPushButton(i18n("I&mport Scheme..."), group);
 
168
        connect(importBt, SIGNAL(clicked()),SLOT(slotImport()));
 
169
 
 
170
        importBt->setWhatsThis( i18n("Press this button to import a new color"
 
171
                " scheme. Note that the color scheme will only be available for the"
 
172
                " current user." ));
 
173
 
 
174
 
 
175
 
 
176
    KVBox *vb=new KVBox(splitter);
 
177
 
 
178
        // LAYOUT
 
179
    splitter->addWidget(group);
 
180
    splitter->addWidget(vb);
 
181
    QVBoxLayout *toplayout = new QVBoxLayout(this);
 
182
    toplayout->setMargin(0);
 
183
 
 
184
    toplayout->addWidget(cs);
 
185
    toplayout->addWidget(splitter);
 
186
    setLayout(toplayout);
 
187
 
 
188
    mColorTreeWidget = new KColorTreeWidget(vb);
 
189
    connect(mColorTreeWidget , SIGNAL(colorChanged(int, const QColor& )) ,
 
190
            this , SLOT(slotColorChanged( int, const QColor& ) ));
 
191
 
 
192
    setColorName(i18n("Inactive Title Bar") , CSM_Inactive_title_text , CSM_Inactive_title_bar);
 
193
    setColorName(i18n("Inactive Title Blend"), -1 , CSM_Inactive_title_blend);
 
194
    setColorName(i18n("Active Title Bar"), CSM_Active_title_text, CSM_Active_title_bar);
 
195
    setColorName(i18n("Active Title Blend"), -1, CSM_Active_title_blend);
 
196
    setColorName(i18n("Window"), CSM_Text , CSM_Background);
 
197
    setColorName(i18n("Selected"), CSM_Select_text , CSM_Select_background);
 
198
    setColorName(i18n("Standard"), CSM_Standard_text,  CSM_Standard_background);
 
199
    setColorName(i18n("Button"), CSM_Button_text , CSM_Button_background);
 
200
    setColorName(i18n("Active Title Button"), CSM_Active_title_button , -1 );
 
201
    setColorName(i18n("Inactive Title Button"), CSM_Inactive_title_button , -1);
 
202
    setColorName(i18n("Active Window Frame"), CSM_Active_frame , -1);
 
203
    setColorName(i18n("Active Window Handle"), CSM_Active_handle , -1);
 
204
    setColorName(i18n("Inactive Window Frame"), CSM_Inactive_frame , -1);
 
205
    setColorName(i18n("Inactive Window Handle"), CSM_Inactive_handle , -1);
 
206
    setColorName(i18n("Link"), CSM_Link , -1);
 
207
    setColorName(i18n("Followed Link"), CSM_Followed_Link , -1 );
 
208
    setColorName(i18n("Alternate Background in Lists"), -1 , CSM_Alternate_background);
 
209
 
 
210
 
 
211
    cbShadeList = new QCheckBox(i18n("Shade sorted column in lists"), vb);
 
212
    connect(cbShadeList, SIGNAL(toggled(bool)), this, SLOT(slotShadeSortColumnChanged(bool)));
 
213
 
 
214
    cbShadeList->setWhatsThis(
 
215
       i18n("Check this box to show the sorted column in a list with a shaded background"));
 
216
 
 
217
    group = new Q3GroupBox(  i18n("Con&trast"), vb );
 
218
 
 
219
    QVBoxLayout *groupLayout2 = new QVBoxLayout(group);
 
220
    QHBoxLayout *groupLayout = new QHBoxLayout;
 
221
    groupLayout2->addLayout(groupLayout);
 
222
 
 
223
    sb = new QSlider(Qt::Horizontal, group);
 
224
    sb->setObjectName(QLatin1String("Slider"));
 
225
    sb->setRange( 0, 10 );
 
226
    sb->setFocusPolicy( Qt::StrongFocus );
 
227
    connect(sb, SIGNAL(valueChanged(int)), SLOT(sliderValueChanged(int)));
 
228
 
 
229
    sb->setWhatsThis( i18n("Use this slider to change the contrast level"
 
230
       " of the current color scheme. Contrast does not affect all of the"
 
231
       " colors, only the edges of 3D objects."));
 
232
 
 
233
    QLabel *label = new QLabel(i18nc("Low Contrast", "Low"), group);
 
234
    label->setBuddy( sb );
 
235
    groupLayout->addWidget(label);
 
236
    groupLayout->addWidget(sb, 10);
 
237
    label = new QLabel(group);
 
238
    label->setText(i18nc("High Contrast", "High"));
 
239
    groupLayout->addWidget( label );
 
240
 
 
241
    cbExportColors = new QCheckBox(i18n("Apply colors to &non-KDE applications"), this);
 
242
    toplayout->addWidget( cbExportColors );
 
243
    connect(cbExportColors, SIGNAL(toggled(bool)), this, SLOT(changed()));
 
244
 
 
245
    cbExportColors->setWhatsThis( i18n("Check this box to apply the"
 
246
       " current color scheme to non-KDE applications."));
 
247
 
 
248
    readSchemeNames();
 
249
    sList->setCurrentItem( 0 );
 
250
 
 
251
    load();
 
252
 
 
253
    KAboutData* about = new KAboutData("kcmcolors", 0, ki18n("Colors"), 0, KLocalizedString(),
 
254
        KAboutData::License_GPL,
 
255
        ki18n("(c) 1997-2005 Colors Developers"));
 
256
    about->addAuthor(ki18n("Mark Donohoe"));
 
257
    about->addAuthor(ki18n("Matthias Hoelzer"));
 
258
    about->addAuthor(ki18n("Matthias Ettrich"));
 
259
    about->addAuthor(ki18n("Geert Jansen"));
 
260
    about->addAuthor(ki18n("Waldo Bastian"));
 
261
    setAboutData( about );
 
262
}
 
263
 
 
264
 
 
265
KColorScheme::~KColorScheme()
 
266
{
 
267
    delete mSchemeList;
 
268
}
 
269
 
 
270
 
 
271
void KColorScheme::load()
 
272
{
 
273
    KConfigGroup config(KGlobal::config(), "KDE");
 
274
    sCurrentScheme = config.readEntry("colorScheme");
 
275
 
 
276
    sList->setCurrentRow(findSchemeByName(sCurrentScheme));
 
277
    readScheme(0);
 
278
 
 
279
    cbShadeList->setChecked(cs->shadeSortColumn);
 
280
 
 
281
    cs->drawSampleWidgets();
 
282
    sb->blockSignals(true);
 
283
    sb->setValue(cs->contrast);
 
284
    sb->blockSignals(false);
 
285
 
 
286
    KConfig _cfg( "kcmdisplayrc", KConfig::NoGlobals  );
 
287
    KConfigGroup cfg(&_cfg, "X11");
 
288
    bool exportColors = cfg.readEntry("exportKDEColors", true);
 
289
    cbExportColors->setChecked(exportColors);
 
290
 
 
291
    emit changed(false);
 
292
}
 
293
 
 
294
 
 
295
void KColorScheme::save()
 
296
{
 
297
    KConfigGroup cfg(KGlobal::config(), "General");
 
298
    cfg.writeEntry("background", cs->back, KConfigBase::Normal|KConfigBase::Global);
 
299
    cfg.writeEntry("selectBackground", cs->select, KConfigBase::Normal|KConfigBase::Global);
 
300
    cfg.writeEntry("foreground", cs->txt, KConfigBase::Normal|KConfigBase::Global);
 
301
    cfg.writeEntry("windowForeground", cs->windowTxt, KConfigBase::Normal|KConfigBase::Global);
 
302
    cfg.writeEntry("windowBackground", cs->window, KConfigBase::Normal|KConfigBase::Global);
 
303
    cfg.writeEntry("selectForeground", cs->selectTxt, KConfigBase::Normal|KConfigBase::Global);
 
304
    cfg.writeEntry("buttonBackground", cs->button, KConfigBase::Normal|KConfigBase::Global);
 
305
    cfg.writeEntry("buttonForeground", cs->buttonTxt, KConfigBase::Normal|KConfigBase::Global);
 
306
    cfg.writeEntry("linkColor", cs->link, KConfigBase::Normal|KConfigBase::Global);
 
307
    cfg.writeEntry("visitedLinkColor", cs->visitedLink, KConfigBase::Normal|KConfigBase::Global);
 
308
    cfg.writeEntry("alternateBackground", cs->alternateBackground, KConfigBase::Normal|KConfigBase::Global);
 
309
 
 
310
    cfg.writeEntry("shadeSortColumn", cs->shadeSortColumn, KConfigBase::Normal|KConfigBase::Global);
 
311
 
 
312
    cfg.changeGroup( "WM" );
 
313
    cfg.writeEntry("activeForeground", cs->aTxt, KConfigBase::Normal|KConfigBase::Global);
 
314
    cfg.writeEntry("inactiveBackground", cs->iaTitle, KConfigBase::Normal|KConfigBase::Global);
 
315
    cfg.writeEntry("inactiveBlend", cs->iaBlend, KConfigBase::Normal|KConfigBase::Global);
 
316
    cfg.writeEntry("activeBackground", cs->aTitle, KConfigBase::Normal|KConfigBase::Global);
 
317
    cfg.writeEntry("activeBlend", cs->aBlend, KConfigBase::Normal|KConfigBase::Global);
 
318
    cfg.writeEntry("inactiveForeground", cs->iaTxt, KConfigBase::Normal|KConfigBase::Global);
 
319
    cfg.writeEntry("activeTitleBtnBg", cs->aTitleBtn, KConfigBase::Normal|KConfigBase::Global);
 
320
    cfg.writeEntry("inactiveTitleBtnBg", cs->iTitleBtn, KConfigBase::Normal|KConfigBase::Global);
 
321
    cfg.writeEntry("frame", cs->aFrame, KConfigBase::Normal|KConfigBase::Global);
 
322
    cfg.writeEntry("inactiveFrame", cs->iaFrame, KConfigBase::Normal|KConfigBase::Global);
 
323
    cfg.writeEntry("handle", cs->aHandle, KConfigBase::Normal|KConfigBase::Global);
 
324
    cfg.writeEntry("inactiveHandle", cs->iaHandle, KConfigBase::Normal|KConfigBase::Global);
 
325
 
 
326
    cfg.changeGroup( "KDE" );
 
327
    cfg.writeEntry("contrast", cs->contrast, KConfigBase::Normal|KConfigBase::Global);
 
328
    cfg.writeEntry("colorScheme", sCurrentScheme, KConfigBase::Normal|KConfigBase::Global);
 
329
    cfg.sync();
 
330
 
 
331
    // KDE-1.x support
 
332
    KConfigGroup config(KSharedConfig::openConfig( QDir::homePath() + "/.kderc" ), "General" );
 
333
    config.writeEntry("background", cs->back );
 
334
    config.writeEntry("selectBackground", cs->select );
 
335
    config.writeEntry("foreground", cs->txt, KConfigBase::Normal|KConfigBase::Global);
 
336
    config.writeEntry("windowForeground", cs->windowTxt );
 
337
    config.writeEntry("windowBackground", cs->window );
 
338
    config.writeEntry("selectForeground", cs->selectTxt );
 
339
    config.sync();
 
340
 
 
341
    KConfig _cfg2("kcmdisplayrc", KConfig::NoGlobals);
 
342
    KConfigGroup cfg2(&_cfg2, "X11");
 
343
    bool exportColors = cbExportColors->isChecked();
 
344
    cfg2.writeEntry("exportKDEColors", exportColors);
 
345
    cfg2.sync();
 
346
    QApplication::syncX();
 
347
 
 
348
    // Notify all qt-only apps of the KDE palette changes
 
349
    uint flags = KRdbExportQtColors;
 
350
    if ( exportColors )
 
351
        flags |= KRdbExportColors;
 
352
    else
 
353
    {
 
354
#if defined Q_WS_X11
 
355
        // Undo the property xrdb has placed on the root window (if any),
 
356
        // i.e. remove all entries, including ours
 
357
        XDeleteProperty( QX11Info::display(), QX11Info::appRootWindow(), XA_RESOURCE_MANAGER );
 
358
#endif
 
359
    }
 
360
    runRdb( flags );    // Save the palette to qtrc for KStyles
 
361
 
 
362
    // Notify all KDE applications
 
363
    KGlobalSettings::self()->emitChange(KGlobalSettings::PaletteChanged);
 
364
 
 
365
    // Update the "Current Scheme"
 
366
    int current = findSchemeByName(sCurrentScheme);
 
367
    sList->setCurrentItem(0);
 
368
    readScheme(0);
 
369
    QPixmap preview = mkColorPreview(cs);
 
370
    sList->item(0)->setIcon(preview);
 
371
    sList->setCurrentRow(current);
 
372
    readScheme(current);
 
373
    preview = mkColorPreview(cs);
 
374
    sList->item(current)->setIcon(preview);
 
375
 
 
376
    emit changed(false);
 
377
}
 
378
 
 
379
 
 
380
void KColorScheme::defaults()
 
381
{
 
382
    readScheme(1);
 
383
    sList->setCurrentRow(1);
 
384
 
 
385
    cbShadeList->setChecked(cs->shadeSortColumn);
 
386
 
 
387
    cs->drawSampleWidgets();
 
388
    sb->blockSignals(true);
 
389
    sb->setValue(cs->contrast);
 
390
    sb->blockSignals(false);
 
391
 
 
392
    cbExportColors->setChecked(true);
 
393
 
 
394
    emit changed(true);
 
395
}
 
396
 
 
397
void KColorScheme::sliderValueChanged( int val )
 
398
{
 
399
    cs->contrast = val;
 
400
    cs->drawSampleWidgets();
 
401
 
 
402
    sCurrentScheme.clear();
 
403
 
 
404
    emit changed(true);
 
405
}
 
406
 
 
407
 
 
408
void KColorScheme::slotSave( )
 
409
{
 
410
    KColorSchemeEntry *entry = mSchemeList->at(sList->currentRow()-nSysSchemes);
 
411
    if (!entry) return;
 
412
    sCurrentScheme = entry->path;
 
413
    KConfig *config = new KConfig(sCurrentScheme, KConfig::OnlyLocal);
 
414
    int i = sCurrentScheme.lastIndexOf('/');
 
415
    if (i >= 0)
 
416
      sCurrentScheme = sCurrentScheme.mid(i+1);
 
417
 
 
418
    KConfigGroup group = config->group("Color Scheme" );
 
419
    group.writeEntry("background", cs->back );
 
420
    group.writeEntry("selectBackground", cs->select );
 
421
    group.writeEntry("foreground", cs->txt );
 
422
    group.writeEntry("activeForeground", cs->aTxt );
 
423
    group.writeEntry("inactiveBackground", cs->iaTitle );
 
424
    group.writeEntry("inactiveBlend", cs->iaBlend );
 
425
    group.writeEntry("activeBackground", cs->aTitle );
 
426
    group.writeEntry("activeBlend", cs->aBlend );
 
427
    group.writeEntry("inactiveForeground", cs->iaTxt );
 
428
    group.writeEntry("windowForeground", cs->windowTxt );
 
429
    group.writeEntry("windowBackground", cs->window );
 
430
    group.writeEntry("selectForeground", cs->selectTxt );
 
431
    group.writeEntry("contrast", cs->contrast );
 
432
    group.writeEntry("buttonForeground", cs->buttonTxt );
 
433
    group.writeEntry("buttonBackground", cs->button );
 
434
    group.writeEntry("activeTitleBtnBg", cs->aTitleBtn);
 
435
    group.writeEntry("inactiveTitleBtnBg", cs->iTitleBtn);
 
436
    group.writeEntry("frame", cs->aFrame);
 
437
    group.writeEntry("inactiveFrame", cs->iaFrame);
 
438
    group.writeEntry("handle", cs->aHandle);
 
439
    group.writeEntry("inactiveHandle", cs->iaHandle);
 
440
    group.writeEntry("linkColor", cs->link);
 
441
    group.writeEntry("visitedLinkColor", cs->visitedLink);
 
442
    group.writeEntry("alternateBackground", cs->alternateBackground);
 
443
    group.writeEntry("shadeSortColumn", cs->shadeSortColumn);
 
444
 
 
445
    delete config;
 
446
}
 
447
 
 
448
 
 
449
void KColorScheme::slotRemove()
 
450
{
 
451
    int ind = sList->currentRow();
 
452
    KColorSchemeEntry *entry = mSchemeList->at(ind-nSysSchemes);
 
453
    if (!entry) return;
 
454
 
 
455
    if (unlink(QFile::encodeName(entry->path).data())) {
 
456
        KMessageBox::error( 0,
 
457
          i18n("This color scheme could not be removed.\n"
 
458
           "Perhaps you do not have permission to alter the file"
 
459
           "system where the color scheme is stored." ));
 
460
        return;
 
461
    }
 
462
 
 
463
    delete sList->takeItem(ind);
 
464
    mSchemeList->remove(entry);
 
465
 
 
466
    ind = sList->currentRow();
 
467
    entry = mSchemeList->at(ind-nSysSchemes);
 
468
    if (!entry) return;
 
469
    removeBt->setEnabled(entry ? entry->local : false);
 
470
}
 
471
 
 
472
 
 
473
/*
 
474
 * Add a local color scheme.
 
475
 */
 
476
void KColorScheme::slotAdd()
 
477
{
 
478
    QString sName;
 
479
    if (sList->currentRow() >= nSysSchemes)
 
480
       sName = sList->currentItem()->text();
 
481
 
 
482
    QString sFile;
 
483
 
 
484
    bool valid = false;
 
485
    bool ok;
 
486
    int exists = -1;
 
487
 
 
488
    while (!valid)
 
489
    {
 
490
        sName = KInputDialog::getText( i18n( "Save Color Scheme" ),
 
491
            i18n( "Enter a name for the color scheme:" ), sName, &ok, this );
 
492
        if (!ok)
 
493
            return;
 
494
 
 
495
        sName = sName.simplified();
 
496
        sFile = sName;
 
497
 
 
498
        int i = 0;
 
499
 
 
500
        exists = -1;
 
501
        // Check if it's already there
 
502
        for (i=0; i < (int) sList->count(); i++)
 
503
        {
 
504
            if (sName == sList->item(i)->text())
 
505
            {
 
506
                exists = i;
 
507
                int result = KMessageBox::warningContinueCancel( this,
 
508
                   i18n("A color scheme with the name '%1' already exists.\n"
 
509
                        "Do you want to overwrite it?\n", sName),
 
510
                   i18n("Save Color Scheme"),
 
511
                   KGuiItem(i18n("Overwrite")));
 
512
                if (result == KMessageBox::Cancel)
 
513
                    break;
 
514
            }
 
515
        }
 
516
        if (i == (int) sList->count())
 
517
            valid = true;
 
518
    }
 
519
 
 
520
    disconnect(sList, SIGNAL(currentRowChanged(int)), this,
 
521
               SLOT(slotPreviewScheme(int)));
 
522
 
 
523
    if (exists != -1)
 
524
    {
 
525
       sList->setFocus();
 
526
       sList->setCurrentRow(exists);
 
527
    }
 
528
    else
 
529
    {
 
530
       sFile = KGlobal::dirs()->saveLocation("data", "kdisplay/color-schemes/") + sFile + ".kcsrc";
 
531
       KConfigGroup config(KSharedConfig::openConfig(sFile, KConfig::OnlyLocal), "Color Scheme");
 
532
       config.writeEntry("Name", sName);
 
533
 
 
534
       insertEntry(sFile, sName);
 
535
 
 
536
    }
 
537
    slotSave();
 
538
 
 
539
    QPixmap preview = mkColorPreview(cs);
 
540
    int current = sList->currentRow();
 
541
    sList->item(current)->setIcon(preview);
 
542
    connect(sList, SIGNAL(currentRowChanged(int)), SLOT(slotPreviewScheme(int)));
 
543
    slotPreviewScheme(current);
 
544
}
 
545
 
 
546
void KColorScheme::slotImport()
 
547
{
 
548
        QString location = KStandardDirs::locateLocal( "data", "kdisplay/color-schemes/" );
 
549
 
 
550
        KUrl file ( KFileDialog::getOpenFileName(QString(), "*.kcsrc", this) );
 
551
        if ( file.isEmpty() )
 
552
                return;
 
553
 
 
554
        //kDebug() << "Location: " << location;
 
555
        if (!KIO::NetAccess::file_copy(file, KUrl( location+file.fileName( KUrl::ObeyTrailingSlash ) ) ) )
 
556
        {
 
557
                KMessageBox::error(this, KIO::NetAccess::lastErrorString(),i18n("Import failed."));
 
558
                return;
 
559
        }
 
560
        else
 
561
        {
 
562
                QString sFile = location + file.fileName( false );
 
563
                KConfigGroup config(KSharedConfig::openConfig(sFile, KConfig::OnlyLocal), "Color Scheme");
 
564
                QString sName = config.readEntry("Name", i18n("Untitled Theme"));
 
565
 
 
566
                insertEntry(sFile, sName);
 
567
                QPixmap preview = mkColorPreview(cs);
 
568
                int current = sList->currentRow();
 
569
                sList->item(current)->setIcon(preview);
 
570
                connect(sList, SIGNAL(currentRowChanged(int)), SLOT(slotPreviewScheme(int)));
 
571
                slotPreviewScheme(current);
 
572
        }
 
573
}
 
574
 
 
575
QColor &KColorScheme::color(int index)
 
576
{
 
577
    switch(index) {
 
578
    case CSM_Inactive_title_bar:
 
579
    return cs->iaTitle;
 
580
    case CSM_Inactive_title_text:
 
581
    return cs->iaTxt;
 
582
    case CSM_Inactive_title_blend:
 
583
    return cs->iaBlend;
 
584
    case CSM_Active_title_bar:
 
585
    return cs->aTitle;
 
586
    case CSM_Active_title_text:
 
587
    return cs->aTxt;
 
588
    case CSM_Active_title_blend:
 
589
    return cs->aBlend;
 
590
    case CSM_Background:
 
591
    return cs->back;
 
592
    case CSM_Text:
 
593
    return cs->txt;
 
594
    case CSM_Select_background:
 
595
    return cs->select;
 
596
    case CSM_Select_text:
 
597
    return cs->selectTxt;
 
598
    case CSM_Standard_background:
 
599
    return cs->window;
 
600
    case CSM_Standard_text:
 
601
    return cs->windowTxt;
 
602
    case CSM_Button_background:
 
603
    return cs->button;
 
604
    case CSM_Button_text:
 
605
    return cs->buttonTxt;
 
606
    case CSM_Active_title_button:
 
607
    return cs->aTitleBtn;
 
608
    case CSM_Inactive_title_button:
 
609
    return cs->iTitleBtn;
 
610
    case CSM_Active_frame:
 
611
    return cs->aFrame;
 
612
    case CSM_Active_handle:
 
613
    return cs->aHandle;
 
614
    case CSM_Inactive_frame:
 
615
    return cs->iaFrame;
 
616
    case CSM_Inactive_handle:
 
617
    return cs->iaHandle;
 
618
    case CSM_Link:
 
619
    return cs->link;
 
620
    case CSM_Followed_Link:
 
621
    return cs->visitedLink;
 
622
    case CSM_Alternate_background:
 
623
    return cs->alternateBackground;
 
624
    }
 
625
 
 
626
    assert(0); // Should never be here!
 
627
    return cs->iaTxt; // Silence compiler
 
628
}
 
629
 
 
630
void KColorScheme::slotColorForWidget(int indx, const QColor& col)
 
631
{
 
632
    mColorTreeWidget->setColor( indx , col );
 
633
}
 
634
 
 
635
void KColorScheme::slotShadeSortColumnChanged(bool b)
 
636
{
 
637
    cs->shadeSortColumn = b;
 
638
    sCurrentScheme.clear();
 
639
 
 
640
    emit changed(true);
 
641
}
 
642
 
 
643
/*
 
644
 * Read a color scheme into "cs".
 
645
 *
 
646
 * KEEP IN SYNC with thememgr!
 
647
 */
 
648
void KColorScheme::readScheme( int index )
 
649
{
 
650
    QColor widget(239, 239, 239);
 
651
    QColor kde34Blue(103,141,178);
 
652
    QColor inactiveBackground(157,170,186);
 
653
    QColor activeBackground(65,142,220);
 
654
    QColor inactiveForeground(221,221,221);
 
655
    QColor activeBlend(107,145,184);
 
656
    QColor inactiveBlend(157,170,186);
 
657
    QColor activeTitleBtnBg(220,220,220);
 
658
    QColor inactiveTitleBtnBg(220,220,220);
 
659
    QColor alternateBackground(237,244,249);
 
660
 
 
661
    QColor button;
 
662
    if (QPixmap::defaultDepth() > 8)
 
663
      button.setRgb(221, 223, 228 );
 
664
    else
 
665
      button.setRgb(220, 220, 220);
 
666
 
 
667
    QColor link(0, 0, 238);
 
668
    QColor visitedLink(82, 24,139);
 
669
 
 
670
    // note: keep default color scheme in sync with default Current Scheme
 
671
    if (index == 1) {
 
672
      sCurrentScheme  = "<default>";
 
673
      cs->txt         = Qt::black;
 
674
      cs->back        = widget;
 
675
      cs->select      = kde34Blue;
 
676
      cs->selectTxt   = Qt::white;
 
677
      cs->window      = Qt::white;
 
678
      cs->windowTxt   = Qt::black;
 
679
      cs->iaTitle     = inactiveBackground;
 
680
      cs->iaTxt       = inactiveForeground;
 
681
      cs->iaBlend     = inactiveBlend;
 
682
      cs->aTitle      = activeBackground;
 
683
      cs->aTxt        = Qt::white;
 
684
      cs->aBlend      = activeBlend;
 
685
      cs->button      = button;
 
686
      cs->buttonTxt   = Qt::black;
 
687
      cs->aTitleBtn   = activeTitleBtnBg;
 
688
      cs->iTitleBtn   = inactiveTitleBtnBg;
 
689
      cs->aFrame      = cs->back;
 
690
      cs->aHandle     = cs->back;
 
691
      cs->iaFrame     = cs->back;
 
692
      cs->iaHandle    = cs->back;
 
693
      cs->link        = link;
 
694
      cs->visitedLink = visitedLink;
 
695
      cs->alternateBackground = alternateBackground;
 
696
 
 
697
      cs->contrast    = 7;
 
698
      cs->shadeSortColumn = KDE_DEFAULT_SHADE_SORT_COLUMN;
 
699
 
 
700
      return;
 
701
    }
 
702
 
 
703
    KConfig *config;
 
704
    KConfigGroup group;
 
705
    if (index == 0) {
 
706
      // Current scheme
 
707
      config = KGlobal::config().data();
 
708
      group = config->group("General");
 
709
    } else {
 
710
      // Open scheme file
 
711
      KColorSchemeEntry *entry = mSchemeList->at(sList->currentRow()-nSysSchemes);
 
712
      if (!entry) return;
 
713
      sCurrentScheme = entry->path;
 
714
      config = new KConfig(sCurrentScheme, KConfig::OnlyLocal);
 
715
      group = config->group("Color Scheme");
 
716
      int i = sCurrentScheme.lastIndexOf('/');
 
717
      if (i >= 0)
 
718
        sCurrentScheme = sCurrentScheme.mid(i+1);
 
719
    }
 
720
 
 
721
    cs->shadeSortColumn = group.readEntry( "shadeSortColumn", KDE_DEFAULT_SHADE_SORT_COLUMN );
 
722
 
 
723
    // note: defaults should be the same as the KDE default
 
724
    QColor auxBlack, auxWhite;
 
725
    auxBlack = Qt::black;
 
726
    auxWhite = Qt::white;
 
727
    cs->txt = group.readEntry( "foreground", auxBlack );
 
728
    cs->back = group.readEntry( "background", widget );
 
729
    cs->select = group.readEntry( "selectBackground", kde34Blue );
 
730
    cs->selectTxt = group.readEntry( "selectForeground", auxWhite );
 
731
    cs->window = group.readEntry( "windowBackground", auxWhite );
 
732
    cs->windowTxt = group.readEntry( "windowForeground", auxBlack );
 
733
    cs->button = group.readEntry( "buttonBackground", button );
 
734
    cs->buttonTxt = group.readEntry( "buttonForeground", auxBlack );
 
735
    cs->link = group.readEntry( "linkColor", link );
 
736
    cs->visitedLink = group.readEntry( "visitedLinkColor", visitedLink );
 
737
    QColor alternate = KGlobalSettings::calculateAlternateBackgroundColor(cs->window);
 
738
    cs->alternateBackground = group.readEntry( "alternateBackground", alternate );
 
739
 
 
740
    if (index == 0)
 
741
      group = config->group( "WM" );
 
742
 
 
743
    cs->iaTitle = group.readEntry("inactiveBackground", inactiveBackground);
 
744
    cs->iaTxt = group.readEntry("inactiveForeground", inactiveForeground);
 
745
    cs->iaBlend = group.readEntry("inactiveBlend", inactiveBackground);
 
746
    cs->iaFrame = group.readEntry("inactiveFrame", cs->back);
 
747
    cs->iaHandle = group.readEntry("inactiveHandle", cs->back);
 
748
    cs->aTitle = group.readEntry("activeBackground", activeBackground);
 
749
    cs->aTxt = group.readEntry("activeForeground", auxWhite);
 
750
    cs->aBlend = group.readEntry("activeBlend", activeBlend);
 
751
    cs->aFrame = group.readEntry("frame", cs->back);
 
752
    cs->aHandle = group.readEntry("handle", cs->back);
 
753
    // hack - this is all going away. For now just set all to button bg
 
754
    cs->aTitleBtn = group.readEntry("activeTitleBtnBg", activeTitleBtnBg);
 
755
    cs->iTitleBtn = group.readEntry("inactiveTitleBtnBg", inactiveTitleBtnBg);
 
756
 
 
757
    if (index == 0)
 
758
      group = config->group( "KDE" );
 
759
 
 
760
    cs->contrast = group.readEntry( "contrast", 7 );
 
761
    if (index != 0)
 
762
      delete config;
 
763
 
 
764
    mColorTreeWidget->blockSignals(true);   //block the colorChanged signal
 
765
    for(int f=0; f< CSM_LAST ; f++)
 
766
        mColorTreeWidget->setColor( f , color(f) );
 
767
    mColorTreeWidget->blockSignals(false);
 
768
 
 
769
}
 
770
 
 
771
 
 
772
/*
 
773
 * Get all installed color schemes.
 
774
 */
 
775
void KColorScheme::readSchemeNames()
 
776
{
 
777
    mSchemeList->clear();
 
778
    sList->clear();
 
779
    // Always a current and a default scheme
 
780
    sList->insertItem( 0 , i18n("Current Scheme") );
 
781
    sList->insertItem( 1 , i18n("KDE Default") );
 
782
    nSysSchemes = 2;
 
783
 
 
784
    // Global + local schemes
 
785
    QStringList list = KGlobal::dirs()->findAllResources("data",
 
786
            "kdisplay/color-schemes/*.kcsrc", KStandardDirs::NoDuplicates);
 
787
 
 
788
    // And add them
 
789
    for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
 
790
       KConfigGroup config(KSharedConfig::openConfig(*it, KConfig::OnlyLocal), "Color Scheme");
 
791
       QString str = config.readEntry("Name");
 
792
       if (str.isEmpty()) {
 
793
          str =  config.readEntry("name");
 
794
          if (str.isEmpty())
 
795
             continue;
 
796
       }
 
797
       mSchemeList->append(new KColorSchemeEntry(*it, str, !config.isImmutable()));
 
798
    }
 
799
 
 
800
    mSchemeList->sort();
 
801
 
 
802
    for(KColorSchemeEntry *entry = mSchemeList->first(); entry; entry = mSchemeList->next())
 
803
    {
 
804
       sList->addItem(entry->name);
 
805
    }
 
806
 
 
807
    for (int i = 0; i < (nSysSchemes + mSchemeList->count()); i++)
 
808
    {
 
809
       sList->setCurrentRow(i);
 
810
       readScheme(i);
 
811
       QPixmap preview = mkColorPreview(cs);
 
812
       sList->item(i)->setIcon(preview);
 
813
    }
 
814
 
 
815
}
 
816
 
 
817
/*
 
818
 * Find scheme based on filename
 
819
 */
 
820
int KColorScheme::findSchemeByName(const QString &scheme)
 
821
{
 
822
   if (scheme.isEmpty())
 
823
      return 0;
 
824
   if (scheme == "<default>")
 
825
      return 1;
 
826
 
 
827
   QString search = scheme;
 
828
   int i = search.lastIndexOf('/');
 
829
   if (i >= 0)
 
830
      search = search.mid(i+1);
 
831
 
 
832
   i = 0;
 
833
 
 
834
   for(KColorSchemeEntry *entry = mSchemeList->first(); entry; entry = mSchemeList->next())
 
835
   {
 
836
      KUrl url;
 
837
      url.setPath(entry->path);
 
838
      if (url.fileName() == search)
 
839
         return i+nSysSchemes;
 
840
      i++;
 
841
   }
 
842
 
 
843
   return 0;
 
844
}
 
845
 
 
846
 
 
847
void KColorScheme::slotPreviewScheme(int indx)
 
848
{
 
849
    readScheme(indx);
 
850
 
 
851
    // Set various appropriate for the scheme
 
852
 
 
853
    cbShadeList->setChecked(cs->shadeSortColumn);
 
854
 
 
855
    cs->drawSampleWidgets();
 
856
    sb->blockSignals(true);
 
857
    sb->setValue(cs->contrast);
 
858
    sb->blockSignals(false);
 
859
    if (indx < nSysSchemes)
 
860
       removeBt->setEnabled(false);
 
861
    else
 
862
    {
 
863
       KColorSchemeEntry *entry = mSchemeList->at(indx-nSysSchemes);
 
864
       removeBt->setEnabled(entry ? entry->local : false);
 
865
    }
 
866
 
 
867
    emit changed((indx != 0));
 
868
}
 
869
 
 
870
 
 
871
/* this function should dissappear: colorscm should work directly on a Qt palette, since
 
872
   this will give us much more cusomization with qt-2.0.
 
873
   */
 
874
QPalette KColorScheme::createPalette()
 
875
{
 
876
    QColorGroup disabledgrp(cs->windowTxt, cs->back, cs->back.light(150),
 
877
                cs->back.dark(), cs->back.dark(120), cs->back.dark(120),
 
878
                cs->window);
 
879
 
 
880
    QColorGroup colgrp(cs->windowTxt, cs->back, cs->back.light(150),
 
881
               cs->back.dark(), cs->back.dark(120), cs->txt, cs->window);
 
882
 
 
883
    colgrp.setColor(QPalette::Highlight, cs->select);
 
884
    colgrp.setColor(QPalette::HighlightedText, cs->selectTxt);
 
885
    colgrp.setColor(QPalette::Button, cs->button);
 
886
    colgrp.setColor(QPalette::ButtonText, cs->buttonTxt);
 
887
    return QPalette( colgrp, disabledgrp, colgrp);
 
888
}
 
889
 
 
890
void KColorScheme::insertEntry(const QString &sFile, const QString &sName)
 
891
{
 
892
       KColorSchemeEntry *newEntry = new KColorSchemeEntry(sFile, sName, true);
 
893
       mSchemeList->inSort(newEntry);
 
894
       int newIndex = mSchemeList->findRef(newEntry)+nSysSchemes;
 
895
       sList->insertItem(newIndex , sName);
 
896
       sList->setCurrentRow(newIndex);
 
897
}
 
898
 
 
899
void KColorScheme::setColorName( const QString & name, int id , int id2 )
 
900
{
 
901
    mColorTreeWidget->addRole( id , id2, name );
 
902
}
 
903
 
 
904
void KColorScheme::slotColorChanged( int selection , const QColor & col)
 
905
{
 
906
    // Adjust the alternate background color if the standard color changes
 
907
    // Only if the previous alternate color was not a user-configured one
 
908
    // of course
 
909
    if ( selection == CSM_Standard_background &&
 
910
         color(CSM_Alternate_background) ==
 
911
         KGlobalSettings::calculateAlternateBackgroundColor(
 
912
             color(CSM_Standard_background) ) )
 
913
    {
 
914
        color(CSM_Alternate_background) =
 
915
            KGlobalSettings::calculateAlternateBackgroundColor( col );
 
916
    }
 
917
 
 
918
    color(selection) = col;
 
919
 
 
920
    cs->drawSampleWidgets();
 
921
 
 
922
    sCurrentScheme.clear();
 
923
 
 
924
    emit changed(true);
 
925
}
 
926
 
 
927
 
 
928
 
 
929
#include "colorscm.moc"