KTextAddons

languagetoolconfigwidget.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 "languagetoolconfigwidget.h"
8#include "languagetoolcombobox.h"
9#include "languagetoolmanager.h"
10#include "languagetoolupdatecombobox.h"
11#include <KLineEditEventHandler>
12#include <KLocalizedString>
13
14#include <QCheckBox>
15#include <QLabel>
16#include <QLineEdit>
17#include <QToolButton>
18#include <QVBoxLayout>
19using namespace TextGrammarCheck;
20LanguageToolConfigWidget::LanguageToolConfigWidget(QWidget *parent)
21 : QWidget(parent)
22 , mUseLocalInstance(new QCheckBox(i18nc("@option:check", "Use Local Instance"), this))
23 , mInstancePath(new QLineEdit(this))
24 , mInstancePathLabel(new QLabel(i18nc("@label:textbox", "Instance Path:"), this))
25 , mLanguageToolCombobox(new LanguageToolComboBox(this))
26 , mLanguageToolUpdateCombobox(new LanguageToolUpdateComboBox(this))
27{
28 auto mainLayout = new QVBoxLayout(this);
29 mainLayout->setObjectName(QStringLiteral("mainlayout"));
30 mainLayout->setContentsMargins({});
31
32 mUseLocalInstance->setObjectName(QStringLiteral("uselocalinstance"));
33 mainLayout->addWidget(mUseLocalInstance);
34
35 auto instanceLayout = new QHBoxLayout;
36 instanceLayout->setObjectName(QStringLiteral("instancelayout"));
37 instanceLayout->setContentsMargins({});
38 mInstancePathLabel->setObjectName(QStringLiteral("instancepath"));
39 mInstancePathLabel->setEnabled(false);
40 mInstancePathLabel->setTextFormat(Qt::PlainText);
41 instanceLayout->addWidget(mInstancePathLabel);
42
43 mInstancePath->setObjectName(QStringLiteral("instancepath"));
44 mInstancePath->setEnabled(false);
45 mInstancePath->setClearButtonEnabled(true);
47 instanceLayout->addWidget(mInstancePath);
48 mainLayout->addLayout(instanceLayout);
49
50 connect(mUseLocalInstance, &QCheckBox::clicked, this, &LanguageToolConfigWidget::updateWidgets);
51
52 auto languageLayout = new QHBoxLayout;
53 languageLayout->setObjectName(QStringLiteral("languagelayout"));
54 auto languageLabel = new QLabel(i18nc("@label:textbox", "Language:"), this);
55 languageLabel->setTextFormat(Qt::PlainText);
56 languageLabel->setObjectName(QStringLiteral("languageLabel"));
57 languageLayout->addWidget(languageLabel);
58
59 mLanguageToolCombobox->setObjectName(QStringLiteral("languagecombobox"));
60 languageLayout->addWidget(mLanguageToolCombobox);
61 mLanguageToolUpdateCombobox->setLanguageToolCombobox(mLanguageToolCombobox);
62 mLanguageToolUpdateCombobox->setParentWidget(this);
63
64 auto refreshButton = new QToolButton(this);
65 refreshButton->setObjectName(QStringLiteral("refreshbutton"));
66 refreshButton->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
67 refreshButton->setToolTip(i18nc("@info:tooltip", "Refresh"));
68 if (!LanguageToolManager::self()->allowToGetListOfLanguages()) {
69 refreshButton->setVisible(false);
70 }
71 languageLayout->addWidget(refreshButton);
72 connect(refreshButton, &QToolButton::clicked, this, [this]() {
73 if (LanguageToolManager::self()->allowToGetListOfLanguages()) {
74 mLanguageToolUpdateCombobox->checkListOfLanguagesFromSpecificPath(LanguageToolManager::convertToLanguagePath(mInstancePath->text()));
75 }
76 });
77
78 mainLayout->addLayout(languageLayout);
79
80 mainLayout->addStretch(1);
81 mLanguageToolUpdateCombobox->refreshListOfLanguages();
82 loadSettings();
83 updateWidgets(mUseLocalInstance->isChecked());
84 connect(this, &LanguageToolConfigWidget::resetValue, this, &LanguageToolConfigWidget::slotResetValue);
85}
86
87LanguageToolConfigWidget::~LanguageToolConfigWidget()
88{
89 saveSettings();
90}
91
92void LanguageToolConfigWidget::slotResetValue()
93{
94 mUseLocalInstance->setChecked(false);
95 mInstancePath->setText(QStringLiteral("https://api.languagetoolplus.com/v2"));
96 updateWidgets(false);
97}
98
99void LanguageToolConfigWidget::updateWidgets(bool enabled)
100{
101 mInstancePathLabel->setEnabled(enabled);
102 mInstancePath->setEnabled(enabled);
103}
104
105void LanguageToolConfigWidget::loadSettings()
106{
107 mUseLocalInstance->setChecked(LanguageToolManager::self()->useLocalInstance());
108 mInstancePath->setText(LanguageToolManager::self()->languageToolPath());
109 mLanguageToolCombobox->setLanguage(LanguageToolManager::self()->language());
110}
111
112void LanguageToolConfigWidget::saveSettings()
113{
114 LanguageToolManager::self()->setUseLocalInstance(mUseLocalInstance->isChecked());
115 LanguageToolManager::self()->setLanguageToolPath(mInstancePath->text());
116 LanguageToolManager::self()->setLanguage(mLanguageToolCombobox->language());
117 LanguageToolManager::self()->saveSettings();
118}
119
120#include "moc_languagetoolconfigwidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
void clicked(bool checked)
QIcon fromTheme(const QString &name)
void setObjectName(QAnyStringView name)
PlainText
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
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.