-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmygraphicsview.cpp
127 lines (96 loc) · 3.59 KB
/
mygraphicsview.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
//Our includes
#include "MyGraphicsView.h"
//Qt includes
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QTextStream>
#include <QScrollBar>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QDebug>
#include <QCoreApplication>
MyGraphicsView::MyGraphicsView(QWidget* parent) : QGraphicsView(parent)
{
diffX=0;
hasMouseTracking();
}
void MyGraphicsView::SetCenter(const QPointF& centerPoint) {
//Get the rectangle of the visible area in scene coords
QRectF visibleArea = mapToScene(rect()).boundingRect();
//Get the scene area
QRectF sceneBounds = sceneRect();
double boundX = visibleArea.width() / 2.0;
double boundY = visibleArea.height() / 2.0;
double boundWidth = sceneBounds.width() - 2.0 * boundX;
double boundHeight = sceneBounds.height() - 2.0 * boundY;
//The max boundary that the centerPoint can be to
QRectF bounds(boundX, boundY, boundWidth, boundHeight);
if(bounds.contains(centerPoint)) {
//We are within the bounds
CurrentCenterPoint = centerPoint;
} else {
//We need to clamp or use the center of the screen
if(visibleArea.contains(sceneBounds)) {
//Use the center of scene ie. we can see the whole scene
CurrentCenterPoint = sceneBounds.center();
} else {
CurrentCenterPoint = centerPoint;
//We need to clamp the center. The centerPoint is too large
if(centerPoint.x() > bounds.x() + bounds.width()) {
CurrentCenterPoint.setX(bounds.x() + bounds.width());
} else if(centerPoint.x() < bounds.x()) {
CurrentCenterPoint.setX(bounds.x());
}
if(centerPoint.y() > bounds.y() + bounds.height()) {
CurrentCenterPoint.setY(bounds.y() + bounds.height());
} else if(centerPoint.y() < bounds.y()) {
CurrentCenterPoint.setY(bounds.y());
}
}
}
//Update the scrollbars
centerOn(CurrentCenterPoint);
}
void MyGraphicsView::mousePressEvent(QMouseEvent* event)
{
setCursor(Qt::ClosedHandCursor);
QCoreApplication::sendEvent(parentWidget(), event);
LastPanPoint = event->pos();
}
void MyGraphicsView::mouseReleaseEvent(QMouseEvent* event)
{
setCursor(Qt::OpenHandCursor);
QCoreApplication::sendEvent(parentWidget(), event);
LastPanPoint = QPoint();
}
void MyGraphicsView::mouseMoveEvent(QMouseEvent* event) {
if(!LastPanPoint.isNull()) {
EventPos=event->pos();
diffX = (LastPanPoint.x() - event->pos().x());
diffY = (LastPanPoint.y() - event->pos().y());
QCoreApplication::sendEvent(parentWidget(), event);
LastPanPoint = event->pos();
}
}
void MyGraphicsView::wheelEvent(QWheelEvent* event) {
//Get the position of the mouse before scaling, in scene coords
QPointF pointBeforeScale(mapToScene(event->pos()));
//Get the original screen centerpoint
QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());
//Scale the view ie. do the zoom
double scaleFactor = 1.15; //How fast we zoom
if(event->delta() > 0) {
//Zoom in
scale(scaleFactor, scaleFactor);
} else {
//Zooming out
scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}
//Get the position after scaling, in scene coords
QPointF pointAfterScale(mapToScene(event->pos()));
//Get the offset of how the screen moved
QPointF offset = pointBeforeScale - pointAfterScale;
//Adjust to the new center for correct zooming
QPointF newCenter = screenCenter + offset;
SetCenter(newCenter);
}