slideshow.cpp Example File
slideshow/slideshow.cpp
#include "slideshow.h"
#include <qmediaservice.h>
#include <qmediaplaylist.h>
#include <qvideowidget.h>
#include <QtGui>
SlideShow::SlideShow(QWidget *parent)
: QWidget(parent)
, imageViewer(0)
, playlist(0)
, imageLabel(0)
, playButton(0)
, stopButton(0)
{
imageViewer = new QMediaImageViewer(this);
connect(imageViewer, SIGNAL(stateChanged(QMediaImageViewer::State)),
this, SLOT(stateChanged(QMediaImageViewer::State)));
playlist = new QMediaPlaylist(imageViewer);
QVideoWidget *videoWidget = new QVideoWidget(imageViewer);
QMenu *openMenu = new QMenu(this);
openMenu->addAction(tr("Directory..."), this, SLOT(openDirectory()));
openMenu->addAction(tr("Playlist..."), this, SLOT(openPlaylist()));
openMenu->addAction(tr("Location..."), this, SLOT(openLocation()));
QToolButton *openButton = new QToolButton;
openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
openButton->setMenu(openMenu);
openButton->setPopupMode(QToolButton::InstantPopup);
playButton = new QToolButton;
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
stopButton = new QToolButton;
stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
stopButton->setEnabled(false);
connect(stopButton, SIGNAL(clicked()), imageViewer, SLOT(stop()));
QAbstractButton *nextButton = new QToolButton;
nextButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipForward));
connect(nextButton, SIGNAL(clicked()), playlist, SLOT(next()));
QAbstractButton *previousButton = new QToolButton;
previousButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
connect(previousButton, SIGNAL(clicked()), playlist, SLOT(previous()));
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(openButton);
controlLayout->addStretch(1);
controlLayout->addWidget(previousButton);
controlLayout->addWidget(stopButton);
controlLayout->addWidget(playButton);
controlLayout->addWidget(nextButton);
controlLayout->addStretch(1);
QBoxLayout *layout = new QVBoxLayout;
layout->addWidget(videoWidget, Qt::AlignCenter);
layout->addLayout(controlLayout);
setLayout(layout);
}
void SlideShow::openPlaylist()
{
QString path = QFileDialog::getOpenFileName(this);
if (!path.isEmpty()) {
#ifndef Q_OS_WIN
playlist->load(QUrl(QLatin1String("file:
#else
playlist->load(QUrl(QLatin1String("file:
#endif
}
}
void SlideShow::openDirectory()
{
QString path = QFileDialog::getExistingDirectory(this);
if (!path.isEmpty()) {
playlist->clear();
QDir dir(path);
foreach (const QString &fileName, dir.entryList(QDir::Files)) {
QString absolutePath = dir.absoluteFilePath(fileName);
#ifndef Q_OS_WIN
playlist->appendItem(QUrl(QLatin1String("file:
#else
playlist->appendItem(QUrl(QLatin1String("file:
#endif
}
}
}
void SlideShow::openLocation()
{
QLineEdit *urlEdit = new QLineEdit;
QDialogButtonBox *buttons = new QDialogButtonBox(
QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QFormLayout *layout = new QFormLayout;
layout->addRow(tr("Location"), urlEdit);
layout->addWidget(buttons);
QDialog dialog(this);
dialog.setLayout(layout);
connect(urlEdit, SIGNAL(returnPressed()), &dialog, SLOT(accept()));
connect(buttons, SIGNAL(accepted()), &dialog, SLOT(accept()));
connect(buttons, SIGNAL(rejected()), &dialog, SLOT(reject()));
if (dialog.exec() == QDialog::Accepted) {
QUrl url(urlEdit->text());
if (url.isValid())
playlist->load(url);
}
}
void SlideShow::play()
{
switch (imageViewer->state()) {
case QMediaImageViewer::StoppedState:
case QMediaImageViewer::PausedState:
imageViewer->play();
break;
case QMediaImageViewer::PlayingState:
imageViewer->pause();
break;
}
}
void SlideShow::stateChanged(QMediaImageViewer::State state)
{
switch (state) {
case QMediaImageViewer::StoppedState:
stopButton->setEnabled(false);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
break;
case QMediaImageViewer::PlayingState:
stopButton->setEnabled(true);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
break;
case QMediaImageViewer::PausedState:
stopButton->setEnabled(true);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
break;
}
}