forked from myshak/ss13telesmap
/
mapview.cpp
80 lines (63 loc) · 1.82 KB
/
mapview.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
#include "mapview.h"
#include <QtGlobal>
#include <QWheelEvent>
MapView::MapView(QWidget *parent) :
QGraphicsView(parent),
m_currentZoom(1.0f),
m_minZoom(1.0f),
m_maxZoom(2.0f)
{
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
}
void MapView::wheelEvent(QWheelEvent* event)
{
// Get the number of steps of the wheel
int numDegrees = event->delta() / 8;
int numSteps = numDegrees / 15;
// Do nothing if the user is trying to zoom past the limits
if((m_currentZoom <= m_minZoom && numSteps <= 0) || (m_currentZoom >= m_maxZoom && numSteps >= 0)) {
return;
}
m_currentZoom += 0.25 * numSteps;
if(m_currentZoom <= m_minZoom) {
m_currentZoom = m_minZoom;
}
if(m_currentZoom >= m_maxZoom) {
m_currentZoom = m_maxZoom;
}
// Reset the zoom, so we can specify an absolute scale factor instead of relative to the current scale
resetMatrix();
scale(m_currentZoom, m_currentZoom);
event->accept();
}
void MapView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
calculateZoom();
if(m_currentZoom < m_minZoom) {
m_currentZoom = m_minZoom;
}
resetMatrix();
scale(m_currentZoom, m_currentZoom);
}
void MapView::calculateZoom()
{
if(scene()) {
int max_size = height();
// The minimum zoom should give us no empty space outside the scene
if(max_size < width()) {
max_size = width();
m_minZoom = static_cast<float>(max_size)/scene()->width();
} else {
m_minZoom = static_cast<float>(max_size)/scene()->height();
}
}else{
m_minZoom = 1.0f;
}
}
void MapView::resetZoom()
{
m_currentZoom = 1.0f;
resetMatrix();
}