summaryrefslogtreecommitdiffstats
path: root/sigmodr/SoundUI.cpp
blob: eac2a4e7e106653dc8d170410788536e42c41faf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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")));
}