Sonnet

configwidget.cpp
1/*
2 * configwidget.cpp
3 *
4 * SPDX-FileCopyrightText: 2004 Zack Rusin <zack@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-or-later
7 */
8#include "configwidget.h"
9#include "ui_configui.h"
10
11#include "loader_p.h"
12#include "settings.h"
13#include "settingsimpl_p.h"
14
15#include "ui_debug.h"
16
17#include <QCheckBox>
18#include <QLineEdit>
19#include <QListWidget>
20#include <QListWidgetItem>
21
22using namespace Sonnet;
23
24class Sonnet::ConfigWidgetPrivate
25{
26public:
27 Ui_SonnetConfigUI ui;
28 Settings *settings = nullptr;
29 QWidget *wdg = nullptr;
30};
31
32ConfigWidget::ConfigWidget(QWidget *parent)
33 : QWidget(parent)
34 , d(new ConfigWidgetPrivate)
35{
36 d->settings = new Settings(this);
37 QVBoxLayout *layout = new QVBoxLayout(this);
38 layout->setContentsMargins(0, 0, 0, 0);
39 layout->setObjectName(QStringLiteral("SonnetConfigUILayout"));
40 d->wdg = new QWidget(this);
41 d->ui.setupUi(d->wdg);
42 d->ui.languageList->setProperty("_breeze_force_frame", true);
43
44 d->ui.m_langCombo->setCurrentByDictionary(d->settings->defaultLanguage());
45
46 QStringList preferredLanguages = d->settings->preferredLanguages();
47 for (int i = 0; i < d->ui.m_langCombo->count(); i++) {
48 const QString tag = d->ui.m_langCombo->itemData(i).toString();
49 if (tag.isEmpty()) { // skip separator
50 continue;
51 }
52
53 QListWidgetItem *item = new QListWidgetItem(d->ui.m_langCombo->itemText(i), d->ui.languageList);
54 item->setData(Qt::UserRole, tag);
55 if (preferredLanguages.contains(tag)) {
57 } else {
59 }
60 }
61
62 d->ui.kcfg_skipUppercase->setChecked(d->settings->skipUppercase());
63 d->ui.kcfg_skipRunTogether->setChecked(d->settings->skipRunTogether());
64 d->ui.kcfg_checkerEnabledByDefault->setChecked(d->settings->checkerEnabledByDefault());
65 d->ui.kcfg_autodetectLanguage->setChecked(d->settings->autodetectLanguage());
66 QStringList ignoreList = d->settings->currentIgnoreList();
67 ignoreList.sort();
68 d->ui.ignoreListWidget->addItems(ignoreList);
69 d->ui.kcfg_backgroundCheckerEnabled->setChecked(d->settings->backgroundCheckerEnabled());
70 d->ui.kcfg_backgroundCheckerEnabled->hide(); // hidden by default
71 connect(d->ui.addButton, &QAbstractButton::clicked, this, &ConfigWidget::slotIgnoreWordAdded);
72 connect(d->ui.removeButton, &QAbstractButton::clicked, this, &ConfigWidget::slotIgnoreWordRemoved);
73
74 layout->addWidget(d->wdg);
77
78 connect(d->ui.kcfg_backgroundCheckerEnabled, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
79 connect(d->ui.kcfg_skipUppercase, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
80 connect(d->ui.kcfg_skipRunTogether, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
81 connect(d->ui.kcfg_checkerEnabledByDefault, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
82 connect(d->ui.kcfg_autodetectLanguage, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
83 connect(d->ui.newIgnoreEdit, &QLineEdit::textChanged, this, &ConfigWidget::slotUpdateButton);
84 connect(d->ui.ignoreListWidget, &QListWidget::itemSelectionChanged, this, &ConfigWidget::slotSelectionChanged);
85 d->ui.nobackendfound->setVisible(d->settings->clients().isEmpty());
86 d->ui.addButton->setEnabled(false);
87 d->ui.removeButton->setEnabled(false);
88}
89
90ConfigWidget::~ConfigWidget() = default;
91
92void ConfigWidget::slotUpdateButton(const QString &text)
93{
94 d->ui.addButton->setEnabled(!text.isEmpty());
95}
96
97void ConfigWidget::slotSelectionChanged()
98{
99 d->ui.removeButton->setEnabled(!d->ui.ignoreListWidget->selectedItems().isEmpty());
100}
101
102void ConfigWidget::save()
103{
104 setFromGui();
105}
106
107void ConfigWidget::setFromGui()
108{
109 if (d->ui.m_langCombo->count()) {
110 d->settings->setDefaultLanguage(d->ui.m_langCombo->currentDictionary());
111 }
112
113 QStringList preferredLanguages;
114 for (int i = 0; i < d->ui.languageList->count(); i++) {
115 if (d->ui.languageList->item(i)->checkState() == Qt::Unchecked) {
116 continue;
117 }
118 preferredLanguages << d->ui.languageList->item(i)->data(Qt::UserRole).toString();
119 }
120 d->settings->setPreferredLanguages(preferredLanguages);
121
122 d->settings->setSkipUppercase(d->ui.kcfg_skipUppercase->isChecked());
123 d->settings->setSkipRunTogether(d->ui.kcfg_skipRunTogether->isChecked());
124 d->settings->setBackgroundCheckerEnabled(d->ui.kcfg_backgroundCheckerEnabled->isChecked());
125 d->settings->setCheckerEnabledByDefault(d->ui.kcfg_checkerEnabledByDefault->isChecked());
126 d->settings->setAutodetectLanguage(d->ui.kcfg_autodetectLanguage->isChecked());
127
128 if (d->settings->modified()) {
129 d->settings->save();
130 }
131}
132
133void ConfigWidget::slotIgnoreWordAdded()
134{
135 QStringList ignoreList = d->settings->currentIgnoreList();
136 QString newWord = d->ui.newIgnoreEdit->text();
137 d->ui.newIgnoreEdit->clear();
138 if (newWord.isEmpty() || ignoreList.contains(newWord)) {
139 return;
140 }
141 ignoreList.append(newWord);
142 d->settings->setCurrentIgnoreList(ignoreList);
143
144 d->ui.ignoreListWidget->clear();
145 d->ui.ignoreListWidget->addItems(ignoreList);
146
148}
149
150void ConfigWidget::slotIgnoreWordRemoved()
151{
152 QStringList ignoreList = d->settings->currentIgnoreList();
153 const QList<QListWidgetItem *> selectedItems = d->ui.ignoreListWidget->selectedItems();
154 for (const QListWidgetItem *item : selectedItems) {
155 ignoreList.removeAll(item->text());
156 }
157 d->settings->setCurrentIgnoreList(ignoreList);
158
159 d->ui.ignoreListWidget->clear();
160 d->ui.ignoreListWidget->addItems(ignoreList);
161
163}
164
165void ConfigWidget::setBackgroundCheckingButtonShown(bool b)
166{
167 d->ui.kcfg_backgroundCheckerEnabled->setVisible(b);
168}
169
170bool ConfigWidget::backgroundCheckingButtonShown() const
171{
172 return !d->ui.kcfg_backgroundCheckerEnabled->isHidden();
173}
174
175void ConfigWidget::slotDefault()
176{
177 d->ui.kcfg_autodetectLanguage->setChecked(Settings::defaultAutodetectLanguage());
178 d->ui.kcfg_skipUppercase->setChecked(Settings::defaultSkipUppercase());
179 d->ui.kcfg_skipRunTogether->setChecked(Settings::defauktSkipRunTogether());
180 d->ui.kcfg_checkerEnabledByDefault->setChecked(Settings::defaultCheckerEnabledByDefault());
181 d->ui.kcfg_backgroundCheckerEnabled->setChecked(Settings::defaultBackgroundCheckerEnabled());
182 d->ui.ignoreListWidget->clear();
183 d->ui.m_langCombo->setCurrentByDictionary(d->settings->defaultLanguage());
184}
185
187{
188 d->ui.m_langCombo->setCurrentByDictionary(language);
189}
190
192{
193 if (d->ui.m_langCombo->count()) {
194 return d->ui.m_langCombo->currentDictionary();
195 } else {
196 return QString();
197 }
198}
199
200#include "moc_configwidget.cpp"
void configChanged()
Signal sends when config was changed.
void setLanguage(const QString &language)
Sets the language/dictionary that will be selected by default in this config widget.
QString language() const
Get the currently selected language for spell checking.
void dictionaryChanged(const QString &dictionary)
Emitted whenever the current dictionary changes.
The sonnet namespace.
void clicked(bool checked)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
void append(QList< T > &&value)
qsizetype removeAll(const AT &t)
void itemChanged(QListWidgetItem *item)
void itemSelectionChanged()
void setCheckState(Qt::CheckState state)
virtual void setData(int role, const QVariant &value)
QString text() const const
Q_EMITQ_EMIT
void setObjectName(QAnyStringView name)
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
void sort(Qt::CaseSensitivity cs)
UserRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.