qtmobility/examples/slideshow/slideshow.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
--- a/qtmobility/examples/slideshow/slideshow.cpp	Fri Apr 16 15:51:22 2010 +0300
+++ b/qtmobility/examples/slideshow/slideshow.cpp	Mon May 03 13:18:40 2010 +0300
@@ -51,25 +51,40 @@
     : QWidget(parent)
     , imageViewer(0)
     , playlist(0)
-    , imageLabel(0)
+    , statusLabel(0)
+    , countdownLabel(0)
     , playButton(0)
     , stopButton(0)
+    , viewerLayout(0)
 {
     imageViewer = new QMediaImageViewer(this);
 
     connect(imageViewer, SIGNAL(stateChanged(QMediaImageViewer::State)),
             this, SLOT(stateChanged(QMediaImageViewer::State)));
+    connect(imageViewer, SIGNAL(mediaStatusChanged(QMediaImageViewer::MediaStatus)),
+            this, SLOT(statusChanged(QMediaImageViewer::MediaStatus)));
+    connect(imageViewer, SIGNAL(elapsedTimeChanged(int)), this, SLOT(elapsedTimeChanged(int)));
 
     playlist = new QMediaPlaylist;
     playlist->setMediaObject(imageViewer);
 
+    connect(playlist, SIGNAL(loaded()), this, SLOT(playlistLoaded()));
+    connect(playlist, SIGNAL(loadFailed()), this, SLOT(playlistLoadFailed()));
+
     QVideoWidget *videoWidget = new QVideoWidget;
     videoWidget->setMediaObject(imageViewer);
 
+    statusLabel = new QLabel(tr("%1 Images").arg(0));
+    statusLabel->setAlignment(Qt::AlignCenter);
+
+    viewerLayout = new QStackedLayout;
+    viewerLayout->setStackingMode(QStackedLayout::StackAll);
+    viewerLayout->addWidget(videoWidget);
+    viewerLayout->addWidget(statusLabel);
+
     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));
@@ -78,8 +93,10 @@
 
     playButton = new QToolButton;
     playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
+    playButton->setEnabled(false);
 
     connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
+    connect(this, SIGNAL(enableButtons(bool)), playButton, SLOT(setEnabled(bool)));
 
     stopButton = new QToolButton;
     stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
@@ -89,13 +106,19 @@
 
     QAbstractButton *nextButton = new QToolButton;
     nextButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipForward));
+    nextButton->setEnabled(false);
 
     connect(nextButton, SIGNAL(clicked()), playlist, SLOT(next()));
+    connect(this, SIGNAL(enableButtons(bool)), nextButton, SLOT(setEnabled(bool)));
 
     QAbstractButton *previousButton = new QToolButton;
     previousButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
+    previousButton->setEnabled(false);
 
     connect(previousButton, SIGNAL(clicked()), playlist, SLOT(previous()));
+    connect(this, SIGNAL(enableButtons(bool)), previousButton, SLOT(setEnabled(bool)));
+
+    countdownLabel = new QLabel;
 
     QBoxLayout *controlLayout = new QHBoxLayout;
     controlLayout->setMargin(0);
@@ -106,9 +129,10 @@
     controlLayout->addWidget(playButton);
     controlLayout->addWidget(nextButton);
     controlLayout->addStretch(1);
+    controlLayout->addWidget(countdownLabel);
 
     QBoxLayout *layout = new QVBoxLayout;
-    layout->addWidget(videoWidget, Qt::AlignCenter);
+    layout->addLayout(viewerLayout);
     layout->addLayout(controlLayout);
 
     setLayout(layout);
@@ -120,11 +144,8 @@
     QString path = QFileDialog::getOpenFileName(this);
 
     if (!path.isEmpty()) {
-#ifndef Q_OS_WIN
-        playlist->load(QUrl(QLatin1String("file://") + path));
-#else
-        playlist->load(QUrl(QLatin1String("file:///") + path));
-#endif
+        playlist->clear();
+        playlist->load(QUrl::fromLocalFile(path));
     }
 }
 
@@ -137,40 +158,12 @@
 
         QDir dir(path);
 
-        foreach (const QString &fileName, dir.entryList(QDir::Files)) {
-            QString absolutePath = dir.absoluteFilePath(fileName);
-#ifndef Q_OS_WIN
-            playlist->addMedia(QUrl(QLatin1String("file://") + absolutePath));
-#else
-            playlist->addMedia(QUrl(QLatin1String("file:///") + absolutePath));
-#endif
-        }
-    }
-}
-
-void SlideShow::openLocation()
-{
-    QLineEdit *urlEdit = new QLineEdit;
+        foreach (const QString &fileName, dir.entryList(QDir::Files))
+            playlist->addMedia(QUrl::fromLocalFile(dir.absoluteFilePath(fileName)));
 
-    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);
+        statusChanged(imageViewer->mediaStatus());
 
-    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);
+        emit enableButtons(playlist->mediaCount() > 0);
     }
 }
 
@@ -204,3 +197,57 @@
         break;
     }
 }
+
+void SlideShow::statusChanged(QMediaImageViewer::MediaStatus status)
+{
+    switch (status) {
+    case QMediaImageViewer::NoMedia:
+        statusLabel->setText(tr("%1 Images").arg(playlist->mediaCount()));
+        viewerLayout->setCurrentIndex(1);
+        break;
+    case QMediaImageViewer::LoadingMedia:
+        statusLabel->setText(tr("Image %1 of %2\nLoading...")
+                .arg(playlist->currentIndex())
+                .arg(playlist->mediaCount()));
+        viewerLayout->setCurrentIndex(1);
+        break;
+    case QMediaImageViewer::LoadedMedia:
+        statusLabel->setText(tr("Image %1 of %2")
+                .arg(playlist->currentIndex())
+                .arg(playlist->mediaCount()));
+        viewerLayout->setCurrentIndex(0);
+        break;
+    case QMediaImageViewer::InvalidMedia:
+        statusLabel->setText(tr("Image %1 of %2\nInvalid")
+                .arg(playlist->currentIndex())
+                .arg(playlist->mediaCount()));
+        viewerLayout->setCurrentIndex(1);
+        break;
+    default:
+        break;
+    }
+}
+
+void SlideShow::playlistLoaded()
+{
+    statusChanged(imageViewer->mediaStatus());
+
+    emit enableButtons(playlist->mediaCount() > 0);
+}
+
+void SlideShow::playlistLoadFailed()
+{
+    statusLabel->setText(playlist->errorString());
+    viewerLayout->setCurrentIndex(1);
+
+    emit enableButtons(false);
+}
+
+void SlideShow::elapsedTimeChanged(int time)
+{
+    const int remaining = (imageViewer->timeout() - time) / 1000;
+
+    countdownLabel->setText(tr("%1:%2")
+            .arg(remaining / 60, 2, 10, QLatin1Char('0'))
+            .arg(remaining % 60, 2, 10, QLatin1Char('0')));
+}