examples/slideshow/slideshow.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    15 **   * Redistributions of source code must retain the above copyright
       
    16 **     notice, this list of conditions and the following disclaimer.
       
    17 **   * Redistributions in binary form must reproduce the above copyright
       
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
       
    25 **
       
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #include "slideshow.h"
       
    42 
       
    43 #include <qmediaservice.h>
       
    44 #include <qmediaplaylist.h>
       
    45 #include <qvideowidget.h>
       
    46 
       
    47 #include <QtGui>
       
    48 
       
    49 SlideShow::SlideShow(QWidget *parent)
       
    50     : QWidget(parent)
       
    51     , imageViewer(0)
       
    52     , playlist(0)
       
    53     , statusLabel(0)
       
    54     , countdownLabel(0)
       
    55     , playButton(0)
       
    56     , stopButton(0)
       
    57     , viewerLayout(0)
       
    58 {
       
    59     imageViewer = new QMediaImageViewer(this);
       
    60 
       
    61     connect(imageViewer, SIGNAL(stateChanged(QMediaImageViewer::State)),
       
    62             this, SLOT(stateChanged(QMediaImageViewer::State)));
       
    63     connect(imageViewer, SIGNAL(mediaStatusChanged(QMediaImageViewer::MediaStatus)),
       
    64             this, SLOT(statusChanged(QMediaImageViewer::MediaStatus)));
       
    65     connect(imageViewer, SIGNAL(elapsedTimeChanged(int)), this, SLOT(elapsedTimeChanged(int)));
       
    66 
       
    67     playlist = new QMediaPlaylist;
       
    68     imageViewer->bind(playlist);
       
    69 
       
    70     connect(playlist, SIGNAL(loaded()), this, SLOT(playlistLoaded()));
       
    71     connect(playlist, SIGNAL(loadFailed()), this, SLOT(playlistLoadFailed()));
       
    72 
       
    73     connect(playlist, SIGNAL(loaded()), this, SLOT(playlistLoaded()));
       
    74     connect(playlist, SIGNAL(loadFailed()), this, SLOT(playlistLoadFailed()));
       
    75 
       
    76     QVideoWidget *videoWidget = new QVideoWidget;
       
    77     imageViewer->bind(videoWidget);
       
    78 
       
    79     statusLabel = new QLabel(tr("%1 Images").arg(0));
       
    80     statusLabel->setAlignment(Qt::AlignCenter);
       
    81 
       
    82     viewerLayout = new QStackedLayout;
       
    83     viewerLayout->setStackingMode(QStackedLayout::StackAll);
       
    84     viewerLayout->addWidget(videoWidget);
       
    85     viewerLayout->addWidget(statusLabel);
       
    86 
       
    87     statusLabel = new QLabel(tr("%1 Images").arg(0));
       
    88     statusLabel->setAlignment(Qt::AlignCenter);
       
    89 
       
    90     viewerLayout = new QStackedLayout;
       
    91     viewerLayout->setStackingMode(QStackedLayout::StackAll);
       
    92     viewerLayout->addWidget(videoWidget);
       
    93     viewerLayout->addWidget(statusLabel);
       
    94 
       
    95     QMenu *openMenu = new QMenu(this);
       
    96     openMenu->addAction(tr("Directory..."), this, SLOT(openDirectory()));
       
    97     openMenu->addAction(tr("Playlist..."), this, SLOT(openPlaylist()));
       
    98 
       
    99     QToolButton *openButton = new QToolButton;
       
   100     openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
       
   101     openButton->setMenu(openMenu);
       
   102     openButton->setPopupMode(QToolButton::InstantPopup);
       
   103 
       
   104     playButton = new QToolButton;
       
   105     playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
   106     playButton->setEnabled(false);
       
   107 
       
   108     connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
       
   109     connect(this, SIGNAL(enableButtons(bool)), playButton, SLOT(setEnabled(bool)));
       
   110 
       
   111     stopButton = new QToolButton;
       
   112     stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
       
   113     stopButton->setEnabled(false);
       
   114 
       
   115     connect(stopButton, SIGNAL(clicked()), imageViewer, SLOT(stop()));
       
   116 
       
   117     QAbstractButton *nextButton = new QToolButton;
       
   118     nextButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipForward));
       
   119     nextButton->setEnabled(false);
       
   120 
       
   121     connect(nextButton, SIGNAL(clicked()), playlist, SLOT(next()));
       
   122     connect(this, SIGNAL(enableButtons(bool)), nextButton, SLOT(setEnabled(bool)));
       
   123 
       
   124     QAbstractButton *previousButton = new QToolButton;
       
   125     previousButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
       
   126     previousButton->setEnabled(false);
       
   127 
       
   128     connect(previousButton, SIGNAL(clicked()), playlist, SLOT(previous()));
       
   129     connect(this, SIGNAL(enableButtons(bool)), previousButton, SLOT(setEnabled(bool)));
       
   130 
       
   131     countdownLabel = new QLabel;
       
   132 
       
   133     QBoxLayout *controlLayout = new QHBoxLayout;
       
   134     controlLayout->setMargin(0);
       
   135     controlLayout->addWidget(openButton);
       
   136     controlLayout->addStretch(1);
       
   137     controlLayout->addWidget(previousButton);
       
   138     controlLayout->addWidget(stopButton);
       
   139     controlLayout->addWidget(playButton);
       
   140     controlLayout->addWidget(nextButton);
       
   141     controlLayout->addStretch(1);
       
   142     controlLayout->addWidget(countdownLabel);
       
   143 
       
   144     QBoxLayout *layout = new QVBoxLayout;
       
   145     layout->addLayout(viewerLayout);
       
   146     layout->addLayout(controlLayout);
       
   147 
       
   148     setLayout(layout);
       
   149 
       
   150 }
       
   151 
       
   152 void SlideShow::openPlaylist()
       
   153 {
       
   154     QString path = QFileDialog::getOpenFileName(this);
       
   155 
       
   156     if (!path.isEmpty()) {
       
   157         playlist->clear();
       
   158         playlist->load(QUrl::fromLocalFile(path));
       
   159     }
       
   160 }
       
   161 
       
   162 void SlideShow::openDirectory()
       
   163 {
       
   164     QString path = QFileDialog::getExistingDirectory(this);
       
   165 
       
   166     if (!path.isEmpty()) {
       
   167         playlist->clear();
       
   168 
       
   169         QDir dir(path);
       
   170 
       
   171         foreach (const QString &fileName, dir.entryList(QDir::Files))
       
   172             playlist->addMedia(QUrl::fromLocalFile(dir.absoluteFilePath(fileName)));
       
   173 
       
   174         statusChanged(imageViewer->mediaStatus());
       
   175 
       
   176         emit enableButtons(playlist->mediaCount() > 0);
       
   177     }
       
   178 }
       
   179 
       
   180 void SlideShow::play()
       
   181 {
       
   182     switch (imageViewer->state()) {
       
   183     case QMediaImageViewer::StoppedState:
       
   184     case QMediaImageViewer::PausedState:
       
   185         imageViewer->play();
       
   186         break;
       
   187     case QMediaImageViewer::PlayingState:
       
   188         imageViewer->pause();
       
   189         break;
       
   190     }
       
   191 }
       
   192 
       
   193 void SlideShow::stateChanged(QMediaImageViewer::State state)
       
   194 {
       
   195     switch (state) {
       
   196     case QMediaImageViewer::StoppedState:
       
   197         stopButton->setEnabled(false);
       
   198         playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
   199         break;
       
   200     case QMediaImageViewer::PlayingState:
       
   201         stopButton->setEnabled(true);
       
   202         playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
       
   203         break;
       
   204     case QMediaImageViewer::PausedState:
       
   205         stopButton->setEnabled(true);
       
   206         playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
   207         break;
       
   208     }
       
   209 }
       
   210 
       
   211 void SlideShow::statusChanged(QMediaImageViewer::MediaStatus status)
       
   212 {
       
   213     switch (status) {
       
   214     case QMediaImageViewer::NoMedia:
       
   215         statusLabel->setText(tr("%1 Images").arg(playlist->mediaCount()));
       
   216         viewerLayout->setCurrentIndex(1);
       
   217         break;
       
   218     case QMediaImageViewer::LoadingMedia:
       
   219         statusLabel->setText(tr("Image %1 of %2\nLoading...")
       
   220                 .arg(playlist->currentIndex())
       
   221                 .arg(playlist->mediaCount()));
       
   222         viewerLayout->setCurrentIndex(1);
       
   223         break;
       
   224     case QMediaImageViewer::LoadedMedia:
       
   225         statusLabel->setText(tr("Image %1 of %2")
       
   226                 .arg(playlist->currentIndex())
       
   227                 .arg(playlist->mediaCount()));
       
   228         viewerLayout->setCurrentIndex(0);
       
   229         break;
       
   230     case QMediaImageViewer::InvalidMedia:
       
   231         statusLabel->setText(tr("Image %1 of %2\nInvalid")
       
   232                 .arg(playlist->currentIndex())
       
   233                 .arg(playlist->mediaCount()));
       
   234         viewerLayout->setCurrentIndex(1);
       
   235         break;
       
   236     default:
       
   237         break;
       
   238     }
       
   239 }
       
   240 
       
   241 void SlideShow::playlistLoaded()
       
   242 {
       
   243     statusChanged(imageViewer->mediaStatus());
       
   244 
       
   245     emit enableButtons(playlist->mediaCount() > 0);
       
   246 }
       
   247 
       
   248 void SlideShow::playlistLoadFailed()
       
   249 {
       
   250     statusLabel->setText(playlist->errorString());
       
   251     viewerLayout->setCurrentIndex(1);
       
   252 
       
   253     emit enableButtons(false);
       
   254 }
       
   255 
       
   256 void SlideShow::elapsedTimeChanged(int time)
       
   257 {
       
   258     const int remaining = (imageViewer->timeout() - time) / 1000;
       
   259 
       
   260     countdownLabel->setText(tr("%1:%2")
       
   261             .arg(remaining / 60, 2, 10, QLatin1Char('0'))
       
   262             .arg(remaining % 60, 2, 10, QLatin1Char('0')));
       
   263 }