Mailcommon

soundtestwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2001 Marc Mutz <mutz@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "soundtestwidget.h"
8
9#include <KLineEdit>
10#include <KLocalizedString>
11#include <KUrlRequester>
12#include <QAudioOutput>
13#include <QIcon>
14#include <QMediaFormat>
15#include <QMediaPlayer>
16#include <QMimeType>
17#include <QUrl>
18
19#include <QHBoxLayout>
20#include <QPushButton>
21
22#include <QStandardPaths>
23
24using namespace MailCommon;
25
28 , m_urlRequester(new KUrlRequester(this))
29 , m_playButton(new QPushButton(this))
30{
31 auto layout = new QHBoxLayout(this);
32 layout->setContentsMargins({});
33
34 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
35 m_playButton->setToolTip(i18nc("@info:tooltip", "Play"));
36 layout->addWidget(m_playButton);
37
38 layout->addWidget(m_urlRequester);
39
40 connect(m_playButton, &QPushButton::clicked, this, &SoundTestWidget::playSound);
41 connect(m_urlRequester, &KUrlRequester::openFileDialog, this, &SoundTestWidget::openSoundDialog);
42 connect(m_urlRequester->lineEdit(), &QLineEdit::textChanged, this, &SoundTestWidget::slotUrlChanged);
43
44 slotUrlChanged(m_urlRequester->lineEdit()->text());
45}
46
48
49void SoundTestWidget::slotUrlChanged(const QString &url)
50{
51 m_playButton->setEnabled(!url.trimmed().isEmpty());
53}
54
55void SoundTestWidget::openSoundDialog(KUrlRequester *)
56{
57 static bool init = true;
58 if (!init) {
59 return;
60 }
61
62 init = false;
63 QFileDialog *fileDialog = m_urlRequester->fileDialog();
64 fileDialog->setWindowTitle(i18nc("@title:window", "Select Sound File"));
65
66 QStringList mimeTypes;
67 for (const auto &format : QMediaFormat(QMediaFormat::UnspecifiedFormat).supportedFileFormats(QMediaFormat::Decode)) {
68 mimeTypes << QMediaFormat(format).mimeType().name();
69 }
70 m_urlRequester->setMimeTypeFilters(mimeTypes);
71
72 const QStringList soundDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("sound/"), QStandardPaths::LocateDirectory);
73
74 if (!soundDirs.isEmpty()) {
75 QUrl soundURL;
76 QDir dir;
77 dir.setFilter(QDir::Files | QDir::Readable);
78
79 for (const QString &soundDir : soundDirs) {
80 dir = soundDir;
81 if (dir.isReadable() && dir.count() > 2) {
82 soundURL.setPath(soundDir);
83 m_urlRequester->setUrl(soundURL);
84 break;
85 }
86 }
87 }
88}
89
90void SoundTestWidget::playSound()
91{
92 const QString parameter = m_urlRequester->lineEdit()->text();
93 if (parameter.isEmpty()) {
94 return;
95 }
96
97 const QString file = QStringLiteral("file:");
98 const QString play = (parameter.startsWith(file) ? parameter.mid(file.length()) : parameter);
99 if (m_player) {
100 if (m_player->playbackState() == QMediaPlayer::PlayingState) {
101 m_player->pause();
102 } else {
103 m_player->setSource(QUrl::fromLocalFile(play));
104 m_player->play();
105 }
106 } else {
107 m_player = new QMediaPlayer(this);
108 m_player->setAudioOutput(new QAudioOutput(this));
109 m_player->setSource(QUrl::fromLocalFile(play));
110 m_player->play();
111 connect(m_player, &QMediaPlayer::playbackStateChanged, this, [this](QMediaPlayer::PlaybackState newState) {
112 if (newState == QMediaPlayer::PlayingState) {
113 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-pause")));
114 m_playButton->setToolTip(i18nc("@info:tooltip", "Pause"));
115 } else {
116 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
117 m_playButton->setToolTip(i18nc("@info:tooltip", "Play"));
118 }
119 });
120 }
121}
122
124{
125 return m_urlRequester->lineEdit()->text();
126}
127
129{
130 m_urlRequester->lineEdit()->setText(url);
131}
132
134{
135 m_urlRequester->lineEdit()->clear();
136}
137
138#include "moc_soundtestwidget.cpp"
void openFileDialog(KUrlRequester *)
SoundTestWidget(QWidget *parent=nullptr)
Creates a new sound test widget.
void setUrl(const QString &url)
Sets the url of the sound file to play.
void textChanged(const QString &)
This signal is emitted when the user enters a new URL.
~SoundTestWidget() override
Destroys the sound test widget.
void clear()
Clears the url of the sound file to play.
QString url() const
Returns the url of the sound file to play.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
KIOCORE_EXPORT QString dir(const QString &fileClass)
The filter dialog.
void clicked(bool checked)
QIcon fromTheme(const QString &name)
void textChanged(const QString &text)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
qsizetype count() const const
void clear()
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString trimmed() const const
QUrl fromLocalFile(const QString &localFile)
void setPath(const QString &path, ParsingMode mode)
QWidget(QWidget *parent, Qt::WindowFlags f)
void setEnabled(bool)
QLayout * layout() const const
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:56:35 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.