Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix map themes in combination w/ offline editing
  • Loading branch information
m-kuhn committed Mar 2, 2017
1 parent e84829e commit 9c3fa18
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
16 changes: 11 additions & 5 deletions python/core/qgsmapthemecollection.sip
Expand Up @@ -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<QString> checkedLegendItems;
};

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

//! Sets layer records for the theme.
void setLayerRecords( const QList<QgsMapThemeCollection::MapThemeLayerRecord>& 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
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsmapthemecollection.cpp
Expand Up @@ -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<QgsMapLayer*, QgsMapThemeCollection::MapThemeLayerRecord> QgsMapThemeCollection::MapThemeRecord::validLayerRecords() const
{
QHash<QgsMapLayer*, MapThemeLayerRecord> validSet;
Expand All @@ -475,3 +489,8 @@ QHash<QgsMapLayer*, QgsMapThemeCollection::MapThemeLayerRecord> QgsMapThemeColle
}
return validSet;
}

void QgsMapThemeCollection::MapThemeLayerRecord::setLayer( QgsMapLayer* layer )
{
mLayer = layer;
}
9 changes: 9 additions & 0 deletions src/core/qgsmapthemecollection.h
Expand Up @@ -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
Expand Down Expand Up @@ -112,6 +115,12 @@ class CORE_EXPORT QgsMapThemeCollection : public QObject
//! Sets layer records for the theme.
void setLayerRecords( const QList<MapThemeLayerRecord>& 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<QgsMapLayer*, MapThemeLayerRecord> validLayerRecords() const;
Expand Down
26 changes: 26 additions & 0 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -33,6 +33,7 @@
#include "qgslogger.h"
#include "qgsvectorlayerutils.h"
#include "qgsrelationmanager.h"
#include "qgsmapthemecollection.h"

#include <QDir>
#include <QDomDocument>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -614,6 +616,7 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
}

updateRelations( layer, newLayer );
updateMapThemes( layer, newLayer );
// copy features
newLayer->startEditing();
QgsFeature f;
Expand Down Expand Up @@ -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<int, int> QgsOfflineEditing::attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer )
{
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsofflineediting.h
Expand Up @@ -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<int, int> attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer );

void showWarning( const QString& message );
Expand Down

0 comments on commit 9c3fa18

Please sign in to comment.