forked from utat-uav/Software
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomview.cpp
More file actions
120 lines (93 loc) · 2.55 KB
/
customview.cpp
File metadata and controls
120 lines (93 loc) · 2.55 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
#include "customview.h"
#include <QMouseEvent>
#include <QScrollBar>
#include <QDebug>
#define VIEW_CENTER viewport()->rect().center()
#define VIEW_WIDTH viewport()->rect().width()
#define VIEW_HEIGHT viewport()->rect().height()
CustomView::CustomView(QWidget *parent)
: QGraphicsView(parent)
{
QGraphicsScene *scene = new QGraphicsScene(0,0,1000,1000);
this->setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setDragMode(DragMode::ScrollHandDrag);
setMaxSize();
centerOn(0, 0);
zoomDelta = 0.1;
panSpeed = 1.0;
_doMousePanning = false;
_scale = 1.0;
panButton = Qt::LeftButton;
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
}
CustomView::~CustomView()
{
}
void CustomView::setMaxSize()
{
setSceneRect(-1e10, -1e10, 2e10, 2e10);
}
void CustomView::wheelEvent(QWheelEvent *event)
{
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
QPoint scrollAmount = event->angleDelta();
// Apply zoom.
scrollAmount.y() > 0 ? zoomIn() : zoomOut();
}
void CustomView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == panButton)
{
_lastMousePos = event->pos();
_doMousePanning = true;
}
QGraphicsView::mousePressEvent(event);
}
void CustomView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == panButton)
{
_doMousePanning = false;
}
QGraphicsView::mouseReleaseEvent(event);
}
void CustomView::mouseMoveEvent(QMouseEvent *event)
{
if (_doMousePanning)
{
QPointF mouseDelta = mapToScene(event->pos()) - mapToScene(_lastMousePos);
//pan(mouseDelta);
}
QPointF point = mapToScene(event->pos());
emit mouseMoved(point);
QGraphicsView::mouseMoveEvent(event);
_lastMousePos = event->pos();
}
void CustomView::zoomIn()
{
zoom(1 + zoomDelta);
}
void CustomView::zoomOut()
{
zoom(1 - zoomDelta);
}
void CustomView::zoom(float scaleFactor)
{
scale(scaleFactor, scaleFactor);
_scale *= scaleFactor;
}
void CustomView::pan(QPointF delta)
{
// Scale the pan amount by the current zoom.
delta *= _scale;
delta *= panSpeed;
qDebug() << delta;
// Have panning be anchored from the mouse.
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
QPoint newCenter(VIEW_WIDTH / 2 - delta.x(), VIEW_HEIGHT / 2 - delta.y());
centerOn(mapToScene(newCenter));
// For zooming to anchor from the view center.
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
}