File indexing completed on 2025-01-05 05:19:42

0001 /*
0002     SPDX-FileCopyrightText: 2022 Waqar Ahmed <waqar.17a@gmail.com>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 #include "FormatConfig.h"
0006 
0007 #include "FormatPlugin.h"
0008 #include "FormattersEnum.h"
0009 
0010 #include <QCheckBox>
0011 #include <QComboBox>
0012 #include <QDebug>
0013 #include <QFile>
0014 #include <QGroupBox>
0015 #include <QLabel>
0016 #include <QPlainTextEdit>
0017 #include <QTabWidget>
0018 #include <QTimer>
0019 #include <QVBoxLayout>
0020 
0021 #include <KConfigGroup>
0022 #include <KSharedConfig>
0023 #include <KSyntaxHighlighting/Definition>
0024 #include <KSyntaxHighlighting/Repository>
0025 #include <KSyntaxHighlighting/SyntaxHighlighter>
0026 #include <KTextEditor/Editor>
0027 
0028 static void initTextEdit(QPlainTextEdit *edit)
0029 {
0030     edit->setFont(KTextEditor::Editor::instance()->font());
0031     auto hl = new KSyntaxHighlighting::SyntaxHighlighter(edit->document());
0032     hl->setDefinition(KTextEditor::Editor::instance()->repository().definitionForFileName(QStringLiteral("FormatterSettings.json")));
0033     // we want to have the proper theme for the current palette
0034     const auto theme = KTextEditor::Editor::instance()->theme();
0035     auto pal = edit->palette();
0036     pal.setColor(QPalette::Base, QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::BackgroundColor)));
0037     pal.setColor(QPalette::Highlight, QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::TextSelection)));
0038     edit->setPalette(pal);
0039     hl->setTheme(theme);
0040 }
0041 
0042 class UserConfigEdit : public QWidget
0043 {
0044     Q_OBJECT
0045 public:
0046     UserConfigEdit(FormatPlugin *plugin, QWidget *parent)
0047         : QWidget(parent)
0048         , m_plugin(plugin)
0049     {
0050         auto vbox = new QVBoxLayout(this);
0051         vbox->setContentsMargins({});
0052         vbox->addWidget(&m_edit);
0053         vbox->addWidget(&m_errorLabel);
0054         initTextEdit(&m_edit);
0055 
0056         connect(m_edit.document(), &QTextDocument::contentsChange, this, [this](int, int add, int rem) {
0057             if (add || rem) {
0058                 m_timer.start();
0059                 Q_EMIT changed();
0060             }
0061         });
0062 
0063         m_timer.setInterval(1500);
0064         m_timer.setSingleShot(true);
0065         m_timer.callOnTimeout(this, [this] {
0066             if (m_edit.document()->isEmpty()) {
0067                 m_errorLabel.setVisible(false);
0068                 m_errorLabel.clear();
0069                 return;
0070             }
0071             QJsonParseError err;
0072             QJsonDocument doc = QJsonDocument::fromJson(m_edit.toPlainText().toUtf8(), &err);
0073             if (err.error != QJsonParseError::NoError) {
0074                 m_errorLabel.setText(err.errorString());
0075                 m_errorLabel.setVisible(true);
0076             }
0077         });
0078     }
0079 
0080     void reset()
0081     {
0082         QFile f(m_plugin->userConfigPath());
0083         if (f.open(QFile::ReadOnly)) {
0084             m_edit.setPlainText(QString::fromUtf8(f.readAll()));
0085             m_timer.start();
0086         } else {
0087             m_edit.clear();
0088         }
0089     }
0090 
0091     void apply()
0092     {
0093         QFile f(m_plugin->userConfigPath());
0094         if (f.open(QFile::WriteOnly)) {
0095             f.write(m_edit.toPlainText().toUtf8());
0096             f.flush();
0097             m_plugin->readConfig();
0098         }
0099     }
0100 
0101     Q_SIGNAL void changed();
0102 
0103 private:
0104     FormatPlugin *const m_plugin;
0105     QPlainTextEdit m_edit;
0106     QLabel m_errorLabel;
0107     QTimer m_timer;
0108 };
0109 
0110 FormatConfigPage::FormatConfigPage(class FormatPlugin *plugin, QWidget *parent)
0111     : KTextEditor::ConfigPage(parent)
0112     , m_plugin(plugin)
0113     , m_tabWidget(new QTabWidget(this))
0114 {
0115     auto layout = new QHBoxLayout(this);
0116     layout->setContentsMargins({});
0117     m_tabWidget->setContentsMargins({});
0118     layout->addWidget(m_tabWidget);
0119 
0120     m_userConfigEdit = new UserConfigEdit(m_plugin, this);
0121     connect(m_userConfigEdit, &UserConfigEdit::changed, this, &KTextEditor::ConfigPage::changed);
0122     m_tabWidget->addTab(m_userConfigEdit, i18n("User Settings"));
0123 
0124     m_defaultConfigEdit = new QPlainTextEdit(this);
0125     initTextEdit(m_defaultConfigEdit);
0126     QFile defaultConfigFile(QStringLiteral(":/formatting/FormatterSettings.json"));
0127     defaultConfigFile.open(QIODevice::ReadOnly);
0128     Q_ASSERT(defaultConfigFile.isOpen());
0129     m_defaultConfigEdit->setPlainText(QString::fromUtf8(defaultConfigFile.readAll()));
0130     m_tabWidget->addTab(m_defaultConfigEdit, i18n("Default Settings"));
0131 
0132     m_tabWidget->setCurrentWidget(m_userConfigEdit);
0133 
0134     reset();
0135 }
0136 
0137 void FormatConfigPage::apply()
0138 {
0139     m_userConfigEdit->apply();
0140     m_plugin->configChanged();
0141 }
0142 
0143 void FormatConfigPage::reset()
0144 {
0145     m_userConfigEdit->reset();
0146 }
0147 
0148 #include "FormatConfig.moc"
0149 #include "moc_FormatConfig.cpp"