Skip to content

Commit 814861c

Browse files
author
jef
committedApr 5, 2010
spatialite improvements:
- support geometry column name - remove unnecessary id field - fix windows build - better identifier quoting and utf-8 support - update python binding - fix some translation strings git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13250 c8812cc2-4d05-0410-92ff-de0c093fc19c

12 files changed

+324
-496
lines changed
 

‎debian/control.sid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Build-Depends: debhelper (>= 7), libgdal1-dev, libpq-dev,
88
sharutils, sip4 (>= 4.5), libqt4-core (>=4.4.0), libqt4-dev (>=4.4.0), libqt4-gui (>=4.4.0),
99
libqt4-sql (>=4.4.0), python-qt4 (>=4.1.0), python-qt4-dev (>=4.1.0),
1010
python-sip4-dev (>= 4.5.0), libfontconfig1-dev, libxi-dev, libxrandr-dev, libxrender-dev, libice-dev,
11-
libsm-dev, pyqt4-dev-tools, libqwt5-qt4-dev
11+
libsm-dev, pyqt4-dev-tools, libqwt5-qt4-dev, libspatialite-dev
1212
Build-Conflicts: libqgis-dev, qgis-dev
1313
Standards-Version: 3.8.4
1414
XS-Python-Version: current

‎python/core/qgsapplication.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
133133
//! Returns the path to the master qgis.db file.
134134
static const QString qgisMasterDbFilePath();
135135

136+
//! Returns the path to the spatialite template db file.
137+
//! @note added in 1.5
138+
static const QString qgisSpatialiteDbTemplatePath();
139+
136140
//! Returns the path to the settings directory in user's home dir
137141
static const QString qgisSettingsDirPath();
138142

‎src/app/qgisapp.cpp

Lines changed: 82 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@
213213
#include "postgres/qgspgsourceselect.h"
214214
#endif
215215
#ifdef HAVE_SPATIALITE
216+
extern "C"
217+
{
218+
#include <sqlite3.h>
219+
#include <spatialite.h>
220+
}
216221
#include "qgsspatialitesourceselect.h"
217222
#include "qgsnewspatialitelayerdialog.h"
218223
#endif
@@ -937,15 +942,15 @@ void QgisApp::createActions()
937942

938943
// Layer Menu Items
939944

940-
mActionNewVectorLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New Vector Layer (shapefile)..." ), this );
941-
shortcuts->registerAction( mActionNewVectorLayer, tr( "Ctrl+Shift+N", "Create a New Vector Layer (shapefile)" ) );
942-
mActionNewVectorLayer->setStatusTip( tr( "Create a New Vector Layer (shapefile)" ) );
945+
mActionNewVectorLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New Shapefile Layer..." ), this );
946+
shortcuts->registerAction( mActionNewVectorLayer, tr( "Ctrl+Shift+N", "Create a New Shapefile layer" ) );
947+
mActionNewVectorLayer->setStatusTip( tr( "Create a New Shapefile layer" ) );
943948
connect( mActionNewVectorLayer, SIGNAL( triggered() ), this, SLOT( newVectorLayer() ) );
944949

945950
#ifdef HAVE_SPATIALITE
946-
mActionNewSpatialiteLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New Spatialite Layer ..." ), this );
947-
shortcuts->registerAction( mActionNewSpatialiteLayer, tr( "Ctrl+Shift+S", "Create a New Spatialite Layer " ) );
948-
mActionNewSpatialiteLayer->setStatusTip( tr( "Create a New Spatialite Layer " ) );
951+
mActionNewSpatialiteLayer = new QAction( getThemeIcon( "mActionNewVectorLayer.png" ), tr( "New SpatiaLite Layer ..." ), this );
952+
shortcuts->registerAction( mActionNewSpatialiteLayer, tr( "Ctrl+Shift+S", "Create a New SpatiaLite Layer " ) );
953+
mActionNewSpatialiteLayer->setStatusTip( tr( "Create a New SpatiaLite Layer " ) );
949954
connect( mActionNewSpatialiteLayer, SIGNAL( triggered() ), this, SLOT( newSpatialiteLayer() ) );
950955
#endif
951956

@@ -1420,13 +1425,13 @@ void QgisApp::createMenus()
14201425
mLayerMenu = menuBar()->addMenu( tr( "&Layer" ) );
14211426

14221427
#ifdef HAVE_SPATIALITE
1423-
QMenu *newLayerMenu = mLayerMenu->addMenu( tr( "New ") );
1428+
QMenu *newLayerMenu = mLayerMenu->addMenu( tr( "New" ) );
14241429
newLayerMenu->addAction( mActionNewVectorLayer );
14251430
newLayerMenu->addAction( mActionNewSpatialiteLayer );
14261431
#else
14271432
mLayerMenu->addAction( mActionNewVectorLayer );
14281433
#endif
1429-
1434+
14301435
mLayerMenu->addAction( mActionAddOgrLayer );
14311436
mLayerMenu->addAction( mActionAddRasterLayer );
14321437
#ifdef HAVE_POSTGRESQL
@@ -3097,6 +3102,18 @@ void QgisApp::newVectorLayer()
30973102
addVectorLayers( fileNames, enc, "file" );
30983103
}
30993104

3105+
static QString quotedIdentifier( QString id )
3106+
{
3107+
id.replace( "\"", "\"\"" );
3108+
return id.prepend( "\"" ).append( "\"" );
3109+
}
3110+
3111+
static QString quotedValue( QString value )
3112+
{
3113+
value.replace( "'", "''" );
3114+
return value.prepend( "'" ).append( "'" );
3115+
}
3116+
31003117
void QgisApp::newSpatialiteLayer()
31013118
{
31023119
if ( mMapCanvas && mMapCanvas->isDrawing() )
@@ -3115,195 +3132,103 @@ void QgisApp::newSpatialiteLayer()
31153132
QString crsId = spatialiteDialog.selectedCrsId();
31163133
QString databaseName = spatialiteDialog.databaseName();
31173134
QString newLayerName = spatialiteDialog.layerName();
3135+
QString newGeometryColumn = spatialiteDialog.geometryColumn();
31183136
//QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );
31193137

31203138
// Get the list containing the name/type pairs for each attribute
31213139
QList<QStringList> * items = spatialiteDialog.attributes( );
31223140

31233141
// Build up the sql statement for creating the table
31243142
//
3125-
QString sql = QString("create table %1 (id integer primary key autoincrement").arg(newLayerName);
3143+
QString sql = QString( "create table %1(" ).arg( quotedIdentifier( newLayerName ) );
31263144
// iterate through the field names and add them to the create statement
31273145
// (use indexed access since this is just as fast as iterators
3128-
for ( int i = 0; i < items->size(); ++i )
3146+
for ( int i = 0; i < items->size(); ++i )
31293147
{
3130-
QStringList field = items->at(i);
3131-
sql += QString(", %1 %2").arg(field.at(0)).arg(field.at(1));
3148+
QStringList field = items->at( i );
3149+
if ( i > 0 )
3150+
sql += ",";
3151+
sql += QString( "%1 %2" ).arg( quotedIdentifier( field.at( 0 ) ) ).arg( field.at( 1 ) );
31323152
}
31333153
// complete the create table statement
31343154
sql += ")";
3135-
std::cout << "Creating table in database " << databaseName.toUtf8().data() << std::endl;
3136-
std::cout << sql.toUtf8().data() << std::endl; // OK
3155+
QgsDebugMsg( QString( "Creating table in database %1" ).arg( databaseName ) );
3156+
QgsDebugMsg( sql ); // OK
3157+
3158+
QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" )
3159+
.arg( quotedValue( newLayerName ) )
3160+
.arg( quotedValue( newGeometryColumn ) )
3161+
.arg( crsId )
3162+
.arg( quotedValue( geometrytype ) );
3163+
QgsDebugMsg( sqlAddGeom ); // OK
31373164

3138-
QString sqlAddGeom = QString("select AddGeometryColumn('%1', 'geom', %2, '%3', 2)").arg(newLayerName).arg(crsId).arg(geometrytype);
3139-
std::cout << sqlAddGeom.toUtf8().data() << std::endl; // OK
3140-
QString sqlCreateIndex = QString("select CreateSpatialIndex('%1', 'geom')").arg(newLayerName);
3141-
std::cout << sqlCreateIndex.toUtf8().data() << std::endl; // OK
3165+
QString sqlCreateIndex = QString( "select CreateSpatialIndex(%1,%2)" ).arg( quotedValue( newLayerName ) ).arg( quotedValue( newGeometryColumn ) );
3166+
QgsDebugMsg( sqlCreateIndex ); // OK
3167+
3168+
spatialite_init( 0 );
31423169

31433170
sqlite3 *db;
31443171
int rc = sqlite3_open( databaseName.toUtf8(), &db );
31453172
if ( rc != SQLITE_OK )
31463173
{
3147-
QMessageBox::warning( this, "Spatialite Database", tr( "Unable to open the database: %1").arg(databaseName ) );
3174+
QMessageBox::warning( this,
3175+
tr( "SpatiaLite Database" ),
3176+
tr( "Unable to open the database: %1" ).arg( databaseName ) );
31483177
}
31493178
else
31503179
{
31513180
char * errmsg;
3152-
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg);
3181+
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg );
31533182
if ( rc != SQLITE_OK )
31543183
{
3155-
QMessageBox::warning( this, tr( "Error deleting bookmark" ),
3156-
tr( "Failed to create the Spatialite table %1. The database returned:\n%2" ).arg( newLayerName ).arg( errmsg ) );
3184+
QMessageBox::warning( this,
3185+
tr( "Error Creating SpatiaLite Table" ),
3186+
tr( "Failed to create the SpatiaLite table %1. The database returned:\n%2" ).arg( newLayerName ).arg( errmsg ) );
31573187
sqlite3_free( errmsg );
31583188
}
31593189
else
31603190
{
31613191
// create the geometry column and the spatial index
3162-
rc = sqlite3_exec( db, sqlAddGeom.toUtf8(), NULL, NULL, &errmsg);
3192+
rc = sqlite3_exec( db, sqlAddGeom.toUtf8(), NULL, NULL, &errmsg );
31633193
if ( rc != SQLITE_OK )
31643194
{
3165-
QMessageBox::warning( this, tr( "Error Creating Geometry Column" ),
3166-
tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
3195+
QMessageBox::warning( this,
3196+
tr( "Error Creating Geometry Column" ),
3197+
tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
31673198
sqlite3_free( errmsg );
31683199
}
31693200
else
31703201
{
3171-
// create the spatial index
3172-
rc = sqlite3_exec( db, sqlCreateIndex.toUtf8(), NULL, NULL, &errmsg);
3173-
if ( rc != SQLITE_OK )
3174-
{
3175-
QMessageBox::warning( this, tr( "Error Creating Spatial Index" ),
3176-
tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
3177-
sqlite3_free( errmsg );
3178-
}
3179-
}
3180-
}
3181-
3182-
3183-
}
3184-
3185-
3186-
/*
3187-
bool haveLastUsedFilter = false; // by default, there is no last
3188-
// used filter
3189-
QString enc;
3190-
QString fileName;
3191-
3192-
QSettings settings; // where we keep last used filter in
3193-
// persistent state
3194-
3195-
haveLastUsedFilter = settings.contains( "/UI/lastVectorFileFilter" );
3196-
QString lastUsedFilter = settings.value( "/UI/lastVectorFileFilter",
3197-
QVariant( QString::null ) ).toString();
3198-
3199-
QString lastUsedDir = settings.value( "/UI/lastVectorFileFilterDir",
3200-
"." ).toString();
3201-
3202-
QgsDebugMsg( "Saving vector file dialog without filters: " );
3203-
3204-
QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog( this,
3205-
tr( "Save As" ), lastUsedDir, "", QString( "" ) );
3206-
3207-
// allow for selection of more than one file
3208-
openFileDialog->setFileMode( QFileDialog::AnyFile );
3209-
openFileDialog->setAcceptMode( QFileDialog::AcceptSave );
3210-
openFileDialog->setConfirmOverwrite( true );
3211-
3212-
if ( haveLastUsedFilter ) // set the filter to the last one used
3213-
{
3214-
openFileDialog->selectFilter( lastUsedFilter );
3215-
}
3216-
3217-
int res;
3218-
while (( res = openFileDialog->exec() ) == QDialog::Accepted )
3219-
{
3220-
fileName = openFileDialog->selectedFiles().first();
3221-
3222-
if ( fileformat == "ESRI Shapefile" )
3223-
{
3224-
if ( !isValidShapeFileName( fileName ) )
3225-
{
3226-
fileName += ".shp";
3227-
}
3228-
3229-
if ( !isValidShapeFileName( fileName ) )
3230-
{
3231-
QMessageBox::information( this,
3232-
tr( "New Shapefile" ),
3233-
tr( "Shapefiles must end on .shp" ) );
3234-
continue;
3235-
}
3236-
}
3237-
3238-
break;
3239-
}
3240-
3241-
if ( res == QDialog::Rejected )
3242-
{
3243-
delete openFileDialog;
3244-
return;
3245-
}
3246-
3247-
enc = openFileDialog->encoding();
3248-
3249-
// If the file exists, delete it otherwise we'll end up loading that
3250-
// file, which can cause problems (e.g., if the file contains
3251-
// linestrings, but we're wanting to create points, we'll end up
3252-
// with a linestring file).
3253-
if ( fileformat == "ESRI Shapefile" )
3254-
{
3255-
QgsVectorFileWriter::deleteShapeFile( fileName );
3256-
}
3257-
else
3258-
{
3259-
QFile::remove( fileName );
3260-
}
3261-
3262-
settings.setValue( "/UI/lastVectorFileFilter", openFileDialog->selectedFilter() );
3263-
3264-
settings.setValue( "/UI/lastVectorFileFilterDir", openFileDialog->directory().absolutePath() );
3265-
3266-
delete openFileDialog;
3267-
3268-
//try to create the new layer with OGRProvider instead of QgsVectorFileWriter
3269-
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
3270-
QString ogrlib = pReg->library( "ogr" );
3271-
// load the data provider
3272-
QLibrary* myLib = new QLibrary( ogrlib );
3273-
bool loaded = myLib->load();
3274-
if ( loaded )
3275-
{
3276-
QgsDebugMsg( "ogr provider loaded" );
3202+
// create the spatial index
3203+
rc = sqlite3_exec( db, sqlCreateIndex.toUtf8(), NULL, NULL, &errmsg );
3204+
if ( rc != SQLITE_OK )
3205+
{
3206+
QMessageBox::warning( this,
3207+
tr( "Error Creating Spatial Index" ),
3208+
tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
3209+
sqlite3_free( errmsg );
3210+
}
32773211

3278-
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType,
3279-
const std::list<std::pair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
3280-
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( myLib->resolve( "createEmptyDataSource" ) );
3281-
if ( createEmptyDataSource )
3282-
{
3283-
if ( geometrytype != QGis::WKBUnknown )
3284-
{
3285-
QgsCoordinateReferenceSystem srs( crsId, QgsCoordinateReferenceSystem::InternalCrsId );
3286-
createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, &srs );
3287-
}
3288-
else
3289-
{
3290-
QgsDebugMsg( "geometry type not recognised" );
3291-
return;
3212+
QgsVectorLayer *layer = new QgsVectorLayer( QString( "dbname=%1 table=%2(%3) sql=" )
3213+
.arg( databaseName )
3214+
.arg( newLayerName )
3215+
.arg( newGeometryColumn ), newLayerName, "spatialite" );
3216+
if ( layer->isValid() )
3217+
{
3218+
// register this layer with the central layers registry
3219+
QgsMapLayerRegistry::instance()->addMapLayer( layer );
3220+
}
3221+
else
3222+
{
3223+
QgsDebugMsg( newLayerName + " is an invalid layer - not loaded" );
3224+
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( newLayerName ) );
3225+
delete layer;
3226+
}
32923227
}
32933228
}
3294-
else
3295-
{
3296-
QgsDebugMsg( "Resolving newEmptyDataSource(...) failed" );
3297-
}
32983229
}
3299-
3300-
//then add the layer to the view
3301-
QStringList fileNames;
3302-
fileNames.append( fileName );
3303-
//todo: the last parameter will change accordingly to layer type
3304-
addVectorLayers( fileNames, enc, "file" );
3305-
*/
33063230
}
3231+
33073232
void QgisApp::fileOpen()
33083233
{
33093234
if ( mMapCanvas && mMapCanvas->isDrawing() )
@@ -5026,6 +4951,7 @@ void QgisApp::loadPythonSupport()
50264951
mActionPluginSeparator2 = mPluginMenu->addSeparator();
50274952
mPluginMenu->addAction( mActionShowPythonDialog );
50284953
std::cout << "Python support ENABLED :-) " << std::endl; // OK
4954+
50294955
}
50304956
}
50314957

‎src/app/qgsnewspatialitelayerdialog.cpp

Lines changed: 37 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,14 @@
1818
***************************************************************************/
1919
/* $Id$ */
2020

21-
/*
22-
extern "C"
23-
{
24-
#include <spatialite/sqlite3.h>
25-
#include <spatialite/gaiageo.h>
26-
#include <spatialite.h>
27-
}
28-
*/
21+
2922
#include "qgsnewspatialitelayerdialog.h"
23+
3024
#include "qgsspatialitesridsdialog.h"
3125
#include "qgsapplication.h"
3226
#include "qgisapp.h" // <- for theme icons
3327
#include "qgslogger.h"
34-
//#include <sqlite3.h>
28+
3529
#include <QPushButton>
3630
#include <QSettings>
3731
#include <QLineEdit>
@@ -66,10 +60,9 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
6660
++it;
6761
}
6862
settings.endGroup();
69-
mApplyButton = buttonBox->button( QDialogButtonBox::Apply );
70-
mApplyButton->setEnabled( false );
71-
mApplyButton->setDefault( true );
72-
connect( buttonBox->button( QDialogButtonBox::Apply ), SIGNAL( clicked() ), this, SLOT( apply() ) );
63+
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
64+
mOkButton->setEnabled( false );
65+
mOkButton->setDefault( true );
7366

7467
// Set the SRID box to a default of WGS84
7568
leSRID->setText( "4326" );
@@ -85,17 +78,17 @@ QgsNewSpatialiteLayerDialog::~QgsNewSpatialiteLayerDialog()
8578
void QgsNewSpatialiteLayerDialog::createNewDb()
8679
{
8780
// QMessageBox::information( 0, tr( "Spatialite Layer" ), tr( "Create new db clicked" ) );
88-
QString fileName = QFileDialog::getSaveFileName(this, tr("New Spatialite Database File"),
89-
".",
90-
tr("Spatialite (*.sqlite *.db )"));
81+
QString fileName = QFileDialog::getSaveFileName( this, tr( "New SpatiaLite Database File" ),
82+
".",
83+
tr( "SpatiaLite (*.sqlite *.db )" ) );
9184

9285
if ( !fileName.isEmpty() )
9386
{
9487
mDatabaseComboBox->insertItem( 0, fileName );
95-
mDatabaseComboBox->setCurrentIndex(0);
88+
mDatabaseComboBox->setCurrentIndex( 0 );
9689
createDb();
9790
needNewDb = true;
98-
}
91+
}
9992
}
10093
void QgsNewSpatialiteLayerDialog::on_mTypeBox_currentIndexChanged( int index )
10194
{
@@ -136,74 +129,21 @@ QString QgsNewSpatialiteLayerDialog::selectedType() const
136129
{
137130
return "MULTIPOLYGON";
138131
}
132+
133+
Q_ASSERT( "no type selected" == 0 );
134+
return "";
139135
}
140136

141137
QString QgsNewSpatialiteLayerDialog::selectedCrsId() const
142138
{
143139
return leSRID->text();
144140
}
145141

146-
void QgsNewSpatialiteLayerDialog::apply()
147-
{
148-
// Check to see if the db exists and if not create it
149-
createDb();
150-
151-
// Init spatialite
152-
spatialite_init( 0 );
153-
QString newLayerName = leLayerName->text();
154-
int rc = sqlite3_open_v2( mDatabaseComboBox->currentText().toUtf8(), &db, SQLITE_OPEN_READWRITE, NULL );
155-
if ( rc != SQLITE_OK )
156-
{
157-
QMessageBox::warning( this, "Spatialite Database", tr( "Unable to open the database"));
158-
}
159-
else
160-
{
161-
char * errmsg;
162-
QString sql = QString("CREATE TABLE %1 (id integer primary key autoincrement, name text)").arg( newLayerName );
163-
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg);
164-
if ( rc != SQLITE_OK )
165-
{
166-
QMessageBox::warning( this, tr( "Error Creating Table" ),
167-
tr( "Failed to create the Spatialite table. The database returned:\n%1" ).arg( errmsg ) );
168-
sqlite3_free( errmsg );
169-
}
170-
else
171-
{
172-
// get the geometry type
173-
QString geomType = selectedType();
174-
// create the geometry column and the spatial index
175-
sql = QString("select AddGeometryColumn('%1', 'geom', %2, '%3', 2)").arg( newLayerName ).arg( leSRID->text() ).arg( geomType );
176-
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg);
177-
if ( rc != SQLITE_OK )
178-
{
179-
QMessageBox::warning( this, tr( "Error Creating Geometry Column" ),
180-
tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
181-
sqlite3_free( errmsg );
182-
}
183-
else
184-
{
185-
// create the spatial index
186-
sql = QString("select CreateSpatialIndex('%1', 'geom')").arg( newLayerName );
187-
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg);
188-
if ( rc != SQLITE_OK )
189-
{
190-
QMessageBox::warning( this, tr( "Error Creating Spatial Index" ),
191-
tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
192-
sqlite3_free( errmsg );
193-
}
194-
else
195-
{
196-
QMessageBox::information( 0, tr( "Spatialite Layer" ), tr( "Layer created" ) );
197-
}
198-
}
199-
}
200-
}
201-
}
202142
void QgsNewSpatialiteLayerDialog::on_leLayerName_textChanged( QString text )
203143
{
204-
if ( leLayerName->text().length() > 0 && mAttributeView->topLevelItemCount() > 0)
144+
if ( leLayerName->text().length() > 0 && mAttributeView->topLevelItemCount() > 0 )
205145
{
206-
mApplyButton->setEnabled( true );
146+
mOkButton->setEnabled( true );
207147
}
208148
}
209149

@@ -214,10 +154,10 @@ void QgsNewSpatialiteLayerDialog::on_mAddAttributeButton_clicked()
214154
QString myName = mNameEdit->text();
215155
//use userrole to avoid translated type string
216156
QString myType = mTypeBox->itemData( mTypeBox->currentIndex(), Qt::UserRole ).toString();
217-
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << myName << myType ) );
157+
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << myName << myType ) );
218158
if ( mAttributeView->topLevelItemCount() > 0 && leLayerName->text().length() > 0 )
219159
{
220-
mApplyButton->setEnabled( true );
160+
mOkButton->setEnabled( true );
221161
}
222162
mNameEdit->clear();
223163
}
@@ -228,7 +168,7 @@ void QgsNewSpatialiteLayerDialog::on_mRemoveAttributeButton_clicked()
228168
delete mAttributeView->currentItem();
229169
if ( mAttributeView->topLevelItemCount() == 0 )
230170
{
231-
mApplyButton->setEnabled( false );
171+
mOkButton->setEnabled( false );
232172
}
233173
}
234174

@@ -238,11 +178,11 @@ void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()
238178
int rc = sqlite3_open( mDatabaseComboBox->currentText().toUtf8().data(), &db );
239179
if ( rc != SQLITE_OK )
240180
{
241-
QMessageBox::warning( this, "Spatialite Database", tr( "Unable to open the database: %1").arg(mDatabaseComboBox->currentText() ) );
181+
QMessageBox::warning( this, tr( "Spatialite Database" ), tr( "Unable to open the database: %1").arg(mDatabaseComboBox->currentText() ) );
242182
}
243183
*/
244184
// set the SRID from a list returned from the selected Spatialite database
245-
QgsSpatialiteSridsDialog *sridDlg = new QgsSpatialiteSridsDialog(this);
185+
QgsSpatialiteSridsDialog *sridDlg = new QgsSpatialiteSridsDialog( this );
246186
if ( sridDlg->load( mDatabaseComboBox->currentText() ) )
247187
{
248188
if ( sridDlg->exec() )
@@ -255,7 +195,7 @@ void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()
255195
}
256196
}
257197

258-
// Create a QList of QStringList objects that define the layer attributes.
198+
// Create a QList of QStringList objects that define the layer attributes.
259199
// Each QStringList contains the field name and its type.
260200
QList<QStringList> * QgsNewSpatialiteLayerDialog::attributes() const
261201
{
@@ -267,8 +207,8 @@ QList<QStringList> * QgsNewSpatialiteLayerDialog::attributes() const
267207
QStringList items;
268208
items << item->text( 0 );
269209
items << item->text( 1 );
270-
list->append(items);
271-
210+
list->append( items );
211+
272212
//QString type = QString( "%1;%2;%3" ).arg( item->text( 1 ) ).arg( item->text( 2 ) ).arg( item->text( 3 ) );
273213
//at.push_back( std::make_pair( item->text( 0 ), type ) );
274214
//QgsDebugMsg( QString( "appending %1//%2" ).arg( item->text( 0 ) ).arg( type ) );
@@ -286,6 +226,12 @@ QString QgsNewSpatialiteLayerDialog::layerName() const
286226
{
287227
return leLayerName->text();
288228
}
229+
230+
QString QgsNewSpatialiteLayerDialog::geometryColumn() const
231+
{
232+
return leGeometryColumn->text();
233+
}
234+
289235
bool QgsNewSpatialiteLayerDialog::createDb()
290236
{
291237
QFile newDb( mDatabaseComboBox->currentText() );
@@ -298,28 +244,24 @@ bool QgsNewSpatialiteLayerDialog::createDb()
298244

299245
QFileInfo fullPath = QFileInfo( mDatabaseComboBox->currentText() );
300246
QDir path = fullPath.dir();
301-
qWarning("making this dir: %s", path.absolutePath().toUtf8().data());
302-
247+
qWarning( "making this dir: %s", path.absolutePath().toUtf8().data() );
303248

304249
// Must be sure there is destination directory ~/.qgis
305250
QDir().mkpath( path.absolutePath( ) );
306251

307-
qWarning("Copying %s ", spatialiteTemplate.toUtf8().data());
308-
qWarning("to %s", newDb.fileName().toUtf8().data());
252+
qWarning( "Copying %s ", spatialiteTemplate.toUtf8().data() );
253+
qWarning( "to %s", newDb.fileName().toUtf8().data() );
309254

310255
//now copy the template db file into the chosen location
311-
bool isDbFileCopied = spatialiteTemplateDb.copy( newDb.fileName() );
312-
313-
if ( !isDbFileCopied )
256+
if ( !spatialiteTemplateDb.copy( newDb.fileName() ) )
314257
{
315-
// QMessageBox::warning( this, "Spatialite Database", tr( "Unable to copy the template database to your new location" ));
258+
QMessageBox::warning( 0, tr( "SpatiaLite Database" ), tr( "Could not copy the template database to new location" ) );
316259
return false;
317260
}
318261
else
319262
{
320-
QMessageBox::information( 0, tr( "Spatialite Database" ), tr( "Created new database!" ) );
263+
QMessageBox::information( 0, tr( "SpatiaLite Database" ), tr( "Created new database!" ) );
321264
}
322265
}
323-
return true;
266+
return true;
324267
}
325-

‎src/app/qgsnewspatialitelayerdialog.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/***************************************************************************
22
QgsNewSpatialiteLayerDialog.h - description
33
-------------------
4-
begin : 2010-03-19
4+
begin : 2010-03-19
55
copyright : (C) 2010 by Gary Sherman
66
email : gsherman@mrcc.com
77
***************************************************************************/
@@ -17,18 +17,18 @@
1717
/* $Id$ */
1818
#ifndef qgsnewspatialitelayerdialog_H
1919
#define qgsnewspatialitelayerdialog_H
20-
#include <sqlite3.h>
21-
extern "C"
22-
{
23-
#include <spatialite/headers/spatialite.h>
24-
}
25-
//#include <sqlite3.h>
20+
2621
#include "ui_qgsnewspatialitelayerdialogbase.h"
2722
#include "qgisgui.h"
2823
#include "qgscontexthelp.h"
2924

3025
#include "qgis.h"
3126

27+
extern "C"
28+
{
29+
#include <sqlite3.h>
30+
}
31+
3232
class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteLayerDialogBase
3333
{
3434
Q_OBJECT
@@ -45,6 +45,8 @@ class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteL
4545
QString databaseName() const;
4646
/**Returns the layer name to be created */
4747
QString layerName() const;
48+
/**Returns the geometry column name */
49+
QString geometryColumn() const;
4850
/**Returns the selected crs id*/
4951
QString selectedCrsId() const;
5052
/** Create a new database */
@@ -57,11 +59,11 @@ class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteL
5759
void on_pbnFindSRID_clicked();
5860
void on_leLayerName_textChanged( QString text );
5961
void createNewDb();
60-
void apply();
62+
6163
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
6264

6365
private:
64-
QPushButton *mApplyButton;
66+
QPushButton *mOkButton;
6567
int mCrsId;
6668
sqlite3 *db;
6769
bool needNewDb;

‎src/app/qgsspatialitesourceselect.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ void QgsSpatiaLiteSourceSelect::on_btnNew_clicked()
300300

301301
QString myFile = QFileDialog::getOpenFileName( this,
302302
tr( "Choose a SpatiaLite/SQLite DB to open" ),
303-
lastUsedDir, QObject::tr( "SQLite DB (*.sqlite);;All files (*.*)" ) );
303+
lastUsedDir, QObject::tr( "SQLite DB (*.sqlite *.db);;All files (*.*)" ) );
304304

305305
if ( myFile.isEmpty() )
306306
return;
@@ -507,8 +507,8 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
507507
{
508508
for ( i = 1; i <= rows; i++ )
509509
{
510-
QString tableName = results[( i * columns ) + 0];
511-
QString column = results[( i * columns ) + 1];
510+
QString tableName = QString::fromUtf8( results[( i * columns ) + 0] );
511+
QString column = QString::fromUtf8( results[( i * columns ) + 1] );
512512
QString type = results[( i * columns ) + 2];
513513
if ( isDeclaredHidden( handle, tableName, column ) )
514514
continue;
@@ -534,8 +534,8 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
534534
{
535535
for ( i = 1; i <= rows; i++ )
536536
{
537-
QString tableName = results[( i * columns ) + 0];
538-
QString column = results[( i * columns ) + 1];
537+
QString tableName = QString::fromUtf8( results[( i * columns ) + 0] );
538+
QString column = QString::fromUtf8( results[( i * columns ) + 1] );
539539
QString type = results[( i * columns ) + 2];
540540
if ( isDeclaredHidden( handle, tableName, column ) )
541541
continue;
@@ -561,8 +561,8 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
561561
{
562562
for ( i = 1; i <= rows; i++ )
563563
{
564-
QString tableName = results[( i * columns ) + 0];
565-
QString column = results[( i * columns ) + 1];
564+
QString tableName = QString::fromUtf8( results[( i * columns ) + 0] );
565+
QString column = QString::fromUtf8( results[( i * columns ) + 1] );
566566
QString type = results[( i * columns ) + 2];
567567
if ( isDeclaredHidden( handle, tableName, column ) )
568568
continue;

‎src/app/qgsspatialitesridsdialog.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <QMessageBox>
2020
#include "qgsspatialitesridsdialog.h"
2121
QgsSpatialiteSridsDialog::QgsSpatialiteSridsDialog( QWidget *parent, Qt::WFlags fl )
22-
: QDialog( parent, fl )
22+
: QDialog( parent, fl )
2323
{
2424
setupUi( this );
2525
db = 0;
@@ -29,13 +29,13 @@ QgsSpatialiteSridsDialog::QgsSpatialiteSridsDialog( QWidget *parent, Qt::WFlags
2929

3030
QgsSpatialiteSridsDialog::~QgsSpatialiteSridsDialog()
3131
{
32-
sqlite3_close( db );
32+
sqlite3_close( db );
3333
}
3434
QString QgsSpatialiteSridsDialog::selectedSrid()
3535
{
36-
return twSrids->currentItem()->text(0);
36+
return twSrids->currentItem()->text( 0 );
3737
}
38-
bool QgsSpatialiteSridsDialog::load(QString dbName)
38+
bool QgsSpatialiteSridsDialog::load( QString dbName )
3939
{
4040
mDbName = dbName;
4141
bool status = true;
@@ -44,43 +44,43 @@ bool QgsSpatialiteSridsDialog::load(QString dbName)
4444
int rc = sqlite3_open_v2( dbName.toUtf8(), &db, SQLITE_OPEN_READONLY, NULL );
4545
if ( rc != SQLITE_OK )
4646
{
47-
QMessageBox::warning( this, "Spatialite Database", tr( "Unable to open the database"));
47+
QMessageBox::warning( this, tr( "SpatiaLite Database" ), tr( "Unable to open the database" ) );
4848
return false;
4949
}
5050
}
51-
// load up the srid table
52-
// prepare the sql statement
53-
const char *pzTail;
54-
sqlite3_stmt *ppStmt;
55-
QString sql = "select auth_srid, auth_name, ref_sys_name from spatial_ref_sys order by srid asc";
51+
// load up the srid table
52+
// prepare the sql statement
53+
const char *pzTail;
54+
sqlite3_stmt *ppStmt;
55+
QString sql = "select auth_srid, auth_name, ref_sys_name from spatial_ref_sys order by srid asc";
5656

57-
int rc = sqlite3_prepare( db, sql.toUtf8(), sql.toUtf8().length(), &ppStmt, &pzTail );
58-
// XXX Need to free memory from the error msg if one is set
59-
if ( rc == SQLITE_OK )
57+
int rc = sqlite3_prepare( db, sql.toUtf8(), sql.toUtf8().length(), &ppStmt, &pzTail );
58+
// XXX Need to free memory from the error msg if one is set
59+
if ( rc == SQLITE_OK )
60+
{
61+
// get the first row of the result set
62+
while ( sqlite3_step( ppStmt ) == SQLITE_ROW )
6063
{
61-
// get the first row of the result set
62-
while ( sqlite3_step( ppStmt ) == SQLITE_ROW )
63-
{
64-
QTreeWidgetItem *item = new QTreeWidgetItem( twSrids );
65-
// srid
66-
item->setText (0, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 0 ) ) );
67-
item->setText (1, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 1 ) ) );
68-
// name
69-
item->setText( 2, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 2 ) ) );
70-
// proj4 text
71-
item->setText( 3, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 3 ) ) );
64+
QTreeWidgetItem *item = new QTreeWidgetItem( twSrids );
65+
// srid
66+
item->setText( 0, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 0 ) ) );
67+
item->setText( 1, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 1 ) ) );
68+
// name
69+
item->setText( 2, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 2 ) ) );
70+
// proj4 text
71+
item->setText( 3, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 3 ) ) );
7272

73-
}
74-
twSrids->sortByColumn( 0, Qt::AscendingOrder );
75-
}
76-
else
77-
{
78-
// XXX query failed -- warn the user some how
79-
QMessageBox::warning(0, "Error", QString( "Failed to load SRIDS: %1" ).arg( sqlite3_errmsg( db ) ) );
80-
status = false;
8173
}
82-
// close the statement
83-
sqlite3_finalize( ppStmt );
74+
twSrids->sortByColumn( 0, Qt::AscendingOrder );
75+
}
76+
else
77+
{
78+
// XXX query failed -- warn the user some how
79+
QMessageBox::warning( 0, tr( "Error" ), tr( "Failed to load SRIDS: %1" ).arg( sqlite3_errmsg( db ) ) );
80+
status = false;
81+
}
82+
// close the statement
83+
sqlite3_finalize( ppStmt );
8484
return status;
8585
}
8686
void QgsSpatialiteSridsDialog::on_pbnFilter_clicked()
@@ -102,7 +102,7 @@ void QgsSpatialiteSridsDialog::on_pbnFilter_clicked()
102102
{
103103
search = "ref_sys_name";
104104
}
105-
QString sql = QString("select auth_srid, auth_name, ref_sys_name from spatial_ref_sys where %1 like '%").arg(search) + leSearch->text() + QString("%' order by srid asc");
105+
QString sql = QString( "select auth_srid, auth_name, ref_sys_name from spatial_ref_sys where %1 like '%" ).arg( search ) + leSearch->text() + QString( "%' order by srid asc" );
106106
//QMessageBox::information(0, "Sql", sql);
107107

108108
int rc = sqlite3_prepare( db, sql.toUtf8(), sql.toUtf8().length(), &ppStmt, &pzTail );
@@ -116,8 +116,8 @@ void QgsSpatialiteSridsDialog::on_pbnFilter_clicked()
116116
{
117117
QTreeWidgetItem *item = new QTreeWidgetItem( twSrids );
118118
// srid
119-
item->setText (0, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 0 ) ) );
120-
item->setText (1, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 1 ) ) );
119+
item->setText( 0, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 0 ) ) );
120+
item->setText( 1, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 1 ) ) );
121121
// name
122122
item->setText( 2, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 2 ) ) );
123123
// proj4 text
@@ -129,7 +129,7 @@ void QgsSpatialiteSridsDialog::on_pbnFilter_clicked()
129129
else
130130
{
131131
// XXX query failed -- warn the user some how
132-
QMessageBox::warning(0, "Error", QString( "Failed to load SRIDS: %1" ).arg( sqlite3_errmsg( db ) ) );
132+
QMessageBox::warning( 0, tr( "Error" ), tr( "Failed to load SRIDS: %1" ).arg( sqlite3_errmsg( db ) ) );
133133
}
134134
// close the statement
135135
sqlite3_finalize( ppStmt );

‎src/app/qgsspatialitesridsdialog.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@
2323
#include "qgscontexthelp.h"
2424

2525
#include "qgis.h"
26+
27+
extern "C"
28+
{
2629
#include <sqlite3.h>
30+
}
2731

28-
class QgsSpatialiteSridsDialog: public QDialog, private Ui::QgsSpatialiteSridsDialogBase
32+
class QgsSpatialiteSridsDialog: public QDialog, private Ui::QgsSpatialiteSridsDialogBase
2933
{
30-
Q_OBJECT
31-
public:
32-
QgsSpatialiteSridsDialog( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags);
33-
~QgsSpatialiteSridsDialog();
34-
bool load(QString dbName);
35-
QString selectedSrid();
36-
public slots:
37-
void on_pbnFilter_clicked();
38-
private:
39-
sqlite3 *db;
40-
QString mDbName;
34+
Q_OBJECT
35+
public:
36+
QgsSpatialiteSridsDialog( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
37+
~QgsSpatialiteSridsDialog();
38+
bool load( QString dbName );
39+
QString selectedSrid();
40+
public slots:
41+
void on_pbnFilter_clicked();
42+
private:
43+
sqlite3 *db;
44+
QString mDbName;
4145
};
4246
#endif //QgsSpatialiteSridsDialog_H

‎src/core/qgsapplication.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class CORE_EXPORT QgsApplication: public QApplication
9090
static const QString qgisMasterDbFilePath();
9191

9292
//! Returns the path to the spatialite template db file.
93+
//! @note added in 1.5
9394
static const QString qgisSpatialiteDbTemplatePath();
9495

9596
//! Returns the path to the settings directory in user's home dir

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 125 additions & 177 deletions
Large diffs are not rendered by default.

‎src/providers/spatialite/qgsspatialiteprovider.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
356356
*/
357357
//void sqliteOpen();
358358
void closeDb();
359+
QString quotedIdentifier( QString id ) const;
359360
QString quotedValue( QString value ) const;
360361
bool checkLayerType();
361362
bool getGeometryDetails();

‎src/ui/qgsnewspatialitelayerdialogbase.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@
403403
<enum>Qt::Horizontal</enum>
404404
</property>
405405
<property name="standardButtons">
406-
<set>QDialogButtonBox::Apply|QDialogButtonBox::Close|QDialogButtonBox::Help</set>
406+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set>
407407
</property>
408408
</widget>
409409
</item>

0 commit comments

Comments
 (0)
Please sign in to comment.