-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKelosMapView.cpp
111 lines (93 loc) · 2.99 KB
/
KelosMapView.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
#include "KelosMapView.h"
#include <QGraphicsItem>
#include <QWheelEvent>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <iostream>
#include <math.h>
KelosMapView::KelosMapView(QWidget *parent) :
QGraphicsView(parent),
ZoomFactor(1.2f),
m_fZoomValue(1.0f)
{
setResizeAnchor(QGraphicsView::AnchorUnderMouse); // anchor under the cursor
setTransformationAnchor(QGraphicsView::AnchorUnderMouse); // Hacer que el zoom se haga donde está el ratón
this->setViewportUpdateMode(QGraphicsView::ViewportUpdateMode::FullViewportUpdate);
}
KelosMapView::~KelosMapView()
{
}
void KelosMapView::FitScene()
{
this->centerOn(0,0);
fitInView(this->scene()->sceneRect(),Qt::KeepAspectRatio);
m_fZoomValue = matrix().m11();
emit MapZoomChangeSignal(m_fZoomValue);
}
void KelosMapView::CenterMapOnIndicator()
{
// this->centerOn(static_cast<QGraphicsScene*>(this->scene())->GetPositionIndicatorScenePos());
}
void KelosMapView::FitSceneAt(QPointF const& center, QRectF const& rect)
{
this->centerOn(center);
fitInView(rect,Qt::KeepAspectRatio);
m_fZoomValue = matrix().m11();
emit MapZoomChangeSignal(m_fZoomValue);
}
void KelosMapView::wheelEvent(QWheelEvent* event)
{
int numSteps = event->delta() / 15 / 8;
if (numSteps == 0) {
event->ignore();
return;
}
float fFactor = pow(ZoomFactor, numSteps);
QTransform NewTransform = this->transform();
NewTransform.scale(fFactor, fFactor);
float fZoom = NewTransform.m11();
// Limitar el zoom
if ( fZoom >= 0.01 && fZoom < 100.0)
{
this->hide();
this->resetTransform();
this->setTransform(NewTransform);
this->show(); // AS: Prevenir artifacts
m_fZoomValue = fZoom ;
emit MapZoomChangeSignal(m_fZoomValue);
}
event->accept();
}
void KelosMapView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton)
{
setDragMode(QGraphicsView::DragMode::ScrollHandDrag);
QMouseEvent fake(event->type(), event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers());
QGraphicsView::mousePressEvent(&fake);
}
else if (event->button() == Qt::LeftButton && event->modifiers() & Qt::KeyboardModifier::ShiftModifier)
{
setDragMode(QGraphicsView::DragMode::RubberBandDrag); // Importante el orden.. tras el evento!
QGraphicsView::mousePressEvent(event);
}
else
QGraphicsView::mousePressEvent(event);
}
void KelosMapView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton)
{
QMouseEvent fake(event->type(), event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers());
QGraphicsView::mousePressEvent(&fake);
setCursor(Qt::ArrowCursor);
setDragMode(QGraphicsView::DragMode::NoDrag); // Importante el orden.. tras el evento!
}
else if (event->button() == Qt::LeftButton && event->modifiers().testFlag(Qt::KeyboardModifier::ShiftModifier) )
{
QGraphicsView::mouseReleaseEvent(event);
setDragMode(QGraphicsView::DragMode::NoDrag); // Importante el orden.. tras el evento!
}
else
QGraphicsView::mouseReleaseEvent(event);
}