KTextAddons

grammalecteconfigwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "grammalecteconfigwidget.h"
8#include "grammalectemanager.h"
9#include "grammalecteurlrequesterwidget.h"
10
11#include <KLocalizedString>
12#include <KMessageBox>
13
14#include <QCheckBox>
15#include <QFormLayout>
16#include <QLabel>
17#include <QScrollArea>
18#include <QStackedWidget>
19#include <QTabWidget>
20#include <QToolButton>
21#include <QVBoxLayout>
22
23using namespace TextGrammarCheck;
24GrammalecteConfigWidget::GrammalecteConfigWidget(QWidget *parent, bool disableMessageBox)
25 : QWidget(parent)
26 , mDisableDialogBox(disableMessageBox)
27{
28 auto mainLayout = new QVBoxLayout(this);
29 mainLayout->setObjectName(QStringLiteral("mainlayout"));
30 mainLayout->setContentsMargins({});
31
32 auto mTab = new QTabWidget(this);
33 mTab->setObjectName(QStringLiteral("mTab"));
34 mainLayout->addWidget(mTab);
35 mTab->addTab(addGeneralTab(), i18n("General"));
36 mTab->addTab(addGrammarTab(), i18n("Grammar Settings"));
37 loadSettings(); // First
38 loadGrammarSettings();
39}
40
41GrammalecteConfigWidget::~GrammalecteConfigWidget()
42{
43 saveSettings();
44}
45
46void GrammalecteConfigWidget::loadGrammarSettings()
47{
48 auto job = new GrammalecteGenerateConfigOptionJob(this);
49 job->setPythonPath(mPythonPath->path());
50 job->setGrammarlecteCliPath(mGrammalectePath->path());
51 connect(job, &GrammalecteGenerateConfigOptionJob::finished, this, &GrammalecteConfigWidget::slotGetSettingsFinished);
52 connect(job, &GrammalecteGenerateConfigOptionJob::error, this, &GrammalecteConfigWidget::slotGetSettingsError);
53 job->start();
54}
55
56void GrammalecteConfigWidget::slotGetSettingsError()
57{
58 mStackedWidget->setCurrentWidget(mReloadSettingsWidget);
59 if (!mDisableDialogBox) {
61 i18n("Impossible to get options. Please verify that you have grammalected installed."),
62 i18nc("@title:window", "Error during Extracting Options"));
63 }
64}
65
66void GrammalecteConfigWidget::slotGetSettingsFinished(const QVector<GrammalecteGenerateConfigOptionJob::Option> &result)
67{
68 mStackedWidget->setCurrentWidget(mScrollArea);
69 mListOptions.clear();
70 mListOptions.reserve(result.count());
71 delete mGrammarTabWidget->layout();
72 auto layout = new QVBoxLayout(mGrammarTabWidget);
73 layout->setObjectName(QStringLiteral("grammartablayout"));
74
75 for (const GrammalecteGenerateConfigOptionJob::Option &opt : result) {
76 auto box = new QCheckBox(opt.description, this);
77 box->setProperty("optionname", opt.optionName);
78 if (mSaveOptions.isEmpty()) {
79 box->setChecked(opt.defaultValue);
80 } else {
81 box->setChecked(mSaveOptions.contains(opt.optionName));
82 }
83 mGrammarTabWidget->layout()->addWidget(box);
84 mListOptions.append(box);
85 }
86}
87
88QWidget *GrammalecteConfigWidget::addGrammarTab()
89{
90 mStackedWidget = new QStackedWidget(this);
91 mStackedWidget->setObjectName(QStringLiteral("stackedwidget"));
92
93 mScrollArea = new QScrollArea(this);
94 mScrollArea->setObjectName(QStringLiteral("scrollarea"));
95 mScrollArea->setWidgetResizable(true);
96 mGrammarTabWidget = new QWidget;
97 mGrammarTabWidget->setObjectName(QStringLiteral("grammar"));
98 auto layout = new QVBoxLayout(mGrammarTabWidget);
99 layout->setObjectName(QStringLiteral("grammartablayout"));
100 mScrollArea->setWidget(mGrammarTabWidget);
101
102 mStackedWidget->addWidget(mScrollArea);
103
104 mReloadSettingsWidget = new QWidget;
105 mReloadSettingsWidget->setObjectName(QStringLiteral("reloadwidget"));
106 mStackedWidget->addWidget(mReloadSettingsWidget);
107 auto reloadSettingsLayout = new QVBoxLayout(mReloadSettingsWidget);
108 reloadSettingsLayout->setObjectName(QStringLiteral("reloadSettingsLayout"));
109 auto horizontallayout = new QHBoxLayout;
110 reloadSettingsLayout->addLayout(horizontallayout);
111 auto label = new QLabel(i18nc("@label:textbox", "Press Button for Reloading Settings"), this);
112 label->setTextFormat(Qt::PlainText);
113 label->setObjectName(QStringLiteral("label"));
114 horizontallayout->addWidget(label);
115
116 auto buttonReloadSettings = new QToolButton(this);
117 buttonReloadSettings->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
118 buttonReloadSettings->setObjectName(QStringLiteral("buttonReloadSettings"));
119 buttonReloadSettings->setToolTip(i18nc("@info:tooltip", "Reload Settings"));
120 horizontallayout->addWidget(buttonReloadSettings);
121 connect(buttonReloadSettings, &QToolButton::clicked, this, &GrammalecteConfigWidget::loadGrammarSettings);
122
123 reloadSettingsLayout->addStretch(1);
124 return mStackedWidget;
125}
126
127QWidget *GrammalecteConfigWidget::addGeneralTab()
128{
129 auto w = new QWidget(this);
130 w->setObjectName(QStringLiteral("general"));
131 auto lay = new QFormLayout(w);
132 lay->setObjectName(QStringLiteral("generallayout"));
133
134 mPythonPath = new TextGrammarCheck::GrammalecteUrlRequesterWidget(this);
135 mPythonPath->setObjectName(QStringLiteral("pythonpath"));
136 lay->addRow(i18n("Python Path:"), mPythonPath);
137
138 mGrammalectePath = new TextGrammarCheck::GrammalecteUrlRequesterWidget(this);
139 mGrammalectePath->setObjectName(QStringLiteral("grammalectepath"));
140 mGrammalectePath->setPlaceholderText(i18nc("@info:placeholder", "Add full 'grammalecte-cli.py' path"));
141
142 lay->addRow(i18n("Grammalecte Path:"), mGrammalectePath);
143
144 auto grammalecteInfoDownload = new QLabel(this);
145 grammalecteInfoDownload->setObjectName(QStringLiteral("grammalecteInfoDownload"));
146 grammalecteInfoDownload->setTextFormat(Qt::RichText);
147 grammalecteInfoDownload->setOpenExternalLinks(true);
148 grammalecteInfoDownload->setText(i18n("Grammalecte can be found <a href=\"https://grammalecte.net/#download\">here</a>. Download it and extract it."));
149 lay->addWidget(grammalecteInfoDownload);
150
151 return w;
152}
153
154void GrammalecteConfigWidget::loadSettings()
155{
156 mPythonPath->setPath(GrammalecteManager::self()->pythonPath());
157 mGrammalectePath->setPath(GrammalecteManager::self()->grammalectePath());
158 mSaveOptions = GrammalecteManager::self()->options();
159}
160
161void GrammalecteConfigWidget::saveSettings()
162{
163 QStringList result;
164 for (QCheckBox *checkBox : std::as_const(mListOptions)) {
165 if (checkBox->isChecked()) {
166 result += checkBox->property("optionname").toString();
167 }
168 }
169 GrammalecteManager::self()->setPythonPath(mPythonPath->path());
170 GrammalecteManager::self()->setGrammalectePath(mGrammalectePath->path());
171 GrammalecteManager::self()->setOptions(result);
172 GrammalecteManager::self()->saveSettings();
173}
174
175#include "moc_grammalecteconfigwidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString label(StandardShortcut id)
void clicked(bool checked)
QIcon fromTheme(const QString &name)
qsizetype count() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
PlainText
QWidget(QWidget *parent, Qt::WindowFlags f)
QLayout * layout() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 4 2025 11:55:34 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.