diff options
author | Paolo Angelelli <paolo.angelelli@qt.io> | 2016-08-09 13:16:49 +0200 |
---|---|---|
committer | Paolo Angelelli <paolo.angelelli@qt.io> | 2017-01-31 09:33:41 +0000 |
commit | 9ef81d240ad8d27ea482f9a15d2647a6eee1f7f2 (patch) | |
tree | 63d54d6010fe0e34faeb93ca40f8d17044f254c1 /src/location/declarativemaps/qdeclarativegeomap.cpp | |
parent | d7936f74bda527a85d69e7d1c412d79ee952d139 (diff) | |
download | qtlocation-9ef81d240ad8d27ea482f9a15d2647a6eee1f7f2.tar.gz |
Allow to create maps with groups of map items
This patch lets a user create an external qml file, and put multiple
map items inside a parent MapItemGroup{}, and add that element to a Map
item.
QDeclarativeGeoMap gets also two associated methods: addMapItemGroup and
removeMapItemGroup to deal with item groups at runtime.
Additionally, clearMapItems now clears also added item groups.
Task-number: QTBUG-55211
Change-Id: Ie4e602e4bda65fb56422b721be5fd34c54eb7954
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
Diffstat (limited to 'src/location/declarativemaps/qdeclarativegeomap.cpp')
-rw-r--r-- | src/location/declarativemaps/qdeclarativegeomap.cpp | 113 |
1 files changed, 104 insertions, 9 deletions
diff --git a/src/location/declarativemaps/qdeclarativegeomap.cpp b/src/location/declarativemaps/qdeclarativegeomap.cpp index 970161fa..00be6376 100644 --- a/src/location/declarativemaps/qdeclarativegeomap.cpp +++ b/src/location/declarativemaps/qdeclarativegeomap.cpp @@ -207,6 +207,18 @@ QDeclarativeGeoMap::~QDeclarativeGeoMap() } m_mapItems.clear(); + for (auto g: qAsConst(m_mapItemGroups)) { + if (!g) + continue; + const QList<QQuickItem *> quickKids = g->childItems(); + for (auto c: quickKids) { + QDeclarativeGeoMapItemBase *itemBase = qobject_cast<QDeclarativeGeoMapItemBase *>(c); + if (itemBase) + itemBase->setMap(0,0); + } + } + m_mapItemGroups.clear(); + delete m_copyrights.data(); m_copyrights.clear(); } @@ -506,6 +518,8 @@ QQuickGeoMapGestureArea *QDeclarativeGeoMap::gesture() /*! \internal + + This may happen before mappingManagerInitialized() */ void QDeclarativeGeoMap::populateMap() { @@ -525,6 +539,13 @@ void QDeclarativeGeoMap::populateMap() QDeclarativeGeoMapItemBase *mapItem = qobject_cast<QDeclarativeGeoMapItemBase *>(kids.at(i)); if (mapItem) { addMapItem(mapItem); + continue; + } + // Allow to add to the map Map items contained inside a parent QQuickItem, but only those at one level of nesting. + QDeclarativeGeoMapItemGroup *itemGroup = qobject_cast<QDeclarativeGeoMapItemGroup *>(kids.at(i)); + if (itemGroup) { + addMapItemGroup(itemGroup); + continue; } } } @@ -686,6 +707,17 @@ void QDeclarativeGeoMap::mappingManagerInitialized() } } + // Any map item groups that were added before the plugin was ready + // need to have setMap called again on their children map items + for (auto g: qAsConst(m_mapItemGroups)) { + const QList<QQuickItem *> quickKids = g->childItems(); + for (auto c: quickKids) { + QDeclarativeGeoMapItemBase *itemBase = qobject_cast<QDeclarativeGeoMapItemBase *>(c); + if (itemBase) + itemBase->setMap(this, m_map); + } + } + // All map parameters that were added before the plugin was ready // need to be added to m_map for (QDeclarativeGeoMapParameter *p : m_mapParameters) @@ -1406,7 +1438,9 @@ void QDeclarativeGeoMap::addMapItem(QDeclarativeGeoMapItemBase *item) { if (!item || item->quickMap()) return; - item->setParentItem(this); + // If the item comes from a MapItemGroup, do not reparent it. + if (!qobject_cast<QDeclarativeGeoMapItemGroup *>(item->parentItem())) + item->setParentItem(this); m_mapItems.append(item); if (m_map) { item->setMap(this, m_map); @@ -1542,8 +1576,9 @@ void QDeclarativeGeoMap::removeMapItem(QDeclarativeGeoMapItemBase *ptr) QPointer<QDeclarativeGeoMapItemBase> item(ptr); if (!m_mapItems.contains(item)) return; - item.data()->setParentItem(0); - item.data()->setMap(0, 0); + if (item->parentItem() == this) + item->setParentItem(0); + item->setMap(0, 0); // these can be optimized for perf, as we already check the 'contains' above m_mapItems.removeOne(item); emit mapItemsChanged(); @@ -1552,26 +1587,86 @@ void QDeclarativeGeoMap::removeMapItem(QDeclarativeGeoMapItemBase *ptr) /*! \qmlmethod void QtLocation::Map::clearMapItems() - Removes all items from the map. + Removes all items and item groups from the map. - \sa mapItems, addMapItem, removeMapItem + \sa mapItems, addMapItem, removeMapItem, addMapItemGroup, removeMapItemGroup */ void QDeclarativeGeoMap::clearMapItems() { m_map->clearMapItems(); if (m_mapItems.isEmpty()) return; - for (int i = 0; i < m_mapItems.count(); ++i) { - if (m_mapItems.at(i)) { - m_mapItems.at(i).data()->setParentItem(0); - m_mapItems.at(i).data()->setMap(0, 0); + for (auto i : qAsConst(m_mapItems)) { + if (i) { + i->setMap(0, 0); + if (i->parentItem() == this) + i->setParentItem(0); } } m_mapItems.clear(); + m_mapItemGroups.clear(); emit mapItemsChanged(); } /*! + \qmlmethod void QtLocation::Map::addMapItemGroup(MapItemGroup itemGroup) + + Adds the map items contained in the given \a itemGroup to the Map + (for example MapQuickItem, MapCircle). These items will be reparented, and the map + will be their new parent. Property bindings defined using \e{parent.} inside a MapItemGroup + will therefore not work. + + \sa MapItemGroup, removeMapItemGroup + + \since 5.9 +*/ +void QDeclarativeGeoMap::addMapItemGroup(QDeclarativeGeoMapItemGroup *itemGroup) +{ + if (!itemGroup) + return; + + QPointer<QDeclarativeGeoMapItemGroup> g(itemGroup); + if (m_mapItemGroups.contains(g)) + return; + + m_mapItemGroups.append(g); + const QList<QQuickItem *> quickKids = g->childItems(); + for (auto c: quickKids) { + QDeclarativeGeoMapItemBase *mapItem = qobject_cast<QDeclarativeGeoMapItemBase *>(c); + if (mapItem) + addMapItem(mapItem); + } + itemGroup->setParentItem(this); +} + +/*! + \qmlmethod void QtLocation::Map::removeMapItemGroup(MapItemGroup itemGroup) + + Removes \a itemGroup and the items contained therein from the Map. + + \sa MapItemGroup, addMapItemGroup + + \since 5.9 +*/ +void QDeclarativeGeoMap::removeMapItemGroup(QDeclarativeGeoMapItemGroup *itemGroup) +{ + if (!itemGroup) + return; + + QPointer<QDeclarativeGeoMapItemGroup> g(itemGroup); + if (!m_mapItemGroups.removeOne(g)) + return; + + const QList<QQuickItem *> quickKids = itemGroup->childItems(); + for (auto c: quickKids) { + QDeclarativeGeoMapItemBase *mapItem = qobject_cast<QDeclarativeGeoMapItemBase *>(c); + if (mapItem) + removeMapItem(mapItem); + } + itemGroup->setParentItem(0); +} + +/*! \qmlproperty MapType QtLocation::Map::activeMapType \brief Access to the currently active \l{MapType}{map type}. |