File indexing completed on 2025-01-19 04:46:56

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "viewerplugintranslatorinterface.h"
0008 #include <TextTranslator/TranslatorWidget>
0009 
0010 #include <KActionCollection>
0011 #include <KLocalizedString>
0012 #include <QAction>
0013 #include <QIcon>
0014 #include <QLayout>
0015 
0016 using namespace MessageViewer;
0017 
0018 ViewerPluginTranslatorInterface::ViewerPluginTranslatorInterface(KActionCollection *ac, QWidget *parent)
0019     : ViewerPluginInterface(parent)
0020 {
0021     createAction(ac);
0022 }
0023 
0024 ViewerPluginTranslatorInterface::~ViewerPluginTranslatorInterface() = default;
0025 
0026 void ViewerPluginTranslatorInterface::setText(const QString &text)
0027 {
0028     widget()->setTextToTranslate(text);
0029 }
0030 
0031 QList<QAction *> ViewerPluginTranslatorInterface::actions() const
0032 {
0033     return mAction;
0034 }
0035 
0036 void ViewerPluginTranslatorInterface::showWidget()
0037 {
0038     widget()->show();
0039 }
0040 
0041 ViewerPluginInterface::SpecificFeatureTypes ViewerPluginTranslatorInterface::featureTypes() const
0042 {
0043     return NeedSelection;
0044 }
0045 
0046 void ViewerPluginTranslatorInterface::createAction(KActionCollection *ac)
0047 {
0048     if (ac) {
0049         auto act = new QAction(i18n("Translate..."), this);
0050         ac->setDefaultShortcut(act, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_T));
0051         act->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-locale")));
0052         ac->addAction(QStringLiteral("translate_text"), act);
0053         connect(act, &QAction::triggered, this, &ViewerPluginTranslatorInterface::slotActivatePlugin);
0054         mAction.append(act);
0055     }
0056 }
0057 
0058 TextTranslator::TranslatorWidget *ViewerPluginTranslatorInterface::widget()
0059 {
0060     if (!mTranslatorWidget) {
0061         auto parentWidget = static_cast<QWidget *>(parent());
0062         mTranslatorWidget = new TextTranslator::TranslatorWidget(parentWidget);
0063         mTranslatorWidget->setObjectName(QLatin1StringView("translatorwidget"));
0064         parentWidget->layout()->addWidget(mTranslatorWidget);
0065         mTranslatorWidget->hide();
0066     }
0067     return mTranslatorWidget;
0068 }
0069 
0070 #include "moc_viewerplugintranslatorinterface.cpp"