Skip to content

Commit b1db8da

Browse files
committedDec 11, 2017
Add non spatial option to create spatialite layer dialog
1 parent 83cdd84 commit b1db8da

File tree

2 files changed

+84
-67
lines changed

2 files changed

+84
-67
lines changed
 

‎src/app/qgsnewspatialitelayerdialog.cpp

Lines changed: 83 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
4646
setupUi( this );
4747
connect( mAddAttributeButton, &QToolButton::clicked, this, &QgsNewSpatialiteLayerDialog::mAddAttributeButton_clicked );
4848
connect( mRemoveAttributeButton, &QToolButton::clicked, this, &QgsNewSpatialiteLayerDialog::mRemoveAttributeButton_clicked );
49+
connect( mGeometryTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewSpatialiteLayerDialog::mGeometryTypeBox_currentIndexChanged );
4950
connect( mTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewSpatialiteLayerDialog::mTypeBox_currentIndexChanged );
5051
connect( pbnFindSRID, &QPushButton::clicked, this, &QgsNewSpatialiteLayerDialog::pbnFindSRID_clicked );
5152
connect( toolButtonNewDatabase, &QToolButton::clicked, this, &QgsNewSpatialiteLayerDialog::toolButtonNewDatabase_clicked );
@@ -55,13 +56,19 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
5556
QgsSettings settings;
5657
restoreGeometry( settings.value( QStringLiteral( "Windows/NewSpatiaLiteLayer/geometry" ) ).toByteArray() );
5758

59+
mGeometryTypeBox->addItem( tr( "Non spatial" ), QStringLiteral( "" ) );
5860
mGeometryTypeBox->addItem( tr( "Point" ), QStringLiteral( "POINT" ) );
5961
mGeometryTypeBox->addItem( tr( "Line" ), QStringLiteral( "LINESTRING" ) );
6062
mGeometryTypeBox->addItem( tr( "Polygon" ), QStringLiteral( "POLYGON" ) );
6163
mGeometryTypeBox->addItem( tr( "MultiPoint" ), QStringLiteral( "MULTIPOINT" ) );
6264
mGeometryTypeBox->addItem( tr( "MultiLine" ), QStringLiteral( "MULTILINESTRING" ) );
6365
mGeometryTypeBox->addItem( tr( "MultiPolygon" ), QStringLiteral( "MULTIPOLYGON" ) );
6466

67+
pbnFindSRID->setEnabled( false );
68+
mGeometryWithZCheckBox->setEnabled( false );
69+
mGeometryWithMCheckBox->setEnabled( false );
70+
leGeometryColumn->setEnabled( false );
71+
6572
mAddAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionNewAttribute.svg" ) ) );
6673
mRemoveAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDeleteAttribute.svg" ) ) );
6774
mTypeBox->addItem( tr( "Text data" ), "text" );
@@ -109,6 +116,14 @@ QgsNewSpatialiteLayerDialog::~QgsNewSpatialiteLayerDialog()
109116
settings.setValue( QStringLiteral( "Windows/NewSpatiaLiteLayer/geometry" ), saveGeometry() );
110117
}
111118

119+
void QgsNewSpatialiteLayerDialog::mGeometryTypeBox_currentIndexChanged( int index )
120+
{
121+
pbnFindSRID->setEnabled( index != 0 );
122+
mGeometryWithZCheckBox->setEnabled( index != 0 );
123+
mGeometryWithMCheckBox->setEnabled( index != 0 );
124+
leGeometryColumn->setEnabled( index != 0 );
125+
}
126+
112127
void QgsNewSpatialiteLayerDialog::mTypeBox_currentIndexChanged( int index )
113128
{
114129
// This isn't used since widths are irrelevant in sqlite3
@@ -377,26 +392,11 @@ bool QgsNewSpatialiteLayerDialog::apply()
377392
delim = QStringLiteral( "," );
378393
++it;
379394
}
380-
381395
// complete the create table statement
382396
sql += ')';
383397

384398
QgsDebugMsg( QString( "Creating table in database %1" ).arg( mDatabaseComboBox->currentText() ) );
385-
386-
QgsDebugMsg( sql ); // OK
387-
388-
QString sqlAddGeom = QStringLiteral( "select AddGeometryColumn(%1,%2,%3,%4,%5)" )
389-
.arg( quotedValue( leLayerName->text() ),
390-
quotedValue( leGeometryColumn->text() ) )
391-
.arg( mCrsId.split( ':' ).value( 1, QStringLiteral( "0" ) ).toInt() )
392-
.arg( quotedValue( selectedType() ) )
393-
.arg( quotedValue( selectedZM() ) );
394-
QgsDebugMsg( sqlAddGeom ); // OK
395-
396-
QString sqlCreateIndex = QStringLiteral( "select CreateSpatialIndex(%1,%2)" )
397-
.arg( quotedValue( leLayerName->text() ),
398-
quotedValue( leGeometryColumn->text() ) );
399-
QgsDebugMsg( sqlCreateIndex ); // OK
399+
QgsDebugMsg( sql );
400400

401401
spatialite_database_unique_ptr database;
402402
int rc = database.open( mDatabaseComboBox->currentText() );
@@ -405,68 +405,84 @@ bool QgsNewSpatialiteLayerDialog::apply()
405405
QMessageBox::warning( this,
406406
tr( "SpatiaLite Database" ),
407407
tr( "Unable to open the database: %1" ).arg( mDatabaseComboBox->currentText() ) );
408+
return false;
408409
}
409-
else
410+
411+
char *errmsg = nullptr;
412+
413+
// create the table
414+
rc = sqlite3_exec( database.get(), sql.toUtf8(), nullptr, nullptr, &errmsg );
415+
if ( rc != SQLITE_OK )
416+
{
417+
QMessageBox::warning( this,
418+
tr( "Error Creating SpatiaLite Table" ),
419+
tr( "Failed to create the SpatiaLite table %1. The database returned:\n%2" ).arg( leLayerName->text(), errmsg ) );
420+
sqlite3_free( errmsg );
421+
return false;
422+
}
423+
424+
// create the geometry column and the spatial index
425+
if ( mGeometryTypeBox->currentIndex() != 0 )
410426
{
411-
char *errmsg = nullptr;
412-
rc = sqlite3_exec( database.get(), sql.toUtf8(), nullptr, nullptr, &errmsg );
427+
QString sqlAddGeom = QStringLiteral( "select AddGeometryColumn(%1,%2,%3,%4,%5)" )
428+
.arg( quotedValue( leLayerName->text() ),
429+
quotedValue( leGeometryColumn->text() ) )
430+
.arg( mCrsId.split( ':' ).value( 1, QStringLiteral( "0" ) ).toInt() )
431+
.arg( quotedValue( selectedType() ) )
432+
.arg( quotedValue( selectedZM() ) );
433+
QgsDebugMsg( sqlAddGeom );
434+
435+
rc = sqlite3_exec( database.get(), sqlAddGeom.toUtf8(), nullptr, nullptr, &errmsg );
413436
if ( rc != SQLITE_OK )
414437
{
415438
QMessageBox::warning( this,
416-
tr( "Error Creating SpatiaLite Table" ),
417-
tr( "Failed to create the SpatiaLite table %1. The database returned:\n%2" ).arg( leLayerName->text(), errmsg ) );
439+
tr( "Error Creating Geometry Column" ),
440+
tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
418441
sqlite3_free( errmsg );
442+
return false;
419443
}
420-
else
444+
445+
QString sqlCreateIndex = QStringLiteral( "select CreateSpatialIndex(%1,%2)" )
446+
.arg( quotedValue( leLayerName->text() ),
447+
quotedValue( leGeometryColumn->text() ) );
448+
QgsDebugMsg( sqlCreateIndex );
449+
450+
rc = sqlite3_exec( database.get(), sqlCreateIndex.toUtf8(), nullptr, nullptr, &errmsg );
451+
if ( rc != SQLITE_OK )
421452
{
422-
// create the geometry column and the spatial index
423-
rc = sqlite3_exec( database.get(), sqlAddGeom.toUtf8(), nullptr, nullptr, &errmsg );
424-
if ( rc != SQLITE_OK )
425-
{
426-
QMessageBox::warning( this,
427-
tr( "Error Creating Geometry Column" ),
428-
tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
429-
sqlite3_free( errmsg );
430-
}
431-
else
432-
{
433-
// create the spatial index
434-
rc = sqlite3_exec( database.get(), sqlCreateIndex.toUtf8(), nullptr, nullptr, &errmsg );
435-
if ( rc != SQLITE_OK )
436-
{
437-
QMessageBox::warning( this,
438-
tr( "Error Creating Spatial Index" ),
439-
tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
440-
sqlite3_free( errmsg );
441-
}
442-
443-
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "dbname='%1' table='%2'(%3) sql=" )
444-
.arg( mDatabaseComboBox->currentText(),
445-
leLayerName->text(),
446-
leGeometryColumn->text() ), leLayerName->text(), QStringLiteral( "spatialite" ) );
447-
if ( layer->isValid() )
448-
{
449-
// Reload connections to refresh browser panel
450-
QgisApp::instance()->reloadConnections();
451-
452-
// register this layer with the central layers registry
453-
QList<QgsMapLayer *> myList;
454-
myList << layer;
455-
//addMapLayers returns a list of all successfully added layers
456-
//so we compare that to our original list.
457-
if ( myList == QgsProject::instance()->addMapLayers( myList ) )
458-
return true;
459-
}
460-
else
461-
{
462-
QgsDebugMsg( leLayerName->text() + " is an invalid layer - not loaded" );
463-
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( leLayerName->text() ) );
464-
delete layer;
465-
}
466-
}
453+
QMessageBox::warning( this,
454+
tr( "Error Creating Spatial Index" ),
455+
tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
456+
sqlite3_free( errmsg );
457+
return false;
467458
}
468459
}
469460

461+
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "dbname='%1' table='%2'%3 sql=" )
462+
.arg( mDatabaseComboBox->currentText(),
463+
leLayerName->text(),
464+
mGeometryTypeBox->currentIndex() != 0 ? QStringLiteral( "(%1)" ).arg( leGeometryColumn->text() ) : QStringLiteral( "" ) ),
465+
leLayerName->text(), QStringLiteral( "spatialite" ) );
466+
if ( layer->isValid() )
467+
{
468+
// Reload connections to refresh browser panel
469+
QgisApp::instance()->reloadConnections();
470+
471+
// register this layer with the central layers registry
472+
QList<QgsMapLayer *> myList;
473+
myList << layer;
474+
//addMapLayers returns a list of all successfully added layers
475+
//so we compare that to our original list.
476+
if ( myList == QgsProject::instance()->addMapLayers( myList ) )
477+
return true;
478+
}
479+
else
480+
{
481+
QgsDebugMsg( leLayerName->text() + " is an invalid layer - not loaded" );
482+
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( leLayerName->text() ) );
483+
delete layer;
484+
}
485+
470486
return false;
471487
}
472488

‎src/app/qgsnewspatialitelayerdialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class APP_EXPORT QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNew
4242
protected slots:
4343
void mAddAttributeButton_clicked();
4444
void mRemoveAttributeButton_clicked();
45+
void mGeometryTypeBox_currentIndexChanged( int index );
4546
void mTypeBox_currentIndexChanged( int index );
4647
void pbnFindSRID_clicked();
4748
void toolButtonNewDatabase_clicked();

0 commit comments

Comments
 (0)
Please sign in to comment.