File indexing completed on 2024-04-14 03:58:23
0001 /* 0002 * SPDX-FileCopyrightText: 2003 Ingo Kloecker <kloecker@kde.org> 0003 * SPDX-FileCopyrightText: 2008 Tom Albers <tomalbers@kde.nl> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #include "dictionarycombobox.h" 0009 0010 #include "ui_debug.h" 0011 #include <speller.h> 0012 0013 namespace Sonnet 0014 { 0015 //@cond PRIVATE 0016 class DictionaryComboBoxPrivate 0017 { 0018 public: 0019 explicit DictionaryComboBoxPrivate(DictionaryComboBox *combo) 0020 : q(combo) 0021 { 0022 } 0023 0024 DictionaryComboBox *const q; 0025 void slotDictionaryChanged(int idx); 0026 }; 0027 0028 void DictionaryComboBoxPrivate::slotDictionaryChanged(int idx) 0029 { 0030 Q_EMIT q->dictionaryChanged(q->itemData(idx).toString()); 0031 Q_EMIT q->dictionaryNameChanged(q->itemText(idx)); 0032 } 0033 0034 //@endcon 0035 0036 DictionaryComboBox::DictionaryComboBox(QWidget *parent) 0037 : QComboBox(parent) 0038 , d(new DictionaryComboBoxPrivate(this)) 0039 { 0040 reloadCombo(); 0041 connect(this, SIGNAL(activated(int)), SLOT(slotDictionaryChanged(int))); 0042 } 0043 0044 DictionaryComboBox::~DictionaryComboBox() = default; 0045 0046 QString DictionaryComboBox::currentDictionaryName() const 0047 { 0048 return currentText(); 0049 } 0050 0051 QString DictionaryComboBox::currentDictionary() const 0052 { 0053 return itemData(currentIndex()).toString(); 0054 } 0055 0056 bool DictionaryComboBox::assignDictionnaryName(const QString &name) 0057 { 0058 if (name.isEmpty() || name == currentText()) { 0059 return false; 0060 } 0061 0062 int idx = findText(name); 0063 if (idx == -1) { 0064 qCDebug(SONNET_LOG_UI) << "name not found" << name; 0065 return false; 0066 } 0067 0068 setCurrentIndex(idx); 0069 d->slotDictionaryChanged(idx); 0070 return true; 0071 } 0072 0073 void DictionaryComboBox::setCurrentByDictionaryName(const QString &name) 0074 { 0075 assignDictionnaryName(name); 0076 } 0077 0078 bool DictionaryComboBox::assignByDictionnary(const QString &dictionary) 0079 { 0080 if (dictionary.isEmpty()) { 0081 return false; 0082 } 0083 if (dictionary == itemData(currentIndex()).toString()) { 0084 return true; 0085 } 0086 0087 int idx = findData(dictionary); 0088 if (idx == -1) { 0089 qCDebug(SONNET_LOG_UI) << "dictionary not found" << dictionary; 0090 return false; 0091 } 0092 0093 setCurrentIndex(idx); 0094 d->slotDictionaryChanged(idx); 0095 return true; 0096 } 0097 0098 void DictionaryComboBox::setCurrentByDictionary(const QString &dictionary) 0099 { 0100 assignByDictionnary(dictionary); 0101 } 0102 0103 void DictionaryComboBox::reloadCombo() 0104 { 0105 clear(); 0106 Sonnet::Speller speller; 0107 QMap<QString, QString> preferredDictionaries = speller.preferredDictionaries(); 0108 QMapIterator<QString, QString> i(preferredDictionaries); 0109 while (i.hasNext()) { 0110 i.next(); 0111 addItem(i.key(), i.value()); 0112 } 0113 if (count()) { 0114 insertSeparator(count()); 0115 } 0116 0117 QMap<QString, QString> dictionaries = speller.availableDictionaries(); 0118 i = dictionaries; 0119 while (i.hasNext()) { 0120 i.next(); 0121 if (preferredDictionaries.contains(i.key())) { 0122 continue; 0123 } 0124 addItem(i.key(), i.value()); 0125 } 0126 } 0127 0128 } // namespace Sonnet 0129 0130 #include "moc_dictionarycombobox.cpp"