QGIS/src/app/qgsnewspatialitelayerdialog.cpp
gsherman f36bfa5e39 Implementation of the "Add New Spatialite Layer/Database" feature.
Squashed commit of the following:

commit e6e01ec0b3955d1cf9cb2756dfcf1aac27a0725e
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sun Apr 4 17:45:05 2010 -0800

    Implementation of the add new spatialite layer dialog with ability to search for epsg srid

commit c1d46e35715e4f8baa137800c88483f612ebcb89
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sun Apr 4 17:44:23 2010 -0800

    Spatialite database template used when creating a new database

commit 6d4fd8d80e610d6f0f6d22ed49815134292924c5
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sun Apr 4 17:43:31 2010 -0800

    Context help for the Add New Spatialite Layer dialog

commit 1a6398be174d62df1bf74428bcf898b8d1ea8455
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sun Apr 4 17:42:51 2010 -0800

    Dialog for finding an SRID from the spatialite spatial_ref_sys table

commit 197a65c134ed06b12fd6c8d96900dcf8197f802d
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Sat Apr 3 11:32:58 2010 -0800

    Renamed the spatialite srids dialog selector to the proper form

commit 66c7cafa78f71b7f06504fe3b7bd4577614bfe67
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Fri Apr 2 22:25:09 2010 -0800

    Implemented create new db. Can now create a new db and an empty layer in
    one pass.

commit 088730293ad4722e7d6045e8661e6c28488b8cae
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Fri Apr 2 20:43:12 2010 -0800

    Adjust new spatialite layer dialog. Add field for geometry column name

commit d7c60012ddce6a70d0512bc9195524e6d672b372
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Fri Apr 2 12:46:49 2010 -0800

    New spatialite layer menu item does not show up if qgis is built with internal spatialite

commit 1343dfe7ac7868c38e8b7bd17ef1dd5a4a04ebad
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Fri Apr 2 11:35:26 2010 -0800

    Test of geometry column creation in the dialog

commit c8785f4aaee9d5b08fe0c0dd6f6bbfcd9cf6c7a5
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Wed Mar 31 21:39:03 2010 -0800

    Added multi geometry types to the radio button group

commit 06350192b9dfa6d6cc5c6ac9c4d0b1a65f260476
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Wed Mar 31 21:13:20 2010 -0800

    SQL for creating the new spatialite table complete---still needs the code to actually push it to the database

commit 0e05d6ab9904d003d9c7c7704f53392fc478cee1
Author: Gary Sherman <gsherman@geoapt.com>
Date:   Wed Mar 31 21:12:25 2010 -0800

    Added field for the layer name to be created

commit 3c372b4da3097981d824c2dbb7a661c916d9cf7b
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Tue Mar 30 21:39:10 2010 -0800

    Removed unneeded fields from the spatialite dialog (width and precision). Added methods for getting the attributes

commit 1871db18a5d87f22caf9a34001c879eaf7f58342
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Fri Mar 19 20:51:34 2010 -0800

    Renamed ui file

commit a42e578afac13d2a9e984faedc79d0d206b48c66
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Fri Mar 19 20:51:00 2010 -0800

    Added dialog for displaying and selecting a spatialite srid

commit d0c48bc2317d175fa13c4e52fb6d4f0b10a58518
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Fri Mar 19 19:41:07 2010 -0800

    More work on the dialog to create a new spatialite layer

commit cc6d8a4e48e42c865eec9da3d2ad1e900fbbe295
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Thu Mar 18 20:51:08 2010 -0800

    Wire the new spatialite layer dialog into qgisapp

commit c5a54601fd9b76e46aeb534f92a605cd744ae768
Author: Gary Sherman <gsherman@Macintosh.local>
Date:   Thu Mar 18 20:50:18 2010 -0800

    Dialog implementation for creating a new spatialite layer

git-svn-id: http://svn.osgeo.org/qgis/trunk@13240 c8812cc2-4d05-0410-92ff-de0c093fc19c
2010-04-05 02:32:06 +00:00

321 lines
10 KiB
C++

/***************************************************************************
qgsnewspatialitelayerdialog.cpp
Creates a new Spatialite layer. This dialog borrows heavily from
qgsnewvectorlayerdialog.cpp
-------------------
begin : 2010-03-18
copyright : (C) 2010 by Gary Sherman
email : gsherman@mrcc.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
/*
extern "C"
{
#include <spatialite/sqlite3.h>
#include <spatialite/gaiageo.h>
#include <spatialite.h>
}
*/
#include "qgsnewspatialitelayerdialog.h"
#include "qgsspatialitesridsdialog.h"
#include "qgsapplication.h"
#include "qgisapp.h" // <- for theme icons
#include "qgslogger.h"
//#include <sqlite3.h>
#include <QPushButton>
#include <QSettings>
#include <QLineEdit>
#include <QMessageBox>
#include <QFileDialog>
QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::WFlags fl )
: QDialog( parent, fl )
{
setupUi( this );
mAddAttributeButton->setIcon( QgisApp::getThemeIcon( "/mActionNewAttribute.png" ) );
mRemoveAttributeButton->setIcon( QgisApp::getThemeIcon( "/mActionDeleteAttribute.png" ) );
mTypeBox->addItem( tr( "Text data" ), "text" );
mTypeBox->addItem( tr( "Whole number" ), "integer" );
mTypeBox->addItem( tr( "Decimal number" ), "real" );
mPointRadioButton->setChecked( true );
// Populate the database list from the stored connections
QSettings settings;
settings.beginGroup( "/SpatiaLite/connections" );
QStringList keys = settings.childGroups();
QStringList::Iterator it = keys.begin();
mDatabaseComboBox->clear();
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();
mApplyButton = buttonBox->button( QDialogButtonBox::Apply );
mApplyButton->setEnabled( false );
mApplyButton->setDefault( true );
connect( buttonBox->button( QDialogButtonBox::Apply ), SIGNAL( clicked() ), this, SLOT( apply() ) );
// 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;
}
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);
needNewDb = true;
}
}
void QgsNewSpatialiteLayerDialog::on_mTypeBox_currentIndexChanged( int index )
{
// This isn't used since widths are irrelevant in sqlite3
switch ( index )
{
case 0: // Text data
case 1: // Whole number
case 2: // Decimal number
default:
break;
}
}
QString QgsNewSpatialiteLayerDialog::selectedType() const
{
if ( mPointRadioButton->isChecked() )
{
return "POINT";
}
else if ( mLineRadioButton->isChecked() )
{
return "LINESTRING";
}
else if ( mPolygonRadioButton->isChecked() )
{
return "POLYGON";
}
else if ( mMultipointRadioButton->isChecked() )
{
return "MULTIPOINT";
}
else if ( mMultilineRadioButton->isChecked() )
{
return "MULTILINESTRING";
}
else if ( mMultipolygonRadioButton->isChecked() )
{
return "MULTIPOLYGON";
}
}
QString QgsNewSpatialiteLayerDialog::selectedCrsId() const
{
return leSRID->text();
}
void QgsNewSpatialiteLayerDialog::apply()
{
// Check to see if the db exists and if not create it
createDb();
// Init spatialite
spatialite_init( 0 );
QString newLayerName = leLayerName->text();
int rc = sqlite3_open_v2( mDatabaseComboBox->currentText().toUtf8(), &db, SQLITE_OPEN_READWRITE, NULL );
if ( rc != SQLITE_OK )
{
QMessageBox::warning( this, "Spatialite Database", tr( "Unable to open the database"));
}
else
{
char * errmsg;
QString sql = QString("CREATE TABLE %1 (id integer primary key autoincrement, name text)").arg( newLayerName );
rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg);
if ( rc != SQLITE_OK )
{
QMessageBox::warning( this, tr( "Error Creating Table" ),
tr( "Failed to create the Spatialite table. The database returned:\n%1" ).arg( errmsg ) );
sqlite3_free( errmsg );
}
else
{
// get the geometry type
QString geomType = selectedType();
// create the geometry column and the spatial index
sql = QString("select AddGeometryColumn('%1', 'geom', %2, '%3', 2)").arg( newLayerName ).arg( leSRID->text() ).arg( geomType );
rc = sqlite3_exec( db, sql.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
sql = QString("select CreateSpatialIndex('%1', 'geom')").arg( newLayerName );
rc = sqlite3_exec( db, sql.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 );
}
else
{
QMessageBox::information( 0, tr( "Spatialite Layer" ), tr( "Layer created" ) );
}
}
}
}
}
void QgsNewSpatialiteLayerDialog::on_leLayerName_textChanged( QString text )
{
if ( leLayerName->text().length() > 0 && mAttributeView->topLevelItemCount() > 0)
{
mApplyButton->setEnabled( true );
}
}
void QgsNewSpatialiteLayerDialog::on_mAddAttributeButton_clicked()
{
if ( mNameEdit->text().length() > 0 )
{
QString myName = mNameEdit->text();
//use userrole to avoid translated type string
QString myType = mTypeBox->itemData( mTypeBox->currentIndex(), Qt::UserRole ).toString();
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << myName << myType ) );
if ( mAttributeView->topLevelItemCount() > 0 && leLayerName->text().length() > 0 )
{
mApplyButton->setEnabled( true );
}
mNameEdit->clear();
}
}
void QgsNewSpatialiteLayerDialog::on_mRemoveAttributeButton_clicked()
{
delete mAttributeView->currentItem();
if ( mAttributeView->topLevelItemCount() == 0 )
{
mApplyButton->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, "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() ) )
{
if ( sridDlg->exec() )
{
// set the srid field to the selection
sridDlg->selectedSrid();
leSRID->setText( sridDlg->selectedSrid() );
sridDlg->accept();
}
}
}
// 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();
}
bool QgsNewSpatialiteLayerDialog::createDb()
{
QFile newDb( mDatabaseComboBox->currentText() );
if ( !newDb.exists() )
{
qWarning( "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());
// 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());
//now copy the template db file into the chosen location
bool isDbFileCopied = spatialiteTemplateDb.copy( newDb.fileName() );
if ( !isDbFileCopied )
{
// QMessageBox::warning( this, "Spatialite Database", tr( "Unable to copy the template database to your new location" ));
return false;
}
}
return true;
}