source: flair-src/trunk/tools/FlairGCS/src/Map.cpp@ 9

Last change on this file since 9 was 9, checked in by Sanahuja Guillaume, 7 years ago

gcs

File size: 9.6 KB
Line 
1#include "Map.h"
2#include "Landmark.h"
3#include "Layout.h"
4#include "mapwidget.h"
5#include <QNetworkProxy>
6#include <QFormLayout>
7#include <QInputDialog>
8#include <QtGlobal>
9#include <QMenu>
10#include <QGeoMappingManager>
11#include <QGeoServiceProvider>
12#include <QComboBox>
13#include <QToolButton>
14#include <QCoreApplication>
15#include <qendian.h>
16
17using namespace QtMobility;
18
19Map::Map(Layout* parent,int row, int col,QString name,QList<QGeoCoordinate> coordinates,bool enabled,int period):
20 DataRemote(name,"Map",parent,enabled,period)
21{
22 visible_widget=new QWidget();
23 visible_widget->setObjectName(name);
24 centeredpoint=0;
25
26 mapTypes[QGraphicsGeoMap::StreetMap] = tr("Street map");
27 mapTypes[QGraphicsGeoMap::SatelliteMapDay] = tr("Satellite map (day)");
28 mapTypes[QGraphicsGeoMap::SatelliteMapNight] = tr("Satellite map (night)");
29 mapTypes[QGraphicsGeoMap::TerrainMap] = tr("Terrain map");
30
31 mapWidget = new MapWidget(this,visible_widget);
32 geoMap = 0;
33
34
35 QVBoxLayout *map_layout = new QVBoxLayout();
36 parent->getQGridLayout()->addLayout(map_layout,row,col);
37 QHBoxLayout *tool_layout = new QHBoxLayout();
38 map_layout->addLayout(tool_layout,0);
39
40 QFormLayout* servicelayout = new QFormLayout();
41 serviceCombo = new QComboBox(visible_widget);
42 servicelayout->addRow("service: ",serviceCombo);
43 QFormLayout* maplayout = new QFormLayout();
44 mapTypeCombo = new QComboBox(visible_widget);
45 maplayout->addRow("map type: ",mapTypeCombo);
46 QFormLayout* proxylayout = new QFormLayout();
47 proxyCombo = new QComboBox(visible_widget);
48 proxylayout->addRow("proxy: ",proxyCombo);
49
50 if(findServices())
51 {
52 tool_layout->addLayout(servicelayout,0);
53 tool_layout->addLayout(maplayout,1);
54 tool_layout->addLayout(proxylayout,2);
55
56 zoomin=new QToolButton(visible_widget);
57 zoomout=new QToolButton(visible_widget);
58 zoomInAction = new QAction(QIcon(QPixmap(":zoomin.png")), tr("Zoom &In"),zoomin);
59 zoomOutAction = new QAction(QIcon(QPixmap(":zoomout.png")), tr("Zoom &Out"),zoomout);
60 zoomin->setDefaultAction(zoomInAction);
61 zoomout->setDefaultAction(zoomOutAction);
62 tool_layout->addWidget(zoomin,3);
63 tool_layout->addWidget(zoomout,4);
64 connect(zoomInAction, SIGNAL(triggered()), this, SLOT(zoomIn()));
65 connect(zoomOutAction, SIGNAL(triggered()), this, SLOT(zoomOut()));
66
67 selectService(0);
68
69 findproxy();
70
71 connect(serviceCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(selectService(int)));
72 connect(mapTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeMapType(int)));
73 connect(proxyCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeproxy(int)));
74 }
75 else
76 {
77 printf("Error, no services!\n");
78 mapWidget->setEnabled(false);
79 }
80
81 map_layout->addWidget(mapWidget,1);
82 for(int i=0;i<coordinates.size();i++) {
83 mapWidget->AddLandmark((QGeoCoordinate &)coordinates.at(i));
84 }
85}
86
87Map::~Map()
88{
89}
90
91bool Map::findServices(void)
92{
93 QCoreApplication::addLibraryPath(qgetenv("OECORE_HOST_SYSROOT") + "/usr/lib/qt4/");
94
95 QStringList services = QGeoServiceProvider::availableServiceProviders();
96
97 if (services.isEmpty())
98 {
99 QGraphicsTextItem *item = mapWidget->scene()->addText(
100 tr("Failed to find any map services. Please ensure that "
101 "the location services plugins for Qt Mobility are "
102 "installed and, if necessary, set the IGEP_ROOT "
103 "environment variable to the good location."));
104
105 item->setTextWidth(300);
106
107 return false;
108 }
109
110 QMap<QString, QVariant> parameters;
111
112 foreach (QString name, services)
113 {
114
115 parameters["mapping.cache.directory"] = name;
116 parameters["mapping.cache.size"] = QString("500000000");
117
118 QGeoServiceProvider *service = new QGeoServiceProvider(name,parameters);
119
120 if (service->error() != QGeoServiceProvider::NoError) {
121 delete service;
122 continue;
123 }
124
125 QGeoMappingManager *manager = service->mappingManager();
126
127 if (manager)
128 {
129 serviceCombo->addItem(name);
130 managers.append(manager);
131 }
132 }
133
134 return true;
135}
136
137void Map::selectService(int item)
138{
139 QString name = serviceCombo-> currentText ();
140
141 QGeoCoordinate coordinate = QGeoCoordinate(0, 0);
142 QGraphicsGeoMap::MapType mapType = QGraphicsGeoMap::StreetMap;
143 int zoomLevel = 1;
144
145 if (geoMap) {
146 coordinate = geoMap->center();
147 mapType = geoMap->mapType();
148 zoomLevel = geoMap->zoomLevel();
149
150 mapWidget->RemoveLandmarks();
151 RemovePoints();
152 geoMap->deleteLater();
153 }
154
155 manager = managers[item];
156 geoMap = new QGraphicsGeoMap(manager);
157
158 mapWidget->AddLandmarks(geoMap);
159 AddPoints(geoMap);
160 geoMap->setCenter(coordinate);
161 geoMap->setMapType(mapType);
162
163 populateMapTypeCombo();
164 updateZoom(zoomLevel);
165
166 mapWidget->setMap(geoMap);
167 connect(geoMap, SIGNAL(zoomLevelChanged(qreal)), this, SLOT(updateZoom(qreal)));
168}
169
170void Map::changeMapType(int index)
171{
172 geoMap->setMapType(QGraphicsGeoMap::MapType(mapTypeCombo->itemData(index).toInt()));
173}
174
175void Map::populateMapTypeCombo(void)
176{
177 mapTypeCombo->clear();
178
179 foreach (QGraphicsGeoMap::MapType mapType, geoMap->supportedMapTypes()) {
180 if (mapTypes.contains(mapType))
181 mapTypeCombo->addItem(mapTypes[mapType], QVariant(mapType));
182 }
183
184 if (mapTypeCombo->count() > 0)
185 geoMap->setMapType(QGraphicsGeoMap::MapType(mapTypeCombo->itemData(0).toInt()));
186}
187
188void Map::zoomIn(void)
189{
190 updateZoom(geoMap->zoomLevel() + 1);
191}
192
193void Map::zoomOut(void)
194{
195 updateZoom(geoMap->zoomLevel() - 1);
196}
197
198void Map::updateZoom(qreal value)
199{
200 value = qBound(manager->minimumZoomLevel(), value, manager->maximumZoomLevel());
201
202 geoMap->setZoomLevel(value);
203 zoomin->setEnabled(geoMap->zoomLevel() < manager->maximumZoomLevel());
204 zoomout->setEnabled(geoMap->zoomLevel() > manager->minimumZoomLevel());
205}
206
207void Map::findproxy(void)
208{
209 proxyCombo->addItem("none");
210 proxyCombo->addItem("new");
211 proxyCombo->addItem("proxyweb.utc.fr:3128");
212}
213
214void Map::changeproxy(int index)
215{
216 if(index==0)
217 {
218 QNetworkProxy proxy;
219 proxy.setType(QNetworkProxy::NoProxy);
220 QNetworkProxy::setApplicationProxy(proxy);
221 }
222 else if(index==1)
223 {
224 bool ok;
225 QString text = QInputDialog::getText(visible_widget, tr("New proxy:"),
226 tr("proxy: "), QLineEdit::Normal,
227 "adress:port", &ok);
228 if(ok==true)
229 {
230 proxyCombo->addItem(text);
231 proxyCombo->setCurrentIndex(proxyCombo->count()-1);
232 }
233 }
234 else
235 {
236 QStringList string=proxyCombo->itemText(index).split(":");
237 QNetworkProxy proxy;
238 proxy.setType(QNetworkProxy::HttpProxy);
239 proxy.setHostName(string.at(0));
240 proxy.setPort(string.at(1).toInt());
241 QNetworkProxy::setApplicationProxy(proxy);
242 updateZoom(geoMap->zoomLevel());//refresh
243 }
244}
245
246void Map::SetUptodate(void) {
247 QList<Landmark*> *landmarks=mapWidget->Landmarks();
248 for(int i=0;i<landmarks->count();i++) {
249 RemoveAttribute("lat" +QString::number(i));
250 RemoveAttribute("long" +QString::number(i));
251
252 if(mapWidget->LandmarkToSend(i)) {
253 SetAttribute("lat" +QString::number( i ) ,landmarks->at(i)->coordinate().latitude());
254 SetAttribute("long" +QString::number( i ) ,landmarks->at(i)->coordinate().longitude());
255 }
256 }
257
258 mapWidget->SetUptodate();
259}
260
261bool Map::IsUptodate(void)
262{
263 return mapWidget->IsUptodate();
264}
265
266void Map::Reset(void)
267{
268 mapWidget->Reset();
269}
270
271void Map::setCenteredPoint(int i) {
272 centeredpoint=i;
273 if(isCentered()) geoMap->setCenter(points.at(i)->coordinate());
274}
275
276int Map::centeredPoint(void) {
277 return centeredpoint;
278}
279
280bool Map::isCentered(void) {
281 if(centeredpoint!=-1) {
282 return true;
283 } else {
284 return false;
285 }
286}
287
288void Map::XmlEvent(QDomElement dom) {
289 if(dom.attribute("point")!="") {
290 receivesize+=3*sizeof(double);
291 if(mapWidget->isEnabled()) {
292 Landmark* landmark=new Landmark(geoMap,QGeoCoordinate(0,0,0),dom.attribute("point"),"cross");
293 landmark->setColor(Qt::black);
294 points.append(landmark);
295 mapWidget->AddPoint(dom.attribute("point"));
296 }
297 } else {
298 XmlSetup(dom);
299 }
300}
301
302void Map::BufEvent(char** buf,int *buf_size,uint16_t period,bool big_endian) {
303 if(IsEnabled()==false || RefreshRate_ms()!=period) return;
304
305 for(int i=0;i<points.count();i++) {
306 qint64 lat_raw;
307 double* lat=(double*)&lat_raw;
308 qint64 longitude_raw;
309 double* longitude=(double*)&longitude_raw;
310 qint64 alt_raw;
311 double* alt=(double*)&alt_raw;
312
313 memcpy(&lat_raw,*buf,sizeof(qint64));
314 *buf+=sizeof(qint64);
315 memcpy(&longitude_raw,*buf,sizeof(qint64));
316 *buf+=sizeof(qint64);
317 memcpy(&alt_raw,*buf,sizeof(qint64));
318 *buf+=sizeof(qint64);
319 if(big_endian==true) lat_raw=qFromBigEndian(lat_raw);
320 if(big_endian==true) longitude_raw=qFromBigEndian(longitude_raw);
321 if(big_endian==true) alt_raw=qFromBigEndian(alt_raw);
322 points.at(i)->setCoordinate(QGeoCoordinate(*lat,*longitude,*alt));
323
324 if(i==centeredpoint) geoMap->setCenter(points.at(i)->coordinate());
325 }
326}
327
328void Map::RemovePoints(void) {
329 for(int i=0;i<points.count();i++) {
330 points.at(i)->RemoveLandmark();
331 }
332}
333
334void Map::AddPoints(QGraphicsGeoMap *geoMap) {
335 for(int i=0;i<points.count();i++) {
336 points.at(i)->AddLandmark(geoMap);
337 }
338}
Note: See TracBrowser for help on using the repository browser.