tests/auto/mediaobject/dummy/videowidget.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 test suite module 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 "videowidget.h"
       
    43 #include <QtCore/QEvent>
       
    44 #include <QtGui/QResizeEvent>
       
    45 #include <QtGui/QPalette>
       
    46 #include <QtGui/QImage>
       
    47 #include <QtGui/QPainter>
       
    48 #include <QtGui/QBoxLayout>
       
    49 #include <QApplication>
       
    50 #include "mediaobject.h"
       
    51 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 namespace Phonon
       
    55 {
       
    56 namespace Dummy
       
    57 {
       
    58 
       
    59 VideoWidget::VideoWidget(Backend *backend, QWidget *parent) :
       
    60     QWidget(parent),
       
    61     m_aspectRatio(Phonon::VideoWidget::AspectRatioAuto),
       
    62     m_brightness(0.0),
       
    63     m_hue(0.0),
       
    64     m_contrast(0.0),
       
    65     m_saturation(0.0),
       
    66     m_scaleMode(Phonon::VideoWidget::FitInView)
       
    67 {
       
    68     Q_UNUSED(backend)
       
    69 
       
    70 }
       
    71 
       
    72 VideoWidget::~VideoWidget()
       
    73 {
       
    74 }
       
    75 
       
    76 void VideoWidget::paintEvent(QPaintEvent *event)
       
    77 {
       
    78     Q_UNUSED(event)
       
    79 }
       
    80 
       
    81 void VideoWidget::setVisible(bool val)
       
    82 {
       
    83     Q_UNUSED(val)
       
    84 }
       
    85 
       
    86 Phonon::VideoWidget::AspectRatio VideoWidget::aspectRatio() const
       
    87 {
       
    88     return m_aspectRatio;
       
    89 }
       
    90 
       
    91 QSize VideoWidget::sizeHint() const
       
    92 {
       
    93     return QSize(640, 480);
       
    94 }
       
    95 
       
    96 void VideoWidget::setAspectRatio(Phonon::VideoWidget::AspectRatio aspectRatio)
       
    97 {
       
    98     Q_UNUSED(aspectRatio)
       
    99 }
       
   100 
       
   101 Phonon::VideoWidget::ScaleMode VideoWidget::scaleMode() const
       
   102 {
       
   103     return m_scaleMode;
       
   104 }
       
   105 
       
   106 QRect VideoWidget::scaleToAspect(QRect srcRect, int w, int h) const
       
   107 {
       
   108     float width = srcRect.width();
       
   109     float height = srcRect.width() * (float(h) / float(w));
       
   110     if (height > srcRect.height()) {
       
   111         height = srcRect.height();
       
   112         width = srcRect.height() * (float(w) / float(h));
       
   113     }
       
   114     return QRect(0, 0, (int)width, (int)height);
       
   115 }
       
   116 
       
   117 /***
       
   118  * Calculates the actual rectangle the movie will be presented with
       
   119  **/
       
   120 QRect VideoWidget::calculateDrawFrameRect() const
       
   121 {
       
   122     QRect widgetRect = rect();
       
   123     QRect drawFrameRect;
       
   124     // Set m_drawFrameRect to be the size of the smallest possible
       
   125     // rect conforming to the aspect and containing the whole frame:
       
   126     switch (aspectRatio()) {
       
   127 
       
   128     case Phonon::VideoWidget::AspectRatioWidget:
       
   129         drawFrameRect = widgetRect;
       
   130         // No more calculations needed.
       
   131         return drawFrameRect;
       
   132 
       
   133     case Phonon::VideoWidget::AspectRatio4_3:
       
   134         drawFrameRect = scaleToAspect(widgetRect, 4, 3);
       
   135         break;
       
   136 
       
   137     case Phonon::VideoWidget::AspectRatio16_9:
       
   138         drawFrameRect = scaleToAspect(widgetRect, 16, 9);
       
   139         break;
       
   140 
       
   141     case Phonon::VideoWidget::AspectRatioAuto:
       
   142     default:
       
   143         drawFrameRect = QRect(0, 0, movieSize().width(), movieSize().height());
       
   144         break;
       
   145     }
       
   146 
       
   147     // Scale m_drawFrameRect to fill the widget
       
   148     // without breaking aspect:
       
   149     float widgetWidth = widgetRect.width();
       
   150     float widgetHeight = widgetRect.height();
       
   151     float frameWidth = widgetWidth;
       
   152     float frameHeight = drawFrameRect.height() * float(widgetWidth) / float(drawFrameRect.width());
       
   153 
       
   154     switch (scaleMode()) {
       
   155     case Phonon::VideoWidget::ScaleAndCrop:
       
   156         if (frameHeight < widgetHeight) {
       
   157             frameWidth *= float(widgetHeight) / float(frameHeight);
       
   158             frameHeight = widgetHeight;
       
   159         }
       
   160         break;
       
   161     case Phonon::VideoWidget::FitInView:
       
   162     default:
       
   163         if (frameHeight > widgetHeight) {
       
   164             frameWidth *= float(widgetHeight) / float(frameHeight);
       
   165             frameHeight = widgetHeight;
       
   166         }
       
   167         break;
       
   168     }
       
   169     drawFrameRect.setSize(QSize(int(frameWidth), int(frameHeight)));
       
   170     drawFrameRect.moveTo(int((widgetWidth - frameWidth) / 2.0f),
       
   171                            int((widgetHeight - frameHeight) / 2.0f));
       
   172     return drawFrameRect;
       
   173 }
       
   174 
       
   175 void VideoWidget::setScaleMode(Phonon::VideoWidget::ScaleMode scaleMode)
       
   176 {
       
   177     Q_UNUSED(scaleMode)
       
   178 }
       
   179 
       
   180 qreal VideoWidget::brightness() const
       
   181 {
       
   182     return m_brightness;
       
   183 }
       
   184 
       
   185 qreal clampedValue(qreal val)
       
   186 {
       
   187     if (val > 1.0 )
       
   188         return 1.0;
       
   189     else if (val < -1.0)
       
   190         return -1.0;
       
   191     else return val;
       
   192 }
       
   193 
       
   194 void VideoWidget::setBrightness(qreal newValue)
       
   195 {
       
   196     Q_UNUSED(newValue)
       
   197 }
       
   198 
       
   199 qreal VideoWidget::contrast() const
       
   200 {
       
   201     return m_contrast;
       
   202 }
       
   203 
       
   204 void VideoWidget::setContrast(qreal newValue)
       
   205 {
       
   206     Q_UNUSED(newValue)
       
   207 }
       
   208 
       
   209 qreal VideoWidget::hue() const
       
   210 {
       
   211     return m_hue;
       
   212 }
       
   213 
       
   214 void VideoWidget::setHue(qreal newValue)
       
   215 {
       
   216     Q_UNUSED(newValue)
       
   217 }
       
   218 
       
   219 qreal VideoWidget::saturation() const
       
   220 {
       
   221     return m_saturation;
       
   222 }
       
   223 
       
   224 void VideoWidget::setSaturation(qreal newValue)
       
   225 {
       
   226     Q_UNUSED(newValue)
       
   227 }
       
   228 
       
   229 bool VideoWidget::event(QEvent *event)
       
   230 {
       
   231     return QWidget::event(event);
       
   232 }
       
   233 
       
   234 void VideoWidget::setMovieSize(const QSize &size)
       
   235 {
       
   236     m_movieSize = size;
       
   237     widget()->updateGeometry();
       
   238     widget()->update();
       
   239 }
       
   240 
       
   241 }
       
   242 } //namespace Phonon::Dummy
       
   243 
       
   244 QT_END_NAMESPACE
       
   245 
       
   246 #include "moc_videowidget.cpp"