-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathaxishandle.cpp
More file actions
375 lines (321 loc) · 9.27 KB
/
axishandle.cpp
File metadata and controls
375 lines (321 loc) · 9.27 KB
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
/*
* Copyright (c) 2024 Analog Devices Inc.
*
* This file is part of Scopy
* (see https://www.github.com/analogdevicesinc/scopy).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "axishandle.h"
#include "qcoreevent.h"
#include "qpainter.h"
#include <qwt_scale_div.h>
#include <QMouseEvent>
#include <style.h>
using namespace scopy;
AxisHandle::AxisHandle(QwtAxisId axisId, HandlePos handlePos, QwtPlot *plot)
: QWidget(plot->canvas())
, m_axisId(axisId)
, m_plot(plot)
, m_color(Style::getColor(json::theme::content_silent))
, m_handlePos(handlePos)
, m_pos(QPoint(0, 0))
, m_handleMargins(6)
, m_handlePadding(3)
, m_handleSize(17)
, m_isHovering(false)
, m_pressed(false)
, m_bounded(true)
, m_barVisibility(BarVisibility::ALWAYS)
{
init();
}
AxisHandle::~AxisHandle() {}
uint AxisHandle::getLineWidth() { return m_pen.width(); }
void AxisHandle::setLineWidth(int width) { m_pen.setWidth(width); }
QColor AxisHandle::getColor() { return m_color; }
QwtAxisId AxisHandle::getAxisId() { return m_axisId; }
int AxisHandle::getPos() { return m_axisId.isXAxis() ? m_pos.x() : m_pos.y(); }
void AxisHandle::setPosSilent(int pos) { m_axisId.isXAxis() ? m_pos.setX(pos) : m_pos.setY(pos); }
void AxisHandle::setBarVisibility(BarVisibility bar) { m_barVisibility = bar; }
BarVisibility AxisHandle::getBarVisibility() { return m_barVisibility; }
HandlePos AxisHandle::getHandlePos() { return m_handlePos; }
void AxisHandle::setBounded(bool bounded) { m_bounded = bounded; }
bool AxisHandle::isBounded() const { return m_bounded; }
void AxisHandle::setAxis(QwtAxisId axis)
{
m_axisId = axis;
repaint();
}
void AxisHandle::setHandlePos(HandlePos pos)
{
m_handlePos = pos;
updateHandleOrientation();
}
void AxisHandle::setHandle(HandleOrientation orientation)
{
m_handle = QPixmap(":/gui/icons/handle_arrow.svg")
.scaled(m_handleSize, m_handleSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
if(m_handlePos == HandlePos::NORTH_OR_WEST)
m_handle = m_handle.transformed(QTransform().scale(-1, -1));
if(m_axisId.isYAxis())
m_handle = m_handle.transformed(QTransform().rotate(-90));
if(orientation != HandleOrientation::TO_CENTER) {
if(orientation == HandleOrientation::TO_MIN) {
m_handle = m_handle.transformed(QTransform().rotate(-90));
}
if(orientation == HandleOrientation::TO_MAX) {
m_handle = m_handle.transformed(QTransform().rotate(90));
}
if(m_axisId.isYAxis()) {
m_handle = m_handle.transformed(QTransform().rotate(180));
}
if(m_handlePos == HandlePos::NORTH_OR_WEST) {
m_handle = m_handle.transformed(QTransform().rotate(180));
}
}
}
void AxisHandle::init()
{
installEventFilter(this);
m_plot->canvas()->installEventFilter(this);
setAttribute(Qt::WA_TransparentForMouseEvents, false);
m_pen = QPen(m_color, 1, Qt::DashLine);
setHandle();
}
void AxisHandle::setColor(const QColor &color)
{
m_color = color;
m_pen.setColor(color);
repaint();
}
void AxisHandle::setPos(int pos)
{
setPosSilent(pos);
Q_EMIT pixelPosChanged(pos);
repaint();
}
QRect AxisHandle::getBigRect()
{
QRect rect;
int rectWidth = m_handle.width() + m_handleMargins * 2;
int rectHeight = m_handle.height() + m_handleMargins * 2;
if(m_axisId.isXAxis()) {
double x =
std::min(std::max(m_pos.x() - m_handleMargins - m_handle.width() / 2, 0), width() - rectWidth);
rect = QRect(x, m_pos.y() - m_handleMargins * 2, rectWidth, rectHeight);
} else {
double y = std::min(std::max(m_pos.y() - m_handleMargins - m_handle.height() / 2, 0),
height() - rectHeight);
rect = QRect(m_pos.x() - m_handleMargins * 2, y, rectWidth, rectHeight);
}
if(m_handlePos == HandlePos::NORTH_OR_WEST) {
if(m_axisId.isXAxis()) {
int height = rect.height();
rect.setTop(0);
rect.setBottom(height);
} else {
int width = rect.width();
rect.setLeft(0);
rect.setWidth(width);
}
}
return rect;
}
QRect AxisHandle::getSmallRect()
{
QRect rect;
if(m_axisId.isXAxis()) {
double x = std::min(std::max(m_pos.x() - m_handle.width() / 2, 0), width() - m_handle.width());
rect = QRect(x, m_pos.y() - m_handlePadding, m_handle.width(), m_handle.height());
} else {
double y = std::min(std::max(m_pos.y() - m_handle.height() / 2, 0), height() - m_handle.height());
rect = QRect(m_pos.x() - m_handlePadding, y, m_handle.width(), m_handle.height());
}
if(m_handlePos == HandlePos::NORTH_OR_WEST) {
if(m_axisId.isXAxis()) {
int height = rect.height();
rect.setTop(m_handlePadding);
rect.setBottom(height);
} else {
int width = rect.width();
rect.setLeft(m_handlePadding);
rect.setWidth(width);
}
}
return rect;
}
QRect AxisHandle::getRect() { return m_isHovering ? getBigRect() : getSmallRect(); }
QLine AxisHandle::getLine()
{
QLine line;
if(m_axisId.isXAxis()) {
line = QLine(m_pos.x(), height(), m_pos.x(), 0);
} else {
line = QLine(width(), m_pos.y(), 0, m_pos.y());
}
return line;
}
QRect AxisHandle::getRectFromLine(QLine line)
{
QRect rect;
if(m_axisId.isXAxis()) {
rect = QRect(QPoint(line.x1() - m_pen.width(), line.y2()),
QPoint(line.x2() + m_pen.width(), line.y1()));
} else {
rect = QRect(QPoint(line.x2(), line.y1() - m_pen.width()),
QPoint(line.x1(), line.y2() + m_pen.width()));
}
return rect;
}
void AxisHandle::updateHandleOrientation()
{
double x = getBigRect().x();
double y = getBigRect().y();
int rectWidth = getBigRect().width();
int rectHeight = getBigRect().height();
if(m_axisId.isXAxis()) {
if(x == 0) {
setHandle(HandleOrientation::TO_MIN);
} else if(x == width() - rectWidth) {
setHandle(HandleOrientation::TO_MAX);
} else {
setHandle(HandleOrientation::TO_CENTER);
}
} else {
if(y == 0) {
setHandle(HandleOrientation::TO_MIN);
} else if(y == height() - rectHeight) {
setHandle(HandleOrientation::TO_MAX);
} else {
setHandle(HandleOrientation::TO_CENTER);
}
}
}
void AxisHandle::paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.setBrush(m_color);
p.setPen(m_pen);
p.setRenderHint(QPainter::Antialiasing);
QLine line = getLine();
if(m_barVisibility == BarVisibility::ALWAYS || (m_barVisibility == BarVisibility::ON_HOVER && m_isHovering)) {
p.drawLine(line);
setMask(QRegion(getRectFromLine(line)) + QRegion(getBigRect()));
} else {
setMask(QRegion(getBigRect()));
}
QRect rect = getRect();
double rad = 50.;
p.setPen(QPen());
p.drawRoundedRect(rect, rad, rad, Qt::RelativeSize);
updateHandleOrientation();
p.drawPixmap(QPoint(rect.center().x() - m_handle.width() / 2, rect.center().y() - m_handle.height() / 2),
m_handle);
}
void AxisHandle::onMouseMove(QPointF pos)
{
if(m_axisId.isXAxis()) {
if(m_bounded) {
setPos(std::min(m_plot->canvas()->width() - 1., std::max(pos.x(), 0.)));
} else {
setPos(pos.x());
}
} else {
if(m_bounded) {
setPos(std::min(m_plot->canvas()->height() - 1., std::max(pos.y(), 0.)));
} else {
setPos(pos.y());
}
}
}
bool AxisHandle::onMouseButtonPress(QPointF pos)
{
if(getBigRect().contains(pos.toPoint())) {
m_isHovering = true;
m_pressed = true;
setCursor(Qt::ClosedHandCursor);
return true;
}
return false;
}
void AxisHandle::onMouseButtonRelease()
{
if(m_isHovering) {
m_pressed = false;
}
setCursor(Qt::OpenHandCursor);
}
bool AxisHandle::onEnter(QPointF pos)
{
if(getBigRect().contains(pos.toPoint())) {
m_isHovering = true;
repaint();
setCursor(Qt::OpenHandCursor);
return true;
}
return false;
}
void AxisHandle::onLeave()
{
m_isHovering = false;
repaint();
setCursor(Qt::ArrowCursor);
}
void AxisHandle::onResize()
{
setFixedSize(QSize(m_plot->canvas()->size()));
if(m_axisId.isXAxis()) {
m_pos.setY(m_plot->canvas()->height() - m_handle.height());
} else {
m_pos.setX(m_plot->canvas()->width() - m_handle.width());
}
}
void AxisHandle::onDoubleClick(QPointF pos)
{
if(getBigRect().contains(pos.toPoint())) {
if(m_axisId.isXAxis()) {
setPos(m_plot->canvas()->width() / 2);
} else {
setPos(m_plot->canvas()->height() / 2);
}
}
}
bool AxisHandle::eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::MouseButtonRelease) {
onMouseButtonRelease();
} else if(event->type() == QEvent::Leave) {
onLeave();
} else if(event->type() == QEvent::MouseButtonDblClick) {
onDoubleClick(dynamic_cast<QMouseEvent *>(event)->pos());
} else if(event->type() == QEvent::Resize) {
onResize();
} else if(event->type() == QEvent::Enter) {
if(onEnter(dynamic_cast<QEnterEvent *>(event)->pos())) {
return true;
}
} else if(event->type() == QEvent::MouseButtonPress) {
if(onMouseButtonPress(dynamic_cast<QMouseEvent *>(event)->pos())) {
return true;
}
}
if(event->type() == QEvent::MouseMove && m_pressed) {
onMouseMove(dynamic_cast<QMouseEvent *>(event)->pos());
return true;
}
return QObject::eventFilter(object, event);
}
#include "moc_axishandle.cpp"