KTextAddons

languagetoolconfigwidget.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 "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 instanceLayout->addWidget(mInstancePathLabel);
41
42 mInstancePath->setObjectName(QStringLiteral("instancepath"));
43 mInstancePath->setEnabled(false);
44 mInstancePath->setClearButtonEnabled(true);
46 instanceLayout->addWidget(mInstancePath);
47 mainLayout->addLayout(instanceLayout);
48
49 connect(mUseLocalInstance, &QCheckBox::clicked, this, &LanguageToolConfigWidget::updateWidgets);
50
51 auto languageLayout = new QHBoxLayout;
52 languageLayout->setObjectName(QStringLiteral("languagelayout"));
53 auto languageLabel = new QLabel(i18nc("@label:textbox", "Language:"), this);
54 languageLabel->setObjectName(QStringLiteral("languageLabel"));
55 languageLayout->addWidget(languageLabel);
56
57 mLanguageToolCombobox->setObjectName(QStringLiteral("languagecombobox"));
58 languageLayout->addWidget(mLanguageToolCombobox);
59 mLanguageToolUpdateCombobox->setLanguageToolCombobox(mLanguageToolCombobox);
60 mLanguageToolUpdateCombobox->setParentWidget(this);
61
62 auto refreshButton = new QToolButton(this);
63 refreshButton->setObjectName(QStringLiteral("refreshbutton"));
64 refreshButton->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
65 refreshButton->setToolTip(i18nc("@info:tooltip", "Refresh"));
66 if (!LanguageToolManager::self()->allowToGetListOfLanguages()) {
67 refreshButton->setVisible(false);
68 }
69 languageLayout->addWidget(refreshButton);
70 connect(refreshButton, &QToolButton::clicked, this, [this]() {
71 if (LanguageToolManager::self()->allowToGetListOfLanguages()) {
72 mLanguageToolUpdateCombobox->checkListOfLanguagesFromSpecificPath(LanguageToolManager::convertToLanguagePath(mInstancePath->text()));
73 }
74 });
75
76 mainLayout->addLayout(languageLayout);
77
78 mainLayout->addStretch(1);
79 mLanguageToolUpdateCombobox->refreshListOfLanguages();
80 loadSettings();
81 updateWidgets(mUseLocalInstance->isChecked());
82 connect(this, &LanguageToolConfigWidget::resetValue, this, &LanguageToolConfigWidget::slotResetValue);
83}
84
85LanguageToolConfigWidget::~LanguageToolConfigWidget()
86{
87 saveSettings();
88}
89
90void LanguageToolConfigWidget::slotResetValue()
91{
92 mUseLocalInstance->setChecked(false);
93 mInstancePath->setText(QStringLiteral("https://api.languagetoolplus.com/v2"));
94 updateWidgets(false);
95}
96
97void LanguageToolConfigWidget::updateWidgets(bool enabled)
98{
99 mInstancePathLabel->setEnabled(enabled);
100 mInstancePath->setEnabled(enabled);
101}
102
103void LanguageToolConfigWidget::loadSettings()
104{
105 mUseLocalInstance->setChecked(LanguageToolManager::self()->useLocalInstance());
106 mInstancePath->setText(LanguageToolManager::self()->languageToolPath());
107 mLanguageToolCombobox->setLanguage(LanguageToolManager::self()->language());
108}
109
110void LanguageToolConfigWidget::saveSettings()
111{
112 LanguageToolManager::self()->setUseLocalInstance(mUseLocalInstance->isChecked());
113 LanguageToolManager::self()->setLanguageToolPath(mInstancePath->text());
114 LanguageToolManager::self()->setLanguage(mLanguageToolCombobox->language());
115 LanguageToolManager::self()->saveSettings();
116}
117
118#include "moc_languagetoolconfigwidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
void setChecked(bool)
void clicked(bool checked)
QIcon fromTheme(const QString &name)
void setText(const QString &)
void setObjectName(QAnyStringView name)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:48:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.