File indexing completed on 2024-04-28 11:38:49

0001 /*
0002  * Copyright (C) 2009 Michael Howell <mhowell123@gmail.com>.
0003  * Parts copyright (C) 2007, 2008 Apple Inc. All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
0015  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0016  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0017  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
0018  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0019  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0020  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0021  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
0022  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0024  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025  */
0026 
0027 #include "render_media.h"
0028 #include "media_controls.h"
0029 #include <phonon/mediaobject.h>
0030 #include <phonon/videowidget.h>
0031 #include <QVBoxLayout>
0032 
0033 const double doubleMax = 999999999.8; // ### numeric_limits<double>::max()
0034 const double doubleInf = 999999999.0; // ### numeric_limits<double>::infinity()
0035 
0036 using namespace DOM;
0037 
0038 namespace khtml
0039 {
0040 
0041 RenderMedia::RenderMedia(HTMLMediaElement *element) : RenderWidget(element), m_player(nullptr)
0042 {
0043     setInline(true); // <video> is an inline element.
0044     QWidget *container = new QWidget();
0045     container->setLayout(new QVBoxLayout(container));
0046     setQWidget(container);
0047 }
0048 
0049 void RenderMedia::setPlayer(MediaPlayer *player)
0050 {
0051     if (m_player == player) {
0052         return;
0053     }
0054     if (m_player) {
0055         m_player->deleteLater();
0056     }
0057     m_player = player;
0058     connect(player->mediaObject(), SIGNAL(metaDataChanged()), SLOT(slotMetaDataChanged()));
0059     player->setParent(widget());
0060     widget()->layout()->addWidget(player);
0061 }
0062 
0063 void RenderMedia::layout()
0064 {
0065     calcWidth();
0066     calcHeight();
0067 
0068     RenderWidget::layout();
0069 
0070     if (mediaElement()->controls() && widget()->layout()->count() == 1) {
0071         MediaControls *toolbox = new MediaControls(player(), widget());
0072         widget()->layout()->addWidget(toolbox);
0073         if ((!widget()->underMouse()) && mediaElement()->isVideo()) {
0074             toolbox->hide();
0075         } else {
0076             toolbox->show();
0077         }
0078     }
0079 }
0080 
0081 bool RenderMedia::eventFilter(QObject *o, QEvent *e)
0082 {
0083     if (widget()->layout()->count() > 1 && mediaElement()->isVideo()) {
0084         switch (e->type()) {
0085         case QEvent::Enter:
0086         case QEvent::FocusIn:
0087             widget()->layout()->itemAt(1)->widget()->show();
0088             break;
0089         case QEvent::Leave:
0090         case QEvent::FocusOut:
0091             widget()->layout()->itemAt(1)->widget()->hide();
0092             break;
0093         default:;
0094         }
0095     }
0096 
0097     return RenderWidget::eventFilter(o, e);
0098 }
0099 
0100 void RenderMedia::updateFromElement()
0101 {
0102     RenderWidget::updateFromElement();
0103 }
0104 
0105 void RenderMedia::slotMetaDataChanged()
0106 {
0107     if (mediaElement()->isVideo()) {
0108         if (player()->videoWidget()->sizeHint().isValid()) {
0109             setIntrinsicWidth(player()->videoWidget()->sizeHint().width());
0110             setIntrinsicHeight(player()->videoWidget()->sizeHint().height());
0111         }
0112     } else {
0113         if (widget()->sizeHint().isValid()) {
0114             setIntrinsicWidth(widget()->sizeHint().width());
0115             setIntrinsicHeight(widget()->sizeHint().height());
0116         }
0117         player()->hide();
0118     }
0119 
0120     setNeedsLayoutAndMinMaxRecalc();
0121 }
0122 
0123 }
0124 
0125 #include "moc_render_media.cpp"