File indexing completed on 2024-04-28 17:06:09

0001 /*
0002     SPDX-FileCopyrightText: 2003 Csaba Karai <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "konfiguratoritems.h"
0009 
0010 #include "../icon.h"
0011 #include "../krglobal.h"
0012 
0013 // QtCore
0014 #include <QMetaMethod>
0015 // QtGui
0016 #include <QPainter>
0017 #include <QPen>
0018 #include <QPixmap>
0019 // QtWidgets
0020 #include <QColorDialog>
0021 #include <QFontDialog>
0022 #include <QLabel>
0023 
0024 #include <KCompletion/KLineEdit>
0025 #include <KConfigCore/KSharedConfig>
0026 #include <KI18n/KLocalizedString>
0027 #include <utility>
0028 
0029 KonfiguratorExtension::KonfiguratorExtension(QObject *obj, QString cfgGroup, QString cfgName, bool restartNeeded, int page)
0030     : objectPtr(obj)
0031     , applyConnected(false)
0032     , setDefaultsConnected(false)
0033     , changed(false)
0034     , restartNeeded(restartNeeded)
0035     , subpage(page)
0036     , configGroup(std::move(cfgGroup))
0037     , configName(std::move(cfgName))
0038 {
0039 }
0040 
0041 void KonfiguratorExtension::connectNotify(const QMetaMethod &signal)
0042 {
0043     if (signal == QMetaMethod::fromSignal(&KonfiguratorExtension::applyManually))
0044         applyConnected = true;
0045     else if (signal == QMetaMethod::fromSignal(&KonfiguratorExtension::setDefaultsManually))
0046         setDefaultsConnected = true;
0047 
0048     QObject::connectNotify(signal);
0049 }
0050 
0051 bool KonfiguratorExtension::apply()
0052 {
0053     if (!changed)
0054         return false;
0055 
0056     if (applyConnected)
0057         emit applyManually(objectPtr, configGroup, configName);
0058     else
0059         emit applyAuto(objectPtr, configGroup, configName);
0060 
0061     setChanged(false);
0062     return restartNeeded;
0063 }
0064 
0065 void KonfiguratorExtension::setDefaults()
0066 {
0067     if (setDefaultsConnected)
0068         emit setDefaultsManually(objectPtr);
0069     else
0070         emit setDefaultsAuto(objectPtr);
0071 }
0072 
0073 void KonfiguratorExtension::loadInitialValue()
0074 {
0075     emit setInitialValue(objectPtr);
0076 }
0077 
0078 bool KonfiguratorExtension::isChanged()
0079 {
0080     return changed;
0081 }
0082 
0083 // KonfiguratorCheckBox class
0084 ///////////////////////////////
0085 
0086 KonfiguratorCheckBox::KonfiguratorCheckBox(QString configGroup, QString name, bool defaultValue, const QString &text, QWidget *parent, bool restart, int page)
0087     : QCheckBox(text, parent)
0088     , defaultValue(defaultValue)
0089 {
0090     ext = new KonfiguratorExtension(this, std::move(configGroup), std::move(name), restart, page);
0091     connect(ext, &KonfiguratorExtension::applyAuto, this, &KonfiguratorCheckBox::slotApply);
0092     connect(ext, &KonfiguratorExtension::setDefaultsAuto, this, &KonfiguratorCheckBox::slotSetDefaults);
0093     connect(ext, &KonfiguratorExtension::setInitialValue, this, &KonfiguratorCheckBox::loadInitialValue);
0094 
0095     connect(this, &KonfiguratorCheckBox::stateChanged, ext, QOverload<>::of(&KonfiguratorExtension::setChanged));
0096     loadInitialValue();
0097 }
0098 
0099 KonfiguratorCheckBox::~KonfiguratorCheckBox()
0100 {
0101     delete ext;
0102 }
0103 
0104 void KonfiguratorCheckBox::loadInitialValue()
0105 {
0106     KConfigGroup group(krConfig, ext->getConfigGroup());
0107     setChecked(group.readEntry(ext->getConfigName(), defaultValue));
0108     ext->setChanged(false);
0109 }
0110 
0111 void KonfiguratorCheckBox::checkStateSet()
0112 {
0113     QCheckBox::checkStateSet();
0114     updateDeps();
0115 }
0116 
0117 void KonfiguratorCheckBox::nextCheckState()
0118 {
0119     QCheckBox::nextCheckState();
0120     updateDeps();
0121 }
0122 
0123 void KonfiguratorCheckBox::addDep(KonfiguratorCheckBox *dep)
0124 {
0125     deps << dep;
0126     dep->setEnabled(isChecked());
0127 }
0128 
0129 void KonfiguratorCheckBox::updateDeps()
0130 {
0131     foreach (KonfiguratorCheckBox *dep, deps)
0132         dep->setEnabled(isChecked());
0133 }
0134 
0135 void KonfiguratorCheckBox::slotApply(QObject *, const QString &configGroup, const QString &name)
0136 {
0137     KConfigGroup(krConfig, configGroup).writeEntry(name, isChecked());
0138 }
0139 
0140 void KonfiguratorCheckBox::slotSetDefaults(QObject *)
0141 {
0142     if (isChecked() != defaultValue)
0143         setChecked(defaultValue);
0144 }
0145 
0146 // KonfiguratorSpinBox class
0147 ///////////////////////////////
0148 
0149 KonfiguratorSpinBox::KonfiguratorSpinBox(QString configGroup,
0150                                          QString configName,
0151                                          int defaultValue,
0152                                          int min,
0153                                          int max,
0154                                          QWidget *parent,
0155                                          bool restartNeeded,
0156                                          int page)
0157     : QSpinBox(parent)
0158     , defaultValue(defaultValue)
0159 {
0160     ext = new KonfiguratorExtension(this, std::move(configGroup), std::move(configName), restartNeeded, page);
0161     connect(ext, &KonfiguratorExtension::applyAuto, this, &KonfiguratorSpinBox::slotApply);
0162     connect(ext, &KonfiguratorExtension::setDefaultsAuto, this, &KonfiguratorSpinBox::slotSetDefaults);
0163     connect(ext, &KonfiguratorExtension::setInitialValue, this, &KonfiguratorSpinBox::loadInitialValue);
0164 
0165     connect(this, QOverload<int>::of(&KonfiguratorSpinBox::valueChanged), ext, QOverload<>::of(&KonfiguratorExtension::setChanged));
0166 
0167     setMinimum(min);
0168     setMaximum(max);
0169 
0170     loadInitialValue();
0171 }
0172 
0173 KonfiguratorSpinBox::~KonfiguratorSpinBox()
0174 {
0175     delete ext;
0176 }
0177 
0178 void KonfiguratorSpinBox::loadInitialValue()
0179 {
0180     KConfigGroup group(krConfig, ext->getConfigGroup());
0181     setValue(group.readEntry(ext->getConfigName(), defaultValue));
0182     ext->setChanged(false);
0183 }
0184 
0185 void KonfiguratorSpinBox::slotApply(QObject *, const QString &configGroup, const QString &name)
0186 {
0187     KConfigGroup(krConfig, configGroup).writeEntry(name, value());
0188 }
0189 
0190 void KonfiguratorSpinBox::slotSetDefaults(QObject *)
0191 {
0192     if (value() != defaultValue)
0193         setValue(defaultValue);
0194 }
0195 
0196 // KonfiguratorCheckBoxGroup class
0197 ///////////////////////////////
0198 
0199 void KonfiguratorCheckBoxGroup::add(KonfiguratorCheckBox *checkBox)
0200 {
0201     checkBoxList.append(checkBox);
0202 }
0203 
0204 KonfiguratorCheckBox *KonfiguratorCheckBoxGroup::find(int index)
0205 {
0206     if (index < 0 || index >= checkBoxList.count())
0207         return nullptr;
0208     return checkBoxList.at(index);
0209 }
0210 
0211 KonfiguratorCheckBox *KonfiguratorCheckBoxGroup::find(const QString &name)
0212 {
0213     QListIterator<KonfiguratorCheckBox *> it(checkBoxList);
0214     while (it.hasNext()) {
0215         KonfiguratorCheckBox *checkBox = it.next();
0216 
0217         if (checkBox->extension()->getConfigName() == name)
0218             return checkBox;
0219     }
0220 
0221     return nullptr;
0222 }
0223 
0224 // KonfiguratorRadioButtons class
0225 ///////////////////////////////
0226 
0227 KonfiguratorRadioButtons::KonfiguratorRadioButtons(QString configGroup, QString name, QString defaultValue, QWidget *parent, bool restart, int page)
0228     : QWidget(parent)
0229     , defaultValue(std::move(defaultValue))
0230 {
0231     ext = new KonfiguratorExtension(this, std::move(configGroup), std::move(name), restart, page);
0232     connect(ext, &KonfiguratorExtension::applyAuto, this, &KonfiguratorRadioButtons::slotApply);
0233     connect(ext, &KonfiguratorExtension::setDefaultsAuto, this, &KonfiguratorRadioButtons::slotSetDefaults);
0234     connect(ext, &KonfiguratorExtension::setInitialValue, this, &KonfiguratorRadioButtons::loadInitialValue);
0235 }
0236 
0237 KonfiguratorRadioButtons::~KonfiguratorRadioButtons()
0238 {
0239     delete ext;
0240 }
0241 
0242 void KonfiguratorRadioButtons::addRadioButton(QRadioButton *radioWidget, const QString &name, const QString &value)
0243 {
0244     radioButtons.append(radioWidget);
0245     radioNames.push_back(name);
0246     radioValues.push_back(value);
0247 
0248     connect(radioWidget, &QRadioButton::toggled, ext, QOverload<>::of(&KonfiguratorExtension::setChanged));
0249 }
0250 
0251 QRadioButton *KonfiguratorRadioButtons::find(int index)
0252 {
0253     if (index < 0 || index >= radioButtons.count())
0254         return nullptr;
0255 
0256     return radioButtons.at(index);
0257 }
0258 
0259 QRadioButton *KonfiguratorRadioButtons::find(const QString &name)
0260 {
0261     int index = radioNames.indexOf(name);
0262     if (index == -1)
0263         return nullptr;
0264 
0265     return radioButtons.at(index);
0266 }
0267 
0268 void KonfiguratorRadioButtons::selectButton(const QString &value)
0269 {
0270     int cnt = 0;
0271 
0272     QListIterator<QRadioButton *> it(radioButtons);
0273     while (it.hasNext()) {
0274         QRadioButton *btn = it.next();
0275 
0276         if (value == radioValues[cnt]) {
0277             btn->setChecked(true);
0278             return;
0279         }
0280 
0281         cnt++;
0282     }
0283 
0284     if (!radioButtons.isEmpty())
0285         radioButtons.first()->setChecked(true);
0286 }
0287 
0288 void KonfiguratorRadioButtons::loadInitialValue()
0289 {
0290     KConfigGroup group(krConfig, ext->getConfigGroup());
0291     QString initValue = group.readEntry(ext->getConfigName(), defaultValue);
0292 
0293     selectButton(initValue);
0294     ext->setChanged(false);
0295 }
0296 
0297 QString KonfiguratorRadioButtons::selectedValue()
0298 {
0299     int cnt = 0;
0300 
0301     QListIterator<QRadioButton *> it(radioButtons);
0302     while (it.hasNext()) {
0303         QRadioButton *btn = it.next();
0304 
0305         if (btn->isChecked()) {
0306             return radioValues[cnt];
0307         }
0308 
0309         cnt++;
0310     }
0311     return QString();
0312 }
0313 
0314 void KonfiguratorRadioButtons::slotApply(QObject *, const QString &configGroup, const QString &name)
0315 {
0316     QString value = selectedValue();
0317 
0318     if (!value.isEmpty())
0319         KConfigGroup(krConfig, configGroup).writeEntry(name, value);
0320 }
0321 
0322 void KonfiguratorRadioButtons::slotSetDefaults(QObject *)
0323 {
0324     selectButton(defaultValue);
0325 }
0326 
0327 // KonfiguratorEditBox class
0328 ///////////////////////////////
0329 
0330 KonfiguratorEditBox::KonfiguratorEditBox(QString configGroup, QString name, QString defaultValue, QWidget *parent, bool restart, int page)
0331     : QLineEdit(parent)
0332     , defaultValue(std::move(defaultValue))
0333 {
0334     ext = new KonfiguratorExtension(this, std::move(configGroup), std::move(name), restart, page);
0335     connect(ext, &KonfiguratorExtension::applyAuto, this, &KonfiguratorEditBox::slotApply);
0336     connect(ext, &KonfiguratorExtension::setDefaultsAuto, this, &KonfiguratorEditBox::slotSetDefaults);
0337     connect(ext, &KonfiguratorExtension::setInitialValue, this, &KonfiguratorEditBox::loadInitialValue);
0338 
0339     connect(this, &KonfiguratorEditBox::textChanged, ext, QOverload<>::of(&KonfiguratorExtension::setChanged));
0340 
0341     loadInitialValue();
0342 }
0343 
0344 KonfiguratorEditBox::~KonfiguratorEditBox()
0345 {
0346     delete ext;
0347 }
0348 
0349 void KonfiguratorEditBox::loadInitialValue()
0350 {
0351     KConfigGroup group(krConfig, ext->getConfigGroup());
0352     setText(group.readEntry(ext->getConfigName(), defaultValue));
0353     ext->setChanged(false);
0354 }
0355 
0356 void KonfiguratorEditBox::slotApply(QObject *, const QString &configGroup, const QString &name)
0357 {
0358     KConfigGroup(krConfig, configGroup).writeEntry(name, text());
0359 }
0360 
0361 void KonfiguratorEditBox::slotSetDefaults(QObject *)
0362 {
0363     if (text() != defaultValue)
0364         setText(defaultValue);
0365 }
0366 
0367 // KonfiguratorURLRequester class
0368 ///////////////////////////////
0369 
0370 KonfiguratorURLRequester::KonfiguratorURLRequester(QString configGroup,
0371                                                    QString name,
0372                                                    QString defaultValue,
0373                                                    QWidget *parent,
0374                                                    bool restart,
0375                                                    int page,
0376                                                    bool expansion)
0377     : KUrlRequester(parent)
0378     , defaultValue(std::move(defaultValue))
0379     , expansion(expansion)
0380 {
0381     ext = new KonfiguratorExtension(this, std::move(configGroup), std::move(name), restart, page);
0382     connect(ext, &KonfiguratorExtension::applyAuto, this, &KonfiguratorURLRequester::slotApply);
0383     connect(ext, &KonfiguratorExtension::setDefaultsAuto, this, &KonfiguratorURLRequester::slotSetDefaults);
0384     connect(ext, &KonfiguratorExtension::setInitialValue, this, &KonfiguratorURLRequester::loadInitialValue);
0385 
0386     connect(this, &KonfiguratorURLRequester::textChanged, ext, QOverload<>::of(&KonfiguratorExtension::setChanged));
0387 
0388     loadInitialValue();
0389 }
0390 
0391 KonfiguratorURLRequester::~KonfiguratorURLRequester()
0392 {
0393     delete ext;
0394 }
0395 
0396 void KonfiguratorURLRequester::loadInitialValue()
0397 {
0398     KConfigGroup group(krConfig, ext->getConfigGroup());
0399     lineEdit()->setText(group.readEntry(ext->getConfigName(), defaultValue));
0400     ext->setChanged(false);
0401 }
0402 
0403 void KonfiguratorURLRequester::slotApply(QObject *, const QString &configGroup, const QString &name)
0404 {
0405     KConfigGroup(krConfig, configGroup).writeEntry(name, expansion ? url().toDisplayString(QUrl::PreferLocalFile) : text());
0406 }
0407 
0408 void KonfiguratorURLRequester::slotSetDefaults(QObject *)
0409 {
0410     if (url().toDisplayString(QUrl::PreferLocalFile) != defaultValue)
0411         lineEdit()->setText(defaultValue);
0412 }
0413 
0414 // KonfiguratorFontChooser class
0415 ///////////////////////////////
0416 
0417 KonfiguratorFontChooser::KonfiguratorFontChooser(QString configGroup, QString name, const QFont &defaultValue, QWidget *parent, bool restart, int page)
0418     : QWidget(parent)
0419     , defaultValue(defaultValue)
0420 {
0421     auto *layout = new QHBoxLayout(this);
0422 
0423     ext = new KonfiguratorExtension(this, std::move(configGroup), std::move(name), restart, page);
0424     connect(ext, &KonfiguratorExtension::applyAuto, this, &KonfiguratorFontChooser::slotApply);
0425     connect(ext, &KonfiguratorExtension::setDefaultsAuto, this, &KonfiguratorFontChooser::slotSetDefaults);
0426     connect(ext, &KonfiguratorExtension::setInitialValue, this, &KonfiguratorFontChooser::loadInitialValue);
0427 
0428     pLabel = new QLabel(this);
0429     pLabel->setMinimumWidth(150);
0430     layout->addWidget(pLabel);
0431 
0432     pToolButton = new QToolButton(this);
0433 
0434     connect(pToolButton, &QToolButton::clicked, this, &KonfiguratorFontChooser::slotBrowseFont);
0435 
0436     pToolButton->setIcon(Icon("document-open"));
0437     layout->addWidget(pToolButton);
0438 
0439     loadInitialValue();
0440 }
0441 
0442 KonfiguratorFontChooser::~KonfiguratorFontChooser()
0443 {
0444     delete ext;
0445 }
0446 
0447 void KonfiguratorFontChooser::loadInitialValue()
0448 {
0449     KConfigGroup group(krConfig, ext->getConfigGroup());
0450     font = group.readEntry(ext->getConfigName(), defaultValue);
0451     ext->setChanged(false);
0452     setFont();
0453 }
0454 
0455 void KonfiguratorFontChooser::setFont()
0456 {
0457     pLabel->setFont(font);
0458     pLabel->setText(font.family() + QString(", %1").arg(font.pointSize()));
0459 }
0460 
0461 void KonfiguratorFontChooser::slotApply(QObject *, const QString &configGroup, const QString &name)
0462 {
0463     KConfigGroup(krConfig, configGroup).writeEntry(name, font);
0464 }
0465 
0466 void KonfiguratorFontChooser::slotSetDefaults(QObject *)
0467 {
0468     font = defaultValue;
0469     ext->setChanged();
0470     setFont();
0471 }
0472 
0473 void KonfiguratorFontChooser::slotBrowseFont()
0474 {
0475     bool ok;
0476     font = QFontDialog::getFont(&ok, font, this);
0477     if (!ok)
0478         return; // cancelled by the user, and font is actually not changed (getFont returns the font we gave it)
0479     ext->setChanged();
0480     setFont();
0481 }
0482 
0483 // KonfiguratorComboBox class
0484 ///////////////////////////////
0485 
0486 KonfiguratorComboBox::KonfiguratorComboBox(QString configGroup,
0487                                            QString name,
0488                                            QString defaultValue,
0489                                            KONFIGURATOR_NAME_VALUE_PAIR *listIn,
0490                                            int listInLen,
0491                                            QWidget *parent,
0492                                            bool restart,
0493                                            bool editable,
0494                                            int page)
0495     : QComboBox(parent)
0496     , defaultValue(std::move(defaultValue))
0497     , listLen(listInLen)
0498 {
0499     list = new KONFIGURATOR_NAME_VALUE_PAIR[listInLen];
0500 
0501     for (int i = 0; i != listLen; i++) {
0502         list[i] = listIn[i];
0503         addItem(list[i].text);
0504     }
0505 
0506     ext = new KonfiguratorExtension(this, std::move(configGroup), std::move(name), restart, page);
0507     connect(ext, &KonfiguratorExtension::applyAuto, this, &KonfiguratorComboBox::slotApply);
0508     connect(ext, &KonfiguratorExtension::setDefaultsAuto, this, &KonfiguratorComboBox::slotSetDefaults);
0509     connect(ext, &KonfiguratorExtension::setInitialValue, this, &KonfiguratorComboBox::loadInitialValue);
0510 
0511     // connect(this, &KonfiguratorComboBox::highlighted, ext, &KonfiguratorExtension::setChanged); /* Removed because of startup combo failure */
0512     connect(this, QOverload<int>::of(&KonfiguratorComboBox::activated), ext, QOverload<>::of(&KonfiguratorExtension::setChanged));
0513     connect(this, &KonfiguratorComboBox::currentTextChanged, ext, QOverload<>::of(&KonfiguratorExtension::setChanged));
0514 
0515     setEditable(editable);
0516     loadInitialValue();
0517 }
0518 
0519 KonfiguratorComboBox::~KonfiguratorComboBox()
0520 {
0521     delete[] list;
0522     delete ext;
0523 }
0524 
0525 void KonfiguratorComboBox::loadInitialValue()
0526 {
0527     KConfigGroup group(krConfig, ext->getConfigGroup());
0528     QString select = group.readEntry(ext->getConfigName(), defaultValue);
0529     selectEntry(select);
0530     ext->setChanged(false);
0531 }
0532 
0533 void KonfiguratorComboBox::slotApply(QObject *, const QString &configGroup, const QString &name)
0534 {
0535     QString text = isEditable() ? lineEdit()->text() : currentText();
0536     QString value = text;
0537 
0538     for (int i = 0; i != listLen; i++)
0539         if (list[i].text == text) {
0540             value = list[i].value;
0541             break;
0542         }
0543 
0544     KConfigGroup(krConfig, configGroup).writeEntry(name, value);
0545 }
0546 
0547 void KonfiguratorComboBox::selectEntry(const QString &entry)
0548 {
0549     for (int i = 0; i != listLen; i++)
0550         if (list[i].value == entry) {
0551             setCurrentIndex(i);
0552             return;
0553         }
0554 
0555     if (isEditable())
0556         lineEdit()->setText(entry);
0557     else
0558         setCurrentIndex(0);
0559 }
0560 
0561 void KonfiguratorComboBox::slotSetDefaults(QObject *)
0562 {
0563     selectEntry(defaultValue);
0564 }
0565 
0566 // KonfiguratorColorChooser class
0567 ///////////////////////////////
0568 
0569 KonfiguratorColorChooser::KonfiguratorColorChooser(QString configGroup,
0570                                                    QString name,
0571                                                    const QColor &defaultValue,
0572                                                    QWidget *parent,
0573                                                    bool restart,
0574                                                    ADDITIONAL_COLOR *addColPtr,
0575                                                    int addColNum,
0576                                                    int page)
0577     : QComboBox(parent)
0578     , defaultValue(defaultValue)
0579     , disableColorChooser(true)
0580 {
0581     ext = new KonfiguratorExtension(this, std::move(configGroup), std::move(name), restart, page);
0582 
0583     connect(ext, &KonfiguratorExtension::applyAuto, this, &KonfiguratorColorChooser::slotApply);
0584     connect(ext, &KonfiguratorExtension::setDefaultsAuto, this, &KonfiguratorColorChooser::slotSetDefaults);
0585     connect(ext, &KonfiguratorExtension::setInitialValue, this, &KonfiguratorColorChooser::loadInitialValue);
0586 
0587     addColor(i18n("Custom color"), QColor(255, 255, 255));
0588     addColor(i18nc("Default color", "Default"), defaultValue);
0589 
0590     for (int i = 0; i != addColNum; i++) {
0591         additionalColors.push_back(addColPtr[i]);
0592         addColor(addColPtr[i].name, addColPtr[i].color);
0593     }
0594 
0595     addColor(i18n("Red"), Qt::red);
0596     addColor(i18n("Green"), Qt::green);
0597     addColor(i18n("Blue"), Qt::blue);
0598     addColor(i18n("Cyan"), Qt::cyan);
0599     addColor(i18n("Magenta"), Qt::magenta);
0600     addColor(i18n("Yellow"), Qt::yellow);
0601     addColor(i18n("Dark Red"), Qt::darkRed);
0602     addColor(i18n("Dark Green"), Qt::darkGreen);
0603     addColor(i18n("Dark Blue"), Qt::darkBlue);
0604     addColor(i18n("Dark Cyan"), Qt::darkCyan);
0605     addColor(i18n("Dark Magenta"), Qt::darkMagenta);
0606     addColor(i18n("Dark Yellow"), Qt::darkYellow);
0607     addColor(i18n("White"), Qt::white);
0608     addColor(i18n("Light Gray"), Qt::lightGray);
0609     addColor(i18n("Gray"), Qt::gray);
0610     addColor(i18n("Dark Gray"), Qt::darkGray);
0611     addColor(i18n("Black"), Qt::black);
0612 
0613     connect(this, QOverload<int>::of(&KonfiguratorColorChooser::activated), this, &KonfiguratorColorChooser::slotCurrentChanged);
0614 
0615     loadInitialValue();
0616 }
0617 
0618 KonfiguratorColorChooser::~KonfiguratorColorChooser()
0619 {
0620     delete ext;
0621 }
0622 
0623 QPixmap KonfiguratorColorChooser::createPixmap(const QColor &color)
0624 {
0625     QPainter painter;
0626     QPen pen;
0627     int size = QFontMetrics(font()).height() * 3 / 4;
0628     QRect rect(0, 0, size, size);
0629     QPixmap pixmap(rect.width(), rect.height());
0630 
0631     pen.setColor(Qt::black);
0632 
0633     painter.begin(&pixmap);
0634     QBrush brush(color);
0635     painter.fillRect(rect, brush);
0636     painter.setPen(pen);
0637     painter.drawRect(rect);
0638     painter.end();
0639 
0640     pixmap.detach();
0641     return pixmap;
0642 }
0643 
0644 void KonfiguratorColorChooser::addColor(const QString &text, const QColor &color)
0645 {
0646     addItem(createPixmap(color), text);
0647     palette.push_back(color);
0648 }
0649 
0650 void KonfiguratorColorChooser::loadInitialValue()
0651 {
0652     KConfigGroup group(krConfig, ext->getConfigGroup());
0653     QString selected = group.readEntry(ext->getConfigName(), QString(""));
0654     setValue(selected);
0655     ext->setChanged(false);
0656 }
0657 
0658 void KonfiguratorColorChooser::setDefaultColor(QColor dflt)
0659 {
0660     defaultValue = std::move(dflt);
0661     palette[1] = defaultValue;
0662     setItemIcon(1, createPixmap(defaultValue));
0663 
0664     if (currentIndex() == 1)
0665         emit colorChanged();
0666 }
0667 
0668 void KonfiguratorColorChooser::changeAdditionalColor(int num, const QColor &color)
0669 {
0670     if (num < additionalColors.size()) {
0671         palette[2 + num] = color;
0672         additionalColors[num].color = color;
0673         setItemIcon(2 + num, createPixmap(color));
0674 
0675         if (currentIndex() == 2 + num)
0676             emit colorChanged();
0677     }
0678 }
0679 
0680 void KonfiguratorColorChooser::setDefaultText(const QString &text)
0681 {
0682     setItemIcon(1, createPixmap(defaultValue));
0683     setItemText(1, text);
0684 }
0685 
0686 void KonfiguratorColorChooser::slotApply(QObject *, const QString &configGroup, const QString &name)
0687 {
0688     KConfigGroup(krConfig, configGroup).writeEntry(name, getValue());
0689 }
0690 
0691 void KonfiguratorColorChooser::setValue(const QString &value)
0692 {
0693     disableColorChooser = true;
0694 
0695     if (value.isEmpty()) {
0696         setCurrentIndex(1);
0697         customValue = defaultValue;
0698     } else {
0699         bool found = false;
0700 
0701         for (int j = 0; j != additionalColors.size(); j++)
0702             if (additionalColors[j].value == value) {
0703                 setCurrentIndex(2 + j);
0704                 found = true;
0705                 break;
0706             }
0707 
0708         if (!found) {
0709             KConfigGroup colGroup(krConfig, ext->getConfigGroup());
0710             colGroup.writeEntry("TmpColor", value);
0711             QColor color = colGroup.readEntry("TmpColor", defaultValue);
0712             customValue = color;
0713             colGroup.deleteEntry("TmpColor");
0714 
0715             setCurrentIndex(0);
0716             for (int i = 2 + additionalColors.size(); i != palette.size(); i++)
0717                 if (palette[i] == color) {
0718                     setCurrentIndex(i);
0719                     break;
0720                 }
0721         }
0722     }
0723 
0724     palette[0] = customValue;
0725     setItemIcon(0, createPixmap(customValue));
0726 
0727     ext->setChanged();
0728     emit colorChanged();
0729     disableColorChooser = false;
0730 }
0731 
0732 QString KonfiguratorColorChooser::getValue()
0733 {
0734     QColor color = palette[currentIndex()];
0735     if (currentIndex() == 1) /* it's the default value? */
0736         return "";
0737     else if (currentIndex() >= 2 && currentIndex() < 2 + additionalColors.size())
0738         return additionalColors[currentIndex() - 2].value;
0739     else
0740         return QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue());
0741 }
0742 
0743 bool KonfiguratorColorChooser::isValueRGB()
0744 {
0745     return !(currentIndex() >= 1 && currentIndex() < 2 + additionalColors.size());
0746 }
0747 
0748 void KonfiguratorColorChooser::slotSetDefaults(QObject *)
0749 {
0750     ext->setChanged();
0751     setCurrentIndex(1);
0752     emit colorChanged();
0753 }
0754 
0755 void KonfiguratorColorChooser::slotCurrentChanged(int number)
0756 {
0757     ext->setChanged();
0758     if (number == 0 && !disableColorChooser) {
0759         QColor color = QColorDialog::getColor(customValue, this);
0760         if (color.isValid()) {
0761             disableColorChooser = true;
0762             customValue = color;
0763             palette[0] = customValue;
0764             setItemIcon(0, createPixmap(customValue));
0765             disableColorChooser = false;
0766         }
0767     }
0768 
0769     emit colorChanged();
0770 }
0771 
0772 QColor KonfiguratorColorChooser::getColor()
0773 {
0774     return palette[currentIndex()];
0775 }
0776 
0777 // KonfiguratorListBox class
0778 ///////////////////////////////
0779 
0780 KonfiguratorListBox::KonfiguratorListBox(QString configGroup, QString name, QStringList defaultValue, QWidget *parent, bool restart, int page)
0781     : KrListWidget(parent)
0782     , defaultValue(std::move(defaultValue))
0783 {
0784     ext = new KonfiguratorExtension(this, std::move(configGroup), std::move(name), restart, page);
0785     connect(ext, &KonfiguratorExtension::applyAuto, this, &KonfiguratorListBox::slotApply);
0786     connect(ext, &KonfiguratorExtension::setDefaultsAuto, this, &KonfiguratorListBox::slotSetDefaults);
0787     connect(ext, &KonfiguratorExtension::setInitialValue, this, &KonfiguratorListBox::loadInitialValue);
0788 
0789     loadInitialValue();
0790 }
0791 
0792 KonfiguratorListBox::~KonfiguratorListBox()
0793 {
0794     delete ext;
0795 }
0796 
0797 void KonfiguratorListBox::loadInitialValue()
0798 {
0799     KConfigGroup group(krConfig, ext->getConfigGroup());
0800     setList(group.readEntry(ext->getConfigName(), defaultValue));
0801     ext->setChanged(false);
0802 }
0803 
0804 void KonfiguratorListBox::slotApply(QObject *, const QString &configGroup, const QString &name)
0805 {
0806     KConfigGroup(krConfig, configGroup).writeEntry(name, list());
0807 }
0808 
0809 void KonfiguratorListBox::slotSetDefaults(QObject *)
0810 {
0811     if (list() != defaultValue) {
0812         ext->setChanged();
0813         setList(defaultValue);
0814     }
0815 }
0816 
0817 void KonfiguratorListBox::setList(const QStringList &list)
0818 {
0819     clear();
0820     addItems(list);
0821 }
0822 
0823 QStringList KonfiguratorListBox::list()
0824 {
0825     QStringList lst;
0826 
0827     for (int i = 0; i != count(); i++)
0828         lst += item(i)->text();
0829 
0830     return lst;
0831 }
0832 
0833 void KonfiguratorListBox::addItem(const QString &item)
0834 {
0835     if (!list().contains(item)) {
0836         KrListWidget::addItem(item);
0837         ext->setChanged();
0838     }
0839 }
0840 
0841 void KonfiguratorListBox::removeItem(const QString &item)
0842 {
0843     QList<QListWidgetItem *> list = findItems(item, Qt::MatchExactly);
0844     for (int i = 0; i != list.count(); i++)
0845         delete list[i];
0846 
0847     if (list.count())
0848         ext->setChanged();
0849 }