examples/player/playercontrols.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 "playercontrols.h"
       
    42 
       
    43 #include <QtGui/qboxlayout.h>
       
    44 #include <QtGui/qslider.h>
       
    45 #include <QtGui/qstyle.h>
       
    46 #include <QtGui/qtoolbutton.h>
       
    47 #include <QtGui/qcombobox.h>
       
    48 
       
    49 PlayerControls::PlayerControls(QWidget *parent)
       
    50     : QWidget(parent)
       
    51     , playerState(QMediaPlayer::StoppedState)
       
    52     , playerMuted(false)
       
    53     , playButton(0)
       
    54     , stopButton(0)
       
    55     , nextButton(0)
       
    56     , previousButton(0)
       
    57     , muteButton(0)
       
    58     , volumeSlider(0)
       
    59     , rateBox(0)
       
    60 {
       
    61     playButton = new QToolButton(this);
       
    62     playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
    63 
       
    64     connect(playButton, SIGNAL(clicked()), this, SLOT(playClicked()));
       
    65 
       
    66     stopButton = new QToolButton(this);
       
    67     stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
       
    68     stopButton->setEnabled(false);
       
    69 
       
    70     connect(stopButton, SIGNAL(clicked()), this, SIGNAL(stop()));
       
    71 
       
    72     nextButton = new QToolButton(this);
       
    73     nextButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipForward));
       
    74 
       
    75     connect(nextButton, SIGNAL(clicked()), this, SIGNAL(next()));
       
    76 
       
    77     previousButton = new QToolButton(this);
       
    78     previousButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
       
    79 
       
    80     connect(previousButton, SIGNAL(clicked()), this, SIGNAL(previous()));
       
    81 
       
    82     muteButton = new QToolButton(this);
       
    83     muteButton->setIcon(style()->standardIcon(QStyle::SP_MediaVolume));
       
    84 
       
    85     connect(muteButton, SIGNAL(clicked()), this, SLOT(muteClicked()));
       
    86 
       
    87     volumeSlider = new QSlider(Qt::Horizontal, this);
       
    88 
       
    89 #ifndef Q_WS_MAEMO_5
       
    90     volumeSlider->setRange(0, 100);
       
    91 
       
    92     connect(volumeSlider, SIGNAL(sliderMoved(int)), this, SIGNAL(changeVolume(int)));
       
    93 #endif
       
    94 
       
    95     rateBox = new QComboBox(this);
       
    96     rateBox->addItem("0.5x", QVariant(0.5));
       
    97     rateBox->addItem("1.0x", QVariant(1.0));
       
    98     rateBox->addItem("2.0x", QVariant(2.0));
       
    99     rateBox->setCurrentIndex(1);
       
   100 
       
   101     connect(rateBox, SIGNAL(activated(int)), SLOT(updateRate()));
       
   102 
       
   103 
       
   104     QBoxLayout *layout = new QHBoxLayout;
       
   105     layout->setMargin(0);
       
   106     layout->addWidget(stopButton);
       
   107     layout->addWidget(previousButton);
       
   108     layout->addWidget(playButton);
       
   109     layout->addWidget(nextButton);
       
   110     layout->addWidget(muteButton);
       
   111     if (volumeSlider)
       
   112         layout->addWidget(volumeSlider);
       
   113 
       
   114     if (rateBox)
       
   115         layout->addWidget(rateBox);
       
   116     setLayout(layout);
       
   117 }
       
   118 
       
   119 QMediaPlayer::State PlayerControls::state() const
       
   120 {
       
   121     return playerState;
       
   122 }
       
   123 
       
   124 void PlayerControls::setState(QMediaPlayer::State state)
       
   125 {
       
   126     if (state != playerState) {
       
   127         playerState = state;
       
   128 
       
   129         switch (state) {
       
   130         case QMediaPlayer::StoppedState:
       
   131             stopButton->setEnabled(false);
       
   132             playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
   133             break;
       
   134         case QMediaPlayer::PlayingState:
       
   135             stopButton->setEnabled(true);
       
   136             playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
       
   137             break;
       
   138         case QMediaPlayer::PausedState:
       
   139             stopButton->setEnabled(true);
       
   140             playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
   141             break;
       
   142         }
       
   143     }
       
   144 }
       
   145 
       
   146 int PlayerControls::volume() const
       
   147 {
       
   148     return volumeSlider ? volumeSlider->value() : 0;
       
   149 }
       
   150 
       
   151 void PlayerControls::setVolume(int volume)
       
   152 {
       
   153     if (volumeSlider)
       
   154         volumeSlider->setValue(volume);
       
   155 }
       
   156 
       
   157 bool PlayerControls::isMuted() const
       
   158 {
       
   159     return playerMuted;
       
   160 }
       
   161 
       
   162 void PlayerControls::setMuted(bool muted)
       
   163 {
       
   164     if (muted != playerMuted) {
       
   165         playerMuted = muted;
       
   166 
       
   167         muteButton->setIcon(style()->standardIcon(muted
       
   168                 ? QStyle::SP_MediaVolumeMuted
       
   169                 : QStyle::SP_MediaVolume));
       
   170     }
       
   171 }
       
   172 
       
   173 void PlayerControls::playClicked()
       
   174 {
       
   175     switch (playerState) {
       
   176     case QMediaPlayer::StoppedState:
       
   177     case QMediaPlayer::PausedState:
       
   178         emit play();
       
   179         break;
       
   180     case QMediaPlayer::PlayingState:
       
   181         emit pause();
       
   182         break;
       
   183     }
       
   184 }
       
   185 
       
   186 void PlayerControls::muteClicked()
       
   187 {
       
   188     emit changeMuting(!playerMuted);
       
   189 }
       
   190 
       
   191 qreal PlayerControls::playbackRate() const
       
   192 {
       
   193     return rateBox->itemData(rateBox->currentIndex()).toDouble();
       
   194 }
       
   195 
       
   196 void PlayerControls::setPlaybackRate(float rate)
       
   197 {
       
   198     for (int i=0; i<rateBox->count(); i++) {
       
   199         if (qFuzzyCompare(rate, float(rateBox->itemData(i).toDouble()))) {
       
   200             rateBox->setCurrentIndex(i);
       
   201             return;
       
   202         }
       
   203     }
       
   204 
       
   205     rateBox->addItem( QString("%1x").arg(rate), QVariant(rate));
       
   206     rateBox->setCurrentIndex(rateBox->count()-1);
       
   207 }
       
   208 
       
   209 void PlayerControls::updateRate()
       
   210 {
       
   211     emit changeRate(playbackRate());
       
   212 }