summaryrefslogtreecommitdiffstats
path: root/src/graphs3d/input/qgraphsinputhandler.cpp
blob: c862a1a8e7f2bbc3320958618ae944ace6b505d0 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qgraphsinputhandler_p.h"

#include <QtQuick/private/qquickdraghandler_p.h>
#include <QtQuick/private/qquickpinchhandler_p.h>
#include <QtQuick/private/qquicktaphandler_p.h>
#include <QtQuick/private/qquickwheelhandler_p.h>

#include "qquickgraphsitem_p.h"
#include "qgraphs3dlogging_p.h"

QGraphsInputHandler::QGraphsInputHandler(QQuickItem *parent)
    : QQuickItem(parent)
    , m_zoomEnabled(true)
    , m_zoomAtTarget(true)
    , m_rotationEnabled(true)
    , m_selectionEnabled(true)
    , m_pendingPoint(QPoint())
    , m_pinchDiff(.0f)
    , m_graphsItem(nullptr)
{
    m_pinchHandler = new QQuickPinchHandler(this);
    m_tapHandler = new QQuickTapHandler(this);

    // This is to support QQuickGraphsItem's mouseMove signal.
    setAcceptHoverEvents(true);
    m_dragHandler = new QQuickDragHandler(this);
    m_wheelHandler = new QQuickWheelHandler(this);
    m_dragHandler->setAcceptedButtons(Qt::MouseButton::RightButton);
    m_wheelHandler->setAcceptedDevices(QInputDevice::DeviceType::Mouse
                                       | QInputDevice::DeviceType::TouchPad);
    QObject::connect(m_tapHandler, &QQuickTapHandler::tapped, this, &QGraphsInputHandler::onTapped);
    QObject::connect(m_dragHandler,
                     &QQuickDragHandler::translationChanged,
                     this,
                     &QGraphsInputHandler::onTranslationChanged);
    QObject::connect(m_dragHandler,
                     &QQuickDragHandler::grabChanged,
                     this,
                     &QGraphsInputHandler::onGrabChanged);
    QObject::connect(m_wheelHandler,
                     &QQuickWheelHandler::wheel,
                     this,
                     &QGraphsInputHandler::onWheel);
    QObject::connect(m_pinchHandler,
                     &QQuickPinchHandler::scaleChanged,
                     this,
                     &QGraphsInputHandler::onPinchScaleChanged);
    QObject::connect(m_pinchHandler,
                     &QQuickPinchHandler::grabChanged,
                     this,
                     &QGraphsInputHandler::onGrabChanged);
}

QGraphsInputHandler::~QGraphsInputHandler() {}

void QGraphsInputHandler::setZoomEnabled(bool enable)
{
    if (m_zoomEnabled == enable) {
        qCDebug(lcProperties3D) << __FUNCTION__
            << "value is already set to:" << enable;
        return;
    }
    m_zoomEnabled = enable;
    if (m_graphsItem)
        emit m_graphsItem->zoomEnabledChanged(enable);
}

bool QGraphsInputHandler::isZoomEnabled()
{
    return m_zoomEnabled;
}

void QGraphsInputHandler::setZoomAtTargetEnabled(bool enable)
{
    if (m_zoomAtTarget == enable) {
        qCDebug(lcProperties3D) << __FUNCTION__
            << "value is already set to:" << enable;
        return;
    }
    m_zoomAtTarget = enable;
    if (m_graphsItem)
        emit m_graphsItem->zoomAtTargetEnabledChanged(enable);
}

bool QGraphsInputHandler::isZoomAtTargetEnabled()
{
    return m_zoomAtTarget;
}

void QGraphsInputHandler::setRotationEnabled(bool enable)
{
    if (m_rotationEnabled == enable) {
        qCDebug(lcProperties3D) << __FUNCTION__
            << "value is already set to:" << enable;
    }
    m_rotationEnabled = enable;
    if (m_graphsItem)
        emit m_graphsItem->rotationEnabledChanged(enable);
}

bool QGraphsInputHandler::isRotationEnabled()
{
    return m_rotationEnabled;
}

void QGraphsInputHandler::setSelectionEnabled(bool enable)
{
    if (m_selectionEnabled == enable) {
        qCDebug(lcProperties3D) << __FUNCTION__
            << "value is already set to:" << enable;
        return;
    }
    m_selectionEnabled = enable;
    if (m_graphsItem)
        emit m_graphsItem->selectionEnabledChanged(enable);
}

bool QGraphsInputHandler::isSelectionEnabled()
{
    return m_selectionEnabled;
}

void QGraphsInputHandler::setDefaultInputHandler()
{
    setVisible(true);
}

void QGraphsInputHandler::unsetDefaultInputHandler()
{
    setVisible(false);
}

void QGraphsInputHandler::unsetDefaultTapHandler()
{
    QObject::disconnect(m_tapHandler,
                        &QQuickTapHandler::tapped,
                        this,
                        &QGraphsInputHandler::onTapped);
}

void QGraphsInputHandler::unsetDefaultDragHandler()
{
    QObject::disconnect(m_dragHandler,
                        &QQuickDragHandler::translationChanged,
                        this,
                        &QGraphsInputHandler::onTranslationChanged);
}

void QGraphsInputHandler::unsetDefaultWheelHandler()
{
    QObject::disconnect(m_wheelHandler,
                        &QQuickWheelHandler::wheel,
                        this,
                        &QGraphsInputHandler::onWheel);
}

void QGraphsInputHandler::unsetDefaultPinchHandler()
{
    QObject::disconnect(m_pinchHandler,
                        &QQuickPinchHandler::scaleChanged,
                        this,
                        &QGraphsInputHandler::onPinchScaleChanged);
}

void QGraphsInputHandler::setDragButton(Qt::MouseButtons button)
{
    m_dragHandler->setAcceptedButtons(button | Qt::MouseButton::RightButton);
}

void QGraphsInputHandler::setGraphsItem(QQuickGraphsItem *item)
{
    m_graphsItem = item;
    QObject::connect(m_tapHandler, &QQuickTapHandler::tapped, item, &QQuickGraphsItem::tapped);
    QObject::connect(m_tapHandler,
                     &QQuickTapHandler::doubleTapped,
                     item,
                     &QQuickGraphsItem::doubleTapped);
    QObject::connect(m_tapHandler,
                     &QQuickTapHandler::longPressed,
                     item,
                     &QQuickGraphsItem::longPressed);
    QObject::connect(m_dragHandler,
                     &QQuickDragHandler::translationChanged,
                     item,
                     &QQuickGraphsItem::dragged);
    QObject::connect(m_wheelHandler, &QQuickWheelHandler::wheel, item, &QQuickGraphsItem::wheel);
    QObject::connect(m_pinchHandler,
                     &QQuickPinchHandler::scaleChanged,
                     item,
                     &QQuickGraphsItem::pinch);
    QObject::connect(this, &QGraphsInputHandler::mouseMove, item, &QQuickGraphsItem::mouseMove);
}

void QGraphsInputHandler::onTapped()
{
    if (!m_selectionEnabled)
        return;

    if (m_graphsItem->isSlicingActive()) {
        m_graphsItem->setSliceActivatedChanged(true);
        m_graphsItem->update();
        return;
    }
    m_graphsItem->doPicking(m_tapHandler->point().position());
}

void QGraphsInputHandler::onTranslationChanged(QVector2D delta)
{
    if (!m_rotationEnabled)
        return;

    if (m_dragHandler->centroid().pressedButtons().testFlag(Qt::LeftButton))
        return;

    float rotationSpeed = 1.0f;
#if !defined(Q_OS_IOS)
    rotationSpeed = 10.0f;
#endif
    QQuickGraphsItem *item = m_graphsItem;
    // Calculate mouse movement since last frame
    float xRotation = item->cameraXRotation();
    float yRotation = item->cameraYRotation();
    // Apply to rotations
    xRotation += (delta.x() / rotationSpeed);
    yRotation += (delta.y() / rotationSpeed);
    item->setCameraXRotation(xRotation);
    item->setCameraYRotation(yRotation);
}

void QGraphsInputHandler::onGrabChanged(QPointingDevice::GrabTransition transition,
                                        QEventPoint point)
{
    static QPointF pickPoint;

    if (transition == QPointingDevice::GrabPassive) {
        pickPoint = point.position().toPoint();
    } else if (transition == QPointingDevice::GrabExclusive) {
        if (m_dragHandler->centroid().pressedButtons().testFlag(Qt::LeftButton))
            m_graphsItem->doPicking(pickPoint);
    } else if (transition == QPointingDevice::UngrabExclusive
               || transition == QPointingDevice::UngrabPassive) {
        setPosition(QPointF(.0f, .0f));
        setScale(1.f);
        setRotation(.0f);
        emit m_graphsItem->selectedElementChanged(QtGraphs3D::ElementType::None);
        pickPoint = QPointF();
    }
}

void QGraphsInputHandler::onWheel(QQuickWheelEvent *event)
{
    if (!m_zoomEnabled) {
        qCWarning(lcEvents3D, "%s zooming needs to be enabled",
                qUtf8Printable(QLatin1String(__FUNCTION__)));
        return;
    }

    int halfSizeZoomLevel = 50;
    int oneToOneZoomLevel = 100;

    int driftTowardCenterLevel = 175;
    float wheelZoomDrift = 0.1f;

    int nearZoomRangeDivider = 12;
    int midZoomRangeDivider = 60;
    int farZoomRangeDivider = 120;

    if (m_graphsItem->isSlicingActive())
        return;

    // Adjust zoom level based on what zoom range we're in.
    QQuickGraphsItem *item = m_graphsItem;
    int zoomLevel = int(item->cameraZoomLevel());
    const int minZoomLevel = int(item->minCameraZoomLevel());
    const int maxZoomLevel = int(item->maxCameraZoomLevel());
    if (zoomLevel > oneToOneZoomLevel)
        zoomLevel += event->angleDelta().y() / nearZoomRangeDivider;
    else if (zoomLevel > halfSizeZoomLevel)
        zoomLevel += event->angleDelta().y() / midZoomRangeDivider;
    else
        zoomLevel += event->angleDelta().y() / farZoomRangeDivider;
    zoomLevel = qBound(minZoomLevel, zoomLevel, maxZoomLevel);

    if (m_zoomAtTarget) {
        QVector3D targetPosition = item->graphPositionAt(QPoint(event->x(), event->y()));
        float previousZoom = item->cameraZoomLevel();
        item->setCameraZoomLevel(zoomLevel);

        float diffAdj = 0.0f;

        // If zooming in/out outside the graph, or zooming out after certain point,
        // move towards the center.
        if ((qAbs(targetPosition.x()) > 2.0f || qAbs(targetPosition.y()) > 2.0f
             || qAbs(targetPosition.z()) > 2.0f)
            || (previousZoom > zoomLevel && zoomLevel <= driftTowardCenterLevel)) {
            targetPosition = QVector3D();
            // Add some extra correction so that we actually reach the center
            // eventually
            diffAdj = wheelZoomDrift;
            if (previousZoom > zoomLevel)
                diffAdj *= 2.0f; // Correct towards center little more when zooming out
        }

        float zoomFraction = 1.0f - (previousZoom / zoomLevel);

        // Adjust camera towards the zoom point, attempting to keep the cursor at
        // same graph point
        QVector3D oldTarget = item->cameraTargetPosition();
        QVector3D origDiff = targetPosition - oldTarget;
        QVector3D diff = origDiff * zoomFraction + (origDiff.normalized() * diffAdj);
        if (diff.length() > origDiff.length())
            diff = origDiff;
        item->setCameraTargetPosition(oldTarget + diff);
    } else {
        item->setCameraZoomLevel(zoomLevel);
    }

    item->update();
}

void QGraphsInputHandler::onPinchScaleChanged(qreal delta)
{
    if (!m_zoomEnabled) {
        qCWarning(lcEvents3D, "%s zooming needs to be enabled",
                qUtf8Printable(QLatin1String(__FUNCTION__)));
        return;
    }

    m_pinchDiff += (delta - 1.0f);
    int driftTowardCenterLevel = 175;
    float wheelZoomDrift = 0.1f;

    QQuickGraphsItem *item = m_graphsItem;
    int zoomLevel = int(item->cameraZoomLevel());
    const int minZoomLevel = int(item->minCameraZoomLevel());
    const int maxZoomLevel = int(item->maxCameraZoomLevel());
    float zoomRate = qSqrt(qSqrt(zoomLevel));
    if (m_pinchDiff > .0f)
        zoomLevel += zoomRate;
    else
        zoomLevel -= zoomRate;
    zoomLevel = qBound(minZoomLevel, zoomLevel, maxZoomLevel);
    if (m_zoomAtTarget) {
        QVector3D targetPosition = item->graphPositionAt(
            m_pinchHandler->centroid().position().toPoint());
        item->setCameraZoomLevel(zoomLevel);

        float diffAdj = 0.0f;

        // If zooming in/out outside the graph, or zooming out after certain point,
        // move towards the center.
        if ((qAbs(targetPosition.x()) > 2.0f || qAbs(targetPosition.y()) > 2.0f
             || qAbs(targetPosition.z()) > 2.0f)
            || (m_pinchDiff > .0f && zoomLevel <= driftTowardCenterLevel)) {
            targetPosition = QVector3D();
            // Add some extra correction so that we actually reach the center
            // eventually
            diffAdj = wheelZoomDrift;
            if (m_pinchDiff > .0f)
                diffAdj *= 2.0f; // Correct towards center little more when zooming out
        }

        // Adjust camera towards the zoom point, attempting to keep the cursor at
        // same graph point
        QVector3D oldTarget = item->cameraTargetPosition();
        QVector3D origDiff = targetPosition - oldTarget;
        QVector3D diff = origDiff * m_pinchDiff + (origDiff.normalized() * diffAdj);
        if (diff.length() > origDiff.length())
            diff = origDiff;
        item->setCameraTargetPosition(oldTarget + diff);
    } else {
        item->setCameraZoomLevel(zoomLevel);
    }
    m_pinchDiff = .0f;
}

void QGraphsInputHandler::hoverMoveEvent(QHoverEvent *event)
{
    Q_EMIT mouseMove(event->oldPos());
}