KTextAddons

texttospeechactions.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "texttospeechactions.h"
8#include <KLocalizedString>
9#include <QAction>
10
11using namespace TextEditTextToSpeech;
12
13class Q_DECL_HIDDEN TextEditTextToSpeech::TextToSpeechActionsPrivate
14{
15public:
16 TextToSpeechActionsPrivate() = default;
17
18 void updateButtonState();
19 TextToSpeechWidget::State mState = TextToSpeechWidget::Stop;
20 QAction *mStopAction = nullptr;
21 QAction *mPlayPauseAction = nullptr;
22};
23
24TextToSpeechActions::TextToSpeechActions(QObject *parent)
25 : QObject(parent)
26 , d(new TextEditTextToSpeech::TextToSpeechActionsPrivate)
27{
28 d->mStopAction = new QAction(i18nc("@action", "Stop"), this);
29 d->mStopAction->setObjectName(QStringLiteral("stopbutton"));
30 d->mStopAction->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-stop")));
31 d->mStopAction->setToolTip(i18nc("@info:tooltip", "Stop"));
32 connect(d->mStopAction, &QAction::triggered, this, &TextToSpeechActions::slotStop);
33
34 d->mPlayPauseAction = new QAction(this);
35 d->mPlayPauseAction->setObjectName(QStringLiteral("playpausebutton"));
36 d->mPlayPauseAction->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
37 connect(d->mPlayPauseAction, &QAction::triggered, this, &TextToSpeechActions::slotPlayPause);
38
39 d->updateButtonState();
40}
41
42TextToSpeechActions::~TextToSpeechActions() = default;
43
44QAction *TextToSpeechActions::stopAction() const
45{
46 return d->mStopAction;
47}
48
49QAction *TextToSpeechActions::playPauseAction() const
50{
51 return d->mPlayPauseAction;
52}
53
54TextToSpeechWidget::State TextToSpeechActions::state() const
55{
56 return d->mState;
57}
58
59void TextToSpeechActions::setState(TextToSpeechWidget::State state)
60{
61 if (d->mState != state) {
62 d->mState = state;
63 d->updateButtonState();
64 }
65}
66
67void TextToSpeechActionsPrivate::updateButtonState()
68{
69 mPlayPauseAction->setIcon(
70 QIcon::fromTheme((mState == TextToSpeechWidget::Stop) ? QStringLiteral("media-playback-start") : QStringLiteral("media-playback-pause")));
71 mPlayPauseAction->setEnabled((mState != TextToSpeechWidget::Stop));
72 const QString text = (mState != TextToSpeechWidget::Play) ? i18n("Pause") : i18n("Play");
73 mPlayPauseAction->setToolTip(text);
74 mPlayPauseAction->setText(text);
75}
76
77void TextToSpeechActions::slotPlayPause()
78{
79 if (d->mState == TextEditTextToSpeech::TextToSpeechWidget::Pause) {
80 d->mState = TextEditTextToSpeech::TextToSpeechWidget::Play;
81 } else if (d->mState == TextEditTextToSpeech::TextToSpeechWidget::Play) {
82 d->mState = TextEditTextToSpeech::TextToSpeechWidget::Pause;
83 } else if (d->mState == TextEditTextToSpeech::TextToSpeechWidget::Stop) {
84 d->mState = TextEditTextToSpeech::TextToSpeechWidget::Play;
85 } else {
86 return;
87 }
88 d->updateButtonState();
89 Q_EMIT stateChanged(d->mState);
90}
91
92void TextToSpeechActions::slotStop()
93{
94 if (d->mState != TextEditTextToSpeech::TextToSpeechWidget::Stop) {
95 d->mState = TextEditTextToSpeech::TextToSpeechWidget::Stop;
96 d->updateButtonState();
97 Q_EMIT stateChanged(d->mState);
98 }
99}
100
101#include "moc_texttospeechactions.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void setEnabled(bool)
void setIcon(const QIcon &icon)
void setText(const QString &text)
void setToolTip(const QString &tip)
void triggered(bool checked)
QIcon fromTheme(const QString &name)
Q_EMITQ_EMIT
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 Mon Nov 18 2024 12:19:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.