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 | |
22 | using namespace Sonnet; |
23 | |
24 | class Sonnet::ConfigWidgetPrivate |
25 | { |
26 | public: |
27 | Ui_SonnetConfigUI ui; |
28 | Settings *settings = nullptr; |
29 | QWidget *wdg = nullptr; |
30 | }; |
31 | |
32 | ConfigWidget::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(left: 0, top: 0, right: 0, bottom: 0); |
39 | layout->setObjectName(QStringLiteral("SonnetConfigUILayout" )); |
40 | d->wdg = new QWidget(this); |
41 | d->ui.setupUi(d->wdg); |
42 | d->ui.languageList->setProperty(name: "_breeze_force_frame" , value: 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(index: i).toString(); |
49 | if (tag.isEmpty()) { // skip separator |
50 | continue; |
51 | } |
52 | |
53 | QListWidgetItem *item = new QListWidgetItem(d->ui.m_langCombo->itemText(index: i), d->ui.languageList); |
54 | item->setData(role: Qt::UserRole, value: tag); |
55 | if (preferredLanguages.contains(str: tag)) { |
56 | item->setCheckState(Qt::Checked); |
57 | } else { |
58 | item->setCheckState(Qt::Unchecked); |
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(labels: ignoreList); |
69 | d->ui.kcfg_backgroundCheckerEnabled->setChecked(d->settings->backgroundCheckerEnabled()); |
70 | d->ui.kcfg_backgroundCheckerEnabled->hide(); // hidden by default |
71 | connect(sender: d->ui.addButton, signal: &QAbstractButton::clicked, context: this, slot: &ConfigWidget::slotIgnoreWordAdded); |
72 | connect(sender: d->ui.removeButton, signal: &QAbstractButton::clicked, context: this, slot: &ConfigWidget::slotIgnoreWordRemoved); |
73 | |
74 | layout->addWidget(d->wdg); |
75 | connect(sender: d->ui.m_langCombo, signal: &DictionaryComboBox::dictionaryChanged, context: this, slot: &ConfigWidget::configChanged); |
76 | connect(sender: d->ui.languageList, signal: &QListWidget::itemChanged, context: this, slot: &ConfigWidget::configChanged); |
77 | |
78 | connect(sender: d->ui.kcfg_backgroundCheckerEnabled, signal: &QAbstractButton::clicked, context: this, slot: &ConfigWidget::configChanged); |
79 | connect(sender: d->ui.kcfg_skipUppercase, signal: &QAbstractButton::clicked, context: this, slot: &ConfigWidget::configChanged); |
80 | connect(sender: d->ui.kcfg_skipRunTogether, signal: &QAbstractButton::clicked, context: this, slot: &ConfigWidget::configChanged); |
81 | connect(sender: d->ui.kcfg_checkerEnabledByDefault, signal: &QAbstractButton::clicked, context: this, slot: &ConfigWidget::configChanged); |
82 | connect(sender: d->ui.kcfg_autodetectLanguage, signal: &QAbstractButton::clicked, context: this, slot: &ConfigWidget::configChanged); |
83 | connect(sender: d->ui.newIgnoreEdit, signal: &QLineEdit::textChanged, context: this, slot: &ConfigWidget::slotUpdateButton); |
84 | connect(sender: d->ui.ignoreListWidget, signal: &QListWidget::itemSelectionChanged, context: this, slot: &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 | |
90 | ConfigWidget::~ConfigWidget() = default; |
91 | |
92 | void ConfigWidget::slotUpdateButton(const QString &text) |
93 | { |
94 | d->ui.addButton->setEnabled(!text.isEmpty()); |
95 | } |
96 | |
97 | void ConfigWidget::slotSelectionChanged() |
98 | { |
99 | d->ui.removeButton->setEnabled(!d->ui.ignoreListWidget->selectedItems().isEmpty()); |
100 | } |
101 | |
102 | void ConfigWidget::save() |
103 | { |
104 | setFromGui(); |
105 | } |
106 | |
107 | void 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(row: i)->checkState() == Qt::Unchecked) { |
116 | continue; |
117 | } |
118 | preferredLanguages << d->ui.languageList->item(row: i)->data(role: 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 | |
133 | void 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(str: newWord)) { |
139 | return; |
140 | } |
141 | ignoreList.append(t: newWord); |
142 | d->settings->setCurrentIgnoreList(ignoreList); |
143 | |
144 | d->ui.ignoreListWidget->clear(); |
145 | d->ui.ignoreListWidget->addItems(labels: ignoreList); |
146 | |
147 | Q_EMIT configChanged(); |
148 | } |
149 | |
150 | void 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(t: item->text()); |
156 | } |
157 | d->settings->setCurrentIgnoreList(ignoreList); |
158 | |
159 | d->ui.ignoreListWidget->clear(); |
160 | d->ui.ignoreListWidget->addItems(labels: ignoreList); |
161 | |
162 | Q_EMIT configChanged(); |
163 | } |
164 | |
165 | void ConfigWidget::setBackgroundCheckingButtonShown(bool b) |
166 | { |
167 | d->ui.kcfg_backgroundCheckerEnabled->setVisible(b); |
168 | } |
169 | |
170 | bool ConfigWidget::backgroundCheckingButtonShown() const |
171 | { |
172 | return !d->ui.kcfg_backgroundCheckerEnabled->isHidden(); |
173 | } |
174 | |
175 | void 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 | |
186 | void ConfigWidget::setLanguage(const QString &language) |
187 | { |
188 | d->ui.m_langCombo->setCurrentByDictionary(language); |
189 | } |
190 | |
191 | QString ConfigWidget::language() const |
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" |
201 | |