1 /*****************************************************************************
2 * Copyright (C) 2003 Csaba Karai <krusader@users.sourceforge.net> *
3 * Copyright (C) 2004-2019 Krusader Krew [https://krusader.org] *
4 * *
5 * This file is part of Krusader [https://krusader.org]. *
6 * *
7 * Krusader is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Krusader is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with Krusader. If not, see [http://www.gnu.org/licenses/]. *
19 *****************************************************************************/
20
21 #include "konfiguratoritems.h"
22
23 #include "../krglobal.h"
24 #include "../icon.h"
25
26 // QtCore
27 #include <QMetaMethod>
28 // QtGui
29 #include <QPainter>
30 #include <QPen>
31 #include <QPixmap>
32 // QtWidgets
33 #include <QColorDialog>
34 #include <QFontDialog>
35 #include <QLabel>
36
37 #include <KCompletion/KLineEdit>
38 #include <KConfigCore/KSharedConfig>
39 #include <KI18n/KLocalizedString>
40
41
KonfiguratorExtension(QObject * obj,QString cfgGroup,QString cfgName,bool restartNeeded,int page)42 KonfiguratorExtension::KonfiguratorExtension(QObject *obj, QString cfgGroup, QString cfgName,
43 bool restartNeeded, int page)
44 : QObject(), objectPtr(obj), applyConnected(false), setDefaultsConnected(false), changed(false),
45 restartNeeded(restartNeeded), subpage(page), configGroup(cfgGroup), configName(cfgName)
46 {
47 }
48
connectNotify(const QMetaMethod & signal)49 void KonfiguratorExtension::connectNotify(const QMetaMethod &signal)
50 {
51 if (signal == QMetaMethod::fromSignal(&KonfiguratorExtension::applyManually))
52 applyConnected = true;
53 else if (signal == QMetaMethod::fromSignal(&KonfiguratorExtension::setDefaultsManually))
54 setDefaultsConnected = true;
55
56 QObject::connectNotify(signal);
57 }
58
apply()59 bool KonfiguratorExtension::apply()
60 {
61 if (!changed)
62 return false;
63
64 if (applyConnected)
65 emit applyManually(objectPtr, configGroup, configName);
66 else
67 emit applyAuto(objectPtr, configGroup, configName);
68
69 setChanged(false);
70 return restartNeeded;
71 }
72
setDefaults()73 void KonfiguratorExtension::setDefaults()
74 {
75 if (setDefaultsConnected)
76 emit setDefaultsManually(objectPtr);
77 else
78 emit setDefaultsAuto(objectPtr);
79 }
80
loadInitialValue()81 void KonfiguratorExtension::loadInitialValue()
82 {
83 emit setInitialValue(objectPtr);
84 }
85
isChanged()86 bool KonfiguratorExtension::isChanged()
87 {
88 return changed;
89 }
90
91 // KonfiguratorCheckBox class
92 ///////////////////////////////
93
KonfiguratorCheckBox(QString configGroup,QString name,bool defaultValue,QString text,QWidget * parent,bool restart,int page)94 KonfiguratorCheckBox::KonfiguratorCheckBox(QString configGroup, QString name, bool defaultValue, QString text,
95 QWidget *parent, bool restart, int page) : QCheckBox(text, parent),
96 defaultValue(defaultValue)
97 {
98 ext = new KonfiguratorExtension(this, configGroup, name, restart, page);
99 connect(ext, SIGNAL(applyAuto(QObject*,QString,QString)), this, SLOT(slotApply(QObject*,QString,QString)));
100 connect(ext, SIGNAL(setDefaultsAuto(QObject*)), this, SLOT(slotSetDefaults(QObject*)));
101 connect(ext, SIGNAL(setInitialValue(QObject*)), this, SLOT(loadInitialValue()));
102
103 connect(this, SIGNAL(stateChanged(int)), ext, SLOT(setChanged()));
104 loadInitialValue();
105 }
106
~KonfiguratorCheckBox()107 KonfiguratorCheckBox::~KonfiguratorCheckBox()
108 {
109 delete ext;
110 }
111
loadInitialValue()112 void KonfiguratorCheckBox::loadInitialValue()
113 {
114 KConfigGroup group(krConfig, ext->getConfigGroup());
115 setChecked(group.readEntry(ext->getConfigName(), defaultValue));
116 ext->setChanged(false);
117 }
118
checkStateSet()119 void KonfiguratorCheckBox::checkStateSet()
120 {
121 QCheckBox::checkStateSet();
122 updateDeps();
123 }
124
nextCheckState()125 void KonfiguratorCheckBox::nextCheckState()
126 {
127 QCheckBox::nextCheckState();
128 updateDeps();
129 }
130
addDep(KonfiguratorCheckBox * dep)131 void KonfiguratorCheckBox::addDep(KonfiguratorCheckBox *dep)
132 {
133 deps << dep;
134 dep->setEnabled(isChecked());
135 }
136
updateDeps()137 void KonfiguratorCheckBox::updateDeps()
138 {
139 foreach(KonfiguratorCheckBox *dep, deps)
140 dep->setEnabled(isChecked());
141 }
142
slotApply(QObject *,QString configGroup,QString name)143 void KonfiguratorCheckBox::slotApply(QObject *, QString configGroup, QString name)
144 {
145 KConfigGroup(krConfig, configGroup).writeEntry(name, isChecked());
146 }
147
slotSetDefaults(QObject *)148 void KonfiguratorCheckBox::slotSetDefaults(QObject *)
149 {
150 if (isChecked() != defaultValue)
151 setChecked(defaultValue);
152 }
153
154
155 // KonfiguratorSpinBox class
156 ///////////////////////////////
157
KonfiguratorSpinBox(QString configGroup,QString configName,int defaultValue,int min,int max,QWidget * parent,bool restartNeeded,int page)158 KonfiguratorSpinBox::KonfiguratorSpinBox(QString configGroup, QString configName, int defaultValue,
159 int min, int max, QWidget *parent, bool restartNeeded,
160 int page)
161 : QSpinBox(parent), defaultValue(defaultValue)
162 {
163 ext = new KonfiguratorExtension(this, configGroup, configName, restartNeeded, page);
164 connect(ext, SIGNAL(applyAuto(QObject*,QString,QString)), this, SLOT(slotApply(QObject*,QString,QString)));
165 connect(ext, SIGNAL(setDefaultsAuto(QObject*)), this, SLOT(slotSetDefaults(QObject*)));
166 connect(ext, SIGNAL(setInitialValue(QObject*)), this, SLOT(loadInitialValue()));
167
168 connect(this, SIGNAL(valueChanged(int)), ext, SLOT(setChanged()));
169
170 setMinimum(min);
171 setMaximum(max);
172
173 loadInitialValue();
174 }
175
~KonfiguratorSpinBox()176 KonfiguratorSpinBox::~KonfiguratorSpinBox()
177 {
178 delete ext;
179 }
180
loadInitialValue()181 void KonfiguratorSpinBox::loadInitialValue()
182 {
183 KConfigGroup group(krConfig, ext->getConfigGroup());
184 setValue(group.readEntry(ext->getConfigName(), defaultValue));
185 ext->setChanged(false);
186 }
187
slotApply(QObject *,QString configGroup,QString name)188 void KonfiguratorSpinBox::slotApply(QObject *, QString configGroup, QString name)
189 {
190 KConfigGroup(krConfig, configGroup).writeEntry(name, value());
191 }
192
slotSetDefaults(QObject *)193 void KonfiguratorSpinBox::slotSetDefaults(QObject *)
194 {
195 if (value() != defaultValue)
196 setValue(defaultValue);
197 }
198
199 // KonfiguratorCheckBoxGroup class
200 ///////////////////////////////
201
add(KonfiguratorCheckBox * checkBox)202 void KonfiguratorCheckBoxGroup::add(KonfiguratorCheckBox *checkBox)
203 {
204 checkBoxList.append(checkBox);
205 }
206
find(int index)207 KonfiguratorCheckBox * KonfiguratorCheckBoxGroup::find(int index)
208 {
209 if (index < 0 || index >= checkBoxList.count())
210 return 0;
211 return checkBoxList.at(index);
212 }
213
find(QString name)214 KonfiguratorCheckBox * KonfiguratorCheckBoxGroup::find(QString name)
215 {
216 QListIterator<KonfiguratorCheckBox *> it(checkBoxList);
217 while (it.hasNext()) {
218 KonfiguratorCheckBox * checkBox = it.next();
219
220 if (checkBox->extension()->getConfigName() == name)
221 return checkBox;
222 }
223
224 return 0;
225 }
226
227
228 // KonfiguratorRadioButtons class
229 ///////////////////////////////
230
KonfiguratorRadioButtons(QString configGroup,QString name,QString defaultValue,QWidget * parent,bool restart,int page)231 KonfiguratorRadioButtons::KonfiguratorRadioButtons(QString configGroup, QString name,
232 QString defaultValue, QWidget *parent, bool restart, int page) :
233 QWidget(parent), defaultValue(defaultValue)
234 {
235 ext = new KonfiguratorExtension(this, configGroup, name, restart, page);
236 connect(ext, SIGNAL(applyAuto(QObject*,QString,QString)), this, SLOT(slotApply(QObject*,QString,QString)));
237 connect(ext, SIGNAL(setDefaultsAuto(QObject*)), this, SLOT(slotSetDefaults(QObject*)));
238 connect(ext, SIGNAL(setInitialValue(QObject*)), this, SLOT(loadInitialValue()));
239 }
240
~KonfiguratorRadioButtons()241 KonfiguratorRadioButtons::~KonfiguratorRadioButtons()
242 {
243 delete ext;
244 }
245
addRadioButton(QRadioButton * radioWidget,QString name,QString value)246 void KonfiguratorRadioButtons::addRadioButton(QRadioButton *radioWidget, QString name, QString value)
247 {
248 radioButtons.append(radioWidget);
249 radioNames.push_back(name);
250 radioValues.push_back(value);
251
252 connect(radioWidget, SIGNAL(toggled(bool)), ext, SLOT(setChanged()));
253 }
254
find(int index)255 QRadioButton * KonfiguratorRadioButtons::find(int index)
256 {
257 if (index < 0 || index >= radioButtons.count())
258 return 0;
259
260 return radioButtons.at(index);
261 }
262
find(QString name)263 QRadioButton * KonfiguratorRadioButtons::find(QString name)
264 {
265 int index = radioNames.indexOf(name);
266 if (index == -1)
267 return 0;
268
269 return radioButtons.at(index);
270 }
271
selectButton(QString value)272 void KonfiguratorRadioButtons::selectButton(QString value)
273 {
274 int cnt = 0;
275
276 QListIterator<QRadioButton *> it(radioButtons);
277 while (it.hasNext()) {
278 QRadioButton * btn = it.next();
279
280 if (value == radioValues[ cnt ]) {
281 btn->setChecked(true);
282 return;
283 }
284
285 cnt++;
286 }
287
288 if (!radioButtons.isEmpty())
289 radioButtons.first()->setChecked(true);
290 }
291
loadInitialValue()292 void KonfiguratorRadioButtons::loadInitialValue()
293 {
294 KConfigGroup group(krConfig, ext->getConfigGroup());
295 QString initValue = group.readEntry(ext->getConfigName(), defaultValue);
296
297 selectButton(initValue);
298 ext->setChanged(false);
299 }
300
selectedValue()301 QString KonfiguratorRadioButtons::selectedValue()
302 {
303 int cnt = 0;
304
305 QListIterator<QRadioButton *> it(radioButtons);
306 while (it.hasNext()) {
307 QRadioButton * btn = it.next();
308
309 if (btn->isChecked()) {
310 return radioValues[ cnt ];
311 }
312
313 cnt++;
314 }
315 return QString();
316 }
317
slotApply(QObject *,QString configGroup,QString name)318 void KonfiguratorRadioButtons::slotApply(QObject *, QString configGroup, QString name)
319 {
320 QString value = selectedValue();
321
322 if (!value.isEmpty())
323 KConfigGroup(krConfig, configGroup).writeEntry(name, value);
324 }
325
slotSetDefaults(QObject *)326 void KonfiguratorRadioButtons::slotSetDefaults(QObject *)
327 {
328 selectButton(defaultValue);
329 }
330
331 // KonfiguratorEditBox class
332 ///////////////////////////////
333
KonfiguratorEditBox(QString configGroup,QString name,QString defaultValue,QWidget * parent,bool restart,int page)334 KonfiguratorEditBox::KonfiguratorEditBox(QString configGroup, QString name, QString defaultValue,
335 QWidget *parent, bool restart, int page) : QLineEdit(parent),
336 defaultValue(defaultValue)
337 {
338 ext = new KonfiguratorExtension(this, configGroup, name, restart, page);
339 connect(ext, SIGNAL(applyAuto(QObject *, QString, QString)), this,
340 SLOT(slotApply(QObject *, QString, QString)));
341 connect(ext, SIGNAL(setDefaultsAuto(QObject*)), this, SLOT(slotSetDefaults(QObject*)));
342 connect(ext, SIGNAL(setInitialValue(QObject*)), this, SLOT(loadInitialValue()));
343
344 connect(this, SIGNAL(textChanged(QString)), ext, SLOT(setChanged()));
345
346 loadInitialValue();
347 }
348
~KonfiguratorEditBox()349 KonfiguratorEditBox::~KonfiguratorEditBox()
350 {
351 delete ext;
352 }
353
loadInitialValue()354 void KonfiguratorEditBox::loadInitialValue()
355 {
356 KConfigGroup group(krConfig, ext->getConfigGroup());
357 setText(group.readEntry(ext->getConfigName(), defaultValue));
358 ext->setChanged(false);
359 }
360
slotApply(QObject *,QString configGroup,QString name)361 void KonfiguratorEditBox::slotApply(QObject *, QString configGroup, QString name)
362 {
363 KConfigGroup(krConfig, configGroup).writeEntry(name, text());
364 }
365
slotSetDefaults(QObject *)366 void KonfiguratorEditBox::slotSetDefaults(QObject *)
367 {
368 if (text() != defaultValue)
369 setText(defaultValue);
370 }
371
372
373 // KonfiguratorURLRequester class
374 ///////////////////////////////
375
KonfiguratorURLRequester(QString configGroup,QString name,QString defaultValue,QWidget * parent,bool restart,int page,bool expansion)376 KonfiguratorURLRequester::KonfiguratorURLRequester(QString configGroup, QString name,
377 QString defaultValue, QWidget *parent,
378 bool restart, int page, bool expansion)
379 : KUrlRequester(parent), defaultValue(defaultValue), expansion(expansion)
380 {
381 ext = new KonfiguratorExtension(this, configGroup, name, restart, page);
382 connect(ext, SIGNAL(applyAuto(QObject *, QString, QString)), this,
383 SLOT(slotApply(QObject *, QString, QString)));
384 connect(ext, SIGNAL(setDefaultsAuto(QObject*)), this, SLOT(slotSetDefaults(QObject*)));
385 connect(ext, SIGNAL(setInitialValue(QObject*)), this, SLOT(loadInitialValue()));
386
387 connect(this, SIGNAL(textChanged(QString)), ext, SLOT(setChanged()));
388
389 loadInitialValue();
390 }
391
~KonfiguratorURLRequester()392 KonfiguratorURLRequester::~KonfiguratorURLRequester()
393 {
394 delete ext;
395 }
396
loadInitialValue()397 void KonfiguratorURLRequester::loadInitialValue()
398 {
399 KConfigGroup group(krConfig, ext->getConfigGroup());
400 lineEdit()->setText(group.readEntry(ext->getConfigName(), defaultValue));
401 ext->setChanged(false);
402 }
403
slotApply(QObject *,QString configGroup,QString name)404 void KonfiguratorURLRequester::slotApply(QObject *, QString configGroup, QString name)
405 {
406 KConfigGroup(krConfig, configGroup)
407 .writeEntry(name, expansion ? url().toDisplayString(QUrl::PreferLocalFile) : text());
408 }
409
slotSetDefaults(QObject *)410 void KonfiguratorURLRequester::slotSetDefaults(QObject *)
411 {
412 if (url().toDisplayString(QUrl::PreferLocalFile) != defaultValue)
413 lineEdit()->setText(defaultValue);
414 }
415
416 // KonfiguratorFontChooser class
417 ///////////////////////////////
418
KonfiguratorFontChooser(QString configGroup,QString name,QFont defaultValue,QWidget * parent,bool restart,int page)419 KonfiguratorFontChooser::KonfiguratorFontChooser(QString configGroup, QString name, QFont defaultValue,
420 QWidget *parent, bool restart, int page) : QWidget(parent),
421 defaultValue(defaultValue)
422 {
423 QHBoxLayout *layout = new QHBoxLayout(this);
424
425 ext = new KonfiguratorExtension(this, configGroup, name, restart, page);
426 connect(ext, SIGNAL(applyAuto(QObject*,QString,QString)), this, SLOT(slotApply(QObject*,QString,QString)));
427 connect(ext, SIGNAL(setDefaultsAuto(QObject*)), this, SLOT(slotSetDefaults(QObject*)));
428 connect(ext, SIGNAL(setInitialValue(QObject*)), this, SLOT(loadInitialValue()));
429
430 pLabel = new QLabel(this);
431 pLabel->setMinimumWidth(150);
432 layout->addWidget(pLabel);
433
434 pToolButton = new QToolButton(this);
435
436 connect(pToolButton, SIGNAL(clicked()), this, SLOT(slotBrowseFont()));
437
438 pToolButton->setIcon(Icon("document-open"));
439 layout->addWidget(pToolButton);
440
441 loadInitialValue();
442 }
443
~KonfiguratorFontChooser()444 KonfiguratorFontChooser::~KonfiguratorFontChooser()
445 {
446 delete ext;
447 }
448
loadInitialValue()449 void KonfiguratorFontChooser::loadInitialValue()
450 {
451 KConfigGroup group(krConfig, ext->getConfigGroup());
452 font = group.readEntry(ext->getConfigName(), defaultValue);
453 ext->setChanged(false);
454 setFont();
455 }
456
setFont()457 void KonfiguratorFontChooser::setFont()
458 {
459 pLabel->setFont(font);
460 pLabel->setText(font.family() + QString(", %1").arg(font.pointSize()));
461 }
462
slotApply(QObject *,QString configGroup,QString name)463 void KonfiguratorFontChooser::slotApply(QObject *, QString configGroup, QString name)
464 {
465 KConfigGroup(krConfig, configGroup).writeEntry(name, font);
466 }
467
slotSetDefaults(QObject *)468 void KonfiguratorFontChooser::slotSetDefaults(QObject *)
469 {
470 font = defaultValue;
471 ext->setChanged();
472 setFont();
473 }
474
slotBrowseFont()475 void KonfiguratorFontChooser::slotBrowseFont()
476 {
477 bool ok;
478 font = QFontDialog::getFont(&ok, font, this);
479 if (!ok) return; // cancelled by the user, and font is actually not changed (getFont returns the font we gave it)
480 ext->setChanged();
481 setFont();
482 }
483
484 // KonfiguratorComboBox class
485 ///////////////////////////////
486
KonfiguratorComboBox(QString configGroup,QString name,QString defaultValue,KONFIGURATOR_NAME_VALUE_PAIR * listIn,int listInLen,QWidget * parent,bool restart,bool editable,int page)487 KonfiguratorComboBox::KonfiguratorComboBox(QString configGroup, QString name, QString defaultValue,
488 KONFIGURATOR_NAME_VALUE_PAIR *listIn, int listInLen, QWidget *parent,
489 bool restart, bool editable, int page) : QComboBox(parent),
490 defaultValue(defaultValue), listLen(listInLen)
491 {
492 list = new KONFIGURATOR_NAME_VALUE_PAIR[ listInLen ];
493
494 for (int i = 0; i != listLen; i++) {
495 list[i] = listIn[i];
496 addItem(list[i].text);
497 }
498
499 ext = new KonfiguratorExtension(this, configGroup, name, restart, page);
500 connect(ext, SIGNAL(applyAuto(QObject*,QString,QString)), this, SLOT(slotApply(QObject*,QString,QString)));
501 connect(ext, SIGNAL(setDefaultsAuto(QObject*)), this, SLOT(slotSetDefaults(QObject*)));
502 connect(ext, SIGNAL(setInitialValue(QObject*)), this, SLOT(loadInitialValue()));
503
504 // connect( this, SIGNAL(highlighted(int)), ext, SLOT(setChanged()) ); /* Removed because of startup combo failure */
505 connect(this, SIGNAL(activated(int)), ext, SLOT(setChanged()));
506 connect(this, SIGNAL(currentTextChanged(QString)), ext, SLOT(setChanged()));
507
508 setEditable(editable);
509 loadInitialValue();
510 }
511
~KonfiguratorComboBox()512 KonfiguratorComboBox::~KonfiguratorComboBox()
513 {
514 delete []list;
515 delete ext;
516 }
517
loadInitialValue()518 void KonfiguratorComboBox::loadInitialValue()
519 {
520 KConfigGroup group(krConfig, ext->getConfigGroup());
521 QString select = group.readEntry(ext->getConfigName(), defaultValue);
522 selectEntry(select);
523 ext->setChanged(false);
524 }
525
slotApply(QObject *,QString configGroup,QString name)526 void KonfiguratorComboBox::slotApply(QObject *, QString configGroup, QString name)
527 {
528 QString text = isEditable() ? lineEdit()->text() : currentText();
529 QString value = text;
530
531 for (int i = 0; i != listLen; i++)
532 if (list[i].text == text) {
533 value = list[i].value;
534 break;
535 }
536
537 KConfigGroup(krConfig, configGroup).writeEntry(name, value);
538 }
539
selectEntry(QString entry)540 void KonfiguratorComboBox::selectEntry(QString entry)
541 {
542 for (int i = 0; i != listLen; i++)
543 if (list[i].value == entry) {
544 setCurrentIndex(i);
545 return;
546 }
547
548 if (isEditable())
549 lineEdit()->setText(entry);
550 else
551 setCurrentIndex(0);
552 }
553
slotSetDefaults(QObject *)554 void KonfiguratorComboBox::slotSetDefaults(QObject *)
555 {
556 selectEntry(defaultValue);
557 }
558
559
560 // KonfiguratorColorChooser class
561 ///////////////////////////////
562
KonfiguratorColorChooser(QString configGroup,QString name,QColor defaultValue,QWidget * parent,bool restart,ADDITIONAL_COLOR * addColPtr,int addColNum,int page)563 KonfiguratorColorChooser::KonfiguratorColorChooser(QString configGroup, QString name,
564 QColor defaultValue, QWidget *parent,
565 bool restart, ADDITIONAL_COLOR *addColPtr,
566 int addColNum, int page)
567 : QComboBox(parent), defaultValue(defaultValue), disableColorChooser(true)
568 {
569 ext = new KonfiguratorExtension(this, configGroup, name, restart, page);
570
571 connect(ext, SIGNAL(applyAuto(QObject*,QString,QString)), this, SLOT(slotApply(QObject*,QString,QString)));
572 connect(ext, SIGNAL(setDefaultsAuto(QObject*)), this, SLOT(slotSetDefaults(QObject*)));
573 connect(ext, SIGNAL(setInitialValue(QObject*)), this, SLOT(loadInitialValue()));
574
575 addColor(i18n("Custom color"), QColor(255, 255, 255));
576 addColor(i18nc("Default color", "Default"), defaultValue);
577
578 for (int i = 0; i != addColNum; i++) {
579 additionalColors.push_back(addColPtr[i]);
580 addColor(addColPtr[i].name, addColPtr[i].color);
581 }
582
583 addColor(i18n("Red"), Qt::red);
584 addColor(i18n("Green"), Qt::green);
585 addColor(i18n("Blue"), Qt::blue);
586 addColor(i18n("Cyan"), Qt::cyan);
587 addColor(i18n("Magenta"), Qt::magenta);
588 addColor(i18n("Yellow"), Qt::yellow);
589 addColor(i18n("Dark Red"), Qt::darkRed);
590 addColor(i18n("Dark Green"), Qt::darkGreen);
591 addColor(i18n("Dark Blue"), Qt::darkBlue);
592 addColor(i18n("Dark Cyan"), Qt::darkCyan);
593 addColor(i18n("Dark Magenta"), Qt::darkMagenta);
594 addColor(i18n("Dark Yellow"), Qt::darkYellow);
595 addColor(i18n("White"), Qt::white);
596 addColor(i18n("Light Gray"), Qt::lightGray);
597 addColor(i18n("Gray"), Qt::gray);
598 addColor(i18n("Dark Gray"), Qt::darkGray);
599 addColor(i18n("Black"), Qt::black);
600
601 connect(this, SIGNAL(activated(int)), this, SLOT(slotCurrentChanged(int)));
602
603 loadInitialValue();
604 }
605
~KonfiguratorColorChooser()606 KonfiguratorColorChooser::~KonfiguratorColorChooser()
607 {
608 delete ext;
609 }
610
createPixmap(QColor color)611 QPixmap KonfiguratorColorChooser::createPixmap(QColor color)
612 {
613 QPainter painter;
614 QPen pen;
615 int size = QFontMetrics(font()).height() * 3 / 4;
616 QRect rect(0, 0, size, size);
617 QPixmap pixmap(rect.width(), rect.height());
618
619 pen.setColor(Qt::black);
620
621 painter.begin(&pixmap);
622 QBrush brush(color);
623 painter.fillRect(rect, brush);
624 painter.setPen(pen);
625 painter.drawRect(rect);
626 painter.end();
627
628 pixmap.detach();
629 return pixmap;
630 }
631
addColor(QString text,QColor color)632 void KonfiguratorColorChooser::addColor(QString text, QColor color)
633 {
634 addItem(createPixmap(color), text);
635 palette.push_back(color);
636 }
637
loadInitialValue()638 void KonfiguratorColorChooser::loadInitialValue()
639 {
640 KConfigGroup group(krConfig, ext->getConfigGroup());
641 QString selected = group.readEntry(ext->getConfigName(), QString(""));
642 setValue(selected);
643 ext->setChanged(false);
644 }
645
setDefaultColor(QColor dflt)646 void KonfiguratorColorChooser::setDefaultColor(QColor dflt)
647 {
648 defaultValue = dflt;
649 palette[1] = defaultValue;
650 setItemIcon(1, createPixmap(defaultValue));
651
652 if (currentIndex() == 1)
653 emit colorChanged();
654 }
655
changeAdditionalColor(int num,QColor color)656 void KonfiguratorColorChooser::changeAdditionalColor(int num, QColor color)
657 {
658 if (num < additionalColors.size()) {
659 palette[2+num] = color;
660 additionalColors[num].color = color;
661 setItemIcon(2 + num, createPixmap(color));
662
663 if (currentIndex() == 2 + num)
664 emit colorChanged();
665 }
666 }
667
setDefaultText(QString text)668 void KonfiguratorColorChooser::setDefaultText(QString text)
669 {
670 setItemIcon(1, createPixmap(defaultValue));
671 setItemText(1, text);
672 }
673
slotApply(QObject *,QString configGroup,QString name)674 void KonfiguratorColorChooser::slotApply(QObject *, QString configGroup, QString name)
675 {
676 KConfigGroup(krConfig, configGroup).writeEntry(name, getValue());
677 }
678
setValue(QString value)679 void KonfiguratorColorChooser::setValue(QString value)
680 {
681 disableColorChooser = true;
682
683 if (value.isEmpty()) {
684 setCurrentIndex(1);
685 customValue = defaultValue;
686 } else {
687 bool found = false;
688
689 for (int j = 0; j != additionalColors.size(); j++)
690 if (additionalColors[j].value == value) {
691 setCurrentIndex(2 + j);
692 found = true;
693 break;
694 }
695
696 if (! found) {
697 KConfigGroup colGroup(krConfig, ext->getConfigGroup());
698 colGroup.writeEntry("TmpColor", value);
699 QColor color = colGroup.readEntry("TmpColor", defaultValue);
700 customValue = color;
701 colGroup.deleteEntry("TmpColor");
702
703 setCurrentIndex(0);
704 for (int i = 2 + additionalColors.size(); i != palette.size(); i++)
705 if (palette[i] == color) {
706 setCurrentIndex(i);
707 break;
708 }
709 }
710 }
711
712 palette[0] = customValue;
713 setItemIcon(0, createPixmap(customValue));
714
715 ext->setChanged();
716 emit colorChanged();
717 disableColorChooser = false;
718 }
719
getValue()720 QString KonfiguratorColorChooser::getValue()
721 {
722 QColor color = palette[ currentIndex()];
723 if (currentIndex() == 1) /* it's the default value? */
724 return "";
725 else if (currentIndex() >= 2 && currentIndex() < 2 + additionalColors.size())
726 return additionalColors[ currentIndex() - 2 ].value;
727 else
728 return QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue());
729 }
730
isValueRGB()731 bool KonfiguratorColorChooser::isValueRGB()
732 {
733 return !(currentIndex() >= 1 && currentIndex() < 2 + additionalColors.size());
734 }
735
slotSetDefaults(QObject *)736 void KonfiguratorColorChooser::slotSetDefaults(QObject *)
737 {
738 ext->setChanged();
739 setCurrentIndex(1);
740 emit colorChanged();
741 }
742
slotCurrentChanged(int number)743 void KonfiguratorColorChooser::slotCurrentChanged(int number)
744 {
745 ext->setChanged();
746 if (number == 0 && !disableColorChooser) {
747 QColor color = QColorDialog::getColor(customValue, this);
748 if (color.isValid()) {
749 disableColorChooser = true;
750 customValue = color;
751 palette[0] = customValue;
752 setItemIcon(0, createPixmap(customValue));
753 disableColorChooser = false;
754 }
755 }
756
757 emit colorChanged();
758 }
759
getColor()760 QColor KonfiguratorColorChooser::getColor()
761 {
762 return palette[ currentIndex()];
763 }
764
765 // KonfiguratorListBox class
766 ///////////////////////////////
767
KonfiguratorListBox(QString configGroup,QString name,QStringList defaultValue,QWidget * parent,bool restart,int page)768 KonfiguratorListBox::KonfiguratorListBox(QString configGroup, QString name, QStringList defaultValue,
769 QWidget *parent, bool restart, int page) : KrListWidget(parent),
770 defaultValue(defaultValue)
771 {
772 ext = new KonfiguratorExtension(this, configGroup, name, restart, page);
773 connect(ext, SIGNAL(applyAuto(QObject*,QString,QString)), this, SLOT(slotApply(QObject*,QString,QString)));
774 connect(ext, SIGNAL(setDefaultsAuto(QObject*)), this, SLOT(slotSetDefaults(QObject*)));
775 connect(ext, SIGNAL(setInitialValue(QObject*)), this, SLOT(loadInitialValue()));
776
777 loadInitialValue();
778 }
779
~KonfiguratorListBox()780 KonfiguratorListBox::~KonfiguratorListBox()
781 {
782 delete ext;
783 }
784
loadInitialValue()785 void KonfiguratorListBox::loadInitialValue()
786 {
787 KConfigGroup group(krConfig, ext->getConfigGroup());
788 setList(group.readEntry(ext->getConfigName(), defaultValue));
789 ext->setChanged(false);
790 }
791
slotApply(QObject *,QString configGroup,QString name)792 void KonfiguratorListBox::slotApply(QObject *, QString configGroup, QString name)
793 {
794 KConfigGroup(krConfig, configGroup).writeEntry(name, list());
795 }
796
slotSetDefaults(QObject *)797 void KonfiguratorListBox::slotSetDefaults(QObject *)
798 {
799 if (list() != defaultValue) {
800 ext->setChanged();
801 setList(defaultValue);
802 }
803 }
804
setList(QStringList list)805 void KonfiguratorListBox::setList(QStringList list)
806 {
807 clear();
808 addItems(list);
809 }
810
list()811 QStringList KonfiguratorListBox::list()
812 {
813 QStringList lst;
814
815 for (int i = 0; i != count(); i++)
816 lst += item(i)->text();
817
818 return lst;
819 }
820
addItem(const QString & item)821 void KonfiguratorListBox::addItem(const QString & item)
822 {
823 if (!list().contains(item)) {
824 KrListWidget::addItem(item);
825 ext->setChanged();
826 }
827 }
828
removeItem(const QString & item)829 void KonfiguratorListBox::removeItem(const QString & item)
830 {
831 QList<QListWidgetItem *> list = findItems(item, Qt::MatchExactly);
832 for (int i = 0; i != list.count(); i++)
833 delete list[ i ];
834
835 if (list.count())
836 ext->setChanged();
837 }
838
839