QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsofflineediting.h
Go to the documentation of this file.
1 /***************************************************************************
2  offline_editing.h
3 
4  Offline Editing Plugin
5  a QGIS plugin
6  --------------------------------------
7  Date : 22-Jul-2010
8  Copyright : (C) 2010 by Sourcepole
9  Email : info at sourcepole.ch
10  ***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #ifndef QGS_OFFLINE_EDITING_H
20 #define QGS_OFFLINE_EDITING_H
21 
22 #include "qgis_core.h"
23 #include "qgsfeature.h"
24 #include "qgssqliteutils.h"
25 
26 #include <QObject>
27 #include <QString>
28 
29 class QgsMapLayer;
30 class QgsVectorLayer;
31 
35 class CORE_EXPORT QgsOfflineEditing : public QObject
36 {
37  Q_OBJECT
38 
39  public:
41  {
42  CopyFeatures = 0,
48  UpdateGeometries
49  };
50 
53  {
55  GPKG
56  };
57 
59 
69  bool convertToOfflineProject( const QString &offlineDataPath, const QString &offlineDbFile, const QStringList &layerIds, bool onlySelected = false, ContainerType containerType = SpatiaLite, const QString &layerNameSuffix = QStringLiteral( " (offline)" ) );
70 
72  bool isOfflineProject() const;
73 
74 
79  void synchronize( bool useTransaction = false );
80 
81  signals:
82 
86  void progressStarted();
87 
94  void layerProgressUpdated( int layer, int numLayers );
95 
102  void progressModeSet( QgsOfflineEditing::ProgressMode mode, long long maximum );
103 
108  void progressUpdated( long long progress );
109 
111  void progressStopped();
112 
118  void warning( const QString &title, const QString &message );
119 
120  private:
121  void initializeSpatialMetadata( sqlite3 *sqlite_handle );
122  bool createOfflineDb( const QString &offlineDbPath, ContainerType containerType = SpatiaLite );
123  void createLoggingTables( sqlite3 *db );
124 
125  void convertToOfflineLayer( QgsVectorLayer *layer, sqlite3 *db, const QString &offlineDbPath, bool onlySelected, ContainerType containerType = SpatiaLite, const QString &layerNameSuffix = QStringLiteral( " (offline)" ) );
126 
127  void applyAttributesAdded( QgsVectorLayer *remoteLayer, sqlite3 *db, int layerId, int commitNo );
128  void applyFeaturesAdded( QgsVectorLayer *offlineLayer, QgsVectorLayer *remoteLayer, sqlite3 *db, int layerId );
129  void applyFeaturesRemoved( QgsVectorLayer *remoteLayer, sqlite3 *db, int layerId );
130  void applyAttributeValueChanges( QgsVectorLayer *offlineLayer, QgsVectorLayer *remoteLayer, sqlite3 *db, int layerId, int commitNo );
131  void applyGeometryChanges( QgsVectorLayer *remoteLayer, sqlite3 *db, int layerId, int commitNo );
132  void updateFidLookup( QgsVectorLayer *remoteLayer, sqlite3 *db, int layerId );
133 
137  int getLayerPkIdx( const QgsVectorLayer *layer ) const;
138 
139  QMap<int, int> attributeLookup( QgsVectorLayer *offlineLayer, QgsVectorLayer *remoteLayer );
140 
141  void showWarning( const QString &message );
142 
143  sqlite3_database_unique_ptr openLoggingDb();
144  int getOrCreateLayerId( sqlite3 *db, const QString &qgisLayerId );
145  int getCommitNo( sqlite3 *db );
146  void increaseCommitNo( sqlite3 *db );
147  void addFidLookup( sqlite3 *db, int layerId, QgsFeatureId offlineFid, QgsFeatureId remoteFid, const QString &remotePk );
148  QgsFeatureId remoteFid( sqlite3 *db, int layerId, QgsFeatureId offlineFid, QgsVectorLayer *remoteLayer );
149  QgsFeatureId offlineFid( sqlite3 *db, int layerId, QgsFeatureId remoteFid );
150  bool isAddedFeature( sqlite3 *db, int layerId, QgsFeatureId fid );
151 
152  int sqlExec( sqlite3 *db, const QString &sql );
153  int sqlQueryInt( sqlite3 *db, const QString &sql, int defaultValue );
154  QString sqlQueryStr( sqlite3 *db, const QString &sql, QString &defaultValue );
155  QList<int> sqlQueryInts( sqlite3 *db, const QString &sql );
156  QString sqlEscape( QString value ) const;
157 
158  QList<QgsField> sqlQueryAttributesAdded( sqlite3 *db, const QString &sql );
159  QgsFeatureIds sqlQueryFeaturesRemoved( sqlite3 *db, const QString &sql );
160 
161  struct AttributeValueChange
162  {
163  QgsFeatureId fid;
164  int attr;
165  QString value;
166  };
167  typedef QList<AttributeValueChange> AttributeValueChanges;
168  AttributeValueChanges sqlQueryAttributeValueChanges( sqlite3 *db, const QString &sql );
169 
170  struct GeometryChange
171  {
172  QgsFeatureId fid;
173  QString geom_wkt;
174  };
175  typedef QList<GeometryChange> GeometryChanges;
176  GeometryChanges sqlQueryGeometryChanges( sqlite3 *db, const QString &sql );
177 
178  private slots:
179  void setupLayer( QgsMapLayer *layer );
180  void committedAttributesAdded( const QString &qgisLayerId, const QList<QgsField> &addedAttributes );
181  void committedFeaturesAdded( const QString &qgisLayerId, const QgsFeatureList &addedFeatures );
182  void committedFeaturesRemoved( const QString &qgisLayerId, const QgsFeatureIds &deletedFeatureIds );
183  void committedAttributeValuesChanges( const QString &qgisLayerId, const QgsChangedAttributesMap &changedAttrsMap );
184  void committedGeometriesChanges( const QString &qgisLayerId, const QgsGeometryMap &changedGeometries );
185  void startListenFeatureChanges();
186  void stopListenFeatureChanges();
187 };
188 
189 #endif // QGS_OFFLINE_EDITING_H
sqlite3
struct sqlite3 sqlite3
Definition: qgscoordinatereferencesystem.h:61
qgsfeature.h
QgsChangedAttributesMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:868
QgsOfflineEditing::UpdateFeatures
@ UpdateFeatures
Definition: qgsofflineediting.h:47
QgsOfflineEditing::SpatiaLite
@ SpatiaLite
Definition: qgsofflineediting.h:54
QgsOfflineEditing::AddFields
@ AddFields
Definition: qgsofflineediting.h:44
QgsOfflineEditing
Definition: qgsofflineediting.h:35
QgsOfflineEditing::AddFeatures
@ AddFeatures
Definition: qgsofflineediting.h:45
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsOfflineEditing::RemoveFeatures
@ RemoveFeatures
Definition: qgsofflineediting.h:46
QgsOfflineEditing::ContainerType
ContainerType
Type of offline database container file.
Definition: qgsofflineediting.h:52
QgsOfflineEditing::ProcessFeatures
@ ProcessFeatures
Definition: qgsofflineediting.h:43
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
qgssqliteutils.h
QgsGeometryMap
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:877
sqlite3_database_unique_ptr
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
Definition: qgssqliteutils.h:118
QgsOfflineEditing::ProgressMode
ProgressMode
Definition: qgsofflineediting.h:40
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28