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 |
|
---|
23 | using namespace QtMobility;
|
---|
24 |
|
---|
25 | Map::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 | printf("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 |
|
---|
94 | Map::~Map() {
|
---|
95 | for (int i = 0; i < points.count(); i++) delete points.at(i);
|
---|
96 | delete mapWidget;
|
---|
97 |
|
---|
98 | if(geoMap!=0) delete geoMap;
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool Map::findServices(void) {
|
---|
102 | QCoreApplication::addLibraryPath(qgetenv("OECORE_HOST_SYSROOT") +
|
---|
103 | "/usr/lib/qt4/");
|
---|
104 |
|
---|
105 | QStringList services = QGeoServiceProvider::availableServiceProviders();
|
---|
106 |
|
---|
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."));
|
---|
113 |
|
---|
114 | item->setTextWidth(300);
|
---|
115 |
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 |
|
---|
119 | QMap<QString, QVariant> parameters;
|
---|
120 |
|
---|
121 | foreach (QString name, services) {
|
---|
122 |
|
---|
123 | parameters["mapping.cache.directory"] = name;
|
---|
124 | parameters["mapping.cache.size"] = QString("500000000");
|
---|
125 |
|
---|
126 | QGeoServiceProvider *service = new QGeoServiceProvider(name, parameters);
|
---|
127 |
|
---|
128 | if (service->error() != QGeoServiceProvider::NoError) {
|
---|
129 | delete service;
|
---|
130 | continue;
|
---|
131 | }
|
---|
132 |
|
---|
133 | QGeoMappingManager *manager = service->mappingManager();
|
---|
134 |
|
---|
135 | if (manager) {
|
---|
136 | serviceCombo->addItem(name);
|
---|
137 | managers.append(manager);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | return true;
|
---|
142 | }
|
---|
143 |
|
---|
144 | void Map::selectService(int item) {
|
---|
145 | QString name = serviceCombo->currentText();
|
---|
146 |
|
---|
147 | QGeoCoordinate coordinate = QGeoCoordinate(0, 0);
|
---|
148 | QGraphicsGeoMap::MapType mapType = QGraphicsGeoMap::StreetMap;
|
---|
149 | int zoomLevel = 1;
|
---|
150 |
|
---|
151 | if (geoMap) {
|
---|
152 | coordinate = geoMap->center();
|
---|
153 | mapType = geoMap->mapType();
|
---|
154 | zoomLevel = geoMap->zoomLevel();
|
---|
155 |
|
---|
156 | mapWidget->RemoveLandmarks();
|
---|
157 | RemovePoints();
|
---|
158 | geoMap->deleteLater();
|
---|
159 | }
|
---|
160 |
|
---|
161 | manager = managers[item];
|
---|
162 | geoMap = new QGraphicsGeoMap(manager);
|
---|
163 |
|
---|
164 | mapWidget->AddLandmarks(geoMap);
|
---|
165 | AddPoints(geoMap);
|
---|
166 | geoMap->setCenter(coordinate);
|
---|
167 | geoMap->setMapType(mapType);
|
---|
168 |
|
---|
169 | populateMapTypeCombo();
|
---|
170 | updateZoom(zoomLevel);
|
---|
171 |
|
---|
172 | mapWidget->setMap(geoMap);
|
---|
173 | connect(geoMap, SIGNAL(zoomLevelChanged(qreal)), this,
|
---|
174 | SLOT(updateZoom(qreal)));
|
---|
175 | }
|
---|
176 |
|
---|
177 | void Map::changeMapType(int index) {
|
---|
178 | geoMap->setMapType(
|
---|
179 | QGraphicsGeoMap::MapType(mapTypeCombo->itemData(index).toInt()));
|
---|
180 | }
|
---|
181 |
|
---|
182 | void Map::populateMapTypeCombo(void) {
|
---|
183 | mapTypeCombo->clear();
|
---|
184 |
|
---|
185 | foreach (QGraphicsGeoMap::MapType mapType, geoMap->supportedMapTypes()) {
|
---|
186 | if (mapTypes.contains(mapType))
|
---|
187 | mapTypeCombo->addItem(mapTypes[mapType], QVariant(mapType));
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (mapTypeCombo->count() > 0)
|
---|
191 | geoMap->setMapType(
|
---|
192 | QGraphicsGeoMap::MapType(mapTypeCombo->itemData(0).toInt()));
|
---|
193 | }
|
---|
194 |
|
---|
195 | void Map::zoomIn(void) { updateZoom(geoMap->zoomLevel() + 1); }
|
---|
196 |
|
---|
197 | void Map::zoomOut(void) { updateZoom(geoMap->zoomLevel() - 1); }
|
---|
198 |
|
---|
199 | void Map::updateZoom(qreal value) {
|
---|
200 | value =
|
---|
201 | qBound(manager->minimumZoomLevel(), value, manager->maximumZoomLevel());
|
---|
202 |
|
---|
203 | geoMap->setZoomLevel(value);
|
---|
204 | zoomin->setEnabled(geoMap->zoomLevel() < manager->maximumZoomLevel());
|
---|
205 | zoomout->setEnabled(geoMap->zoomLevel() > manager->minimumZoomLevel());
|
---|
206 | }
|
---|
207 |
|
---|
208 | void Map::findproxy(void) {
|
---|
209 | proxyCombo->addItem("none");
|
---|
210 | proxyCombo->addItem("new");
|
---|
211 | proxyCombo->addItem("proxyweb.utc.fr:3128");
|
---|
212 | }
|
---|
213 |
|
---|
214 | void 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);
|
---|
227 | }
|
---|
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 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | void Map::SetUptodate(void) {
|
---|
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));
|
---|
244 |
|
---|
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());
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | mapWidget->SetUptodate();
|
---|
254 | }
|
---|
255 |
|
---|
256 | bool Map::IsUptodate(void) { return mapWidget->IsUptodate(); }
|
---|
257 |
|
---|
258 | void Map::Reset(void) { mapWidget->Reset(); }
|
---|
259 |
|
---|
260 | void Map::setCenteredPoint(int i) {
|
---|
261 | centeredpoint = i;
|
---|
262 | if (isCentered())
|
---|
263 | geoMap->setCenter(points.at(i)->coordinate());
|
---|
264 | }
|
---|
265 |
|
---|
266 | int Map::centeredPoint(void) { return centeredpoint; }
|
---|
267 |
|
---|
268 | bool Map::isCentered(void) {
|
---|
269 | if (centeredpoint != -1) {
|
---|
270 | return true;
|
---|
271 | } else {
|
---|
272 | return false;
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | void Map::XmlEvent(QDomElement dom) {
|
---|
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"));
|
---|
285 | }
|
---|
286 | } else {
|
---|
287 | XmlSetup(dom);
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | void Map::BufEvent(char **buf, int *buf_size, uint16_t period,
|
---|
292 | bool big_endian) {
|
---|
293 | if (IsEnabled() == false || RefreshRate_ms() != period)
|
---|
294 | return;
|
---|
295 |
|
---|
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;
|
---|
303 |
|
---|
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));
|
---|
317 |
|
---|
318 | if (i == centeredpoint)
|
---|
319 | geoMap->setCenter(points.at(i)->coordinate());
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | void Map::RemovePoints(void) {
|
---|
324 | for (int i = 0; i < points.count(); i++) {
|
---|
325 | points.at(i)->RemoveLandmark();
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | void Map::AddPoints(QGraphicsGeoMap *geoMap) {
|
---|
330 | for (int i = 0; i < points.count(); i++) {
|
---|
331 | points.at(i)->AddLandmark(geoMap);
|
---|
332 | }
|
---|
333 | }
|
---|