2 Copyright (C) 2010 by Juan Carlos Torres <jucato@kdemail.net>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License or (at your option) version 3 or any later version
8 accepted by the membership of KDE e.V. (or its successor appro-
9 ved by the membership of KDE e.V.), which shall act as a proxy
10 defined in Section 14 of version 3 of the license.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/.
22 #include "musicplayer.h"
24 #include <MediaObject>
25 #include <MediaSource>
26 #include <AudioOutput>
27 #include <VolumeSlider>
29 #include <QPushButton>
31 #include <QHBoxLayout>
32 #include <QVBoxLayout>
34 #include <QFileDialog>
35 #include <QDesktopServices>
37 MusicPlayer::MusicPlayer(QWidget* parent, Qt::WindowFlags flags): QWidget(parent, flags)
39 // Create the Phonon objects and paths
40 m_player = new Phonon::MediaObject(this);
41 m_audio = new Phonon::AudioOutput(Phonon::MusicCategory, this);
42 Phonon::createPath(m_player, m_audio);
44 // Setup the UI widgets and layout
45 QPushButton* openButton = new QPushButton(this);
46 openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
48 m_rewindButton = new QPushButton(this);
49 m_rewindButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
50 m_rewindButton->setEnabled(false);
52 m_playButton = new QPushButton(this);
53 m_playIcon = style()->standardIcon(QStyle::SP_MediaPlay);
54 m_pauseIcon = style()->standardIcon(QStyle::SP_MediaPause);
55 m_playButton->setIcon(m_playIcon);
56 m_playButton->setCheckable(true);
57 m_playButton->setChecked(false);
58 m_playButton->setEnabled(false);
60 m_filenameEdit = new QLineEdit(this);
61 m_filenameEdit->setPlaceholderText(tr("Open audio file"));
63 QHBoxLayout* fileLayout = new QHBoxLayout;
64 fileLayout->addWidget(m_filenameEdit, 1);
65 fileLayout->addWidget(openButton, 0);
67 QHBoxLayout* buttonsLayout = new QHBoxLayout;
68 buttonsLayout->addWidget(m_rewindButton, 0);
69 buttonsLayout->addWidget(m_playButton, 0);
71 QVBoxLayout* mainLayout = new QVBoxLayout(this);
72 mainLayout->addLayout(fileLayout, 0);
73 mainLayout->addLayout(buttonsLayout, 0);
75 setLayout(mainLayout);
78 connect(openButton, SIGNAL(clicked(bool)), this, SLOT(openFile()));
79 connect(m_filenameEdit, SIGNAL(textChanged(QString)), this, SLOT(enableButtons(QString)));
80 connect(m_playButton, SIGNAL(toggled(bool)), this, SLOT(playOrPause(bool)));
81 connect(m_rewindButton, SIGNAL(clicked(bool)), this, SLOT(rewind()));
84 MusicPlayer::~MusicPlayer()
89 void MusicPlayer::openFile()
91 QString filename = QFileDialog::getOpenFileName(this, tr("Open a music file"), QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
93 if (!filename.isEmpty())
95 m_filenameEdit->setText(filename);
99 void MusicPlayer::playOrPause(bool checked)
101 QString filename = m_filenameEdit->text();
103 // Only set the currentSource if the filename is different
104 // to prevent rewinding the file
105 if (filename != m_player->currentSource().fileName())
107 m_player->setCurrentSource(Phonon::MediaSource(filename));
110 // checked == true, play the file
113 m_playButton->setIcon(m_pauseIcon);
116 // If the file cannot be played, do not play ("pause")
117 if (m_player->state() == Phonon::ErrorState)
119 m_playButton->setChecked(false);
124 m_playButton->setIcon(m_playIcon);
129 void MusicPlayer::enableButtons(QString filename)
131 if (!filename.isEmpty())
133 m_rewindButton->setEnabled(true);
134 m_playButton->setEnabled(true);
138 m_rewindButton->setEnabled(false);
139 m_playButton->setEnabled(false);
143 void MusicPlayer::rewind()
148 void MusicPlayer::setVolume(qreal volume)
150 m_audio->setVolume(volume);
153 qreal MusicPlayer::volume() const
155 return m_audio->volume();