Skip to content
Permalink
Browse files

Merge pull request #35291 from m-kuhn/offline_editing_fix_added_features

Improve offline editing layer detection and code
  • Loading branch information
m-kuhn committed Mar 24, 2020
2 parents 9c4834d + ad9f968 commit 79e2dfd5f60dcd583df60161cd1e55ece1d682a6
Showing with 18 additions and 10 deletions.
  1. +18 −10 src/core/qgsofflineediting.cpp
@@ -38,6 +38,8 @@
#include "qgsogrutils.h"
#include "qgsvectorfilewriter.h"
#include "qgsvectorlayer.h"
#include "qgsproviderregistry.h"
#include "qgsprovidermetadata.h"

#include <QDir>
#include <QDomDocument>
@@ -135,7 +137,7 @@ bool QgsOfflineEditing::convertToOfflineProject( const QString &offlineDataPath,

QgsSnappingConfig snappingConfig = QgsProject::instance()->snappingConfig();

// copy selected vector layers to SpatiaLite
// copy selected vector layers to offline layer
for ( int i = 0; i < layerIds.count(); i++ )
{
emit layerProgressUpdated( i + 1, layerIds.count() );
@@ -162,7 +164,7 @@ bool QgsOfflineEditing::convertToOfflineProject( const QString &offlineDataPath,

QgsProject::instance()->setSnappingConfig( snappingConfig );

// restore join info on new SpatiaLite layer
// restore join info on new offline layer
QMap<QString, QgsVectorJoinList >::ConstIterator it;
for ( it = joinInfoBuffer.constBegin(); it != joinInfoBuffer.constEnd(); ++it )
{
@@ -1456,9 +1458,8 @@ void QgsOfflineEditing::committedAttributesAdded( const QString &qgisLayerId, co
int layerId = getOrCreateLayerId( database.get(), qgisLayerId );
int commitNo = getCommitNo( database.get() );

for ( QList<QgsField>::const_iterator it = addedAttributes.begin(); it != addedAttributes.end(); ++it )
for ( const QgsField &field : addedAttributes )
{
QgsField field = *it;
QString sql = QStringLiteral( "INSERT INTO 'log_added_attrs' VALUES ( %1, %2, '%3', %4, %5, %6, '%7' )" )
.arg( layerId )
.arg( commitNo )
@@ -1484,7 +1485,8 @@ void QgsOfflineEditing::committedFeaturesAdded( const QString &qgisLayerId, cons

// get new feature ids from db
QgsMapLayer *layer = QgsProject::instance()->mapLayer( qgisLayerId );
QgsDataSourceUri uri = QgsDataSourceUri( layer->source() );
QString dataSourceString = layer->source();
QgsDataSourceUri uri = QgsDataSourceUri( dataSourceString );

QString offlinePath = QgsProject::instance()->readPath( QgsProject::instance()->readEntry( PROJECT_ENTRY_SCOPE_OFFLINE, PROJECT_ENTRY_KEY_OFFLINE_DB_PATH ) );
QString tableName;
@@ -1495,7 +1497,13 @@ void QgsOfflineEditing::committedFeaturesAdded( const QString &qgisLayerId, cons
}
else
{
tableName = uri.param( offlinePath + "|layername" );
QgsProviderMetadata *ogrProviderMetaData = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "ogr" ) );
QVariantMap decodedUri = ogrProviderMetaData->decodeUri( dataSourceString );
tableName = decodedUri.value( QStringLiteral( "layerName" ) ).toString();
if ( tableName.isEmpty() )
{
showWarning( tr( "Could not deduce table name from data source %1." ).arg( dataSourceString ) );
}
}

// only store feature ids
@@ -1519,19 +1527,19 @@ void QgsOfflineEditing::committedFeaturesRemoved( const QString &qgisLayerId, co
// insert log
int layerId = getOrCreateLayerId( database.get(), qgisLayerId );

for ( QgsFeatureIds::const_iterator it = deletedFeatureIds.begin(); it != deletedFeatureIds.end(); ++it )
for ( QgsFeatureId id : deletedFeatureIds )
{
if ( isAddedFeature( database.get(), layerId, *it ) )
if ( isAddedFeature( database.get(), layerId, id ) )
{
// remove from added features log
QString sql = QStringLiteral( "DELETE FROM 'log_added_features' WHERE \"layer_id\" = %1 AND \"fid\" = %2" ).arg( layerId ).arg( *it );
QString sql = QStringLiteral( "DELETE FROM 'log_added_features' WHERE \"layer_id\" = %1 AND \"fid\" = %2" ).arg( layerId ).arg( id );
sqlExec( database.get(), sql );
}
else
{
QString sql = QStringLiteral( "INSERT INTO 'log_removed_features' VALUES ( %1, %2)" )
.arg( layerId )
.arg( *it );
.arg( id );
sqlExec( database.get(), sql );
}
}

0 comments on commit 79e2dfd

Please sign in to comment.
You can’t perform that action at this time.