Sonnet

dictionarycombobox.cpp
1/*
2 * SPDX-FileCopyrightText: 2003 Ingo Kloecker <kloecker@kde.org>
3 * SPDX-FileCopyrightText: 2008 Tom Albers <tomalbers@kde.nl>
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
13namespace Sonnet
14{
15//@cond PRIVATE
16class DictionaryComboBoxPrivate
17{
18public:
19 explicit DictionaryComboBoxPrivate(DictionaryComboBox *combo)
20 : q(combo)
21 {
22 }
23
24 DictionaryComboBox *const q;
25 void slotDictionaryChanged(int idx);
26};
27
28void 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
44DictionaryComboBox::~DictionaryComboBox() = default;
45
46QString DictionaryComboBox::currentDictionaryName() const
47{
48 return currentText();
49}
50
51QString DictionaryComboBox::currentDictionary() const
52{
53 return itemData(currentIndex()).toString();
54}
55
56bool DictionaryComboBox::assignDictionnaryName(const QString &name)
57{
58 if (name.isEmpty() || name == currentText()) {
59 return false;
60 }
61
62 int idx = findText(name);
63 if (idx == -1) {
64 qCDebug(SONNET_LOG_UI) << "name not found" << name;
65 return false;
66 }
67
68 setCurrentIndex(idx);
69 d->slotDictionaryChanged(idx);
70 return true;
71}
72
73void DictionaryComboBox::setCurrentByDictionaryName(const QString &name)
74{
75 assignDictionnaryName(name);
76}
77
78bool DictionaryComboBox::assignByDictionnary(const QString &dictionary)
79{
80 if (dictionary.isEmpty()) {
81 return false;
82 }
83 if (dictionary == itemData(currentIndex()).toString()) {
84 return true;
85 }
86
87 int idx = findData(dictionary);
88 if (idx == -1) {
89 qCDebug(SONNET_LOG_UI) << "dictionary not found" << dictionary;
90 return false;
91 }
92
93 setCurrentIndex(idx);
94 d->slotDictionaryChanged(idx);
95 return true;
96}
97
98void DictionaryComboBox::setCurrentByDictionary(const QString &dictionary)
99{
100 assignByDictionnary(dictionary);
101}
102
103void DictionaryComboBox::reloadCombo()
104{
105 clear();
106 Sonnet::Speller speller;
107 QMap<QString, QString> preferredDictionaries = speller.preferredDictionaries();
108 QMapIterator<QString, QString> i(preferredDictionaries);
109 while (i.hasNext()) {
110 i.next();
111 addItem(i.key(), i.value());
112 }
113 if (count()) {
114 insertSeparator(count());
115 }
116
117 QMap<QString, QString> dictionaries = speller.availableDictionaries();
118 i = dictionaries;
119 while (i.hasNext()) {
120 i.next();
121 if (preferredDictionaries.contains(i.key())) {
122 continue;
123 }
124 addItem(i.key(), i.value());
125 }
126}
127
128} // namespace Sonnet
129
130#include "moc_dictionarycombobox.cpp"
DictionaryComboBox(QWidget *parent=nullptr)
Constructor.
Spell checker object.
QMap< QString, QString > preferredDictionaries() const
Definition speller.cpp:262
QMap< QString, QString > availableDictionaries() const
Definition speller.cpp:249
char * toString(const EngineQuery &query)
QString name(StandardAction id)
KGuiItem clear()
The sonnet namespace.
bool contains(const Key &key) const const
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:04:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.