1 #include "widget.h"
2 #include <QSlider>
3 #include <QGridLayout>
4 #include <QFile>
5 #include <QTextStream>
6 #include <QGraphicsScene>
7 #include <math.h>
8
9 MapWidget::MapWidget()
10 {
11 readMap(); //读取地图信息
12
13 zoom=50;
14
15 int width = map.width();
16 int height = map.height();
17 QGraphicsScene *scene = new QGraphicsScene(this);
18 scene->setSceneRect(-width/2,-height/2,width,height);
19 setScene(scene);
20 setCacheMode(CacheBackground);
21
22 //用于地图缩放的滑动条
23 QSlider *slider = new QSlider;
24 slider->setOrientation(Qt::Vertical);
25 slider->setRange(1,100);
26 slider->setTickInterval(10);
27 slider->setValue(50);
28 connect(slider,SIGNAL(valueChanged(int)),this,SLOT(slotZoom(int)));
29
30 QLabel *zoominLabel = new QLabel;
31 zoominLabel->setScaledContents(true);
32 zoominLabel->setPixmap(QPixmap("zoomin.png"));
33
34 QLabel *zoomoutLabel = new QLabel;
35 zoomoutLabel->setScaledContents(true);
36 zoomoutLabel->setPixmap(QPixmap("zoomout.png"));
37
38 //坐标值显示区
39 QLabel *label1 = new QLabel(tr("GraphicsView:"));
40 viewCoord = new QLabel;
41 QLabel *label2 = new QLabel(tr("GraphicsScene:"));
42 sceneCoord = new QLabel;
43 QLabel *label3 = new QLabel(tr("map:"));
44 mapCoord = new QLabel;
45
46 //坐标显示区布局
47 QGridLayout *gridLayout = new QGridLayout;
48 gridLayout->addWidget(label1,0,0);
49 gridLayout->addWidget(viewCoord,0,1);
50 gridLayout->addWidget(label2,1,0);
51 gridLayout->addWidget(sceneCoord,1,1);
52 gridLayout->addWidget(label3,2,0);
53 gridLayout->addWidget(mapCoord,2,1);
54 gridLayout->setSizeConstraint(QLayout::SetFixedSize);
55
56 QFrame *coordFrame = new QFrame;
57 coordFrame->setLayout(gridLayout);
58
59 //缩放控制子布局
60 QVBoxLayout *zoomLayout = new QVBoxLayout;
61 zoomLayout->addWidget(zoominLabel);
62 zoomLayout->addWidget(slider);
63 zoomLayout->addWidget(zoomoutLabel);
64
65 //坐标显示区域布局
66 QVBoxLayout *coordLayout = new QVBoxLayout;
67 coordLayout->addWidget(coordFrame);
68 coordLayout->addStretch();
69
70 //主布局
71 QHBoxLayout *mainLayout = new QHBoxLayout;
72 //滑动
73 mainLayout->addLayout(zoomLayout);
74 //坐标
75 mainLayout->addLayout(coordLayout);
76 mainLayout->addStretch();
77 mainLayout->setMargin(30);
78 mainLayout->setSpacing(10);
79 setLayout(mainLayout);
80
81 setWindowTitle("Map Widget");
82 setMinimumSize(600,400);
83 }
84
85 void MapWidget::readMap() //读取地图信息
86 {
87 QString mapName;
88 QFile mapFile("maps.txt");
89 //打开文件
90 int ok = mapFile.open(QIODevice::ReadOnly);
91 if(ok)
92 {
93 QTextStream ts(&mapFile);
94 if(!ts.atEnd())
95 {
96 ts>>mapName;
97 ts>>x1>>y1>>x2>>y2;
98 }
99 }
100 //载入地图
101 map.load(mapName);
102 }
103
104 void MapWidget::slotZoom(int value) //地图缩放与放大
105 {
106 qreal s;
107 if(value>zoom) //放大
108 {
109 s=pow(1.01,(value-zoom));
110 }
111 else //缩小
112 {
113 s=pow(1/1.01,(zoom-value));
114 }
115 scale(s,s);//放大缩小
116 // rotate(s);
117 zoom = value;
118 }
119
120 //绘制背景
121 void MapWidget::drawBackground(QPainter *painter, const QRectF &rect)
122 {
123 painter->drawPixmap(int(sceneRect().left()),int(sceneRect().top()), map);//绘图
124 }
125
126 void MapWidget::mouseMoveEvent(QMouseEvent *event)
127 {
128 //QGraphicsView 坐标
129 QPoint viewPoint = event->pos();
130 viewCoord->setText(QString::number(viewPoint.x())+","+QString::number(viewPoint.y()));
131
132 //QGraphicsScene 坐标
133 QPointF scenePoint = mapToScene(viewPoint);
134 sceneCoord->setText(QString::number(scenePoint.x())+","+QString::number(scenePoint.y()));
135
136 //地图坐标(经、纬度值)
137 QPointF latLon = mapToMap(scenePoint);
138 mapCoord->setText(QString::number(latLon.x())+","+QString::number(latLon.y()));
139 }
140
141 //用于实现场景坐标系与地图坐标之间的映射,以获得某点的经纬度值
142 QPointF MapWidget::mapToMap(QPointF p)
143 {
144 QPointF latLon;
145 qreal w =sceneRect().width();
146 qreal h =sceneRect().height();
147
148 qreal lon = y1-((h/2+p.y())*abs(y1-y2)/h);
149 qreal lat = x1+((w/2+p.x())*abs(x1-x2)/w);
150
151 latLon.setX(lat);
152 latLon.setY(lon);
153
154 return latLon;
155 }