KTextAddons

grammalecteconfigwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 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->setObjectName(QStringLiteral("label"));
113 horizontallayout->addWidget(label);
114
115 auto buttonReloadSettings = new QToolButton(this);
116 buttonReloadSettings->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
117 buttonReloadSettings->setObjectName(QStringLiteral("buttonReloadSettings"));
118 buttonReloadSettings->setToolTip(i18nc("@info:tooltip", "Reload Settings"));
119 horizontallayout->addWidget(buttonReloadSettings);
120 connect(buttonReloadSettings, &QToolButton::clicked, this, &GrammalecteConfigWidget::loadGrammarSettings);
121
122 reloadSettingsLayout->addStretch(1);
123 return mStackedWidget;
124}
125
126QWidget *GrammalecteConfigWidget::addGeneralTab()
127{
128 auto w = new QWidget(this);
129 w->setObjectName(QStringLiteral("general"));
130 auto lay = new QFormLayout(w);
131 lay->setObjectName(QStringLiteral("generallayout"));
132
133 mPythonPath = new TextGrammarCheck::GrammalecteUrlRequesterWidget(this);
134 mPythonPath->setObjectName(QStringLiteral("pythonpath"));
135 lay->addRow(i18n("Python Path:"), mPythonPath);
136
137 mGrammalectePath = new TextGrammarCheck::GrammalecteUrlRequesterWidget(this);
138 mGrammalectePath->setObjectName(QStringLiteral("grammalectepath"));
139 mGrammalectePath->setPlaceholderText(i18nc("@info:placeholder", "Add full 'grammalecte-cli.py' path"));
140
141 lay->addRow(i18n("Grammalecte Path:"), mGrammalectePath);
142
143 auto grammalecteInfoDownload = new QLabel(this);
144 grammalecteInfoDownload->setObjectName(QStringLiteral("grammalecteInfoDownload"));
145 grammalecteInfoDownload->setTextFormat(Qt::RichText);
146 grammalecteInfoDownload->setOpenExternalLinks(true);
147 grammalecteInfoDownload->setText(i18n("Grammalecte can be found <a href=\"https://grammalecte.net/#download\">here</a>. Download it and extract it."));
148 lay->addWidget(grammalecteInfoDownload);
149
150 return w;
151}
152
153void GrammalecteConfigWidget::loadSettings()
154{
155 mPythonPath->setPath(GrammalecteManager::self()->pythonPath());
156 mGrammalectePath->setPath(GrammalecteManager::self()->grammalectePath());
157 mSaveOptions = GrammalecteManager::self()->options();
158}
159
160void GrammalecteConfigWidget::saveSettings()
161{
162 QStringList result;
163 for (QCheckBox *checkBox : std::as_const(mListOptions)) {
164 if (checkBox->isChecked()) {
165 result += checkBox->property("optionname").toString();
166 }
167 }
168 GrammalecteManager::self()->setPythonPath(mPythonPath->path());
169 GrammalecteManager::self()->setGrammalectePath(mGrammalectePath->path());
170 GrammalecteManager::self()->setOptions(result);
171 GrammalecteManager::self()->saveSettings();
172}
173
174#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)
void addLayout(QLayout *layout, int stretch)
QIcon fromTheme(const QString &name)
void addWidget(QWidget *w)
qsizetype count() const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setObjectName(QAnyStringView name)
void setWidget(QWidget *widget)
void setWidgetResizable(bool resizable)
int addWidget(QWidget *widget)
void setCurrentWidget(QWidget *widget)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
RichText
QWidget(QWidget *parent, Qt::WindowFlags f)
QLayout * layout() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.