Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
use of enumerations for DB container type
GPKG or SpatiaLite
  • Loading branch information
signedav committed Jul 6, 2018
1 parent 8232cb7 commit 774b4f6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand All @@ -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 )
Expand Down Expand Up @@ -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;
Expand All @@ -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 );
Expand Down
12 changes: 9 additions & 3 deletions src/core/qgsofflineediting.h
Expand Up @@ -49,6 +49,12 @@ class CORE_EXPORT QgsOfflineEditing : public QObject
UpdateGeometries
};

enum ContainerType
{
SpatiaLite,
GPKG
};

QgsOfflineEditing();

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/offline_editing/offline_editing_plugin.cpp
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/plugins/offline_editing/offline_editing_plugin_gui.cpp
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/offline_editing/offline_editing_plugin_gui.h
Expand Up @@ -23,6 +23,7 @@

#include "ui_offline_editing_plugin_guibase.h"

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

class QgsSelectLayerTreeModel : public QgsLayerTreeModel
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgsofflineediting.cpp
Expand Up @@ -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)" ) );
Expand All @@ -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)" ) );
Expand Down

0 comments on commit 774b4f6

Please sign in to comment.