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