1 /*****************************************************************************
2 * Copyright (C) 2019 VLC authors and VideoLAN
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17 *****************************************************************************/
18 #include "videosurface.hpp"
19 #include "maininterface/main_interface.hpp"
22 VideoSurfaceProvider::VideoSurfaceProvider(QObject
* parent
)
27 VideoSurface::VideoSurface(QQuickItem
* parent
)
30 setAcceptHoverEvents(true);
31 setAcceptedMouseButtons(Qt::AllButtons
);
32 setFlag(ItemAcceptsInputMethod
, true);
33 setFlag(ItemHasContents
, true);
35 connect(this, &QQuickItem::widthChanged
, this, &VideoSurface::onSurfaceSizeChanged
);
36 connect(this, &QQuickItem::heightChanged
, this, &VideoSurface::onSurfaceSizeChanged
);
39 QmlMainContext
*VideoSurface::getCtx()
44 void VideoSurface::setCtx(QmlMainContext
* mainctx
)
47 emit
ctxChanged(mainctx
);
50 QSize
VideoSurface::getSourceSize() const
55 int VideoSurface::qtMouseButton2VLC( Qt::MouseButton qtButton
)
63 case Qt::MiddleButton
:
70 void VideoSurface::mousePressEvent(QMouseEvent
* event
)
72 int vlc_button
= qtMouseButton2VLC( event
->button() );
75 emit
mousePressed(vlc_button
);
82 void VideoSurface::mouseReleaseEvent(QMouseEvent
* event
)
84 int vlc_button
= qtMouseButton2VLC( event
->button() );
87 emit
mouseReleased(vlc_button
);
94 void VideoSurface::mouseMoveEvent(QMouseEvent
* event
)
96 QPointF current_pos
= event
->localPos();
97 emit
mouseMoved(current_pos
.x() , current_pos
.y());
101 void VideoSurface::hoverMoveEvent(QHoverEvent
* event
)
103 QPointF current_pos
= event
->posF();
104 if (current_pos
!= m_oldHoverPos
)
106 float scaleW
= m_sourceSize
.width() / width();
107 float scaleH
= m_sourceSize
.height() / height();
108 emit
mouseMoved(current_pos
.x() * scaleW
, current_pos
.y() * scaleH
);
109 m_oldHoverPos
= current_pos
;
114 void VideoSurface::mouseDoubleClickEvent(QMouseEvent
* event
)
116 int vlc_button
= qtMouseButton2VLC( event
->button() );
117 if( vlc_button
>= 0 )
119 emit
mouseDblClicked(vlc_button
);
126 void VideoSurface::keyPressEvent(QKeyEvent
* event
)
128 emit
keyPressed(event
->key(), event
->modifiers());
132 #if QT_CONFIG(wheelevent)
133 void VideoSurface::wheelEvent(QWheelEvent
*event
)
135 emit
mouseWheeled(event
->posF(), event
->delta(), event
->buttons(), event
->modifiers(), event
->orientation());
140 Qt::CursorShape
VideoSurface::getCursorShape() const
142 return cursor().shape();
145 void VideoSurface::setCursorShape(Qt::CursorShape shape
)
150 QSGNode
*VideoSurface::updatePaintNode(QSGNode
* node
, QQuickItem::UpdatePaintNodeData
* nodeData
)
152 if (m_provider
== nullptr)
154 if (m_mainCtx
== nullptr)
156 m_provider
= m_mainCtx
->getMainInterface()->getVideoSurfaceProvider();
160 //forward signal to the provider
161 connect(this, &VideoSurface::mouseMoved
, m_provider
, &VideoSurfaceProvider::mouseMoved
);
162 connect(this, &VideoSurface::mousePressed
, m_provider
, &VideoSurfaceProvider::mousePressed
);
163 connect(this, &VideoSurface::mouseDblClicked
, m_provider
, &VideoSurfaceProvider::mouseDblClicked
);
164 connect(this, &VideoSurface::mouseReleased
, m_provider
, &VideoSurfaceProvider::mouseReleased
);
165 connect(this, &VideoSurface::mouseWheeled
, m_provider
, &VideoSurfaceProvider::mouseWheeled
);
166 connect(this, &VideoSurface::keyPressed
, m_provider
, &VideoSurfaceProvider::keyPressed
);
167 connect(this, &VideoSurface::surfaceSizeChanged
, m_provider
, &VideoSurfaceProvider::surfaceSizeChanged
);
169 connect(m_provider
, &VideoSurfaceProvider::update
, this, &VideoSurface::update
);
170 connect(m_provider
, &VideoSurfaceProvider::sourceSizeChanged
, this, &VideoSurface::onSourceSizeChanged
);
172 onSurfaceSizeChanged();
174 return m_provider
->updatePaintNode(this, node
, nodeData
);
177 void VideoSurface::onSourceSizeChanged(QSize newSize
)
179 if (newSize
!= m_sourceSize
) {
180 m_sourceSize
= newSize
;
181 emit
sourceSizeChanged(m_sourceSize
);
182 onSurfaceSizeChanged();
186 void VideoSurface::onSurfaceSizeChanged()
188 emit
surfaceSizeChanged(size());