Sonnet

configview.cpp
1/*
2 * configwidget.cpp
3 *
4 * SPDX-FileCopyrightText: 2004 Zack Rusin <zack@kde.org>
5 * SPDX-FileCopyrightText: 2020 Benjamin Port <benjamin.port@kde.org>
6 *
7 * SPDX-License-Identifier: LGPL-2.1-or-later
8 */
9#include "configview.h"
10#include "ui_configui.h"
11
12#include "ui_debug.h"
13
14#include <QCheckBox>
15#include <QLineEdit>
16#include <QListWidget>
17
18using namespace Sonnet;
19
20class Sonnet::ConfigViewPrivate
21{
22public:
23 explicit ConfigViewPrivate(ConfigView *v);
24 Ui_SonnetConfigUI ui;
25 QWidget *wdg = nullptr;
26 QStringList ignoreList;
27 ConfigView *q;
28 void slotUpdateButton(const QString &text);
29 void slotSelectionChanged();
30 void slotIgnoreWordAdded();
31 void slotIgnoreWordRemoved();
32};
33
34ConfigViewPrivate::ConfigViewPrivate(ConfigView *v)
35{
36 q = v;
37}
38
39void ConfigViewPrivate::slotUpdateButton(const QString &text)
40{
41 ui.addButton->setEnabled(!text.isEmpty());
42}
43
44void ConfigViewPrivate::slotSelectionChanged()
45{
46 ui.removeButton->setEnabled(!ui.ignoreListWidget->selectedItems().isEmpty());
47}
48
49void ConfigViewPrivate::slotIgnoreWordAdded()
50{
51 QString newWord = ui.newIgnoreEdit->text();
52 ui.newIgnoreEdit->clear();
53 if (newWord.isEmpty() || ignoreList.contains(newWord)) {
54 return;
55 }
56 ignoreList.append(newWord);
57
58 ui.ignoreListWidget->clear();
59 ui.ignoreListWidget->addItems(ignoreList);
60
61 Q_EMIT q->configChanged();
62}
63
64void ConfigViewPrivate::slotIgnoreWordRemoved()
65{
66 const QList<QListWidgetItem *> selectedItems = ui.ignoreListWidget->selectedItems();
67 for (const QListWidgetItem *item : selectedItems) {
68 ignoreList.removeAll(item->text());
69 }
70
71 ui.ignoreListWidget->clear();
72 ui.ignoreListWidget->addItems(ignoreList);
73
74 Q_EMIT q->configChanged();
75}
76
77ConfigView::ConfigView(QWidget *parent)
78 : QWidget(parent)
79 , d(new ConfigViewPrivate(this))
80{
81 auto *layout = new QVBoxLayout(this);
82 layout->setContentsMargins(0, 0, 0, 0);
83 layout->setObjectName(QStringLiteral("SonnetConfigUILayout"));
84 d->wdg = new QWidget(this);
85 d->ui.setupUi(d->wdg);
86
87 for (int i = 0; i < d->ui.m_langCombo->count(); i++) {
88 const QString tag = d->ui.m_langCombo->itemData(i).toString();
89 if (tag.isEmpty()) { // skip separator
90 continue;
91 }
92 auto *item = new QListWidgetItem(d->ui.m_langCombo->itemText(i), d->ui.languageList);
93 item->setData(Qt::UserRole, tag);
94 }
95
96 d->ui.kcfg_backgroundCheckerEnabled->hide(); // hidden by default
97
98 connect(d->ui.addButton, &QAbstractButton::clicked, this, [this] {
99 d->slotIgnoreWordAdded();
100 });
101 connect(d->ui.removeButton, &QAbstractButton::clicked, this, [this] {
102 d->slotIgnoreWordRemoved();
103 });
104
105 layout->addWidget(d->wdg);
106 connect(d->ui.newIgnoreEdit, &QLineEdit::textChanged, this, [this](const QString &text) {
107 d->slotUpdateButton(text);
108 });
109 connect(d->ui.ignoreListWidget, &QListWidget::itemSelectionChanged, this, [this] {
110 d->slotSelectionChanged();
111 });
112 d->ui.addButton->setEnabled(false);
113 d->ui.removeButton->setEnabled(false);
114
115 connect(d->ui.m_langCombo, &DictionaryComboBox::dictionaryChanged, this, &ConfigView::configChanged);
116 connect(d->ui.languageList, &QListWidget::itemChanged, this, &ConfigView::configChanged);
117
118 connect(d->ui.kcfg_backgroundCheckerEnabled, &QAbstractButton::clicked, this, &ConfigView::configChanged);
119 connect(d->ui.kcfg_skipUppercase, &QAbstractButton::clicked, this, &ConfigView::configChanged);
120 connect(d->ui.kcfg_skipRunTogether, &QAbstractButton::clicked, this, &ConfigView::configChanged);
121 connect(d->ui.kcfg_checkerEnabledByDefault, &QAbstractButton::clicked, this, &ConfigView::configChanged);
122 connect(d->ui.kcfg_autodetectLanguage, &QAbstractButton::clicked, this, &ConfigView::configChanged);
123}
124
125ConfigView::~ConfigView() = default;
126
127void ConfigView::setNoBackendFoundVisible(bool show)
128{
129 d->ui.nobackendfound->setVisible(show);
130}
131
132bool ConfigView::noBackendFoundVisible() const
133{
134 return d->ui.nobackendfound->isVisible();
135}
136
137void ConfigView::setBackgroundCheckingButtonShown(bool b)
138{
139 d->ui.kcfg_backgroundCheckerEnabled->setVisible(b);
140}
141
142bool ConfigView::backgroundCheckingButtonShown() const
143{
144 return !d->ui.kcfg_backgroundCheckerEnabled->isHidden();
145}
146
147void ConfigView::setLanguage(const QString &language)
148{
149 d->ui.m_langCombo->setCurrentByDictionary(language);
150}
151
152QString ConfigView::language() const
153{
154 if (d->ui.m_langCombo->count()) {
155 return d->ui.m_langCombo->currentDictionary();
156 } else {
157 return QString();
158 }
159}
160
161void ConfigView::setPreferredLanguages(const QStringList &preferredLanguages)
162{
163 for (int i = 0; i < d->ui.languageList->count(); ++i) {
164 QListWidgetItem *item = d->ui.languageList->item(i);
165 QString tag = item->data(Qt::UserRole).toString();
166 if (preferredLanguages.contains(tag)) {
168 } else {
170 }
171 }
172 Q_EMIT configChanged();
173}
174
175QStringList ConfigView::preferredLanguages() const
176{
177 QStringList preferredLanguages;
178 for (int i = 0; i < d->ui.languageList->count(); i++) {
179 if (d->ui.languageList->item(i)->checkState() == Qt::Unchecked) {
180 continue;
181 }
182 preferredLanguages << d->ui.languageList->item(i)->data(Qt::UserRole).toString();
183 }
184 return preferredLanguages;
185}
186
187void ConfigView::setIgnoreList(const QStringList &ignoreList)
188{
189 d->ignoreList = ignoreList;
190 d->ignoreList.sort();
191 d->ui.ignoreListWidget->clear();
192 d->ui.ignoreListWidget->addItems(d->ignoreList);
193 Q_EMIT configChanged();
194}
195
196QStringList ConfigView::ignoreList() const
197{
198 return d->ignoreList;
199}
200
201#include "moc_configview.cpp"
void dictionaryChanged(const QString &dictionary)
Emitted whenever the current dictionary changes.
The sonnet namespace.
void clicked(bool checked)
void textChanged(const QString &text)
void append(QList< T > &&value)
qsizetype removeAll(const AT &t)
void itemChanged(QListWidgetItem *item)
void itemSelectionChanged()
virtual QVariant data(int role) const const
void setCheckState(Qt::CheckState state)
Q_EMITQ_EMIT
T qobject_cast(QObject *object)
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
UserRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:10 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.