/* * Copyright 2008-2009 Ben Boeckel * * 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 . */ // Header include #include "SoundUI.h" // Sigmod includes #include #include // KDE includes #include #include #include #include #include // Phonon includes #include #include #include #include // Qt includes #include #include #include #include #include #include using namespace Sigmod; using namespace Sigmodr::Widgets; SoundUI::SoundUI(Sound* sound, QWidget* parent) : ObjectUI(parent), m_output(new Phonon::AudioOutput(Phonon::MusicCategory, this)), m_media(new Phonon::MediaObject(this)), m_buffer(new QBuffer) { setObjects(sound, new Sound(*sound)); } SoundUI::~SoundUI() { delete m_media; delete m_buffer; } void SoundUI::initGui() { QFile file(":/gui/sound.ui"); file.open(QFile::ReadOnly); QWidget *formWidget = QUiLoader().load(&file, this); file.close(); ui_name = formWidget->findChild("varName"); Phonon::SeekSlider* seeker = formWidget->findChild("seeker"); ui_time = formWidget->findChild("label"); ui_play = formWidget->findChild("buttonPlay"); ui_stop = formWidget->findChild("buttonStop"); Phonon::VolumeSlider* sliderVolume = formWidget->findChild("sliderVolume"); KPushButton* buttonBrowse = formWidget->findChild("buttonBrowse"); 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))); connect(ui_play, SIGNAL(clicked()), m_media, SLOT(play())); connect(ui_stop, SIGNAL(clicked()), m_media, SLOT(stop())); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(formWidget); setLayout(layout); ui_play->setIcon(KIcon("media-playback-start")); ui_stop->setIcon(KIcon("media-playback-stop")); buttonBrowse->setIcon(KIcon("document-open")); } void SoundUI::refreshGui() { resetAudioData(); } void SoundUI::setGui() { ui_name->setText(qobject_cast(modified())->name()); } void SoundUI::apply() { *qobject_cast(original()) = *qobject_cast(modified()); emit(changed(false)); } void SoundUI::discard() { *qobject_cast(modified()) = *qobject_cast(original()); setGui(); emit(changed(false)); } void SoundUI::nameChanged(const QString& name) { const int cursor = ui_name->cursorPosition(); qobject_cast(modified())->setName(name); ui_name->setCursorPosition(cursor); } void SoundUI::browse() { QFile file(KFileDialog::getOpenFileName(KUrl("kfiledialog:///audio"), "audio/*|Audio files", this)); if (file.open(QIODevice::ReadOnly)) { qobject_cast(modified())->setData(file.readAll()); file.close(); resetAudioData(); } } void SoundUI::stateChanged(Phonon::State newState) { 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: disconnect(ui_play, SIGNAL(clicked()), m_media, SLOT(play())); connect(ui_play, SIGNAL(clicked()), m_media, SLOT(pause())); ui_play->setIcon(KIcon("media-playback-pause")); ui_stop->setEnabled(true); break; case Phonon::StoppedState: disconnect(ui_play, SIGNAL(clicked()), m_media, SLOT(play())); disconnect(ui_play, SIGNAL(clicked()), m_media, SLOT(pause())); connect(ui_play, SIGNAL(clicked()), m_media, SLOT(play())); ui_play->setIcon(KIcon("media-playback-start")); ui_stop->setEnabled(false); tick(0); break; case Phonon::PausedState: disconnect(ui_play, SIGNAL(clicked()), m_media, SLOT(play())); disconnect(ui_play, SIGNAL(clicked()), m_media, SLOT(pause())); connect(ui_play, SIGNAL(clicked()), m_media, SLOT(play())); ui_play->setIcon(KIcon("media-playback-start")); ui_stop->setEnabled(true); break; default: break; } } void SoundUI::resetAudioData() { m_media->stop(); m_buffer->setData(qobject_cast(modified())->data()); m_media->setCurrentSource(m_buffer); tick(0); } void 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); ui_time->setText(QString("%1 / %2").arg(currentTime.toString("mm:ss.zzz")).arg(totalTime.toString("mm:ss.zzz"))); }