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 <QIcon>
13#include <QUrl>
14
15#include <QHBoxLayout>
16#include <QPushButton>
17
18#include <QStandardPaths>
19#include <phonon/backendcapabilities.h>
20#include <phonon/mediaobject.h>
21
22using namespace MailCommon;
23
25 : QWidget(parent)
26 , m_urlRequester(new KUrlRequester(this))
27 , m_playButton(new QPushButton(this))
28{
29 auto layout = new QHBoxLayout(this);
31
32 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
33 m_playButton->setToolTip(i18nc("@info:tooltip", "Play"));
34 layout->addWidget(m_playButton);
35
36 layout->addWidget(m_urlRequester);
37
38 connect(m_playButton, &QPushButton::clicked, this, &SoundTestWidget::playSound);
39 connect(m_urlRequester, &KUrlRequester::openFileDialog, this, &SoundTestWidget::openSoundDialog);
40 connect(m_urlRequester->lineEdit(), &QLineEdit::textChanged, this, &SoundTestWidget::slotUrlChanged);
41
42 slotUrlChanged(m_urlRequester->lineEdit()->text());
43}
44
46
47void SoundTestWidget::slotUrlChanged(const QString &url)
48{
49 m_playButton->setEnabled(!url.trimmed().isEmpty());
51}
52
53void SoundTestWidget::openSoundDialog(KUrlRequester *)
54{
55 static bool init = true;
56 if (!init) {
57 return;
58 }
59
60 init = false;
61 QFileDialog *fileDialog = m_urlRequester->fileDialog();
62 fileDialog->setWindowTitle(i18nc("@title:window", "Select Sound File"));
63
65
67
68 if (!soundDirs.isEmpty()) {
69 QUrl soundURL;
70 QDir dir;
71 dir.setFilter(QDir::Files | QDir::Readable);
72
73 for (const QString &soundDir : soundDirs) {
74 dir = soundDir;
75 if (dir.isReadable() && dir.count() > 2) {
76 soundURL.setPath(soundDir);
77 m_urlRequester->setUrl(soundURL);
78 break;
79 }
80 }
81 }
82}
83
84void SoundTestWidget::playSound()
85{
86 const QString parameter = m_urlRequester->lineEdit()->text();
87 if (parameter.isEmpty()) {
88 return;
89 }
90
91 const QString file = QStringLiteral("file:");
92 const QString play = (parameter.startsWith(file) ? parameter.mid(file.length()) : parameter);
93 if (m_player) {
94 if (m_player->state() == Phonon::PlayingState) {
95 m_player->pause();
96 } else {
98 m_player->play();
99 }
100 } else {
101 m_player = Phonon::createPlayer(Phonon::NotificationCategory, QUrl::fromLocalFile(play));
102 m_player->setParent(this);
103 m_player->play();
104 connect(m_player, &Phonon::MediaObject::stateChanged, this, [this](Phonon::State newState, Phonon::State) {
105 if (newState == Phonon::PlayingState) {
106 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-pause")));
107 m_playButton->setToolTip(i18nc("@info:tooltip", "Pause"));
108 } else {
109 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
110 m_playButton->setToolTip(i18nc("@info:tooltip", "Play"));
111 }
112 });
113 }
114}
115
117{
118 return m_urlRequester->lineEdit()->text();
119}
120
122{
123 m_urlRequester->lineEdit()->setText(url);
124}
125
127{
128 m_urlRequester->lineEdit()->clear();
129}
130
131#include "moc_soundtestwidget.cpp"
virtual void setText(const QString &)
void setUrl(const QUrl &url)
virtual QFileDialog * fileDialog() const
KLineEdit * lineEdit() const
void openFileDialog(KUrlRequester *)
void setMimeTypeFilters(const QStringList &mimeTypes)
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.
void setCurrentSource(const MediaSource &source)
void stateChanged(Phonon::State newstate, Phonon::State oldstate)
State state() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KIOCORE_EXPORT QString dir(const QString &fileClass)
The filter dialog.
PHONON_EXPORT QStringList availableMimeTypes()
QCA_EXPORT void init()
void clicked(bool checked)
void setIcon(const QIcon &icon)
QIcon fromTheme(const QString &name)
void addWidget(QWidget *w)
void setContentsMargins(const QMargins &margins)
void clear()
void textChanged(const QString &text)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setParent(QObject *parent)
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
qsizetype count() const const
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)
void setEnabled(bool)
QLayout * layout() const const
void setToolTip(const QString &)
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.