examples/widgets/movie/movieplayer.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "movieplayer.h"
       
    45 
       
    46 MoviePlayer::MoviePlayer(QWidget *parent)
       
    47     : QWidget(parent)
       
    48 {
       
    49     movie = new QMovie(this);
       
    50     movie->setCacheMode(QMovie::CacheAll);
       
    51 
       
    52     movieLabel = new QLabel(tr("No movie loaded"));
       
    53     movieLabel->setAlignment(Qt::AlignCenter);
       
    54     movieLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
       
    55     movieLabel->setBackgroundRole(QPalette::Dark);
       
    56     movieLabel->setAutoFillBackground(true);
       
    57 
       
    58     currentMovieDirectory = "movies";
       
    59 
       
    60     createControls();
       
    61     createButtons();
       
    62 
       
    63     connect(movie, SIGNAL(frameChanged(int)), this, SLOT(updateFrameSlider()));
       
    64     connect(movie, SIGNAL(stateChanged(QMovie::MovieState)),
       
    65             this, SLOT(updateButtons()));
       
    66     connect(fitCheckBox, SIGNAL(clicked()), this, SLOT(fitToWindow()));
       
    67     connect(frameSlider, SIGNAL(valueChanged(int)), this, SLOT(goToFrame(int)));
       
    68     connect(speedSpinBox, SIGNAL(valueChanged(int)),
       
    69             movie, SLOT(setSpeed(int)));
       
    70 
       
    71     mainLayout = new QVBoxLayout;
       
    72     mainLayout->addWidget(movieLabel);
       
    73     mainLayout->addLayout(controlsLayout);
       
    74     mainLayout->addLayout(buttonsLayout);
       
    75     setLayout(mainLayout);
       
    76 
       
    77     updateFrameSlider();
       
    78     updateButtons();
       
    79 
       
    80     setWindowTitle(tr("Movie Player"));
       
    81     resize(400, 400);
       
    82 }
       
    83 
       
    84 void MoviePlayer::open()
       
    85 {
       
    86     QString fileName = QFileDialog::getOpenFileName(this, tr("Open a Movie"),
       
    87                                currentMovieDirectory);
       
    88     if (!fileName.isEmpty())
       
    89         openFile(fileName);
       
    90 }
       
    91 
       
    92 void MoviePlayer::openFile(const QString &fileName)
       
    93 {
       
    94     currentMovieDirectory = QFileInfo(fileName).path();
       
    95 
       
    96     movie->stop();
       
    97     movieLabel->setMovie(movie);
       
    98     movie->setFileName(fileName);
       
    99     movie->start();
       
   100 
       
   101     updateFrameSlider();
       
   102     updateButtons();
       
   103 }
       
   104 
       
   105 void MoviePlayer::goToFrame(int frame)
       
   106 {
       
   107     movie->jumpToFrame(frame);
       
   108 }
       
   109 
       
   110 void MoviePlayer::fitToWindow()
       
   111 {
       
   112     movieLabel->setScaledContents(fitCheckBox->isChecked());
       
   113 }
       
   114 
       
   115 void MoviePlayer::updateFrameSlider()
       
   116 {
       
   117     bool hasFrames = (movie->currentFrameNumber() >= 0);
       
   118 
       
   119     if (hasFrames) {
       
   120         if (movie->frameCount() > 0) {
       
   121             frameSlider->setMaximum(movie->frameCount() - 1);
       
   122         } else {
       
   123             if (movie->currentFrameNumber() > frameSlider->maximum())
       
   124                 frameSlider->setMaximum(movie->currentFrameNumber());
       
   125         }
       
   126         frameSlider->setValue(movie->currentFrameNumber());
       
   127     } else {
       
   128         frameSlider->setMaximum(0);
       
   129     }
       
   130     frameLabel->setEnabled(hasFrames);
       
   131     frameSlider->setEnabled(hasFrames);
       
   132 }
       
   133 
       
   134 void MoviePlayer::updateButtons()
       
   135 {
       
   136     playButton->setEnabled(movie->isValid() && movie->frameCount() != 1
       
   137                            && movie->state() == QMovie::NotRunning);
       
   138     pauseButton->setEnabled(movie->state() != QMovie::NotRunning);
       
   139     pauseButton->setChecked(movie->state() == QMovie::Paused);
       
   140     stopButton->setEnabled(movie->state() != QMovie::NotRunning);
       
   141 }
       
   142 
       
   143 void MoviePlayer::createControls()
       
   144 {
       
   145     fitCheckBox = new QCheckBox(tr("Fit to Window"));
       
   146 
       
   147     frameLabel = new QLabel(tr("Current frame:"));
       
   148 
       
   149     frameSlider = new QSlider(Qt::Horizontal);
       
   150     frameSlider->setTickPosition(QSlider::TicksBelow);
       
   151     frameSlider->setTickInterval(10);
       
   152 
       
   153     speedLabel = new QLabel(tr("Speed:"));
       
   154 
       
   155     speedSpinBox = new QSpinBox;
       
   156     speedSpinBox->setRange(1, 9999);
       
   157     speedSpinBox->setValue(100);
       
   158     speedSpinBox->setSuffix(tr("%"));
       
   159 
       
   160     controlsLayout = new QGridLayout;
       
   161     controlsLayout->addWidget(fitCheckBox, 0, 0, 1, 2);
       
   162     controlsLayout->addWidget(frameLabel, 1, 0);
       
   163     controlsLayout->addWidget(frameSlider, 1, 1, 1, 2);
       
   164     controlsLayout->addWidget(speedLabel, 2, 0);
       
   165     controlsLayout->addWidget(speedSpinBox, 2, 1);
       
   166 }
       
   167 
       
   168 void MoviePlayer::createButtons()
       
   169 {
       
   170     QSize iconSize(36, 36);
       
   171 
       
   172     openButton = new QToolButton;
       
   173     openButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
       
   174     openButton->setIconSize(iconSize);
       
   175     openButton->setToolTip(tr("Open File"));
       
   176     connect(openButton, SIGNAL(clicked()), this, SLOT(open()));
       
   177 
       
   178     playButton = new QToolButton;
       
   179     playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
   180     playButton->setIconSize(iconSize);
       
   181     playButton->setToolTip(tr("Play"));
       
   182     connect(playButton, SIGNAL(clicked()), movie, SLOT(start()));
       
   183 
       
   184     pauseButton = new QToolButton;
       
   185     pauseButton->setCheckable(true);
       
   186     pauseButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
       
   187     pauseButton->setIconSize(iconSize);
       
   188     pauseButton->setToolTip(tr("Pause"));
       
   189     connect(pauseButton, SIGNAL(clicked(bool)), movie, SLOT(setPaused(bool)));
       
   190 
       
   191     stopButton = new QToolButton;
       
   192     stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
       
   193     stopButton->setIconSize(iconSize);
       
   194     stopButton->setToolTip(tr("Stop"));
       
   195     connect(stopButton, SIGNAL(clicked()), movie, SLOT(stop()));
       
   196 
       
   197     quitButton = new QToolButton;
       
   198     quitButton->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
       
   199     quitButton->setIconSize(iconSize);
       
   200     quitButton->setToolTip(tr("Quit"));
       
   201     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
       
   202 
       
   203     buttonsLayout = new QHBoxLayout;
       
   204     buttonsLayout->addStretch();
       
   205     buttonsLayout->addWidget(openButton);
       
   206     buttonsLayout->addWidget(playButton);
       
   207     buttonsLayout->addWidget(pauseButton);
       
   208     buttonsLayout->addWidget(stopButton);
       
   209     buttonsLayout->addWidget(quitButton);
       
   210     buttonsLayout->addStretch();
       
   211 }