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

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

sources reformatted with flair-format-dir script

File size: 9.5 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
21using namespace QtMobility;
22
[15]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;
[9]29
[15]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");
[9]34
[15]35  mapWidget = new MapWidget(this, visible_widget);
36  geoMap = 0;
[9]37
[15]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);
[9]42
[15]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);
[9]52
[15]53  if (findServices()) {
54    tool_layout->addLayout(servicelayout, 0);
55    tool_layout->addLayout(maplayout, 1);
56    tool_layout->addLayout(proxylayout, 2);
[9]57
[15]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()));
[9]70
[15]71    selectService(0);
[9]72
[15]73    findproxy();
[9]74
[15]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  }
[9]85
[15]86  map_layout->addWidget(mapWidget, 1);
87  for (int i = 0; i < coordinates.size(); i++) {
88    mapWidget->AddLandmark((QGeoCoordinate &)coordinates.at(i));
89  }
[9]90}
91
[15]92Map::~Map() {}
[9]93
[15]94bool Map::findServices(void) {
95  QCoreApplication::addLibraryPath(qgetenv("OECORE_HOST_SYSROOT") +
96                                   "/usr/lib/qt4/");
[9]97
[15]98  QStringList services = QGeoServiceProvider::availableServiceProviders();
[9]99
[15]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."));
[9]106
[15]107    item->setTextWidth(300);
[9]108
[15]109    return false;
110  }
[9]111
[15]112  QMap<QString, QVariant> parameters;
[9]113
[15]114  foreach (QString name, services) {
[9]115
[15]116    parameters["mapping.cache.directory"] = name;
117    parameters["mapping.cache.size"] = QString("500000000");
[9]118
[15]119    QGeoServiceProvider *service = new QGeoServiceProvider(name, parameters);
[9]120
[15]121    if (service->error() != QGeoServiceProvider::NoError) {
122      delete service;
123      continue;
124    }
[9]125
[15]126    QGeoMappingManager *manager = service->mappingManager();
[9]127
[15]128    if (manager) {
129      serviceCombo->addItem(name);
130      managers.append(manager);
[9]131    }
[15]132  }
[9]133
[15]134  return true;
[9]135}
136
[15]137void Map::selectService(int item) {
138  QString name = serviceCombo->currentText();
[9]139
[15]140  QGeoCoordinate coordinate = QGeoCoordinate(0, 0);
141  QGraphicsGeoMap::MapType mapType = QGraphicsGeoMap::StreetMap;
142  int zoomLevel = 1;
[9]143
[15]144  if (geoMap) {
145    coordinate = geoMap->center();
146    mapType = geoMap->mapType();
147    zoomLevel = geoMap->zoomLevel();
[9]148
[15]149    mapWidget->RemoveLandmarks();
150    RemovePoints();
151    geoMap->deleteLater();
152  }
[9]153
[15]154  manager = managers[item];
155  geoMap = new QGraphicsGeoMap(manager);
[9]156
[15]157  mapWidget->AddLandmarks(geoMap);
158  AddPoints(geoMap);
159  geoMap->setCenter(coordinate);
160  geoMap->setMapType(mapType);
[9]161
[15]162  populateMapTypeCombo();
163  updateZoom(zoomLevel);
[9]164
[15]165  mapWidget->setMap(geoMap);
166  connect(geoMap, SIGNAL(zoomLevelChanged(qreal)), this,
167          SLOT(updateZoom(qreal)));
[9]168}
169
[15]170void Map::changeMapType(int index) {
171  geoMap->setMapType(
172      QGraphicsGeoMap::MapType(mapTypeCombo->itemData(index).toInt()));
[9]173}
174
[15]175void Map::populateMapTypeCombo(void) {
176  mapTypeCombo->clear();
[9]177
[15]178  foreach (QGraphicsGeoMap::MapType mapType, geoMap->supportedMapTypes()) {
179    if (mapTypes.contains(mapType))
180      mapTypeCombo->addItem(mapTypes[mapType], QVariant(mapType));
181  }
[9]182
[15]183  if (mapTypeCombo->count() > 0)
184    geoMap->setMapType(
185        QGraphicsGeoMap::MapType(mapTypeCombo->itemData(0).toInt()));
[9]186}
187
[15]188void Map::zoomIn(void) { updateZoom(geoMap->zoomLevel() + 1); }
[9]189
[15]190void Map::zoomOut(void) { updateZoom(geoMap->zoomLevel() - 1); }
[9]191
[15]192void Map::updateZoom(qreal value) {
193  value =
194      qBound(manager->minimumZoomLevel(), value, manager->maximumZoomLevel());
[9]195
[15]196  geoMap->setZoomLevel(value);
197  zoomin->setEnabled(geoMap->zoomLevel() < manager->maximumZoomLevel());
198  zoomout->setEnabled(geoMap->zoomLevel() > manager->minimumZoomLevel());
[9]199}
200
[15]201void Map::findproxy(void) {
202  proxyCombo->addItem("none");
203  proxyCombo->addItem("new");
204  proxyCombo->addItem("proxyweb.utc.fr:3128");
[9]205}
206
[15]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);
[9]220    }
[15]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  }
[9]230}
231
232void Map::SetUptodate(void) {
[15]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));
[9]237
[15]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());
[9]243    }
[15]244  }
[9]245
[15]246  mapWidget->SetUptodate();
[9]247}
248
[15]249bool Map::IsUptodate(void) { return mapWidget->IsUptodate(); }
[9]250
[15]251void Map::Reset(void) { mapWidget->Reset(); }
[9]252
253void Map::setCenteredPoint(int i) {
[15]254  centeredpoint = i;
255  if (isCentered())
256    geoMap->setCenter(points.at(i)->coordinate());
[9]257}
258
[15]259int Map::centeredPoint(void) { return centeredpoint; }
[9]260
261bool Map::isCentered(void) {
[15]262  if (centeredpoint != -1) {
263    return true;
264  } else {
265    return false;
266  }
[9]267}
268
269void Map::XmlEvent(QDomElement dom) {
[15]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"));
[9]278    }
[15]279  } else {
280    XmlSetup(dom);
281  }
[9]282}
283
[15]284void Map::BufEvent(char **buf, int *buf_size, uint16_t period,
285                   bool big_endian) {
286  if (IsEnabled() == false || RefreshRate_ms() != period)
287    return;
[9]288
[15]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;
[9]296
[15]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));
[9]310
[15]311    if (i == centeredpoint)
312      geoMap->setCenter(points.at(i)->coordinate());
313  }
[9]314}
315
316void Map::RemovePoints(void) {
[15]317  for (int i = 0; i < points.count(); i++) {
318    points.at(i)->RemoveLandmark();
319  }
[9]320}
321
322void Map::AddPoints(QGraphicsGeoMap *geoMap) {
[15]323  for (int i = 0; i < points.count(); i++) {
324    points.at(i)->AddLandmark(geoMap);
325  }
[9]326}
Note: See TracBrowser for help on using the repository browser.