summaryrefslogtreecommitdiff
path: root/src/location/declarativemaps/qdeclarativegeoroute.cpp
diff options
context:
space:
mode:
authorPaolo Angelelli <paolo.angelelli@qt.io>2017-07-25 17:11:32 +0200
committerBogDan Vatra <bogdan@kdab.com>2017-08-24 12:16:32 +0000
commit8ac6377e62af803b567449cdf30c669b92114cc4 (patch)
tree0a8d9d797df2dbc32524f28510712f2ceacde764 /src/location/declarativemaps/qdeclarativegeoroute.cpp
parent2dc1acb63777c983cfc4cbdbd2176a8dab112209 (diff)
downloadqtlocation-8ac6377e62af803b567449cdf30c669b92114cc4.tar.gz
Make QGeoRoute extensible
This change makes it possible to subclass QGeoRoute, QGeoRouteSegment or QGeoRouteManeuver, with custom private implementations. It also attempts to minimize the cost that creating a QDeclarativeGeoRoute currently has, by deferring the initialization of QDeclarativeGeoRoute::segments_ to the first access, and also populating the list only to the requested point. Change-Id: I4c87391bcc380ddca6523c748ebb97d2a44ed9d2 Reviewed-by: BogDan Vatra <bogdan@kdab.com>
Diffstat (limited to 'src/location/declarativemaps/qdeclarativegeoroute.cpp')
-rw-r--r--src/location/declarativemaps/qdeclarativegeoroute.cpp53
1 files changed, 41 insertions, 12 deletions
diff --git a/src/location/declarativemaps/qdeclarativegeoroute.cpp b/src/location/declarativemaps/qdeclarativegeoroute.cpp
index 039b1297..5540263c 100644
--- a/src/location/declarativemaps/qdeclarativegeoroute.cpp
+++ b/src/location/declarativemaps/qdeclarativegeoroute.cpp
@@ -37,6 +37,7 @@
#include "qdeclarativegeoroute_p.h"
#include "locationvaluetypehelper_p.h"
#include <QtLocation/private/qgeomap_p.h>
+#include <QtLocation/private/qgeoroute_p.h>
#include <QtQml/QQmlEngine>
#include <QtQml/qqmlinfo.h>
@@ -77,29 +78,37 @@ QT_BEGIN_NAMESPACE
*/
QDeclarativeGeoRoute::QDeclarativeGeoRoute(QObject *parent)
- : QObject(parent)
+ : QObject(parent), segmentsDirty_(true)
{
- this->init();
}
QDeclarativeGeoRoute::QDeclarativeGeoRoute(const QGeoRoute &route, QObject *parent)
- : QObject(parent),
- route_(route)
+ : QObject(parent), route_(route), segmentsDirty_(true)
{
- this->init();
}
QDeclarativeGeoRoute::~QDeclarativeGeoRoute() {}
-void QDeclarativeGeoRoute::init()
+void QDeclarativeGeoRoute::initSegments(unsigned int lastIndex) // -1 turns it into unsigned int max
{
+ if (!segmentsDirty_)
+ return;
+
QGeoRouteSegment segment = route_.firstRouteSegment();
+ unsigned int idx = 0;
+ unsigned int initialListSize = static_cast<unsigned int>(segments_.size());
while (segment.isValid()) {
- QDeclarativeGeoRouteSegment *routeSegment = new QDeclarativeGeoRouteSegment(segment, this);
- QQmlEngine::setContextForObject(routeSegment, QQmlEngine::contextForObject(this));
- segments_.append(routeSegment);
+ if (idx >= initialListSize) {
+ QDeclarativeGeoRouteSegment *routeSegment = new QDeclarativeGeoRouteSegment(segment, this);
+ QQmlEngine::setContextForObject(routeSegment, QQmlEngine::contextForObject(this));
+ segments_.append(routeSegment);
+ }
+ ++idx;
segment = segment.nextRouteSegment();
+ if (idx > lastIndex && segment.isValid()) // Do not clean segmentsDirty_ if there are still segments to initialize
+ return;
}
+ segmentsDirty_ = false;
}
/*!
@@ -229,7 +238,9 @@ QQmlListProperty<QDeclarativeGeoRouteSegment> QDeclarativeGeoRoute::segments()
void QDeclarativeGeoRoute::segments_append(QQmlListProperty<QDeclarativeGeoRouteSegment> *prop,
QDeclarativeGeoRouteSegment *segment)
{
- static_cast<QDeclarativeGeoRoute *>(prop->object)->appendSegment(segment);
+ QDeclarativeGeoRoute *declRoute = static_cast<QDeclarativeGeoRoute *>(prop->object);
+ declRoute->initSegments();
+ declRoute->appendSegment(segment);
}
/*!
@@ -237,7 +248,8 @@ void QDeclarativeGeoRoute::segments_append(QQmlListProperty<QDeclarativeGeoRoute
*/
int QDeclarativeGeoRoute::segments_count(QQmlListProperty<QDeclarativeGeoRouteSegment> *prop)
{
- return static_cast<QDeclarativeGeoRoute *>(prop->object)->segments_.count();
+ QDeclarativeGeoRoute *declRoute = static_cast<QDeclarativeGeoRoute *>(prop->object);
+ return declRoute->segmentsCount();
}
/*!
@@ -245,7 +257,9 @@ int QDeclarativeGeoRoute::segments_count(QQmlListProperty<QDeclarativeGeoRouteSe
*/
QDeclarativeGeoRouteSegment *QDeclarativeGeoRoute::segments_at(QQmlListProperty<QDeclarativeGeoRouteSegment> *prop, int index)
{
- return static_cast<QDeclarativeGeoRoute *>(prop->object)->segments_.at(index);
+ QDeclarativeGeoRoute *declRoute = static_cast<QDeclarativeGeoRoute *>(prop->object);
+ declRoute->initSegments(index); // init only what's needed.
+ return declRoute->segments_.at(index);
}
/*!
@@ -272,4 +286,19 @@ void QDeclarativeGeoRoute::clearSegments()
segments_.clear();
}
+/*!
+ \qmlmethod int QtLocation::Route::segmentsCount()
+
+ Returns the number of segments in the route
+
+ \sa RouteSegment
+
+ \since 5.11
+*/
+
+int QDeclarativeGeoRoute::segmentsCount() const
+{
+ return qMax(route_.d_ptr->segmentsCount(), segments_.count());
+}
+
QT_END_NAMESPACE