-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqcpl_cursor.cpp
183 lines (161 loc) · 4.92 KB
/
qcpl_cursor.cpp
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
#include "qcpl_cursor.h"
#include <QApplication>
namespace QCPL {
Cursor::Cursor(QCustomPlot *plot) : QCPGraph(plot->xAxis, plot->yAxis)
{
setAntialiased(false);
setPen(QPen(QColor::fromRgb(80, 80, 255))); // TODO make customizable
connect(plot, SIGNAL(emptySpaceDoubleClicked(QMouseEvent*)), this, SLOT(mouseDoubleClick(QMouseEvent*)));
connect(plot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress(QMouseEvent*)));
connect(plot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseRelease(QMouseEvent*)));
connect(plot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
removeFromLegend();
setPosition(0, 0);
auto axesLayer = plot->layer(QStringLiteral("axes"));
if (axesLayer) setLayer(axesLayer);
}
void Cursor::setVisible(bool on)
{
QCPGraph::setVisible(on);
parentPlot()->replot();
}
void Cursor::setShape(CursorShape value)
{
_shape = value;
parentPlot()->replot();
}
void Cursor::mouseDoubleClickEvent(QMouseEvent *event, const QVariant &details)
{
Q_UNUSED(details)
_followMouse = false;
setPixelPosition(event->pos());
}
void Cursor::mouseDoubleClick(QMouseEvent *evt)
{
setPixelPosition(evt->pos());
}
void Cursor::mousePress(QMouseEvent *evt)
{
if (!visible()) return;
if (evt->button() == Qt::LeftButton)
{
if (_canDragX) _dragX = true;
if (_canDragY) _dragY = true;
}
}
void Cursor::mouseRelease(QMouseEvent*)
{
if (!visible()) return;
_dragX = false;
_dragY = false;
#ifdef Q_OS_MAC
parentPlot()->skipDragging = false;
#endif
}
void Cursor::mouseMove(QMouseEvent *evt)
{
if (!visible()) return;
if (_followMouse)
{
setPixelPosition(evt->pos());
return;
}
else
{
if (_dragX || _dragY)
{
if (_dragX && _dragY)
{
setPixelPosition(evt->pos());
}
else if (_dragX)
{
double cursorX, cursorY;
pixelPosition(cursorX, cursorY);
setPixelPosition(evt->pos().x(), cursorY);
}
else
{
double cursorX, cursorY;
pixelPosition(cursorX, cursorY);
setPixelPosition(cursorX, evt->pos().y());
}
}
else
{
double mouseX = evt->pos().x();
double mouseY = evt->pos().y();
double cursorX, cursorY;
pixelPosition(cursorX, cursorY);
QCPAxisRect *r = parentPlot()->axisRect();
_canDragX = (_shape == VerticalLine || _shape == CrossLines)
&& mouseY > r->top()
&& mouseY < r->bottom() && qAbs(cursorX - mouseX) <= 2;
_canDragY = (_shape == HorizontalLine || _shape == CrossLines)
&& mouseX > r->left()
&& mouseX < r->right() && qAbs(cursorY - mouseY) <= 2;
#ifdef Q_OS_MAC
parentPlot()->skipDragging = _canDragX or _canDragY;
#endif
QApplication::restoreOverrideCursor();
if (_canDragX && _canDragY)
QApplication::setOverrideCursor(Qt::SizeAllCursor);
else if (_canDragX)
QApplication::setOverrideCursor(Qt::SizeHorCursor);
else if (_canDragY)
QApplication::setOverrideCursor(Qt::SizeVerCursor);
}
}
}
void Cursor::draw(QCPPainter *painter)
{
painter->setPen(mPen);
painter->setBrush(Qt::NoBrush);
double x, y;
pixelPosition(x, y);
QCPAxisRect *r = parentPlot()->axisRect();
if (_shape == HorizontalLine || _shape == CrossLines)
painter->drawLine(QPointF(r->left(), y), QPointF(r->right(), y));
if (_shape == VerticalLine || _shape == CrossLines)
painter->drawLine(QPointF(x, r->bottom()), QPointF(x, r->top()));
}
QPointF Cursor::position() const
{
auto point = data()->constBegin();
return QPointF(point->key, point->value);
}
void Cursor::setPosition(const double& x, const double& y, bool replot)
{
setData(QVector<double>() << x, QVector<double>() << y);
if (replot)
parentPlot()->replot();
emit positionChanged();
}
void Cursor::pixelPosition(double& x, double& y) const
{
QPointF point = position();
coordsToPixels(point.x(), point.y(), x, y);
}
void Cursor::setPixelPosition(const double &x, const double &y, bool replot)
{
double key, value;
pixelsToCoords(x, y, key, value);
setPosition(key, value, replot);
}
void Cursor::moveToCenter(bool replot)
{
QCPAxisRect *r = parentPlot()->axisRect();
setPixelPosition((r->right() + r->left())/2, (r->top() + r->bottom())/2, replot);
}
void Cursor::setFollowMouse(bool value)
{
_followMouse = value;
if (_followMouse)
{
_dragX = _canDragX = false;
_dragY = _canDragY = false;
QApplication::restoreOverrideCursor();
setPixelPosition(parentPlot()->mapFromGlobal(QCursor::pos()));
}
}
} // namespace QCPL