File indexing completed on 2024-05-05 05:51:20

0001 /*
0002     SPDX-FileCopyrightText: 2018 Sven Brauch <mail@svenbrauch.de>
0003     SPDX-FileCopyrightText: 2018 Michal Srb <michalsrb@gmail.com>
0004     SPDX-FileCopyrightText: 2020 Jan Paul Batrina <jpmbatrina01@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "colorpickerconfigpage.h"
0010 
0011 #include <KConfigGroup>
0012 #include <KLocalizedString>
0013 #include <KSharedConfig>
0014 
0015 #include <QGroupBox>
0016 #include <QVBoxLayout>
0017 
0018 KTextEditor::ConfigPage *KateColorPickerPlugin::configPage(int number, QWidget *parent)
0019 {
0020     if (number != 0) {
0021         return nullptr;
0022     }
0023 
0024     return new KateColorPickerConfigPage(parent, this);
0025 }
0026 
0027 KateColorPickerConfigPage::KateColorPickerConfigPage(QWidget *parent, KateColorPickerPlugin *plugin)
0028     : KTextEditor::ConfigPage(parent)
0029     , m_plugin(plugin)
0030 {
0031     QVBoxLayout *layout = new QVBoxLayout(this);
0032     layout->setContentsMargins(0, 0, 0, 0);
0033 
0034     chkNamedColors = new QCheckBox(i18n("Show preview for known color names"), this);
0035     chkNamedColors->setToolTip(i18n(
0036         "Also show the color picker for known color names (e.g. skyblue).\nSee https://www.w3.org/TR/SVG11/types.html#ColorKeywords for the list of colors."));
0037     layout->addWidget(chkNamedColors);
0038 
0039     chkPreviewAfterColor = new QCheckBox(i18n("Place preview after text color"), this);
0040     layout->addWidget(chkPreviewAfterColor);
0041 
0042     connect(chkNamedColors, &QCheckBox::stateChanged, this, &KateColorPickerConfigPage::changed);
0043     connect(chkPreviewAfterColor, &QCheckBox::stateChanged, this, &KateColorPickerConfigPage::changed);
0044 
0045     QGroupBox *hexGroup = new QGroupBox(i18n("Hex color matching"), this);
0046     QVBoxLayout *hexLayout = new QVBoxLayout();
0047     // Hex color formats supported by QColor. See https://doc.qt.io/qt-5/qcolor.html#setNamedColor
0048     chkHexLengths.insert_or_assign(12, new QCheckBox(i18n("12 digits (#RRRRGGGGBBBB)"), this));
0049     chkHexLengths.insert_or_assign(9, new QCheckBox(i18n("9 digits (#RRRGGGBBB)"), this));
0050     chkHexLengths.insert_or_assign(8, new QCheckBox(i18n("8 digits (#AARRGGBB)"), this));
0051     chkHexLengths.insert_or_assign(6, new QCheckBox(i18n("6 digits (#RRGGBB)"), this));
0052     chkHexLengths.insert_or_assign(3, new QCheckBox(i18n("3 digits (#RGB)"), this));
0053 
0054     for (const auto &[_, chk] : std::as_const(chkHexLengths)) {
0055         hexLayout->addWidget(chk);
0056         connect(chk, &QCheckBox::stateChanged, this, &KateColorPickerConfigPage::changed);
0057     }
0058     hexGroup->setLayout(hexLayout);
0059     layout->addWidget(hexGroup);
0060 
0061     layout->addStretch();
0062 
0063     connect(this, &KateColorPickerConfigPage::changed, this, [this]() {
0064         m_colorConfigChanged = true;
0065     });
0066 
0067     reset();
0068 }
0069 
0070 QString KateColorPickerConfigPage::name() const
0071 {
0072     return i18n("Color Picker");
0073 }
0074 
0075 QString KateColorPickerConfigPage::fullName() const
0076 {
0077     return i18n("Color Picker Settings");
0078 }
0079 
0080 QIcon KateColorPickerConfigPage::icon() const
0081 {
0082     return QIcon::fromTheme(QStringLiteral("color-picker"));
0083 }
0084 
0085 void KateColorPickerConfigPage::apply()
0086 {
0087     if (!m_colorConfigChanged) {
0088         // apply() gets called when the "Apply" or "OK" button is pressed. This means that if a user presses "Apply" THEN "OK", the config is updated twice
0089         // Since the the regeneration of color note positions is expensive, we only update on the first call to apply() before changes are made again
0090         return;
0091     }
0092 
0093     KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("ColorPicker"));
0094     config.writeEntry("NamedColors", chkNamedColors->isChecked());
0095     config.writeEntry("PreviewAfterColor", chkPreviewAfterColor->isChecked());
0096 
0097     QList<int> hexLengths;
0098     for (const auto &[hexLength, chkBox] : chkHexLengths) {
0099         if (chkBox->isChecked()) {
0100             hexLengths.append(hexLength);
0101         }
0102     }
0103     config.writeEntry("HexLengths", hexLengths);
0104 
0105     config.sync();
0106     m_plugin->readConfig();
0107     m_colorConfigChanged = false;
0108 }
0109 
0110 void KateColorPickerConfigPage::reset()
0111 {
0112     KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("ColorPicker"));
0113     chkNamedColors->setChecked(config.readEntry("NamedColors", false));
0114     chkPreviewAfterColor->setChecked(config.readEntry("PreviewAfterColor", true));
0115 
0116     const QList<int> enabledHexLengths = config.readEntry("HexLengths", QList<int>{12, 9, 6, 3});
0117 
0118     for (const auto &[hexLength, chkBox] : chkHexLengths) {
0119         chkBox->setChecked(enabledHexLengths.contains(hexLength));
0120     }
0121 }
0122 
0123 #include "moc_colorpickerconfigpage.cpp"