Mailcommon

soundtestwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2001 Marc Mutz <[email protected]>
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 
22 using 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(i18n("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 
47 void SoundTestWidget::slotUrlChanged(const QString &url)
48 {
49  m_playButton->setEnabled(!url.trimmed().isEmpty());
51 }
52 
53 void 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 
84 void 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 {
97  m_player->setCurrentSource(QUrl::fromLocalFile(play));
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(i18n("Pause"));
108  } else {
109  m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
110  m_playButton->setToolTip(i18n("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 }
void clear()
Clears the url of the sound file to play.
Q_EMITQ_EMIT
KLineEdit * lineEdit() const
QCA_EXPORT void init()
QLayout * layout() const const
void setMimeTypeFilters(const QStringList &mimeTypes)
QString trimmed() const const
void clicked(bool checked)
void openFileDialog(KUrlRequester *)
void setUrl(const QUrl &url)
QIcon fromTheme(const QString &name)
virtual void setText(const QString &)
PHONON_EXPORT QStringList availableMimeTypes()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void clear()
QString i18n(const char *text, const TYPE &arg...)
State state() const
void textChanged(const QString &text)
bool isEmpty() const const
QUrl fromLocalFile(const QString &localFile)
int length() const const
void setWindowTitle(const QString &)
bool isEmpty() const const
SoundTestWidget(QWidget *parent=nullptr)
Creates a new sound test widget.
void setEnabled(bool)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
void setIcon(const QIcon &icon)
void textChanged(const QString &)
This signal is emitted when the user enters a new URL.
KIOFILEWIDGETS_EXPORT QString dir(const QString &fileClass)
int count() const const
void setToolTip(const QString &)
void setPath(const QString &path, QUrl::ParsingMode mode)
void addWidget(QWidget *w)
void setParent(QObject *parent)
QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
void setUrl(const QString &url)
Sets the url of the sound file to play.
void setContentsMargins(int left, int top, int right, int bottom)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setCurrentSource(const MediaSource &source)
void stateChanged(Phonon::State newstate, Phonon::State oldstate)
virtual QFileDialog * fileDialog() const
QString mid(int position, int n) const const
QString url() const
Returns the url of the sound file to play.
~SoundTestWidget() override
Destroys the sound test widget.
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.