KTextAddons

translatorconfigurelistswidget.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 "translatorconfigurelistswidget.h"
8#include "translator/misc/translatorutil.h"
9#include "translator/translatorengineloader.h"
10#include "translatorconfigurecombowidget.h"
11#include "translatorconfigurelanguagelistwidget.h"
12#include "translatorconfigurelistswidget.h"
13#include <KConfigGroup>
14#include <KLocalizedString>
15#include <KSharedConfig>
16#include <QLabel>
17#include <QVBoxLayout>
18using namespace TextTranslator;
19class Q_DECL_HIDDEN TextTranslator::TranslatorConfigureListsWidget::TranslatorConfigureListsWidgetPrivate
20{
21public:
22 TranslatorConfigureListsWidgetPrivate(TranslatorConfigureListsWidget *parent)
23 : mEngineConfigureComboWidget(new TranslatorConfigureComboWidget(parent))
24 , mFromLanguageWidget(new TranslatorConfigureLanguageListWidget(i18n("From:"), parent))
25 , mToLanguageWidget(new TranslatorConfigureLanguageListWidget(i18n("To:"), parent))
26 {
27 }
28 bool mLanguageListLoaded = false;
29 TranslatorConfigureComboWidget *const mEngineConfigureComboWidget;
32};
33
34TranslatorConfigureListsWidget::TranslatorConfigureListsWidget(QWidget *parent)
35 : QWidget{parent}
36 , d(new TranslatorConfigureListsWidgetPrivate(this))
37{
38 auto mainLayout = new QVBoxLayout(this);
39 mainLayout->setObjectName(QStringLiteral("mainLayout"));
40 d->mEngineConfigureComboWidget->setObjectName(QStringLiteral("mEngineConfigureComboWidget"));
41 connect(d->mEngineConfigureComboWidget,
42 &TranslatorConfigureComboWidget::configureChanged,
43 this,
44 &TranslatorConfigureListsWidget::slotEngineSettingsChanged);
45
46 auto hboxLayout = new QHBoxLayout;
47 hboxLayout->setObjectName(QStringLiteral("hboxLayout"));
48 hboxLayout->setContentsMargins({});
49 mainLayout->addLayout(hboxLayout);
50
51 auto label = new QLabel(i18nc("@label:textbox", "Engine:"), this);
52 label->setObjectName(QStringLiteral("label"));
53 hboxLayout->addWidget(label);
54 hboxLayout->addWidget(d->mEngineConfigureComboWidget);
55 hboxLayout->addStretch(0);
56
57 auto hLanguageListboxLayout = new QHBoxLayout;
58 hLanguageListboxLayout->setObjectName(QStringLiteral("hLanguageListboxLayout"));
59 hLanguageListboxLayout->setContentsMargins({});
60 mainLayout->addLayout(hLanguageListboxLayout);
61
62 d->mFromLanguageWidget->setObjectName(QStringLiteral("mFromLanguageWidget"));
63 d->mToLanguageWidget->setObjectName(QStringLiteral("mToLanguageWidget"));
64 hLanguageListboxLayout->addWidget(d->mFromLanguageWidget);
65 hLanguageListboxLayout->addWidget(d->mToLanguageWidget);
66
67 connect(d->mEngineConfigureComboWidget, &TranslatorConfigureComboWidget::engineChanged, this, &TranslatorConfigureListsWidget::slotEngineChanged);
68}
69
70TranslatorConfigureListsWidget::~TranslatorConfigureListsWidget() = default;
71
72void TranslatorConfigureListsWidget::save()
73{
74 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TranslatorUtil::groupTranslateName());
75 groupTranslate.writeEntry(QStringLiteral("From"), d->mFromLanguageWidget->selectedLanguages());
76 groupTranslate.writeEntry(QStringLiteral("To"), d->mToLanguageWidget->selectedLanguages());
77 d->mEngineConfigureComboWidget->save();
78}
79
80void TranslatorConfigureListsWidget::load()
81{
82 d->mEngineConfigureComboWidget->load();
83}
84
85void TranslatorConfigureListsWidget::slotEngineSettingsChanged(const QString &engineName)
86{
87 slotEngineChanged(engineName);
88}
89
90void TranslatorConfigureListsWidget::loadLanguagesList()
91{
92 KConfigGroup groupTranslate(KSharedConfig::openConfig(), TranslatorUtil::groupTranslateName());
93 const auto fromLanguages = groupTranslate.readEntry(QStringLiteral("From"), QStringList());
94 const auto toLanguages = groupTranslate.readEntry(QStringLiteral("To"), QStringList());
95 d->mFromLanguageWidget->setSelectedLanguages(fromLanguages);
96 d->mToLanguageWidget->setSelectedLanguages(toLanguages);
97}
98
99void TranslatorConfigureListsWidget::fillFromToLanguages(const QMap<TextTranslator::TranslatorUtil::Language, QString> &listFromLanguage,
101{
103 TranslatorUtil translatorUtil;
104 while (i.hasNext()) {
105 i.next();
106 const QString languageCode = TranslatorUtil::languageCode(i.key());
107 d->mFromLanguageWidget->addItem(i.value(), languageCode);
108 }
109
111 while (toLangIt.hasNext()) {
112 toLangIt.next();
113 const QString languageCode = TranslatorUtil::languageCode(toLangIt.key());
114 d->mToLanguageWidget->addItem(toLangIt.value(), languageCode);
115 }
116}
117
118void TranslatorConfigureListsWidget::fillLanguages(const QMap<TextTranslator::TranslatorUtil::Language, QString> &listLanguage)
119{
121 TranslatorUtil translatorUtil;
122 while (i.hasNext()) {
123 i.next();
124 const QString languageCode = TranslatorUtil::languageCode(i.key());
125 d->mFromLanguageWidget->addItem(i.value(), languageCode);
126 if ((i.key() != TranslatorUtil::automatic)) {
127 d->mToLanguageWidget->addItem(i.value(), languageCode);
128 }
129 }
130}
131
132void TranslatorConfigureListsWidget::slotEngineChanged(const QString &engine)
133{
135 TextTranslator::TranslatorEngineLoader::self()->supportedFromLanguages(engine);
136 const QMap<TextTranslator::TranslatorUtil::Language, QString> listToLanguage = TextTranslator::TranslatorEngineLoader::self()->supportedToLanguages(engine);
137
138 if (!d->mLanguageListLoaded) {
139 fillLanguages(listFromLanguage);
140 loadLanguagesList();
141 d->mLanguageListLoaded = true;
142 } else {
143 const QStringList fromLanguages = d->mFromLanguageWidget->selectedLanguages();
144 const QStringList toLanguages = d->mToLanguageWidget->selectedLanguages();
145
146 d->mFromLanguageWidget->clear();
147 d->mToLanguageWidget->clear();
148 if (listFromLanguage != listToLanguage) {
149 fillFromToLanguages(listFromLanguage, listToLanguage);
150 } else {
151 fillLanguages(listFromLanguage);
152 }
153
154 // Restore if possible
155 d->mFromLanguageWidget->setSelectedLanguages(fromLanguages);
156 d->mToLanguageWidget->setSelectedLanguages(toLanguages);
157 }
158}
159
160#include "moc_translatorconfigurelistswidget.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString label(StandardShortcut id)
void setObjectName(QAnyStringView name)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.