KTextAddons

translatormenu.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 "translatormenu.h"
8#include "texttranslator_debug.h"
9#include "translator/misc/translatorutil.h"
10#include "translator/translatorengineloader.h"
11
12#include <KConfigGroup>
13#include <KLocalizedString>
14#include <KSharedConfig>
15#include <QMenu>
16
17using namespace TextTranslator;
18TranslatorMenu::TranslatorMenu(QObject *parent)
19 : QObject(parent)
20 , mMenu(new QMenu)
21{
22 mMenu->setObjectName(QStringLiteral("menu"));
23 mMenu->setTitle(i18n("Translate…"));
24 mMenu->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-locale")));
25 updateMenu();
26}
27
28TranslatorMenu::~TranslatorMenu()
29{
30 delete mMenu;
31}
32
33bool TranslatorMenu::isEmpty() const
34{
35 return mMenu->isEmpty();
36}
37
38void TranslatorMenu::updateMenu()
39{
40 mMenu->clear();
41 KConfigGroup groupTranslate(KSharedConfig::openConfig(), QStringLiteral("Translate"));
42 const QString engine = groupTranslate.readEntry(QStringLiteral("engine"), QStringLiteral("google")); // Google by default
43 // qDebug() << " engine " << engine;
44 const QString currentPluginName = TextTranslator::TranslatorEngineLoader::self()->currentPluginName(engine);
45 QString actionText;
46 if (!currentPluginName.isEmpty()) {
47 actionText = QStringLiteral("[%1] ").arg(currentPluginName);
48 }
49 const auto fromList = groupTranslate.readEntry(QStringLiteral("From"), QStringList());
50 const auto toList = groupTranslate.readEntry(QStringLiteral("To"), QStringList());
51 for (const auto &fromLang : fromList) {
52 const QString fromLangI18n = TextTranslator::TranslatorUtil::searchI18nFromLanguage(fromLang);
53 if (fromLangI18n.isEmpty()) {
54 qCWarning(TEXTTRANSLATOR_LOG) << "Impossible to find \"from\" language " << fromLangI18n;
55 } else {
56 for (const auto &toLang : toList) {
57 const QString toLangI18n = TextTranslator::TranslatorUtil::searchI18nFromLanguage(toLang);
58 if (toLangI18n.isEmpty()) {
59 qCWarning(TEXTTRANSLATOR_LOG) << "Impossible to find \"to\" language " << fromLangI18n;
60 } else {
61 if (fromLangI18n != toLangI18n) {
62 auto action = new QAction(mMenu);
63 action->setText(QStringLiteral("%1%2 -> %3").arg(actionText, fromLangI18n, toLangI18n));
64 connect(action, &QAction::triggered, this, [this, fromLang, toLang]() {
65 Q_EMIT translate(fromLang, toLang, mModelIndex);
66 });
67 mMenu->addAction(action);
68 }
69 }
70 }
71 }
72 }
73}
74
75const QPersistentModelIndex &TranslatorMenu::modelIndex() const
76{
77 return mModelIndex;
78}
79
80void TranslatorMenu::setModelIndex(const QPersistentModelIndex &newModelIndex)
81{
82 mModelIndex = newModelIndex;
83}
84
85QMenu *TranslatorMenu::menu() const
86{
87 return mMenu;
88}
89
90#include "moc_translatormenu.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18n(const char *text, const TYPE &arg...)
void triggered(bool checked)
QIcon fromTheme(const QString &name)
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
void clear()
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setObjectName(QAnyStringView name)
QString arg(Args &&... args) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:02:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.