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(dictionary: q->itemData(index: idx).toString());
31 Q_EMIT q->dictionaryNameChanged(dictionaryName: q->itemText(index: idx));
32}
33
34//@endcon
35
36DictionaryComboBox::DictionaryComboBox(QWidget *parent)
37 : QComboBox(parent)
38 , d(new DictionaryComboBoxPrivate(this))
39{
40 reloadCombo();
41 connect(asender: 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(index: 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(text: 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(index: currentIndex()).toString()) {
84 return true;
85 }
86
87 int idx = findData(data: 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(atext: i.key(), auserData: i.value());
112 }
113 if (count()) {
114 insertSeparator(index: count());
115 }
116
117 QMap<QString, QString> dictionaries = speller.availableDictionaries();
118 i = dictionaries;
119 while (i.hasNext()) {
120 i.next();
121 if (preferredDictionaries.contains(key: i.key())) {
122 continue;
123 }
124 addItem(atext: i.key(), auserData: i.value());
125 }
126}
127
128} // namespace Sonnet
129
130#include "moc_dictionarycombobox.cpp"
131

source code of sonnet/src/ui/dictionarycombobox.cpp