summaryrefslogtreecommitdiffstats
path: root/src/input/frontend/qmousehandler.cpp
blob: 419052d8ad146ef1781751e4b163e7a727d1f8d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmousehandler.h"
#include "qmousehandler_p.h"

#include <Qt3DInput/qmousedevice.h>
#include <Qt3DInput/qmouseevent.h>
#include <Qt3DCore/qpropertyupdatedchange.h>
#include <QtCore/QTimer>

QT_BEGIN_NAMESPACE

using namespace Qt3DCore;

namespace Qt3DInput {
/*! \internal */
QMouseHandlerPrivate::QMouseHandlerPrivate()
    : QComponentPrivate()
    , m_mouseDevice(nullptr)
    , m_containsMouse(false)
    , m_pressAndHoldTimer(new QTimer)
{
    m_shareable = false;
    m_pressAndHoldTimer->setSingleShot(true);
    m_pressAndHoldTimer->setInterval(500);
    QObject::connect(m_pressAndHoldTimer.data(), &QTimer::timeout, [this] {
        emit q_func()->pressAndHold(m_lastPressedEvent.data());
    });
}

QMouseHandlerPrivate::~QMouseHandlerPrivate()
{
}

void QMouseHandlerPrivate::mouseEvent(const QMouseEventPtr &event)
{
    Q_Q(QMouseHandler);
    switch (event->type()) {
    case QEvent::MouseButtonPress: {
        m_lastPressedEvent = event;
        m_pressAndHoldTimer->start();
        emit q->pressed(event.data());
        break;
    }
    case QEvent::MouseButtonRelease:
        m_pressAndHoldTimer->stop();
        emit q->released(event.data());
        break;
#if QT_CONFIG(gestures)
    case Qt::TapGesture:
        emit q->clicked(event.data());
        break;
#endif
    case QEvent::MouseButtonDblClick:
        emit q->doubleClicked(event.data());
        break;
    case QEvent::MouseMove:
        emit q->positionChanged(event.data());
        break;
    default:
        break;
    }
}

/*!
 * \qmltype MouseHandler
 * \instantiates Qt3DInput::QMouseHandler
 * \inqmlmodule Qt3D.Input
 * \since 5.5
 * \brief Provides mouse event notification
 *
 * \TODO
 * \sa MouseDevice
 */

/*!
 * \class Qt3DInput::QMouseHandler
 * \inheaderfile Qt3DInput/QMouseHandler
 * \inmodule Qt3DInput
 *
 * \brief Provides a means of being notified about mouse events when attached to
 * a QMouseDevice instance.
 *
 * \since 5.5
 *
 * \note QMouseHandler components shouldn't be shared, not respecting that
 * condition will most likely result in undefined behaviors.
 *
 * \sa QMouseDevice
 */

/*!
    \qmlproperty MouseDevice Qt3D.Input::MouseHandler::sourceDevice
    Holds the current mouse source device of the MouseHandler instance.
 */

/*!
    \qmlproperty bool Qt3D.Input::MouseHandler::containsMouse
    \readonly
    Holds \c true if the QMouseHandler currently contains the mouse.
 */

/*!
    \qmlsignal Qt3D.Input::MouseHandler::clicked()
    This signal is emitted when a mouse button is clicked
 */

/*!
    \qmlsignal Qt3D.Input::MouseHandler::doubleClicked()
    This signal is emitted when a mouse button is double clicked
 */

/*!
    \qmlsignal Qt3D.Input::MouseHandler::entered()
 */

/*!
    \qmlsignal Qt3D.Input::MouseHandler::exited()
 */

/*!
    \qmlsignal Qt3D.Input::MouseHandler::pressed()
    This signal is emitted when a mouse button is pressed
 */

/*!
    \qmlsignal Qt3D.Input::MouseHandler::released()
    This signal is emitted when a mouse button is released
 */

/*!
    \qmlsignal Qt3D.Input::MouseHandler::pressAndHold()
    This signal is emitted when a mouse button is pressed and held down
 */

/*!
    \qmlsignal Qt3D.Input::MouseHandler::positionChanged()
    This signal is emitted when the mouse position changes
 */

/*!
    \qmlsignal Qt3D.Input::MouseHandler::wheel()
    This signal is emitted when the mouse wheel is used
 */



/*!
    \fn QMouseHandler::clicked(Qt3DInput::QMouseEvent *mouse)
    This signal is emitted when a mouse button is clicked with the event details being contained within \a mouse
 */

/*!
    \fn QMouseHandler::doubleClicked(Qt3DInput::QMouseEvent *mouse)
    This signal is emitted when a mouse button is double clicked with the event details being contained within \a mouse

 */

/*!
    \fn QMouseHandler::entered()
 */

/*!
    \fn QMouseHandler::exited()
 */

/*!
    \fn QMouseHandler::pressed(Qt3DInput::QMouseEvent *mouse)
    This signal is emitted when a mouse button is pressed with the event details being contained within \a mouse
 */

/*!
    \fn QMouseHandler::released(Qt3DInput::QMouseEvent *mouse)
    This signal is emitted when a mouse button is released with the event details being contained within \a mouse

 */

/*!
    \fn QMouseHandler::pressAndHold(Qt3DInput::QMouseEvent *mouse)
    This signal is emitted when a mouse button is pressed and held down with the event details being contained within \a mouse
 */

/*!
    \fn QMouseHandler::positionChanged(Qt3DInput::QMouseEvent *mouse)
    This signal is emitted when the mouse position changes with the event details being contained within \a mouse

 */

/*!
    \fn QMouseHandler::wheel(Qt3DInput::QWheelEvent *wheel)
    This signal is emitted when the mouse wheel is used with the event details being contained within \a wheel

 */

/*!
 * Constructs a new QMouseHandler instance with parent \a parent.
 */
QMouseHandler::QMouseHandler(QNode *parent)
    : QComponent(*new QMouseHandlerPrivate, parent)
{
}

QMouseHandler::~QMouseHandler()
{
}

/*!
 * Sets the mouse device of the QMouseHandler instance to \a mouseDevice.
 */
void QMouseHandler::setSourceDevice(QMouseDevice *mouseDevice)
{
    Q_D(QMouseHandler);
    if (d->m_mouseDevice != mouseDevice) {

        if (d->m_mouseDevice)
            d->unregisterDestructionHelper(d->m_mouseDevice);

        // We need to add it as a child of the current node if it has been declared inline
        // Or not previously added as a child of the current node so that
        // 1) The backend gets notified about it's creation
        // 2) When the current node is destroyed, it gets destroyed as well
        if (mouseDevice && !mouseDevice->parent())
            mouseDevice->setParent(this);
        d->m_mouseDevice = mouseDevice;

        // Ensures proper bookkeeping
        if (d->m_mouseDevice)
            d->registerDestructionHelper(d->m_mouseDevice, &QMouseHandler::setSourceDevice, d->m_mouseDevice);

        emit sourceDeviceChanged(mouseDevice);
    }
}

/*!
 * \property QMouseHandler::sourceDevice
 *
 * Holds the current mouse source device of the QMouseHandler instance.
 */
QMouseDevice *QMouseHandler::sourceDevice() const
{
    Q_D(const QMouseHandler);
    return d->m_mouseDevice;
}

/*!
 * \property QMouseHandler::containsMouse
 *
 * Holds \c true if the QMouseHandler currently contains the mouse.
 *
 * \note In this context, contains mean that the ray originating from the
 * mouse is intersecting with the Qt3DCore::QEntity that aggregates the current
 * QMouseHandler instance component.
 */
bool QMouseHandler::containsMouse() const
{
    Q_D(const QMouseHandler);
    return d->m_containsMouse;
}

/*! \internal */
void QMouseHandler::setContainsMouse(bool contains)
{
    Q_D(QMouseHandler);
    if (contains != d->m_containsMouse) {
        d->m_containsMouse = contains;
        emit containsMouseChanged(contains);
    }
}

/*! \internal */
void QMouseHandler::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change)
{
    Q_D(QMouseHandler);
    QPropertyUpdatedChangePtr e = qSharedPointerCast<QPropertyUpdatedChange>(change);
    if (e->type() == PropertyUpdated) {
        if (e->propertyName() == QByteArrayLiteral("mouse")) {
            QMouseEventPtr ev = e->value().value<QMouseEventPtr>();
            d->mouseEvent(ev);
#if QT_CONFIG(wheelevent)
        } else if (e->propertyName() == QByteArrayLiteral("wheel")) {
            QWheelEventPtr ev = e->value().value<QWheelEventPtr>();
            emit wheel(ev.data());
#endif
        }
    }
}

Qt3DCore::QNodeCreatedChangeBasePtr QMouseHandler::createNodeCreationChange() const
{
    auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QMouseHandlerData>::create(this);
    auto &data = creationChange->data;

    Q_D(const QMouseHandler);
    data.mouseDeviceId = qIdForNode(d->m_mouseDevice);

    return creationChange;
}

} // namespace Qt3DInput

QT_END_NAMESPACE