Sonnet

settings.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020 Benjamin Port <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 #include "settingsimpl_p.h"
7 
8 #include <QLocale>
9 
10 #include "loader_p.h"
11 #include "settings.h"
12 #include <QDebug>
13 #include <speller.h>
14 
15 namespace Sonnet
16 {
17 class DictionaryModel : public QAbstractListModel
18 {
19  Q_OBJECT
20 public:
21  explicit DictionaryModel(QObject *parent = nullptr)
23  {
24  reload();
25  }
26 
27  ~DictionaryModel() override
28  {
29  }
30 
31  void reload()
32  {
34  Sonnet::Speller speller;
35  m_preferredDictionaries = speller.preferredDictionaries();
36  m_availableDictionaries = speller.availableDictionaries();
37  endResetModel();
38  }
39 
40  void setDefaultLanguage(const QString &language)
41  {
42  m_defaultDictionary = language;
43  Q_EMIT dataChanged(index(0, 0), index(rowCount(QModelIndex()) - 1, 0), {Settings::DefaultRole});
44  }
45 
46  bool setData(const QModelIndex &idx, const QVariant &value, int role = Qt::EditRole) override
47  {
48  Q_UNUSED(value)
49 
50  if (!checkIndex(idx) || role != Qt::CheckStateRole) {
51  return false;
52  }
53  const int row = idx.row();
54  const auto language = m_availableDictionaries.keys().at(row);
55  const auto inPreferredDictionaries = m_preferredDictionaries.contains(m_availableDictionaries.keys().at(row));
56 
57  if (inPreferredDictionaries) {
58  m_preferredDictionaries.remove(language);
59  } else {
60  m_preferredDictionaries[language] = m_availableDictionaries.values().at(row);
61  }
62  qobject_cast<Settings *>(parent())->setPreferredLanguages(m_preferredDictionaries.values());
63  Q_EMIT dataChanged(index(row, 0), index(row, 0), {Qt::CheckStateRole});
64  return true;
65  }
66 
67  QVariant data(const QModelIndex &index, int role) const override
68  {
69  if (!checkIndex(index)) {
70  return {};
71  }
72  const int row = index.row();
73 
74  switch (role) {
75  case Qt::DisplayRole:
76  return m_availableDictionaries.keys().at(row);
77  case Settings::LanguageCodeRole:
78  return m_availableDictionaries.values().at(row);
79  case Qt::CheckStateRole:
80  return m_preferredDictionaries.contains(m_availableDictionaries.keys().at(row));
81  case Settings::DefaultRole:
82  return data(index, Settings::LanguageCodeRole) == m_defaultDictionary;
83  }
84  return {};
85  }
86 
87  int rowCount(const QModelIndex &parent) const override
88  {
89  Q_UNUSED(parent)
90  return m_availableDictionaries.count();
91  }
92 
93  QHash<int, QByteArray> roleNames() const override
94  {
95  return {
96  {Qt::DisplayRole, QByteArrayLiteral("display")},
97  {Qt::CheckStateRole, QByteArrayLiteral("checked")},
98  {Settings::PreferredRole, QByteArrayLiteral("isPreferred")},
99  {Settings::LanguageCodeRole, QByteArrayLiteral("languageCode")},
100  {Settings::DefaultRole, QByteArrayLiteral("isDefault")},
101  };
102  }
103 
104 private:
105  QMap<QString, QString> m_preferredDictionaries;
106  QMap<QString, QString> m_availableDictionaries;
107  QString m_defaultDictionary;
108 };
109 
110 class SettingsPrivate
111 {
112 public:
113  Loader *loader = nullptr;
114  DictionaryModel *dictionaryModel = nullptr;
115 };
116 
117 Settings::Settings(QObject *parent)
118  : QObject(parent)
119  , d(new SettingsPrivate)
120 {
121  d->loader = Loader::openLoader();
122 }
123 
124 Settings::~Settings()
125 {
126  delete d;
127 }
128 
129 void Settings::setDefaultLanguage(const QString &lang)
130 {
131  if (defaultLanguage() == lang) {
132  return;
133  }
134  d->loader->settings()->setDefaultLanguage(lang);
135  Q_EMIT defaultLanguageChanged();
136  Q_EMIT modifiedChanged();
137 
138  if (d->dictionaryModel) {
139  d->dictionaryModel->setDefaultLanguage(lang);
140  }
141 }
142 
143 QString Settings::defaultLanguage() const
144 {
145  return d->loader->settings()->defaultLanguage();
146 }
147 
148 void Settings::setPreferredLanguages(const QStringList &lang)
149 {
150  if (!d->loader->settings()->setPreferredLanguages(lang)) {
151  return;
152  }
153  Q_EMIT modifiedChanged();
154  Q_EMIT preferredLanguagesChanged();
155 }
156 
157 QStringList Settings::preferredLanguages() const
158 {
159  return d->loader->settings()->preferredLanguages();
160 }
161 
162 void Settings::setDefaultClient(const QString &client)
163 {
164  if (!d->loader->settings()->setDefaultClient(client)) {
165  return;
166  }
167  Q_EMIT defaultClientChanged();
168  Q_EMIT modifiedChanged();
169 }
170 
171 QString Settings::defaultClient() const
172 {
173  return d->loader->settings()->defaultClient();
174 }
175 
176 void Settings::setSkipUppercase(bool skip)
177 {
178  if (!d->loader->settings()->setCheckUppercase(!skip)) {
179  return;
180  }
181  Q_EMIT skipUppercaseChanged();
182  Q_EMIT modifiedChanged();
183 }
184 
185 bool Settings::skipUppercase() const
186 {
187  return !d->loader->settings()->checkUppercase();
188 }
189 
190 void Settings::setAutodetectLanguage(bool detect)
191 {
192  if (!d->loader->settings()->setAutodetectLanguage(detect)) {
193  return;
194  }
195  Q_EMIT autodetectLanguageChanged();
196  Q_EMIT modifiedChanged();
197 }
198 
199 bool Settings::autodetectLanguage() const
200 {
201  return d->loader->settings()->autodetectLanguage();
202 }
203 
204 void Settings::setSkipRunTogether(bool skip)
205 {
206  if (skipRunTogether() == skip) {
207  return;
208  }
209  d->loader->settings()->setSkipRunTogether(skip);
210  Q_EMIT skipRunTogetherChanged();
211  Q_EMIT modifiedChanged();
212 }
213 
214 bool Settings::skipRunTogether() const
215 {
216  return d->loader->settings()->skipRunTogether();
217 }
218 
219 void Settings::setCheckerEnabledByDefault(bool check)
220 {
221  if (checkerEnabledByDefault() == check) {
222  return;
223  }
224  d->loader->settings()->setCheckerEnabledByDefault(check);
225  Q_EMIT checkerEnabledByDefaultChanged();
226  Q_EMIT modifiedChanged();
227 }
228 
229 bool Settings::checkerEnabledByDefault() const
230 {
231  return d->loader->settings()->checkerEnabledByDefault();
232 }
233 
234 void Settings::setBackgroundCheckerEnabled(bool enable)
235 {
236  if (backgroundCheckerEnabled() == enable) {
237  return;
238  }
239  d->loader->settings()->setBackgroundCheckerEnabled(enable);
240  Q_EMIT backgroundCheckerEnabledChanged();
241  Q_EMIT modifiedChanged();
242 }
243 
244 bool Settings::backgroundCheckerEnabled() const
245 {
246  return d->loader->settings()->backgroundCheckerEnabled();
247 }
248 
249 void Settings::setCurrentIgnoreList(const QStringList &ignores)
250 {
251  if (currentIgnoreList() == ignores) {
252  return;
253  }
254  d->loader->settings()->setCurrentIgnoreList(ignores);
255  Q_EMIT currentIgnoreListChanged();
256  Q_EMIT modifiedChanged();
257 }
258 
259 QStringList Settings::currentIgnoreList() const
260 {
261  return d->loader->settings()->currentIgnoreList();
262 }
263 
264 QStringList Settings::clients() const
265 {
266  return d->loader->clients();
267 }
268 
269 void Settings::save()
270 {
271  d->loader->settings()->save();
272  Q_EMIT modifiedChanged();
273 }
274 
275 bool Settings::modified() const
276 {
277  return d->loader->settings()->modified();
278 }
279 
280 // default values
281 // A static list of KDE specific words that we want to recognize
282 QStringList Settings::defaultIgnoreList()
283 {
284  QStringList l;
285  l.append(QStringLiteral("KMail"));
286  l.append(QStringLiteral("KOrganizer"));
287  l.append(QStringLiteral("KAddressBook"));
288  l.append(QStringLiteral("KHTML"));
289  l.append(QStringLiteral("KIO"));
290  l.append(QStringLiteral("KJS"));
291  l.append(QStringLiteral("Konqueror"));
292  l.append(QStringLiteral("Sonnet"));
293  l.append(QStringLiteral("Kontact"));
294  l.append(QStringLiteral("Qt"));
295  l.append(QStringLiteral("Okular"));
296  l.append(QStringLiteral("KMix"));
297  l.append(QStringLiteral("Amarok"));
298  l.append(QStringLiteral("KDevelop"));
299  l.append(QStringLiteral("Nepomuk"));
300  return l;
301 }
302 
303 bool Settings::defaultSkipUppercase()
304 {
305  return false;
306 }
307 
308 bool Settings::defaultAutodetectLanguage()
309 {
310  return true;
311 }
312 
313 bool Settings::defaultBackgroundCheckerEnabled()
314 {
315  return true;
316 }
317 
318 bool Settings::defaultCheckerEnabledByDefault()
319 {
320  return false;
321 }
322 
323 bool Settings::defauktSkipRunTogether()
324 {
325  return true;
326 }
327 
328 QString Settings::defaultDefaultLanguage()
329 {
330  return QLocale::system().name();
331 }
332 
333 QStringList Settings::defaultPreferredLanguages()
334 {
335  return QStringList();
336 }
337 
338 QAbstractListModel *Settings::dictionaryModel()
339 {
340  // Lazy loading
341  if (d->dictionaryModel) {
342  return d->dictionaryModel;
343  }
344 
345  d->dictionaryModel = new DictionaryModel(this);
346  d->dictionaryModel->setDefaultLanguage(defaultLanguage());
347  return d->dictionaryModel;
348 }
349 }
350 
351 #include "settings.moc"
Q_OBJECTQ_OBJECT
void append(const T &value)
EditRole
Q_EMITQ_EMIT
const QList< QKeySequence > & reload()
class used for actual spell checking
Definition: speller.h:25
virtual QModelIndex parent(const QModelIndex &index) const const=0
QLocale system()
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
bool checkIndex(const QModelIndex &index, QAbstractItemModel::CheckIndexOptions options) const const
QString name() const const
int row() const const
The sonnet namespace.
const QChar at(int position) const const
QMap< QString, QString > availableDictionaries() const
Definition: speller.cpp:250
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
QMap< QString, QString > preferredDictionaries() const
Definition: speller.cpp:263
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.