qtmobility/examples/slideshow/slideshow.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    49 
    49 
    50 SlideShow::SlideShow(QWidget *parent)
    50 SlideShow::SlideShow(QWidget *parent)
    51     : QWidget(parent)
    51     : QWidget(parent)
    52     , imageViewer(0)
    52     , imageViewer(0)
    53     , playlist(0)
    53     , playlist(0)
    54     , imageLabel(0)
    54     , statusLabel(0)
       
    55     , countdownLabel(0)
    55     , playButton(0)
    56     , playButton(0)
    56     , stopButton(0)
    57     , stopButton(0)
       
    58     , viewerLayout(0)
    57 {
    59 {
    58     imageViewer = new QMediaImageViewer(this);
    60     imageViewer = new QMediaImageViewer(this);
    59 
    61 
    60     connect(imageViewer, SIGNAL(stateChanged(QMediaImageViewer::State)),
    62     connect(imageViewer, SIGNAL(stateChanged(QMediaImageViewer::State)),
    61             this, SLOT(stateChanged(QMediaImageViewer::State)));
    63             this, SLOT(stateChanged(QMediaImageViewer::State)));
       
    64     connect(imageViewer, SIGNAL(mediaStatusChanged(QMediaImageViewer::MediaStatus)),
       
    65             this, SLOT(statusChanged(QMediaImageViewer::MediaStatus)));
       
    66     connect(imageViewer, SIGNAL(elapsedTimeChanged(int)), this, SLOT(elapsedTimeChanged(int)));
    62 
    67 
    63     playlist = new QMediaPlaylist;
    68     playlist = new QMediaPlaylist;
    64     playlist->setMediaObject(imageViewer);
    69     playlist->setMediaObject(imageViewer);
    65 
    70 
       
    71     connect(playlist, SIGNAL(loaded()), this, SLOT(playlistLoaded()));
       
    72     connect(playlist, SIGNAL(loadFailed()), this, SLOT(playlistLoadFailed()));
       
    73 
    66     QVideoWidget *videoWidget = new QVideoWidget;
    74     QVideoWidget *videoWidget = new QVideoWidget;
    67     videoWidget->setMediaObject(imageViewer);
    75     videoWidget->setMediaObject(imageViewer);
       
    76 
       
    77     statusLabel = new QLabel(tr("%1 Images").arg(0));
       
    78     statusLabel->setAlignment(Qt::AlignCenter);
       
    79 
       
    80     viewerLayout = new QStackedLayout;
       
    81     viewerLayout->setStackingMode(QStackedLayout::StackAll);
       
    82     viewerLayout->addWidget(videoWidget);
       
    83     viewerLayout->addWidget(statusLabel);
    68 
    84 
    69     QMenu *openMenu = new QMenu(this);
    85     QMenu *openMenu = new QMenu(this);
    70     openMenu->addAction(tr("Directory..."), this, SLOT(openDirectory()));
    86     openMenu->addAction(tr("Directory..."), this, SLOT(openDirectory()));
    71     openMenu->addAction(tr("Playlist..."), this, SLOT(openPlaylist()));
    87     openMenu->addAction(tr("Playlist..."), this, SLOT(openPlaylist()));
    72     openMenu->addAction(tr("Location..."), this, SLOT(openLocation()));
       
    73 
    88 
    74     QToolButton *openButton = new QToolButton;
    89     QToolButton *openButton = new QToolButton;
    75     openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
    90     openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
    76     openButton->setMenu(openMenu);
    91     openButton->setMenu(openMenu);
    77     openButton->setPopupMode(QToolButton::InstantPopup);
    92     openButton->setPopupMode(QToolButton::InstantPopup);
    78 
    93 
    79     playButton = new QToolButton;
    94     playButton = new QToolButton;
    80     playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    95     playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
    96     playButton->setEnabled(false);
    81 
    97 
    82     connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
    98     connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
       
    99     connect(this, SIGNAL(enableButtons(bool)), playButton, SLOT(setEnabled(bool)));
    83 
   100 
    84     stopButton = new QToolButton;
   101     stopButton = new QToolButton;
    85     stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
   102     stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
    86     stopButton->setEnabled(false);
   103     stopButton->setEnabled(false);
    87 
   104 
    88     connect(stopButton, SIGNAL(clicked()), imageViewer, SLOT(stop()));
   105     connect(stopButton, SIGNAL(clicked()), imageViewer, SLOT(stop()));
    89 
   106 
    90     QAbstractButton *nextButton = new QToolButton;
   107     QAbstractButton *nextButton = new QToolButton;
    91     nextButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipForward));
   108     nextButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipForward));
       
   109     nextButton->setEnabled(false);
    92 
   110 
    93     connect(nextButton, SIGNAL(clicked()), playlist, SLOT(next()));
   111     connect(nextButton, SIGNAL(clicked()), playlist, SLOT(next()));
       
   112     connect(this, SIGNAL(enableButtons(bool)), nextButton, SLOT(setEnabled(bool)));
    94 
   113 
    95     QAbstractButton *previousButton = new QToolButton;
   114     QAbstractButton *previousButton = new QToolButton;
    96     previousButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
   115     previousButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
       
   116     previousButton->setEnabled(false);
    97 
   117 
    98     connect(previousButton, SIGNAL(clicked()), playlist, SLOT(previous()));
   118     connect(previousButton, SIGNAL(clicked()), playlist, SLOT(previous()));
       
   119     connect(this, SIGNAL(enableButtons(bool)), previousButton, SLOT(setEnabled(bool)));
       
   120 
       
   121     countdownLabel = new QLabel;
    99 
   122 
   100     QBoxLayout *controlLayout = new QHBoxLayout;
   123     QBoxLayout *controlLayout = new QHBoxLayout;
   101     controlLayout->setMargin(0);
   124     controlLayout->setMargin(0);
   102     controlLayout->addWidget(openButton);
   125     controlLayout->addWidget(openButton);
   103     controlLayout->addStretch(1);
   126     controlLayout->addStretch(1);
   104     controlLayout->addWidget(previousButton);
   127     controlLayout->addWidget(previousButton);
   105     controlLayout->addWidget(stopButton);
   128     controlLayout->addWidget(stopButton);
   106     controlLayout->addWidget(playButton);
   129     controlLayout->addWidget(playButton);
   107     controlLayout->addWidget(nextButton);
   130     controlLayout->addWidget(nextButton);
   108     controlLayout->addStretch(1);
   131     controlLayout->addStretch(1);
       
   132     controlLayout->addWidget(countdownLabel);
   109 
   133 
   110     QBoxLayout *layout = new QVBoxLayout;
   134     QBoxLayout *layout = new QVBoxLayout;
   111     layout->addWidget(videoWidget, Qt::AlignCenter);
   135     layout->addLayout(viewerLayout);
   112     layout->addLayout(controlLayout);
   136     layout->addLayout(controlLayout);
   113 
   137 
   114     setLayout(layout);
   138     setLayout(layout);
   115 
   139 
   116 }
   140 }
   117 
   141 
   118 void SlideShow::openPlaylist()
   142 void SlideShow::openPlaylist()
   119 {
   143 {
   120     QString path = QFileDialog::getOpenFileName(this);
   144     QString path = QFileDialog::getOpenFileName(this);
   121 
       
   122     if (!path.isEmpty()) {
       
   123 #ifndef Q_OS_WIN
       
   124         playlist->load(QUrl(QLatin1String("file://") + path));
       
   125 #else
       
   126         playlist->load(QUrl(QLatin1String("file:///") + path));
       
   127 #endif
       
   128     }
       
   129 }
       
   130 
       
   131 void SlideShow::openDirectory()
       
   132 {
       
   133     QString path = QFileDialog::getExistingDirectory(this);
       
   134 
   145 
   135     if (!path.isEmpty()) {
   146     if (!path.isEmpty()) {
   136         playlist->clear();
   147         playlist->clear();
       
   148         playlist->load(QUrl::fromLocalFile(path));
       
   149     }
       
   150 }
       
   151 
       
   152 void SlideShow::openDirectory()
       
   153 {
       
   154     QString path = QFileDialog::getExistingDirectory(this);
       
   155 
       
   156     if (!path.isEmpty()) {
       
   157         playlist->clear();
   137 
   158 
   138         QDir dir(path);
   159         QDir dir(path);
   139 
   160 
   140         foreach (const QString &fileName, dir.entryList(QDir::Files)) {
   161         foreach (const QString &fileName, dir.entryList(QDir::Files))
   141             QString absolutePath = dir.absoluteFilePath(fileName);
   162             playlist->addMedia(QUrl::fromLocalFile(dir.absoluteFilePath(fileName)));
   142 #ifndef Q_OS_WIN
   163 
   143             playlist->addMedia(QUrl(QLatin1String("file://") + absolutePath));
   164         statusChanged(imageViewer->mediaStatus());
   144 #else
   165 
   145             playlist->addMedia(QUrl(QLatin1String("file:///") + absolutePath));
   166         emit enableButtons(playlist->mediaCount() > 0);
   146 #endif
       
   147         }
       
   148     }
       
   149 }
       
   150 
       
   151 void SlideShow::openLocation()
       
   152 {
       
   153     QLineEdit *urlEdit = new QLineEdit;
       
   154 
       
   155     QDialogButtonBox *buttons = new QDialogButtonBox(
       
   156             QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
       
   157 
       
   158     QFormLayout *layout = new QFormLayout;
       
   159     layout->addRow(tr("Location"), urlEdit);
       
   160     layout->addWidget(buttons);
       
   161 
       
   162     QDialog dialog(this);
       
   163     dialog.setLayout(layout);
       
   164 
       
   165     connect(urlEdit, SIGNAL(returnPressed()), &dialog, SLOT(accept()));
       
   166     connect(buttons, SIGNAL(accepted()), &dialog, SLOT(accept()));
       
   167     connect(buttons, SIGNAL(rejected()), &dialog, SLOT(reject()));
       
   168 
       
   169     if (dialog.exec() == QDialog::Accepted) {
       
   170         QUrl url(urlEdit->text());
       
   171 
       
   172         if (url.isValid())
       
   173             playlist->load(url);
       
   174     }
   167     }
   175 }
   168 }
   176 
   169 
   177 void SlideShow::play()
   170 void SlideShow::play()
   178 {
   171 {
   202         stopButton->setEnabled(true);
   195         stopButton->setEnabled(true);
   203         playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
   196         playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
   204         break;
   197         break;
   205     }
   198     }
   206 }
   199 }
       
   200 
       
   201 void SlideShow::statusChanged(QMediaImageViewer::MediaStatus status)
       
   202 {
       
   203     switch (status) {
       
   204     case QMediaImageViewer::NoMedia:
       
   205         statusLabel->setText(tr("%1 Images").arg(playlist->mediaCount()));
       
   206         viewerLayout->setCurrentIndex(1);
       
   207         break;
       
   208     case QMediaImageViewer::LoadingMedia:
       
   209         statusLabel->setText(tr("Image %1 of %2\nLoading...")
       
   210                 .arg(playlist->currentIndex())
       
   211                 .arg(playlist->mediaCount()));
       
   212         viewerLayout->setCurrentIndex(1);
       
   213         break;
       
   214     case QMediaImageViewer::LoadedMedia:
       
   215         statusLabel->setText(tr("Image %1 of %2")
       
   216                 .arg(playlist->currentIndex())
       
   217                 .arg(playlist->mediaCount()));
       
   218         viewerLayout->setCurrentIndex(0);
       
   219         break;
       
   220     case QMediaImageViewer::InvalidMedia:
       
   221         statusLabel->setText(tr("Image %1 of %2\nInvalid")
       
   222                 .arg(playlist->currentIndex())
       
   223                 .arg(playlist->mediaCount()));
       
   224         viewerLayout->setCurrentIndex(1);
       
   225         break;
       
   226     default:
       
   227         break;
       
   228     }
       
   229 }
       
   230 
       
   231 void SlideShow::playlistLoaded()
       
   232 {
       
   233     statusChanged(imageViewer->mediaStatus());
       
   234 
       
   235     emit enableButtons(playlist->mediaCount() > 0);
       
   236 }
       
   237 
       
   238 void SlideShow::playlistLoadFailed()
       
   239 {
       
   240     statusLabel->setText(playlist->errorString());
       
   241     viewerLayout->setCurrentIndex(1);
       
   242 
       
   243     emit enableButtons(false);
       
   244 }
       
   245 
       
   246 void SlideShow::elapsedTimeChanged(int time)
       
   247 {
       
   248     const int remaining = (imageViewer->timeout() - time) / 1000;
       
   249 
       
   250     countdownLabel->setText(tr("%1:%2")
       
   251             .arg(remaining / 60, 2, 10, QLatin1Char('0'))
       
   252             .arg(remaining % 60, 2, 10, QLatin1Char('0')));
       
   253 }