File indexing completed on 2024-04-28 15:34:21

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()
0045 {
0046     delete d;
0047 }
0048 
0049 QString DictionaryComboBox::currentDictionaryName() const
0050 {
0051     return currentText();
0052 }
0053 
0054 QString DictionaryComboBox::currentDictionary() const
0055 {
0056     return itemData(currentIndex()).toString();
0057 }
0058 
0059 bool DictionaryComboBox::assignDictionnaryName(const QString &name)
0060 {
0061     if (name.isEmpty() || name == currentText()) {
0062         return false;
0063     }
0064 
0065     int idx = findText(name);
0066     if (idx == -1) {
0067         qCDebug(SONNET_LOG_UI) << "name not found" << name;
0068         return false;
0069     }
0070 
0071     setCurrentIndex(idx);
0072     d->slotDictionaryChanged(idx);
0073     return true;
0074 }
0075 
0076 void DictionaryComboBox::setCurrentByDictionaryName(const QString &name)
0077 {
0078     assignDictionnaryName(name);
0079 }
0080 
0081 bool DictionaryComboBox::assignByDictionnary(const QString &dictionary)
0082 {
0083     if (dictionary.isEmpty()) {
0084         return false;
0085     }
0086     if (dictionary == itemData(currentIndex()).toString()) {
0087         return true;
0088     }
0089 
0090     int idx = findData(dictionary);
0091     if (idx == -1) {
0092         qCDebug(SONNET_LOG_UI) << "dictionary not found" << dictionary;
0093         return false;
0094     }
0095 
0096     setCurrentIndex(idx);
0097     d->slotDictionaryChanged(idx);
0098     return true;
0099 }
0100 
0101 void DictionaryComboBox::setCurrentByDictionary(const QString &dictionary)
0102 {
0103     assignByDictionnary(dictionary);
0104 }
0105 
0106 void DictionaryComboBox::reloadCombo()
0107 {
0108     clear();
0109     Sonnet::Speller speller;
0110     QMap<QString, QString> preferredDictionaries = speller.preferredDictionaries();
0111     QMapIterator<QString, QString> i(preferredDictionaries);
0112     while (i.hasNext()) {
0113         i.next();
0114         addItem(i.key(), i.value());
0115     }
0116     if (count()) {
0117         insertSeparator(count());
0118     }
0119 
0120     QMap<QString, QString> dictionaries = speller.availableDictionaries();
0121     i = dictionaries;
0122     while (i.hasNext()) {
0123         i.next();
0124         if (preferredDictionaries.contains(i.key())) {
0125             continue;
0126         }
0127         addItem(i.key(), i.value());
0128     }
0129 }
0130 
0131 } // namespace Sonnet
0132 
0133 #include "moc_dictionarycombobox.cpp"