Skip to content
Permalink
Browse files

use of enumerations for DB container type

GPKG or SpatiaLite
  • Loading branch information
signedav committed Jul 6, 2018
1 parent 8232cb7 commit 774b4f60ef803156882b2cd70163eb6e67e7e82e
@@ -76,14 +76,14 @@ QgsOfflineEditing::QgsOfflineEditing()
* - remove remote layers
* - mark as offline project
*/
bool QgsOfflineEditing::convertToOfflineProject( const QString &offlineDataPath, const QString &offlineDbFile, const QStringList &layerIds, bool onlySelected, bool gpkg )
bool QgsOfflineEditing::convertToOfflineProject( const QString &offlineDataPath, const QString &offlineDbFile, const QStringList &layerIds, bool onlySelected, ContainerType dbContainerType )
{
if ( layerIds.isEmpty() )
{
return false;
}
QString dbPath = QDir( offlineDataPath ).absoluteFilePath( offlineDbFile );
if ( createOfflineDb( dbPath, gpkg ) )
if ( createOfflineDb( dbPath, dbContainerType ) )
{
spatialite_database_unique_ptr database;
int rc = database.open( dbPath );
@@ -138,7 +138,7 @@ bool QgsOfflineEditing::convertToOfflineProject( const QString &offlineDataPath,
if ( vl )
{
QString origLayerId = vl->id();
QgsVectorLayer *newLayer = copyVectorLayer( vl, database.get(), dbPath, onlySelected, gpkg );
QgsVectorLayer *newLayer = copyVectorLayer( vl, database.get(), dbPath, onlySelected, dbContainerType );
if ( newLayer )
{
layerIdMapping.insert( origLayerId, newLayer );
@@ -385,7 +385,7 @@ void QgsOfflineEditing::initializeSpatialMetadata( sqlite3 *sqlite_handle )
spatial_ref_sys_init( sqlite_handle, 0 );
}

bool QgsOfflineEditing::createOfflineDb( const QString &offlineDbPath, bool gpkg )
bool QgsOfflineEditing::createOfflineDb( const QString &offlineDbPath, ContainerType dbContainerType )
{
int ret;
char *errMsg = nullptr;
@@ -407,7 +407,7 @@ bool QgsOfflineEditing::createOfflineDb( const QString &offlineDbPath, bool gpkg
QString dbPath = newDb.fileName();

// creating geopackage
if ( gpkg )
if ( dbContainerType == GPKG )
{
OGRSFDriverH hGpkgDriver = OGRGetDriverByName( "GPKG" );
if ( !hGpkgDriver )
@@ -492,7 +492,7 @@ void QgsOfflineEditing::createLoggingTables( sqlite3 *db )
*/
}

QgsVectorLayer *QgsOfflineEditing::copyVectorLayer( QgsVectorLayer *layer, sqlite3 *db, const QString &offlineDbPath, bool onlySelected, bool gpkg )
QgsVectorLayer *QgsOfflineEditing::copyVectorLayer( QgsVectorLayer *layer, sqlite3 *db, const QString &offlineDbPath, bool onlySelected, ContainerType dbContainerType )
{
if ( !layer )
return nullptr;
@@ -503,7 +503,7 @@ QgsVectorLayer *QgsOfflineEditing::copyVectorLayer( QgsVectorLayer *layer, sqlit
// new layer
QgsVectorLayer *newLayer = nullptr;

if ( !gpkg )
if ( dbContainerType != GPKG )
{
// create table
QString sql = QStringLiteral( "CREATE TABLE '%1' (" ).arg( tableName );
@@ -49,6 +49,12 @@ class CORE_EXPORT QgsOfflineEditing : public QObject
UpdateGeometries
};

enum ContainerType
{
SpatiaLite,
GPKG
};

QgsOfflineEditing();

/**
@@ -58,7 +64,7 @@ class CORE_EXPORT QgsOfflineEditing : public QObject
* \param layerIds List of layer names to convert
* \param onlySelected Only copy selected features from layers where a selection is present
*/
bool convertToOfflineProject( const QString &offlineDataPath, const QString &offlineDbFile, const QStringList &layerIds, bool onlySelected = false, bool gpkg = false );
bool convertToOfflineProject( const QString &offlineDataPath, const QString &offlineDbFile, const QStringList &layerIds, bool onlySelected = false, ContainerType dbContainerType = SpatiaLite );

//! Returns true if current project is offline
bool isOfflineProject() const;
@@ -107,10 +113,10 @@ class CORE_EXPORT QgsOfflineEditing : public QObject

private:
void initializeSpatialMetadata( sqlite3 *sqlite_handle );
bool createOfflineDb( const QString &offlineDbPath, bool gpkg = false );
bool createOfflineDb( const QString &offlineDbPath, ContainerType dbContainerType = SpatiaLite );
void createLoggingTables( sqlite3 *db );

QgsVectorLayer *copyVectorLayer( QgsVectorLayer *layer, sqlite3 *db, const QString &offlineDbPath, bool onlySelected, bool gpkg );
QgsVectorLayer *copyVectorLayer( QgsVectorLayer *layer, sqlite3 *db, const QString &offlineDbPath, bool onlySelected, ContainerType dbContainerType = SpatiaLite );

void applyAttributesAdded( QgsVectorLayer *remoteLayer, sqlite3 *db, int layerId, int commitNo );
void applyFeaturesAdded( QgsVectorLayer *offlineLayer, QgsVectorLayer *remoteLayer, sqlite3 *db, int layerId );
@@ -104,7 +104,7 @@ void QgsOfflineEditingPlugin::convertProject()
}

mProgressDialog->setTitle( tr( "Converting to Offline Project" ) );
if ( mOfflineEditing->convertToOfflineProject( myPluginGui->offlineDataPath(), myPluginGui->offlineDbFile(), selectedLayerIds, myPluginGui->onlySelected(), myPluginGui->isGeopackage() ) )
if ( mOfflineEditing->convertToOfflineProject( myPluginGui->offlineDataPath(), myPluginGui->offlineDbFile(), selectedLayerIds, myPluginGui->onlySelected(), myPluginGui->dbContainerType() ) )
{
updateActions();
// Redraw, to make the offline layer visible
@@ -146,14 +146,17 @@ bool QgsOfflineEditingPluginGui::onlySelected() const
return mOnlySelectedCheckBox->checkState() == Qt::Checked;
}

bool QgsOfflineEditingPluginGui::isGeopackage() const
QgsOfflineEditing::ContainerType QgsOfflineEditingPluginGui::dbContainerType() const
{
return mSelectDatatypeCombo->currentIndex() == 0;
if ( mSelectDatatypeCombo->currentIndex() == 0 )
return QgsOfflineEditing::GPKG;
else
return QgsOfflineEditing::SpatiaLite;
}

void QgsOfflineEditingPluginGui::mBrowseButton_clicked()
{
if ( isGeopackage() )
if ( dbContainerType() == QgsOfflineEditing::GPKG )
{
//GeoPackage
QString fileName = QFileDialog::getSaveFileName( this,
@@ -23,6 +23,7 @@

#include "ui_offline_editing_plugin_guibase.h"

#include "qgsofflineediting.h"
#include "qgslayertreemodel.h"

class QgsSelectLayerTreeModel : public QgsLayerTreeModel
@@ -46,7 +47,7 @@ class QgsOfflineEditingPluginGui : public QDialog, private Ui::QgsOfflineEditing
QString offlineDbFile();
QStringList selectedLayerIds();
bool onlySelected() const;
bool isGeopackage() const;
QgsOfflineEditing::ContainerType dbContainerType() const;

public slots:
//! Change the selection of layers in the list
@@ -103,7 +103,7 @@ void TestQgsOfflineEditing::createSpatialiteAndSynchronizeBack()
QCOMPARE( mpLayer->fields().size(), numberOfFields );

//convert
mOfflineEditing->convertToOfflineProject( offlineDataPath, offlineDbFile, layerIds, false, false );
mOfflineEditing->convertToOfflineProject( offlineDataPath, offlineDbFile, layerIds, false, QgsOfflineEditing::SpatiaLite );

mpLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayers().first() );
QCOMPARE( mpLayer->name(), QStringLiteral( "points (offline)" ) );
@@ -126,7 +126,7 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
QCOMPARE( mpLayer->fields().size(), numberOfFields );

//convert
mOfflineEditing->convertToOfflineProject( offlineDataPath, offlineDbFile, layerIds, false, true );
mOfflineEditing->convertToOfflineProject( offlineDataPath, offlineDbFile, layerIds, false, QgsOfflineEditing::GPKG );

mpLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayers().first() );
QCOMPARE( mpLayer->name(), QStringLiteral( "points (offline)" ) );

0 comments on commit 774b4f6

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