Skip to content

Commit

Permalink
Backup layer join information when converting to offline project
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jan 22, 2015
1 parent 46490d4 commit 34dc427
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
57 changes: 49 additions & 8 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -20,14 +20,15 @@
#include "qgsapplication.h"
#include "qgsdatasourceuri.h"
#include "qgsgeometry.h"
#include "qgslayertreegroup.h"
#include "qgslayertreelayer.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsofflineediting.h"
#include "qgsproject.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayereditbuffer.h"
#include "qgslayertreegroup.h"
#include "qgslayertreelayer.h"
#include "qgsvectorlayerjoinbuffer.h"

#include <QDir>
#include <QDomDocument>
Expand Down Expand Up @@ -87,15 +88,55 @@ bool QgsOfflineEditing::convertToOfflineProject( const QString& offlineDataPath,

emit progressStarted();

QMap<QString, QgsVectorJoinList > joinInfoBuffer;
QMap<QString, QgsVectorLayer*> layerIdMapping;

for ( int i = 0; i < layerIds.count(); i++ )
{
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerIds.at( i ) );
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( layer );
const QgsVectorJoinList& joins = vl->vectorJoins();
joinInfoBuffer.insert( vl->id(), joins );
}

// copy selected vector layers to SpatiaLite
for ( int i = 0; i < layerIds.count(); i++ )
{
emit layerProgressUpdated( i + 1, layerIds.count() );

QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerIds.at( i ) );
copyVectorLayer( qobject_cast<QgsVectorLayer*>( layer ), db, dbPath );
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( layer );
QString origLayerId = vl->id();
QgsVectorLayer* newLayer = copyVectorLayer( vl, db, dbPath );

if ( newLayer )
{
layerIdMapping.insert( origLayerId, newLayer );
}
}

// restore join info on new spatialite layer
QMap<QString, QgsVectorJoinList >::ConstIterator it;
for ( it = joinInfoBuffer.constBegin(); it != joinInfoBuffer.constEnd(); ++it )
{
QgsVectorLayer* newLayer = layerIdMapping.value( it.key() );

if ( newLayer )
{
Q_FOREACH( QgsVectorJoinInfo join, it.value() )
{
QgsVectorLayer* newJoinedLayer = layerIdMapping.value( join.joinLayerId );
if ( newJoinedLayer )
{
// If the layer has been offline'd, update join information
join.joinLayerId = newJoinedLayer->id();
}
newLayer->addJoin( join );
}
}
}


emit progressStopped();

sqlite3_close( db );
Expand Down Expand Up @@ -396,11 +437,11 @@ void QgsOfflineEditing::createLoggingTables( sqlite3* db )
*/
}

void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, const QString& offlineDbPath )
QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, const QString& offlineDbPath )
{
if ( layer == NULL )
if ( layer == 0 )
{
return;
return 0;
}

QString tableName = layer->name();
Expand Down Expand Up @@ -531,8 +572,6 @@ void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, con
copySymbology( layer, newLayer );
}

// TODO: layer order

// copy features
newLayer->startEditing();
QgsFeature f;
Expand Down Expand Up @@ -599,7 +638,9 @@ void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, con
QgsMapLayerRegistry::instance()->removeMapLayers(
QStringList() << layer->id() );
}
return newLayer;
}
return 0;
}

void QgsOfflineEditing::applyAttributesAdded( QgsVectorLayer* remoteLayer, sqlite3* db, int layerId, int commitNo )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsofflineediting.h
Expand Up @@ -96,7 +96,7 @@ class CORE_EXPORT QgsOfflineEditing : public QObject
void initializeSpatialMetadata( sqlite3 *sqlite_handle );
bool createSpatialiteDB( const QString& offlineDbPath );
void createLoggingTables( sqlite3* db );
void copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, const QString& offlineDbPath );
QgsVectorLayer* copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, const QString& offlineDbPath );

void applyAttributesAdded( QgsVectorLayer* remoteLayer, sqlite3* db, int layerId, int commitNo );
void applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer, sqlite3* db, int layerId );
Expand Down

0 comments on commit 34dc427

Please sign in to comment.