/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the examples of the Qt Mobility Components. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "mapbox.h" #include "geomap.h" #include "mainwindow.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include MapBox::MapBox(MainWindow * parent) : QWidget(parent), m_qgv(0), m_scene(0), m_mapWidget(0), m_serviceProvider(0), m_mapManager(0), m_connectivityMode(QGraphicsGeoMap::HybridMode), m_popupMenu(0), m_popupMenuMapObject(0), m_displayMode(DisplayNone), m_optionsWidget(0) { for (QVariantHash::const_iterator it = parent->programOptions().begin(); it != parent->programOptions().end(); ++it) { m_parameters.insert(it.key(), it.value()); } m_scene = new QGraphicsScene(this); m_qgv = new QGraphicsView(m_scene, this); m_qgv->setOptimizationFlag(QGraphicsView::IndirectPainting, true); m_qgv->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_qgv->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_qgv->setFrameShape(QFrame::NoFrame); m_optionsButton = new QPushButton("Options", m_qgv); m_optionsButton->resize(m_optionsButton->sizeHint()); connect(m_optionsButton, SIGNAL(clicked()), this, SLOT(showOptions())); QVBoxLayout * layout = new QVBoxLayout(this); layout->setMargin(0); setLayout(layout); setDisplayMode(DisplayMap); createDialogs(); createPixmapIcon(); } MapBox::~MapBox() { } void MapBox::createDialogs() { // Build coordinates dialog m_latitudeEdit = new QLineEdit(); m_longitudeEdit = new QLineEdit(); QFormLayout * latitudeLayout = new QFormLayout(); QFormLayout * longitudeLayout = new QFormLayout(); #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) latitudeLayout->addRow(tr("Lat:"), m_latitudeEdit); longitudeLayout->addRow(tr("Lng:"), m_longitudeEdit); #else latitudeLayout->addRow(tr("Latitude"), m_latitudeEdit); longitudeLayout->addRow(tr("Longitude"), m_longitudeEdit); #endif m_setCoordsButton = new QPushButton(); #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) m_setCoordsButton->setText(tr("Goto coords")); #else m_setCoordsButton->setText(tr("Go to coordinates")); #endif QGridLayout * coordControlLayout = new QGridLayout(); coordControlLayout->addLayout(latitudeLayout, 0, 0); coordControlLayout->addLayout(longitudeLayout, 0, 1); coordControlLayout->addWidget(m_setCoordsButton, 1, 1, 1, 2); m_coordControlDialog = new QDialog(this); m_coordControlDialog->setWindowTitle("Coordinates"); m_coordControlDialog->setLayout(coordControlLayout); connect(m_setCoordsButton, SIGNAL(clicked()), this, SLOT(gotoCoordsClicked())); connect(m_setCoordsButton, SIGNAL(clicked()), m_coordControlDialog, SLOT(hide())); } enum MapShapeType { SHAPE_BALLOON = 0, SHAPE_STAR = 1 }; QPair makeStandardMarker(MapShapeType shapeType, const QString & text, const QPen & pen, const QPen & textPen, const QBrush & brush) { QString svgTemplate; QPoint offset; switch (shapeType) { case SHAPE_BALLOON: svgTemplate = "" "" "" "" "" "" "" "%text%" "" ""; offset = QPoint(-14, -34); break; case SHAPE_STAR: svgTemplate = "%text%"; offset = QPoint(-16, -16); break; } svgTemplate.replace("%text%", text, Qt::CaseSensitive); svgTemplate.replace("%pen%", pen.color().name(), Qt::CaseSensitive); svgTemplate.replace("%penOpacity%", QString::number(pen.color().alpha() / 255.0), Qt::CaseSensitive); svgTemplate.replace("%textPen%", textPen.color().name(), Qt::CaseSensitive); svgTemplate.replace("%brush%", brush.color().name(), Qt::CaseSensitive); svgTemplate.replace("%brushOpacity%", QString::number(brush.color().alpha() / 255.0), Qt::CaseSensitive); svgTemplate.replace("%textContent%", "", Qt::CaseSensitive); QSvgRenderer renderer(svgTemplate.toAscii()); QPixmap pixmap(renderer.defaultSize()); pixmap.fill(QColor(0, 0, 0, 0)); QPainter painter(&pixmap); renderer.render(&painter); return qMakePair(pixmap, offset); } void MapBox::createPixmapIcon() { if (!m_markerIcon.isNull()) return; QPair markerPair = makeStandardMarker(SHAPE_BALLOON, "", QPen(), QPen(QColor(Qt::white)), QBrush(QColor(0, 0, 136))); m_markerIcon = markerPair.first; m_markerOffset = markerPair.second; } void MapBox::setProvider(const QString & providerId) { if (m_providerId == providerId) return; m_providerId = providerId; createProvider(); } void MapBox::createProvider() { delete m_serviceProvider; qDebug("Creating provider %s", m_providerId.toLocal8Bit().constData()); m_serviceProvider = new QGeoServiceProvider(m_providerId, m_parameters); if (m_serviceProvider->error() != QGeoServiceProvider::NoError) { QMessageBox::information(this, tr("MapViewer Example"), tr( "Unable to find the %1 geoservices plugin.").arg(m_providerId)); qApp->quit(); } m_mapManager = m_serviceProvider->mappingManager(); createMapWidget(); } void MapBox::createMapWidget() { delete m_mapWidget; m_mapWidget = new GeoMap(m_mapManager); m_mapWidget->setConnectivityMode(m_connectivityMode); m_mapWidget->setCenter(QGeoCoordinate(52.5, 13.0)); m_mapWidget->setZoomLevel(5); m_scene->addItem(m_mapWidget); connect(m_mapWidget, SIGNAL(contextMenu(QGraphicsSceneContextMenuEvent *, QGeoMapObject *)), this, SLOT(mapContextMenu(QGraphicsSceneContextMenuEvent *, QGeoMapObject *))); createOptionsDialog(); } void MapBox::setConnectivityMode(QGraphicsGeoMap::ConnectivityMode mode) { m_connectivityMode = mode; if (m_mapWidget) m_mapWidget->setConnectivityMode(m_connectivityMode); } void MapBox::createOptionsDialog() { if (m_optionsWidget) delete m_optionsWidget; m_optionsWidget = new QWidget(this); QGridLayout * optionsLayout = new QGridLayout; QVector mapTypeNames; QVector mapControlTypes; mapControlTypes.append(QGraphicsGeoMap::StreetMap); mapTypeNames.append(tr("Street")); mapControlTypes.append(QGraphicsGeoMap::SatelliteMapDay); mapTypeNames.append(tr("Satellite")); mapControlTypes.append(QGraphicsGeoMap::SatelliteMapNight); mapTypeNames.append(tr("Satellite - Night")); mapControlTypes.append(QGraphicsGeoMap::TerrainMap); mapTypeNames.append(tr("Terrain")); { // map types QSignalMapper * mapper = new QSignalMapper(m_optionsWidget); connect(mapper, SIGNAL(mapped(int)), this, SLOT(mapTypeToggled(int))); QLabel * mapTypesLabel = new QLabel(tr("Map types")); mapTypesLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); mapTypesLabel->setAlignment(Qt::AlignHCenter); optionsLayout->addWidget(mapTypesLabel, 0, 0); QList types = m_mapWidget->supportedMapTypes(); for (int controlIndex = 0; controlIndex < mapControlTypes.size(); ++controlIndex) { QPushButton *button = new QPushButton(mapTypeNames[controlIndex], m_optionsWidget); button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); QFont font = button->font(); font.setPointSize(24); button->setFont(font); optionsLayout->addWidget(button, controlIndex+1, 0); int supportedTypeIndex = types.indexOf(mapControlTypes[controlIndex]); if (supportedTypeIndex == -1) { button->setEnabled(false); } else { connect(button, SIGNAL(clicked()), mapper, SLOT(map())); mapper->setMapping(button, mapControlTypes[controlIndex]); button->setEnabled(true); //button->setChecked(mapControlTypes[controlIndex] == m_mapWidget->mapType()); } } } { // connectivity modes QVector connectivityModeNames; QVector connectivityModes; connectivityModes.append(QGraphicsGeoMap::NoConnectivity); connectivityModeNames.append(tr("No connectivity")); connectivityModes.append(QGraphicsGeoMap::OfflineMode); connectivityModeNames.append(tr("Offline")); connectivityModes.append(QGraphicsGeoMap::OnlineMode); connectivityModeNames.append(tr("Online")); connectivityModes.append(QGraphicsGeoMap::HybridMode); connectivityModeNames.append(tr("Hybrid")); // map types QSignalMapper * mapper = new QSignalMapper(m_optionsWidget); connect(mapper, SIGNAL(mapped(int)), this, SLOT(connectivityModeToggled(int))); QLabel * connectivityModesLabel = new QLabel(tr("Connectivity")); connectivityModesLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); connectivityModesLabel->setAlignment(Qt::AlignHCenter); optionsLayout->addWidget(connectivityModesLabel, 0, 1); QList modes = m_mapWidget->supportedConnectivityModes(); for (int controlIndex = 0; controlIndex < connectivityModes.size(); ++controlIndex) { QPushButton *button = new QPushButton(connectivityModeNames[controlIndex], m_optionsWidget); button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); QFont font = button->font(); font.setPointSize(24); button->setFont(font); optionsLayout->addWidget(button, controlIndex+1, 1); int supportedTypeIndex = modes.indexOf(connectivityModes[controlIndex]); if (supportedTypeIndex == -1) { button->setEnabled(false); } else { connect(button, SIGNAL(clicked()), mapper, SLOT(map())); mapper->setMapping(button, connectivityModes[controlIndex]); button->setEnabled(true); //button->setChecked(connectivityModes[controlIndex] == m_mapWidget->connectivityMode()); } } } /* { // plugins, TODO QList providers = QGeoServiceProvider::availableServiceProviders(); for (int i = 0; i < providers.count(); ++i) { QAction *providerAction = new QAction(providers[i], this); providerAction->setCheckable(true); providerAction->setChecked(i == 0); m_pluginMenu->addAction(providerAction); } } */ m_optionsWidget->setLayout(optionsLayout); if (displayMode() == DisplayOptions) { setDisplayMode(DisplayNone); setDisplayMode(DisplayOptions); } } void MapBox::showOptions() { setDisplayMode(DisplayOptions); } void MapBox::mapTypeToggled(int type) { m_mapWidget->setMapType((QGraphicsGeoMap::MapType)type); setDisplayMode(DisplayMap); } void MapBox::connectivityModeToggled(int type) { m_mapWidget->setConnectivityMode((QGraphicsGeoMap::ConnectivityMode)type); setDisplayMode(DisplayMap); } void MapBox::resizeEvent(QResizeEvent * event) { m_qgv->setSceneRect(QRectF(QPointF(0.0, 0.0), m_qgv->size())); if (m_mapWidget) m_mapWidget->resize(m_qgv->size()); m_optionsButton->move(m_qgv->width() - m_optionsButton->width() - 9, m_qgv->height() - m_optionsButton->height() - 9); if (event) QWidget::resizeEvent(event); } static QPen defaultPen() { QPen pen(Qt::white); pen.setWidth(2); pen.setCosmetic(true); return pen; } static QBrush defaultBrush() { return QBrush(QColor(0,0,0,65)); } QGeoMapRectangleObject * MapBox::addRectangle(qreal top, qreal left, qreal bottom, qreal right) { return addRectangle(QGeoCoordinate(top, left), QGeoCoordinate(bottom, right)); } QGeoMapRectangleObject * MapBox::addRectangle(const QGeoCoordinate & topLeft, const QGeoCoordinate & bottomRight) { QGeoMapRectangleObject * rectangle = new QGeoMapRectangleObject(topLeft, bottomRight); rectangle->setPen(defaultPen()); rectangle->setBrush(defaultBrush()); m_mapWidget->addMapObject(rectangle); return rectangle; } QGeoMapPolylineObject * MapBox::addPolyline(const QList & path) { QGeoMapPolylineObject * polyline = new QGeoMapPolylineObject(); polyline->setPen(defaultPen()); // TODO: check polyline->setPath(path); m_mapWidget->addMapObject(polyline); return polyline; } QGeoMapPolygonObject * MapBox::addPolygon(const QList & path) { QGeoMapPolygonObject * polygon = new QGeoMapPolygonObject(); polygon->setPen(defaultPen()); polygon->setBrush(defaultBrush()); polygon->setPath(path); m_mapWidget->addMapObject(polygon); return polygon; } QGeoMapCircleObject * MapBox::addCircle(const QGeoCoordinate & center, qreal radius) { QGeoMapCircleObject * circle = new QGeoMapCircleObject(center, radius); circle->setPen(defaultPen()); circle->setBrush(defaultBrush()); m_mapWidget->addMapObject(circle); return circle; } void MapBox::addRoute(const QGeoCoordinate & start, const QGeoCoordinate & end) { QList waypoints; waypoints << start << end; addRoute(waypoints); } void MapBox::addRoute(const QList & waypoints) { QGeoRoutingManager * routingManager = m_serviceProvider->routingManager(); QGeoRouteRequest req(waypoints); QGeoRouteReply * reply = routingManager->calculateRoute(req); connect(reply, SIGNAL(finished()), this, SLOT(routeFinished())); } void MapBox::routeFinished() { QGeoRouteReply * reply = static_cast(sender()); if (!reply) return; if (reply->routes().size() < 1) return; QPen pen(QColor(0, 0, 255, 127)); // blue, semi-transparent pen.setWidth(7); //pen.setCosmetic(true); pen.setCapStyle(Qt::RoundCap); QGeoMapRouteObject * route = new QGeoMapRouteObject(reply->routes().at(0)); route->setPen(pen); m_mapWidget->addMapObject(route); } void MapBox::addPixmap(const QGeoCoordinate &coord, const QPoint &offset, const QPixmap &pixmap) { QGeoMapPixmapObject *pixmapObject = new QGeoMapPixmapObject(coord, offset, pixmap); m_mapWidget->addMapObject(pixmapObject); } void MapBox::addText(const QGeoCoordinate &coord, const QString &text, const QFont &font, const QPoint &offset, Qt::Alignment alignment) { QGeoMapTextObject *textObject = new QGeoMapTextObject(coord, text, font, offset, alignment); m_mapWidget->addMapObject(textObject); } void MapBox::addFakeRoute(const QList &waypoints, int segments) { QGeoRoute route; int index = 0; QGeoRouteSegment lastSegment; for (int i = 0; i < segments; ++i) { QGeoRouteSegment segment; QList path; int limit = (waypoints.size() * (i + 1)) / segments; for (; index < limit; ++index) path.append(waypoints.at(index)); segment.setPath(path); if (i == 0) route.setFirstRouteSegment(segment); else lastSegment.setNextRouteSegment(segment); lastSegment = segment; } QPen pen(QColor(0, 0, 255, 127)); // blue, semi-transparent pen.setWidth(7); //pen.setCosmetic(true); pen.setCapStyle(Qt::RoundCap); QGeoMapRouteObject * routeObject = new QGeoMapRouteObject(route); routeObject->setPen(pen); m_mapWidget->addMapObject(routeObject); } void MapBox::addCustom(const QGeoCoordinate &coord, const QPoint &offset, QGraphicsItem *graphicsItem) { QGeoMapCustomObject *customObject = new QGeoMapCustomObject(coord, offset); //customObject->setUnits(QGeoMapObject::MeterUnit); //customObject->setTransformType(); customObject->setGraphicsItem(graphicsItem); m_mapWidget->addMapObject(customObject); } void MapBox::mapContextMenu(QGraphicsSceneContextMenuEvent * event, QGeoMapObject * clickedMapObject) { if (!m_popupMenu) createMenus(); m_lastClicked = event->scenePos(); m_lastClickedMapObject = clickedMapObject; if (clickedMapObject) { // There is an object here, store it and open the appropriate context menu m_popupMenuMapObject->popup(event->screenPos()); } else { // No objects, display the default context menu m_popupMenu->popup(event->screenPos()); } } void MapBox::createMenus() { QAction * menuItem; QMenu * subMenuItem; m_popupMenu = new QMenu(this); m_popupMenuMapObject = new QMenu(this); //************************************************************** // MapObject-specific context menu. The map object is in m_lastClickedMapObject. subMenuItem = m_popupMenuMapObject->addMenu(tr("MapObject")); menuItem = subMenuItem->addAction(tr("Remove")); connect(menuItem, SIGNAL(triggered()), this, SLOT(removeMapObject())); //************************************************************** /* // These are gread for testing, but not really what we want in // an example application. // We should probably creating a testing branch which tracks // the master branch and has various "test enabled" versions // of the examples at some point anyhow. subMenuItem = m_popupMenuMapObject->addMenu(tr("Spawn stuff")); m_popupMenu->addMenu(subMenuItem); menuItem = subMenuItem->addAction(tr("Items near the dateline")); connect(menuItem, SIGNAL(triggered()), this, SLOT(demo1())); menuItem = subMenuItem->addAction(tr("Regular grid of items")); connect(menuItem, SIGNAL(triggered()), this, SLOT(demo2())); menuItem = subMenuItem->addAction(tr("Clusters")); connect(menuItem, SIGNAL(triggered()), this, SLOT(demo3())); */ //************************************************************** subMenuItem = m_popupMenuMapObject->addMenu(tr("Marker")); m_popupMenu->addMenu(subMenuItem); menuItem = subMenuItem->addAction(tr("Set marker")); connect(menuItem, SIGNAL(triggered()), this, SLOT(drawPixmap())); menuItem = subMenuItem->addAction(tr("Remove markers")); connect(menuItem, SIGNAL(triggered()), this, SLOT(removePixmaps())); menuItem = subMenuItem->addAction(tr("Select objects")); connect(menuItem, SIGNAL(triggered()), this, SLOT(selectObjects())); //************************************************************** subMenuItem = m_popupMenu->addMenu(tr("Draw")); m_popupMenu->addMenu(subMenuItem); menuItem = subMenuItem->addAction(tr("Rectangle")); connect(menuItem, SIGNAL(triggered()), this, SLOT(drawRect())); menuItem = subMenuItem->addAction(tr("Polyline")); connect(menuItem, SIGNAL(triggered()), this, SLOT(drawPolyline())); menuItem = subMenuItem->addAction(tr("Polygon")); connect(menuItem, SIGNAL(triggered()), this, SLOT(drawPolygon())); menuItem = subMenuItem->addAction(tr("Circle")); connect(menuItem, SIGNAL(triggered()), this, SLOT(drawCircle())); menuItem = subMenuItem->addAction(tr("Text")); connect(menuItem, SIGNAL(triggered()), this, SLOT(drawText())); //************************************************************** subMenuItem = m_popupMenuMapObject->addMenu(tr("Route")); m_popupMenu->addMenu(subMenuItem); menuItem = subMenuItem->addAction(tr("Calculate route")); connect(menuItem, SIGNAL(triggered()), this, SLOT(calcRoute())); //************************************************************** subMenuItem = m_popupMenuMapObject->addMenu(tr("Coordinates")); m_popupMenu->addMenu(subMenuItem); menuItem = subMenuItem->addAction(tr("Capture")); connect(menuItem, SIGNAL(triggered()), this, SLOT(captureCoordinates())); connect(menuItem, SIGNAL(triggered()), m_coordControlDialog, SLOT(show())); menuItem = subMenuItem->addAction(tr("Go to")); connect(menuItem, SIGNAL(triggered()), m_coordControlDialog, SLOT(show())); //************************************************************** subMenuItem = m_popupMenuMapObject->addMenu(tr("Tests")); m_popupMenu->addMenu(subMenuItem); menuItem = subMenuItem->addAction(tr("Dateline")); connect(menuItem, SIGNAL(triggered()), this, SLOT(testDateline())); } void MapBox::gotoCoordsClicked() { bool ok = false; double lat = m_latitudeEdit->text().toDouble(&ok); if (!ok) return; double lon = m_longitudeEdit->text().toDouble(&ok); if (!ok) return; m_mapWidget->setCenter(QGeoCoordinate(lat, lon)); } void MapBox::setDisplayMode(DisplayMode mode) { if (m_displayMode == mode) return; m_displayMode = mode; QWidget * newWidget; switch (m_displayMode) { case DisplayMap: newWidget = m_qgv; break; case DisplayOptions: newWidget = m_optionsWidget; break; default: newWidget = 0; } // remove all current widgets while (QLayoutItem * item = layout()->takeAt(0)) { item->widget()->setVisible(false); } // add the new widget if (newWidget) { layout()->addWidget(newWidget); newWidget->setVisible(true); } resizeEvent(0); } MapBox::DisplayMode MapBox::displayMode() { return m_displayMode; }