File indexing completed on 2024-05-12 16:16:01
0001 /* 0002 SPDX-FileCopyrightText: 2019-2023 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "grammalecteconfigwidget.h" 0008 #include "grammalectemanager.h" 0009 #include "grammalecteurlrequesterwidget.h" 0010 0011 #include <KLocalizedString> 0012 #include <KMessageBox> 0013 0014 #include <QCheckBox> 0015 #include <QFormLayout> 0016 #include <QLabel> 0017 #include <QScrollArea> 0018 #include <QStackedWidget> 0019 #include <QTabWidget> 0020 #include <QToolButton> 0021 #include <QVBoxLayout> 0022 0023 using namespace TextGrammarCheck; 0024 GrammalecteConfigWidget::GrammalecteConfigWidget(QWidget *parent, bool disableMessageBox) 0025 : QWidget(parent) 0026 , mDisableDialogBox(disableMessageBox) 0027 { 0028 auto mainLayout = new QVBoxLayout(this); 0029 mainLayout->setObjectName(QStringLiteral("mainlayout")); 0030 mainLayout->setContentsMargins({}); 0031 0032 auto mTab = new QTabWidget(this); 0033 mTab->setObjectName(QStringLiteral("mTab")); 0034 mainLayout->addWidget(mTab); 0035 mTab->addTab(addGeneralTab(), i18n("General")); 0036 mTab->addTab(addGrammarTab(), i18n("Grammar Settings")); 0037 loadSettings(); // First 0038 loadGrammarSettings(); 0039 } 0040 0041 GrammalecteConfigWidget::~GrammalecteConfigWidget() 0042 { 0043 saveSettings(); 0044 } 0045 0046 void GrammalecteConfigWidget::loadGrammarSettings() 0047 { 0048 auto job = new GrammalecteGenerateConfigOptionJob(this); 0049 job->setPythonPath(mPythonPath->path()); 0050 job->setGrammarlecteCliPath(mGrammalectePath->path()); 0051 connect(job, &GrammalecteGenerateConfigOptionJob::finished, this, &GrammalecteConfigWidget::slotGetSettingsFinished); 0052 connect(job, &GrammalecteGenerateConfigOptionJob::error, this, &GrammalecteConfigWidget::slotGetSettingsError); 0053 job->start(); 0054 } 0055 0056 void GrammalecteConfigWidget::slotGetSettingsError() 0057 { 0058 mStackedWidget->setCurrentWidget(mReloadSettingsWidget); 0059 if (!mDisableDialogBox) { 0060 KMessageBox::error(this, 0061 i18n("Impossible to get options. Please verify that you have grammalected installed."), 0062 i18n("Error during Extracting Options")); 0063 } 0064 } 0065 0066 void GrammalecteConfigWidget::slotGetSettingsFinished(const QVector<GrammalecteGenerateConfigOptionJob::Option> &result) 0067 { 0068 mStackedWidget->setCurrentWidget(mScrollArea); 0069 mListOptions.clear(); 0070 mListOptions.reserve(result.count()); 0071 delete mGrammarTabWidget->layout(); 0072 auto layout = new QVBoxLayout(mGrammarTabWidget); 0073 layout->setObjectName(QStringLiteral("grammartablayout")); 0074 0075 for (const GrammalecteGenerateConfigOptionJob::Option &opt : result) { 0076 auto box = new QCheckBox(opt.description, this); 0077 box->setProperty("optionname", opt.optionName); 0078 if (mSaveOptions.isEmpty()) { 0079 box->setChecked(opt.defaultValue); 0080 } else { 0081 box->setChecked(mSaveOptions.contains(opt.optionName)); 0082 } 0083 mGrammarTabWidget->layout()->addWidget(box); 0084 mListOptions.append(box); 0085 } 0086 } 0087 0088 QWidget *GrammalecteConfigWidget::addGrammarTab() 0089 { 0090 mStackedWidget = new QStackedWidget(this); 0091 mStackedWidget->setObjectName(QStringLiteral("stackedwidget")); 0092 0093 mScrollArea = new QScrollArea(this); 0094 mScrollArea->setObjectName(QStringLiteral("scrollarea")); 0095 mScrollArea->setWidgetResizable(true); 0096 mGrammarTabWidget = new QWidget; 0097 mGrammarTabWidget->setObjectName(QStringLiteral("grammar")); 0098 auto layout = new QVBoxLayout(mGrammarTabWidget); 0099 layout->setObjectName(QStringLiteral("grammartablayout")); 0100 mScrollArea->setWidget(mGrammarTabWidget); 0101 0102 mStackedWidget->addWidget(mScrollArea); 0103 0104 mReloadSettingsWidget = new QWidget; 0105 mReloadSettingsWidget->setObjectName(QStringLiteral("reloadwidget")); 0106 mStackedWidget->addWidget(mReloadSettingsWidget); 0107 auto reloadSettingsLayout = new QVBoxLayout(mReloadSettingsWidget); 0108 reloadSettingsLayout->setObjectName(QStringLiteral("reloadSettingsLayout")); 0109 auto horizontallayout = new QHBoxLayout; 0110 reloadSettingsLayout->addLayout(horizontallayout); 0111 auto label = new QLabel(i18n("Press Button for Reloading Settings"), this); 0112 label->setObjectName(QStringLiteral("label")); 0113 horizontallayout->addWidget(label); 0114 0115 auto buttonReloadSettings = new QToolButton(this); 0116 buttonReloadSettings->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh"))); 0117 buttonReloadSettings->setObjectName(QStringLiteral("buttonReloadSettings")); 0118 buttonReloadSettings->setToolTip(i18n("Reload Settings")); 0119 horizontallayout->addWidget(buttonReloadSettings); 0120 connect(buttonReloadSettings, &QToolButton::clicked, this, &GrammalecteConfigWidget::loadGrammarSettings); 0121 0122 reloadSettingsLayout->addStretch(1); 0123 return mStackedWidget; 0124 } 0125 0126 QWidget *GrammalecteConfigWidget::addGeneralTab() 0127 { 0128 auto w = new QWidget(this); 0129 w->setObjectName(QStringLiteral("general")); 0130 auto lay = new QFormLayout(w); 0131 lay->setObjectName(QStringLiteral("generallayout")); 0132 0133 mPythonPath = new TextGrammarCheck::GrammalecteUrlRequesterWidget(this); 0134 mPythonPath->setObjectName(QStringLiteral("pythonpath")); 0135 lay->addRow(i18n("Python Path:"), mPythonPath); 0136 0137 mGrammalectePath = new TextGrammarCheck::GrammalecteUrlRequesterWidget(this); 0138 mGrammalectePath->setObjectName(QStringLiteral("grammalectepath")); 0139 mGrammalectePath->setPlaceholderText(i18n("Add full 'grammalecte-cli.py' path")); 0140 lay->addRow(i18n("Grammalecte Path:"), mGrammalectePath); 0141 0142 return w; 0143 } 0144 0145 void GrammalecteConfigWidget::loadSettings() 0146 { 0147 mPythonPath->setPath(GrammalecteManager::self()->pythonPath()); 0148 mGrammalectePath->setPath(GrammalecteManager::self()->grammalectePath()); 0149 mSaveOptions = GrammalecteManager::self()->options(); 0150 } 0151 0152 void GrammalecteConfigWidget::saveSettings() 0153 { 0154 QStringList result; 0155 for (QCheckBox *checkBox : std::as_const(mListOptions)) { 0156 if (checkBox->isChecked()) { 0157 result += checkBox->property("optionname").toString(); 0158 } 0159 } 0160 GrammalecteManager::self()->setPythonPath(mPythonPath->path()); 0161 GrammalecteManager::self()->setGrammalectePath(mGrammalectePath->path()); 0162 GrammalecteManager::self()->setOptions(result); 0163 GrammalecteManager::self()->saveSettings(); 0164 } 0165 0166 #include "moc_grammalecteconfigwidget.cpp"