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

Last change on this file was 444, checked in by Sanahuja Guillaume, 6 weeks ago

update buffering (gcs part)
seems to work!

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,
[437]26         QList<QGeoCoordinate> coordinates, bool enabled, uint16_t period)
[15]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 {
[244]84    fprintf(stderr,"Error, no services!\n");
[15]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;
97}
98
[15]99bool Map::findServices(void) {
100  QCoreApplication::addLibraryPath(qgetenv("OECORE_HOST_SYSROOT") +
101                                   "/usr/lib/qt4/");
[9]102
[15]103  QStringList services = QGeoServiceProvider::availableServiceProviders();
[9]104
[15]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 "
[222]109           "installed and, if necessary, set the OECORE_HOST_SYSROOT "
[15]110           "environment variable to the good location."));
[9]111
[15]112    item->setTextWidth(300);
[9]113
[15]114    return false;
115  }
[9]116
[15]117  QMap<QString, QVariant> parameters;
[9]118
[15]119  foreach (QString name, services) {
[9]120
[15]121    parameters["mapping.cache.directory"] = name;
122    parameters["mapping.cache.size"] = QString("500000000");
[9]123
[15]124    QGeoServiceProvider *service = new QGeoServiceProvider(name, parameters);
[9]125
[15]126    if (service->error() != QGeoServiceProvider::NoError) {
127      delete service;
128      continue;
129    }
[9]130
[15]131    QGeoMappingManager *manager = service->mappingManager();
[9]132
[15]133    if (manager) {
134      serviceCombo->addItem(name);
135      managers.append(manager);
[9]136    }
[15]137  }
[9]138
[15]139  return true;
[9]140}
141
[15]142void Map::selectService(int item) {
143  QString name = serviceCombo->currentText();
[9]144
[15]145  QGeoCoordinate coordinate = QGeoCoordinate(0, 0);
146  QGraphicsGeoMap::MapType mapType = QGraphicsGeoMap::StreetMap;
147  int zoomLevel = 1;
[9]148
[15]149  if (geoMap) {
150    coordinate = geoMap->center();
151    mapType = geoMap->mapType();
152    zoomLevel = geoMap->zoomLevel();
[9]153
[15]154    mapWidget->RemoveLandmarks();
155    RemovePoints();
156    geoMap->deleteLater();
157  }
[9]158
[15]159  manager = managers[item];
160  geoMap = new QGraphicsGeoMap(manager);
[9]161
[15]162  mapWidget->AddLandmarks(geoMap);
163  AddPoints(geoMap);
164  geoMap->setCenter(coordinate);
165  geoMap->setMapType(mapType);
[9]166
[15]167  populateMapTypeCombo();
168  updateZoom(zoomLevel);
[9]169
[15]170  mapWidget->setMap(geoMap);
171  connect(geoMap, SIGNAL(zoomLevelChanged(qreal)), this,
172          SLOT(updateZoom(qreal)));
[9]173}
174
[15]175void Map::changeMapType(int index) {
176  geoMap->setMapType(
177      QGraphicsGeoMap::MapType(mapTypeCombo->itemData(index).toInt()));
[9]178}
179
[15]180void Map::populateMapTypeCombo(void) {
181  mapTypeCombo->clear();
[9]182
[15]183  foreach (QGraphicsGeoMap::MapType mapType, geoMap->supportedMapTypes()) {
184    if (mapTypes.contains(mapType))
185      mapTypeCombo->addItem(mapTypes[mapType], QVariant(mapType));
186  }
[9]187
[15]188  if (mapTypeCombo->count() > 0)
189    geoMap->setMapType(
190        QGraphicsGeoMap::MapType(mapTypeCombo->itemData(0).toInt()));
[9]191}
192
[15]193void Map::zoomIn(void) { updateZoom(geoMap->zoomLevel() + 1); }
[9]194
[15]195void Map::zoomOut(void) { updateZoom(geoMap->zoomLevel() - 1); }
[9]196
[15]197void Map::updateZoom(qreal value) {
198  value =
199      qBound(manager->minimumZoomLevel(), value, manager->maximumZoomLevel());
[9]200
[15]201  geoMap->setZoomLevel(value);
202  zoomin->setEnabled(geoMap->zoomLevel() < manager->maximumZoomLevel());
203  zoomout->setEnabled(geoMap->zoomLevel() > manager->minimumZoomLevel());
[9]204}
205
[15]206void Map::findproxy(void) {
207  proxyCombo->addItem("none");
208  proxyCombo->addItem("new");
209  proxyCombo->addItem("proxyweb.utc.fr:3128");
[9]210}
211
[15]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);
[9]225    }
[15]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  }
[9]235}
236
237void Map::SetUptodate(void) {
[15]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));
[9]242
[15]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());
[9]248    }
[15]249  }
[9]250
[15]251  mapWidget->SetUptodate();
[9]252}
253
[15]254bool Map::IsUptodate(void) { return mapWidget->IsUptodate(); }
[9]255
[15]256void Map::Reset(void) { mapWidget->Reset(); }
[9]257
258void Map::setCenteredPoint(int i) {
[15]259  centeredpoint = i;
260  if (isCentered())
261    geoMap->setCenter(points.at(i)->coordinate());
[9]262}
263
[15]264int Map::centeredPoint(void) { return centeredpoint; }
[9]265
266bool Map::isCentered(void) {
[15]267  if (centeredpoint != -1) {
268    return true;
269  } else {
270    return false;
271  }
[9]272}
273
[269]274void Map::XmlEvent(QDomElement *dom) {
275  if (dom->attribute("point") != "") {
[15]276    receivesize += 3 * sizeof(double);
277    if (mapWidget->isEnabled()) {
278      Landmark *landmark = new Landmark(geoMap, QGeoCoordinate(0, 0, 0),
[269]279                                        dom->attribute("point"), "cross");
[15]280      landmark->setColor(Qt::black);
281      points.append(landmark);
[269]282      mapWidget->AddPoint(dom->attribute("point"));
[9]283    }
[15]284  } else {
285    XmlSetup(dom);
286  }
[9]287}
288
[444]289void Map::BufEvent(char **buf, int *buf_size, uint16_t period,uint16_t nb_buffering,bool big_endian) {
290  if (IsEnabled() == false || RefreshRate_ms() != period || NbBuffering()!=nb_buffering) return;
[9]291
[15]292  for (int i = 0; i < points.count(); i++) {
293    qint64 lat_raw;
294    double *lat = (double *)&lat_raw;
295    qint64 longitude_raw;
296    double *longitude = (double *)&longitude_raw;
297    qint64 alt_raw;
298    double *alt = (double *)&alt_raw;
[9]299
[15]300    memcpy(&lat_raw, *buf, sizeof(qint64));
301    *buf += sizeof(qint64);
302    memcpy(&longitude_raw, *buf, sizeof(qint64));
303    *buf += sizeof(qint64);
304    memcpy(&alt_raw, *buf, sizeof(qint64));
305    *buf += sizeof(qint64);
306    if (big_endian == true)
307      lat_raw = qFromBigEndian(lat_raw);
308    if (big_endian == true)
309      longitude_raw = qFromBigEndian(longitude_raw);
310    if (big_endian == true)
311      alt_raw = qFromBigEndian(alt_raw);
312    points.at(i)->setCoordinate(QGeoCoordinate(*lat, *longitude, *alt));
[9]313
[15]314    if (i == centeredpoint)
315      geoMap->setCenter(points.at(i)->coordinate());
316  }
[9]317}
318
319void Map::RemovePoints(void) {
[15]320  for (int i = 0; i < points.count(); i++) {
321    points.at(i)->RemoveLandmark();
322  }
[9]323}
324
325void Map::AddPoints(QGraphicsGeoMap *geoMap) {
[15]326  for (int i = 0; i < points.count(); i++) {
327    points.at(i)->AddLandmark(geoMap);
328  }
[9]329}
Note: See TracBrowser for help on using the repository browser.