Sonnet

configwidget.cpp
1 /*
2  * configwidget.cpp
3  *
4  * SPDX-FileCopyrightText: 2004 Zack Rusin <[email protected]>
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 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(0, 0, 0, 0);
39  layout->setObjectName(QStringLiteral("SonnetConfigUILayout"));
40  d->wdg = new QWidget(this);
41  d->ui.setupUi(d->wdg);
42 
43  d->ui.m_langCombo->setCurrentByDictionary(d->settings->defaultLanguage());
44 
45  QStringList preferredLanguages = d->settings->preferredLanguages();
46  for (int i = 0; i < d->ui.m_langCombo->count(); i++) {
47  const QString tag = d->ui.m_langCombo->itemData(i).toString();
48  if (tag.isEmpty()) { // skip separator
49  continue;
50  }
51 
52  QListWidgetItem *item = new QListWidgetItem(d->ui.m_langCombo->itemText(i), d->ui.languageList);
53  item->setData(Qt::UserRole, tag);
54  if (preferredLanguages.contains(tag)) {
56  } else {
58  }
59  }
60 
61  d->ui.kcfg_skipUppercase->setChecked(d->settings->skipUppercase());
62  d->ui.kcfg_skipRunTogether->setChecked(d->settings->skipRunTogether());
63  d->ui.kcfg_checkerEnabledByDefault->setChecked(d->settings->checkerEnabledByDefault());
64  d->ui.kcfg_autodetectLanguage->setChecked(d->settings->autodetectLanguage());
65  QStringList ignoreList = d->settings->currentIgnoreList();
66  ignoreList.sort();
67  d->ui.ignoreListWidget->addItems(ignoreList);
68  d->ui.kcfg_backgroundCheckerEnabled->setChecked(d->settings->backgroundCheckerEnabled());
69  d->ui.kcfg_backgroundCheckerEnabled->hide(); // hidden by default
70  connect(d->ui.addButton, &QAbstractButton::clicked, this, &ConfigWidget::slotIgnoreWordAdded);
71  connect(d->ui.removeButton, &QAbstractButton::clicked, this, &ConfigWidget::slotIgnoreWordRemoved);
72 
73  layout->addWidget(d->wdg);
75  connect(d->ui.languageList, &QListWidget::itemChanged, this, &ConfigWidget::configChanged);
76 
77  connect(d->ui.kcfg_backgroundCheckerEnabled, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
78  connect(d->ui.kcfg_skipUppercase, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
79  connect(d->ui.kcfg_skipRunTogether, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
80  connect(d->ui.kcfg_checkerEnabledByDefault, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
81  connect(d->ui.kcfg_autodetectLanguage, &QAbstractButton::clicked, this, &ConfigWidget::configChanged);
82  connect(d->ui.newIgnoreEdit, &QLineEdit::textChanged, this, &ConfigWidget::slotUpdateButton);
83  connect(d->ui.ignoreListWidget, &QListWidget::itemSelectionChanged, this, &ConfigWidget::slotSelectionChanged);
84  d->ui.nobackendfound->setVisible(d->settings->clients().isEmpty());
85  d->ui.addButton->setEnabled(false);
86  d->ui.removeButton->setEnabled(false);
87 }
88 
89 ConfigWidget::~ConfigWidget()
90 {
91  delete d;
92 }
93 
94 void ConfigWidget::slotUpdateButton(const QString &text)
95 {
96  d->ui.addButton->setEnabled(!text.isEmpty());
97 }
98 
99 void ConfigWidget::slotSelectionChanged()
100 {
101  d->ui.removeButton->setEnabled(!d->ui.ignoreListWidget->selectedItems().isEmpty());
102 }
103 
104 void ConfigWidget::save()
105 {
106  setFromGui();
107 }
108 
109 void ConfigWidget::setFromGui()
110 {
111  if (d->ui.m_langCombo->count()) {
112  d->settings->setDefaultLanguage(d->ui.m_langCombo->currentDictionary());
113  }
114 
115  QStringList preferredLanguages;
116  for (int i = 0; i < d->ui.languageList->count(); i++) {
117  if (d->ui.languageList->item(i)->checkState() == Qt::Unchecked) {
118  continue;
119  }
120  preferredLanguages << d->ui.languageList->item(i)->data(Qt::UserRole).toString();
121  }
122  d->settings->setPreferredLanguages(preferredLanguages);
123 
124  d->settings->setSkipUppercase(d->ui.kcfg_skipUppercase->isChecked());
125  d->settings->setSkipRunTogether(d->ui.kcfg_skipRunTogether->isChecked());
126  d->settings->setBackgroundCheckerEnabled(d->ui.kcfg_backgroundCheckerEnabled->isChecked());
127  d->settings->setCheckerEnabledByDefault(d->ui.kcfg_checkerEnabledByDefault->isChecked());
128  d->settings->setAutodetectLanguage(d->ui.kcfg_autodetectLanguage->isChecked());
129 
130  if (d->settings->modified()) {
131  d->settings->save();
132  }
133 }
134 
135 void ConfigWidget::slotIgnoreWordAdded()
136 {
137  QStringList ignoreList = d->settings->currentIgnoreList();
138  QString newWord = d->ui.newIgnoreEdit->text();
139  d->ui.newIgnoreEdit->clear();
140  if (newWord.isEmpty() || ignoreList.contains(newWord)) {
141  return;
142  }
143  ignoreList.append(newWord);
144  d->settings->setCurrentIgnoreList(ignoreList);
145 
146  d->ui.ignoreListWidget->clear();
147  d->ui.ignoreListWidget->addItems(ignoreList);
148 
150 }
151 
152 void ConfigWidget::slotIgnoreWordRemoved()
153 {
154  QStringList ignoreList = d->settings->currentIgnoreList();
155  const QList<QListWidgetItem *> selectedItems = d->ui.ignoreListWidget->selectedItems();
156  for (const QListWidgetItem *item : selectedItems) {
157  ignoreList.removeAll(item->text());
158  }
159  d->settings->setCurrentIgnoreList(ignoreList);
160 
161  d->ui.ignoreListWidget->clear();
162  d->ui.ignoreListWidget->addItems(ignoreList);
163 
165 }
166 
167 void ConfigWidget::setBackgroundCheckingButtonShown(bool b)
168 {
169  d->ui.kcfg_backgroundCheckerEnabled->setVisible(b);
170 }
171 
172 bool ConfigWidget::backgroundCheckingButtonShown() const
173 {
174  return !d->ui.kcfg_backgroundCheckerEnabled->isHidden();
175 }
176 
177 void ConfigWidget::slotDefault()
178 {
179  d->ui.kcfg_autodetectLanguage->setChecked(Settings::defaultAutodetectLanguage());
180  d->ui.kcfg_skipUppercase->setChecked(Settings::defaultSkipUppercase());
181  d->ui.kcfg_skipRunTogether->setChecked(Settings::defauktSkipRunTogether());
182  d->ui.kcfg_checkerEnabledByDefault->setChecked(Settings::defaultCheckerEnabledByDefault());
183  d->ui.kcfg_backgroundCheckerEnabled->setChecked(Settings::defaultBackgroundCheckerEnabled());
184  d->ui.ignoreListWidget->clear();
185  d->ui.m_langCombo->setCurrentByDictionary(d->settings->defaultLanguage());
186 }
187 
188 void ConfigWidget::setLanguage(const QString &language)
189 {
190  d->ui.m_langCombo->setCurrentByDictionary(language);
191 }
192 
194 {
195  if (d->ui.m_langCombo->count()) {
196  return d->ui.m_langCombo->currentDictionary();
197  } else {
198  return QString();
199  }
200 }
void append(const T &value)
QString text() const const
UserRole
QWidget(QWidget *parent, Qt::WindowFlags f)
Q_EMITQ_EMIT
int removeAll(const T &value)
QLayout * layout() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void clicked(bool checked)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void dictionaryChanged(const QString &dictionary)
Emitted whenever the current dictionary changes.
void setCheckState(Qt::CheckState state)
void setLanguage(const QString &language)
Sets the language/dictionary that will be selected by default in this config widget.
virtual void setData(int role, const QVariant &value)
void textChanged(const QString &text)
bool isEmpty() const const
The sonnet namespace.
void configChanged()
Signal sends when config was changed.
void setObjectName(const QString &name)
void addWidget(QWidget *w)
void setContentsMargins(int left, int top, int right, int bottom)
QString language() const
Get the currently selected language for spell checking.
void itemSelectionChanged()
void itemChanged(QListWidgetItem *item)
void sort(Qt::CaseSensitivity cs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Sep 25 2022 04:14:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.