Sonnet

configview.cpp
1 /*
2  * configwidget.cpp
3  *
4  * SPDX-FileCopyrightText: 2004 Zack Rusin <[email protected]>
5  * SPDX-FileCopyrightText: 2020 Benjamin Port <[email protected]>
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 
18 using namespace Sonnet;
19 
20 class ConfigViewPrivate
21 {
22 public:
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 
34 ConfigViewPrivate::ConfigViewPrivate(ConfigView *v)
35 {
36  q = v;
37 }
38 
39 void ConfigViewPrivate::slotUpdateButton(const QString &text)
40 {
41  ui.addButton->setEnabled(!text.isEmpty());
42 }
43 
44 void ConfigViewPrivate::slotSelectionChanged()
45 {
46  ui.removeButton->setEnabled(!ui.ignoreListWidget->selectedItems().isEmpty());
47 }
48 
49 void 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 
64 void 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 
77 ConfigView::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 
125 ConfigView::~ConfigView()
126 {
127  delete d;
128 }
129 
130 void ConfigView::setNoBackendFoundVisible(bool show)
131 {
132  d->ui.nobackendfound->setVisible(show);
133 }
134 
135 bool ConfigView::noBackendFoundVisible() const
136 {
137  return d->ui.nobackendfound->isVisible();
138 }
139 
140 void ConfigView::setBackgroundCheckingButtonShown(bool b)
141 {
142  d->ui.kcfg_backgroundCheckerEnabled->setVisible(b);
143 }
144 
145 bool ConfigView::backgroundCheckingButtonShown() const
146 {
147  return !d->ui.kcfg_backgroundCheckerEnabled->isHidden();
148 }
149 
150 void ConfigView::setLanguage(const QString &language)
151 {
152  d->ui.m_langCombo->setCurrentByDictionary(language);
153 }
154 
155 QString ConfigView::language() const
156 {
157  if (d->ui.m_langCombo->count()) {
158  return d->ui.m_langCombo->currentDictionary();
159  } else {
160  return QString();
161  }
162 }
163 
164 void ConfigView::setPreferredLanguages(const QStringList &preferredLanguages)
165 {
166  for (int i = 0; i < d->ui.languageList->count(); ++i) {
167  QListWidgetItem *item = d->ui.languageList->item(i);
168  QString tag = item->data(Qt::UserRole).toString();
169  if (preferredLanguages.contains(tag)) {
170  item->setCheckState(Qt::Checked);
171  } else {
173  }
174  }
175  Q_EMIT configChanged();
176 }
177 
178 QStringList ConfigView::preferredLanguages() const
179 {
180  QStringList preferredLanguages;
181  for (int i = 0; i < d->ui.languageList->count(); i++) {
182  if (d->ui.languageList->item(i)->checkState() == Qt::Unchecked) {
183  continue;
184  }
185  preferredLanguages << d->ui.languageList->item(i)->data(Qt::UserRole).toString();
186  }
187  return preferredLanguages;
188 }
189 
190 void ConfigView::setIgnoreList(const QStringList &ignoreList)
191 {
192  d->ignoreList = ignoreList;
193  d->ignoreList.sort();
194  d->ui.ignoreListWidget->clear();
195  d->ui.ignoreListWidget->addItems(d->ignoreList);
196  Q_EMIT configChanged();
197 }
198 
199 QStringList ConfigView::ignoreList() const
200 {
201  return d->ignoreList;
202 }
203 
204 #include "moc_configview.cpp"
UserRole
QWidget(QWidget *parent, Qt::WindowFlags f)
int removeAll(const T &value)
QLayout * layout() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void clear()
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)
virtual QVariant data(int role) const const
void textChanged(const QString &text)
bool isEmpty() const const
The sonnet namespace.
void setObjectName(const QString &name)
void addWidget(QWidget *w)
void setContentsMargins(int left, int top, int right, int bottom)
void itemSelectionChanged()
void itemChanged(QListWidgetItem *item)
void sort(Qt::CaseSensitivity cs)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Dec 9 2023 03:57:40 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.