Initial commit. corresponds to 1.0-1 release
[flashstrobe] / src / musicplayer.cpp
1 /*
2   Copyright (C) 2010 by Juan Carlos Torres <jucato@kdemail.net>
3
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.
11
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.
16
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/.
19 */
20
21
22 #include "musicplayer.h"
23
24 #include <MediaObject>
25 #include <MediaSource>
26 #include <AudioOutput>
27 #include <VolumeSlider>
28
29 #include <QPushButton>
30 #include <QLineEdit>
31 #include <QHBoxLayout>
32 #include <QVBoxLayout>
33 #include <QStyle>
34 #include <QFileDialog>
35 #include <QDesktopServices>
36
37 MusicPlayer::MusicPlayer(QWidget* parent, Qt::WindowFlags flags): QWidget(parent, flags)
38 {
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);
43
44     // Setup the UI widgets and layout
45     QPushButton* openButton = new QPushButton(this);
46     openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
47
48     m_rewindButton = new QPushButton(this);
49     m_rewindButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
50     m_rewindButton->setEnabled(false);
51
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);
59
60     m_filenameEdit = new QLineEdit(this);
61     m_filenameEdit->setPlaceholderText(tr("Open audio file"));
62
63     QHBoxLayout* fileLayout = new QHBoxLayout;
64     fileLayout->addWidget(m_filenameEdit, 1);
65     fileLayout->addWidget(openButton, 0);
66
67     QHBoxLayout* buttonsLayout = new QHBoxLayout;
68     buttonsLayout->addWidget(m_rewindButton, 0);
69     buttonsLayout->addWidget(m_playButton, 0);
70
71     QVBoxLayout* mainLayout = new QVBoxLayout(this);
72     mainLayout->addLayout(fileLayout, 0);
73     mainLayout->addLayout(buttonsLayout, 0);
74
75     setLayout(mainLayout);
76
77     // Setup connections
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()));
82 }
83
84 MusicPlayer::~MusicPlayer()
85 {
86
87 }
88
89 void MusicPlayer::openFile()
90 {
91     QString filename = QFileDialog::getOpenFileName(this, tr("Open a music file"), QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
92
93     if (!filename.isEmpty())
94     {
95         m_filenameEdit->setText(filename);
96     }
97 }
98
99 void MusicPlayer::playOrPause(bool checked)
100 {
101     QString filename = m_filenameEdit->text();
102
103     // Only set the currentSource if the filename is different
104     // to prevent rewinding the file
105     if (filename != m_player->currentSource().fileName())
106     {
107         m_player->setCurrentSource(Phonon::MediaSource(filename));
108     }
109
110     // checked == true, play the file
111     if (checked)
112     {
113         m_playButton->setIcon(m_pauseIcon);
114         m_player->play();
115
116         // If the file cannot be played, do not play ("pause")
117         if (m_player->state() == Phonon::ErrorState)
118         {
119             m_playButton->setChecked(false);
120         }
121     }
122     else
123     {
124         m_playButton->setIcon(m_playIcon);
125         m_player->pause();
126     }
127 }
128
129 void MusicPlayer::enableButtons(QString filename)
130 {
131     if (!filename.isEmpty())
132     {
133         m_rewindButton->setEnabled(true);
134         m_playButton->setEnabled(true);
135     }
136     else
137     {
138         m_rewindButton->setEnabled(false);
139         m_playButton->setEnabled(false);
140     }
141 }
142
143 void MusicPlayer::rewind()
144 {
145     m_player->seek(0);
146 }
147
148 void MusicPlayer::setVolume(qreal volume)
149 {
150     m_audio->setVolume(volume);
151 }
152
153 qreal MusicPlayer::volume() const
154 {
155     return m_audio->volume();
156 }