KTextAddons

bergamotengineplugin.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "bergamotengineplugin.h"
8#include "bergamotmarianinterface.h"
9#include <KConfigGroup>
10#include <KLocalizedString>
11#include <KSharedConfig>
12
13BergamotEnginePlugin::BergamotEnginePlugin(QObject *parent)
14 : TextTranslator::TranslatorEnginePlugin(parent)
15 , mBergamotInterface(new BergamotMarianInterface(this))
16{
17 loadSettings();
18
19 connect(mBergamotInterface, &BergamotMarianInterface::errorText, this, [this](const QString &message) {
20 Q_EMIT translateFailed(message);
21 });
22 connect(mBergamotInterface, &BergamotMarianInterface::translationReady, this, [&](Translation translation) {
23 appendResult(translation.translation());
24 Q_EMIT translateDone();
25 });
26 connect(this, &BergamotEnginePlugin::languagesChanged, this, &BergamotEnginePlugin::slotLanguagesChanged);
27}
28
29BergamotEnginePlugin::~BergamotEnginePlugin() = default;
30
31void BergamotEnginePlugin::translate()
32{
33 clear();
34 mBergamotInterface->translate(inputText());
35}
36
37void BergamotEnginePlugin::loadSettings()
38{
39 mInstalledLanguages = BergamotEngineUtils::languageLocallyStored();
40 mSettingInfo.loadSettingsInfo();
41 updateBergamotModel();
42}
43
44void BergamotEnginePlugin::updateBergamotModel()
45{
46 // qDebug() << "mInstalledLanguages " << mInstalledLanguages << " from " << from() << " to() " << to();
47 if (from().isEmpty() || to().isEmpty()) {
48 return;
49 }
50 QString absolutePath;
51 for (const auto &installed : std::as_const(mInstalledLanguages)) {
52 if (installed.from == from() && installed.to == to()) {
53 absolutePath = installed.absoluteLanguageModelPath;
54 break;
55 }
56 }
57 if (absolutePath.isEmpty()) {
58 return;
59 }
60 qDebug() << " absolutePath " << absolutePath;
61 if (QDir().exists(absolutePath)) {
62 mBergamotInterface->setModel(absolutePath, mSettingInfo);
63 }
64}
65
66void BergamotEnginePlugin::slotConfigureChanged()
67{
68 loadSettings();
69}
70
71void BergamotEnginePlugin::slotLanguagesChanged()
72{
73 updateBergamotModel();
74}
75
76#include "moc_bergamotengineplugin.cpp"
Wrapper around a translation response from the bergamot service.
Definition translation.h:27
QString translation() const
Translation result.
bool isEmpty() const const
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 Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.