source: ntrip/branches/BNC_LM/bncmapview.cpp@ 10583

Last change on this file since 10583 was 3408, checked in by mervart, 13 years ago
File size: 4.5 KB
Line 
1
2#include <QGraphicsScene>
3#include <QGraphicsTextItem>
4#include <QTextStream>
5#include <QScrollBar>
6#include <QMouseEvent>
7#include <QWheelEvent>
8#include <QDebug>
9#include <iostream>
10
11#include "bncmapview.h"
12
13
14// -------------
15BncMapView::BncMapView(QWidget* parent) : QGraphicsView(parent)
16{
17 setCursor(Qt::OpenHandCursor);
18 setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
19 resetScale();
20}
21
22/* -------------
23 * Sets the current centerpoint. Also updates the scene's center point.
24 * Unlike centerOn, which has no way of getting the floating point center
25 * back, SetCenter() stores the center point. It also handles the special
26 * sidebar case. This function will claim the centerPoint to sceneRec ie.
27 * the centerPoint must be within the sceneRec.
28 */
29void BncMapView::SetCenter(const QPointF& centerPoint)
30{
31 // get the rectangle of the visible area in scene coords
32 QRectF visibleArea = mapToScene(rect()).boundingRect();
33
34 // get the scene area
35 QRectF sceneBounds = sceneRect();
36
37 double boundX = + visibleArea.width() / 2.0;
38 double boundY = - visibleArea.height() / 2.0; // opposite sign for latitude !
39
40 double boundWidth = sceneBounds.width() - boundX * 2.0;
41 double boundHeight = sceneBounds.height() - boundY * 2.0;
42
43 // the max boundary that the centerPoint can be to
44 QRectF bounds(boundX, boundY, boundWidth, boundHeight);
45
46 if( bounds.contains(centerPoint) ){
47 // we are within the bounds
48 _currentCenterPoint = centerPoint;
49
50 }else{
51
52 // we need to clamp or use the center of the screen
53 if( visibleArea.contains(sceneBounds) ){
54
55 // use the center of scene ie. we can see the whole scene
56 _currentCenterPoint = sceneBounds.center();
57
58 }else{
59 _currentCenterPoint = centerPoint;
60
61 // we need to clamp the center. The centerPoint is too large
62 if( centerPoint.x() > bounds.x() + bounds.width() ){
63 _currentCenterPoint.setX(bounds.x() + bounds.width());
64
65 }else if( centerPoint.x() < bounds.x() ){
66 _currentCenterPoint.setX(bounds.x());
67 }
68
69 // opposite sign for latitude !
70 if( centerPoint.y() < - bounds.y() - bounds.height() ){
71 _currentCenterPoint.setY( - bounds.y() - bounds.height());
72
73 }else if( centerPoint.y() > - bounds.y() ){
74 _currentCenterPoint.setY( - bounds.y());
75 }
76 }
77 }
78 // update the scrollbars
79 centerOn(_currentCenterPoint);
80}
81
82
83// -------------
84void BncMapView::mousePressEvent(QMouseEvent* event)
85{
86// std::cout << " PRES " << event->pos().x() << " " << event->pos().y() << std::endl;
87 _lastPanPoint = event->pos();
88 setCursor(Qt::ClosedHandCursor);
89}
90
91
92// -------------
93void BncMapView::mouseReleaseEvent(QMouseEvent* /* event */)
94{
95 setCursor(Qt::OpenHandCursor);
96 _lastPanPoint = QPoint();
97}
98
99
100// -------------
101void BncMapView::mouseMoveEvent(QMouseEvent* event)
102{
103 if( !_lastPanPoint.isNull() ){
104
105 // get how much we panned
106 QPointF delta = mapToScene(_lastPanPoint) - mapToScene(event->pos());
107 _lastPanPoint = event->pos();
108
109 // update the center ie. do the pan
110 SetCenter(GetCenter() + delta);
111 }
112}
113
114
115// -------------
116void BncMapView::wheelEvent(QWheelEvent* event)
117{
118 // het the position of the mouse before scaling, in scene coords
119 QPointF pointBeforeScale(mapToScene(event->pos()));
120
121 // get the original screen centerpoint
122 QPointF screenCenter = GetCenter();
123
124 // scale the view ie. do the zoom
125 double scaleFactor = 1.10; // how fast we zoom
126 if( event->delta() > 0 ){
127
128 // zooming in
129 zoom( scaleFactor );
130
131 }else{
132
133 // zooming out
134 zoom( 1.0/scaleFactor );
135 }
136
137 // get the position after scaling, in scene coords
138 QPointF pointAfterScale(mapToScene(event->pos()));
139
140 // get the offset of how the screen moved
141 QPointF offset = pointBeforeScale - pointAfterScale;
142
143 // adjust to the new center for correct zooming
144 QPointF newCenter = screenCenter + offset;
145 SetCenter(newCenter);
146}
147
148
149// -------------
150void BncMapView::resizeEvent(QResizeEvent* event)
151{
152 // get the rectangle of the visible area in scene coords
153 QRectF visibleArea = mapToScene(rect()).boundingRect();
154 SetCenter(visibleArea.center());
155
156 // call the subclass resize so the scrollbars are updated correctly
157 QGraphicsView::resizeEvent(event);
158}
159
160
161// -------------
162void BncMapView::resetScale()
163{
164 _scale = _scCur = 2.0;
165 setMatrix(QMatrix(_scale,0,0,_scale,0,0));
166}
167
168
169// -------------
170void BncMapView::zoom(qreal scale)
171{
172 QGraphicsView::scale( scale, scale );
173 _scCur = _scCur * scale;
174}
Note: See TracBrowser for help on using the repository browser.