Skip to content

Commit c3488f4

Browse files
committed
offline editing plugin: allow exporting non geometric layers
1 parent b042d8e commit c3488f4

File tree

3 files changed

+97
-43
lines changed

3 files changed

+97
-43
lines changed

src/core/qgsofflineediting.cpp

+44-38
Original file line numberDiff line numberDiff line change
@@ -433,55 +433,61 @@ void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, con
433433
}
434434
sql += ")";
435435

436+
int rc = sqlExec( db, sql );
437+
436438
// add geometry column
437-
QString geomType = "";
438-
switch ( layer->wkbType() )
439+
if ( layer->hasGeometryType() )
439440
{
440-
case QGis::WKBPoint:
441-
geomType = "POINT";
442-
break;
443-
case QGis::WKBMultiPoint:
444-
geomType = "MULTIPOINT";
445-
break;
446-
case QGis::WKBLineString:
447-
geomType = "LINESTRING";
448-
break;
449-
case QGis::WKBMultiLineString:
450-
geomType = "MULTILINESTRING";
451-
break;
452-
case QGis::WKBPolygon:
453-
geomType = "POLYGON";
454-
break;
455-
case QGis::WKBMultiPolygon:
456-
geomType = "MULTIPOLYGON";
457-
break;
458-
default:
459-
showWarning( tr( "QGIS wkbType %1 not supported" ).arg( layer->wkbType() ) );
460-
break;
461-
};
462-
QString sqlAddGeom = QString( "SELECT AddGeometryColumn('%1', 'Geometry', %2, '%3', 2)" )
463-
.arg( tableName )
464-
.arg( layer->crs().authid().startsWith( "EPSG:", Qt::CaseInsensitive ) ? layer->crs().authid().mid( 5 ).toLong() : 0 )
465-
.arg( geomType );
466-
467-
// create spatial index
468-
QString sqlCreateIndex = QString( "SELECT CreateSpatialIndex('%1', 'Geometry')" ).arg( tableName );
441+
QString geomType = "";
442+
switch ( layer->wkbType() )
443+
{
444+
case QGis::WKBPoint:
445+
geomType = "POINT";
446+
break;
447+
case QGis::WKBMultiPoint:
448+
geomType = "MULTIPOINT";
449+
break;
450+
case QGis::WKBLineString:
451+
geomType = "LINESTRING";
452+
break;
453+
case QGis::WKBMultiLineString:
454+
geomType = "MULTILINESTRING";
455+
break;
456+
case QGis::WKBPolygon:
457+
geomType = "POLYGON";
458+
break;
459+
case QGis::WKBMultiPolygon:
460+
geomType = "MULTIPOLYGON";
461+
break;
462+
default:
463+
showWarning( tr( "QGIS wkbType %1 not supported" ).arg( layer->wkbType() ) );
464+
break;
465+
};
466+
QString sqlAddGeom = QString( "SELECT AddGeometryColumn('%1', 'Geometry', %2, '%3', 2)" )
467+
.arg( tableName )
468+
.arg( layer->crs().authid().startsWith( "EPSG:", Qt::CaseInsensitive ) ? layer->crs().authid().mid( 5 ).toLong() : 0 )
469+
.arg( geomType );
470+
471+
// create spatial index
472+
QString sqlCreateIndex = QString( "SELECT CreateSpatialIndex('%1', 'Geometry')" ).arg( tableName );
469473

470-
int rc = sqlExec( db, sql );
471-
if ( rc == SQLITE_OK )
472-
{
473-
rc = sqlExec( db, sqlAddGeom );
474474
if ( rc == SQLITE_OK )
475475
{
476-
rc = sqlExec( db, sqlCreateIndex );
476+
rc = sqlExec( db, sqlAddGeom );
477+
if ( rc == SQLITE_OK )
478+
{
479+
rc = sqlExec( db, sqlCreateIndex );
480+
}
477481
}
478482
}
479483

480484
if ( rc == SQLITE_OK )
481485
{
482486
// add new layer
483-
QgsVectorLayer* newLayer = new QgsVectorLayer( QString( "dbname='%1' table='%2'(Geometry) sql=" )
484-
.arg( offlineDbPath ).arg( tableName ), tableName + " (offline)", "spatialite" );
487+
QgsVectorLayer* newLayer = new QgsVectorLayer( QString( "dbname='%1' table='%2'%3 sql=" )
488+
.arg( offlineDbPath )
489+
.arg( tableName ).arg( layer->hasGeometryType() ? "(Geometry)" : "" ),
490+
tableName + " (offline)", "spatialite" );
485491
if ( newLayer->isValid() )
486492
{
487493
// mark as offline layer

src/plugins/offline_editing/offline_editing_plugin_gui.cpp

+38-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,42 @@
3131
#include <QMessageBox>
3232
#include <QSettings>
3333

34+
35+
QgsSelectLayerTreeModel::QgsSelectLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject* parent )
36+
: QgsLayerTreeModel( rootNode, parent )
37+
{
38+
setFlag( QgsLayerTreeModel::ShowLegend, false );
39+
setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility, true );
40+
}
41+
42+
QgsSelectLayerTreeModel::~QgsSelectLayerTreeModel()
43+
{
44+
}
45+
46+
QVariant QgsSelectLayerTreeModel::data( const QModelIndex& index, int role ) const
47+
{
48+
if ( role == Qt::CheckStateRole )
49+
{
50+
QgsLayerTreeNode* node = index2node( index );
51+
if ( QgsLayerTree::isLayer( node ) )
52+
{
53+
QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node );
54+
return nodeLayer->isVisible();
55+
}
56+
else if ( QgsLayerTree::isGroup( node ) )
57+
{
58+
QgsLayerTreeGroup* nodeGroup = QgsLayerTree::toGroup( node );
59+
return nodeGroup->isVisible();
60+
}
61+
else
62+
{
63+
return QVariant();
64+
}
65+
}
66+
return QgsLayerTreeModel::data( index, role );
67+
}
68+
69+
3470
QgsOfflineEditingPluginGui::QgsOfflineEditingPluginGui( QWidget* parent /*= 0*/, Qt::WindowFlags fl /*= 0*/ )
3571
: QDialog( parent, fl )
3672
{
@@ -42,9 +78,7 @@ QgsOfflineEditingPluginGui::QgsOfflineEditingPluginGui( QWidget* parent /*= 0*/,
4278
mOfflineDataPathLineEdit->setText( QDir( mOfflineDataPath ).absoluteFilePath( mOfflineDbFile ) );
4379

4480
QgsLayerTreeGroup* rootNode = QgsLayerTree::toGroup( QgsProject::instance()->layerTreeRoot()->clone() );
45-
QgsLayerTreeModel* treeModel = new QgsLayerTreeModel( rootNode, this );
46-
treeModel->setFlag( QgsLayerTreeModel::ShowLegend, false );
47-
treeModel->setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility, true );
81+
QgsLayerTreeModel* treeModel = new QgsSelectLayerTreeModel( rootNode, this );
4882
mLayerTree->setModel( treeModel );
4983

5084
connect( mSelectAllButton, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
@@ -114,7 +148,7 @@ void QgsOfflineEditingPluginGui::on_buttonBox_accepted()
114148
{
115149
if ( nodeLayer->isVisible() )
116150
{
117-
QgsDebugMsg(nodeLayer->layerId());
151+
QgsDebugMsg( nodeLayer->layerId() );
118152
mSelectedLayerIds.append( nodeLayer->layerId() );
119153
}
120154
}

src/plugins/offline_editing/offline_editing_plugin_gui.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@
2020
#define QGS_OFFLINE_EDITING_PLUGIN_GUI_H
2121

2222
#include <QDialog>
23-
#include <ui_offline_editing_plugin_guibase.h>
23+
24+
#include "ui_offline_editing_plugin_guibase.h"
25+
26+
#include "qgslayertreemodel.h"
27+
28+
class QgsSelectLayerTreeModel : public QgsLayerTreeModel
29+
{
30+
Q_OBJECT
31+
public:
32+
QgsSelectLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent = 0 );
33+
~QgsSelectLayerTreeModel();
34+
35+
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const OVERRIDE;
36+
// bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) OVERRIDE;
37+
};
2438

2539
class QgsOfflineEditingPluginGui : public QDialog, private Ui::QgsOfflineEditingPluginGuiBase
2640
{

0 commit comments

Comments
 (0)