source: trunk/demos/mediaplayer/mediaplayer.cpp@ 493

Last change on this file since 493 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 30.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40***************************************************************************/
41
42#include <QtGui>
43
44#define SLIDER_RANGE 8
45
46#include "mediaplayer.h"
47#include "ui_settings.h"
48
49
50class MediaVideoWidget : public Phonon::VideoWidget
51{
52public:
53 MediaVideoWidget(MediaPlayer *player, QWidget *parent = 0) :
54 Phonon::VideoWidget(parent), m_player(player), m_action(this)
55 {
56 m_action.setCheckable(true);
57 m_action.setChecked(false);
58 m_action.setShortcut(QKeySequence( Qt::AltModifier + Qt::Key_Return));
59 m_action.setShortcutContext(Qt::WindowShortcut);
60 connect(&m_action, SIGNAL(toggled(bool)), SLOT(setFullScreen(bool)));
61 addAction(&m_action);
62 setAcceptDrops(true);
63 }
64
65protected:
66 void mouseDoubleClickEvent(QMouseEvent *e)
67 {
68 Phonon::VideoWidget::mouseDoubleClickEvent(e);
69 setFullScreen(!isFullScreen());
70 }
71
72 void keyPressEvent(QKeyEvent *e)
73 {
74 if (e->key() == Qt::Key_Space && !e->modifiers()) {
75 m_player->playPause();
76 e->accept();
77 return;
78 } else if (e->key() == Qt::Key_Escape && !e->modifiers()) {
79 setFullScreen(false);
80 e->accept();
81 return;
82 }
83 Phonon::VideoWidget::keyPressEvent(e);
84 }
85
86 bool event(QEvent *e)
87 {
88 switch(e->type())
89 {
90 case QEvent::Close:
91 //we just ignore the cose events on the video widget
92 //this prevents ALT+F4 from having an effect in fullscreen mode
93 e->ignore();
94 return true;
95 case QEvent::MouseMove:
96#ifndef QT_NO_CURSOR
97 unsetCursor();
98#endif
99 //fall through
100 case QEvent::WindowStateChange:
101 {
102 //we just update the state of the checkbox, in case it wasn't already
103 m_action.setChecked(windowState() & Qt::WindowFullScreen);
104 const Qt::WindowFlags flags = m_player->windowFlags();
105 if (windowState() & Qt::WindowFullScreen) {
106 m_timer.start(1000, this);
107 } else {
108 m_timer.stop();
109#ifndef QT_NO_CURSOR
110 unsetCursor();
111#endif
112 }
113 }
114 break;
115 default:
116 break;
117 }
118
119 return Phonon::VideoWidget::event(e);
120 }
121
122 void timerEvent(QTimerEvent *e)
123 {
124 if (e->timerId() == m_timer.timerId()) {
125 //let's store the cursor shape
126#ifndef QT_NO_CURSOR
127 setCursor(Qt::BlankCursor);
128#endif
129 }
130 Phonon::VideoWidget::timerEvent(e);
131 }
132
133 void dropEvent(QDropEvent *e)
134 {
135 m_player->handleDrop(e);
136 }
137
138 void dragEnterEvent(QDragEnterEvent *e) {
139 if (e->mimeData()->hasUrls())
140 e->acceptProposedAction();
141 }
142
143private:
144 MediaPlayer *m_player;
145 QBasicTimer m_timer;
146 QAction m_action;
147};
148
149
150MediaPlayer::MediaPlayer(const QString &filePath) :
151 playButton(0), nextEffect(0), settingsDialog(0), ui(0),
152 m_AudioOutput(Phonon::VideoCategory),
153 m_videoWidget(new MediaVideoWidget(this))
154{
155 setWindowTitle(tr("Media Player"));
156 setContextMenuPolicy(Qt::CustomContextMenu);
157 m_videoWidget->setContextMenuPolicy(Qt::CustomContextMenu);
158
159 QSize buttonSize(34, 28);
160
161 QPushButton *openButton = new QPushButton(this);
162
163 openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
164 QPalette bpal;
165 QColor arrowcolor = bpal.buttonText().color();
166 if (arrowcolor == Qt::black)
167 arrowcolor = QColor(80, 80, 80);
168 bpal.setBrush(QPalette::ButtonText, arrowcolor);
169 openButton->setPalette(bpal);
170
171 rewindButton = new QPushButton(this);
172 rewindButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
173
174 forwardButton = new QPushButton(this);
175 forwardButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipForward));
176 forwardButton->setEnabled(false);
177
178 playButton = new QPushButton(this);
179 playIcon = style()->standardIcon(QStyle::SP_MediaPlay);
180 pauseIcon = style()->standardIcon(QStyle::SP_MediaPause);
181 playButton->setIcon(playIcon);
182
183 slider = new Phonon::SeekSlider(this);
184 slider->setMediaObject(&m_MediaObject);
185 volume = new Phonon::VolumeSlider(&m_AudioOutput);
186
187 QVBoxLayout *vLayout = new QVBoxLayout(this);
188 vLayout->setContentsMargins(8, 8, 8, 8);
189
190 QHBoxLayout *layout = new QHBoxLayout();
191
192 info = new QLabel(this);
193 info->setMinimumHeight(70);
194 info->setAcceptDrops(false);
195 info->setMargin(2);
196 info->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
197 info->setLineWidth(2);
198 info->setAutoFillBackground(true);
199
200 QPalette palette;
201 palette.setBrush(QPalette::WindowText, Qt::white);
202#ifndef Q_WS_MAC
203 openButton->setMinimumSize(54, buttonSize.height());
204 rewindButton->setMinimumSize(buttonSize);
205 forwardButton->setMinimumSize(buttonSize);
206 playButton->setMinimumSize(buttonSize);
207#endif
208 info->setStyleSheet("border-image:url(:/images/screen.png) ; border-width:3px");
209 info->setPalette(palette);
210 info->setText(tr("<center>No media</center>"));
211
212 volume->setFixedWidth(120);
213
214 layout->addWidget(openButton);
215 layout->addWidget(rewindButton);
216 layout->addWidget(playButton);
217 layout->addWidget(forwardButton);
218
219 layout->addStretch();
220 layout->addWidget(volume);
221
222 vLayout->addWidget(info);
223 initVideoWindow();
224 vLayout->addWidget(&m_videoWindow);
225 QVBoxLayout *buttonPanelLayout = new QVBoxLayout();
226 m_videoWindow.hide();
227 buttonPanelLayout->addLayout(layout);
228
229 timeLabel = new QLabel(this);
230 progressLabel = new QLabel(this);
231 QWidget *sliderPanel = new QWidget(this);
232 QHBoxLayout *sliderLayout = new QHBoxLayout();
233 sliderLayout->addWidget(slider);
234 sliderLayout->addWidget(timeLabel);
235 sliderLayout->addWidget(progressLabel);
236 sliderLayout->setContentsMargins(0, 0, 0, 0);
237 sliderPanel->setLayout(sliderLayout);
238
239 buttonPanelLayout->addWidget(sliderPanel);
240 buttonPanelLayout->setContentsMargins(0, 0, 0, 0);
241#ifdef Q_OS_MAC
242 layout->setSpacing(4);
243 buttonPanelLayout->setSpacing(0);
244 info->setMinimumHeight(100);
245 info->setFont(QFont("verdana", 15));
246 // QStyle *flatButtonStyle = new QWindowsStyle;
247 openButton->setFocusPolicy(Qt::NoFocus);
248 // openButton->setStyle(flatButtonStyle);
249 // playButton->setStyle(flatButtonStyle);
250 // rewindButton->setStyle(flatButtonStyle);
251 // forwardButton->setStyle(flatButtonStyle);
252 #endif
253 QWidget *buttonPanelWidget = new QWidget(this);
254 buttonPanelWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
255 buttonPanelWidget->setLayout(buttonPanelLayout);
256 vLayout->addWidget(buttonPanelWidget);
257
258 QHBoxLayout *labelLayout = new QHBoxLayout();
259
260 vLayout->addLayout(labelLayout);
261 setLayout(vLayout);
262
263 // Create menu bar:
264 fileMenu = new QMenu(this);
265 QAction *openFileAction = fileMenu->addAction(tr("Open &File..."));
266 QAction *openUrlAction = fileMenu->addAction(tr("Open &Location..."));
267
268 fileMenu->addSeparator();
269 QMenu *aspectMenu = fileMenu->addMenu(tr("&Aspect ratio"));
270 QActionGroup *aspectGroup = new QActionGroup(aspectMenu);
271 connect(aspectGroup, SIGNAL(triggered(QAction *)), this, SLOT(aspectChanged(QAction *)));
272 aspectGroup->setExclusive(true);
273 QAction *aspectActionAuto = aspectMenu->addAction(tr("Auto"));
274 aspectActionAuto->setCheckable(true);
275 aspectActionAuto->setChecked(true);
276 aspectGroup->addAction(aspectActionAuto);
277 QAction *aspectActionScale = aspectMenu->addAction(tr("Scale"));
278 aspectActionScale->setCheckable(true);
279 aspectGroup->addAction(aspectActionScale);
280 QAction *aspectAction16_9 = aspectMenu->addAction(tr("16/9"));
281 aspectAction16_9->setCheckable(true);
282 aspectGroup->addAction(aspectAction16_9);
283 QAction *aspectAction4_3 = aspectMenu->addAction(tr("4/3"));
284 aspectAction4_3->setCheckable(true);
285 aspectGroup->addAction(aspectAction4_3);
286
287 QMenu *scaleMenu = fileMenu->addMenu(tr("&Scale mode"));
288 QActionGroup *scaleGroup = new QActionGroup(scaleMenu);
289 connect(scaleGroup, SIGNAL(triggered(QAction *)), this, SLOT(scaleChanged(QAction *)));
290 scaleGroup->setExclusive(true);
291 QAction *scaleActionFit = scaleMenu->addAction(tr("Fit in view"));
292 scaleActionFit->setCheckable(true);
293 scaleActionFit->setChecked(true);
294 scaleGroup->addAction(scaleActionFit);
295 QAction *scaleActionCrop = scaleMenu->addAction(tr("Scale and crop"));
296 scaleActionCrop->setCheckable(true);
297 scaleGroup->addAction(scaleActionCrop);
298
299 fileMenu->addSeparator();
300 QAction *settingsAction = fileMenu->addAction(tr("&Settings..."));
301
302 // Setup signal connections:
303 connect(rewindButton, SIGNAL(clicked()), this, SLOT(rewind()));
304 //connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
305 openButton->setMenu(fileMenu);
306
307 connect(playButton, SIGNAL(clicked()), this, SLOT(playPause()));
308 connect(forwardButton, SIGNAL(clicked()), this, SLOT(forward()));
309 //connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
310 connect(settingsAction, SIGNAL(triggered(bool)), this, SLOT(showSettingsDialog()));
311 connect(openUrlAction, SIGNAL(triggered(bool)), this, SLOT(openUrl()));
312 connect(openFileAction, SIGNAL(triggered(bool)), this, SLOT(openFile()));
313
314 connect(m_videoWidget, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
315 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
316 connect(&m_MediaObject, SIGNAL(metaDataChanged()), this, SLOT(updateInfo()));
317 connect(&m_MediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(updateTime()));
318 connect(&m_MediaObject, SIGNAL(tick(qint64)), this, SLOT(updateTime()));
319 connect(&m_MediaObject, SIGNAL(finished()), this, SLOT(finished()));
320 connect(&m_MediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged(Phonon::State, Phonon::State)));
321 connect(&m_MediaObject, SIGNAL(bufferStatus(int)), this, SLOT(bufferStatus(int)));
322
323 rewindButton->setEnabled(false);
324 playButton->setEnabled(false);
325 setAcceptDrops(true);
326
327 m_audioOutputPath = Phonon::createPath(&m_MediaObject, &m_AudioOutput);
328 Phonon::createPath(&m_MediaObject, m_videoWidget);
329
330 if (!filePath.isEmpty())
331 setFile(filePath);
332 resize(minimumSizeHint());
333}
334
335void MediaPlayer::stateChanged(Phonon::State newstate, Phonon::State oldstate)
336{
337 Q_UNUSED(oldstate);
338
339 if (oldstate == Phonon::LoadingState) {
340 m_videoWindow.setVisible(m_MediaObject.hasVideo());
341 info->setVisible(!m_MediaObject.hasVideo());
342 QRect videoHintRect = QRect(QPoint(0, 0), m_videoWindow.sizeHint());
343 QRect newVideoRect = QApplication::desktop()->screenGeometry().intersected(videoHintRect);
344 if (m_MediaObject.hasVideo()){
345 // Flush event que so that sizeHint takes the
346 // recently shown/hidden m_videoWindow into account:
347 qApp->processEvents();
348 resize(sizeHint());
349 } else
350 resize(minimumSize());
351 }
352
353 switch (newstate) {
354 case Phonon::ErrorState:
355 QMessageBox::warning(this, "Phonon Mediaplayer", m_MediaObject.errorString(), QMessageBox::Close);
356 if (m_MediaObject.errorType() == Phonon::FatalError) {
357 playButton->setEnabled(false);
358 rewindButton->setEnabled(false);
359 } else {
360 m_MediaObject.pause();
361 }
362 break;
363 case Phonon::PausedState:
364 case Phonon::StoppedState:
365 playButton->setIcon(playIcon);
366 if (m_MediaObject.currentSource().type() != Phonon::MediaSource::Invalid){
367 playButton->setEnabled(true);
368 rewindButton->setEnabled(true);
369 } else {
370 playButton->setEnabled(false);
371 rewindButton->setEnabled(false);
372 }
373 break;
374 case Phonon::PlayingState:
375 playButton->setEnabled(true);
376 playButton->setIcon(pauseIcon);
377 if (m_MediaObject.hasVideo())
378 m_videoWindow.show();
379 // Fall through
380 case Phonon::BufferingState:
381 rewindButton->setEnabled(true);
382 break;
383 case Phonon::LoadingState:
384 rewindButton->setEnabled(false);
385 break;
386 }
387
388}
389
390void MediaPlayer::initSettingsDialog()
391{
392 settingsDialog = new QDialog(this);
393 ui = new Ui_settings();
394 ui->setupUi(settingsDialog);
395
396 connect(ui->brightnessSlider, SIGNAL(valueChanged(int)), this, SLOT(setBrightness(int)));
397 connect(ui->hueSlider, SIGNAL(valueChanged(int)), this, SLOT(setHue(int)));
398 connect(ui->saturationSlider, SIGNAL(valueChanged(int)), this, SLOT(setSaturation(int)));
399 connect(ui->contrastSlider , SIGNAL(valueChanged(int)), this, SLOT(setContrast(int)));
400 connect(ui->aspectCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setAspect(int)));
401 connect(ui->scalemodeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setScale(int)));
402
403 ui->brightnessSlider->setValue(int(m_videoWidget->brightness() * SLIDER_RANGE));
404 ui->hueSlider->setValue(int(m_videoWidget->hue() * SLIDER_RANGE));
405 ui->saturationSlider->setValue(int(m_videoWidget->saturation() * SLIDER_RANGE));
406 ui->contrastSlider->setValue(int(m_videoWidget->contrast() * SLIDER_RANGE));
407 ui->aspectCombo->setCurrentIndex(m_videoWidget->aspectRatio());
408 ui->scalemodeCombo->setCurrentIndex(m_videoWidget->scaleMode());
409 connect(ui->effectButton, SIGNAL(clicked()), this, SLOT(configureEffect()));
410
411#ifdef Q_WS_X11
412 //Cross fading is not currently implemented in the GStreamer backend
413 ui->crossFadeSlider->setVisible(false);
414 ui->crossFadeLabel->setVisible(false);
415 ui->crossFadeLabel1->setVisible(false);
416 ui->crossFadeLabel2->setVisible(false);
417 ui->crossFadeLabel3->setVisible(false);
418#endif
419 ui->crossFadeSlider->setValue((int)(2 * m_MediaObject.transitionTime() / 1000.0f));
420
421 // Insert audio devices:
422 QList<Phonon::AudioOutputDevice> devices = Phonon::BackendCapabilities::availableAudioOutputDevices();
423 for (int i=0; i<devices.size(); i++){
424 QString itemText = devices[i].name();
425 if (!devices[i].description().isEmpty()) {
426 itemText += QString::fromLatin1(" (%1)").arg(devices[i].description());
427 }
428 ui->deviceCombo->addItem(itemText);
429 if (devices[i] == m_AudioOutput.outputDevice())
430 ui->deviceCombo->setCurrentIndex(i);
431 }
432
433 // Insert audio effects:
434 ui->audioEffectsCombo->addItem(tr("<no effect>"));
435 QList<Phonon::Effect *> currEffects = m_audioOutputPath.effects();
436 Phonon::Effect *currEffect = currEffects.size() ? currEffects[0] : 0;
437 QList<Phonon::EffectDescription> availableEffects = Phonon::BackendCapabilities::availableAudioEffects();
438 for (int i=0; i<availableEffects.size(); i++){
439 ui->audioEffectsCombo->addItem(availableEffects[i].name());
440 if (currEffect && availableEffects[i] == currEffect->description())
441 ui->audioEffectsCombo->setCurrentIndex(i+1);
442 }
443 connect(ui->audioEffectsCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(effectChanged()));
444
445}
446
447void MediaPlayer::effectChanged()
448{
449 int currentIndex = ui->audioEffectsCombo->currentIndex();
450 if (currentIndex) {
451 QList<Phonon::EffectDescription> availableEffects = Phonon::BackendCapabilities::availableAudioEffects();
452 Phonon::EffectDescription chosenEffect = availableEffects[currentIndex - 1];
453
454 QList<Phonon::Effect *> currEffects = m_audioOutputPath.effects();
455 Phonon::Effect *currentEffect = currEffects.size() ? currEffects[0] : 0;
456
457 // Deleting the running effect will stop playback, it is deleted when removed from path
458 if (nextEffect && !(currentEffect && (currentEffect->description().name() == nextEffect->description().name())))
459 delete nextEffect;
460
461 nextEffect = new Phonon::Effect(chosenEffect);
462 }
463 ui->effectButton->setEnabled(currentIndex);
464}
465
466void MediaPlayer::showSettingsDialog()
467{
468 if (!settingsDialog)
469 initSettingsDialog();
470
471 float oldBrightness = m_videoWidget->brightness();
472 float oldHue = m_videoWidget->hue();
473 float oldSaturation = m_videoWidget->saturation();
474 float oldContrast = m_videoWidget->contrast();
475 Phonon::VideoWidget::AspectRatio oldAspect = m_videoWidget->aspectRatio();
476 Phonon::VideoWidget::ScaleMode oldScale = m_videoWidget->scaleMode();
477 int currentEffect = ui->audioEffectsCombo->currentIndex();
478 settingsDialog->exec();
479
480 if (settingsDialog->result() == QDialog::Accepted){
481 m_MediaObject.setTransitionTime((int)(1000 * float(ui->crossFadeSlider->value()) / 2.0f));
482 QList<Phonon::AudioOutputDevice> devices = Phonon::BackendCapabilities::availableAudioOutputDevices();
483 m_AudioOutput.setOutputDevice(devices[ui->deviceCombo->currentIndex()]);
484 QList<Phonon::Effect *> currEffects = m_audioOutputPath.effects();
485 QList<Phonon::EffectDescription> availableEffects = Phonon::BackendCapabilities::availableAudioEffects();
486
487 if (ui->audioEffectsCombo->currentIndex() > 0){
488 Phonon::Effect *currentEffect = currEffects.size() ? currEffects[0] : 0;
489 if (!currentEffect || currentEffect->description() != nextEffect->description()){
490 foreach(Phonon::Effect *effect, currEffects) {
491 m_audioOutputPath.removeEffect(effect);
492 delete effect;
493 }
494 m_audioOutputPath.insertEffect(nextEffect);
495 }
496 } else {
497 foreach(Phonon::Effect *effect, currEffects) {
498 m_audioOutputPath.removeEffect(effect);
499 delete effect;
500 nextEffect = 0;
501 }
502 }
503 } else {
504 // Restore previous settings
505 m_videoWidget->setBrightness(oldBrightness);
506 m_videoWidget->setSaturation(oldSaturation);
507 m_videoWidget->setHue(oldHue);
508 m_videoWidget->setContrast(oldContrast);
509 m_videoWidget->setAspectRatio(oldAspect);
510 m_videoWidget->setScaleMode(oldScale);
511 ui->audioEffectsCombo->setCurrentIndex(currentEffect);
512 }
513}
514
515void MediaPlayer::initVideoWindow()
516{
517 QVBoxLayout *videoLayout = new QVBoxLayout();
518 videoLayout->addWidget(m_videoWidget);
519 videoLayout->setContentsMargins(0, 0, 0, 0);
520 m_videoWindow.setLayout(videoLayout);
521 m_videoWindow.setMinimumSize(100, 100);
522}
523
524
525void MediaPlayer::configureEffect()
526{
527 if (!nextEffect)
528 return;
529
530
531 QList<Phonon::Effect *> currEffects = m_audioOutputPath.effects();
532 const QList<Phonon::EffectDescription> availableEffects = Phonon::BackendCapabilities::availableAudioEffects();
533 if (ui->audioEffectsCombo->currentIndex() > 0) {
534 Phonon::EffectDescription chosenEffect = availableEffects[ui->audioEffectsCombo->currentIndex() - 1];
535
536 QDialog effectDialog;
537 effectDialog.setWindowTitle(tr("Configure effect"));
538 QVBoxLayout *topLayout = new QVBoxLayout(&effectDialog);
539
540 QLabel *description = new QLabel("<b>Description:</b><br>" + chosenEffect.description(), &effectDialog);
541 description->setWordWrap(true);
542 topLayout->addWidget(description);
543
544 QScrollArea *scrollArea = new QScrollArea(&effectDialog);
545 topLayout->addWidget(scrollArea);
546
547 QVariantList savedParamValues;
548 foreach(Phonon::EffectParameter param, nextEffect->parameters()) {
549 savedParamValues << nextEffect->parameterValue(param);
550 }
551
552 QWidget *scrollWidget = new Phonon::EffectWidget(nextEffect);
553 scrollWidget->setMinimumWidth(320);
554 scrollWidget->setContentsMargins(10, 10, 10,10);
555 scrollArea->setWidget(scrollWidget);
556
557 QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &effectDialog);
558 connect(bbox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), &effectDialog, SLOT(accept()));
559 connect(bbox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), &effectDialog, SLOT(reject()));
560 topLayout->addWidget(bbox);
561
562 effectDialog.exec();
563
564 if (effectDialog.result() != QDialog::Accepted) {
565 //we need to restore the paramaters values
566 int currentIndex = 0;
567 foreach(Phonon::EffectParameter param, nextEffect->parameters()) {
568 nextEffect->setParameterValue(param, savedParamValues.at(currentIndex++));
569 }
570
571 }
572 }
573}
574
575void MediaPlayer::handleDrop(QDropEvent *e)
576{
577 QList<QUrl> urls = e->mimeData()->urls();
578 if (e->proposedAction() == Qt::MoveAction){
579 // Just add to the queue:
580 for (int i=0; i<urls.size(); i++)
581 m_MediaObject.enqueue(Phonon::MediaSource(urls[i].toLocalFile()));
582 } else {
583 // Create new queue:
584 m_MediaObject.clearQueue();
585 if (urls.size() > 0) {
586 QString fileName = urls[0].toLocalFile();
587 QDir dir(fileName);
588 if (dir.exists()) {
589 dir.setFilter(QDir::Files);
590 QStringList entries = dir.entryList();
591 if (entries.size() > 0) {
592 setFile(fileName + QDir::separator() + entries[0]);
593 for (int i=1; i< entries.size(); ++i)
594 m_MediaObject.enqueue(fileName + QDir::separator() + entries[i]);
595 }
596 } else {
597 setFile(fileName);
598 for (int i=1; i<urls.size(); i++)
599 m_MediaObject.enqueue(Phonon::MediaSource(urls[i].toLocalFile()));
600 }
601 }
602 }
603 forwardButton->setEnabled(m_MediaObject.queue().size() > 0);
604 m_MediaObject.play();
605}
606
607void MediaPlayer::dropEvent(QDropEvent *e)
608{
609 if (e->mimeData()->hasUrls() && e->proposedAction() != Qt::LinkAction) {
610 e->acceptProposedAction();
611 handleDrop(e);
612 } else {
613 e->ignore();
614 }
615}
616
617void MediaPlayer::dragEnterEvent(QDragEnterEvent *e)
618{
619 dragMoveEvent(e);
620}
621
622void MediaPlayer::dragMoveEvent(QDragMoveEvent *e)
623{
624 if (e->mimeData()->hasUrls()) {
625 if (e->proposedAction() == Qt::CopyAction || e->proposedAction() == Qt::MoveAction){
626 e->acceptProposedAction();
627 }
628 }
629}
630
631void MediaPlayer::playPause()
632{
633 if (m_MediaObject.state() == Phonon::PlayingState)
634 m_MediaObject.pause();
635 else {
636 if (m_MediaObject.currentTime() == m_MediaObject.totalTime())
637 m_MediaObject.seek(0);
638 m_MediaObject.play();
639 }
640}
641
642void MediaPlayer::setFile(const QString &fileName)
643{
644 setWindowTitle(fileName.right(fileName.length() - fileName.lastIndexOf('/') - 1));
645 m_MediaObject.setCurrentSource(Phonon::MediaSource(fileName));
646 m_MediaObject.play();
647}
648
649void MediaPlayer::openFile()
650{
651 QStringList fileNames = QFileDialog::getOpenFileNames(this);
652 m_MediaObject.clearQueue();
653 if (fileNames.size() > 0) {
654 QString fileName = fileNames[0];
655 setFile(fileName);
656 for (int i=1; i<fileNames.size(); i++)
657 m_MediaObject.enqueue(Phonon::MediaSource(fileNames[i]));
658 }
659 forwardButton->setEnabled(m_MediaObject.queue().size() > 0);
660}
661
662void MediaPlayer::bufferStatus(int percent)
663{
664 if (percent == 0 || percent == 100)
665 progressLabel->setText(QString());
666 else {
667 QString str = QString::fromLatin1("(%1%)").arg(percent);
668 progressLabel->setText(str);
669 }
670}
671
672void MediaPlayer::setSaturation(int val)
673{
674 m_videoWidget->setSaturation(val / qreal(SLIDER_RANGE));
675}
676
677void MediaPlayer::setHue(int val)
678{
679 m_videoWidget->setHue(val / qreal(SLIDER_RANGE));
680}
681
682void MediaPlayer::setAspect(int val)
683{
684 m_videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatio(val));
685}
686
687void MediaPlayer::setScale(int val)
688{
689 m_videoWidget->setScaleMode(Phonon::VideoWidget::ScaleMode(val));
690}
691
692void MediaPlayer::setBrightness(int val)
693{
694 m_videoWidget->setBrightness(val / qreal(SLIDER_RANGE));
695}
696
697void MediaPlayer::setContrast(int val)
698{
699 m_videoWidget->setContrast(val / qreal(SLIDER_RANGE));
700}
701
702void MediaPlayer::updateInfo()
703{
704 int maxLength = 30;
705 QString font = "<font color=#ffeeaa>";
706 QString fontmono = "<font family=\"monospace,courier new\" color=#ffeeaa>";
707
708 QMap <QString, QString> metaData = m_MediaObject.metaData();
709 QString trackArtist = metaData.value("ARTIST");
710 if (trackArtist.length() > maxLength)
711 trackArtist = trackArtist.left(maxLength) + "...";
712
713 QString trackTitle = metaData.value("TITLE");
714 int trackBitrate = metaData.value("BITRATE").toInt();
715
716 QString fileName;
717 if (m_MediaObject.currentSource().type() == Phonon::MediaSource::Url) {
718 fileName = m_MediaObject.currentSource().url().toString();
719 } else {
720 fileName = m_MediaObject.currentSource().fileName();
721 fileName = fileName.right(fileName.length() - fileName.lastIndexOf('/') - 1);
722 if (fileName.length() > maxLength)
723 fileName = fileName.left(maxLength) + "...";
724 }
725
726 QString title;
727 if (!trackTitle.isEmpty()) {
728 if (trackTitle.length() > maxLength)
729 trackTitle = trackTitle.left(maxLength) + "...";
730 title = "Title: " + font + trackTitle + "<br></font>";
731 } else if (!fileName.isEmpty()) {
732 if (fileName.length() > maxLength)
733 fileName = fileName.left(maxLength) + "...";
734 title = font + fileName + "</font>";
735 if (m_MediaObject.currentSource().type() == Phonon::MediaSource::Url) {
736 title.prepend("Url: ");
737 } else {
738 title.prepend("File: ");
739 }
740 }
741
742 QString artist;
743 if (!trackArtist.isEmpty())
744 artist = "Artist: " + font + trackArtist + "</font>";
745
746 QString bitrate;
747 if (trackBitrate != 0)
748 bitrate = "<br>Bitrate: " + font + QString::number(trackBitrate/1000) + "kbit</font>";
749
750 info->setText(title + artist + bitrate);
751}
752
753void MediaPlayer::updateTime()
754{
755 long len = m_MediaObject.totalTime();
756 long pos = m_MediaObject.currentTime();
757 QString timeString;
758 if (pos || len)
759 {
760 int sec = pos/1000;
761 int min = sec/60;
762 int hour = min/60;
763 int msec = pos;
764
765 QTime playTime(hour%60, min%60, sec%60, msec%1000);
766 sec = len / 1000;
767 min = sec / 60;
768 hour = min / 60;
769 msec = len;
770
771 QTime stopTime(hour%60, min%60, sec%60, msec%1000);
772 QString timeFormat = "m:ss";
773 if (hour > 0)
774 timeFormat = "h:mm:ss";
775 timeString = playTime.toString(timeFormat);
776 if (len)
777 timeString += " / " + stopTime.toString(timeFormat);
778 }
779 timeLabel->setText(timeString);
780}
781
782void MediaPlayer::rewind()
783{
784 m_MediaObject.seek(0);
785}
786
787void MediaPlayer::forward()
788{
789 QList<Phonon::MediaSource> queue = m_MediaObject.queue();
790 if (queue.size() > 0) {
791 m_MediaObject.setCurrentSource(queue[0]);
792 forwardButton->setEnabled(queue.size() > 1);
793 m_MediaObject.play();
794 }
795}
796
797void MediaPlayer::openUrl()
798{
799 QSettings settings;
800 settings.beginGroup(QLatin1String("BrowserMainWindow"));
801 QString sourceURL = settings.value("location").toString();
802 bool ok = false;
803 sourceURL = QInputDialog::getText(this, tr("Open Location"), tr("Please enter a valid address here:"), QLineEdit::Normal, sourceURL, &ok);
804 if (ok && !sourceURL.isEmpty()) {
805 setWindowTitle(sourceURL.right(sourceURL.length() - sourceURL.lastIndexOf('/') - 1));
806 m_MediaObject.setCurrentSource(Phonon::MediaSource(QUrl::fromEncoded(sourceURL.toUtf8())));
807 m_MediaObject.play();
808 settings.setValue("location", sourceURL);
809 }
810}
811
812void MediaPlayer::finished()
813{
814}
815
816void MediaPlayer::showContextMenu(const QPoint &p)
817{
818 fileMenu->popup(m_videoWidget->isFullScreen() ? p : mapToGlobal(p));
819}
820
821void MediaPlayer::scaleChanged(QAction *act)
822{
823 if (act->text() == tr("Scale and crop"))
824 m_videoWidget->setScaleMode(Phonon::VideoWidget::ScaleAndCrop);
825 else
826 m_videoWidget->setScaleMode(Phonon::VideoWidget::FitInView);
827}
828
829void MediaPlayer::aspectChanged(QAction *act)
830{
831 if (act->text() == tr("16/9"))
832 m_videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatio16_9);
833 else if (act->text() == tr("Scale"))
834 m_videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatioWidget);
835 else if (act->text() == tr("4/3"))
836 m_videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatio4_3);
837 else
838 m_videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);
839}
840
Note: See TracBrowser for help on using the repository browser.