summaryrefslogtreecommitdiffstats
path: root/sigmodr/widgets/SoundUI.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sigmodr/widgets/SoundUI.cpp')
-rw-r--r--sigmodr/widgets/SoundUI.cpp159
1 files changed, 159 insertions, 0 deletions
diff --git a/sigmodr/widgets/SoundUI.cpp b/sigmodr/widgets/SoundUI.cpp
new file mode 100644
index 00000000..eac2a4e7
--- /dev/null
+++ b/sigmodr/widgets/SoundUI.cpp
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2008 Ben Boeckel <MathStuf@gmail.com>
+ *
+ * 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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/>.
+ */
+
+// Header include
+#include "SoundUI.h"
+
+// Sigmod includes
+#include "../sigmod/Sigmod.h"
+#include "../sigmod/Sound.h"
+
+// KDE includes
+#include <KFileDialog>
+#include <KMessageBox>
+#include <KUrl>
+
+// Qt includes
+#include <QtCore/QBuffer>
+#include <QtCore/QFile>
+#include <QtCore/QTime>
+
+// Phonon includes
+#include <Phonon/AudioOutput>
+#include <Phonon/MediaObject>
+
+Sigmodr::SoundUI::SoundUI(Sigmod::Sound* sound, QWidget* parent) :
+ ObjectUI(parent),
+ m_output(new Phonon::AudioOutput(Phonon::MusicCategory, this)),
+ m_media(new Phonon::MediaObject(this)),
+ m_buffer(new QBuffer)
+{
+ setupUi(this);
+ setObjects(sound, new Sigmod::Sound(*sound));
+}
+
+Sigmodr::SoundUI::~SoundUI()
+{
+ delete m_media;
+ delete m_buffer;
+}
+
+void Sigmodr::SoundUI::initGui()
+{
+ Phonon::createPath(m_media, m_output);
+ m_media->setTickInterval(100);
+ seeker->setMediaObject(m_media);
+ sliderVolume->setAudioOutput(m_output);
+ connect(m_media, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
+ connect(m_media, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged(Phonon::State, Phonon::State)));
+ connect(buttonPlay, SIGNAL(clicked()), m_media, SLOT(play()));
+ connect(buttonPause, SIGNAL(clicked()), m_media, SLOT(pause()));
+ connect(buttonStop, SIGNAL(clicked()), m_media, SLOT(stop()));
+ buttonPlay->setIcon(KIcon("media-playback-start"));
+ buttonPause->setIcon(KIcon("media-playback-pause"));
+ buttonStop->setIcon(KIcon("media-playback-stop"));
+ buttonBrowse->setIcon(KIcon("document-open"));
+}
+
+void Sigmodr::SoundUI::refreshGui()
+{
+ resetAudioData();
+}
+
+void Sigmodr::SoundUI::setGui()
+{
+ varName->setText(qobject_cast<Sigmod::Sound*>(modified())->name());
+}
+
+void Sigmodr::SoundUI::apply()
+{
+ *qobject_cast<Sigmod::Sound*>(original()) = *qobject_cast<Sigmod::Sound*>(modified());
+ emit(changed(false));
+}
+
+void Sigmodr::SoundUI::discard()
+{
+ *qobject_cast<Sigmod::Sound*>(modified()) = *qobject_cast<Sigmod::Sound*>(original());
+ setGui();
+ emit(changed(false));
+}
+
+void Sigmodr::SoundUI::on_varName_textChanged(const QString& name)
+{
+ const int cursor = varName->cursorPosition();
+ qobject_cast<Sigmod::Sound*>(modified())->setName(name);
+ varName->setCursorPosition(cursor);
+}
+
+void Sigmodr::SoundUI::on_buttonBrowse_clicked()
+{
+ QFile file(KFileDialog::getOpenFileName(KUrl("kfiledialog:///audio"), "audio/*|Audio files", this));
+ if (file.open(QIODevice::ReadOnly))
+ {
+ qobject_cast<Sigmod::Sound*>(modified())->setData(file.readAll());
+ file.close();
+ resetAudioData();
+ }
+}
+
+void Sigmodr::SoundUI::stateChanged(Phonon::State newState, Phonon::State oldState)
+{
+ Q_UNUSED(oldState)
+ switch (newState)
+ {
+ case Phonon::ErrorState:
+ if (m_media->errorType() == Phonon::FatalError)
+ KMessageBox::warningContinueCancel(this, "Fatal Error", m_media->errorString());
+ else
+ KMessageBox::warningContinueCancel(this, "Error", m_media->errorString());
+ break;
+ case Phonon::PlayingState:
+ buttonPlay->setEnabled(false);
+ buttonPause->setEnabled(true);
+ buttonStop->setEnabled(true);
+ break;
+ case Phonon::StoppedState:
+ buttonPlay->setEnabled(true);
+ buttonPause->setEnabled(false);
+ buttonStop->setEnabled(false);
+ tick(0);
+ break;
+ case Phonon::PausedState:
+ buttonPlay->setEnabled(true);
+ buttonPause->setEnabled(false);
+ buttonStop->setEnabled(true);
+ break;
+ default:
+ break;
+ }
+}
+
+void Sigmodr::SoundUI::resetAudioData()
+{
+ m_media->stop();
+ m_buffer->setData(qobject_cast<Sigmod::Sound*>(modified())->data());
+ m_media->setCurrentSource(m_buffer);
+ tick(0);
+}
+
+void Sigmodr::SoundUI::tick(qint64 time)
+{
+ QTime currentTime(0, (time / 60000) % 60, (time / 1000) % 60, time % 1000);
+ qint64 total = m_media->totalTime();
+ QTime totalTime(0, (total / 60000) % 60, (total / 1000) % 60, total % 1000);
+ label->setText(QString("%1 / %2").arg(currentTime.toString("mm:ss.zzz")).arg(totalTime.toString("mm:ss.zzz")));
+}