1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qabstract3dinputhandler_p.h"
5
6QT_BEGIN_NAMESPACE
7
8/*!
9 * \class QAbstract3DInputHandler
10 * \inmodule QtGraphs
11 * \brief The base class for implementations of input handlers.
12 *
13 * QAbstract3DInputHandler is the base class that is subclassed by different input handling implementations
14 * that take input events and translate those to camera and light movements. Input handlers also translate
15 * raw input events to slicing and selection events in the scene.
16 */
17
18/*!
19 * \enum QAbstract3DInputHandler::InputView
20 *
21 * Predefined input views for mouse and touch based input handlers.
22 *
23 * \value InputViewNone
24 * Mouse or touch not on a view.
25 * \value InputViewOnPrimary
26 * Mouse or touch input received on the primary view area. If secondary view is displayed when
27 * inputView becomes InputViewOnPrimary, secondary view is closed.
28 * \value InputViewOnSecondary
29 * Mouse or touch input received on the secondary view area.
30 */
31
32/*!
33 * \qmltype AbstractInputHandler3D
34 * \inqmlmodule QtGraphs
35 * \ingroup graphs_qml
36 * \instantiates QAbstract3DInputHandler
37 * \brief Base type for all QtGraphs input handlers.
38 *
39 * This type is uncreatable.
40 *
41 * For AbstractInputHandler3D enums, see \l{QAbstract3DInputHandler::InputView}.
42 */
43
44/*!
45 * \internal
46 */
47QAbstract3DInputHandler::QAbstract3DInputHandler(QAbstract3DInputHandlerPrivate *d,
48 QObject *parent) :
49 QObject(parent),
50 d_ptr(d)
51{
52}
53
54/*!
55 * Constructs the base class. An optional \a parent parameter can be given
56 * and is then passed to QObject constructor.
57 */
58QAbstract3DInputHandler::QAbstract3DInputHandler(QObject *parent) :
59 QObject(parent),
60 d_ptr(new QAbstract3DInputHandlerPrivate(this))
61{
62}
63
64/*!
65 * Destroys the base class.
66 */
67QAbstract3DInputHandler::~QAbstract3DInputHandler()
68{
69}
70
71// Input event listeners
72/*!
73 * Override this to handle mouse double click events.
74 * Mouse double click event is given in the \a event.
75 */
76void QAbstract3DInputHandler::mouseDoubleClickEvent(QMouseEvent *event)
77{
78 Q_UNUSED(event);
79}
80
81/*!
82 * Override this to handle touch input events.
83 * Touch event is given in the \a event.
84 */
85void QAbstract3DInputHandler::touchEvent(QTouchEvent *event)
86{
87 Q_UNUSED(event);
88}
89
90/*!
91 * Override this to handle mouse press events.
92 * Mouse press event is given in the \a event and the mouse position in \a mousePos.
93 */
94void QAbstract3DInputHandler::mousePressEvent(QMouseEvent *event, const QPoint &mousePos)
95{
96 Q_UNUSED(event);
97 Q_UNUSED(mousePos);
98}
99
100/*!
101 * Override this to handle mouse release events.
102 * Mouse release event is given in the \a event and the mouse position in \a mousePos.
103 */
104void QAbstract3DInputHandler::mouseReleaseEvent(QMouseEvent *event, const QPoint &mousePos)
105{
106 Q_UNUSED(event);
107 Q_UNUSED(mousePos);
108}
109
110/*!
111 * Override this to handle mouse move events.
112 * Mouse move event is given in the \a event and the mouse position in \a mousePos.
113 */
114void QAbstract3DInputHandler::mouseMoveEvent(QMouseEvent *event, const QPoint &mousePos)
115{
116 Q_UNUSED(event);
117 Q_UNUSED(mousePos);
118}
119
120#if QT_CONFIG(wheelevent)
121/*!
122 * Override this to handle wheel events.
123 * Wheel event is given in the \a event.
124 */
125void QAbstract3DInputHandler::wheelEvent(QWheelEvent *event)
126{
127 Q_UNUSED(event);
128}
129#endif
130
131// Property get/set
132/*!
133 * \property QAbstract3DInputHandler::inputView
134 *
135 * \brief The current enumerated input view based on the view of the processed
136 * input events.
137 *
138 * One of the InputView enum values.
139 *
140 * When the view changes, the \c inputViewChanged signal is emitted.
141 *
142 * \sa InputView
143 */
144QAbstract3DInputHandler::InputView QAbstract3DInputHandler::inputView() const
145{
146 const Q_D(QAbstract3DInputHandler);
147 return d->m_inputView;
148}
149
150void QAbstract3DInputHandler::setInputView(QAbstract3DInputHandler::InputView inputView)
151{
152 Q_D(QAbstract3DInputHandler);
153 if (inputView != d->m_inputView) {
154 d->m_inputView = inputView;
155 emit inputViewChanged(view: inputView);
156 }
157}
158
159/*!
160 * \property QAbstract3DInputHandler::inputPosition
161 *
162 * \brief The last input position based on the processed input events.
163 */
164QPoint QAbstract3DInputHandler::inputPosition() const
165{
166 const Q_D(QAbstract3DInputHandler);
167 return d->m_inputPosition;
168}
169
170void QAbstract3DInputHandler::setInputPosition(const QPoint &position, bool forceSelection)
171{
172 Q_D(QAbstract3DInputHandler);
173 if (position != d->m_inputPosition) {
174 if (forceSelection)
175 d->m_inputState = QAbstract3DInputHandlerPrivate::InputStateSelecting;
176 d->m_inputPosition = position;
177 emit positionChanged(position);
178 }
179}
180
181/*!
182 * Returns the manhattan length between last two input positions.
183 */
184int QAbstract3DInputHandler::prevDistance() const
185{
186 const Q_D(QAbstract3DInputHandler);
187 return d->m_prevDistance;
188}
189
190/*!
191 * Sets the \a distance (manhattan length) between last two input positions.
192 */
193void QAbstract3DInputHandler::setPrevDistance(int distance)
194{
195 Q_D(QAbstract3DInputHandler);
196 d->m_prevDistance = distance;
197}
198
199/*!
200 * \property QAbstract3DInputHandler::scene
201 *
202 * \brief The 3D scene this abstract input handler is controlling.
203 *
204 * One input handler can control one scene. Setting a scene to an input handler
205 * does not transfer the ownership of the scene.
206 */
207Q3DScene *QAbstract3DInputHandler::scene() const
208{
209 const Q_D(QAbstract3DInputHandler);
210 return d->m_scene;
211}
212
213void QAbstract3DInputHandler::setScene(Q3DScene *scene)
214{
215 Q_D(QAbstract3DInputHandler);
216 if (scene != d->m_scene) {
217 if (d->m_scene) {
218 QObject::disconnect(sender: d->m_scene, signal: &Q3DScene::selectionQueryPositionChanged,
219 receiver: this, slot: &QAbstract3DInputHandler::handleSelection);
220 }
221
222 QObject::connect(sender: scene, signal: &Q3DScene::selectionQueryPositionChanged,
223 context: this, slot: &QAbstract3DInputHandler::handleSelection);
224
225 d->m_scene = scene;
226 emit sceneChanged(scene);
227 }
228}
229
230/*!
231 * Sets the previous input position to the point given by \a position.
232 */
233void QAbstract3DInputHandler::setPreviousInputPos(const QPoint &position)
234{
235 Q_D(QAbstract3DInputHandler);
236 d->m_previousInputPos = position;
237}
238
239/*!
240 * Returns the previous input position.
241 */
242QPoint QAbstract3DInputHandler::previousInputPos() const
243{
244 const Q_D(QAbstract3DInputHandler);
245 return d->m_previousInputPos;
246}
247
248/*!
249 * Converts an incoming selection query \a position to a selection.
250 *
251 * \sa Q3DScene::selectionQueryPosition
252 */
253void QAbstract3DInputHandler::handleSelection(const QPoint &position)
254{
255 setInputPosition(position, forceSelection: true);
256}
257
258QAbstract3DInputHandlerPrivate::QAbstract3DInputHandlerPrivate(QAbstract3DInputHandler *q) :
259 m_prevDistance(0),
260 m_previousInputPos(QPoint(0,0)),
261 q_ptr(q),
262 m_inputView(QAbstract3DInputHandler::InputViewNone),
263 m_inputPosition(QPoint(0,0)),
264 m_scene(0),
265 m_isDefaultHandler(false)
266{
267}
268
269QAbstract3DInputHandlerPrivate::~QAbstract3DInputHandlerPrivate()
270{
271
272}
273
274QT_END_NAMESPACE
275

source code of qtgraphs/src/graphs/input/qabstract3dinputhandler.cpp