Initial commit of musik, my KDE4 simplest (and hopefully faster..)
[musik.git] / musikclass.cpp
blobeee7c5ae90c267afba78a2801f167c2272cced65
1 // andrea diamantini - adjam7@gmail.com
2 // musik - simplest KDE4 audio player
3 // file musikui.cpp
5 #include <QtGui>
6 #include <KFileDialog>
8 #include "musikclass.h"
9 #include "musikedit.h"
12 Musik::Musik()
14 audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
15 mediaObject = new Phonon::MediaObject(this);
17 connect( mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
18 this, SLOT(changeState(Phonon::State, Phonon::State)));
20 Phonon::createPath(mediaObject, audioOutput);
22 setupActions();
23 setupUi();
26 Musik::~Musik()
27 {};
30 void Musik::setupActions()
32 playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay),
33 tr("Play"), this);
34 playAction->setShortcut(tr("Crl+P"));
35 playAction->setDisabled(true);
38 pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause),
39 tr("Pause"), this);
40 pauseAction->setShortcut(tr("Space"));
41 pauseAction->setDisabled(true);
44 stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop),
45 tr("Stop"), this);
46 stopAction->setShortcut(tr("Ctrl+S"));
47 stopAction->setDisabled(true);
50 openFileAction = new QAction(style()->standardIcon(QStyle::SP_FileDialogStart),
51 tr("Add &Files"), this);
52 openFileAction->setShortcut(tr("Ctrl+F"));
55 editTagAction = new QAction(style()->standardIcon(QStyle::SP_FileDialogContentsView),
56 tr("Edit Tag"), this);
57 editTagAction->setShortcut(tr("Crl+E"));
58 editTagAction->setDisabled(true);
61 connect ( playAction, SIGNAL( triggered() ), mediaObject, SLOT( play() ) );
62 connect ( pauseAction, SIGNAL( triggered() ), mediaObject, SLOT( pause() ) );
63 connect ( stopAction, SIGNAL( triggered() ), mediaObject, SLOT( stop() ) );
64 connect ( openFileAction, SIGNAL( triggered() ), this, SLOT( chooseFile() ) );
65 connect ( editTagAction, SIGNAL( triggered() ), this, SLOT( editTag() ) );
69 void Musik::setupUi()
71 line = new QLineEdit;
72 line->setReadOnly(true);
74 QToolBar *bar = new QToolBar;
75 bar->addAction(playAction);
76 bar->addAction(pauseAction);
77 bar->addAction(stopAction);
78 bar->addAction(openFileAction);
79 bar->addAction(editTagAction);
81 seekSlider = new Phonon::SeekSlider(this);
82 seekSlider->setMediaObject(mediaObject);
84 volumeSlider = new Phonon::VolumeSlider(this);
85 volumeSlider->setAudioOutput(audioOutput);
86 volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
88 QLabel *volumeLabel = new QLabel;
89 volumeLabel->setPixmap(QPixmap("images/volume.png"));
91 QHBoxLayout *seekerLayout = new QHBoxLayout;
92 seekerLayout->addWidget(seekSlider);
94 QVBoxLayout *upperLayout = new QVBoxLayout;
95 upperLayout->addWidget(line);
96 upperLayout->addLayout(seekerLayout);
98 QHBoxLayout *playbackLayout = new QHBoxLayout;
99 playbackLayout->addWidget(bar);
100 playbackLayout->addStretch();
101 playbackLayout->addWidget(volumeLabel);
102 playbackLayout->addWidget(volumeSlider);
104 QVBoxLayout *mainLayout = new QVBoxLayout;
105 mainLayout->addLayout(upperLayout);
106 mainLayout->addLayout(playbackLayout);
108 setLayout(mainLayout);
109 setWindowTitle("Musik");
110 setWindowIcon( QIcon("/DATI/future/kde/src/musik/musik.png") ); // FIXME
113 // ----------------------------------------------------------------------
115 void Musik::chooseFile()
117 filePath = KFileDialog::getOpenFileName(KUrl(),
118 "Audio ( *.mp3 , *.wav , *.ogg )",
119 this,
120 tr("Open audio file") );
122 if( !filePath.isEmpty() )
124 mediaObject->setCurrentSource(filePath);
125 playAction->setDisabled(false);
128 mediaObject->play();
132 void Musik::changeState(Phonon::State newState, Phonon::State /* oldState */ )
134 switch (newState) {
135 case Phonon::ErrorState:
136 if (mediaObject->errorType() == Phonon::FatalError) {
137 QMessageBox::warning(this, tr("Fatal Error"),
138 mediaObject->errorString());
139 } else {
140 QMessageBox::warning(this, tr("Error"),
141 mediaObject->errorString());
143 break;
144 case Phonon::PlayingState:
145 playAction->setEnabled(false);
146 pauseAction->setEnabled(true);
147 stopAction->setEnabled(true);
148 editTagAction->setEnabled(true);
149 setInfo();
150 break;
151 case Phonon::StoppedState:
152 stopAction->setEnabled(false);
153 playAction->setEnabled(true);
154 pauseAction->setEnabled(false);
155 break;
156 case Phonon::PausedState:
157 pauseAction->setEnabled(false);
158 stopAction->setEnabled(true);
159 playAction->setEnabled(true);
160 break;
161 case Phonon::BufferingState:
162 break;
163 default:
169 void Musik::setInfo()
171 metaData = mediaObject->metaData();
173 QString info;
174 QString author = metaData.value("ARTIST");
175 QString title = metaData.value("TITLE");
176 if (author == "" || title == "")
177 info = mediaObject->currentSource().fileName();
178 else
179 info = author + QString(" - ") + title;
181 line->setText(info);
184 void Musik::editTag()
186 MusikEdit *win = new MusikEdit(*mediaObject);
187 win->show();