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

Last change on this file since 65 was 62, checked in by Sanahuja Guillaume, 8 years ago

correction bug destruction map

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