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

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 "konfiguratorpage.h"
0009 
0010 // QtWidgets
0011 #include <QGridLayout>
0012 #include <QHBoxLayout>
0013 #include <QLabel>
0014 #include <QLayout>
0015 #include <QScrollArea>
0016 
0017 #include <KConfigCore/KConfig>
0018 #include <utility>
0019 
0020 #include "../krglobal.h"
0021 
0022 KonfiguratorPage::KonfiguratorPage(bool firstTime, QWidget *parent)
0023     : QScrollArea(parent)
0024     , firstCall(firstTime)
0025 {
0026     setFrameStyle(QFrame::NoFrame);
0027 }
0028 
0029 bool KonfiguratorPage::apply()
0030 {
0031     bool restartNeeded = false;
0032 
0033     for (auto &item : itemList)
0034         restartNeeded = item->apply() || restartNeeded;
0035 
0036     krConfig->sync();
0037     return restartNeeded;
0038 }
0039 
0040 void KonfiguratorPage::setDefaults()
0041 {
0042     int activePage = activeSubPage();
0043 
0044     for (auto &item : itemList) {
0045         if (item->subPage() == activePage)
0046             item->setDefaults();
0047     }
0048 }
0049 
0050 void KonfiguratorPage::loadInitialValues()
0051 {
0052     for (auto &item : itemList)
0053         item->loadInitialValue();
0054 }
0055 
0056 bool KonfiguratorPage::isChanged()
0057 {
0058     bool isChanged = false;
0059 
0060     for (auto &item : itemList)
0061         isChanged = isChanged || item->isChanged();
0062 
0063     return isChanged;
0064 }
0065 
0066 KonfiguratorCheckBox *KonfiguratorPage::createCheckBox(QString configGroup,
0067                                                        QString name,
0068                                                        bool defaultValue,
0069                                                        QString text,
0070                                                        QWidget *parent,
0071                                                        bool restart,
0072                                                        const QString &toolTip,
0073                                                        int page)
0074 {
0075     KonfiguratorCheckBox *checkBox = new KonfiguratorCheckBox(std::move(configGroup), std::move(name), defaultValue, std::move(text), parent, restart, page);
0076     if (!toolTip.isEmpty()) {
0077         checkBox->setWhatsThis(toolTip);
0078         checkBox->setToolTip(toolTip);
0079     }
0080 
0081     registerObject(checkBox->extension());
0082     return checkBox;
0083 }
0084 
0085 KonfiguratorSpinBox *KonfiguratorPage::createSpinBox(QString configGroup,
0086                                                      QString configName,
0087                                                      int defaultValue,
0088                                                      int min,
0089                                                      int max,
0090                                                      QLabel *label,
0091                                                      QWidget *parent,
0092                                                      bool restart,
0093                                                      const QString &toolTip,
0094                                                      int page)
0095 {
0096     KonfiguratorSpinBox *spinBox = new KonfiguratorSpinBox(std::move(configGroup), std::move(configName), defaultValue, min, max, parent, restart, page);
0097     if (!toolTip.isEmpty()) {
0098         label->setWhatsThis(toolTip);
0099         label->setToolTip(toolTip);
0100         spinBox->setWhatsThis(toolTip);
0101         spinBox->setToolTip(toolTip);
0102     }
0103 
0104     registerObject(spinBox->extension());
0105     return spinBox;
0106 }
0107 
0108 KonfiguratorEditBox *KonfiguratorPage::createEditBox(QString configGroup,
0109                                                      QString name,
0110                                                      QString defaultValue,
0111                                                      QLabel *label,
0112                                                      QWidget *parent,
0113                                                      bool restart,
0114                                                      const QString &toolTip,
0115                                                      int page)
0116 {
0117     KonfiguratorEditBox *editBox = new KonfiguratorEditBox(std::move(configGroup), std::move(name), std::move(defaultValue), parent, restart, page);
0118     if (!toolTip.isEmpty()) {
0119         label->setWhatsThis(toolTip);
0120         label->setToolTip(toolTip);
0121         editBox->setWhatsThis(toolTip);
0122         editBox->setToolTip(toolTip);
0123     }
0124 
0125     registerObject(editBox->extension());
0126     return editBox;
0127 }
0128 
0129 KonfiguratorListBox *
0130 KonfiguratorPage::createListBox(QString configGroup, QString name, QStringList defaultValue, QWidget *parent, bool restart, const QString &toolTip, int page)
0131 {
0132     KonfiguratorListBox *listBox = new KonfiguratorListBox(std::move(configGroup), std::move(name), std::move(defaultValue), parent, restart, page);
0133     if (!toolTip.isEmpty()) {
0134         listBox->setWhatsThis(toolTip);
0135         listBox->setToolTip(toolTip);
0136     }
0137 
0138     registerObject(listBox->extension());
0139     return listBox;
0140 }
0141 
0142 KonfiguratorURLRequester *KonfiguratorPage::createURLRequester(QString configGroup,
0143                                                                QString name,
0144                                                                QString defaultValue,
0145                                                                QLabel *label,
0146                                                                QWidget *parent,
0147                                                                bool restart,
0148                                                                const QString &toolTip,
0149                                                                int page,
0150                                                                bool expansion)
0151 {
0152     KonfiguratorURLRequester *urlRequester =
0153         new KonfiguratorURLRequester(std::move(configGroup), std::move(name), std::move(defaultValue), parent, restart, page, expansion);
0154     if (!toolTip.isEmpty()) {
0155         label->setWhatsThis(toolTip);
0156         label->setToolTip(toolTip);
0157         urlRequester->setWhatsThis(toolTip);
0158         urlRequester->setToolTip(toolTip);
0159     }
0160 
0161     registerObject(urlRequester->extension());
0162     return urlRequester;
0163 }
0164 
0165 QGroupBox *KonfiguratorPage::createFrame(const QString &text, QWidget *parent)
0166 {
0167     auto *groupBox = new QGroupBox(parent);
0168     if (!text.isNull())
0169         groupBox->setTitle(text);
0170     return groupBox;
0171 }
0172 
0173 QGridLayout *KonfiguratorPage::createGridLayout(QWidget *parent)
0174 {
0175     auto *gridLayout = new QGridLayout(parent);
0176     gridLayout->setAlignment(Qt::AlignTop);
0177     gridLayout->setSpacing(6);
0178     gridLayout->setContentsMargins(11, 11, 11, 11);
0179     return gridLayout;
0180 }
0181 
0182 QLabel *KonfiguratorPage::addLabel(QGridLayout *layout, int x, int y, const QString &label, QWidget *parent)
0183 {
0184     QLabel *lbl = new QLabel(label, parent);
0185     layout->addWidget(lbl, x, y);
0186     return lbl;
0187 }
0188 
0189 QWidget *KonfiguratorPage::createSpacer(QWidget *parent)
0190 {
0191     QWidget *widget = new QWidget(parent);
0192     auto *hboxlayout = new QHBoxLayout(widget);
0193     auto *spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
0194     hboxlayout->addItem(spacer);
0195     return widget;
0196 }
0197 
0198 KonfiguratorCheckBoxGroup *
0199 KonfiguratorPage::createCheckBoxGroup(int sizex, int sizey, KONFIGURATOR_CHECKBOX_PARAM *params, int paramNum, QWidget *parent, int page)
0200 {
0201     auto *groupWidget = new KonfiguratorCheckBoxGroup(parent);
0202     auto *layout = new QGridLayout(groupWidget);
0203     layout->setSpacing(6);
0204     layout->setContentsMargins(0, 0, 0, 0);
0205 
0206     int x = 0, y = 0;
0207 
0208     for (int i = 0; i != paramNum; i++) {
0209         KonfiguratorCheckBox *checkBox = createCheckBox(params[i].configClass,
0210                                                         params[i].configName,
0211                                                         params[i].defaultValue,
0212                                                         params[i].text,
0213                                                         groupWidget,
0214                                                         params[i].restart,
0215                                                         params[i].toolTip,
0216                                                         page);
0217 
0218         groupWidget->add(checkBox);
0219         layout->addWidget(checkBox, y, x);
0220 
0221         if (sizex) {
0222             if (++x == sizex)
0223                 x = 0, y++;
0224         } else {
0225             if (++y == sizey)
0226                 y = 0, x++;
0227         }
0228     }
0229 
0230     return groupWidget;
0231 }
0232 
0233 KonfiguratorRadioButtons *KonfiguratorPage::createRadioButtonGroup(QString configGroup,
0234                                                                    QString name,
0235                                                                    QString defaultValue,
0236                                                                    int sizex,
0237                                                                    int sizey,
0238                                                                    KONFIGURATOR_NAME_VALUE_TIP *params,
0239                                                                    int paramNum,
0240                                                                    QWidget *parent,
0241                                                                    bool restart,
0242                                                                    int page)
0243 {
0244     KonfiguratorRadioButtons *radioWidget =
0245         new KonfiguratorRadioButtons(std::move(configGroup), std::move(name), std::move(defaultValue), parent, restart, page);
0246 
0247     auto *layout = new QGridLayout(radioWidget);
0248     layout->setAlignment(Qt::AlignTop);
0249     layout->setSpacing(6);
0250     layout->setContentsMargins(0, 0, 0, 0);
0251 
0252     int x = 0, y = 0;
0253 
0254     for (int i = 0; i != paramNum; i++) {
0255         auto *radBtn = new QRadioButton(params[i].text, radioWidget);
0256 
0257         if (!params[i].tooltip.isEmpty()) {
0258             radBtn->setWhatsThis(params[i].tooltip);
0259             radBtn->setToolTip(params[i].tooltip);
0260         }
0261 
0262         layout->addWidget(radBtn, y, x);
0263 
0264         radioWidget->addRadioButton(radBtn, params[i].text, params[i].value);
0265 
0266         if (sizex) {
0267             if (++x == sizex)
0268                 x = 0, y++;
0269         } else {
0270             if (++y == sizey)
0271                 y = 0, x++;
0272         }
0273     }
0274 
0275     radioWidget->loadInitialValue();
0276     registerObject(radioWidget->extension());
0277     return radioWidget;
0278 }
0279 
0280 KonfiguratorFontChooser *KonfiguratorPage::createFontChooser(QString configGroup,
0281                                                              QString name,
0282                                                              const QFont &defaultValue,
0283                                                              QLabel *label,
0284                                                              QWidget *parent,
0285                                                              bool restart,
0286                                                              const QString &toolTip,
0287                                                              int page)
0288 {
0289     KonfiguratorFontChooser *fontChooser = new KonfiguratorFontChooser(std::move(configGroup), std::move(name), defaultValue, parent, restart, page);
0290     if (!toolTip.isEmpty()) {
0291         label->setWhatsThis(toolTip);
0292         label->setToolTip(toolTip);
0293         fontChooser->setWhatsThis(toolTip);
0294         fontChooser->setToolTip(toolTip);
0295     }
0296 
0297     registerObject(fontChooser->extension());
0298     return fontChooser;
0299 }
0300 
0301 KonfiguratorComboBox *KonfiguratorPage::createComboBox(QString configGroup,
0302                                                        QString name,
0303                                                        QString defaultValue,
0304                                                        KONFIGURATOR_NAME_VALUE_PAIR *params,
0305                                                        int paramNum,
0306                                                        QLabel *label,
0307                                                        QWidget *parent,
0308                                                        bool restart,
0309                                                        bool editable,
0310                                                        const QString &toolTip,
0311                                                        int page)
0312 {
0313     KonfiguratorComboBox *comboBox =
0314         new KonfiguratorComboBox(std::move(configGroup), std::move(name), std::move(defaultValue), params, paramNum, parent, restart, editable, page);
0315     if (!toolTip.isEmpty()) {
0316         label->setWhatsThis(toolTip);
0317         label->setToolTip(toolTip);
0318         comboBox->setWhatsThis(toolTip);
0319         comboBox->setToolTip(toolTip);
0320     }
0321 
0322     registerObject(comboBox->extension());
0323     return comboBox;
0324 }
0325 
0326 QFrame *KonfiguratorPage::createLine(QWidget *parent, bool vertical)
0327 {
0328     QFrame *line = new QFrame(parent);
0329     line->setFrameStyle((vertical ? QFrame::VLine : QFrame::HLine) | QFrame::Sunken);
0330     return line;
0331 }
0332 
0333 void KonfiguratorPage::registerObject(KonfiguratorExtension *item)
0334 {
0335     itemList.push_back(item);
0336     connect(item, SIGNAL(sigChanged(bool)), this, SIGNAL(sigChanged()));
0337 }
0338 
0339 void KonfiguratorPage::removeObject(KonfiguratorExtension *item)
0340 {
0341     int ndx = itemList.indexOf(item);
0342     if (ndx != -1) {
0343         QList<KonfiguratorExtension *>::iterator it = itemList.begin() + ndx;
0344         delete *it;
0345         itemList.erase(it);
0346     }
0347 }
0348 
0349 KonfiguratorColorChooser *KonfiguratorPage::createColorChooser(QString configGroup,
0350                                                                QString name,
0351                                                                QColor defaultValue,
0352                                                                QWidget *parent,
0353                                                                bool restart,
0354                                                                ADDITIONAL_COLOR *addColPtr,
0355                                                                int addColNum,
0356                                                                int page)
0357 {
0358     KonfiguratorColorChooser *colorChooser =
0359         new KonfiguratorColorChooser(std::move(configGroup), std::move(name), std::move(defaultValue), parent, restart, addColPtr, addColNum, page);
0360 
0361     registerObject(colorChooser->extension());
0362     return colorChooser;
0363 }