source: ntrip/trunk/BNC/bncmap.cpp@ 3762

Last change on this file since 3762 was 3288, checked in by weber, 13 years ago

Scaling site distribution map from caster sourcetable contents

File size: 5.9 KB
Line 
1// ------------
2// author: jan dousa (jan.dousa@pecny.cz)
3// ------------
4
5#include <iostream>
6#include "bncmap.h"
7
8
9// ------------
10bncMap::bncMap(QWidget* parent) : QDialog(parent)
11{
12 _LaOff = 25; // shift longitude
13 _mapScen = new QGraphicsScene();
14 _mapView = new BncMapView();
15 _mapView->setScene(_mapScen);
16 _mapView->resetScale();
17 slotReadMap();
18 slotCreateMap();
19 _mapScen->setSceneRect(QRect(0,-90,360,180));
20
21 setWindowTitle(tr("Source-Table Map [*]"));
22
23 /* close button */
24 QPushButton* buttClose = new QPushButton("Close");
25 connect(buttClose, SIGNAL(clicked()), this, SLOT(close()));
26
27 /* rescale button */
28// QPushButton* buttClean = new QPushButton("Clean");
29// connect(buttClean, SIGNAL(clicked()), this, SLOT(slotCleanMap()));
30
31 /* zoom button */
32 QPushButton* buttZoomIn = new QPushButton("Zoom +");
33 connect(buttZoomIn, SIGNAL(clicked()), this, SLOT(slotZoomIn()));
34
35 /* zoom button */
36 QPushButton* buttZoomOut = new QPushButton("Zoom -");
37 connect(buttZoomOut, SIGNAL(clicked()), this, SLOT(slotZoomOut()));
38
39 /* reset button */
40 QPushButton* buttReset = new QPushButton("World");
41 connect(buttReset, SIGNAL(clicked()), this, SLOT(slotResetMap()));
42
43 /* fit button */
44 QPushButton* buttFit = new QPushButton("Fit Map");
45 connect(buttFit, SIGNAL(clicked()), this, SLOT(slotFitMap()));
46
47 /* font reset button */
48 QPushButton* buttFont = new QPushButton("Fit Font");
49 connect(buttFont, SIGNAL(clicked()), this, SLOT(slotFitFont()));
50
51 /* layout */
52 QVBoxLayout* MapLayout = new QVBoxLayout;
53 QHBoxLayout* ButLayout = new QHBoxLayout;
54
55 ButLayout->addWidget(buttZoomIn);
56 ButLayout->addWidget(buttZoomOut);
57// ButLayout->addWidget(buttClean);
58 ButLayout->addWidget(buttReset);
59 ButLayout->addWidget(buttFit);
60 ButLayout->addWidget(buttFont);
61 ButLayout->addWidget(buttClose);
62
63 MapLayout->addWidget(_mapView);
64 MapLayout->addLayout(ButLayout);
65
66 setLayout(MapLayout);
67
68 this->show();
69}
70
71
72// ------------
73bncMap::~bncMap(){
74 delete _mapView;
75}
76
77
78// ------------
79void bncMap::slotReadMap()
80{
81 QFile world(":worldmap.dat");
82 float fi, la;
83
84 world.open(QIODevice::ReadOnly | QIODevice::Text);
85
86 QTextStream in(&world);
87 in.setRealNumberNotation(QTextStream::FixedNotation);
88
89 while( ! in.atEnd() ){
90
91 in >> la >> fi;
92
93 // la = 0-360
94 while( la < 0 ){ la += 360; }
95 while( la >= 360 ){ la -= 360; }
96
97 // fi opposite
98 _worldMap << QPointF( la, -fi );
99
100 }
101 world.close();
102}
103
104
105// ------------
106void bncMap::slotCreateMap()
107{
108 // _mapScen->setForegroundBrush(QBrush(Qt::lightGray, Qt::CrossPattern)); // grid
109
110 int begIdx = 0;
111 int endIdx = 0;
112 for( int i=0; i < _worldMap.size(); i++ ){
113 if( _worldMap.at(i).x() == 0.0 and _worldMap.at(i).y() == 0.0 ){
114 if( i > 0 ){
115 endIdx = i-1;
116 while( begIdx < endIdx ){
117
118 int l1 = 0;
119 int l2 = 0;
120
121 float la1 = _worldMap.at(begIdx+0).x() + _LaOff;
122 float fi1 = _worldMap.at(begIdx+0).y();
123 float la2 = _worldMap.at(begIdx+1).x() + _LaOff;
124 float fi2 = _worldMap.at(begIdx+1).y();
125 begIdx++;
126
127 while( la1 < 0 ){ la1 += 360; l1++; }
128 while( la1 >= 360 ){ la1 -= 360; l1--; }
129 while( la2 < 0 ){ la2 += 360; l2++; }
130 while( la2 >= 360 ){ la2 -= 360; l2--; }
131
132 if( l1 != 0 and l2 == 0 ){ continue; } // break this line
133 if( l2 != 0 and l1 == 0 ){ continue; } // break this line
134
135 _mapScen->addLine(la1, fi1, la2, fi2, QPen(QBrush(Qt::gray),0.3));
136 }
137 }
138 if( i+1 < _worldMap.size() ) begIdx = i+1;
139 }
140 }
141}
142
143
144// ------------
145void bncMap::slotCleanMap()
146{
147 QMutexLocker locker(&_mutexMap);
148 _mapScen->clear();
149 slotCreateMap();
150 slotResetMap();
151 slotFitFont();
152}
153
154
155// ------------
156void bncMap::slotResetMap()
157{
158 _mapView->resetScale();
159}
160
161
162// ------------
163void bncMap::slotFitMap()
164{
165 QRectF reg = _allPoints.boundingRect().adjusted(-10,-10,10,10);
166
167 _mapView->resetScale();
168 _mapView->updateSceneRect(reg);
169 _mapView->centerOn(reg.center());
170 _mapView->fitInView(reg,Qt::KeepAspectRatio);
171
172 slotFitFont();
173}
174
175
176// ------------
177void bncMap::slotFitFont()
178{
179 _mapScen->clear();
180 slotCreateMap();
181
182 float fontsize = _mapView->scale_rate();
183 float pointsize = _mapView->scale_rate();
184
185 QMapIterator<QString, QList<QVariant> > it(_allNames);
186 while( it.hasNext() ){
187
188 it.next();
189 QString name = it.key();
190 QList<QVariant> tmp = it.value();
191
192 double la = tmp.at(0).toPointF().x();
193 double fi = tmp.at(0).toPointF().y();
194 QPen pen = tmp.at(1).value<QPen>();
195 double basPT = tmp.at(2).toDouble();
196 double basFT = tmp.at(3).toDouble();
197
198 float tmpPT = pointsize * basPT;
199 float tmpFT = fontsize * basFT;
200
201 QFont font(QFont("Arial", 2, 1));
202 font.setPointSizeF( tmpFT );
203
204 QGraphicsTextItem* nameItem = new QGraphicsTextItem( name );
205 nameItem->setFont( font );
206
207 if( floor(fontsize) < 1 ){
208 nameItem->setPos( la - 4.0 - floor(fontsize), fi - 5.0 - floor(fontsize) );
209 }else{
210 nameItem->setPos( la - 1.0 - floor(fontsize), fi - 4.0 - floor(fontsize) );
211 }
212
213 if( tmpPT < 0.25 ) tmpPT = 0.25;
214 pen.setWidthF(tmpPT);
215
216 _mapScen->addItem( nameItem );
217 _mapScen->addEllipse( la, fi, tmpPT, tmpPT, pen );
218 }
219 _mapView->zoom( 1.0 );
220}
221
222
223// ------------
224void bncMap::slotZoomIn()
225{
226 _mapView->zoom( 1.2 );
227}
228
229
230// ------------
231void bncMap::slotZoomOut()
232{
233 _mapView->zoom( 1/1.2 );
234}
235
236
237// ------------
238void bncMap::slotNewPoint(QPointF point, QString name, QPen pen, double size)
239{
240 float la = point.x() + _LaOff;
241 float fi = - point.y();
242
243 while( la < 0 ){ la += 360; }
244 while( la >= 360 ){ la -= 360; }
245
246 QPointF tmppoint(la,fi);
247 _allPoints << tmppoint;
248
249 if( ! name.isEmpty() ){
250
251 QList<QVariant> tmp;
252 tmp << tmppoint // QPoint
253 << pen // QPen
254 << size << 4.5; // base pointsize, fontsize
255 _allNames.insert( name, tmp );
256 }
257}
Note: See TracBrowser for help on using the repository browser.