File indexing completed on 2024-04-28 16:00:58

0001 /*
0002    SPDX-FileCopyrightText: 2014-2023 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "texttospeechactions.h"
0008 #include <KLocalizedString>
0009 #include <QAction>
0010 
0011 using namespace TextEditTextToSpeech;
0012 
0013 class Q_DECL_HIDDEN TextEditTextToSpeech::TextToSpeechActionsPrivate
0014 {
0015 public:
0016     TextToSpeechActionsPrivate() = default;
0017 
0018     void updateButtonState();
0019     TextToSpeechWidget::State mState = TextToSpeechWidget::Stop;
0020     QAction *mStopAction = nullptr;
0021     QAction *mPlayPauseAction = nullptr;
0022 };
0023 
0024 TextToSpeechActions::TextToSpeechActions(QObject *parent)
0025     : QObject(parent)
0026     , d(new TextEditTextToSpeech::TextToSpeechActionsPrivate)
0027 {
0028     d->mStopAction = new QAction(i18n("Stop"), this);
0029     d->mStopAction->setObjectName(QStringLiteral("stopbutton"));
0030     d->mStopAction->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-stop")));
0031     d->mStopAction->setToolTip(i18n("Stop"));
0032     connect(d->mStopAction, &QAction::triggered, this, &TextToSpeechActions::slotStop);
0033 
0034     d->mPlayPauseAction = new QAction(this);
0035     d->mPlayPauseAction->setObjectName(QStringLiteral("playpausebutton"));
0036     d->mPlayPauseAction->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
0037     connect(d->mPlayPauseAction, &QAction::triggered, this, &TextToSpeechActions::slotPlayPause);
0038 
0039     d->updateButtonState();
0040 }
0041 
0042 TextToSpeechActions::~TextToSpeechActions() = default;
0043 
0044 QAction *TextToSpeechActions::stopAction() const
0045 {
0046     return d->mStopAction;
0047 }
0048 
0049 QAction *TextToSpeechActions::playPauseAction() const
0050 {
0051     return d->mPlayPauseAction;
0052 }
0053 
0054 TextToSpeechWidget::State TextToSpeechActions::state() const
0055 {
0056     return d->mState;
0057 }
0058 
0059 void TextToSpeechActions::setState(TextToSpeechWidget::State state)
0060 {
0061     if (d->mState != state) {
0062         d->mState = state;
0063         d->updateButtonState();
0064     }
0065 }
0066 
0067 void TextToSpeechActionsPrivate::updateButtonState()
0068 {
0069     mPlayPauseAction->setIcon(
0070         QIcon::fromTheme((mState == TextToSpeechWidget::Stop) ? QStringLiteral("media-playback-start") : QStringLiteral("media-playback-pause")));
0071     mPlayPauseAction->setEnabled((mState != TextToSpeechWidget::Stop));
0072     const QString text = (mState != TextToSpeechWidget::Play) ? i18n("Pause") : i18n("Play");
0073     mPlayPauseAction->setToolTip(text);
0074     mPlayPauseAction->setText(text);
0075 }
0076 
0077 void TextToSpeechActions::slotPlayPause()
0078 {
0079     if (d->mState == TextEditTextToSpeech::TextToSpeechWidget::Pause) {
0080         d->mState = TextEditTextToSpeech::TextToSpeechWidget::Play;
0081     } else if (d->mState == TextEditTextToSpeech::TextToSpeechWidget::Play) {
0082         d->mState = TextEditTextToSpeech::TextToSpeechWidget::Pause;
0083     } else if (d->mState == TextEditTextToSpeech::TextToSpeechWidget::Stop) {
0084         d->mState = TextEditTextToSpeech::TextToSpeechWidget::Play;
0085     } else {
0086         return;
0087     }
0088     d->updateButtonState();
0089     Q_EMIT stateChanged(d->mState);
0090 }
0091 
0092 void TextToSpeechActions::slotStop()
0093 {
0094     if (d->mState != TextEditTextToSpeech::TextToSpeechWidget::Stop) {
0095         d->mState = TextEditTextToSpeech::TextToSpeechWidget::Stop;
0096         d->updateButtonState();
0097         Q_EMIT stateChanged(d->mState);
0098     }
0099 }
0100 
0101 #include "moc_texttospeechactions.cpp"