Initial commit. corresponds to 1.0-1 release
[flashstrobe] / src / musicplayer.cpp
diff --git a/src/musicplayer.cpp b/src/musicplayer.cpp
new file mode 100644 (file)
index 0000000..7228d15
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+  Copyright (C) 2010 by Juan Carlos Torres <jucato@kdemail.net>
+
+  This program is free software; you can redistribute it and/or
+  modify it under the terms of the GNU General Public License as
+  published by the Free Software Foundation; either version 2 of
+  the License or (at your option) version 3 or any later version
+  accepted by the membership of KDE e.V. (or its successor appro-
+  ved by the membership of KDE e.V.), which shall act as a proxy
+  defined in Section 14 of version 3 of the license.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program. If not, see http://www.gnu.org/licenses/.
+*/
+
+
+#include "musicplayer.h"
+
+#include <MediaObject>
+#include <MediaSource>
+#include <AudioOutput>
+#include <VolumeSlider>
+
+#include <QPushButton>
+#include <QLineEdit>
+#include <QHBoxLayout>
+#include <QVBoxLayout>
+#include <QStyle>
+#include <QFileDialog>
+#include <QDesktopServices>
+
+MusicPlayer::MusicPlayer(QWidget* parent, Qt::WindowFlags flags): QWidget(parent, flags)
+{
+    // Create the Phonon objects and paths
+    m_player = new Phonon::MediaObject(this);
+    m_audio = new Phonon::AudioOutput(Phonon::MusicCategory, this);
+    Phonon::createPath(m_player, m_audio);
+
+    // Setup the UI widgets and layout
+    QPushButton* openButton = new QPushButton(this);
+    openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
+
+    m_rewindButton = new QPushButton(this);
+    m_rewindButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
+    m_rewindButton->setEnabled(false);
+
+    m_playButton = new QPushButton(this);
+    m_playIcon = style()->standardIcon(QStyle::SP_MediaPlay);
+    m_pauseIcon = style()->standardIcon(QStyle::SP_MediaPause);
+    m_playButton->setIcon(m_playIcon);
+    m_playButton->setCheckable(true);
+    m_playButton->setChecked(false);
+    m_playButton->setEnabled(false);
+
+    m_filenameEdit = new QLineEdit(this);
+    m_filenameEdit->setPlaceholderText(tr("Open audio file"));
+
+    QHBoxLayout* fileLayout = new QHBoxLayout;
+    fileLayout->addWidget(m_filenameEdit, 1);
+    fileLayout->addWidget(openButton, 0);
+
+    QHBoxLayout* buttonsLayout = new QHBoxLayout;
+    buttonsLayout->addWidget(m_rewindButton, 0);
+    buttonsLayout->addWidget(m_playButton, 0);
+
+    QVBoxLayout* mainLayout = new QVBoxLayout(this);
+    mainLayout->addLayout(fileLayout, 0);
+    mainLayout->addLayout(buttonsLayout, 0);
+
+    setLayout(mainLayout);
+
+    // Setup connections
+    connect(openButton, SIGNAL(clicked(bool)), this, SLOT(openFile()));
+    connect(m_filenameEdit, SIGNAL(textChanged(QString)), this, SLOT(enableButtons(QString)));
+    connect(m_playButton, SIGNAL(toggled(bool)), this, SLOT(playOrPause(bool)));
+    connect(m_rewindButton, SIGNAL(clicked(bool)), this, SLOT(rewind()));
+}
+
+MusicPlayer::~MusicPlayer()
+{
+
+}
+
+void MusicPlayer::openFile()
+{
+    QString filename = QFileDialog::getOpenFileName(this, tr("Open a music file"), QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
+
+    if (!filename.isEmpty())
+    {
+        m_filenameEdit->setText(filename);
+    }
+}
+
+void MusicPlayer::playOrPause(bool checked)
+{
+    QString filename = m_filenameEdit->text();
+
+    // Only set the currentSource if the filename is different
+    // to prevent rewinding the file
+    if (filename != m_player->currentSource().fileName())
+    {
+        m_player->setCurrentSource(Phonon::MediaSource(filename));
+    }
+
+    // checked == true, play the file
+    if (checked)
+    {
+        m_playButton->setIcon(m_pauseIcon);
+        m_player->play();
+
+        // If the file cannot be played, do not play ("pause")
+        if (m_player->state() == Phonon::ErrorState)
+        {
+            m_playButton->setChecked(false);
+        }
+    }
+    else
+    {
+        m_playButton->setIcon(m_playIcon);
+        m_player->pause();
+    }
+}
+
+void MusicPlayer::enableButtons(QString filename)
+{
+    if (!filename.isEmpty())
+    {
+        m_rewindButton->setEnabled(true);
+        m_playButton->setEnabled(true);
+    }
+    else
+    {
+        m_rewindButton->setEnabled(false);
+        m_playButton->setEnabled(false);
+    }
+}
+
+void MusicPlayer::rewind()
+{
+    m_player->seek(0);
+}
+
+void MusicPlayer::setVolume(qreal volume)
+{
+    m_audio->setVolume(volume);
+}
+
+qreal MusicPlayer::volume() const
+{
+    return m_audio->volume();
+}