Skip to content

Commit 9c3fa18

Browse files
committed
Fix map themes in combination w/ offline editing
1 parent e84829e commit 9c3fa18

5 files changed

+71
-5
lines changed

python/core/qgsmapthemecollection.sip

+11-5
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ class QgsMapThemeCollection : QObject
2727
bool operator==( const QgsMapThemeCollection::MapThemeLayerRecord& other ) const;
2828
bool operator!=( const QgsMapThemeCollection::MapThemeLayerRecord& other ) const;
2929

30-
//! Returns map layer or null if the layer does not exist anymore
3130
QgsMapLayer* layer() const;
3231

33-
//! Whether current style is valid and should be applied
32+
33+
void setLayer( QgsMapLayer* layer );
34+
35+
3436
bool usingCurrentStyle;
35-
//! Name of the current style of the layer
37+
3638
QString currentStyle;
37-
//! Whether checkedLegendItems should be applied
39+
3840
bool usingLegendItems;
39-
//! Rule keys of check legend items in layer tree model
41+
4042
QSet<QString> checkedLegendItems;
4143
};
4244

@@ -55,6 +57,10 @@ class QgsMapThemeCollection : QObject
5557

5658
//! Sets layer records for the theme.
5759
void setLayerRecords( const QList<QgsMapThemeCollection::MapThemeLayerRecord>& records );
60+
void removeLayerRecord( QgsMapLayer* layer );
61+
62+
63+
void addLayerRecord( const QgsMapThemeCollection::MapThemeLayerRecord& record );
5864

5965
//! Return set with only records for valid layers
6066
//! @note not available in python bindings

src/core/qgsmapthemecollection.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,20 @@ void QgsMapThemeCollection::layerStyleRenamed( const QString& oldName, const QSt
465465
emit mapThemesChanged();
466466
}
467467

468+
void QgsMapThemeCollection::MapThemeRecord::removeLayerRecord( QgsMapLayer* layer )
469+
{
470+
for ( int i = 0; i < mLayerRecords.length(); ++i )
471+
{
472+
if ( mLayerRecords.at( i ).layer() == layer )
473+
mLayerRecords.removeAt( i );
474+
}
475+
}
476+
477+
void QgsMapThemeCollection::MapThemeRecord::addLayerRecord( const QgsMapThemeCollection::MapThemeLayerRecord& record )
478+
{
479+
mLayerRecords.append( record );
480+
}
481+
468482
QHash<QgsMapLayer*, QgsMapThemeCollection::MapThemeLayerRecord> QgsMapThemeCollection::MapThemeRecord::validLayerRecords() const
469483
{
470484
QHash<QgsMapLayer*, MapThemeLayerRecord> validSet;
@@ -475,3 +489,8 @@ QHash<QgsMapLayer*, QgsMapThemeCollection::MapThemeLayerRecord> QgsMapThemeColle
475489
}
476490
return validSet;
477491
}
492+
493+
void QgsMapThemeCollection::MapThemeLayerRecord::setLayer( QgsMapLayer* layer )
494+
{
495+
mLayer = layer;
496+
}

src/core/qgsmapthemecollection.h

+9
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class CORE_EXPORT QgsMapThemeCollection : public QObject
7474
//! Returns map layer or null if the layer does not exist anymore
7575
QgsMapLayer* layer() const { return mLayer; }
7676

77+
//! Set the map layer for this record
78+
void setLayer( QgsMapLayer* layer );
79+
7780
//! Whether current style is valid and should be applied
7881
bool usingCurrentStyle;
7982
//! Name of the current style of the layer
@@ -112,6 +115,12 @@ class CORE_EXPORT QgsMapThemeCollection : public QObject
112115
//! Sets layer records for the theme.
113116
void setLayerRecords( const QList<MapThemeLayerRecord>& records ) { mLayerRecords = records; }
114117

118+
//! Removes a record for \a layer if present.
119+
void removeLayerRecord( QgsMapLayer* layer );
120+
121+
//! Add a new record for a layer.
122+
void addLayerRecord( const MapThemeLayerRecord& record );
123+
115124
//! Return set with only records for valid layers
116125
//! @note not available in python bindings
117126
QHash<QgsMapLayer*, MapThemeLayerRecord> validLayerRecords() const;

src/core/qgsofflineediting.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qgslogger.h"
3434
#include "qgsvectorlayerutils.h"
3535
#include "qgsrelationmanager.h"
36+
#include "qgsmapthemecollection.h"
3637

3738
#include <QDir>
3839
#include <QDomDocument>
@@ -256,6 +257,7 @@ void QgsOfflineEditing::synchronize()
256257
// copy style
257258
copySymbology( offlineLayer, remoteLayer );
258259
updateRelations( offlineLayer, remoteLayer );
260+
updateMapThemes( offlineLayer, remoteLayer );
259261

260262
// apply layer edit log
261263
QString qgisLayerId = layer->id();
@@ -614,6 +616,7 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
614616
}
615617

616618
updateRelations( layer, newLayer );
619+
updateMapThemes( layer, newLayer );
617620
// copy features
618621
newLayer->startEditing();
619622
QgsFeature f;
@@ -924,6 +927,29 @@ void QgsOfflineEditing::updateRelations( QgsVectorLayer* sourceLayer, QgsVectorL
924927
}
925928
}
926929

930+
void QgsOfflineEditing::updateMapThemes( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer )
931+
{
932+
QgsMapThemeCollection* mapThemeCollection = QgsProject::instance()->mapThemeCollection();
933+
QStringList mapThemeNames = mapThemeCollection->mapThemes();
934+
935+
Q_FOREACH ( const QString& mapThemeName, mapThemeNames )
936+
{
937+
QgsMapThemeCollection::MapThemeRecord record = mapThemeCollection->mapThemeState( mapThemeName );
938+
939+
Q_FOREACH ( QgsMapThemeCollection::MapThemeLayerRecord layerRecord, record.layerRecords() )
940+
{
941+
if ( layerRecord.layer() == sourceLayer )
942+
{
943+
layerRecord.setLayer( targetLayer );
944+
record.removeLayerRecord( sourceLayer );
945+
record.addLayerRecord( layerRecord );
946+
}
947+
}
948+
949+
QgsProject::instance()->mapThemeCollection()->update( mapThemeName, record );
950+
}
951+
}
952+
927953
// NOTE: use this to map column indices in case the remote geometry column is not last
928954
QMap<int, int> QgsOfflineEditing::attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer )
929955
{

src/core/qgsofflineediting.h

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ class CORE_EXPORT QgsOfflineEditing : public QObject
113113
* Updates all relations that reference or are referenced by the source layer to the targetLayer.
114114
*/
115115
void updateRelations( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer );
116+
117+
/**
118+
* Update all map themes that affect the source layer.
119+
*/
120+
void updateMapThemes( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer );
121+
116122
QMap<int, int> attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer );
117123

118124
void showWarning( const QString& message );

0 commit comments

Comments
 (0)