1 /*
2    SPDX-FileCopyrightText: 2015-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "translatorview.h"
8 #include "translatorwidget.h"
9 
10 #include <KActionCollection>
11 #include <KLocalizedString>
12 #include <KToggleAction>
13 #include <QHBoxLayout>
14 
TranslatorView(KActionCollection * ac,QWidget * parent)15 TranslatorView::TranslatorView(KActionCollection *ac, QWidget *parent)
16     : PimCommon::CustomToolsViewInterface(parent)
17     , mTranslatorWidget(new PimCommon::TranslatorWidget(this))
18 {
19     auto layout = new QHBoxLayout(this);
20     layout->setContentsMargins({});
21     connect(mTranslatorWidget, &PimCommon::TranslatorWidget::toolsWasClosed, this, &TranslatorView::toolsWasClosed);
22 
23     layout->addWidget(mTranslatorWidget);
24     createAction(ac);
25 }
26 
~TranslatorView()27 TranslatorView::~TranslatorView()
28 {
29 }
30 
setText(const QString & text)31 void TranslatorView::setText(const QString &text)
32 {
33     mTranslatorWidget->setTextToTranslate(text);
34 }
35 
action() const36 KToggleAction *TranslatorView::action() const
37 {
38     return mAction;
39 }
40 
slotActivateTranslator(bool state)41 void TranslatorView::slotActivateTranslator(bool state)
42 {
43     if (state) {
44         mTranslatorWidget->show();
45         Q_EMIT activateView(this);
46     } else {
47         mTranslatorWidget->hide();
48         Q_EMIT activateView(nullptr);
49     }
50 }
51 
createAction(KActionCollection * ac)52 void TranslatorView::createAction(KActionCollection *ac)
53 {
54     mAction = new KToggleAction(i18n("&Translator"), this);
55     connect(mAction, &KToggleAction::triggered, this, &TranslatorView::slotActivateTranslator);
56     if (ac) {
57         ac->addAction(QStringLiteral("translator"), mAction);
58         ac->setDefaultShortcut(mAction, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_T));
59     }
60     mAction->setChecked(false);
61 }
62