KTextAddons

translatorconfigurecombowidget.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "translatorconfigurecombowidget.h"
8#include "translator/misc/translatorutil.h"
9#include "translator/translatorengineloader.h"
10#include <KConfigGroup>
11#include <KSharedConfig>
12#include <QComboBox>
13#include <QHBoxLayout>
14#include <QToolButton>
15
16using namespace TextTranslator;
17TranslatorConfigureComboWidget::TranslatorConfigureComboWidget(QWidget *parent)
18 : QWidget{parent}
19 , mEngineComboBox(new QComboBox(this))
20 , mConfigureEngine(new QToolButton(this))
21{
22 auto mainLayout = new QHBoxLayout(this);
23 mainLayout->setObjectName(QStringLiteral("mainLayout"));
24 mainLayout->setContentsMargins(QMargins{});
25
26 mEngineComboBox->setObjectName(QStringLiteral("mEngineComboBox"));
27 mainLayout->addWidget(mEngineComboBox);
28
29 mConfigureEngine->setObjectName(QStringLiteral("mConfigureEngine"));
30 mainLayout->addWidget(mConfigureEngine);
31 mConfigureEngine->setEnabled(false); // Disable by default
32 mConfigureEngine->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
33 connect(mConfigureEngine, &QToolButton::clicked, this, &TranslatorConfigureComboWidget::slotConfigureEngine);
34 connect(mEngineComboBox, &QComboBox::currentIndexChanged, this, &TranslatorConfigureComboWidget::slotEngineChanged);
35
36 fillEngine();
37}
38
39TranslatorConfigureComboWidget::~TranslatorConfigureComboWidget() = default;
40
41void TranslatorConfigureComboWidget::slotConfigureEngine()
42{
43 const QString engine = mEngineComboBox->currentData().toString();
44 if (TextTranslator::TranslatorEngineLoader::self()->hasConfigurationDialog(engine)) {
45 if (TextTranslator::TranslatorEngineLoader::self()->showConfigureDialog(engine, this)) {
46 Q_EMIT configureChanged(engine);
47 }
48 }
49}
50
51void TranslatorConfigureComboWidget::fillEngine()
52{
53 const QMap<QString, QString> map = TextTranslator::TranslatorEngineLoader::self()->translatorEngineInfos();
55 while (iMap.hasNext()) {
56 iMap.next();
57 mEngineComboBox->addItem(iMap.value(), iMap.key());
58 }
59}
60
61void TranslatorConfigureComboWidget::slotEngineChanged(int index)
62{
63 const QString engine = mEngineComboBox->itemData(index).toString();
64 mConfigureEngine->setEnabled(TextTranslator::TranslatorEngineLoader::self()->hasConfigurationDialog(engine));
65 Q_EMIT engineChanged(engine);
66}
67
68void TranslatorConfigureComboWidget::load()
69{
70 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TranslatorUtil::groupTranslateName());
71 const QString engine = groupTranslate.readEntry(TranslatorUtil::engineTranslateName(), TranslatorUtil::defaultEngineName()); // Google by default
72 const int index = mEngineComboBox->findData(engine);
73 if (index != -1) {
74 mEngineComboBox->setCurrentIndex(index);
75 }
76}
77
78void TranslatorConfigureComboWidget::save()
79{
80 const QString engine = mEngineComboBox->currentData().toString();
81 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TranslatorUtil::groupTranslateName());
82 groupTranslate.writeEntry(TranslatorUtil::engineTranslateName(), engine);
83}
84
85#include "moc_translatorconfigurecombowidget.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void clicked(bool checked)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void setCurrentIndex(int index)
void currentIndexChanged(int index)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
QVariant itemData(int index, int role) const const
QIcon fromTheme(const QString &name)
Q_EMITQ_EMIT
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.