qt: move the main interface to its own folder
[vlc.git] / modules / gui / qt / components / voutwindow / videosurface.cpp
blob8e559041b4e742cad7e7178cc367990244ea5bd6
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)
23 : QObject(parent)
27 VideoSurface::VideoSurface(QQuickItem* parent)
28 : 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()
41 return m_mainCtx;
44 void VideoSurface::setCtx(QmlMainContext* mainctx)
46 m_mainCtx = mainctx;
47 emit ctxChanged(mainctx);
50 QSize VideoSurface::getSourceSize() const
52 return m_sourceSize;
55 int VideoSurface::qtMouseButton2VLC( Qt::MouseButton qtButton )
57 switch( qtButton )
59 case Qt::LeftButton:
60 return 0;
61 case Qt::RightButton:
62 return 2;
63 case Qt::MiddleButton:
64 return 1;
65 default:
66 return -1;
70 void VideoSurface::mousePressEvent(QMouseEvent* event)
72 int vlc_button = qtMouseButton2VLC( event->button() );
73 if( vlc_button >= 0 )
75 emit mousePressed(vlc_button);
76 event->accept();
78 else
79 event->ignore();
82 void VideoSurface::mouseReleaseEvent(QMouseEvent* event)
84 int vlc_button = qtMouseButton2VLC( event->button() );
85 if( vlc_button >= 0 )
87 emit mouseReleased(vlc_button);
88 event->accept();
90 else
91 event->ignore();
94 void VideoSurface::mouseMoveEvent(QMouseEvent* event)
96 QPointF current_pos = event->localPos();
97 emit mouseMoved(current_pos.x() , current_pos.y());
98 event->accept();
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;
111 event->accept();
114 void VideoSurface::mouseDoubleClickEvent(QMouseEvent* event)
116 int vlc_button = qtMouseButton2VLC( event->button() );
117 if( vlc_button >= 0 )
119 emit mouseDblClicked(vlc_button);
120 event->accept();
122 else
123 event->ignore();
126 void VideoSurface::keyPressEvent(QKeyEvent* event)
128 emit keyPressed(event->key(), event->modifiers());
129 event->ignore();
132 #if QT_CONFIG(wheelevent)
133 void VideoSurface::wheelEvent(QWheelEvent *event)
135 emit mouseWheeled(event->posF(), event->delta(), event->buttons(), event->modifiers(), event->orientation());
136 event->ignore();
138 #endif
140 Qt::CursorShape VideoSurface::getCursorShape() const
142 return cursor().shape();
145 void VideoSurface::setCursorShape(Qt::CursorShape shape)
147 setCursor(shape);
150 QSGNode*VideoSurface::updatePaintNode(QSGNode* node, QQuickItem::UpdatePaintNodeData* nodeData)
152 if (m_provider == nullptr)
154 if (m_mainCtx == nullptr)
155 return nullptr;
156 m_provider = m_mainCtx->getMainInterface()->getVideoSurfaceProvider();
157 if (!m_provider)
158 return nullptr;
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());