Sonnet

dictionarycombobox.cpp
1 /*
2  * SPDX-FileCopyrightText: 2003 Ingo Kloecker <[email protected]>
3  * SPDX-FileCopyrightText: 2008 Tom Albers <[email protected]>
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  */
7 
8 #include "dictionarycombobox.h"
9 
10 #include "ui_debug.h"
11 #include <speller.h>
12 
13 namespace Sonnet
14 {
15 //@cond PRIVATE
16 class DictionaryComboBoxPrivate
17 {
18 public:
19  explicit DictionaryComboBoxPrivate(DictionaryComboBox *combo)
20  : q(combo)
21  {
22  }
23 
24  DictionaryComboBox *const q;
25  void slotDictionaryChanged(int idx);
26 };
27 
28 void DictionaryComboBoxPrivate::slotDictionaryChanged(int idx)
29 {
30  Q_EMIT q->dictionaryChanged(q->itemData(idx).toString());
31  Q_EMIT q->dictionaryNameChanged(q->itemText(idx));
32 }
33 
34 //@endcon
35 
37  : QComboBox(parent)
38  , d(new DictionaryComboBoxPrivate(this))
39 {
40  reloadCombo();
41  connect(this, SIGNAL(activated(int)), SLOT(slotDictionaryChanged(int)));
42 }
43 
45 {
46  delete d;
47 }
48 
50 {
51  return currentText();
52 }
53 
55 {
56  return itemData(currentIndex()).toString();
57 }
58 
60 {
61  if (name.isEmpty() || name == currentText()) {
62  return false;
63  }
64 
65  int idx = findText(name);
66  if (idx == -1) {
67  qCDebug(SONNET_LOG_UI) << "name not found" << name;
68  return false;
69  }
70 
71  setCurrentIndex(idx);
72  d->slotDictionaryChanged(idx);
73  return true;
74 }
75 
77 {
79 }
80 
82 {
83  if (dictionary.isEmpty()) {
84  return false;
85  }
86  if (dictionary == itemData(currentIndex()).toString()) {
87  return true;
88  }
89 
90  int idx = findData(dictionary);
91  if (idx == -1) {
92  qCDebug(SONNET_LOG_UI) << "dictionary not found" << dictionary;
93  return false;
94  }
95 
96  setCurrentIndex(idx);
97  d->slotDictionaryChanged(idx);
98  return true;
99 }
100 
102 {
103  assignByDictionnary(dictionary);
104 }
105 
107 {
108  clear();
109  Sonnet::Speller speller;
110  QMap<QString, QString> preferredDictionaries = speller.preferredDictionaries();
111  QMapIterator<QString, QString> i(preferredDictionaries);
112  while (i.hasNext()) {
113  i.next();
114  addItem(i.key(), i.value());
115  }
116  if (count()) {
118  }
119 
120  QMap<QString, QString> dictionaries = speller.availableDictionaries();
121  i = dictionaries;
122  while (i.hasNext()) {
123  i.next();
124  if (preferredDictionaries.contains(i.key())) {
125  continue;
126  }
127  addItem(i.key(), i.value());
128  }
129 }
130 
131 } // namespace Sonnet
132 
133 #include "moc_dictionarycombobox.cpp"
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
bool contains(const Key &key) const const
bool assignDictionnaryName(const QString &name)
Sets the current dictionaryName to the given dictionaryName Return true if dictionary was found.
~DictionaryComboBox() override
Destructor.
QString currentDictionaryName() const
Returns the current dictionary name, for example "German (Switzerland)".
void reloadCombo()
Clears the widget and reloads the dictionaries from Sonnet.
void insertSeparator(int index)
class used for actual spell checking
Definition: speller.h:25
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setCurrentByDictionary(const QString &dictionary)
Sets the current dictionary to the given dictionary.
bool assignByDictionnary(const QString &dictionary)
Sets the current dictionary to the given dictionary Return true if dictionary was found.
void setCurrentByDictionaryName(const QString &dictionaryName)
Sets the current dictionaryName to the given dictionaryName.
QString currentDictionary() const
Returns the current dictionary, for example "de_CH".
bool isEmpty() const const
DictionaryComboBox(QWidget *parent=nullptr)
Constructor.
The sonnet namespace.
int findText(const QString &text, Qt::MatchFlags flags) const const
QVariant itemData(int index, int role) const const
void clear()
const char * name(StandardAction id)
QMap< QString, QString > availableDictionaries() const
Definition: speller.cpp:250
void addItem(const QString &text, const QVariant &userData)
QMap< QString, QString > preferredDictionaries() const
Definition: speller.cpp:263
char * toString(const EngineQuery &query)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Dec 11 2023 03:56:41 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.