Skip to content

Commit

Permalink
offline editing plugin: allow exporting non geometric layers
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Jan 14, 2015
1 parent b042d8e commit c3488f4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 43 deletions.
82 changes: 44 additions & 38 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -433,55 +433,61 @@ void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, con
}
sql += ")";

int rc = sqlExec( db, sql );

// add geometry column
QString geomType = "";
switch ( layer->wkbType() )
if ( layer->hasGeometryType() )
{
case QGis::WKBPoint:
geomType = "POINT";
break;
case QGis::WKBMultiPoint:
geomType = "MULTIPOINT";
break;
case QGis::WKBLineString:
geomType = "LINESTRING";
break;
case QGis::WKBMultiLineString:
geomType = "MULTILINESTRING";
break;
case QGis::WKBPolygon:
geomType = "POLYGON";
break;
case QGis::WKBMultiPolygon:
geomType = "MULTIPOLYGON";
break;
default:
showWarning( tr( "QGIS wkbType %1 not supported" ).arg( layer->wkbType() ) );
break;
};
QString sqlAddGeom = QString( "SELECT AddGeometryColumn('%1', 'Geometry', %2, '%3', 2)" )
.arg( tableName )
.arg( layer->crs().authid().startsWith( "EPSG:", Qt::CaseInsensitive ) ? layer->crs().authid().mid( 5 ).toLong() : 0 )
.arg( geomType );

// create spatial index
QString sqlCreateIndex = QString( "SELECT CreateSpatialIndex('%1', 'Geometry')" ).arg( tableName );
QString geomType = "";
switch ( layer->wkbType() )
{
case QGis::WKBPoint:
geomType = "POINT";
break;
case QGis::WKBMultiPoint:
geomType = "MULTIPOINT";
break;
case QGis::WKBLineString:
geomType = "LINESTRING";
break;
case QGis::WKBMultiLineString:
geomType = "MULTILINESTRING";
break;
case QGis::WKBPolygon:
geomType = "POLYGON";
break;
case QGis::WKBMultiPolygon:
geomType = "MULTIPOLYGON";
break;
default:
showWarning( tr( "QGIS wkbType %1 not supported" ).arg( layer->wkbType() ) );
break;
};
QString sqlAddGeom = QString( "SELECT AddGeometryColumn('%1', 'Geometry', %2, '%3', 2)" )
.arg( tableName )
.arg( layer->crs().authid().startsWith( "EPSG:", Qt::CaseInsensitive ) ? layer->crs().authid().mid( 5 ).toLong() : 0 )
.arg( geomType );

// create spatial index
QString sqlCreateIndex = QString( "SELECT CreateSpatialIndex('%1', 'Geometry')" ).arg( tableName );

int rc = sqlExec( db, sql );
if ( rc == SQLITE_OK )
{
rc = sqlExec( db, sqlAddGeom );
if ( rc == SQLITE_OK )
{
rc = sqlExec( db, sqlCreateIndex );
rc = sqlExec( db, sqlAddGeom );
if ( rc == SQLITE_OK )
{
rc = sqlExec( db, sqlCreateIndex );
}
}
}

if ( rc == SQLITE_OK )
{
// add new layer
QgsVectorLayer* newLayer = new QgsVectorLayer( QString( "dbname='%1' table='%2'(Geometry) sql=" )
.arg( offlineDbPath ).arg( tableName ), tableName + " (offline)", "spatialite" );
QgsVectorLayer* newLayer = new QgsVectorLayer( QString( "dbname='%1' table='%2'%3 sql=" )
.arg( offlineDbPath )
.arg( tableName ).arg( layer->hasGeometryType() ? "(Geometry)" : "" ),
tableName + " (offline)", "spatialite" );
if ( newLayer->isValid() )
{
// mark as offline layer
Expand Down
42 changes: 38 additions & 4 deletions src/plugins/offline_editing/offline_editing_plugin_gui.cpp
Expand Up @@ -31,6 +31,42 @@
#include <QMessageBox>
#include <QSettings>


QgsSelectLayerTreeModel::QgsSelectLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject* parent )
: QgsLayerTreeModel( rootNode, parent )
{
setFlag( QgsLayerTreeModel::ShowLegend, false );
setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility, true );
}

QgsSelectLayerTreeModel::~QgsSelectLayerTreeModel()
{
}

QVariant QgsSelectLayerTreeModel::data( const QModelIndex& index, int role ) const
{
if ( role == Qt::CheckStateRole )
{
QgsLayerTreeNode* node = index2node( index );
if ( QgsLayerTree::isLayer( node ) )
{
QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node );
return nodeLayer->isVisible();
}
else if ( QgsLayerTree::isGroup( node ) )
{
QgsLayerTreeGroup* nodeGroup = QgsLayerTree::toGroup( node );
return nodeGroup->isVisible();
}
else
{
return QVariant();
}
}
return QgsLayerTreeModel::data( index, role );
}


QgsOfflineEditingPluginGui::QgsOfflineEditingPluginGui( QWidget* parent /*= 0*/, Qt::WindowFlags fl /*= 0*/ )
: QDialog( parent, fl )
{
Expand All @@ -42,9 +78,7 @@ QgsOfflineEditingPluginGui::QgsOfflineEditingPluginGui( QWidget* parent /*= 0*/,
mOfflineDataPathLineEdit->setText( QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ) );

QgsLayerTreeGroup* rootNode = QgsLayerTree::toGroup( QgsProject::instance()->layerTreeRoot()->clone() );
QgsLayerTreeModel* treeModel = new QgsLayerTreeModel( rootNode, this );
treeModel->setFlag( QgsLayerTreeModel::ShowLegend, false );
treeModel->setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility, true );
QgsLayerTreeModel* treeModel = new QgsSelectLayerTreeModel( rootNode, this );
mLayerTree->setModel( treeModel );

connect( mSelectAllButton, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
Expand Down Expand Up @@ -114,7 +148,7 @@ void QgsOfflineEditingPluginGui::on_buttonBox_accepted()
{
if ( nodeLayer->isVisible() )
{
QgsDebugMsg(nodeLayer->layerId());
QgsDebugMsg( nodeLayer->layerId() );
mSelectedLayerIds.append( nodeLayer->layerId() );
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/plugins/offline_editing/offline_editing_plugin_gui.h
Expand Up @@ -20,7 +20,21 @@
#define QGS_OFFLINE_EDITING_PLUGIN_GUI_H

#include <QDialog>
#include <ui_offline_editing_plugin_guibase.h>

#include "ui_offline_editing_plugin_guibase.h"

#include "qgslayertreemodel.h"

class QgsSelectLayerTreeModel : public QgsLayerTreeModel
{
Q_OBJECT
public:
QgsSelectLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent = 0 );
~QgsSelectLayerTreeModel();

QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const OVERRIDE;
// bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) OVERRIDE;
};

class QgsOfflineEditingPluginGui : public QDialog, private Ui::QgsOfflineEditingPluginGuiBase
{
Expand Down

0 comments on commit c3488f4

Please sign in to comment.