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

Last change on this file since 303 was 269, checked in by Sanahuja Guillaume, 6 years ago

flairgcs:
speed up processing time when receiving datas from uav
triger watchdog while receiving datas from uav
(avoids connection lost in uav)

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