Skip to content

Commit 33a6aa6

Browse files
committed
Merge pull request #388 from mwa/offline_editing_api
Move QgsOfflineEditing to core
2 parents ce64ede + a1743ab commit 33a6aa6

8 files changed

+213
-44
lines changed

python/core/core.sip

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
%Include qgsmessageoutput.sip
4343
%Include qgsmimedatautils.sip
4444
%Include qgsnetworkaccessmanager.sip
45+
%Include qgsofflineediting.sip
4546
%Include qgsoverlayobject.sip
4647
%Include qgsowsconnection.sip
4748
%Include qgspaintenginehack.sip

python/core/qgsofflineediting.sip

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/** \class QgsOfflineEditing
2+
* \brief Class for accessing functionality of Offline Editing Plugin.
3+
* This class can be used to access offline editing functionality from plugins.
4+
*/
5+
class QgsOfflineEditing : QObject
6+
{
7+
%TypeHeaderCode
8+
#include <qgsofflineediting.h>
9+
%End
10+
11+
public:
12+
enum ProgressMode {
13+
CopyFeatures = 0,
14+
ProcessFeatures,
15+
AddFields,
16+
AddFeatures,
17+
RemoveFeatures,
18+
UpdateFeatures,
19+
UpdateGeometries
20+
};
21+
22+
QgsOfflineEditing();
23+
~QgsOfflineEditing();
24+
25+
/** convert current project for offline editing
26+
* @param offlineDataPath path to offline db file
27+
* @param offlineDbFile offline db file name
28+
* @param layerIds list of layer names to convert
29+
*/
30+
bool convertToOfflineProject( const QString& offlineDataPath, const QString& offlineDbFile, const QStringList& layerIds );
31+
32+
/** return true if current project is offline */
33+
bool isOfflineProject();
34+
35+
/** synchronize to remote layers */
36+
void synchronize();
37+
38+
signals:
39+
/** emit a signal that processing has started */
40+
void progressStarted();
41+
42+
/** emit a signal that the next layer of numLayers has started processing
43+
* @param layer current layer index
44+
* @param numLayers total number of layers
45+
*/
46+
void layerProgressUpdated( int layer, int numLayers );
47+
48+
/** emit a signal that sets the mode for the progress of the current operation
49+
* @param mode progress mode
50+
* @param maximum total number of entities to process in the current operation
51+
*/
52+
void progressModeSet( QgsOfflineEditing::ProgressMode mode, int maximum );
53+
54+
/** emit a signal with the progress of the current mode
55+
* @param progress current index of processed entities
56+
*/
57+
void progressUpdated( int progress );
58+
59+
/** emit a signal that processing of all layers has finished */
60+
void progressStopped();
61+
};

src/core/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ SET(QGIS_CORE_SRCS
8080
qgsmessagelog.cpp
8181
qgsnetworkreplyparser.cpp
8282
qgscredentials.cpp
83+
qgsofflineediting.cpp
8384
qgsoverlayobject.cpp
8485
qgsowsconnection.cpp
8586
qgspalgeometry.cpp
@@ -267,7 +268,7 @@ IF (WITH_INTERNAL_SPATIALITE)
267268
SET_SOURCE_FILES_PROPERTIES(spatialite/sqlite3.c spatialite/spatialite.c PROPERTIES COMPILE_FLAGS -w)
268269
ENDIF(MSVC)
269270

270-
INCLUDE_DIRECTORIES(BEFORE spatialite/headers/spatialite)
271+
INCLUDE_DIRECTORIES(BEFORE spatialite/headers/spatialite spatialite/headers)
271272
ENDIF (WITH_INTERNAL_SPATIALITE)
272273

273274
ADD_FLEX_FILES(QGIS_CORE_SRCS qgsexpressionlexer.ll)
@@ -288,6 +289,7 @@ SET(QGIS_CORE_MOC_HDRS
288289
qgsmessageoutput.h
289290
qgsmessagelog.h
290291
qgsnetworkreplyparser.h
292+
qgsofflineediting.h
291293
qgscredentials.h
292294
qgspluginlayer.h
293295
qgsproject.h
@@ -379,6 +381,7 @@ SET(QGIS_CORE_HDRS
379381
qgsmimedatautils.h
380382
qgsnetworkreplyparser.h
381383
qgscredentials.h
384+
qgsofflineediting.h
382385
qgsoverlayobjectpositionmanager.h
383386
qgsowsconnection.h
384387
qgspallabeling.h

src/plugins/offline_editing/offline_editing.cpp src/core/qgsofflineediting.cpp

+25-31
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
* *
1717
***************************************************************************/
1818

19-
#include "offline_editing.h"
20-
#include "offline_editing_progress_dialog.h"
2119

2220
#include <qgsapplication.h>
2321
#include <qgsdatasourceuri.h>
2422
#include <qgsgeometry.h>
25-
#include <qgslegendinterface.h>
2623
#include <qgsmaplayer.h>
2724
#include <qgsmaplayerregistry.h>
25+
#include <qgsofflineediting.h>
2826
#include <qgsproject.h>
2927
#include <qgsvectordataprovider.h>
3028

@@ -50,9 +48,8 @@ extern "C"
5048
#define PROJECT_ENTRY_SCOPE_OFFLINE "OfflineEditingPlugin"
5149
#define PROJECT_ENTRY_KEY_OFFLINE_DB_PATH "/OfflineDbPath"
5250

53-
QgsOfflineEditing::QgsOfflineEditing( QgsOfflineEditingProgressDialog* progressDialog )
51+
QgsOfflineEditing::QgsOfflineEditing()
5452
{
55-
mProgressDialog = progressDialog;
5653
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( layerAdded( QgsMapLayer* ) ) );
5754
}
5855

@@ -85,19 +82,18 @@ bool QgsOfflineEditing::convertToOfflineProject( const QString& offlineDataPath,
8582
// create logging tables
8683
createLoggingTables( db );
8784

88-
mProgressDialog->setTitle( "Converting to offline project" );
89-
mProgressDialog->show();
85+
emit progressStarted();
9086

9187
// copy selected vector layers to SpatiaLite
9288
for ( int i = 0; i < layerIds.count(); i++ )
9389
{
94-
mProgressDialog->setCurrentLayer( i + 1, layerIds.count() );
90+
emit layerProgressUpdated( i + 1, layerIds.count() );
9591

9692
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerIds.at( i ) );
9793
copyVectorLayer( qobject_cast<QgsVectorLayer*>( layer ), db, dbPath );
9894
}
9995

100-
mProgressDialog->hide();
96+
emit progressStopped();
10197

10298
sqlite3_close( db );
10399

@@ -135,18 +131,16 @@ bool QgsOfflineEditing::isOfflineProject()
135131
return !QgsProject::instance()->readEntry( PROJECT_ENTRY_SCOPE_OFFLINE, PROJECT_ENTRY_KEY_OFFLINE_DB_PATH ).isEmpty();
136132
}
137133

138-
void QgsOfflineEditing::synchronize( QgsLegendInterface* legendInterface )
134+
void QgsOfflineEditing::synchronize()
139135
{
140-
Q_UNUSED( legendInterface );
141136
// open logging db
142137
sqlite3* db = openLoggingDb();
143138
if ( db == NULL )
144139
{
145140
return;
146141
}
147142

148-
mProgressDialog->setTitle( "Synchronizing to remote layers" );
149-
mProgressDialog->show();
143+
emit progressStarted();
150144

151145
// restore and sync remote layers
152146
QList<QgsMapLayer*> offlineLayers;
@@ -164,7 +158,7 @@ void QgsOfflineEditing::synchronize( QgsLegendInterface* legendInterface )
164158
{
165159
QgsMapLayer* layer = offlineLayers[l];
166160

167-
mProgressDialog->setCurrentLayer( l + 1, offlineLayers.count() );
161+
emit layerProgressUpdated( l + 1, offlineLayers.count() );
168162

169163
QString remoteSource = layer->customProperty( CUSTOM_PROPERTY_REMOTE_SOURCE, "" ).toString();
170164
QString remoteProvider = layer->customProperty( CUSTOM_PROPERTY_REMOTE_PROVIDER, "" ).toString();
@@ -246,7 +240,7 @@ void QgsOfflineEditing::synchronize( QgsLegendInterface* legendInterface )
246240
}
247241
}
248242

249-
mProgressDialog->hide();
243+
emit progressStopped();
250244

251245
sqlite3_close( db );
252246
}
@@ -517,7 +511,7 @@ void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, con
517511

518512
layer->select( layer->pendingAllAttributesList(), QgsRectangle(), true, false );
519513

520-
mProgressDialog->setupProgressBar( tr( "%v / %m features copied" ), layer->featureCount() );
514+
emit progressModeSet( QgsOfflineEditing::CopyFeatures, layer->featureCount() );
521515
int featureCount = 1;
522516

523517
QList<QgsFeatureId> remoteFeatureIds;
@@ -538,11 +532,11 @@ void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, con
538532

539533
newLayer->addFeature( f, false );
540534

541-
mProgressDialog->setProgressValue( featureCount++ );
535+
emit progressUpdated( featureCount++ );
542536
}
543537
if ( newLayer->commitChanges() )
544538
{
545-
mProgressDialog->setupProgressBar( tr( "%v / %m features processed" ), layer->featureCount() );
539+
emit progressModeSet( QgsOfflineEditing::ProcessFeatures, layer->featureCount() );
546540
featureCount = 1;
547541

548542
// update feature id lookup
@@ -560,7 +554,7 @@ void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, con
560554
{
561555
addFidLookup( db, layerId, offlineFeatureIds.at( i ), remoteFeatureIds.at( i ) );
562556

563-
mProgressDialog->setProgressValue( featureCount++ );
557+
emit progressUpdated( featureCount++ );
564558
}
565559
sqlExec( db, "COMMIT" );
566560
}
@@ -592,7 +586,7 @@ void QgsOfflineEditing::applyAttributesAdded( QgsVectorLayer* remoteLayer, sqlit
592586
typeNameLookup[ nativeType.mType ] = nativeType.mTypeName;
593587
}
594588

595-
mProgressDialog->setupProgressBar( tr( "%v / %m fields added" ), fields.size() );
589+
emit progressModeSet( QgsOfflineEditing::AddFields, fields.size() );
596590

597591
for ( int i = 0; i < fields.size(); i++ )
598592
{
@@ -609,7 +603,7 @@ void QgsOfflineEditing::applyAttributesAdded( QgsVectorLayer* remoteLayer, sqlit
609603
showWarning( QString( "Could not add attribute '%1' of type %2" ).arg( field.name() ).arg( field.type() ) );
610604
}
611605

612-
mProgressDialog->setProgressValue( i + 1 );
606+
emit progressUpdated( i + 1 );
613607
}
614608
}
615609

@@ -630,7 +624,7 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
630624
}
631625

632626
// copy features to remote layer
633-
mProgressDialog->setupProgressBar( tr( "%v / %m features added" ), features.size() );
627+
emit progressModeSet( QgsOfflineEditing::AddFeatures, features.size() );
634628

635629
int i = 1;
636630
for ( QgsFeatureList::iterator it = features.begin(); it != features.end(); ++it )
@@ -650,7 +644,7 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
650644

651645
remoteLayer->addFeature( f, false );
652646

653-
mProgressDialog->setProgressValue( i++ );
647+
emit progressUpdated( i++ );
654648
}
655649
}
656650

@@ -659,15 +653,15 @@ void QgsOfflineEditing::applyFeaturesRemoved( QgsVectorLayer* remoteLayer, sqlit
659653
QString sql = QString( "SELECT \"fid\" FROM 'log_removed_features' WHERE \"layer_id\" = %1" ).arg( layerId );
660654
QgsFeatureIds values = sqlQueryFeaturesRemoved( db, sql );
661655

662-
mProgressDialog->setupProgressBar( tr( "%v / %m features removed" ), values.size() );
656+
emit progressModeSet( QgsOfflineEditing::RemoveFeatures, values.size() );
663657

664658
int i = 1;
665659
for ( QgsFeatureIds::const_iterator it = values.begin(); it != values.end(); ++it )
666660
{
667661
QgsFeatureId fid = remoteFid( db, layerId, *it );
668662
remoteLayer->deleteFeature( fid );
669663

670-
mProgressDialog->setProgressValue( i++ );
664+
emit progressUpdated( i++ );
671665
}
672666
}
673667

@@ -676,7 +670,7 @@ void QgsOfflineEditing::applyAttributeValueChanges( QgsVectorLayer* offlineLayer
676670
QString sql = QString( "SELECT \"fid\", \"attr\", \"value\" FROM 'log_feature_updates' WHERE \"layer_id\" = %1 AND \"commit_no\" = %2 " ).arg( layerId ).arg( commitNo );
677671
AttributeValueChanges values = sqlQueryAttributeValueChanges( db, sql );
678672

679-
mProgressDialog->setupProgressBar( tr( "%v / %m feature updates" ), values.size() );
673+
emit progressModeSet( QgsOfflineEditing::UpdateFeatures, values.size() );
680674

681675
QMap<int, int> attrLookup = attributeLookup( offlineLayer, remoteLayer );
682676

@@ -686,7 +680,7 @@ void QgsOfflineEditing::applyAttributeValueChanges( QgsVectorLayer* offlineLayer
686680

687681
remoteLayer->changeAttributeValue( fid, attrLookup[ values.at( i ).attr ], values.at( i ).value, false );
688682

689-
mProgressDialog->setProgressValue( i + 1 );
683+
emit progressUpdated( i + 1 );
690684
}
691685
}
692686

@@ -695,14 +689,14 @@ void QgsOfflineEditing::applyGeometryChanges( QgsVectorLayer* remoteLayer, sqlit
695689
QString sql = QString( "SELECT \"fid\", \"geom_wkt\" FROM 'log_geometry_updates' WHERE \"layer_id\" = %1 AND \"commit_no\" = %2" ).arg( layerId ).arg( commitNo );
696690
GeometryChanges values = sqlQueryGeometryChanges( db, sql );
697691

698-
mProgressDialog->setupProgressBar( tr( "%v / %m feature geometry updates" ), values.size() );
692+
emit progressModeSet( QgsOfflineEditing::UpdateGeometries, values.size() );
699693

700694
for ( int i = 0; i < values.size(); i++ )
701695
{
702696
QgsFeatureId fid = remoteFid( db, layerId, values.at( i ).fid );
703697
remoteLayer->changeGeometry( fid, QgsGeometry::fromWkt( values.at( i ).geom_wkt ) );
704698

705-
mProgressDialog->setProgressValue( i + 1 );
699+
emit progressUpdated( i + 1 );
706700
}
707701
}
708702

@@ -716,7 +710,7 @@ void QgsOfflineEditing::updateFidLookup( QgsVectorLayer* remoteLayer, sqlite3* d
716710
QgsFeature f;
717711
remoteLayer->select( QgsAttributeList(), QgsRectangle(), false, false );
718712

719-
mProgressDialog->setupProgressBar( tr( "%v / %m features processed" ), remoteLayer->featureCount() );
713+
emit progressModeSet( QgsOfflineEditing::ProcessFeatures, remoteLayer->featureCount() );
720714

721715
int i = 1;
722716
while ( remoteLayer->nextFeature( f ) )
@@ -726,7 +720,7 @@ void QgsOfflineEditing::updateFidLookup( QgsVectorLayer* remoteLayer, sqlite3* d
726720
newRemoteFids[ f.id()] = true;
727721
}
728722

729-
mProgressDialog->setProgressValue( i++ );
723+
emit progressUpdated( i++ );
730724
}
731725

732726
// get local added fids

0 commit comments

Comments
 (0)