delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / phonon / tests / mediaplayer.cpp
blob3b06c78f04336c9062dcf0e7d419d1e552a20566
1 /* This file is part of the KDE project
2 Copyright (C) 2006-2007 Matthias Kretz <kretz@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) version 3.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "mediacontrols.h"
20 #include <QtCore/QTimer>
21 #include <QtGui/QAction>
22 #include <QtGui/QLabel>
23 #include <QtGui/QDockWidget>
24 #include <QtGui/QMainWindow>
25 #include <QtGui/QBoxLayout>
26 #include <QtGui/QCheckBox>
27 #include <QtGui/QComboBox>
28 #include <QtGui/QPushButton>
29 #include <QtGui/QSlider>
31 #include <phonon/audiooutput.h>
32 #include <phonon/backendcapabilities.h>
33 #include <phonon/effect.h>
34 #include <phonon/effectwidget.h>
35 #include <phonon/mediaobject.h>
36 #include <phonon/path.h>
37 #include <phonon/videowidget.h>
39 #include <kaboutdata.h>
40 #include <kdebug.h>
41 #include <kfiledialog.h>
42 #include <kapplication.h>
43 #include <kcmdlineargs.h>
44 #include <kurl.h>
46 #include <cstdlib>
48 using Phonon::MediaObject;
49 using Phonon::MediaSource;
50 using Phonon::AudioOutput;
51 using Phonon::VideoWidget;
52 using Phonon::MediaControls;
53 using Phonon::Effect;
54 using Phonon::EffectWidget;
56 class MediaPlayer : public QMainWindow
58 Q_OBJECT
59 public:
60 MediaPlayer();
61 void setUrl(const KUrl &url);
63 private Q_SLOTS:
64 void stateChanged(Phonon::State newstate);
65 void workaroundQtBug();
66 void getNextUrl();
67 void startupReady();
68 void openEffectWidget();
69 void toggleScaleMode(bool);
70 void switchAspectRatio(int x);
71 void setBrightness(int b);
73 private:
74 bool setNextSource();
76 QWidget *m_settingsWidget;
77 Phonon::MediaObject *m_media;
78 Phonon::Path m_apath;
79 Phonon::AudioOutput *m_aoutput;
80 Phonon::Path m_vpath;
81 Phonon::Effect *m_effect;
82 Phonon::VideoWidget *m_vwidget;
83 Phonon::MediaControls *m_controls;
84 Phonon::EffectWidget *m_effectWidget;
85 QAction *m_fullScreenAction;
88 MediaPlayer::MediaPlayer()
89 : QMainWindow(0), m_effectWidget(0)
91 QDockWidget *dock = new QDockWidget(this);
92 dock->setAllowedAreas(Qt::BottomDockWidgetArea);
94 m_settingsWidget = new QWidget(dock);
95 dock->setWidget(m_settingsWidget);
96 addDockWidget(Qt::BottomDockWidgetArea, dock);
98 QVBoxLayout *layout = new QVBoxLayout(m_settingsWidget);
100 m_vwidget = new VideoWidget(this);
101 setCentralWidget(m_vwidget);
103 m_fullScreenAction = new QAction(m_vwidget);
104 m_fullScreenAction->setShortcut(Qt::Key_F);
105 m_fullScreenAction->setCheckable(true);
106 m_fullScreenAction->setChecked(false);
107 this->addAction(m_fullScreenAction);
108 connect(m_fullScreenAction, SIGNAL(toggled(bool)), m_vwidget, SLOT(setFullScreen(bool)));
109 connect(m_fullScreenAction, SIGNAL(toggled(bool)), SLOT(workaroundQtBug()));
111 m_aoutput = new AudioOutput(Phonon::VideoCategory, this);
113 m_media = new MediaObject(this);
114 connect(m_media, SIGNAL(finished()), SLOT(getNextUrl()));
115 connect(m_media, SIGNAL(stateChanged(Phonon::State, Phonon::State)), SLOT(stateChanged(Phonon::State)));
117 createPath(m_media, m_vwidget);
118 m_apath = createPath(m_media, m_aoutput);
120 m_controls = new MediaControls(m_settingsWidget);
121 layout->addWidget(m_controls);
122 m_controls->setMediaObject(m_media);
123 m_controls->setAudioOutput(m_aoutput);
124 m_controls->setMaximumHeight(28);
127 QList<AudioEffectDescription> effectList = BackendCapabilities::availableAudioEffects();
128 if (!effectList.isEmpty())
130 m_effect = new AudioEffect(BackendCapabilities::availableAudioEffects().first(), m_apath);
131 m_apath->insertEffect(m_effect);
132 QPushButton *button = new QPushButton(m_settingsWidget);
133 layout->addWidget(button);
134 button->setText("configure effect");
135 connect(button, SIGNAL(clicked()), SLOT(openEffectWidget()));
139 QSlider *slider = new QSlider(m_settingsWidget);
140 layout->addWidget(slider);
141 slider->setOrientation(Qt::Horizontal);
142 slider->setRange(-100, 100);
143 slider->setValue(static_cast<int>(m_vwidget->brightness() * 100));
144 connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setBrightness(int)));
146 QCheckBox *scaleModeCheck = new QCheckBox(m_settingsWidget);
147 layout->addWidget(scaleModeCheck);
148 connect(scaleModeCheck, SIGNAL(toggled(bool)), SLOT(toggleScaleMode(bool)));
150 QWidget *box = new QWidget(m_settingsWidget);
151 layout->addWidget(box);
153 QLabel *label = new QLabel("Aspect Ratio:", box);
154 label->setAlignment(Qt::AlignRight);
155 QComboBox *aspectRatioCombo = new QComboBox(box);
156 QHBoxLayout *hbox = new QHBoxLayout(box);
157 hbox->addWidget(label);
158 hbox->addWidget(aspectRatioCombo);
159 label->setBuddy(aspectRatioCombo);
161 connect(aspectRatioCombo, SIGNAL(currentIndexChanged(int)), SLOT(switchAspectRatio(int)));
162 aspectRatioCombo->addItem("auto");
163 aspectRatioCombo->addItem("fit");
164 aspectRatioCombo->addItem("4:3");
165 aspectRatioCombo->addItem("16:9");
167 this->resize(width(), height() + 240 - m_vwidget->height());
169 QTimer::singleShot(0, this, SLOT(startupReady()));
172 void MediaPlayer::stateChanged(Phonon::State newstate)
174 switch (newstate) {
175 case Phonon::ErrorState:
176 case Phonon::StoppedState:
177 getNextUrl();
178 break;
179 default:
180 break;
184 void MediaPlayer::workaroundQtBug()
186 kDebug();
187 if (m_vwidget->actions().contains(m_fullScreenAction)) {
188 m_vwidget->removeAction(m_fullScreenAction);
189 this->addAction(m_fullScreenAction);
190 } else {
191 this->removeAction(m_fullScreenAction);
192 m_vwidget->addAction(m_fullScreenAction);
196 bool MediaPlayer::setNextSource()
198 QWidget *extraWidget = new QWidget;
199 QHBoxLayout *layout = new QHBoxLayout(extraWidget);
200 layout->setMargin(0);
201 layout->addStretch();
202 QPushButton *dvdButton = new QPushButton(i18n("DVD"), extraWidget);
203 dvdButton->setCheckable(true);
204 dvdButton->setChecked(false);
205 layout->addWidget(dvdButton);
206 QPushButton *cdButton = new QPushButton(i18n("Audio CD"), extraWidget);
207 cdButton->setCheckable(true);
208 cdButton->setChecked(false);
209 layout->addWidget(cdButton);
210 const QString dummyString;
211 const KUrl dummyUrl;
212 KFileDialog dlg(dummyUrl, dummyString, 0, extraWidget);
213 connect(dvdButton, SIGNAL(toggled(bool)), &dlg, SLOT(accept()));
214 connect(cdButton, SIGNAL(toggled(bool)), &dlg, SLOT(accept()));
215 dlg.setOperationMode(KFileDialog::Opening);
216 dlg.setWindowTitle(i18n("Open"));
217 dlg.setMode(KFile::File);
218 dlg.exec();
219 KUrl url = dlg.selectedUrl();
221 if (dvdButton->isChecked()) {
222 if (url.isLocalFile()) {
223 m_media->setCurrentSource(MediaSource(Phonon::Dvd, url.path()));
224 } else {
225 m_media->setCurrentSource(Phonon::Dvd);
227 } else if (cdButton->isChecked()) {
228 m_media->setCurrentSource(Phonon::Cd);
229 } else if (url.isValid()) {
230 m_media->setCurrentSource(url);
231 } else {
232 QApplication::instance()->quit();
233 return false;
235 return true;
238 void MediaPlayer::getNextUrl()
240 static bool fileDialogAlreadyOpen = false;
241 if (fileDialogAlreadyOpen) {
242 return;
244 fileDialogAlreadyOpen = true;
245 if (!setNextSource()) {
246 return;
248 m_media->play();
249 fileDialogAlreadyOpen = false;
252 void MediaPlayer::startupReady()
254 if (m_media->currentSource().type() == MediaSource::Invalid) {
255 if (!setNextSource()) {
256 return;
259 m_media->play();
262 void MediaPlayer::setBrightness(int b)
264 m_vwidget->setBrightness(b * 0.01);
267 void MediaPlayer::switchAspectRatio(int x)
269 m_vwidget->setAspectRatio(static_cast<VideoWidget::AspectRatio>(x));
272 void MediaPlayer::toggleScaleMode(bool mode)
274 if (mode) {
275 m_vwidget->setScaleMode(VideoWidget::ScaleAndCrop);
276 } else {
277 m_vwidget->setScaleMode(VideoWidget::FitInView);
281 void MediaPlayer::openEffectWidget()
283 if (!m_effectWidget)
284 m_effectWidget = new EffectWidget(m_effect);
285 m_effectWidget->show();
286 m_effectWidget->raise();
289 void MediaPlayer::setUrl(const KUrl &url)
291 m_media->setCurrentSource(url);
292 //m_vwidget->setVisible(m_media->hasVideo());
295 int main(int argc, char ** argv)
297 KAboutData about("phononmediaplayer", 0, ki18n("Phonon Media Player"),
298 "0.1", ki18n("Media Player"),
299 KAboutData::License_GPL);
300 about.setProgramIconName("phonon");
301 about.addAuthor(ki18n("Matthias Kretz"), KLocalizedString(), "kretz@kde.org");
303 KCmdLineArgs::init(argc, argv, &about);
304 KCmdLineOptions options;
305 options.add("+[url]", ki18n("File to play"));
306 KCmdLineArgs::addCmdLineOptions( options );
308 KApplication app;
309 MediaPlayer foo;
310 foo.show();
312 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
313 if (args->count() == 1) {
314 KUrl url = args->url(0);
315 if (url.isValid()) {
316 foo.setUrl(url);
319 args->clear();
320 return app.exec();
323 #include "mediaplayer.moc"
324 #include "mediacontrols.cpp"
325 #include "moc_mediacontrols.cpp"