-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzoomgraphicsview.cpp
More file actions
132 lines (110 loc) · 3.53 KB
/
zoomgraphicsview.cpp
File metadata and controls
132 lines (110 loc) · 3.53 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
// Reference: https://wiki.qt.io/Smooth_Zoom_In_QGraphicsView
#include "zoomgraphicsview.h"
ZoomGraphicsView::ZoomGraphicsView(QWidget *parent) : QGraphicsView(parent)
{
m_numScheduledScalings = 0;
m_currentScale = 1.0;
this->setDragMode(QGraphicsView::NoDrag);
this->setMouseTracking(true);
m_lastMousePos = QPoint(0,0);
}
void ZoomGraphicsView::wheelEvent(QWheelEvent *event)
{
int numDegrees = event->delta() / 8;
int numSteps = numDegrees / 15; // see QWheelEvent documentation
m_numScheduledScalings += numSteps;
/*if (m_numScheduledScalings * numSteps < 0)
{
// if user moved the wheel in another direction, we reset previously scheduled scalings
m_numScheduledScalings = numSteps;
}*/
QTimeLine *anim = new QTimeLine(350, this);
anim->setUpdateInterval(10);
connect(anim, SIGNAL (valueChanged(double)), SLOT (scalingTime(double)));
connect(anim, SIGNAL (finished()), SLOT (animFinished()));
anim->start();
}
QPoint ZoomGraphicsView::pixelPosFromScreen(QPoint pos)
{
// Account for zoom level, find the pixel relative to screen click
int hValue = this->horizontalScrollBar()->value();
int vValue = this->verticalScrollBar()->value();
QPoint pixelPos;
pixelPos.setX(((double)hValue + (double)pos.x()) / m_currentScale);
pixelPos.setY(((double)vValue + (double)pos.y()) / m_currentScale);
return pixelPos;
}
void ZoomGraphicsView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MiddleButton)
{
QApplication::setOverrideCursor(QCursor(Qt::SizeAllCursor));
m_lastMousePos = event->pos();
event->accept();
}
else if (event->button() == Qt::LeftButton)
{
emit previewScreenPressed(pixelPosFromScreen(event->pos()));
}
QGraphicsView::mousePressEvent(event);
}
void ZoomGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
// pan with a middle mouse drag
if (event->buttons() & Qt::MiddleButton)
{
QPoint dPos = event->pos() - m_lastMousePos;
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() - dPos.x());
this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() - dPos.y());
m_lastMousePos = event->pos();
event->accept();
}
else if (event->buttons() & Qt::LeftButton)
{
emit previewScreenPressed(pixelPosFromScreen(event->pos()));
}
QGraphicsView::mouseMoveEvent(event);
}
void ZoomGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
QApplication::restoreOverrideCursor();
}
void ZoomGraphicsView::scalingTime(double x)
{
double factor = 1.0 + double(m_numScheduledScalings) / 500.0;
// Min zoom
if (factor < 1.0 && m_currentScale * factor < 1.001)
{
factor = 1.0 / m_currentScale;
if (factor == 1.0) return;
}
// Max zoom
double maxZoom = 4.0;
if (factor > 1.0 && m_currentScale * factor > maxZoom)
{
factor = maxZoom / m_currentScale;
if (factor == 1.0) return;
}
bool initialScale = (m_currentScale == 1.0);
m_currentScale *= factor;
scale(factor, factor);
if (initialScale)
{
QScrollBar* hBar = this->horizontalScrollBar();
hBar->setValue(hBar->maximum() / 2);
QScrollBar* vBar = verticalScrollBar();
vBar->setValue(vBar->maximum() / 2);
}
}
void ZoomGraphicsView::animFinished()
{
if (m_numScheduledScalings > 0)
{
m_numScheduledScalings--;
}
else
{
m_numScheduledScalings++;
}
sender()->~QObject();
}