Skip to content

Commit

Permalink
Preserve custom layer order in offline editing
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Mar 23, 2017
1 parent 8cecf23 commit e5aacea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -34,6 +34,7 @@
#include "qgsvectorlayerutils.h"
#include "qgsrelationmanager.h"
#include "qgsmapthemecollection.h"
#include "qgslayertree.h"

#include <QDir>
#include <QDomDocument>
Expand Down Expand Up @@ -258,6 +259,7 @@ void QgsOfflineEditing::synchronize()
copySymbology( offlineLayer, remoteLayer );
updateRelations( offlineLayer, remoteLayer );
updateMapThemes( offlineLayer, remoteLayer );
updateLayerOrder( offlineLayer, remoteLayer );

// apply layer edit log
QString qgisLayerId = layer->id();
Expand Down Expand Up @@ -617,6 +619,7 @@ QgsVectorLayer *QgsOfflineEditing::copyVectorLayer( QgsVectorLayer *layer, sqlit

updateRelations( layer, newLayer );
updateMapThemes( layer, newLayer );
updateLayerOrder( layer, newLayer );
// copy features
newLayer->startEditing();
QgsFeature f;
Expand Down Expand Up @@ -950,6 +953,32 @@ void QgsOfflineEditing::updateMapThemes( QgsVectorLayer *sourceLayer, QgsVectorL
}
}

void QgsOfflineEditing::updateLayerOrder( QgsVectorLayer *sourceLayer, QgsVectorLayer *targetLayer )
{
QList<QgsMapLayer *> layerOrder = QgsProject::instance()->layerTreeRoot()->customLayerOrder();

auto iterator = layerOrder.begin();

while ( iterator != layerOrder.end() )
{
if ( *iterator == targetLayer )
{
iterator = layerOrder.erase( iterator );
if ( iterator == layerOrder.end() )
break;
}

if ( *iterator == sourceLayer )
{
*iterator = targetLayer;
}

++iterator;
}

QgsProject::instance()->layerTreeRoot()->setCustomLayerOrder( layerOrder );
}

// 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
5 changes: 5 additions & 0 deletions src/core/qgsofflineediting.h
Expand Up @@ -119,6 +119,11 @@ class CORE_EXPORT QgsOfflineEditing : public QObject
*/
void updateMapThemes( QgsVectorLayer *sourceLayer, QgsVectorLayer *targetLayer );

/**
* Preserve the layer order
*/
void updateLayerOrder( QgsVectorLayer *sourceLayer, QgsVectorLayer *targetLayer );

QMap<int, int> attributeLookup( QgsVectorLayer *offlineLayer, QgsVectorLayer *remoteLayer );

void showWarning( const QString &message );
Expand Down

0 comments on commit e5aacea

Please sign in to comment.