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 d->ui.languageList->setProperty("_breeze_force_frame", true);
87
88 for (int i = 0; i < d->ui.m_langCombo->count(); i++) {
89 const QString tag = d->ui.m_langCombo->itemData(i).toString();
90 if (tag.isEmpty()) { // skip separator
91 continue;
92 }
93 auto *item = new QListWidgetItem(d->ui.m_langCombo->itemText(i), d->ui.languageList);
94 item->setData(Qt::UserRole, tag);
95 }
96
97 d->ui.kcfg_backgroundCheckerEnabled->hide(); // hidden by default
98
99 connect(d->ui.addButton, &QAbstractButton::clicked, this, [this] {
100 d->slotIgnoreWordAdded();
101 });
102 connect(d->ui.removeButton, &QAbstractButton::clicked, this, [this] {
103 d->slotIgnoreWordRemoved();
104 });
105
106 layout->addWidget(d->wdg);
107 connect(d->ui.newIgnoreEdit, &QLineEdit::textChanged, this, [this](const QString &text) {
108 d->slotUpdateButton(text);
109 });
110 connect(d->ui.ignoreListWidget, &QListWidget::itemSelectionChanged, this, [this] {
111 d->slotSelectionChanged();
112 });
113 d->ui.addButton->setEnabled(false);
114 d->ui.removeButton->setEnabled(false);
115
116 connect(d->ui.m_langCombo, &DictionaryComboBox::dictionaryChanged, this, &ConfigView::configChanged);
117 connect(d->ui.languageList, &QListWidget::itemChanged, this, &ConfigView::configChanged);
118
119 connect(d->ui.kcfg_backgroundCheckerEnabled, &QAbstractButton::clicked, this, &ConfigView::configChanged);
120 connect(d->ui.kcfg_skipUppercase, &QAbstractButton::clicked, this, &ConfigView::configChanged);
121 connect(d->ui.kcfg_skipRunTogether, &QAbstractButton::clicked, this, &ConfigView::configChanged);
122 connect(d->ui.kcfg_checkerEnabledByDefault, &QAbstractButton::clicked, this, &ConfigView::configChanged);
123 connect(d->ui.kcfg_autodetectLanguage, &QAbstractButton::clicked, this, &ConfigView::configChanged);
124}
125
126ConfigView::~ConfigView() = default;
127
128void ConfigView::setNoBackendFoundVisible(bool show)
129{
130 d->ui.nobackendfound->setVisible(show);
131}
132
133bool ConfigView::noBackendFoundVisible() const
134{
135 return d->ui.nobackendfound->isVisible();
136}
137
138void ConfigView::setBackgroundCheckingButtonShown(bool b)
139{
140 d->ui.kcfg_backgroundCheckerEnabled->setVisible(b);
141}
142
143bool ConfigView::backgroundCheckingButtonShown() const
144{
145 return !d->ui.kcfg_backgroundCheckerEnabled->isHidden();
146}
147
148void ConfigView::setLanguage(const QString &language)
149{
150 d->ui.m_langCombo->setCurrentByDictionary(language);
151}
152
153QString ConfigView::language() const
154{
155 if (d->ui.m_langCombo->count()) {
156 return d->ui.m_langCombo->currentDictionary();
157 } else {
158 return QString();
159 }
160}
161
162void ConfigView::setPreferredLanguages(const QStringList &preferredLanguages)
163{
164 for (int i = 0; i < d->ui.languageList->count(); ++i) {
165 QListWidgetItem *item = d->ui.languageList->item(i);
166 QString tag = item->data(Qt::UserRole).toString();
167 if (preferredLanguages.contains(tag)) {
169 } else {
171 }
172 }
173 Q_EMIT configChanged();
174}
175
176QStringList ConfigView::preferredLanguages() const
177{
178 QStringList preferredLanguages;
179 for (int i = 0; i < d->ui.languageList->count(); i++) {
180 if (d->ui.languageList->item(i)->checkState() == Qt::Unchecked) {
181 continue;
182 }
183 preferredLanguages << d->ui.languageList->item(i)->data(Qt::UserRole).toString();
184 }
185 return preferredLanguages;
186}
187
188void ConfigView::setIgnoreList(const QStringList &ignoreList)
189{
190 d->ignoreList = ignoreList;
191 d->ignoreList.sort();
192 d->ui.ignoreListWidget->clear();
193 d->ui.ignoreListWidget->addItems(d->ignoreList);
194 Q_EMIT configChanged();
195}
196
197QStringList ConfigView::ignoreList() const
198{
199 return d->ignoreList;
200}
201
202#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 Fri May 10 2024 11:48:53 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.