| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | 
| 4 | ** Contact: http://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the QtLocation module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or later as published by the Free | 
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in | 
| 29 | ** the packaging of this file. Please review the following information to | 
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be | 
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. | 
| 32 | ** | 
| 33 | ** $QT_END_LICENSE$ | 
| 34 | ** | 
| 35 | ****************************************************************************/ | 
| 36 |  | 
| 37 | #include "qgeomap_p.h" | 
| 38 | #include "qgeomap_p_p.h" | 
| 39 | #include "qgeocameracapabilities_p.h" | 
| 40 | #include "qgeomappingmanagerengine_p.h" | 
| 41 | #include "qdeclarativegeomapitembase_p.h" | 
| 42 | #include "qgeomapobject_p.h" | 
| 43 | #include "qgeomapobject_p_p.h" | 
| 44 | #include <QtQuick/private/qquickitem_p.h> | 
| 45 | #include <QDebug> | 
| 46 | #include <QRectF> | 
| 47 |  | 
| 48 | QT_BEGIN_NAMESPACE | 
| 49 |  | 
| 50 | QGeoMap::QGeoMap(QGeoMapPrivate &dd, QObject *parent) | 
| 51 |     : QObject(dd,parent) | 
| 52 | { | 
| 53 | } | 
| 54 |  | 
| 55 | QGeoMap::~QGeoMap() | 
| 56 | { | 
| 57 |     Q_D(QGeoMap); | 
| 58 |     clearParameters(); | 
| 59 |     for (QGeoMapObject *p : d->mapObjects()) | 
| 60 |         p->setMap(nullptr); // forces replacing pimpls with the default ones. | 
| 61 | } | 
| 62 |  | 
| 63 | void QGeoMap::setViewportSize(const QSize& size) | 
| 64 | { | 
| 65 |     Q_D(QGeoMap); | 
| 66 |     if (size == d->m_viewportSize) | 
| 67 |         return; | 
| 68 |     d->m_viewportSize = size; | 
| 69 |     d->m_geoProjection->setViewportSize(size); | 
| 70 |     d->changeViewportSize(size); | 
| 71 | } | 
| 72 |  | 
| 73 | QSize QGeoMap::viewportSize() const | 
| 74 | { | 
| 75 |     Q_D(const QGeoMap); | 
| 76 |     return d->m_viewportSize; | 
| 77 | } | 
| 78 |  | 
| 79 | int QGeoMap::viewportWidth() const | 
| 80 | { | 
| 81 |     Q_D(const QGeoMap); | 
| 82 |     return d->m_viewportSize.width(); | 
| 83 | } | 
| 84 |  | 
| 85 | int QGeoMap::viewportHeight() const | 
| 86 | { | 
| 87 |     Q_D(const QGeoMap); | 
| 88 |     return d->m_viewportSize.height(); | 
| 89 | } | 
| 90 |  | 
| 91 | void QGeoMap::setCameraData(const QGeoCameraData &cameraData) | 
| 92 | { | 
| 93 |     Q_D(QGeoMap); | 
| 94 |     if (cameraData == d->m_cameraData) | 
| 95 |         return; | 
| 96 |     d->m_cameraData = cameraData; | 
| 97 |     d->m_geoProjection->setCameraData(cameraData, force: false); | 
| 98 |     d->changeCameraData(oldCameraData: cameraData); | 
| 99 |     emit cameraDataChanged(cameraData: d->m_cameraData); | 
| 100 | } | 
| 101 |  | 
| 102 | void QGeoMap::setCameraCapabilities(const QGeoCameraCapabilities &cameraCapabilities) | 
| 103 | { | 
| 104 |     Q_D(QGeoMap); | 
| 105 |     d->setCameraCapabilities(cameraCapabilities); | 
| 106 | } | 
| 107 |  | 
| 108 | bool QGeoMap::handleEvent(QEvent *event) | 
| 109 | { | 
| 110 |     Q_UNUSED(event); | 
| 111 |     return false; | 
| 112 | } | 
| 113 |  | 
| 114 | bool QGeoMap::setBearing(qreal bearing, const QGeoCoordinate &coordinate) //FIXME visibleArea | 
| 115 | { | 
| 116 |     Q_D(QGeoMap); | 
| 117 |     bool res = d->m_geoProjection->setBearing(bearing, coordinate); | 
| 118 |     if (!res) | 
| 119 |         return false; | 
| 120 |  | 
| 121 |     setCameraData(geoProjection().cameraData()); | 
| 122 |     return true; | 
| 123 | } | 
| 124 |  | 
| 125 | bool QGeoMap::anchorCoordinateToPoint(const QGeoCoordinate &coordinate, const QPointF &anchorPoint) | 
| 126 | { | 
| 127 |     Q_D(QGeoMap); | 
| 128 |     QGeoCoordinate newCenter = geoProjection().anchorCoordinateToPoint(coordinate, anchorPoint); | 
| 129 |     newCenter.setLatitude(qBound(min: d->m_minimumViewportLatitude, val: newCenter.latitude(), max: d->m_maximumViewportLatitude)); | 
| 130 |     QGeoCameraData data = cameraData(); | 
| 131 |     if (data.center() != newCenter) { | 
| 132 |         data.setCenter(newCenter); | 
| 133 |         setCameraData(data); | 
| 134 |         return true; | 
| 135 |     } | 
| 136 |     return false; | 
| 137 | } | 
| 138 |  | 
| 139 | bool QGeoMap::fitViewportToGeoRectangle(const QGeoRectangle &rectangle, const QMargins &borders) | 
| 140 | { | 
| 141 |     Q_UNUSED(rectangle); | 
| 142 |     Q_UNUSED(borders); | 
| 143 |     return false; | 
| 144 | } | 
| 145 |  | 
| 146 | QGeoShape QGeoMap::visibleRegion() const | 
| 147 | { | 
| 148 |     return geoProjection().visibleRegion(); | 
| 149 | } | 
| 150 |  | 
| 151 | const QGeoCameraData &QGeoMap::cameraData() const | 
| 152 | { | 
| 153 |     Q_D(const QGeoMap); | 
| 154 |     return d->m_cameraData; | 
| 155 | } | 
| 156 |  | 
| 157 | void QGeoMap::setActiveMapType(const QGeoMapType type) | 
| 158 | { | 
| 159 |     Q_D(QGeoMap); | 
| 160 |     if (type == d->m_activeMapType) | 
| 161 |         return; | 
| 162 |     d->m_activeMapType = type; | 
| 163 |     d->setCameraCapabilities(d->m_engine->cameraCapabilities(mapId: type.mapId())); // emits | 
| 164 |     d->changeActiveMapType(mapType: type); | 
| 165 |     emit activeMapTypeChanged(); | 
| 166 | } | 
| 167 |  | 
| 168 | const QGeoMapType QGeoMap::activeMapType() const | 
| 169 | { | 
| 170 |     Q_D(const QGeoMap); | 
| 171 |     return d->m_activeMapType; | 
| 172 | } | 
| 173 |  | 
| 174 | double QGeoMap::minimumZoom() const | 
| 175 | { | 
| 176 |     Q_D(const QGeoMap); | 
| 177 |     return d->m_geoProjection->minimumZoom(); | 
| 178 | } | 
| 179 |  | 
| 180 | double QGeoMap::maximumCenterLatitudeAtZoom(const QGeoCameraData &cameraData) const | 
| 181 | { | 
| 182 |     Q_D(const QGeoMap); | 
| 183 |     return d->maximumCenterLatitudeAtZoom(cameraData); | 
| 184 | } | 
| 185 |  | 
| 186 | double QGeoMap::minimumCenterLatitudeAtZoom(const QGeoCameraData &cameraData) const | 
| 187 | { | 
| 188 |     Q_D(const QGeoMap); | 
| 189 |     return d->minimumCenterLatitudeAtZoom(cameraData); | 
| 190 | } | 
| 191 |  | 
| 192 | double QGeoMap::mapWidth() const | 
| 193 | { | 
| 194 |     Q_D(const QGeoMap); | 
| 195 |     return d->mapWidth(); | 
| 196 | } | 
| 197 |  | 
| 198 | double QGeoMap::mapHeight() const | 
| 199 | { | 
| 200 |     Q_D(const QGeoMap); | 
| 201 |     return d->mapHeight(); | 
| 202 | } | 
| 203 |  | 
| 204 | const QGeoProjection &QGeoMap::geoProjection() const | 
| 205 | { | 
| 206 |     Q_D(const QGeoMap); | 
| 207 |     return *(d->m_geoProjection); | 
| 208 | } | 
| 209 |  | 
| 210 | QGeoCameraCapabilities QGeoMap::cameraCapabilities() const | 
| 211 | { | 
| 212 |     Q_D(const QGeoMap); | 
| 213 |     return d->m_cameraCapabilities; | 
| 214 | } | 
| 215 |  | 
| 216 | QGeoMap::Capabilities QGeoMap::capabilities() const | 
| 217 | { | 
| 218 |     return Capabilities(QGeoMap::SupportsNothing); | 
| 219 | } | 
| 220 |  | 
| 221 | void QGeoMap::prefetchData() | 
| 222 | { | 
| 223 |  | 
| 224 | } | 
| 225 |  | 
| 226 | void QGeoMap::clearData() | 
| 227 | { | 
| 228 |  | 
| 229 | } | 
| 230 |  | 
| 231 | void QGeoMap::addParameter(QGeoMapParameter *param) | 
| 232 | { | 
| 233 |     Q_D(QGeoMap); | 
| 234 |     if (param && !d->m_mapParameters.contains(t: param)) { | 
| 235 |         d->m_mapParameters.append(t: param); | 
| 236 |         d->addParameter(param); | 
| 237 |     } | 
| 238 | } | 
| 239 |  | 
| 240 | void QGeoMap::removeParameter(QGeoMapParameter *param) | 
| 241 | { | 
| 242 |     Q_D(QGeoMap); | 
| 243 |     if (param && d->m_mapParameters.contains(t: param)) { | 
| 244 |         d->removeParameter(param); | 
| 245 |         d->m_mapParameters.removeOne(t: param); | 
| 246 |     } | 
| 247 | } | 
| 248 |  | 
| 249 | void QGeoMap::clearParameters() | 
| 250 | { | 
| 251 |     Q_D(QGeoMap); | 
| 252 |     for (QGeoMapParameter *p : qAsConst(t&: d->m_mapParameters)) | 
| 253 |         d->removeParameter(param: p); | 
| 254 |     d->m_mapParameters.clear(); | 
| 255 | } | 
| 256 |  | 
| 257 | QGeoMap::ItemTypes QGeoMap::supportedMapItemTypes() const | 
| 258 | { | 
| 259 |     Q_D(const QGeoMap); | 
| 260 |     return d->supportedMapItemTypes(); | 
| 261 | } | 
| 262 |  | 
| 263 | void QGeoMap::addMapItem(QDeclarativeGeoMapItemBase *item) | 
| 264 | { | 
| 265 |     Q_D(QGeoMap); | 
| 266 |     if (item && !d->m_mapItems.contains(t: item) && d->supportedMapItemTypes() & item->itemType()) { | 
| 267 |         d->m_mapItems.append(t: item); | 
| 268 |         d->addMapItem(item); | 
| 269 |     } | 
| 270 | } | 
| 271 |  | 
| 272 | void QGeoMap::removeMapItem(QDeclarativeGeoMapItemBase *item) | 
| 273 | { | 
| 274 |     Q_D(QGeoMap); | 
| 275 |     if (item && d->m_mapItems.contains(t: item)) { | 
| 276 |         d->removeMapItem(item); | 
| 277 |         d->m_mapItems.removeOne(t: item); | 
| 278 |     } | 
| 279 | } | 
| 280 |  | 
| 281 | void QGeoMap::clearMapItems() | 
| 282 | { | 
| 283 |     Q_D(QGeoMap); | 
| 284 |     for (QDeclarativeGeoMapItemBase *p : d->m_mapItems) | 
| 285 |         d->removeMapItem(item: p); | 
| 286 |     d->m_mapItems.clear(); | 
| 287 | } | 
| 288 |  | 
| 289 | /*! | 
| 290 |     Fills obj with a backend-specific pimpl. | 
| 291 | */ | 
| 292 | bool QGeoMap::createMapObjectImplementation(QGeoMapObject *obj) | 
| 293 | { | 
| 294 |     Q_D(QGeoMap); | 
| 295 |     QExplicitlySharedDataPointer<QGeoMapObjectPrivate> pimpl = | 
| 296 |             QExplicitlySharedDataPointer<QGeoMapObjectPrivate>(d->createMapObjectImplementation(obj)); | 
| 297 |     if (pimpl.constData()) | 
| 298 |         return obj->setImplementation(pimpl); | 
| 299 |     return false; | 
| 300 | } | 
| 301 |  | 
| 302 | /*! | 
| 303 |     To be called in ~QGeoMapObjectPrivate overrides, if needed | 
| 304 | */ | 
| 305 | void QGeoMap::removeMapObject(QGeoMapObject * /*obj*/) | 
| 306 | { | 
| 307 | } | 
| 308 |  | 
| 309 | QList<QObject *> QGeoMap::mapObjectsAt(const QGeoCoordinate &/*coordinate*/) const | 
| 310 | { | 
| 311 |     return QList<QObject *>(); | 
| 312 | } | 
| 313 |  | 
| 314 | void QGeoMap::setItemToWindowTransform(const QTransform &itemToWindowTransform) | 
| 315 | { | 
| 316 |     Q_D(QGeoMap); | 
| 317 |     d->m_geoProjection->setItemToWindowTransform(itemToWindowTransform); | 
| 318 | } | 
| 319 |  | 
| 320 | void QGeoMap::setVisibleArea(const QRectF &visibleArea) | 
| 321 | { | 
| 322 |     Q_D(QGeoMap); | 
| 323 |     const QRectF &va = d->visibleArea(); | 
| 324 |     d->setVisibleArea(visibleArea); | 
| 325 |     if (va != d->visibleArea()) | 
| 326 |         emit visibleAreaChanged(); | 
| 327 | } | 
| 328 |  | 
| 329 | QRectF QGeoMap::visibleArea() const | 
| 330 | { | 
| 331 |     Q_D(const QGeoMap); | 
| 332 |     return d->visibleArea(); | 
| 333 | } | 
| 334 |  | 
| 335 | QList<QGeoMapObject *> QGeoMap::mapObjects() const | 
| 336 | { | 
| 337 |     Q_D(const QGeoMap); | 
| 338 |     return d->mapObjects(); | 
| 339 | } | 
| 340 |  | 
| 341 | QString QGeoMap::copyrightsStyleSheet() const | 
| 342 | { | 
| 343 |     return QStringLiteral("#copyright-root { background: rgba(255, 255, 255, 128) }" ); | 
| 344 | } | 
| 345 |  | 
| 346 | void QGeoMap::setAcceptedGestures(bool pan, bool flick, bool pinch, bool rotate, bool tilt) | 
| 347 | { | 
| 348 |     Q_UNUSED(pan); | 
| 349 |     Q_UNUSED(flick); | 
| 350 |     Q_UNUSED(pinch); | 
| 351 |     Q_UNUSED(rotate); | 
| 352 |     Q_UNUSED(tilt); | 
| 353 | } | 
| 354 |  | 
| 355 | void QGeoMap::setCopyrightVisible(bool visible) | 
| 356 | { | 
| 357 |     Q_D(QGeoMap); | 
| 358 |     if (d->m_copyrightVisible == visible) | 
| 359 |         return; | 
| 360 |  | 
| 361 |     d->m_copyrightVisible = visible; | 
| 362 | } | 
| 363 |  | 
| 364 | QGeoMapPrivate::QGeoMapPrivate(QGeoMappingManagerEngine *engine, QGeoProjection *geoProjection) | 
| 365 |     : QObjectPrivate(), | 
| 366 |       m_geoProjection(geoProjection), | 
| 367 |       m_engine(engine), | 
| 368 |       m_activeMapType(QGeoMapType()) | 
| 369 | { | 
| 370 |     // Setting the default camera caps without emitting anything | 
| 371 |     if (engine) | 
| 372 |         m_cameraCapabilities = m_engine->cameraCapabilities(mapId: m_activeMapType.mapId()); | 
| 373 | } | 
| 374 |  | 
| 375 | QGeoMapPrivate::~QGeoMapPrivate() | 
| 376 | { | 
| 377 |     if (m_geoProjection) | 
| 378 |         delete m_geoProjection; | 
| 379 | } | 
| 380 |  | 
| 381 | void QGeoMapPrivate::setCameraCapabilities(const QGeoCameraCapabilities &cameraCapabilities) | 
| 382 | { | 
| 383 |     Q_Q(QGeoMap); | 
| 384 |     if (m_cameraCapabilities == cameraCapabilities) | 
| 385 |         return; | 
| 386 |     QGeoCameraCapabilities oldCaps = m_cameraCapabilities; | 
| 387 |     m_cameraCapabilities = cameraCapabilities; | 
| 388 |     emit q->cameraCapabilitiesChanged(oldCameraCapabilities: oldCaps); | 
| 389 | } | 
| 390 |  | 
| 391 | const QGeoCameraCapabilities &QGeoMapPrivate::cameraCapabilities() const | 
| 392 | { | 
| 393 |     return m_cameraCapabilities; | 
| 394 | } | 
| 395 |  | 
| 396 | const QGeoMapPrivate *QGeoMapPrivate::get(const QGeoMap &map) | 
| 397 | { | 
| 398 |     return map.d_func(); | 
| 399 | } | 
| 400 |  | 
| 401 | void QGeoMapPrivate::addParameter(QGeoMapParameter *param) | 
| 402 | { | 
| 403 |     Q_UNUSED(param); | 
| 404 | } | 
| 405 |  | 
| 406 | void QGeoMapPrivate::removeParameter(QGeoMapParameter *param) | 
| 407 | { | 
| 408 |     Q_UNUSED(param); | 
| 409 | } | 
| 410 |  | 
| 411 | QGeoMap::ItemTypes QGeoMapPrivate::supportedMapItemTypes() const | 
| 412 | { | 
| 413 |     return QGeoMap::NoItem; | 
| 414 | } | 
| 415 |  | 
| 416 | void QGeoMapPrivate::addMapItem(QDeclarativeGeoMapItemBase *item) | 
| 417 | { | 
| 418 |     Q_UNUSED(item); | 
| 419 | } | 
| 420 |  | 
| 421 | void QGeoMapPrivate::removeMapItem(QDeclarativeGeoMapItemBase *item) | 
| 422 | { | 
| 423 |     Q_UNUSED(item); | 
| 424 | } | 
| 425 |  | 
| 426 | QGeoMapObjectPrivate *QGeoMapPrivate::createMapObjectImplementation(QGeoMapObject *obj) | 
| 427 | { | 
| 428 |     Q_UNUSED(obj); | 
| 429 |     return nullptr; | 
| 430 | } | 
| 431 |  | 
| 432 | QList<QGeoMapObject *> QGeoMapPrivate::mapObjects() const | 
| 433 | { | 
| 434 |     return QList<QGeoMapObject *>(); | 
| 435 | } | 
| 436 |  | 
| 437 | double QGeoMapPrivate::mapWidth() const | 
| 438 | { | 
| 439 |     if (m_geoProjection->projectionType() == QGeoProjection::ProjectionWebMercator) | 
| 440 |         return static_cast<const QGeoProjectionWebMercator *>(m_geoProjection)->mapWidth(); | 
| 441 |     return 0; // override this for maps supporting other projections | 
| 442 | } | 
| 443 |  | 
| 444 | double QGeoMapPrivate::mapHeight() const | 
| 445 | { | 
| 446 |     if (m_geoProjection->projectionType() == QGeoProjection::ProjectionWebMercator) | 
| 447 |         return static_cast<const QGeoProjectionWebMercator *>(m_geoProjection)->mapHeight(); | 
| 448 |     return 0; // override this for maps supporting other projections | 
| 449 | } | 
| 450 |  | 
| 451 | void QGeoMapPrivate::setCopyrightVisible(bool visible) | 
| 452 | { | 
| 453 |     m_copyrightVisible = visible; | 
| 454 | } | 
| 455 |  | 
| 456 | bool QGeoMapPrivate::copyrightVisible() const | 
| 457 | { | 
| 458 |     return m_copyrightVisible; | 
| 459 | } | 
| 460 |  | 
| 461 | double QGeoMapPrivate::maximumCenterLatitudeAtZoom(const QGeoCameraData &cameraData) const | 
| 462 | { | 
| 463 |     m_maximumViewportLatitude = m_geoProjection->maximumCenterLatitudeAtZoom(cameraData); | 
| 464 |     return m_maximumViewportLatitude; | 
| 465 | } | 
| 466 |  | 
| 467 | double QGeoMapPrivate::minimumCenterLatitudeAtZoom(const QGeoCameraData &cameraData) const | 
| 468 | { | 
| 469 |     m_minimumViewportLatitude = m_geoProjection->minimumCenterLatitudeAtZoom(cameraData); | 
| 470 |     return m_minimumViewportLatitude; | 
| 471 | } | 
| 472 |  | 
| 473 | void QGeoMapPrivate::setVisibleArea(const QRectF &/*visibleArea*/) | 
| 474 | { | 
| 475 |  | 
| 476 | } | 
| 477 |  | 
| 478 | QRectF QGeoMapPrivate::visibleArea() const | 
| 479 | { | 
| 480 |     return QRectF(); | 
| 481 | } | 
| 482 |  | 
| 483 | QRectF QGeoMapPrivate::clampVisibleArea(const QRectF &visibleArea) const | 
| 484 | { | 
| 485 |     qreal xp = qMin<qreal>(a: visibleArea.x(), b: qMax(a: m_viewportSize.width() - 1, b: 0)); | 
| 486 |     qreal yp = qMin<qreal>(a: visibleArea.y(), b: qMax(a: m_viewportSize.height() - 1, b: 0)); | 
| 487 |     qreal w = qMin<qreal>(a: visibleArea.width(), b: qMax<qreal>(a: m_viewportSize.width() - xp, b: 0)); | 
| 488 |     qreal h = qMin<qreal>(a: visibleArea.height(), b: qMax<qreal>(a: m_viewportSize.height() - yp, b: 0)); | 
| 489 |     return QRectF(xp, yp, w, h); | 
| 490 | } | 
| 491 |  | 
| 492 | QT_END_NAMESPACE | 
| 493 |  |