summaryrefslogtreecommitdiffstats
path: root/examples/datavisualization/draggableaxes/doc/src/draggableaxes.qdoc
blob: c5b45200331f25bab52b608ff44a9a774a0fed96 (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
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.io
**
** This file is part of the Qt Data Visualization module.
**
** Licensees holding valid commercial license for Qt may use this file in
** accordance with the Qt License Agreement provided with the Software
** or, alternatively, in accordance with the terms contained in a written
** agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.io
**
****************************************************************************/

/*!
    \example draggableaxes
    \title Axis Range Dragging With Labels Example
    \ingroup qtdatavisualization_examples
    \brief Implementing a custom input handler to support axis dragging.
    \since QtDataVisualization 1.1

    The Axis Range Dragging example shows how to customize the 3D graph controls in a widget
    application to allow changing axis ranges by clicking on an axis label and dragging. This is
    done by implementing a custom input handler to react to selection signals emitted from the
    graph.

    \image draggableaxes-example.png

    \section1 Replacing default input handling

    The default input handling mechanism is replaced by setting the active input handler of
    Q3DScatter to \c AxesInputHandler that implements the custom behavior:

    \snippet draggableaxes/data.cpp 0

    \c m_inputHandler was initialized in the constructor:

    \snippet draggableaxes/data.cpp 1

    We will also need the pointers to the axes, so we will pass them to our input handler too:

    \snippet draggableaxes/data.cpp 2

    \section1 Extending mouse event handling

    First of all, we inherited our input handler from Q3DInputHandler instead of
    QAbstract3DInputHandler. The reason for doing this is to keep all the functionality of the
    default input handling, and to add our own functionality on top of it:

    \snippet draggableaxes/axesinputhandler.h 0

    We start extending the default functionality by re-implementing some of the mouse events.
    Let's start with \c {mousePressEvent}. We'll just add button pressed flag for left mouse button
    into it, and keep the rest of the default functionality:

    \snippet draggableaxes/axesinputhandler.cpp 0

    We'll need to modify \c mouseReleaseEvent too to clear the flag and reset the internal state:

    \snippet draggableaxes/axesinputhandler.cpp 1

    Then we'll modify \c {mouseMoveEvent}. Here we check if the \c m_mousePressed is \c true and
    our internal state is something other than \c StateNormal. If so, we'll set the input positions
    for mouse move distance calculations and call the axis dragging function (see
    \l {Implementing axis dragging} for details):

    \snippet draggableaxes/axesinputhandler.cpp 2

    We don't need to change the functionality of mouse wheel, so we will not re-implement that.

    \section1 Implementing axis dragging

    First we need to start listening to the selection signal from the graph. We do that in the
    constructor, and connect it to \c handleElementSelected method:

    \snippet draggableaxes/axesinputhandler.cpp 3

    In \c handleElementSelected we check the type of the selection and set our internal state based on
    it:

    \snippet draggableaxes/axesinputhandler.cpp 4

    The actual dragging logic is implemented in \c handleAxisDragging method, which we call from
    \c mouseMoveEvent in case the required conditions are met:

    \snippet draggableaxes/axesinputhandler.cpp 5

    In \c handleAxisDragging we first get the scene orientation from our active camera:

    \snippet draggableaxes/axesinputhandler.cpp 6

    Then we calculate the modifiers to mouse move direction based on the orientation:

    \snippet draggableaxes/axesinputhandler.cpp 7

    After that, we calculate the mouse movement, and modify it based on the y rotation of the
    camera:

    \snippet draggableaxes/axesinputhandler.cpp 8

    And finally apply the moved distance to the correct axis:

    \snippet draggableaxes/axesinputhandler.cpp 9

    We also have a function for setting the dragging speed:

    \snippet draggableaxes/axesinputhandler.h 1

    This is needed, as the mouse movement distance is absolute (in screen coordinates) and we
    need to adjust it to the axis range. The larger the value, the slower the dragging will be.
    Note that on this example we do not take scene zoom level into account when determining the
    drag speed, so you'll notice changes in the range adjustment as you change the zoom level.

    The modifier could be adjusted automatically based on the axis range and camera zoom level, but
    we'll leave implementing that as an excercise for the reader.

    \section1 Example contents
*/