mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
allow creation of multiple layers in spatialite creation dialog
git-svn-id: http://svn.osgeo.org/qgis/trunk@13337 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
parent
9e93c6e9ae
commit
e24ccb885a
29
resources/context_help/QgsNewSpatialiteLayerDialog-de_DE
Normal file
29
resources/context_help/QgsNewSpatialiteLayerDialog-de_DE
Normal file
@ -0,0 +1,29 @@
|
||||
<h3>Einen neuen SpatiaLite-Layer erzeugen</h3>
|
||||
In diesem Dialog können Sie eine neue SpatiaLite-Datenbank und/oder einen leeren SpatiaLite Layer zur Bearbeitung erzeugen. Unten werden die Eingabemöglichkeiten des Dialogs beschrieben.
|
||||
<h4>Datenbank</h4>
|
||||
Wählen Sie die Datenbank aus der Liste. Diese Liste wird aus den gespeicherten
|
||||
SpatiaLite-Verbindungen erzeugt. Wenn sie keine gespeicherten Verbindungen
|
||||
haben oder oder eine neue Datenbank erzeugen wollen Klicken Sie auf
|
||||
<label>...</label> rechts der Liste.
|
||||
<h4>Layername</h4>
|
||||
Geben Sie dem Layer, den Sie erzeugen wollen, einen Namen. Der Name sollte nur ein Wort sein. Wenn Sie wollen, können Sie auch Unterstriche verwenden
|
||||
<h4>Geometriespalte</h4>
|
||||
Geben Sie der Geometriespalte einen Namen oder übernehmen Sie die Voreinstellung.
|
||||
<h4>Typ</h4>
|
||||
Legen Sie den Layertyp fest, den Sie erzeugen wollen.
|
||||
<h4>EPSG-SRID</h4>
|
||||
Geben Sie einen EPSG-Code für die SRID (räumliche Bezugskennung) an. Für die SRID wird Ihnen WGS 84 vorgeschlagen.
|
||||
Klicken Sie auf den Knopf <label>SRID finden</label> um das Koordinatenbezugssystem ggf. zu ändern.
|
||||
Die SRID muss in der Tabelle spatial_ref_sys in Ihre Datenbank existieren. Sie können SRIDs suchen indem Sie Suchbegriffe verwenden, die
|
||||
ganz oder teilweise zu Namen oder SRIDs passen.
|
||||
<h4>Einen automatisch inkrementierenden Primärschlüssel erzeugen</h4>
|
||||
Das Anwählen dieses Kontrolkästchen fügt dem Layer einen Primärschlüssel hinzu. Diese Feld wird automatisch inkrementiert, sodaß Sie keinen
|
||||
keinen Wert dafür beim Hinzufügen von Objekten zum Layer eingeben müssen.
|
||||
<h4>Neues Attribute</h4>
|
||||
Fügen Sie die gewünschten Attribute durch Klicken auf den Knopf <label>Attribute zur Liste hinzufügen</label> nachdem Sie Namen und Type des Attributes festgelegt haben. Nur Fließkomma-, ganzzahlige und Zeichenkettenattribute werden unterstützt.<br/>
|
||||
Breite und Genauigkeit sind irrelevant in einer SpatiaLite-Datenbank, daher brauchen Sie sie nicht anzugeben.
|
||||
<h4>Attributliste</h4>
|
||||
In diesem Abschnitt sehen Sie die Attributliste. Um eins davon zu löschen, klicken Sie auf den Knopf <label>Gewähltes Attribut löschen</label>.
|
||||
|
||||
<p>
|
||||
Klicken Sie auf <label>Ok</label> um den Layer anzulegen und den Dialog zu schließen. Ein Klick auf <label>Anwenden</label> erzeugt den Layer auch, aber hält den Dialog auf und erlaubt so mehr ähnliche Layer zu erzeugen. <label>Abbruch</label> schließt den Dialog ohne weitere Änderung.
|
@ -1,7 +1,7 @@
|
||||
<h3>Create a New Spatialite Layer</h3>
|
||||
You can use this dialog to create a new Spatialite database and/or an empty Spatialite layer for editing. See below for an explanation of the dialog inputs.
|
||||
<h4>Database</h4>
|
||||
Choose the database from the drop-down list. This list is created from your saved Spatialite connections. If you don't have a saved connection or want to create a new database, click on the button (<label>...</label>) to the right of the drop-down. If you create a new database you will have to add it to your Spatialite connections in the <i>Add Spatialite Table(s)</i> dialog.
|
||||
Choose the database from the drop-down list. This list is created from your saved Spatialite connections. If you don't have a saved connection or want to create a new database, click on the button (<label>...</label>) to the right of the drop-down.
|
||||
<h4>Layer name</h4>
|
||||
Enter a name for the layer you want to create. The name should be one word. You can use underscores in the name if you like.
|
||||
<h4>Geometry column</h4>
|
||||
@ -19,4 +19,4 @@ Width and precision are irrelevant in a Spatialite database so you do not have t
|
||||
In this section you can see the list of attributes. To delete one of them, click on it and choose <label>Remove selected attribute</label> button.
|
||||
|
||||
<p>
|
||||
To create the layer, click on <label>Apply</label>. The layer will be created and you will be returned to the dialog. From here you can click <label>Close</label> or change the parameters to create additional layers.
|
||||
Click on <label>Ok</label> to create the layer and close the dialog. Clicking <label>Apply</label> also creates the layer, but keeps the dialog open, thereby allowing you to create more similar layers. <label>Cancel</label> will close the layer without further changes.
|
||||
|
@ -3098,18 +3098,6 @@ void QgisApp::newVectorLayer()
|
||||
addVectorLayers( fileNames, enc, "file" );
|
||||
}
|
||||
|
||||
static QString quotedIdentifier( QString id )
|
||||
{
|
||||
id.replace( "\"", "\"\"" );
|
||||
return id.prepend( "\"" ).append( "\"" );
|
||||
}
|
||||
|
||||
static QString quotedValue( QString value )
|
||||
{
|
||||
value.replace( "'", "''" );
|
||||
return value.prepend( "'" ).append( "'" );
|
||||
}
|
||||
|
||||
#ifdef HAVE_SPATIALITE
|
||||
void QgisApp::newSpatialiteLayer()
|
||||
{
|
||||
@ -3118,120 +3106,7 @@ void QgisApp::newSpatialiteLayer()
|
||||
return;
|
||||
}
|
||||
QgsNewSpatialiteLayerDialog spatialiteDialog( this );
|
||||
if ( spatialiteDialog.exec() == QDialog::Rejected )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
QString geometrytype = spatialiteDialog.selectedType();
|
||||
//QString fileformat = geomDialog.selectedFileFormat();
|
||||
QString crsId = spatialiteDialog.selectedCrsId();
|
||||
QString databaseName = spatialiteDialog.databaseName();
|
||||
QString newLayerName = spatialiteDialog.layerName();
|
||||
QString newGeometryColumn = spatialiteDialog.geometryColumn();
|
||||
//QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );
|
||||
|
||||
// Get the list containing the name/type pairs for each attribute
|
||||
QList<QStringList> * items = spatialiteDialog.attributes( );
|
||||
|
||||
// Build up the sql statement for creating the table
|
||||
//
|
||||
QString baseSQL;
|
||||
if ( spatialiteDialog.includePrimaryKey() )
|
||||
{
|
||||
baseSQL = "create table %1(pkuid integer primary key autoincrement, ";
|
||||
}
|
||||
else
|
||||
{
|
||||
baseSQL = "create table %1(";
|
||||
}
|
||||
QString sql = baseSQL.arg( quotedIdentifier( newLayerName ) );
|
||||
// iterate through the field names and add them to the create statement
|
||||
// (use indexed access since this is just as fast as iterators
|
||||
for ( int i = 0; i < items->size(); ++i )
|
||||
{
|
||||
QStringList field = items->at( i );
|
||||
if ( i > 0 )
|
||||
sql += ",";
|
||||
sql += QString( "%1 %2" ).arg( quotedIdentifier( field.at( 0 ) ) ).arg( field.at( 1 ) );
|
||||
}
|
||||
// complete the create table statement
|
||||
sql += ")";
|
||||
QgsDebugMsg( QString( "Creating table in database %1" ).arg( databaseName ) );
|
||||
QgsDebugMsg( sql ); // OK
|
||||
|
||||
QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" )
|
||||
.arg( quotedValue( newLayerName ) )
|
||||
.arg( quotedValue( newGeometryColumn ) )
|
||||
.arg( crsId )
|
||||
.arg( quotedValue( geometrytype ) );
|
||||
QgsDebugMsg( sqlAddGeom ); // OK
|
||||
|
||||
QString sqlCreateIndex = QString( "select CreateSpatialIndex(%1,%2)" ).arg( quotedValue( newLayerName ) ).arg( quotedValue( newGeometryColumn ) );
|
||||
QgsDebugMsg( sqlCreateIndex ); // OK
|
||||
|
||||
spatialite_init( 0 );
|
||||
|
||||
sqlite3 *db;
|
||||
int rc = sqlite3_open( databaseName.toUtf8(), &db );
|
||||
if ( rc != SQLITE_OK )
|
||||
{
|
||||
QMessageBox::warning( this,
|
||||
tr( "SpatiaLite Database" ),
|
||||
tr( "Unable to open the database: %1" ).arg( databaseName ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
char * errmsg;
|
||||
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg );
|
||||
if ( rc != SQLITE_OK )
|
||||
{
|
||||
QMessageBox::warning( this,
|
||||
tr( "Error Creating SpatiaLite Table" ),
|
||||
tr( "Failed to create the SpatiaLite table %1. The database returned:\n%2" ).arg( newLayerName ).arg( errmsg ) );
|
||||
sqlite3_free( errmsg );
|
||||
}
|
||||
else
|
||||
{
|
||||
// create the geometry column and the spatial index
|
||||
rc = sqlite3_exec( db, sqlAddGeom.toUtf8(), NULL, NULL, &errmsg );
|
||||
if ( rc != SQLITE_OK )
|
||||
{
|
||||
QMessageBox::warning( this,
|
||||
tr( "Error Creating Geometry Column" ),
|
||||
tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
|
||||
sqlite3_free( errmsg );
|
||||
}
|
||||
else
|
||||
{
|
||||
// create the spatial index
|
||||
rc = sqlite3_exec( db, sqlCreateIndex.toUtf8(), NULL, NULL, &errmsg );
|
||||
if ( rc != SQLITE_OK )
|
||||
{
|
||||
QMessageBox::warning( this,
|
||||
tr( "Error Creating Spatial Index" ),
|
||||
tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
|
||||
sqlite3_free( errmsg );
|
||||
}
|
||||
|
||||
QgsVectorLayer *layer = new QgsVectorLayer( QString( "dbname='%1' table='%2'(%3) sql=" )
|
||||
.arg( databaseName )
|
||||
.arg( newLayerName )
|
||||
.arg( newGeometryColumn ), newLayerName, "spatialite" );
|
||||
if ( layer->isValid() )
|
||||
{
|
||||
// register this layer with the central layers registry
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( layer );
|
||||
}
|
||||
else
|
||||
{
|
||||
QgsDebugMsg( newLayerName + " is an invalid layer - not loaded" );
|
||||
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( newLayerName ) );
|
||||
delete layer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
spatialiteDialog.exec();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -24,6 +24,9 @@
|
||||
#include "qgsspatialitesridsdialog.h"
|
||||
#include "qgsapplication.h"
|
||||
#include "qgisapp.h" // <- for theme icons
|
||||
#include <qgsvectorlayer.h>
|
||||
#include <qgsmaplayerregistry.h>
|
||||
|
||||
#include "qgslogger.h"
|
||||
|
||||
#include <QPushButton>
|
||||
@ -32,7 +35,7 @@
|
||||
#include <QMessageBox>
|
||||
#include <QFileDialog>
|
||||
|
||||
|
||||
#include <spatialite.h>
|
||||
|
||||
QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::WFlags fl )
|
||||
: QDialog( parent, fl )
|
||||
@ -54,42 +57,28 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
|
||||
while ( it != keys.end() )
|
||||
{
|
||||
// retrieving the SQLite DB name and full path
|
||||
//QString text = *it + tr( "@" );
|
||||
QString text = settings.value( *it + "/sqlitepath", "###unknown###" ).toString();
|
||||
mDatabaseComboBox->addItem( text );
|
||||
++it;
|
||||
}
|
||||
settings.endGroup();
|
||||
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
|
||||
mOkButton->setEnabled( false );
|
||||
mOkButton->setDefault( true );
|
||||
|
||||
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
|
||||
buttonBox->button( QDialogButtonBox::Apply )->setEnabled( false );
|
||||
|
||||
connect( buttonBox->button( QDialogButtonBox::Apply ), SIGNAL( clicked() ), this, SLOT( apply() ) );
|
||||
|
||||
buttonBox->button( QDialogButtonBox::Ok )->setDefault( true );
|
||||
|
||||
// Set the SRID box to a default of WGS84
|
||||
leSRID->setText( "4326" );
|
||||
|
||||
// flag to indicate if we need to create a new db before adding a layer
|
||||
needNewDb = false;
|
||||
pbnFindSRID->setEnabled( mDatabaseComboBox->count() );
|
||||
}
|
||||
|
||||
QgsNewSpatialiteLayerDialog::~QgsNewSpatialiteLayerDialog()
|
||||
{
|
||||
}
|
||||
|
||||
void QgsNewSpatialiteLayerDialog::createNewDb()
|
||||
{
|
||||
// QMessageBox::information( 0, tr( "Spatialite Layer" ), tr( "Create new db clicked" ) );
|
||||
QString fileName = QFileDialog::getSaveFileName( this, tr( "New SpatiaLite Database File" ),
|
||||
".",
|
||||
tr( "SpatiaLite (*.sqlite *.db )" ) );
|
||||
|
||||
if ( !fileName.isEmpty() )
|
||||
{
|
||||
mDatabaseComboBox->insertItem( 0, fileName );
|
||||
mDatabaseComboBox->setCurrentIndex( 0 );
|
||||
createDb();
|
||||
needNewDb = true;
|
||||
}
|
||||
}
|
||||
void QgsNewSpatialiteLayerDialog::on_mTypeBox_currentIndexChanged( int index )
|
||||
{
|
||||
// This isn't used since widths are irrelevant in sqlite3
|
||||
@ -103,6 +92,20 @@ void QgsNewSpatialiteLayerDialog::on_mTypeBox_currentIndexChanged( int index )
|
||||
}
|
||||
}
|
||||
|
||||
void QgsNewSpatialiteLayerDialog::on_toolButtonNewDatabase_clicked()
|
||||
{
|
||||
QString fileName = QFileDialog::getSaveFileName( this, tr( "New SpatiaLite Database File" ),
|
||||
".",
|
||||
tr( "SpatiaLite (*.sqlite *.db )" ) );
|
||||
|
||||
if ( fileName.isEmpty() )
|
||||
return;
|
||||
|
||||
mDatabaseComboBox->insertItem( 0, fileName );
|
||||
mDatabaseComboBox->setCurrentIndex( 0 );
|
||||
createDb();
|
||||
}
|
||||
|
||||
QString QgsNewSpatialiteLayerDialog::selectedType() const
|
||||
{
|
||||
if ( mPointRadioButton->isChecked() )
|
||||
@ -134,17 +137,11 @@ QString QgsNewSpatialiteLayerDialog::selectedType() const
|
||||
return "";
|
||||
}
|
||||
|
||||
QString QgsNewSpatialiteLayerDialog::selectedCrsId() const
|
||||
{
|
||||
return leSRID->text();
|
||||
}
|
||||
|
||||
void QgsNewSpatialiteLayerDialog::on_leLayerName_textChanged( QString text )
|
||||
{
|
||||
if ( leLayerName->text().length() > 0 && mAttributeView->topLevelItemCount() > 0 )
|
||||
{
|
||||
mOkButton->setEnabled( true );
|
||||
}
|
||||
bool created = leLayerName->text().length() > 0 && mAttributeView->topLevelItemCount() > 0 && createDb();
|
||||
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( created );
|
||||
buttonBox->button( QDialogButtonBox::Apply )->setEnabled( created );
|
||||
}
|
||||
|
||||
void QgsNewSpatialiteLayerDialog::on_mAddAttributeButton_clicked()
|
||||
@ -157,7 +154,9 @@ void QgsNewSpatialiteLayerDialog::on_mAddAttributeButton_clicked()
|
||||
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << myName << myType ) );
|
||||
if ( mAttributeView->topLevelItemCount() > 0 && leLayerName->text().length() > 0 )
|
||||
{
|
||||
mOkButton->setEnabled( true );
|
||||
bool created = createDb();
|
||||
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( created );
|
||||
buttonBox->button( QDialogButtonBox::Apply )->setEnabled( created );
|
||||
}
|
||||
mNameEdit->clear();
|
||||
}
|
||||
@ -168,19 +167,13 @@ void QgsNewSpatialiteLayerDialog::on_mRemoveAttributeButton_clicked()
|
||||
delete mAttributeView->currentItem();
|
||||
if ( mAttributeView->topLevelItemCount() == 0 )
|
||||
{
|
||||
mOkButton->setEnabled( false );
|
||||
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
|
||||
buttonBox->button( QDialogButtonBox::Apply )->setEnabled( false );
|
||||
}
|
||||
}
|
||||
|
||||
void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()
|
||||
{
|
||||
/* hold on to this for a bit
|
||||
int rc = sqlite3_open( mDatabaseComboBox->currentText().toUtf8().data(), &db );
|
||||
if ( rc != SQLITE_OK )
|
||||
{
|
||||
QMessageBox::warning( this, tr( "Spatialite Database" ), tr( "Unable to open the database: %1").arg(mDatabaseComboBox->currentText() ) );
|
||||
}
|
||||
*/
|
||||
// set the SRID from a list returned from the selected Spatialite database
|
||||
QgsSpatialiteSridsDialog *sridDlg = new QgsSpatialiteSridsDialog( this );
|
||||
if ( sridDlg->load( mDatabaseComboBox->currentText() ) )
|
||||
@ -195,78 +188,190 @@ void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()
|
||||
}
|
||||
}
|
||||
|
||||
// Create a QList of QStringList objects that define the layer attributes.
|
||||
// Each QStringList contains the field name and its type.
|
||||
QList<QStringList> * QgsNewSpatialiteLayerDialog::attributes() const
|
||||
{
|
||||
QTreeWidgetItemIterator it( mAttributeView );
|
||||
QList<QStringList> *list = new QList<QStringList>;
|
||||
while ( *it )
|
||||
{
|
||||
QTreeWidgetItem *item = *it;
|
||||
QStringList items;
|
||||
items << item->text( 0 );
|
||||
items << item->text( 1 );
|
||||
list->append( items );
|
||||
|
||||
//QString type = QString( "%1;%2;%3" ).arg( item->text( 1 ) ).arg( item->text( 2 ) ).arg( item->text( 3 ) );
|
||||
//at.push_back( std::make_pair( item->text( 0 ), type ) );
|
||||
//QgsDebugMsg( QString( "appending %1//%2" ).arg( item->text( 0 ) ).arg( type ) );
|
||||
++it;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
QString QgsNewSpatialiteLayerDialog::databaseName() const
|
||||
{
|
||||
return mDatabaseComboBox->currentText();
|
||||
}
|
||||
|
||||
QString QgsNewSpatialiteLayerDialog::layerName() const
|
||||
{
|
||||
return leLayerName->text();
|
||||
}
|
||||
|
||||
QString QgsNewSpatialiteLayerDialog::geometryColumn() const
|
||||
{
|
||||
return leGeometryColumn->text();
|
||||
}
|
||||
|
||||
bool QgsNewSpatialiteLayerDialog::includePrimaryKey() const
|
||||
{
|
||||
return checkBoxPrimaryKey->isChecked();
|
||||
}
|
||||
|
||||
bool QgsNewSpatialiteLayerDialog::createDb()
|
||||
{
|
||||
QSettings settings;
|
||||
|
||||
if ( mDatabaseComboBox->currentText().isEmpty() )
|
||||
return false;
|
||||
|
||||
QFile newDb( mDatabaseComboBox->currentText() );
|
||||
if ( !newDb.exists() )
|
||||
{
|
||||
qWarning( "creating a new db" );
|
||||
QgsDebugMsg( "creating a new db" );
|
||||
|
||||
// copy the spatilite template to the user specified path
|
||||
QString spatialiteTemplate = QgsApplication::qgisSpatialiteDbTemplatePath();
|
||||
QFile spatialiteTemplateDb( spatialiteTemplate );
|
||||
|
||||
QFileInfo fullPath = QFileInfo( mDatabaseComboBox->currentText() );
|
||||
QDir path = fullPath.dir();
|
||||
qWarning( "making this dir: %s", path.absolutePath().toUtf8().data() );
|
||||
QgsDebugMsg( QString( "making this dir: %1" ).arg( path.absolutePath() ) );
|
||||
|
||||
// Must be sure there is destination directory ~/.qgis
|
||||
QDir().mkpath( path.absolutePath( ) );
|
||||
|
||||
qWarning( "Copying %s ", spatialiteTemplate.toUtf8().data() );
|
||||
qWarning( "to %s", newDb.fileName().toUtf8().data() );
|
||||
QgsDebugMsg( QString( "Copying %1 to %2" ).arg( spatialiteTemplate ).arg( newDb.fileName() ) );
|
||||
|
||||
//now copy the template db file into the chosen location
|
||||
if ( !spatialiteTemplateDb.copy( newDb.fileName() ) )
|
||||
{
|
||||
QMessageBox::warning( 0, tr( "SpatiaLite Database" ), tr( "Could not copy the template database to new location" ) );
|
||||
pbnFindSRID->setEnabled( false );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QFileInfo fi( newDb );
|
||||
if ( !fi.exists() )
|
||||
{
|
||||
pbnFindSRID->setEnabled( false );
|
||||
return false;
|
||||
}
|
||||
|
||||
QString key = "/SpatiaLite/connections/" + fi.fileName() + "/sqlitepath";
|
||||
|
||||
if ( !settings.contains( key ) )
|
||||
{
|
||||
settings.setValue( "/SpatiaLite/connections/selected", fi.fileName() );
|
||||
settings.setValue( key, fi.canonicalFilePath() );
|
||||
|
||||
QMessageBox::information( 0, tr( "SpatiaLite Database" ), tr( "Registered new database!" ) );
|
||||
}
|
||||
|
||||
pbnFindSRID->setEnabled( true );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void QgsNewSpatialiteLayerDialog::on_buttonBox_accepted()
|
||||
{
|
||||
if ( apply() )
|
||||
accept();
|
||||
}
|
||||
|
||||
void QgsNewSpatialiteLayerDialog::on_buttonBox_rejected()
|
||||
{
|
||||
reject();
|
||||
}
|
||||
|
||||
bool QgsNewSpatialiteLayerDialog::apply()
|
||||
{
|
||||
// Build up the sql statement for creating the table
|
||||
QString sql = QString( "create table %1(" ).arg( quotedIdentifier( leLayerName->text() ) );
|
||||
QString delim = "";
|
||||
|
||||
if ( checkBoxPrimaryKey->isChecked() )
|
||||
{
|
||||
sql += "pkuid integer primary key autoincrement,";
|
||||
}
|
||||
|
||||
QTreeWidgetItemIterator it( mAttributeView );
|
||||
while ( *it )
|
||||
{
|
||||
sql += delim + QString( "%1 %2" ).arg( quotedIdentifier(( *it )->text( 0 ) ) ).arg(( *it )->text( 1 ) );
|
||||
|
||||
delim = ",";
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
// complete the create table statement
|
||||
sql += ")";
|
||||
|
||||
QgsDebugMsg( QString( "Creating table in database %1" ).arg( mDatabaseComboBox->currentText() ) );
|
||||
|
||||
QgsDebugMsg( sql ); // OK
|
||||
|
||||
QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" )
|
||||
.arg( quotedValue( leLayerName->text() ) )
|
||||
.arg( quotedValue( leGeometryColumn->text() ) )
|
||||
.arg( leSRID->text().toInt() )
|
||||
.arg( quotedValue( selectedType() ) );
|
||||
QgsDebugMsg( sqlAddGeom ); // OK
|
||||
|
||||
QString sqlCreateIndex = QString( "select CreateSpatialIndex(%1,%2)" )
|
||||
.arg( quotedValue( leLayerName->text() ) )
|
||||
.arg( quotedValue( leGeometryColumn->text() ) );
|
||||
QgsDebugMsg( sqlCreateIndex ); // OK
|
||||
|
||||
spatialite_init( 0 );
|
||||
|
||||
sqlite3 *db;
|
||||
int rc = sqlite3_open( mDatabaseComboBox->currentText().toUtf8(), &db );
|
||||
if ( rc != SQLITE_OK )
|
||||
{
|
||||
QMessageBox::warning( this,
|
||||
tr( "SpatiaLite Database" ),
|
||||
tr( "Unable to open the database: %1" ).arg( mDatabaseComboBox->currentText() ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
char * errmsg;
|
||||
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg );
|
||||
if ( rc != SQLITE_OK )
|
||||
{
|
||||
QMessageBox::warning( this,
|
||||
tr( "Error Creating SpatiaLite Table" ),
|
||||
tr( "Failed to create the SpatiaLite table %1. The database returned:\n%2" ).arg( leLayerName->text() ).arg( errmsg ) );
|
||||
sqlite3_free( errmsg );
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::information( 0, tr( "SpatiaLite Database" ), tr( "Created new database!" ) );
|
||||
// create the geometry column and the spatial index
|
||||
rc = sqlite3_exec( db, sqlAddGeom.toUtf8(), NULL, NULL, &errmsg );
|
||||
if ( rc != SQLITE_OK )
|
||||
{
|
||||
QMessageBox::warning( this,
|
||||
tr( "Error Creating Geometry Column" ),
|
||||
tr( "Failed to create the geometry column. The database returned:\n%1" ).arg( errmsg ) );
|
||||
sqlite3_free( errmsg );
|
||||
}
|
||||
else
|
||||
{
|
||||
// create the spatial index
|
||||
rc = sqlite3_exec( db, sqlCreateIndex.toUtf8(), NULL, NULL, &errmsg );
|
||||
if ( rc != SQLITE_OK )
|
||||
{
|
||||
QMessageBox::warning( this,
|
||||
tr( "Error Creating Spatial Index" ),
|
||||
tr( "Failed to create the spatial index. The database returned:\n%1" ).arg( errmsg ) );
|
||||
sqlite3_free( errmsg );
|
||||
}
|
||||
|
||||
QgsVectorLayer *layer = new QgsVectorLayer( QString( "dbname='%1' table='%2'(%3) sql=" )
|
||||
.arg( mDatabaseComboBox->currentText() )
|
||||
.arg( leLayerName->text() )
|
||||
.arg( leGeometryColumn->text() ), leLayerName->text(), "spatialite" );
|
||||
if ( layer->isValid() )
|
||||
{
|
||||
// register this layer with the central layers registry
|
||||
if ( QgsMapLayerRegistry::instance()->addMapLayer( layer ) )
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
QgsDebugMsg( leLayerName->text() + " is an invalid layer - not loaded" );
|
||||
QMessageBox::critical( this, tr( "Invalid Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( leLayerName->text() ) );
|
||||
delete layer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QString QgsNewSpatialiteLayerDialog::quotedIdentifier( QString id )
|
||||
{
|
||||
id.replace( "\"", "\"\"" );
|
||||
return id.prepend( "\"" ).append( "\"" );
|
||||
}
|
||||
|
||||
QString QgsNewSpatialiteLayerDialog::quotedValue( QString value )
|
||||
{
|
||||
value.replace( "'", "''" );
|
||||
return value.prepend( "'" ).append( "'" );
|
||||
}
|
||||
|
||||
|
||||
|
@ -15,8 +15,8 @@
|
||||
* *
|
||||
***************************************************************************/
|
||||
/* $Id$ */
|
||||
#ifndef qgsnewspatialitelayerdialog_H
|
||||
#define qgsnewspatialitelayerdialog_H
|
||||
#ifndef QGSNEWSPATIALITELAYERDIALOG_H
|
||||
#define QGSNEWSPATIALITELAYERDIALOG_H
|
||||
|
||||
#include "ui_qgsnewspatialitelayerdialogbase.h"
|
||||
#include "qgisgui.h"
|
||||
@ -27,6 +27,7 @@
|
||||
extern "C"
|
||||
{
|
||||
#include <sqlite3.h>
|
||||
#include <spatialite.h>
|
||||
}
|
||||
|
||||
class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteLayerDialogBase
|
||||
@ -36,23 +37,6 @@ class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteL
|
||||
public:
|
||||
QgsNewSpatialiteLayerDialog( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
|
||||
~QgsNewSpatialiteLayerDialog();
|
||||
/**Returns the selected geometry type*/
|
||||
QString selectedType() const;
|
||||
/**Appends the chosen attribute names and types to at*/
|
||||
//void attributes( std::list<std::pair<QString, QString> >& at ) const;
|
||||
QList<QStringList> * attributes() const;
|
||||
/**Returns the database name */
|
||||
QString databaseName() const;
|
||||
/**Returns the layer name to be created */
|
||||
QString layerName() const;
|
||||
/**Returns the geometry column name */
|
||||
QString geometryColumn() const;
|
||||
/**Returns the selected crs id*/
|
||||
QString selectedCrsId() const;
|
||||
/**Returns the state of the primary key checkbox*/
|
||||
bool includePrimaryKey() const;
|
||||
/** Create a new database */
|
||||
bool createDb();
|
||||
|
||||
protected slots:
|
||||
void on_mAddAttributeButton_clicked();
|
||||
@ -60,15 +44,23 @@ class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteL
|
||||
void on_mTypeBox_currentIndexChanged( int index );
|
||||
void on_pbnFindSRID_clicked();
|
||||
void on_leLayerName_textChanged( QString text );
|
||||
void createNewDb();
|
||||
void on_toolButtonNewDatabase_clicked();
|
||||
|
||||
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
|
||||
void on_buttonBox_accepted();
|
||||
void on_buttonBox_rejected();
|
||||
|
||||
bool apply();
|
||||
|
||||
private:
|
||||
QPushButton *mOkButton;
|
||||
int mCrsId;
|
||||
sqlite3 *db;
|
||||
bool needNewDb;
|
||||
/**Returns the selected geometry type*/
|
||||
QString selectedType() const;
|
||||
|
||||
/** Create a new database */
|
||||
bool createDb();
|
||||
|
||||
static QString quotedIdentifier( QString id );
|
||||
static QString quotedValue( QString value );
|
||||
};
|
||||
|
||||
#endif //qgsnewvectorlayerdialog_H
|
||||
#endif // QGSNEWVECTORLAYERDIALOG_H
|
||||
|
@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>431</width>
|
||||
<height>648</height>
|
||||
<width>477</width>
|
||||
<height>596</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
@ -413,7 +413,7 @@
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set>
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok|QDialogButtonBox::Apply</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@ -440,8 +440,8 @@
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>355</x>
|
||||
<y>568</y>
|
||||
<x>364</x>
|
||||
<y>585</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>387</x>
|
||||
@ -456,8 +456,8 @@
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>281</x>
|
||||
<y>568</y>
|
||||
<x>290</x>
|
||||
<y>585</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>242</x>
|
||||
@ -465,24 +465,5 @@
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>toolButtonNewDatabase</sender>
|
||||
<signal>clicked()</signal>
|
||||
<receiver>QgsNewSpatialiteLayerDialogBase</receiver>
|
||||
<slot>createNewDb()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>403</x>
|
||||
<y>28</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>52</x>
|
||||
<y>39</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
<slots>
|
||||
<slot>createNewDb()</slot>
|
||||
</slots>
|
||||
</ui>
|
||||
|
Loading…
x
Reference in New Issue
Block a user