File: texttospeechactions.cpp

package info (click to toggle)
ktextaddons 1.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,856 kB
  • sloc: cpp: 62,045; ansic: 6,520; xml: 2,630; sh: 11; makefile: 7
file content (100 lines) | stat: -rw-r--r-- 3,116 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
   SPDX-FileCopyrightText: 2014-2026 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "texttospeechactions.h"

#include <KLocalizedString>
#include <QAction>

using namespace TextEditTextToSpeech;
using namespace Qt::Literals::StringLiterals;

class Q_DECL_HIDDEN TextEditTextToSpeech::TextToSpeechActionsPrivate
{
public:
    TextToSpeechActionsPrivate() = default;

    void updateButtonState();
    TextToSpeechWidget::State mState = TextToSpeechWidget::Stop;
    QAction *mStopAction = nullptr;
    QAction *mPlayPauseAction = nullptr;
};

TextToSpeechActions::TextToSpeechActions(QObject *parent)
    : QObject(parent)
    , d(new TextEditTextToSpeech::TextToSpeechActionsPrivate)
{
    d->mStopAction = new QAction(i18nc("@action", "Stop"), this);
    d->mStopAction->setObjectName(u"stopbutton"_s);
    d->mStopAction->setIcon(QIcon::fromTheme(u"media-playback-stop"_s));
    d->mStopAction->setToolTip(i18nc("@info:tooltip", "Stop"));
    connect(d->mStopAction, &QAction::triggered, this, &TextToSpeechActions::slotStop);

    d->mPlayPauseAction = new QAction(this);
    d->mPlayPauseAction->setObjectName(u"playpausebutton"_s);
    d->mPlayPauseAction->setIcon(QIcon::fromTheme(u"media-playback-start"_s));
    connect(d->mPlayPauseAction, &QAction::triggered, this, &TextToSpeechActions::slotPlayPause);

    d->updateButtonState();
}

TextToSpeechActions::~TextToSpeechActions() = default;

QAction *TextToSpeechActions::stopAction() const
{
    return d->mStopAction;
}

QAction *TextToSpeechActions::playPauseAction() const
{
    return d->mPlayPauseAction;
}

TextToSpeechWidget::State TextToSpeechActions::state() const
{
    return d->mState;
}

void TextToSpeechActions::setState(TextToSpeechWidget::State state)
{
    if (d->mState != state) {
        d->mState = state;
        d->updateButtonState();
    }
}

void TextToSpeechActionsPrivate::updateButtonState()
{
    mPlayPauseAction->setIcon(QIcon::fromTheme((mState == TextToSpeechWidget::Stop) ? u"media-playback-start"_s : u"media-playback-pause"_s));
    mPlayPauseAction->setEnabled((mState != TextToSpeechWidget::Stop));
    const QString text = (mState != TextToSpeechWidget::Play) ? i18n("Pause") : i18n("Play");
    mPlayPauseAction->setToolTip(text);
    mPlayPauseAction->setText(text);
}

void TextToSpeechActions::slotPlayPause()
{
    if (d->mState == TextEditTextToSpeech::TextToSpeechWidget::Pause || d->mState == TextEditTextToSpeech::TextToSpeechWidget::Stop) {
        d->mState = TextEditTextToSpeech::TextToSpeechWidget::Play;
    } else if (d->mState == TextEditTextToSpeech::TextToSpeechWidget::Play) {
        d->mState = TextEditTextToSpeech::TextToSpeechWidget::Pause;
    } else {
        return;
    }
    d->updateButtonState();
    Q_EMIT stateChanged(d->mState);
}

void TextToSpeechActions::slotStop()
{
    if (d->mState != TextEditTextToSpeech::TextToSpeechWidget::Stop) {
        d->mState = TextEditTextToSpeech::TextToSpeechWidget::Stop;
        d->updateButtonState();
        Q_EMIT stateChanged(d->mState);
    }
}

#include "moc_texttospeechactions.cpp"