File indexing completed on 2024-12-22 04:28:24
0001 /* 0002 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "speechtotextcomboboxwidget.h" 0008 #include "speechtotext/textspeechtotextutil.h" 0009 #include <KConfigGroup> 0010 #include <KLocalizedString> 0011 #include <KSharedConfig> 0012 #include <QComboBox> 0013 #include <QHBoxLayout> 0014 #include <QLabel> 0015 #include <QToolButton> 0016 #include <TextSpeechToText/SpeechToTextEngineLoader> 0017 0018 using namespace TextSpeechToText; 0019 SpeechToTextComboBoxWidget::SpeechToTextComboBoxWidget(QWidget *parent) 0020 : QWidget(parent) 0021 , mEngine(new QComboBox(this)) 0022 , mConfigureEngine(new QToolButton(this)) 0023 { 0024 auto mainLayout = new QHBoxLayout(this); 0025 mainLayout->setObjectName(QStringLiteral("mainLayout")); 0026 mainLayout->setContentsMargins({}); 0027 0028 auto label = new QLabel(i18n("Engine:"), this); 0029 label->setObjectName(QStringLiteral("label")); 0030 mainLayout->addWidget(label); 0031 0032 mEngine->setObjectName(QStringLiteral("mEngine")); 0033 mainLayout->addWidget(mEngine); 0034 0035 mConfigureEngine->setObjectName(QStringLiteral("mConfigureEngine")); 0036 mainLayout->addWidget(mConfigureEngine); 0037 mConfigureEngine->setEnabled(false); // Disable by default 0038 mConfigureEngine->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure"))); 0039 0040 connect(mConfigureEngine, &QToolButton::clicked, this, &SpeechToTextComboBoxWidget::slotConfigureEngine); 0041 connect(mEngine, &QComboBox::currentIndexChanged, this, &SpeechToTextComboBoxWidget::slotEngineChanged); 0042 } 0043 0044 SpeechToTextComboBoxWidget::~SpeechToTextComboBoxWidget() = default; 0045 0046 void SpeechToTextComboBoxWidget::fillEngine() 0047 { 0048 const QMap<QString, QString> map = TextSpeechToText::SpeechToTextEngineLoader::self()->speechToTextEngineInfos(); 0049 QMapIterator<QString, QString> iMap(map); 0050 while (iMap.hasNext()) { 0051 iMap.next(); 0052 mEngine->addItem(iMap.value(), iMap.key()); 0053 } 0054 } 0055 0056 QString SpeechToTextComboBoxWidget::engineName() const 0057 { 0058 return mEngine->currentData().toString(); 0059 } 0060 0061 void SpeechToTextComboBoxWidget::setEngineName(const QString &engineName) 0062 { 0063 const int countItem(mEngine->count()); 0064 for (int i = 0; i < countItem; ++i) { 0065 if (mEngine->itemData(i).toString() == engineName) { 0066 mEngine->setCurrentIndex(i); 0067 break; 0068 } 0069 } 0070 } 0071 0072 void SpeechToTextComboBoxWidget::slotConfigureEngine() 0073 { 0074 const QString engine = mEngine->currentData().toString(); 0075 if (TextSpeechToText::SpeechToTextEngineLoader::self()->hasConfigurationDialog(engine)) { 0076 if (TextSpeechToText::SpeechToTextEngineLoader::self()->showConfigureDialog(engine, this)) { 0077 Q_EMIT configureChanged(engine); 0078 } 0079 } 0080 } 0081 0082 void SpeechToTextComboBoxWidget::slotEngineChanged(int index) 0083 { 0084 const QString engine = mEngine->itemData(index).toString(); 0085 mConfigureEngine->setEnabled(TextSpeechToText::SpeechToTextEngineLoader::self()->hasConfigurationDialog(engine)); 0086 Q_EMIT engineChanged(engine); 0087 } 0088 0089 void SpeechToTextComboBoxWidget::load() 0090 { 0091 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TextSpeechToTextUtil::groupTranslateName()); 0092 const QString engine = groupTranslate.readEntry(TextSpeechToTextUtil::engineTextToSpeechName(), TextSpeechToTextUtil::defaultEngineName()); 0093 const int index = mEngine->findData(engine); 0094 if (index != -1) { 0095 mEngine->setCurrentIndex(index); 0096 } 0097 } 0098 0099 void SpeechToTextComboBoxWidget::save() 0100 { 0101 const QString engine = mEngine->currentData().toString(); 0102 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TextSpeechToTextUtil::groupTranslateName()); 0103 groupTranslate.writeEntry(TextSpeechToTextUtil::engineTextToSpeechName(), engine); 0104 } 0105 0106 #include "moc_speechtotextcomboboxwidget.cpp"