| 1 | /****************************************************************************
 | 
|---|
| 2 | **
 | 
|---|
| 3 | ** Copyright (C) 2011 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 examples of the Qt Toolkit.
 | 
|---|
| 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 <QtGui>
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "movieplayer.h"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | MoviePlayer::MoviePlayer(QWidget *parent)
 | 
|---|
| 46 |     : QWidget(parent)
 | 
|---|
| 47 | {
 | 
|---|
| 48 |     movie = new QMovie(this);
 | 
|---|
| 49 |     movie->setCacheMode(QMovie::CacheAll);
 | 
|---|
| 50 | 
 | 
|---|
| 51 |     movieLabel = new QLabel(tr("No movie loaded"));
 | 
|---|
| 52 |     movieLabel->setAlignment(Qt::AlignCenter);
 | 
|---|
| 53 |     movieLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
 | 
|---|
| 54 |     movieLabel->setBackgroundRole(QPalette::Dark);
 | 
|---|
| 55 |     movieLabel->setAutoFillBackground(true);
 | 
|---|
| 56 | 
 | 
|---|
| 57 |     currentMovieDirectory = "movies";
 | 
|---|
| 58 | 
 | 
|---|
| 59 |     createControls();
 | 
|---|
| 60 |     createButtons();
 | 
|---|
| 61 | 
 | 
|---|
| 62 |     connect(movie, SIGNAL(frameChanged(int)), this, SLOT(updateFrameSlider()));
 | 
|---|
| 63 |     connect(movie, SIGNAL(stateChanged(QMovie::MovieState)),
 | 
|---|
| 64 |             this, SLOT(updateButtons()));
 | 
|---|
| 65 |     connect(fitCheckBox, SIGNAL(clicked()), this, SLOT(fitToWindow()));
 | 
|---|
| 66 |     connect(frameSlider, SIGNAL(valueChanged(int)), this, SLOT(goToFrame(int)));
 | 
|---|
| 67 |     connect(speedSpinBox, SIGNAL(valueChanged(int)),
 | 
|---|
| 68 |             movie, SLOT(setSpeed(int)));
 | 
|---|
| 69 | 
 | 
|---|
| 70 |     mainLayout = new QVBoxLayout;
 | 
|---|
| 71 |     mainLayout->addWidget(movieLabel);
 | 
|---|
| 72 |     mainLayout->addLayout(controlsLayout);
 | 
|---|
| 73 |     mainLayout->addLayout(buttonsLayout);
 | 
|---|
| 74 |     setLayout(mainLayout);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     updateFrameSlider();
 | 
|---|
| 77 |     updateButtons();
 | 
|---|
| 78 | 
 | 
|---|
| 79 |     setWindowTitle(tr("Movie Player"));
 | 
|---|
| 80 |     resize(400, 400);
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | void MoviePlayer::open()
 | 
|---|
| 84 | {
 | 
|---|
| 85 |     QString fileName = QFileDialog::getOpenFileName(this, tr("Open a Movie"),
 | 
|---|
| 86 |                                currentMovieDirectory);
 | 
|---|
| 87 |     if (!fileName.isEmpty())
 | 
|---|
| 88 |         openFile(fileName);
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | void MoviePlayer::openFile(const QString &fileName)
 | 
|---|
| 92 | {
 | 
|---|
| 93 |     currentMovieDirectory = QFileInfo(fileName).path();
 | 
|---|
| 94 | 
 | 
|---|
| 95 |     movie->stop();
 | 
|---|
| 96 |     movieLabel->setMovie(movie);
 | 
|---|
| 97 |     movie->setFileName(fileName);
 | 
|---|
| 98 |     movie->start();
 | 
|---|
| 99 | 
 | 
|---|
| 100 |     updateFrameSlider();
 | 
|---|
| 101 |     updateButtons();
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | void MoviePlayer::goToFrame(int frame)
 | 
|---|
| 105 | {
 | 
|---|
| 106 |     movie->jumpToFrame(frame);
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | void MoviePlayer::fitToWindow()
 | 
|---|
| 110 | {
 | 
|---|
| 111 |     movieLabel->setScaledContents(fitCheckBox->isChecked());
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | void MoviePlayer::updateFrameSlider()
 | 
|---|
| 115 | {
 | 
|---|
| 116 |     bool hasFrames = (movie->currentFrameNumber() >= 0);
 | 
|---|
| 117 | 
 | 
|---|
| 118 |     if (hasFrames) {
 | 
|---|
| 119 |         if (movie->frameCount() > 0) {
 | 
|---|
| 120 |             frameSlider->setMaximum(movie->frameCount() - 1);
 | 
|---|
| 121 |         } else {
 | 
|---|
| 122 |             if (movie->currentFrameNumber() > frameSlider->maximum())
 | 
|---|
| 123 |                 frameSlider->setMaximum(movie->currentFrameNumber());
 | 
|---|
| 124 |         }
 | 
|---|
| 125 |         frameSlider->setValue(movie->currentFrameNumber());
 | 
|---|
| 126 |     } else {
 | 
|---|
| 127 |         frameSlider->setMaximum(0);
 | 
|---|
| 128 |     }
 | 
|---|
| 129 |     frameLabel->setEnabled(hasFrames);
 | 
|---|
| 130 |     frameSlider->setEnabled(hasFrames);
 | 
|---|
| 131 | }
 | 
|---|
| 132 | 
 | 
|---|
| 133 | void MoviePlayer::updateButtons()
 | 
|---|
| 134 | {
 | 
|---|
| 135 |     playButton->setEnabled(movie->isValid() && movie->frameCount() != 1
 | 
|---|
| 136 |                            && movie->state() == QMovie::NotRunning);
 | 
|---|
| 137 |     pauseButton->setEnabled(movie->state() != QMovie::NotRunning);
 | 
|---|
| 138 |     pauseButton->setChecked(movie->state() == QMovie::Paused);
 | 
|---|
| 139 |     stopButton->setEnabled(movie->state() != QMovie::NotRunning);
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | void MoviePlayer::createControls()
 | 
|---|
| 143 | {
 | 
|---|
| 144 |     fitCheckBox = new QCheckBox(tr("Fit to Window"));
 | 
|---|
| 145 | 
 | 
|---|
| 146 |     frameLabel = new QLabel(tr("Current frame:"));
 | 
|---|
| 147 | 
 | 
|---|
| 148 |     frameSlider = new QSlider(Qt::Horizontal);
 | 
|---|
| 149 |     frameSlider->setTickPosition(QSlider::TicksBelow);
 | 
|---|
| 150 |     frameSlider->setTickInterval(10);
 | 
|---|
| 151 | 
 | 
|---|
| 152 |     speedLabel = new QLabel(tr("Speed:"));
 | 
|---|
| 153 | 
 | 
|---|
| 154 |     speedSpinBox = new QSpinBox;
 | 
|---|
| 155 |     speedSpinBox->setRange(1, 9999);
 | 
|---|
| 156 |     speedSpinBox->setValue(100);
 | 
|---|
| 157 |     speedSpinBox->setSuffix(tr("%"));
 | 
|---|
| 158 | 
 | 
|---|
| 159 |     controlsLayout = new QGridLayout;
 | 
|---|
| 160 |     controlsLayout->addWidget(fitCheckBox, 0, 0, 1, 2);
 | 
|---|
| 161 |     controlsLayout->addWidget(frameLabel, 1, 0);
 | 
|---|
| 162 |     controlsLayout->addWidget(frameSlider, 1, 1, 1, 2);
 | 
|---|
| 163 |     controlsLayout->addWidget(speedLabel, 2, 0);
 | 
|---|
| 164 |     controlsLayout->addWidget(speedSpinBox, 2, 1);
 | 
|---|
| 165 | }
 | 
|---|
| 166 | 
 | 
|---|
| 167 | void MoviePlayer::createButtons()
 | 
|---|
| 168 | {
 | 
|---|
| 169 |     QSize iconSize(36, 36);
 | 
|---|
| 170 | 
 | 
|---|
| 171 |     openButton = new QToolButton;
 | 
|---|
| 172 |     openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
 | 
|---|
| 173 |     openButton->setIconSize(iconSize);
 | 
|---|
| 174 |     openButton->setToolTip(tr("Open File"));
 | 
|---|
| 175 |     connect(openButton, SIGNAL(clicked()), this, SLOT(open()));
 | 
|---|
| 176 | 
 | 
|---|
| 177 |     playButton = new QToolButton;
 | 
|---|
| 178 |     playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
 | 
|---|
| 179 |     playButton->setIconSize(iconSize);
 | 
|---|
| 180 |     playButton->setToolTip(tr("Play"));
 | 
|---|
| 181 |     connect(playButton, SIGNAL(clicked()), movie, SLOT(start()));
 | 
|---|
| 182 | 
 | 
|---|
| 183 |     pauseButton = new QToolButton;
 | 
|---|
| 184 |     pauseButton->setCheckable(true);
 | 
|---|
| 185 |     pauseButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
 | 
|---|
| 186 |     pauseButton->setIconSize(iconSize);
 | 
|---|
| 187 |     pauseButton->setToolTip(tr("Pause"));
 | 
|---|
| 188 |     connect(pauseButton, SIGNAL(clicked(bool)), movie, SLOT(setPaused(bool)));
 | 
|---|
| 189 | 
 | 
|---|
| 190 |     stopButton = new QToolButton;
 | 
|---|
| 191 |     stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
 | 
|---|
| 192 |     stopButton->setIconSize(iconSize);
 | 
|---|
| 193 |     stopButton->setToolTip(tr("Stop"));
 | 
|---|
| 194 |     connect(stopButton, SIGNAL(clicked()), movie, SLOT(stop()));
 | 
|---|
| 195 | 
 | 
|---|
| 196 |     quitButton = new QToolButton;
 | 
|---|
| 197 |     quitButton->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
 | 
|---|
| 198 |     quitButton->setIconSize(iconSize);
 | 
|---|
| 199 |     quitButton->setToolTip(tr("Quit"));
 | 
|---|
| 200 |     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
 | 
|---|
| 201 | 
 | 
|---|
| 202 |     buttonsLayout = new QHBoxLayout;
 | 
|---|
| 203 |     buttonsLayout->addStretch();
 | 
|---|
| 204 |     buttonsLayout->addWidget(openButton);
 | 
|---|
| 205 |     buttonsLayout->addWidget(playButton);
 | 
|---|
| 206 |     buttonsLayout->addWidget(pauseButton);
 | 
|---|
| 207 |     buttonsLayout->addWidget(stopButton);
 | 
|---|
| 208 |     buttonsLayout->addWidget(quitButton);
 | 
|---|
| 209 |     buttonsLayout->addStretch();
 | 
|---|
| 210 | }
 | 
|---|