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

Last change on this file since 15 was 15, checked in by Bayard Gildas, 8 years ago

sources reformatted with flair-format-dir script

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