src/3rdparty/phonon/mmf/videooutput.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 22 79de32ba3296
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
     1 /*  This file is part of the KDE project.
       
     2 
       
     3 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 
       
     5 This library is free software: you can redistribute it and/or modify
       
     6 it under the terms of the GNU Lesser General Public License as published by
       
     7 the Free Software Foundation, either version 2.1 or 3 of the License.
       
     8 
       
     9 This library is distributed in the hope that it will be useful,
       
    10 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 GNU Lesser General Public License for more details.
       
    13 
       
    14 You should have received a copy of the GNU Lesser General Public License
       
    15 along with this library.  If not, see <http://www.gnu.org/licenses/>.
       
    16 
       
    17 */
       
    18 
       
    19 #include "ancestormovemonitor.h"
       
    20 #include "utils.h"
       
    21 #include "videooutput.h"
       
    22 
       
    23 #ifndef QT_NO_DEBUG
       
    24 #include "objectdump.h"
       
    25 #endif
       
    26 
       
    27 #include <QPaintEvent>
       
    28 #include <QPainter>
       
    29 #include <QMoveEvent>
       
    30 #include <QResizeEvent>
       
    31 
       
    32 #include <QtCore/private/qcore_symbian_p.h> // for qt_TRect2QRect
       
    33 #include <QtGui/private/qwidget_p.h> // to access QWExtra
       
    34 
       
    35 #include <coecntrl.h>
       
    36 
       
    37 #include <coemain.h>    // for CCoeEnv
       
    38 
       
    39 QT_BEGIN_NAMESPACE
       
    40 
       
    41 using namespace Phonon;
       
    42 using namespace Phonon::MMF;
       
    43 
       
    44 /*! \class MMF::VideoOutput
       
    45   \internal
       
    46 */
       
    47 
       
    48 //-----------------------------------------------------------------------------
       
    49 // Constants
       
    50 //-----------------------------------------------------------------------------
       
    51 
       
    52 static const Phonon::VideoWidget::AspectRatio DefaultAspectRatio =
       
    53     Phonon::VideoWidget::AspectRatioAuto;
       
    54 static const Phonon::VideoWidget::ScaleMode DefaultScaleMode =
       
    55     Phonon::VideoWidget::FitInView;
       
    56 
       
    57 
       
    58 //-----------------------------------------------------------------------------
       
    59 // Constructor / destructor
       
    60 //-----------------------------------------------------------------------------
       
    61 
       
    62 MMF::VideoOutput::VideoOutput
       
    63     (AncestorMoveMonitor* ancestorMoveMonitor, QWidget* parent)
       
    64         :   QWidget(parent)
       
    65         ,   m_ancestorMoveMonitor(ancestorMoveMonitor)
       
    66         ,   m_aspectRatio(DefaultAspectRatio)
       
    67         ,   m_scaleMode(DefaultScaleMode)
       
    68 {
       
    69     TRACE_CONTEXT(VideoOutput::VideoOutput, EVideoInternal);
       
    70     TRACE_ENTRY("parent 0x%08x", parent);
       
    71 
       
    72     setPalette(QPalette(Qt::black));
       
    73     setAttribute(Qt::WA_OpaquePaintEvent, true);
       
    74     setAttribute(Qt::WA_NoSystemBackground, true);
       
    75     setAutoFillBackground(false);
       
    76 
       
    77     qt_widget_private(this)->extraData()->nativePaintMode = QWExtra::ZeroFill;
       
    78     qt_widget_private(this)->extraData()->receiveNativePaintEvents = true;
       
    79 
       
    80     getVideoWindowRect();
       
    81     registerForAncestorMoved();
       
    82 
       
    83     dump();
       
    84 
       
    85     TRACE_EXIT_0();
       
    86 }
       
    87 
       
    88 MMF::VideoOutput::~VideoOutput()
       
    89 {
       
    90     TRACE_CONTEXT(VideoOutput::~VideoOutput, EVideoInternal);
       
    91     TRACE_ENTRY_0();
       
    92 
       
    93     m_ancestorMoveMonitor->unRegisterTarget(this);
       
    94 
       
    95     TRACE_EXIT_0();
       
    96 }
       
    97 
       
    98 void MMF::VideoOutput::setVideoSize(const QSize& frameSize)
       
    99 {
       
   100     TRACE_CONTEXT(VideoOutput::setVideoSize, EVideoInternal);
       
   101     TRACE("oldSize %d %d newSize %d %d",
       
   102           m_videoFrameSize.width(), m_videoFrameSize.height(),
       
   103           frameSize.width(), frameSize.height());
       
   104 
       
   105     if (frameSize != m_videoFrameSize) {
       
   106         m_videoFrameSize = frameSize;
       
   107         updateGeometry();
       
   108     }
       
   109 }
       
   110 
       
   111 void MMF::VideoOutput::ancestorMoved()
       
   112 {
       
   113     TRACE_CONTEXT(VideoOutput::ancestorMoved, EVideoInternal);
       
   114     TRACE_ENTRY_0();
       
   115 
       
   116     RWindowBase *const window = videoWindow();
       
   117 
       
   118     if(window) {
       
   119         const TPoint newWindowPosSymbian = window->AbsPosition();
       
   120         const QPoint newWindowPos(newWindowPosSymbian.iX, newWindowPosSymbian.iY);
       
   121 
       
   122         if(newWindowPos != m_videoWindowRect.topLeft()) {
       
   123             m_videoWindowRect.moveTo(newWindowPos);
       
   124             emit videoWindowChanged();
       
   125         }
       
   126     }
       
   127 
       
   128     TRACE_EXIT_0();
       
   129 }
       
   130 
       
   131 //-----------------------------------------------------------------------------
       
   132 // QWidget
       
   133 //-----------------------------------------------------------------------------
       
   134 
       
   135 QSize MMF::VideoOutput::sizeHint() const
       
   136 {
       
   137     // TODO: replace this with a more sensible default
       
   138     QSize result(320, 240);
       
   139 
       
   140     if (!m_videoFrameSize.isNull())
       
   141         result = m_videoFrameSize;
       
   142 
       
   143     return result;
       
   144 }
       
   145 
       
   146 void MMF::VideoOutput::paintEvent(QPaintEvent* event)
       
   147 {
       
   148     TRACE_CONTEXT(VideoOutput::paintEvent, EVideoInternal);
       
   149     TRACE("rect %d %d - %d %d",
       
   150           event->rect().left(), event->rect().top(),
       
   151           event->rect().right(), event->rect().bottom());
       
   152     TRACE("regions %d", event->region().rectCount());
       
   153     TRACE("type %d", event->type());
       
   154 
       
   155     // Do nothing
       
   156 }
       
   157 
       
   158 void MMF::VideoOutput::resizeEvent(QResizeEvent* event)
       
   159 {
       
   160     TRACE_CONTEXT(VideoOutput::resizeEvent, EVideoInternal);
       
   161     TRACE("%d %d -> %d %d",
       
   162           event->oldSize().width(), event->oldSize().height(),
       
   163           event->size().width(), event->size().height());
       
   164 
       
   165     if(event->size() != event->oldSize()) {
       
   166         m_videoWindowRect.setSize(event->size());
       
   167         emit videoWindowChanged();
       
   168     }
       
   169 }
       
   170 
       
   171 void MMF::VideoOutput::moveEvent(QMoveEvent* event)
       
   172 {
       
   173     TRACE_CONTEXT(VideoOutput::moveEvent, EVideoInternal);
       
   174     TRACE("%d %d -> %d %d",
       
   175           event->oldPos().x(), event->oldPos().y(),
       
   176           event->pos().x(), event->pos().y());
       
   177 
       
   178     if(event->pos() != event->oldPos()) {
       
   179         m_videoWindowRect.moveTo(event->pos());
       
   180         emit videoWindowChanged();
       
   181     }
       
   182 }
       
   183 
       
   184 bool MMF::VideoOutput::event(QEvent* event)
       
   185 {
       
   186     TRACE_CONTEXT(VideoOutput::event, EVideoInternal);
       
   187 
       
   188     if (event->type() == QEvent::WinIdChange) {
       
   189         TRACE_0("WinIdChange");
       
   190         getVideoWindowRect();
       
   191         emit videoWindowChanged();
       
   192         return true;
       
   193     } else if (event->type() == QEvent::ParentChange) {
       
   194         // Tell ancestor move monitor to update ancestor list for this widget
       
   195         registerForAncestorMoved();
       
   196         return true;
       
   197     } else
       
   198         return QWidget::event(event);
       
   199 }
       
   200 
       
   201 
       
   202 //-----------------------------------------------------------------------------
       
   203 // Public functions
       
   204 //-----------------------------------------------------------------------------
       
   205 
       
   206 RWindowBase* MMF::VideoOutput::videoWindow()
       
   207 {
       
   208     CCoeControl *control = internalWinId();
       
   209     if(!control)
       
   210         control = effectiveWinId();
       
   211 
       
   212     RWindowBase *window = 0;
       
   213     if(control)
       
   214         window = control->DrawableWindow();
       
   215 
       
   216     return window;
       
   217 }
       
   218 
       
   219 const QRect& MMF::VideoOutput::videoWindowRect() const
       
   220 {
       
   221     return m_videoWindowRect;
       
   222 }
       
   223 
       
   224 Phonon::VideoWidget::AspectRatio MMF::VideoOutput::aspectRatio() const
       
   225 {
       
   226     return m_aspectRatio;
       
   227 }
       
   228 
       
   229 void MMF::VideoOutput::setAspectRatio
       
   230     (Phonon::VideoWidget::AspectRatio aspectRatio)
       
   231 {
       
   232     if(m_aspectRatio != aspectRatio) {
       
   233         m_aspectRatio = aspectRatio;
       
   234         emit aspectRatioChanged();
       
   235     }
       
   236 }
       
   237 
       
   238 Phonon::VideoWidget::ScaleMode MMF::VideoOutput::scaleMode() const
       
   239 {
       
   240     return m_scaleMode;
       
   241 }
       
   242 
       
   243 void MMF::VideoOutput::setScaleMode
       
   244     (Phonon::VideoWidget::ScaleMode scaleMode)
       
   245 {
       
   246     if(m_scaleMode != scaleMode) {
       
   247         m_scaleMode = scaleMode;
       
   248         emit scaleModeChanged();
       
   249     }
       
   250 }
       
   251 
       
   252 
       
   253 //-----------------------------------------------------------------------------
       
   254 // Private functions
       
   255 //-----------------------------------------------------------------------------
       
   256 
       
   257 void MMF::VideoOutput::getVideoWindowRect()
       
   258 {
       
   259     RWindowBase *const window = videoWindow();
       
   260     if(window)
       
   261         m_videoWindowRect =
       
   262             qt_TRect2QRect(TRect(window->AbsPosition(), window->Size()));
       
   263 }
       
   264 
       
   265 void MMF::VideoOutput::registerForAncestorMoved()
       
   266 {
       
   267     m_ancestorMoveMonitor->registerTarget(this);
       
   268 }
       
   269 
       
   270 void MMF::VideoOutput::dump() const
       
   271 {
       
   272 #ifndef QT_NO_DEBUG
       
   273     TRACE_CONTEXT(VideoOutput::dump, EVideoInternal);
       
   274 
       
   275     QScopedPointer<ObjectDump::QVisitor> visitor(new ObjectDump::QVisitor);
       
   276     visitor->setPrefix("Phonon::MMF"); // to aid searchability of logs
       
   277     ObjectDump::addDefaultAnnotators(*visitor);
       
   278     TRACE("Dumping tree from leaf 0x%08x:", this);
       
   279     ObjectDump::dumpTreeFromLeaf(*this, *visitor);
       
   280 
       
   281     QScopedPointer<ObjectDump::QDumper> dumper(new ObjectDump::QDumper);
       
   282     dumper->setPrefix("Phonon::MMF"); // to aid searchability of logs
       
   283     ObjectDump::addDefaultAnnotators(*dumper);
       
   284     TRACE_0("Dumping VideoOutput:");
       
   285     dumper->dumpObject(*this);
       
   286 #endif
       
   287 }
       
   288 
       
   289 void MMF::VideoOutput::beginNativePaintEvent(const QRect& /*controlRect*/)
       
   290 {
       
   291     emit beginVideoWindowNativePaint();
       
   292 }
       
   293 
       
   294 void MMF::VideoOutput::endNativePaintEvent(const QRect& /*controlRect*/)
       
   295 {
       
   296     // Ensure that draw ops are executed into the WSERV output framebuffer
       
   297     CCoeEnv::Static()->WsSession().Flush();
       
   298 
       
   299     emit endVideoWindowNativePaint();
       
   300 }
       
   301 
       
   302 QT_END_NAMESPACE
       
   303