KTextAddons

texttospeechwidget.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 "texttospeechwidget.h"
8#include "texttospeechactions.h"
9#include "texttospeechconfigdialog.h"
10#include "texttospeechinterface.h"
11#include "texttospeechsliderwidget.h"
12#include <KLocalizedString>
13#include <KMessageBox>
14#include <QHBoxLayout>
15#include <QLabel>
16#include <QPointer>
17#include <QTimer>
18#include <QToolButton>
19
20using namespace std::chrono_literals;
21#include <chrono>
22
23using namespace TextEditTextToSpeech;
24
25class Q_DECL_HIDDEN TextEditTextToSpeech::TextToSpeechWidgetPrivate
26{
27public:
28 TextToSpeechWidgetPrivate() = default;
29
31 QToolButton *mStopButton = nullptr;
32 QToolButton *mPlayPauseButton = nullptr;
33 QToolButton *mConfigureButton = nullptr;
34 TextToSpeechInterface *mTextToSpeechInterface = nullptr;
35 TextToSpeechActions *mTextToSpeechActions = nullptr;
36 TextToSpeechSliderWidget *mVolume = nullptr;
37 bool mNeedToHide = false;
38};
39
40TextToSpeechWidget::TextToSpeechWidget(QWidget *parent)
41 : QWidget(parent)
42 , d(new TextEditTextToSpeech::TextToSpeechWidgetPrivate)
43{
44 auto hbox = new QHBoxLayout(this);
45 hbox->setObjectName(QStringLiteral("hbox"));
46 hbox->setContentsMargins(QMargins{});
47
48 d->mTextToSpeechActions = new TextToSpeechActions(this);
49 connect(d->mTextToSpeechActions, &TextToSpeechActions::stateChanged, this, &TextToSpeechWidget::stateChanged);
50
51 auto close = new QToolButton(this);
52 close->setObjectName(QStringLiteral("close-button"));
53 close->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
54 close->setToolTip(i18nc("@info:tooltip", "Close"));
55 connect(close, &QToolButton::clicked, this, &TextToSpeechWidget::slotCloseTextToSpeechWidget);
56 hbox->addWidget(close);
57 hbox->addStretch(0);
58
59 auto volume = new QLabel(i18nc("@label:textbox", "Volume:"), this);
60 hbox->addWidget(volume);
61 d->mVolume = new TextToSpeechSliderWidget(QStringLiteral("%1 %"), this);
62 d->mVolume->setMinimumWidth(100);
63 d->mVolume->setObjectName(QStringLiteral("volumeslider"));
64 d->mVolume->setRange(0, 100);
65 connect(d->mVolume, &TextToSpeechSliderWidget::valueChanged, this, &TextToSpeechWidget::slotVolumeChanged);
66 hbox->addWidget(d->mVolume);
67
68 d->mStopButton = new QToolButton(this);
69 d->mStopButton->setObjectName(QStringLiteral("stopbutton"));
70 d->mStopButton->setDefaultAction(d->mTextToSpeechActions->stopAction());
71 hbox->addWidget(d->mStopButton);
72
73 d->mPlayPauseButton = new QToolButton(this);
74 d->mPlayPauseButton->setObjectName(QStringLiteral("playpausebutton"));
75 d->mPlayPauseButton->setDefaultAction(d->mTextToSpeechActions->playPauseAction());
76 hbox->addWidget(d->mPlayPauseButton);
77
78 d->mConfigureButton = new QToolButton(this);
79 d->mConfigureButton->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
80 d->mConfigureButton->setToolTip(i18nc("@info:tooltip", "Configureā€¦"));
81 d->mConfigureButton->setObjectName(QStringLiteral("configurebutton"));
82 connect(d->mConfigureButton, &QToolButton::clicked, this, &TextToSpeechWidget::slotConfigure);
83 hbox->addWidget(d->mConfigureButton);
84
85 d->mTextToSpeechInterface = new TextToSpeechInterface(this, this);
86 applyVolume();
88 hideWidget();
89}
90
91TextToSpeechWidget::~TextToSpeechWidget() = default;
92
93void TextToSpeechWidget::slotCloseTextToSpeechWidget()
94{
95 d->mTextToSpeechActions->slotStop();
96 hideWidget();
97}
98
99void TextToSpeechWidget::slotConfigure()
100{
101 if (!d->mConfigDialog.data()) {
102 d->mNeedToHide = false;
103 d->mConfigDialog = new TextToSpeechConfigDialog(this);
104 if (d->mConfigDialog->exec()) {
105 d->mTextToSpeechInterface->reloadSettings();
106 applyVolume();
107 }
108 delete d->mConfigDialog;
109 if (d->mNeedToHide) {
110 hideWidget();
111 d->mNeedToHide = false;
112 }
113 }
114}
115
116void TextToSpeechWidget::slotVolumeChanged(int value)
117{
118 d->mTextToSpeechInterface->setVolume(value / 100.0);
119}
120
121bool TextToSpeechWidget::isReady() const
122{
123 return d->mTextToSpeechInterface->isReady();
124}
125
126void TextToSpeechWidget::say(const QString &text)
127{
128 if (!text.isEmpty()) {
129 if (d->mTextToSpeechInterface->isReady()) {
130 d->mTextToSpeechInterface->say(text);
131 } else {
132 KMessageBox::error(this, i18n("Engine has a problem."), i18nc("@title:window", "Text To Speech"));
133 }
134 }
135}
136
137TextToSpeechWidget::State TextToSpeechWidget::state() const
138{
139 return d->mTextToSpeechActions->state();
140}
141
142void TextToSpeechWidget::slotStateChanged(TextEditTextToSpeech::TextToSpeech::State state)
143{
144 switch (state) {
145 case TextEditTextToSpeech::TextToSpeech::Ready:
146 if (state == TextEditTextToSpeech::TextToSpeech::Ready) {
147 d->mTextToSpeechActions->setState(TextToSpeechWidget::Stop);
148 if (d->mConfigDialog) {
149 d->mNeedToHide = true;
150 } else {
151 QTimer::singleShot(2s, this, &TextToSpeechWidget::hideWidget);
152 }
153 }
154 break;
155 default:
156 // TODO
157 break;
158 }
159}
160
161void TextToSpeechWidget::showWidget()
162{
163 show();
164 Q_EMIT changeVisibility(true);
165}
166
167void TextToSpeechWidget::hideWidget()
168{
169 hide();
170 Q_EMIT changeVisibility(false);
171}
172
173void TextToSpeechWidget::setState(TextToSpeechWidget::State state)
174{
175 d->mTextToSpeechActions->setState(state);
176}
177
178void TextToSpeechWidget::setTextToSpeechInterface(TextToSpeechInterface *interface)
179{
180 delete d->mTextToSpeechInterface;
181 d->mTextToSpeechInterface = interface;
182 // Update volume value
183 if (d->mTextToSpeechInterface) {
184 d->mTextToSpeechInterface->reloadSettings();
185 applyVolume();
186 }
187}
188
189void TextToSpeechWidget::applyVolume()
190{
191 // Api return volume between 0.0 -> 1.0
192 // We want display between 0 -> 100
193 d->mVolume->setValue(d->mTextToSpeechInterface->volume() * 100);
194}
195
196#include "moc_texttospeechwidget.cpp"
void setIcon(const QIcon &iconset)
void setToolTip(const QString &tooltip)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KGuiItem close()
void clicked(bool checked)
QIcon fromTheme(const QString &name)
Q_EMITQ_EMIT
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void hide()
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.