Newer
Older
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
#include "customplotzoom.h"
CustomPlotZoom::CustomPlotZoom(QWidget * parent)
: QCustomPlot(parent)
, mZoomMode(false)
, mRubberBand(new QRubberBand(QRubberBand::Rectangle, this))
{}
CustomPlotZoom::~CustomPlotZoom()
{
delete mRubberBand;
}
void CustomPlotZoom::setZoomMode(bool mode)
{
mZoomMode = mode;
}
void CustomPlotZoom::mousePressEvent(QMouseEvent * event)
{
if (mZoomMode)
{
if (event->button() == Qt::RightButton)
{
mOrigin = event->pos();
mRubberBand->setGeometry(QRect(mOrigin, QSize()));
mRubberBand->show();
}
}
QCustomPlot::mousePressEvent(event);
}
void CustomPlotZoom::mouseMoveEvent(QMouseEvent * event)
{
if (mRubberBand->isVisible())
{
mRubberBand->setGeometry(QRect(mOrigin, event->pos()).normalized());
}
QCustomPlot::mouseMoveEvent(event);
}
void CustomPlotZoom::mouseReleaseEvent(QMouseEvent * event)
{
if (mRubberBand->isVisible())
{
const QRect & zoomRect = mRubberBand->geometry();
int xp1, yp1, xp2, yp2;
zoomRect.getCoords(&xp1, &yp1, &xp2, &yp2);
auto x1 = xAxis->pixelToCoord(xp1);
auto x2 = xAxis->pixelToCoord(xp2);
auto y1 = yAxis->pixelToCoord(yp1);
auto y2 = yAxis->pixelToCoord(yp2);
xAxis->setRange(x1, x2);
yAxis->setRange(y1, y2);
mRubberBand->hide();
replot();
}
QCustomPlot::mouseReleaseEvent(event);
}