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

0001 /*
0002     SPDX-FileCopyrightText: 2004 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 "kgcolors.h"
0009 #include "../Panel/krcolorcache.h"
0010 #include "../Synchronizer/synchronizercolors.h"
0011 #include "../defaults.h"
0012 
0013 // QtCore
0014 #include <QList>
0015 #include <QStandardPaths>
0016 // QtGui
0017 #include <QGuiApplication>
0018 // QtWidgets
0019 #include <QFileDialog>
0020 #include <QGridLayout>
0021 #include <QHeaderView>
0022 #include <QLabel>
0023 #include <QTabWidget>
0024 
0025 #include <KI18n/KLocalizedString>
0026 #include <KWidgetsAddons/KMessageBox>
0027 #include <utility>
0028 
0029 KgColors::KgColors(bool first, QWidget *parent)
0030     : KonfiguratorPage(first, parent)
0031     , offset(0)
0032     , activeTabIdx(-1)
0033     , inactiveTabIdx(-1)
0034     ,
0035 #ifdef SYNCHRONIZER_ENABLED
0036     synchronizerTabIdx(-1)
0037     ,
0038 #endif
0039     otherTabIdx(-1)
0040 {
0041     QWidget *innerWidget = new QFrame(this);
0042     setWidget(innerWidget);
0043     setWidgetResizable(true);
0044     auto *kgColorsLayout = new QGridLayout(innerWidget);
0045     kgColorsLayout->setSpacing(6);
0046 
0047     //  -------------------------- GENERAL GROUPBOX ----------------------------------
0048 
0049     QGroupBox *generalGrp = createFrame(i18n("General"), innerWidget);
0050     QGridLayout *generalGrid = createGridLayout(generalGrp);
0051 
0052     generalGrid->setSpacing(0);
0053     generalGrid->setContentsMargins(5, 5, 5, 5);
0054 
0055     KONFIGURATOR_CHECKBOX_PARAM generalSettings[] =
0056         //  cfg_class   cfg_name    default     text    restart    tooltip
0057         {{"Colors",
0058           "KDE Default",
0059           _KDEDefaultColors,
0060           i18n("Use the default KDE colors"),
0061           false,
0062           "<p><img src='toolbar|kcontrol'></p>"
0063               + i18n("<p>Use KDE's global color configuration.</p><p><i>KDE System Settings -> Application Appearance  -> Colors</i></p>")},
0064          {"Colors",
0065           "Enable Alternate Background",
0066           _AlternateBackground,
0067           i18n("Use alternate background color"),
0068           false,
0069           i18n("<p>The <b>background color</b> and the <b>alternate background</b> color alternates line by line.</p><p>When you don't use the <i>KDE default "
0070                "colors</i>, you can configure the alternate colors in the <i>colors</i> box.</p>")},
0071          {"Colors",
0072           "Show Current Item Always",
0073           _ShowCurrentItemAlways,
0074           i18n("Show current item even if not focused"),
0075           false,
0076           i18n("<p>Shows the last cursor position in the non active list panel.</p><p>This option is only available when you don't use the <i>KDE default "
0077                "colors</i>.</p>")},
0078          {"Colors",
0079           "Dim Inactive Colors",
0080           _DimInactiveColors,
0081           i18n("Dim the colors of the inactive panel"),
0082           false,
0083           i18n("<p>The colors of the inactive panel are calculated by a dim color and a dim factor.</p>")}};
0084 
0085     generals = createCheckBoxGroup(0, 2, generalSettings, sizeof(generalSettings) / sizeof(generalSettings[0]), generalGrp);
0086     generalGrid->addWidget(generals, 1, 0);
0087 
0088     generals->layout()->setSpacing(5);
0089 
0090     connect(generals->find("KDE Default"), &KonfiguratorCheckBox::stateChanged, this, &KgColors::slotDisable);
0091     connect(generals->find("Show Current Item Always"), &KonfiguratorCheckBox::stateChanged, this, &KgColors::slotDisable);
0092     connect(generals->find("Dim Inactive Colors"), &KonfiguratorCheckBox::stateChanged, this, &KgColors::slotDisable);
0093 
0094     kgColorsLayout->addWidget(generalGrp, 0, 0, 1, 3);
0095     QWidget *hboxWidget = new QWidget(innerWidget);
0096     auto *hbox = new QHBoxLayout(hboxWidget);
0097 
0098     //  -------------------------- COLORS GROUPBOX ----------------------------------
0099 
0100     QGroupBox *colorsFrameGrp = createFrame(i18n("Colors"), hboxWidget);
0101     QGridLayout *colorsFrameGrid = createGridLayout(colorsFrameGrp);
0102     colorsFrameGrid->setSpacing(0);
0103     colorsFrameGrid->setContentsMargins(3, 3, 3, 3);
0104 
0105     colorTabWidget = new QTabWidget(colorsFrameGrp);
0106 
0107     colorsGrp = new QWidget(colorTabWidget);
0108     activeTabIdx = colorTabWidget->addTab(colorsGrp, i18n("Active"));
0109 
0110     colorsGrid = new QGridLayout(colorsGrp);
0111     colorsGrid->setSpacing(0);
0112     colorsGrid->setContentsMargins(2, 2, 2, 2);
0113 
0114     ADDITIONAL_COLOR transparent = {i18n("Transparent"), Qt::white, "transparent"};
0115 
0116     QPalette p = QGuiApplication::palette();
0117 
0118     addColorSelector("Foreground", i18n("Foreground:"), p.color(QPalette::Active, QPalette::Text));
0119     addColorSelector("Directory Foreground", i18n("Folder foreground:"), getColorSelector("Foreground")->getColor(), i18n("Same as foreground"));
0120     addColorSelector("Executable Foreground", i18n("Executable foreground:"), getColorSelector("Foreground")->getColor(), i18n("Same as foreground"));
0121     addColorSelector("Symlink Foreground", i18n("Symbolic link foreground:"), getColorSelector("Foreground")->getColor(), i18n("Same as foreground"));
0122     addColorSelector("Invalid Symlink Foreground", i18n("Invalid symlink foreground:"), getColorSelector("Foreground")->getColor(), i18n("Same as foreground"));
0123     addColorSelector("Background", i18n("Background:"), p.color(QPalette::Active, QPalette::Base));
0124     ADDITIONAL_COLOR sameAsBckgnd = {i18n("Same as background"), getColorSelector("Background")->getColor(), "Background"};
0125     addColorSelector("Alternate Background", i18n("Alternate background:"), p.color(QPalette::Active, QPalette::AlternateBase), "", &sameAsBckgnd, 1);
0126     addColorSelector("Marked Foreground", i18n("Selected foreground:"), p.color(QPalette::Active, QPalette::HighlightedText), "", &transparent, 1);
0127     addColorSelector("Marked Background", i18n("Selected background:"), p.color(QPalette::Active, QPalette::Highlight), "", &sameAsBckgnd, 1);
0128     ADDITIONAL_COLOR sameAsAltern = {i18n("Same as alt. background"), getColorSelector("Alternate Background")->getColor(), "Alternate Background"};
0129     addColorSelector("Alternate Marked Background",
0130                      i18n("Alternate selected background:"),
0131                      getColorSelector("Marked Background")->getColor(),
0132                      i18n("Same as selected background"),
0133                      &sameAsAltern,
0134                      1);
0135     addColorSelector("Current Foreground", i18n("Current foreground:"), Qt::white, i18n("Not used"));
0136     ADDITIONAL_COLOR sameAsMarkedForegnd = {i18n("Same as selected foreground"), getColorSelector("Marked Foreground")->getColor(), "Marked Foreground"};
0137     addColorSelector("Marked Current Foreground", i18n("Selected current foreground:"), Qt::white, i18n("Not used"), &sameAsMarkedForegnd, 1);
0138     addColorSelector("Current Background", i18n("Current background:"), Qt::white, i18n("Not used"), &sameAsBckgnd, 1);
0139     addColorSelector("Rename Foreground", i18n("Rename foreground:"), getColorSelector("Foreground")->getColor(), i18n("Same as foreground"));
0140     addColorSelector("Rename Background", i18n("Rename background:"), getColorSelector("Background")->getColor(), i18n("Same as background"));
0141 
0142     colorsGrid->addWidget(createSpacer(colorsGrp), itemList.count() - offset, 1);
0143 
0144     connect(getColorSelector("Foreground"), &KonfiguratorColorChooser::colorChanged, this, &KgColors::slotForegroundChanged);
0145     connect(getColorSelector("Background"), &KonfiguratorColorChooser::colorChanged, this, &KgColors::slotBackgroundChanged);
0146     connect(getColorSelector("Alternate Background"), &KonfiguratorColorChooser::colorChanged, this, &KgColors::slotAltBackgroundChanged);
0147     connect(getColorSelector("Marked Background"), &KonfiguratorColorChooser::colorChanged, this, &KgColors::slotMarkedBackgroundChanged);
0148 
0149     inactiveColorStack = new QStackedWidget(colorTabWidget);
0150     inactiveTabIdx = colorTabWidget->addTab(inactiveColorStack, i18n("Inactive"));
0151 
0152     colorsGrp = normalInactiveWidget = new QWidget(inactiveColorStack);
0153 
0154     colorsGrid = new QGridLayout(normalInactiveWidget);
0155     colorsGrid->setSpacing(0);
0156     colorsGrid->setContentsMargins(2, 2, 2, 2);
0157 
0158     offset = endOfActiveColors = itemList.count();
0159 
0160     addColorSelector("Inactive Foreground", i18n("Foreground:"), getColorSelector("Foreground")->getColor(), i18n("Same as active"));
0161     ADDITIONAL_COLOR sameAsInactForegnd = {i18n("Same as foreground"), getColorSelector("Inactive Foreground")->getColor(), "Inactive Foreground"};
0162     addColorSelector("Inactive Directory Foreground",
0163                      i18n("Folder foreground:"),
0164                      getColorSelector("Directory Foreground")->getColor(),
0165                      i18n("Same as active"),
0166                      &sameAsInactForegnd,
0167                      1);
0168     addColorSelector("Inactive Executable Foreground",
0169                      i18n("Executable foreground:"),
0170                      getColorSelector("Executable Foreground")->getColor(),
0171                      i18n("Same as active"),
0172                      &sameAsInactForegnd,
0173                      1);
0174     addColorSelector("Inactive Symlink Foreground",
0175                      i18n("Symbolic link foreground:"),
0176                      getColorSelector("Symlink Foreground")->getColor(),
0177                      i18n("Same as active"),
0178                      &sameAsInactForegnd,
0179                      1);
0180     addColorSelector("Inactive Invalid Symlink Foreground",
0181                      i18n("Invalid symlink foreground:"),
0182                      getColorSelector("Invalid Symlink Foreground")->getColor(),
0183                      i18n("Same as active"),
0184                      &sameAsInactForegnd,
0185                      1);
0186     addColorSelector("Inactive Background", i18n("Background:"), getColorSelector("Background")->getColor(), i18n("Same as active"));
0187     ADDITIONAL_COLOR sameAsInactBckgnd = {i18n("Same as background"), getColorSelector("Inactive Background")->getColor(), "Inactive Background"};
0188     addColorSelector("Inactive Alternate Background",
0189                      i18n("Alternate background:"),
0190                      getColorSelector("Alternate Background")->getColor(),
0191                      i18n("Same as active"),
0192                      &sameAsInactBckgnd,
0193                      1);
0194     addColorSelector("Inactive Marked Foreground",
0195                      i18n("Selected foreground:"),
0196                      getColorSelector("Marked Foreground")->getColor(),
0197                      i18n("Same as active"),
0198                      &transparent,
0199                      1);
0200     addColorSelector("Inactive Marked Background",
0201                      i18n("Selected background:"),
0202                      getColorSelector("Marked Background")->getColor(),
0203                      i18n("Same as active"),
0204                      &sameAsInactBckgnd,
0205                      1);
0206     ADDITIONAL_COLOR sameAsInactAltern[] = {
0207         {i18n("Same as alt. background"), getColorSelector("Inactive Alternate Background")->getColor(), "Inactive Alternate Background"},
0208         {i18n("Same as selected background"), getColorSelector("Inactive Marked Background")->getColor(), "Inactive Marked Background"}};
0209     addColorSelector("Inactive Alternate Marked Background",
0210                      i18n("Alternate selected background:"),
0211                      getColorSelector("Alternate Marked Background")->getColor(),
0212                      i18n("Same as active"),
0213                      sameAsInactAltern,
0214                      2);
0215     addColorSelector("Inactive Current Foreground", i18n("Current foreground:"), getColorSelector("Current Foreground")->getColor(), i18n("Same as active"));
0216     ADDITIONAL_COLOR sameAsInactMarkedForegnd = {i18n("Same as selected foreground"),
0217                                                  getColorSelector("Inactive Marked Foreground")->getColor(),
0218                                                  "Inactive Marked Foreground"};
0219     addColorSelector("Inactive Marked Current Foreground",
0220                      i18n("Selected current foreground:"),
0221                      getColorSelector("Marked Current Foreground")->getColor(),
0222                      i18n("Same as active"),
0223                      &sameAsInactMarkedForegnd,
0224                      1);
0225     addColorSelector("Inactive Current Background",
0226                      i18n("Current background:"),
0227                      getColorSelector("Current Background")->getColor(),
0228                      i18n("Same as active"),
0229                      &sameAsInactBckgnd,
0230                      1);
0231 
0232     colorsGrid->addWidget(createSpacer(normalInactiveWidget), itemList.count() - offset, 1);
0233 
0234     connect(getColorSelector("Inactive Foreground"), &KonfiguratorColorChooser::colorChanged, this, &KgColors::slotInactiveForegroundChanged);
0235     connect(getColorSelector("Inactive Background"), &KonfiguratorColorChooser::colorChanged, this, &KgColors::slotInactiveBackgroundChanged);
0236     connect(getColorSelector("Inactive Alternate Background"), &KonfiguratorColorChooser::colorChanged, this, &KgColors::slotInactiveAltBackgroundChanged);
0237     connect(getColorSelector("Inactive Marked Background"), &KonfiguratorColorChooser::colorChanged, this, &KgColors::slotInactiveMarkedBackgroundChanged);
0238 
0239     offset = endOfPanelColors = itemList.count();
0240 
0241     inactiveColorStack->addWidget(normalInactiveWidget);
0242 
0243     colorsGrp = dimmedInactiveWidget = new QWidget(inactiveColorStack);
0244 
0245     colorsGrid = new QGridLayout(dimmedInactiveWidget);
0246     colorsGrid->setSpacing(0);
0247     colorsGrid->setContentsMargins(2, 2, 2, 2);
0248 
0249     addColorSelector("Dim Target Color", i18n("Dim target color:"), Qt::black);
0250 
0251     int index = itemList.count() - offset;
0252     QLabel *spinBoxLabel = addLabel(colorsGrid, index, 0, i18n("Dim factor:"), colorsGrp);
0253     labelList.append(spinBoxLabel);
0254     dimFactor = createSpinBox("Colors", "Dim Factor", 80, 0, 100, spinBoxLabel, colorsGrp);
0255     dimFactor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
0256     colorsGrid->addWidget(dimFactor, index++, 1);
0257 
0258     colorsGrid->addWidget(createSpacer(dimmedInactiveWidget), itemList.count() + 1 - offset, 1);
0259 
0260     inactiveColorStack->addWidget(dimmedInactiveWidget);
0261 
0262     inactiveColorStack->setCurrentWidget(normalInactiveWidget);
0263 
0264     ADDITIONAL_COLOR KDEDefaultBase = {i18n("KDE default"), p.color(QPalette::Active, QPalette::Base), "KDE default"};
0265     ADDITIONAL_COLOR KDEDefaultFore = {i18n("KDE default"), p.color(QPalette::Active, QPalette::Text), "KDE default"};
0266 
0267 #ifdef SYNCHRONIZER_ENABLED
0268     colorsGrp = new QWidget(colorTabWidget);
0269     synchronizerTabIdx = colorTabWidget->addTab(colorsGrp, i18n("Synchronizer"));
0270 
0271     colorsGrid = new QGridLayout(colorsGrp);
0272     colorsGrid->setSpacing(0);
0273     colorsGrid->setContentsMargins(2, 2, 2, 2);
0274 
0275     offset = endOfPanelColors = itemList.count();
0276 
0277     DECLARE_SYNCHRONIZER_BACKGROUND_DEFAULTS;
0278     DECLARE_SYNCHRONIZER_FOREGROUND_DEFAULTS;
0279 
0280     addColorSelector("Synchronizer Equals Foreground", i18n("Equals foreground:"), SYNCHRONIZER_FOREGROUND_DEFAULTS[0], QString(), &KDEDefaultFore, 1);
0281     addColorSelector("Synchronizer Equals Background", i18n("Equals background:"), SYNCHRONIZER_BACKGROUND_DEFAULTS[0], QString(), &KDEDefaultBase, 1);
0282     addColorSelector("Synchronizer Differs Foreground", i18n("Differing foreground:"), SYNCHRONIZER_FOREGROUND_DEFAULTS[1], QString(), &KDEDefaultFore, 1);
0283     addColorSelector("Synchronizer Differs Background", i18n("Differing background:"), SYNCHRONIZER_BACKGROUND_DEFAULTS[1], QString(), &KDEDefaultBase, 1);
0284     addColorSelector("Synchronizer LeftCopy Foreground", i18n("Copy to left foreground:"), SYNCHRONIZER_FOREGROUND_DEFAULTS[2], QString(), &KDEDefaultFore, 1);
0285     addColorSelector("Synchronizer LeftCopy Background", i18n("Copy to left background:"), SYNCHRONIZER_BACKGROUND_DEFAULTS[2], QString(), &KDEDefaultBase, 1);
0286     addColorSelector("Synchronizer RightCopy Foreground",
0287                      i18n("Copy to right foreground:"),
0288                      SYNCHRONIZER_FOREGROUND_DEFAULTS[3],
0289                      QString(),
0290                      &KDEDefaultFore,
0291                      1);
0292     addColorSelector("Synchronizer RightCopy Background",
0293                      i18n("Copy to right background:"),
0294                      SYNCHRONIZER_BACKGROUND_DEFAULTS[3],
0295                      QString(),
0296                      &KDEDefaultBase,
0297                      1);
0298     addColorSelector("Synchronizer Delete Foreground", i18n("Delete foreground:"), SYNCHRONIZER_FOREGROUND_DEFAULTS[4], QString(), &KDEDefaultFore, 1);
0299     addColorSelector("Synchronizer Delete Background", i18n("Delete background:"), SYNCHRONIZER_BACKGROUND_DEFAULTS[4], QString(), &KDEDefaultBase, 1);
0300 
0301     colorsGrid->addWidget(createSpacer(colorsGrp), itemList.count() - offset, 1);
0302 #endif
0303 
0304     colorsGrp = new QWidget(colorTabWidget);
0305     otherTabIdx = colorTabWidget->addTab(colorsGrp, i18n("Other"));
0306 
0307     colorsGrid = new QGridLayout(colorsGrp);
0308     colorsGrid->setSpacing(0);
0309     colorsGrid->setContentsMargins(2, 2, 2, 2);
0310 
0311     offset = endOfPanelColors = itemList.count();
0312 
0313     addColorSelector("Quicksearch Match Foreground", i18n("Quicksearch, match foreground:"), Qt::black, QString(), &KDEDefaultFore, 1);
0314     addColorSelector("Quicksearch Match Background", i18n("Quicksearch, match background:"), QColor(192, 255, 192), QString(), &KDEDefaultBase, 1);
0315     addColorSelector("Quicksearch Non-match Foreground", i18n("Quicksearch, non-match foreground:"), Qt::black, QString(), &KDEDefaultFore, 1);
0316     addColorSelector("Quicksearch Non-match Background", i18n("Quicksearch, non-match background:"), QColor(255, 192, 192), QString(), &KDEDefaultBase, 1);
0317     ADDITIONAL_COLOR KDEDefaultWindowFore = {i18n("KDE default"), p.color(QPalette::Active, QPalette::WindowText), "KDE default"};
0318     ADDITIONAL_COLOR KDEDefaultWindowBack = {i18n("KDE default"), p.color(QPalette::Active, QPalette::Window), "KDE default"};
0319     addColorSelector("Statusbar Foreground Active",
0320                      i18n("Statusbar, active foreground:"),
0321                      p.color(QPalette::Active, QPalette::HighlightedText),
0322                      QString(),
0323                      &KDEDefaultWindowFore,
0324                      1);
0325     addColorSelector("Statusbar Background Active",
0326                      i18n("Statusbar, active background:"),
0327                      p.color(QPalette::Active, QPalette::Highlight),
0328                      QString(),
0329                      &KDEDefaultWindowBack,
0330                      1);
0331     addColorSelector("Statusbar Foreground Inactive",
0332                      i18n("Statusbar, inactive foreground:"),
0333                      p.color(QPalette::Inactive, QPalette::Text),
0334                      QString(),
0335                      &KDEDefaultWindowFore,
0336                      1);
0337     addColorSelector("Statusbar Background Inactive",
0338                      i18n("Statusbar, inactive background:"),
0339                      p.color(QPalette::Inactive, QPalette::Base),
0340                      QString(),
0341                      &KDEDefaultWindowBack,
0342                      1);
0343 
0344     colorsGrid->addWidget(createSpacer(colorsGrp), itemList.count() - offset, 1);
0345 
0346     colorsFrameGrid->addWidget(colorTabWidget, 0, 0);
0347     hbox->addWidget(colorsFrameGrp);
0348 
0349     //  -------------------------- PREVIEW GROUPBOX ----------------------------------
0350 
0351     previewGrp = createFrame(i18n("Preview"), hboxWidget);
0352     previewGrid = createGridLayout(previewGrp);
0353 
0354     preview = new KrTreeWidget(previewGrp);
0355     preview->setBackgroundRole(QPalette::Window);
0356     preview->setAutoFillBackground(true);
0357 
0358     QStringList labels;
0359     labels << i18n("Colors");
0360     preview->setHeaderLabels(labels);
0361 
0362     preview->header()->setSortIndicatorShown(false);
0363     preview->setSortingEnabled(false);
0364     preview->setEnabled(false);
0365 
0366     previewGrid->addWidget(preview, 0, 0);
0367     hbox->addWidget(previewGrp);
0368 
0369     connect(generals->find("Enable Alternate Background"), &KonfiguratorCheckBox::stateChanged, this, &KgColors::generatePreview);
0370     connect(colorTabWidget, &QTabWidget::currentChanged, this, &KgColors::generatePreview);
0371     connect(dimFactor, QOverload<int>::of(&KonfiguratorSpinBox::valueChanged), this, &KgColors::generatePreview);
0372 
0373     kgColorsLayout->addWidget(hboxWidget, 1, 0, 1, 3);
0374 
0375     importBtn = new QPushButton(i18n("Import color-scheme"), innerWidget);
0376     kgColorsLayout->addWidget(importBtn, 2, 0);
0377     exportBtn = new QPushButton(i18n("Export color-scheme"), innerWidget);
0378     kgColorsLayout->addWidget(exportBtn, 2, 1);
0379     kgColorsLayout->addWidget(createSpacer(innerWidget), 2, 2);
0380     connect(importBtn, &QPushButton::clicked, this, &KgColors::slotImportColors);
0381     connect(exportBtn, &QPushButton::clicked, this, &KgColors::slotExportColors);
0382 
0383     slotDisable();
0384 }
0385 
0386 int KgColors::addColorSelector(const QString &cfgName, QString name, QColor defaultValue, const QString &dfltName, ADDITIONAL_COLOR *addColor, int addColNum)
0387 {
0388     int index = itemList.count() - offset;
0389 
0390     labelList.append(addLabel(colorsGrid, index, 0, std::move(name), colorsGrp));
0391     KonfiguratorColorChooser *chooser = createColorChooser("Colors", cfgName, std::move(defaultValue), colorsGrp, false, addColor, addColNum);
0392     chooser->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
0393 
0394     if (!dfltName.isEmpty())
0395         chooser->setDefaultText(dfltName);
0396 
0397     colorsGrid->addWidget(chooser, index, 1);
0398 
0399     connect(chooser, &KonfiguratorColorChooser::colorChanged, this, &KgColors::generatePreview);
0400     if (!offset)
0401         connect(chooser, &KonfiguratorColorChooser::colorChanged, this, &KgColors::slotActiveChanged);
0402 
0403     itemList.append(chooser);
0404     itemNames.append(cfgName);
0405 
0406     return index;
0407 }
0408 
0409 KonfiguratorColorChooser *KgColors::getColorSelector(const QString &name)
0410 {
0411     QList<QString>::iterator it;
0412     int position = 0;
0413 
0414     for (it = itemNames.begin(); it != itemNames.end(); it++, position++)
0415         if (*it == name)
0416             return itemList.at(position);
0417 
0418     return nullptr;
0419 }
0420 
0421 QLabel *KgColors::getSelectorLabel(const QString &name)
0422 {
0423     QList<QString>::iterator it;
0424     int position = 0;
0425 
0426     for (it = itemNames.begin(); it != itemNames.end(); it++, position++)
0427         if (*it == name)
0428             return labelList.at(position);
0429 
0430     return nullptr;
0431 }
0432 
0433 void KgColors::slotDisable()
0434 {
0435     bool enabled = generals->find("KDE Default")->isChecked();
0436 
0437     importBtn->setEnabled(!enabled);
0438     exportBtn->setEnabled(!enabled);
0439 
0440     for (int i = 0; i < labelList.count() && i < endOfPanelColors; i++)
0441         labelList.at(i)->setEnabled(!enabled);
0442 
0443     for (int j = 0; j < itemList.count() && j < endOfPanelColors; j++)
0444         itemList.at(j)->setEnabled(!enabled);
0445 
0446     generals->find("Enable Alternate Background")->setEnabled(enabled);
0447     generals->find("Show Current Item Always")->setEnabled(!enabled);
0448     generals->find("Dim Inactive Colors")->setEnabled(!enabled);
0449 
0450     bool dimmed = !enabled && generals->find("Dim Inactive Colors")->isChecked();
0451     if (dimmed)
0452         inactiveColorStack->setCurrentWidget(dimmedInactiveWidget);
0453     else
0454         inactiveColorStack->setCurrentWidget(normalInactiveWidget);
0455 
0456     enabled = enabled || !generals->find("Show Current Item Always")->isChecked();
0457 
0458     getColorSelector("Inactive Current Foreground")->setEnabled(!enabled);
0459     getColorSelector("Inactive Current Background")->setEnabled(!enabled);
0460 
0461     generatePreview();
0462 }
0463 
0464 void KgColors::slotActiveChanged()
0465 {
0466     for (int i = 0; i != endOfActiveColors; i++)
0467         itemList.at(endOfActiveColors + i)->setDefaultColor(itemList.at(i)->getColor());
0468 }
0469 
0470 void KgColors::slotForegroundChanged()
0471 {
0472     QColor color = getColorSelector("Foreground")->getColor();
0473 
0474     getColorSelector("Directory Foreground")->setDefaultColor(color);
0475     getColorSelector("Executable Foreground")->setDefaultColor(color);
0476     getColorSelector("Symlink Foreground")->setDefaultColor(color);
0477     getColorSelector("Invalid Symlink Foreground")->setDefaultColor(color);
0478 }
0479 
0480 void KgColors::slotBackgroundChanged()
0481 {
0482     QColor color = getColorSelector("Background")->getColor();
0483 
0484     getColorSelector("Alternate Background")->changeAdditionalColor(0, color);
0485     getColorSelector("Marked Background")->changeAdditionalColor(0, color);
0486     getColorSelector("Current Background")->changeAdditionalColor(0, color);
0487 }
0488 
0489 void KgColors::slotAltBackgroundChanged()
0490 {
0491     QColor color = getColorSelector("Alternate Background")->getColor();
0492     getColorSelector("Alternate Marked Background")->changeAdditionalColor(0, color);
0493 }
0494 
0495 void KgColors::slotMarkedBackgroundChanged()
0496 {
0497     QColor color = getColorSelector("Marked Background")->getColor();
0498     getColorSelector("Alternate Marked Background")->setDefaultColor(color);
0499 }
0500 
0501 void KgColors::slotInactiveForegroundChanged()
0502 {
0503     QColor color = getColorSelector("Inactive Foreground")->getColor();
0504 
0505     getColorSelector("Inactive Directory Foreground")->changeAdditionalColor(0, color);
0506     getColorSelector("Inactive Executable Foreground")->changeAdditionalColor(0, color);
0507     getColorSelector("Inactive Symlink Foreground")->changeAdditionalColor(0, color);
0508     getColorSelector("Inactive Invalid Symlink Foreground")->changeAdditionalColor(0, color);
0509 }
0510 
0511 void KgColors::slotInactiveBackgroundChanged()
0512 {
0513     QColor color = getColorSelector("Inactive Background")->getColor();
0514 
0515     getColorSelector("Inactive Alternate Background")->changeAdditionalColor(0, color);
0516     getColorSelector("Inactive Marked Background")->changeAdditionalColor(0, color);
0517     getColorSelector("Inactive Current Background")->changeAdditionalColor(0, color);
0518 }
0519 
0520 void KgColors::slotInactiveAltBackgroundChanged()
0521 {
0522     QColor color = getColorSelector("Inactive Alternate Background")->getColor();
0523     getColorSelector("Inactive Alternate Marked Background")->changeAdditionalColor(0, color);
0524 }
0525 
0526 void KgColors::slotInactiveMarkedBackgroundChanged()
0527 {
0528     QColor color = getColorSelector("Inactive Marked Background")->getColor();
0529     getColorSelector("Inactive Alternate Marked Background")->changeAdditionalColor(1, color);
0530 }
0531 
0532 void KgColors::setColorWithDimming(PreviewItem *item, QColor foreground, QColor background, bool dimmed)
0533 {
0534     if (dimmed && dimFactor->value() < 100) {
0535         int dim = dimFactor->value();
0536         QColor dimColor = getColorSelector("Dim Target Color")->getColor();
0537 
0538         foreground = QColor((dimColor.red() * (100 - dim) + foreground.red() * dim) / 100,
0539                             (dimColor.green() * (100 - dim) + foreground.green() * dim) / 100,
0540                             (dimColor.blue() * (100 - dim) + foreground.blue() * dim) / 100);
0541         background = QColor((dimColor.red() * (100 - dim) + background.red() * dim) / 100,
0542                             (dimColor.green() * (100 - dim) + background.green() * dim) / 100,
0543                             (dimColor.blue() * (100 - dim) + background.blue() * dim) / 100);
0544     }
0545     item->setColor(foreground, background);
0546 }
0547 
0548 void KgColors::generatePreview()
0549 {
0550     const int currentPage = colorTabWidget->currentIndex();
0551 
0552     preview->clear();
0553 
0554     if (currentPage == activeTabIdx || currentPage == inactiveTabIdx) {
0555         PreviewItem *pwMarkCur = new PreviewItem(preview, i18n("Selected + Current"));
0556         PreviewItem *pwMark2 = new PreviewItem(preview, i18n("Selected 2"));
0557         PreviewItem *pwMark1 = new PreviewItem(preview, i18n("Selected 1"));
0558         PreviewItem *pwCurrent = new PreviewItem(preview, i18n("Current"));
0559         PreviewItem *pwInvLink = new PreviewItem(preview, i18n("Invalid symlink"));
0560         PreviewItem *pwSymLink = new PreviewItem(preview, i18n("Symbolic link"));
0561         PreviewItem *pwApp = new PreviewItem(preview, i18n("Application"));
0562         PreviewItem *pwFile = new PreviewItem(preview, i18n("File"));
0563         PreviewItem *pwDir = new PreviewItem(preview, i18n("Folder"));
0564 
0565         bool isActive = currentPage == 0;
0566 
0567         // create local color cache instance, which does NOT affect the color cache instance using to paint the panels
0568         KrColorCache colCache;
0569 
0570         // create local color settings instance, which initially contains the settings from krConfig
0571         KrColorSettings colorSettings;
0572 
0573         // copy over local settings to color settings instance, which does not affect the persisted krConfig settings
0574         QList<QString> names = KrColorSettings::getColorNames();
0575         for (auto &name : names) {
0576             KonfiguratorColorChooser *chooser = getColorSelector(name);
0577             if (!chooser)
0578                 continue;
0579             colorSettings.setColorTextValue(name, chooser->getValue());
0580             if (chooser->isValueRGB())
0581                 colorSettings.setColorValue(name, chooser->getColor());
0582             else
0583                 colorSettings.setColorValue(name, QColor());
0584         }
0585 
0586         colorSettings.setBoolValue("KDE Default", generals->find("KDE Default")->isChecked());
0587         colorSettings.setBoolValue("Enable Alternate Background", generals->find("Enable Alternate Background")->isChecked());
0588         colorSettings.setBoolValue("Show Current Item Always", generals->find("Show Current Item Always")->isChecked());
0589         colorSettings.setBoolValue("Dim Inactive Colors", generals->find("Dim Inactive Colors")->isChecked());
0590         colorSettings.setNumValue("Dim Factor", dimFactor->value());
0591 
0592         // let the color cache use the local color settings
0593         colCache.setColors(colorSettings);
0594 
0595         // ask the local color cache for certain color groups and use them to color the preview
0596         KrColorGroup cg;
0597 
0598         // setting the background color
0599         colCache.getColors(cg, KrColorItemType(KrColorItemType::File, false, isActive, false, false));
0600         QPalette pal = preview->palette();
0601         pal.setColor(QPalette::Base, cg.background());
0602         preview->setPalette(pal);
0603 
0604         colCache.getColors(cg, KrColorItemType(KrColorItemType::Directory, false, isActive, false, false));
0605         pwDir->setColor(cg.text(), cg.background());
0606         colCache.getColors(cg, KrColorItemType(KrColorItemType::File, true, isActive, false, false));
0607         pwFile->setColor(cg.text(), cg.background());
0608         colCache.getColors(cg, KrColorItemType(KrColorItemType::Executable, false, isActive, false, false));
0609         pwApp->setColor(cg.text(), cg.background());
0610         colCache.getColors(cg, KrColorItemType(KrColorItemType::Symlink, true, isActive, false, false));
0611         pwSymLink->setColor(cg.text(), cg.background());
0612         colCache.getColors(cg, KrColorItemType(KrColorItemType::InvalidSymlink, false, isActive, false, false));
0613         pwInvLink->setColor(cg.text(), cg.background());
0614         colCache.getColors(cg, KrColorItemType(KrColorItemType::File, true, isActive, true, false));
0615         pwCurrent->setColor(cg.text(), cg.background());
0616         colCache.getColors(cg, KrColorItemType(KrColorItemType::File, false, isActive, false, true));
0617         pwMark1->setColor(cg.highlightedText(), cg.highlight());
0618         colCache.getColors(cg, KrColorItemType(KrColorItemType::File, true, isActive, false, true));
0619         pwMark2->setColor(cg.highlightedText(), cg.highlight());
0620         colCache.getColors(cg, KrColorItemType(KrColorItemType::File, false, isActive, true, true));
0621         pwMarkCur->setColor(cg.highlightedText(), cg.highlight());
0622     }
0623 #ifdef SYNCHRONIZER_ENABLED
0624     else if (currentPage == synchronizerTabIdx) {
0625         PreviewItem *pwDelete = new PreviewItem(preview, i18n("Delete"));
0626         PreviewItem *pwRightCopy = new PreviewItem(preview, i18n("Copy to right"));
0627         PreviewItem *pwLeftCopy = new PreviewItem(preview, i18n("Copy to left"));
0628         PreviewItem *pwDiffers = new PreviewItem(preview, i18n("Differing"));
0629         PreviewItem *pwEquals = new PreviewItem(preview, i18n("Equals"));
0630 
0631         pwEquals->setColor(getColorSelector("Synchronizer Equals Foreground")->getColor(), getColorSelector("Synchronizer Equals Background")->getColor());
0632         pwDiffers->setColor(getColorSelector("Synchronizer Differs Foreground")->getColor(), getColorSelector("Synchronizer Differs Background")->getColor());
0633         pwLeftCopy->setColor(getColorSelector("Synchronizer LeftCopy Foreground")->getColor(),
0634                              getColorSelector("Synchronizer LeftCopy Background")->getColor());
0635         pwRightCopy->setColor(getColorSelector("Synchronizer RightCopy Foreground")->getColor(),
0636                               getColorSelector("Synchronizer RightCopy Background")->getColor());
0637         pwDelete->setColor(getColorSelector("Synchronizer Delete Foreground")->getColor(), getColorSelector("Synchronizer Delete Background")->getColor());
0638     }
0639 #endif
0640     else if (currentPage == otherTabIdx) {
0641         PreviewItem *pwNonMatch = new PreviewItem(preview, i18n("Quicksearch non-match"));
0642         PreviewItem *pwMatch = new PreviewItem(preview, i18n("Quicksearch match"));
0643         pwMatch->setColor(getColorSelector("Quicksearch Match Foreground")->getColor(), getColorSelector("Quicksearch Match Background")->getColor());
0644         pwNonMatch->setColor(getColorSelector("Quicksearch Non-match Foreground")->getColor(),
0645                              getColorSelector("Quicksearch Non-match Background")->getColor());
0646         PreviewItem *pwStatusActive = new PreviewItem(preview, i18n("Statusbar active"));
0647         PreviewItem *pwStatusInactive = new PreviewItem(preview, i18n("Statusbar inactive"));
0648         pwStatusActive->setColor(getColorSelector("Statusbar Foreground Active")->getColor(), getColorSelector("Statusbar Background Active")->getColor());
0649         pwStatusInactive->setColor(getColorSelector("Statusbar Foreground Inactive")->getColor(),
0650                                    getColorSelector("Statusbar Background Inactive")->getColor());
0651     }
0652 }
0653 
0654 bool KgColors::apply()
0655 {
0656     bool result = KonfiguratorPage::apply();
0657     KrColorCache::getColorCache().refreshColors();
0658     return result;
0659 }
0660 
0661 void KgColors::slotImportColors()
0662 {
0663     // find $KDEDIR/share/apps/krusader
0664     QString basedir = QStandardPaths::locate(QStandardPaths::DataLocation, QStringLiteral("total_commander.keymap"));
0665     basedir = QFileInfo(basedir).absolutePath();
0666     // let the user select a file to load
0667     QString file = QFileDialog::getOpenFileName(nullptr, i18n("Select a color-scheme file"), basedir, QStringLiteral("*.color"));
0668     if (file.isEmpty()) {
0669         return;
0670     }
0671     QFile f(file);
0672     if (!f.open(QIODevice::ReadOnly)) {
0673         KMessageBox::error(this, i18n("Error: unable to read from file"), i18n("Error"));
0674         return;
0675     }
0676     QDataStream stream(&f);
0677     // ok, import away
0678     deserialize(stream);
0679     generatePreview();
0680 }
0681 
0682 void KgColors::slotExportColors()
0683 {
0684     QString file = QFileDialog::getSaveFileName(nullptr, i18n("Select a color scheme file"), QString(), QStringLiteral("*"));
0685     if (file.isEmpty()) {
0686         return;
0687     }
0688     QFile f(file);
0689     if (f.exists()
0690         && KMessageBox::warningContinueCancel(this,
0691                                               i18n("File %1 already exists. Are you sure you want to overwrite it?", file),
0692                                               i18n("Warning"),
0693                                               KStandardGuiItem::overwrite())
0694             != KMessageBox::Continue)
0695         return;
0696     if (!f.open(QIODevice::WriteOnly)) {
0697         KMessageBox::error(this, i18n("Error: unable to write to file"), i18n("Error"));
0698         return;
0699     }
0700     QDataStream stream(&f);
0701     serialize(stream);
0702 }
0703 
0704 void KgColors::serialize(QDataStream &stream)
0705 {
0706     serializeItem(stream, "Alternate Background");
0707     serializeItem(stream, "Alternate Marked Background");
0708     serializeItem(stream, "Background");
0709     serializeItem(stream, "Current Background");
0710     serializeItem(stream, "Current Foreground");
0711     serializeItem(stream, "Enable Alternate Background");
0712     serializeItem(stream, "Foreground");
0713     serializeItem(stream, "Directory Foreground");
0714     serializeItem(stream, "Executable Foreground");
0715     serializeItem(stream, "Symlink Foreground");
0716     serializeItem(stream, "Invalid Symlink Foreground");
0717     serializeItem(stream, "Inactive Alternate Background");
0718     serializeItem(stream, "Inactive Alternate Marked Background");
0719     serializeItem(stream, "Inactive Background");
0720     serializeItem(stream, "Inactive Current Foreground");
0721     serializeItem(stream, "Inactive Current Background");
0722     serializeItem(stream, "Inactive Marked Background");
0723     serializeItem(stream, "Inactive Marked Current Foreground");
0724     serializeItem(stream, "Inactive Marked Foreground");
0725     serializeItem(stream, "Inactive Foreground");
0726     serializeItem(stream, "Inactive Directory Foreground");
0727     serializeItem(stream, "Inactive Executable Foreground");
0728     serializeItem(stream, "Inactive Symlink Foreground");
0729     serializeItem(stream, "Inactive Invalid Symlink Foreground");
0730     serializeItem(stream, "Dim Inactive Colors");
0731     serializeItem(stream, "Dim Target Color");
0732     serializeItem(stream, "Dim Factor");
0733     serializeItem(stream, "KDE Default");
0734     serializeItem(stream, "Marked Background");
0735     serializeItem(stream, "Marked Current Foreground");
0736     serializeItem(stream, "Marked Foreground");
0737     serializeItem(stream, "Show Current Item Always");
0738 #ifdef SYNCHRONIZER_ENABLED
0739     serializeItem(stream, "Synchronizer Equals Foreground");
0740     serializeItem(stream, "Synchronizer Equals Background");
0741     serializeItem(stream, "Synchronizer Differs Foreground");
0742     serializeItem(stream, "Synchronizer Differs Background");
0743     serializeItem(stream, "Synchronizer LeftCopy Foreground");
0744     serializeItem(stream, "Synchronizer LeftCopy Background");
0745     serializeItem(stream, "Synchronizer RightCopy Foreground");
0746     serializeItem(stream, "Synchronizer RightCopy Background");
0747     serializeItem(stream, "Synchronizer Delete Foreground");
0748     serializeItem(stream, "Synchronizer Delete Background");
0749 #endif
0750     serializeItem(stream, "Quicksearch Match Foreground");
0751     serializeItem(stream, "Quicksearch Match Background");
0752     serializeItem(stream, "Quicksearch Non-match Foreground");
0753     serializeItem(stream, "Quicksearch Non-match Background");
0754     serializeItem(stream, "Statusbar Foreground Active");
0755     serializeItem(stream, "Statusbar Background Active");
0756     serializeItem(stream, "Statusbar Foreground Inactive");
0757     serializeItem(stream, "Statusbar Background Inactive");
0758     stream << QString("") << QString("");
0759 }
0760 
0761 void KgColors::deserialize(QDataStream &stream)
0762 {
0763     for (;;) {
0764         QString name;
0765         QString value;
0766         stream >> name >> value;
0767         if (name.isEmpty())
0768             break;
0769 
0770         if (name == "KDE Default" || name == "Enable Alternate Background" || name == "Show Current Item Always" || name == "Dim Inactive Colors") {
0771             bool bValue = false;
0772             value = value.toLower();
0773             if (value == "true" || value == "yes" || value == "on" || value == "1")
0774                 bValue = true;
0775 
0776             generals->find(name)->setChecked(bValue);
0777             continue;
0778         }
0779 
0780         if (name == "Dim Factor") {
0781             dimFactor->setValue(value.toInt());
0782             continue;
0783         }
0784 
0785         KonfiguratorColorChooser *selector = getColorSelector(name);
0786         if (selector == nullptr)
0787             break;
0788         selector->setValue(value);
0789     }
0790 }
0791 
0792 void KgColors::serializeItem(class QDataStream &stream, const char *name)
0793 {
0794     stream << QString(name);
0795     if (strcmp(name, "KDE Default") == 0 || strcmp(name, "Enable Alternate Background") == 0 || strcmp(name, "Show Current Item Always") == 0
0796         || strcmp(name, "Dim Inactive Colors") == 0) {
0797         bool bValue = generals->find(name)->isChecked();
0798         stream << QString(bValue ? "true" : "false");
0799     } else if (strcmp(name, "Dim Factor") == 0)
0800         stream << QString::number(dimFactor->value());
0801     else {
0802         KonfiguratorColorChooser *selector = getColorSelector(name);
0803         stream << selector->getValue();
0804     }
0805 }