From 9c3fa187fa0406923305ad7cf03ba5cd0e6f052a Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Tue, 28 Feb 2017 19:31:55 +0100 Subject: [PATCH] Fix map themes in combination w/ offline editing --- python/core/qgsmapthemecollection.sip | 16 +++++++++++----- src/core/qgsmapthemecollection.cpp | 19 +++++++++++++++++++ src/core/qgsmapthemecollection.h | 9 +++++++++ src/core/qgsofflineediting.cpp | 26 ++++++++++++++++++++++++++ src/core/qgsofflineediting.h | 6 ++++++ 5 files changed, 71 insertions(+), 5 deletions(-) diff --git a/python/core/qgsmapthemecollection.sip b/python/core/qgsmapthemecollection.sip index 94ee5370393..af690029151 100644 --- a/python/core/qgsmapthemecollection.sip +++ b/python/core/qgsmapthemecollection.sip @@ -27,16 +27,18 @@ class QgsMapThemeCollection : QObject bool operator==( const QgsMapThemeCollection::MapThemeLayerRecord& other ) const; bool operator!=( const QgsMapThemeCollection::MapThemeLayerRecord& other ) const; - //! Returns map layer or null if the layer does not exist anymore QgsMapLayer* layer() const; - //! Whether current style is valid and should be applied + + void setLayer( QgsMapLayer* layer ); + + bool usingCurrentStyle; - //! Name of the current style of the layer + QString currentStyle; - //! Whether checkedLegendItems should be applied + bool usingLegendItems; - //! Rule keys of check legend items in layer tree model + QSet checkedLegendItems; }; @@ -55,6 +57,10 @@ class QgsMapThemeCollection : QObject //! Sets layer records for the theme. void setLayerRecords( const QList& records ); + void removeLayerRecord( QgsMapLayer* layer ); + + + void addLayerRecord( const QgsMapThemeCollection::MapThemeLayerRecord& record ); //! Return set with only records for valid layers //! @note not available in python bindings diff --git a/src/core/qgsmapthemecollection.cpp b/src/core/qgsmapthemecollection.cpp index 1e06b7a80a5..97192d2ec16 100644 --- a/src/core/qgsmapthemecollection.cpp +++ b/src/core/qgsmapthemecollection.cpp @@ -465,6 +465,20 @@ void QgsMapThemeCollection::layerStyleRenamed( const QString& oldName, const QSt emit mapThemesChanged(); } +void QgsMapThemeCollection::MapThemeRecord::removeLayerRecord( QgsMapLayer* layer ) +{ + for ( int i = 0; i < mLayerRecords.length(); ++i ) + { + if ( mLayerRecords.at( i ).layer() == layer ) + mLayerRecords.removeAt( i ); + } +} + +void QgsMapThemeCollection::MapThemeRecord::addLayerRecord( const QgsMapThemeCollection::MapThemeLayerRecord& record ) +{ + mLayerRecords.append( record ); +} + QHash QgsMapThemeCollection::MapThemeRecord::validLayerRecords() const { QHash validSet; @@ -475,3 +489,8 @@ QHash QgsMapThemeColle } return validSet; } + +void QgsMapThemeCollection::MapThemeLayerRecord::setLayer( QgsMapLayer* layer ) +{ + mLayer = layer; +} diff --git a/src/core/qgsmapthemecollection.h b/src/core/qgsmapthemecollection.h index 275223daf0f..d472c9603d5 100644 --- a/src/core/qgsmapthemecollection.h +++ b/src/core/qgsmapthemecollection.h @@ -74,6 +74,9 @@ class CORE_EXPORT QgsMapThemeCollection : public QObject //! Returns map layer or null if the layer does not exist anymore QgsMapLayer* layer() const { return mLayer; } + //! Set the map layer for this record + void setLayer( QgsMapLayer* layer ); + //! Whether current style is valid and should be applied bool usingCurrentStyle; //! Name of the current style of the layer @@ -112,6 +115,12 @@ class CORE_EXPORT QgsMapThemeCollection : public QObject //! Sets layer records for the theme. void setLayerRecords( const QList& records ) { mLayerRecords = records; } + //! Removes a record for \a layer if present. + void removeLayerRecord( QgsMapLayer* layer ); + + //! Add a new record for a layer. + void addLayerRecord( const MapThemeLayerRecord& record ); + //! Return set with only records for valid layers //! @note not available in python bindings QHash validLayerRecords() const; diff --git a/src/core/qgsofflineediting.cpp b/src/core/qgsofflineediting.cpp index 5ae9cab51d6..d7221d56047 100644 --- a/src/core/qgsofflineediting.cpp +++ b/src/core/qgsofflineediting.cpp @@ -33,6 +33,7 @@ #include "qgslogger.h" #include "qgsvectorlayerutils.h" #include "qgsrelationmanager.h" +#include "qgsmapthemecollection.h" #include #include @@ -256,6 +257,7 @@ void QgsOfflineEditing::synchronize() // copy style copySymbology( offlineLayer, remoteLayer ); updateRelations( offlineLayer, remoteLayer ); + updateMapThemes( offlineLayer, remoteLayer ); // apply layer edit log QString qgisLayerId = layer->id(); @@ -614,6 +616,7 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit } updateRelations( layer, newLayer ); + updateMapThemes( layer, newLayer ); // copy features newLayer->startEditing(); QgsFeature f; @@ -924,6 +927,29 @@ void QgsOfflineEditing::updateRelations( QgsVectorLayer* sourceLayer, QgsVectorL } } +void QgsOfflineEditing::updateMapThemes( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer ) +{ + QgsMapThemeCollection* mapThemeCollection = QgsProject::instance()->mapThemeCollection(); + QStringList mapThemeNames = mapThemeCollection->mapThemes(); + + Q_FOREACH ( const QString& mapThemeName, mapThemeNames ) + { + QgsMapThemeCollection::MapThemeRecord record = mapThemeCollection->mapThemeState( mapThemeName ); + + Q_FOREACH ( QgsMapThemeCollection::MapThemeLayerRecord layerRecord, record.layerRecords() ) + { + if ( layerRecord.layer() == sourceLayer ) + { + layerRecord.setLayer( targetLayer ); + record.removeLayerRecord( sourceLayer ); + record.addLayerRecord( layerRecord ); + } + } + + QgsProject::instance()->mapThemeCollection()->update( mapThemeName, record ); + } +} + // NOTE: use this to map column indices in case the remote geometry column is not last QMap QgsOfflineEditing::attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer ) { diff --git a/src/core/qgsofflineediting.h b/src/core/qgsofflineediting.h index 4425a1832df..525018d6d62 100644 --- a/src/core/qgsofflineediting.h +++ b/src/core/qgsofflineediting.h @@ -113,6 +113,12 @@ class CORE_EXPORT QgsOfflineEditing : public QObject * Updates all relations that reference or are referenced by the source layer to the targetLayer. */ void updateRelations( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer ); + + /** + * Update all map themes that affect the source layer. + */ + void updateMapThemes( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer ); + QMap attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer ); void showWarning( const QString& message );