1
/***************************************************************************
2
qgsspit.cpp - description
4
begin : Fri Dec 19 2003
5
copyright : (C) 2003 by Denis Antipov
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
17
/* $Id: qgsspit.cpp 6290 2006-12-20 04:51:35Z telwertowski $ */
19
#include <QMessageBox>
21
#include <QFileDialog>
22
#include <QProgressDialog>
27
#include <QHeaderView>
32
#include "qgsencodingfiledialog.h"
34
#include "qgspgutil.h"
36
#include "qgsconnectiondialog.h"
37
#include "qgseditreservedwordsdialog.h"
38
#include "qgsmessageviewer.h"
39
#include "spiticon.xpm"
40
#include "spit_icons.h"
42
// Qt implementation of alignment() + changed the numeric types to be shown on the left as well
43
/* Is this still needed? Numbers in Qt4 table seem to be left justified by default.
44
int Q3TableItem::alignment() const
47
bool ok1 = FALSE, ok2 = FALSE;
48
( void ) txt.toInt( &ok1 );
50
( void ) txt.toDouble( &ok2 );
53
return ( num ? Qt::AlignLeft : Qt::AlignLeft ) | Qt::AlignVCenter;
57
QgsSpit::QgsSpit( QWidget *parent, Qt::WFlags fl ) : QDialog( parent, fl )
61
icon = QPixmap( spitIcon );
64
// Set up the table column headers
65
tblShapefiles->setColumnCount(5);
66
QStringList headerText;
67
headerText << tr("File Name") << tr("Feature Class") << tr("Features")
68
<< tr("DB Relation Name") << tr("Schema");
69
tblShapefiles->setHorizontalHeaderLabels(headerText);
70
tblShapefiles->verticalHeader()->hide();
71
tblShapefiles->horizontalHeader()->setStretchLastSection(true);
73
populateConnectionList();
78
chkUseDefaultSrid->setChecked( true );
79
chkUseDefaultGeom->setChecked( true );
83
schema_list << "public";
84
gl_key = "/PostgreSQL/connections/";
87
// Install a delegate that provides the combo box widget for
88
// changing the schema (but there can only be one delegate per
89
// table, so it also provides edit widgets for the textual columns).
90
// This needs to be done after the call to getSchema() so that
91
// schema_list is populated.
92
ShapefileTableDelegate* delegate = new ShapefileTableDelegate(tblShapefiles, schema_list);
93
tblShapefiles->setItemDelegate(delegate);
95
// Now that everything is in the table, adjust the column sizes
96
tblShapefiles->resizeColumnsToContents();
102
void QgsSpit::populateConnectionList()
105
QStringList keys = settings.subkeyList( "/PostgreSQL/connections" );
106
QStringList::Iterator it = keys.begin();
107
cmbConnections->clear();
108
while ( it != keys.end() )
110
cmbConnections->insertItem( *it );
115
void QgsSpit::newConnection()
117
QgsConnectionDialog * con = new QgsConnectionDialog( this, tr("New Connection") );
121
populateConnectionList();
126
void QgsSpit::editConnection()
128
QgsConnectionDialog * con = new QgsConnectionDialog( this, cmbConnections->currentText() );
131
con->saveConnection();
136
void QgsSpit::removeConnection()
139
QString key = "/PostgreSQL/connections/" + cmbConnections->currentText();
140
QString msg = tr("Are you sure you want to remove the [") + cmbConnections->currentText() + tr("] connection and all associated settings?");
141
int result = QMessageBox::information( this, tr("Confirm Delete"), msg, tr("Yes"), tr("No") );
144
settings.removeEntry( key + "/host" );
145
settings.removeEntry( key + "/database" );
146
settings.removeEntry( key + "/port" );
147
settings.removeEntry( key + "/username" );
148
settings.removeEntry( key + "/password" );
149
settings.removeEntry( key + "/save" );
151
cmbConnections->removeItem( cmbConnections->currentItem() );
156
void QgsSpit::addFile()
161
bool is_error = false;
164
QgsEncodingFileDialog dlg(this,
165
tr("Add Shapefiles"),
166
settings.readEntry( "/Plugin-Spit/last_directory" ),
167
tr("Shapefiles (*.shp);;All files (*.*)"),
168
settings.readEntry( "/Plugin-Spit/last_encoding" ) );
169
dlg.setMode(QFileDialog::ExistingFiles);
171
if (dlg.exec() != QDialog::Accepted)
173
QStringList files = dlg.selectedFiles();
175
if ( files.size() > 0 )
177
// Save the directory for future use
178
QFileInfo fi( files[ 0 ] );
179
settings.writeEntry( "/Plugin-Spit/last_directory", fi.dirPath( true ) );
180
settings.writeEntry( "/Plugin-Spit/last_encoding", dlg.encoding());
183
for ( QStringList::Iterator it = files.begin(); it != files.end(); ++it )
188
// Check to ensure that we don't insert the same file twice
189
QList<QTableWidgetItem*> items = tblShapefiles->findItems(*it,
191
if (items.count() > 0)
198
// check other files: file.dbf and file.shx
200
if ( !QFile::exists( name.left( name.length() - 3 ) + "dbf" ) )
204
else if ( !QFile::exists( name.left( name.length() - 3 ) + "shx" ) )
211
QgsShapeFile * file = new QgsShapeFile( name, dlg.encoding() );
212
if ( file->is_valid() )
214
/* XXX getFeatureClass actually does a whole bunch
215
* of things and is probably better named
218
QString featureClass = file->getFeatureClass();
219
fileList.push_back( file );
221
QTableWidgetItem *filenameItem = new QTableWidgetItem( name );
222
QTableWidgetItem *featureClassItem = new QTableWidgetItem( featureClass );
223
QTableWidgetItem *featureCountItem = new QTableWidgetItem( QString( "%1" ).arg( file->getFeatureCount() ) );
224
// Sanitize the relation name to make it pg friendly
225
QString relName = file->getTable().replace(QRegExp("\\s"), "_");
226
QTableWidgetItem *dbRelationNameItem = new QTableWidgetItem( relName );
227
QTableWidgetItem *dbSchemaNameItem = new QTableWidgetItem( cmbSchema->currentText() );
229
// All items are editable except for these two
230
filenameItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
231
featureCountItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
233
int row = tblShapefiles->rowCount();
234
tblShapefiles->insertRow( row );
235
tblShapefiles->setItem( row, ColFILENAME, filenameItem );
236
tblShapefiles->setItem( row, ColFEATURECLASS, featureClassItem );
237
tblShapefiles->setItem( row, ColFEATURECOUNT, featureCountItem );
238
tblShapefiles->setItem( row, ColDBRELATIONNAME, dbRelationNameItem );
239
tblShapefiles->setItem( row, ColDBSCHEMA, dbSchemaNameItem );
241
total_features += file->getFeatureCount();
243
// check for postgresql reserved words
244
// First get an instance of the PG utility class
245
QgsPgUtil *pgu = QgsPgUtil::instance();
246
bool hasReservedWords = false;
247
// if a reserved word is found, set the flag so the "adjustment"
248
// dialog can be presented to the user
249
hasReservedWords = false;
250
for ( int i = 0; i < file->column_names.size(); i++ )
252
if ( pgu->isReserved( file->column_names[ i ] ) )
254
hasReservedWords = true;
257
// Why loop through all of them and then turn around and test
258
// the flag? Because if there are reserved words, we want to
259
// add all columns to the listview so the user can have the
260
// opportunity to change them.
261
if ( hasReservedWords )
263
// show the dialog for adjusting reserved words. Reserved
264
// words are displayed differently so they are easy to spot
265
QgsEditReservedWordsDialog * srw = new QgsEditReservedWordsDialog( this );
266
srw->setCaption( file->getTable().upper() + tr(" - Edit Column Names") );
267
// load the reserved words list
268
srw->setReservedWords( pgu->reservedWords() );
269
// load the columns and set their status
270
for ( int i = 0; i < file->column_names.size(); i++ )
272
srw->addColumn( file->column_names[ i ],
273
pgu->isReserved( file->column_names[ i ] ), i );
277
// get the new column specs from the listview control
278
// and replace the existing column spec for the shapefile
279
file->setColumnNames( srw->columnNames() );
287
error1 += name + "\n";
294
error2 += name + "\n";
299
tblShapefiles->resizeColumnsToContents();
301
if ( error1 != "" || error2 != "" )
303
QString message = tr("The following Shapefile(s) could not be loaded:\n\n");
306
error1 += "----------------------------------------------------------------------------------------";
307
error1 += "\n" + tr("REASON: File cannot be opened") + "\n\n";
311
error2 += "----------------------------------------------------------------------------------------";
312
error2 += "\n" + tr("REASON: One or both of the Shapefile files (*.dbf, *.shx) missing") + "\n\n";
314
QgsMessageViewer * e = new QgsMessageViewer( this );
315
e->setMessageAsPlainText( message + error1 + error2 );
320
void QgsSpit::removeFile()
322
std::vector <int> temp;
323
for ( int n = 0; n < tblShapefiles->rowCount(); n++ )
324
if ( tblShapefiles->isItemSelected( tblShapefiles->item( n, 0 ) ) )
326
for ( std::vector<QgsShapeFile *>::iterator vit = fileList.begin(); vit != fileList.end(); vit++ )
328
if ( ( *vit ) ->getName() == tblShapefiles->item( n, 0 )->text() )
330
total_features -= ( *vit ) ->getFeatureCount();
331
fileList.erase( vit );
338
for ( int i = temp.size()-1; i >= 0; --i )
339
tblShapefiles->removeRow( temp[ i ] );
341
tblShapefiles->setCurrentCell( -1, 0 );
344
void QgsSpit::removeAllFiles()
346
int i = tblShapefiles->rowCount() - 1;
348
tblShapefiles->removeRow(i);
354
void QgsSpit::useDefaultSrid()
356
if ( chkUseDefaultSrid->isChecked() )
358
defaultSridValue = spinSrid->value();
359
spinSrid->setValue( defSrid );
360
spinSrid->setEnabled( false );
364
spinSrid->setEnabled( true );
365
spinSrid->setValue( defaultSridValue );
369
void QgsSpit::useDefaultGeom()
371
if ( chkUseDefaultGeom->isChecked() )
373
defaultGeomValue = txtGeomName->text();
374
txtGeomName->setText( defGeom );
375
txtGeomName->setEnabled( false );
379
txtGeomName->setEnabled( true );
380
txtGeomName->setText( defaultGeomValue );
384
// TODO: make translation of helpinfo
385
void QgsSpit::helpInfo()
387
QString message = tr("General Interface Help:") + "\n\n";
389
tr("PostgreSQL Connections:") + "\n" ) + QString(
390
"----------------------------------------------------------------------------------------\n" ) + QString(
391
tr("[New ...] - create a new connection") + "\n" ) + QString(
392
tr("[Edit ...] - edit the currently selected connection") + "\n" ) + QString(
393
tr("[Remove] - remove the currently selected connection") + "\n" ) + QString(
394
tr("-you need to select a connection that works (connects properly) in order to import files") + "\n" ) + QString(
395
tr("-when changing connections Global Schema also changes accordingly") + "\n\n" ) + QString(
396
tr("Shapefile List:") + "\n" ) + QString(
397
"----------------------------------------------------------------------------------------\n" ) + QString(
398
tr("[Add ...] - open a File dialog and browse to the desired file(s) to import") + "\n" ) + QString(
399
tr("[Remove] - remove the currently selected file(s) from the list") + "\n" ) + QString(
400
tr("[Remove All] - remove all the files in the list") + "\n" ) + QString(
401
tr("[SRID] - Reference ID for the shapefiles to be imported") + "\n" ) + QString(
402
tr("[Use Default (SRID)] - set SRID to -1") + "\n" ) + QString(
403
tr("[Geometry Column Name] - name of the geometry column in the database") + "\n" ) + QString(
404
tr("[Use Default (Geometry Column Name)] - set column name to \'the_geom\'") + "\n" ) + QString(
405
tr("[Glogal Schema] - set the schema for all files to be imported into") + "\n\n" ) + QString(
406
"----------------------------------------------------------------------------------------\n" ) + QString(
407
tr("[Import] - import the current shapefiles in the list") + "\n" ) + QString(
408
tr("[Quit] - quit the program\n") ) + QString(
409
tr("[Help] - display this help dialog") + "\n\n" );
410
QgsMessageViewer * e = new QgsMessageViewer( this );
411
e->setMessageAsPlainText( message );
415
PGconn* QgsSpit::checkConnection()
420
QString connName = cmbConnections->currentText();
421
if ( connName.isEmpty() )
423
QMessageBox::warning( this, tr("Import Shapefiles"), tr("You need to specify a Connection first") );
428
QString connInfo = "host=" + settings.readEntry( gl_key + connName + "/host" ) +
429
" dbname=" + settings.readEntry( gl_key + connName + "/database" ) +
430
" port=" + settings.readEntry( gl_key + connName + "/port" ) +
431
" user=" + settings.readEntry( gl_key + connName + "/username" ) +
432
" password=" + settings.readEntry( gl_key + connName + "/password" );
433
pd = PQconnectdb( ( const char * ) connInfo );
435
if ( PQstatus( pd ) != CONNECTION_OK )
437
QMessageBox::warning( this, tr("Import Shapefiles"), tr("Connection failed - Check settings and try again") );
441
int errcode = PQsetClientEncoding(pd, "UNICODE");
444
qWarning("encoding successfully set");
446
qWarning("error in setting encoding");
448
qWarning("undefined return value from encoding setting");
455
// Check that the database actually has postgis in it.
456
QString sql1 = "SELECT postgis_lib_version()"; // available from v 0.9.0 onwards
457
QString sql2 = "SELECT postgis_version()"; // depreciated
459
PGresult* ver = PQexec(pd, sql1.toLocal8Bit().data());
460
if ( PQresultStatus(ver) != PGRES_TUPLES_OK)
462
// In case the version of postgis is older than 0.9.0, try the
463
// depreciated call before erroring out.
465
ver = PQexec(pd, sql2.toLocal8Bit().data());
466
if ( PQresultStatus(ver) != PGRES_TUPLES_OK)
468
QMessageBox::warning( this, tr("PostGIS not available"),
469
tr("<p>The chosen database does not have PostGIS installed, "
470
"but this is required for storage of spatial data.</p>"));
480
void QgsSpit::getSchema()
484
schema_list << "public";
485
PGconn* pd = checkConnection();
488
QString connName = cmbConnections->currentText();
489
QString user = settings.readEntry( gl_key + connName + "/username" );
490
QString schemaSql = QString( "select nspname from pg_namespace,pg_user where nspowner = usesysid and usename = '%1'" ).arg( user );
491
PGresult *schemas = PQexec( pd, ( const char * ) schemaSql );
492
// get the schema names
493
if ( PQresultStatus( schemas ) == PGRES_TUPLES_OK )
495
for ( int i = 0; i < PQntuples( schemas ); i++ )
497
if ( QString( PQgetvalue( schemas, i, 0 ) ) != "public" )
498
schema_list << QString( PQgetvalue( schemas, i, 0 ) );
506
// install a new delegate with an updated schema list (rather than
507
// update the existing delegate because delegates don't seem able to
508
// store modifiable data).
509
ShapefileTableDelegate* delegate = new ShapefileTableDelegate(tblShapefiles, schema_list);
510
tblShapefiles->setItemDelegate(delegate);
513
cmbSchema->insertItems( 0, schema_list );
514
cmbSchema->setCurrentIndex( 0 ); // index 0 is always "public"
517
void QgsSpit::updateSchema()
519
// install a new delegate with an updated schema list (rather than
520
// update the existing delegate because delegates don't seem able to
521
// store modifiable data).
522
ShapefileTableDelegate* delegate = new ShapefileTableDelegate(tblShapefiles, schema_list);
523
tblShapefiles->setItemDelegate(delegate);
526
void QgsSpit::import()
528
tblShapefiles->setCurrentCell( -1, 0 );
530
QString connName = cmbConnections->currentText();
532
bool cancelled = false;
533
PGconn* pd = checkConnection();
536
if ( total_features == 0 )
538
QMessageBox::warning( this, tr("Import Shapefiles"),
539
tr("You need to add shapefiles to the list first") );
541
else if ( pd != NULL )
544
QProgressDialog pro( tr("Importing files"), tr("Cancel"),
545
0, total_features, this);
548
pro.setLabelText(tr("Progress"));
549
pro.setAutoClose( true );
551
qApp->processEvents();
553
for ( int i = 0; i < fileList.size() ; i++ )
555
QString error = tr("Problem inserting features from file:") + "\n" +
556
tblShapefiles->item( i, ColFILENAME )->text();
558
// if a name starts with invalid character
559
if ( ! tblShapefiles->item( i, ColDBRELATIONNAME )->text()[ 0 ].isLetter() )
561
QMessageBox::warning( &pro, tr("Import Shapefiles"),
562
error + "\n" + tr("Invalid table name.") );
563
pro.setValue( pro.value()
564
+ tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
568
// if no fields detected
569
if ( ( fileList[ i ] ->column_names ).size() == 0 )
571
QMessageBox::warning( &pro, tr("Import Shapefiles"),
572
error + "\n" + tr("No fields detected.") );
573
pro.setValue( pro.value() +
574
tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
578
// duplicate field check
579
std::vector<QString> names_copy = fileList[ i ] ->column_names;
580
std::cerr << "Size of names_copy before sort: " << names_copy.size() << std::endl;
582
std::sort( names_copy.begin(), names_copy.end() );
583
std::cerr << "Size of names_copy after sort: " << names_copy.size() << std::endl;
585
for ( int k = 1; k < names_copy.size(); k++ )
587
std::cerr << "USING :" << names_copy[ k ].toLocal8Bit().data() << " index " << k << std::endl;
588
qWarning( tr("Checking to see if ") + names_copy[ k ] + " == " + names_copy[ k - 1 ] );
589
if ( names_copy[ k ] == names_copy[ k - 1 ] )
590
dupl += names_copy[ k ] + "\n";
592
// if duplicate field names exist
595
QMessageBox::warning( &pro, tr("Import Shapefiles"), error +
596
"\n" + tr("The following fields are duplicates:") + "\n" + dupl );
597
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
601
// Check and set destination table
602
fileList[ i ] ->setTable( tblShapefiles->item( i, ColDBRELATIONNAME )->text() );
603
pro.setLabelText( tr("Importing files") + "\n"
604
+ tblShapefiles->item(i, ColFILENAME)->text() );
605
bool rel_exists1 = false;
606
bool rel_exists2 = false;
607
query = "SELECT f_table_name FROM geometry_columns WHERE f_table_name=\'"
608
+ tblShapefiles->item( i, ColDBRELATIONNAME )->text() +
609
"\' AND f_table_schema=\'"
610
+ tblShapefiles->item( i, ColDBSCHEMA )->text() + "\'";
611
res = PQexec( pd, ( const char * ) query );
612
rel_exists1 = ( PQntuples( res ) > 0 );
613
if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
615
QString err = PQresultErrorMessage( res );
617
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
618
tr("<p>Error while executing the SQL:</p><p>") +
619
query + tr("</p><p>The database said:") +
621
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
629
query = "SELECT tablename FROM pg_tables WHERE tablename=\'" + tblShapefiles->item( i, ColDBRELATIONNAME )->text() + "\' AND schemaname=\'" +
630
tblShapefiles->item( i, ColDBSCHEMA )->text() + "\'";
631
res = PQexec( pd, ( const char * ) query );
633
rel_exists2 = ( PQntuples( res ) > 0 );
634
if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
636
QString err = PQresultErrorMessage( res );
638
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
639
tr("<p>Error while executing the SQL:</p><p>") +
640
query + tr("</p><p>The database said:") +
643
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
653
res = PQexec( pd, query );
654
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
656
QString err = PQresultErrorMessage( res );
658
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
659
tr("<p>Error while executing the SQL:</p><p>") +
660
query + tr("</p><p>The database said:") +
662
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
670
query = "SET SEARCH_PATH TO \'";
671
if ( tblShapefiles->item( i, ColDBSCHEMA )->text() == "public" )
674
query += tblShapefiles->item( i, ColDBSCHEMA )->text() + "\', \'public\'";
675
res = PQexec( pd, query );
677
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
679
QString err = PQresultErrorMessage( res );
681
qWarning( PQresStatus( PQresultStatus( res ) ) );
682
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
683
tr("<p>Error while executing the SQL:</p><p>") +
684
query + tr("</p><p>The database said:") +
686
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
694
QMessageBox *del_confirm;
695
if ( rel_exists1 || rel_exists2 )
697
del_confirm = new QMessageBox( tr("Import Shapefiles - Relation Exists"),
698
tr("The Shapefile:" ) + "\n" + tblShapefiles->item( i, 0 )->text() + "\n" + tr("will use [") +
699
tblShapefiles->item( i, ColDBRELATIONNAME )->text() + tr("] relation for its data,") + "\n" + tr("which already exists and possibly contains data.") + "\n" +
700
tr("To avoid data loss change the \"DB Relation Name\"") + "\n" + tr("for this Shapefile in the main dialog file list.") + "\n\n" +
701
tr("Do you want to overwrite the [") + tblShapefiles->item( i, ColDBRELATIONNAME )->text() + tr("] relation?"),
702
QMessageBox::Warning,
703
QMessageBox::Yes | QMessageBox::Default,
704
QMessageBox::No | QMessageBox::Escape,
707
// tr("Relation Exists")
710
if ( del_confirm->exec() == QMessageBox::Yes )
714
query = "DROP TABLE " + tblShapefiles->item( i, ColDBRELATIONNAME )->text();
716
res = PQexec( pd, ( const char * ) query );
717
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
719
QString err = PQresultErrorMessage( res );
721
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
722
tr("<p>Error while executing the SQL:</p><p>") +
723
query + tr("</p><p>The database said:") +
725
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
736
/*query = "SELECT DropGeometryColumn(\'"+QString(settings.readEntry(key + "/database"))+"\', \'"+
737
fileList[i]->getTable()+"\', \'"+txtGeomName->text()+"')";*/
738
query = "DELETE FROM geometry_columns WHERE f_table_schema=\'" + tblShapefiles->item( i, ColDBSCHEMA )->text() + "\' AND " +
739
"f_table_name=\'" + tblShapefiles->item( i, ColDBRELATIONNAME )->text() + "\'";
741
res = PQexec( pd, ( const char * ) query );
742
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
744
QString err = PQresultErrorMessage( res );
746
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
747
tr("<p>Error while executing the SQL:</p><p>") +
748
query + tr("</p><p>The database said:") +
750
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
762
res = PQexec( pd, query );
763
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
765
QString err = PQresultErrorMessage( res );
767
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
768
tr("<p>Error while executing the SQL:</p><p>") +
769
query + tr("</p><p>The database said:") +
776
pro.setValue( pro.value() + tblShapefiles->item( i, 2 )->text().toInt() );
781
// importing file here
782
int temp_progress = pro.value();
784
if ( fileList[ i ] ->insertLayer( settings.readEntry( gl_key + connName + "/database" ), tblShapefiles->item( i, ColDBSCHEMA )->text(),
785
txtGeomName->text(), QString( "%1" ).arg( spinSrid->value() ), pd, pro, cancelled ) && !cancelled )
786
{ // if file has been imported successfully
788
res = PQexec( pd, query );
789
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
791
QString err = PQresultErrorMessage( res );
793
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
794
tr("<p>Error while executing the SQL:</p><p>") +
795
query + tr("</p><p>The database said:") +
805
for ( int j = 0; j < tblShapefiles->rowCount(); j++ )
807
if ( tblShapefiles->item( j, ColFILENAME )->text() == QString( fileList[ i ] ->getName() ) )
809
tblShapefiles->selectRow( j );
817
else if ( !cancelled )
818
{ // if problem importing file occured
819
pro.setValue( temp_progress + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
820
QMessageBox::warning( this, tr("Import Shapefiles"), error );
822
res = PQexec( pd, query );
823
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
825
QString err = PQresultErrorMessage( res );
827
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
828
tr("<p>Error while executing the SQL:</p><p>") +
829
query + tr("</p><p>The database said:") +
838
{ // if import was actually cancelled
845
void QgsSpit::editColumns( int row, int col, int button, const QPoint &mousePos )
847
// get the shapefile - table row maps directly to the fileList array
848
QgsPgUtil * pgu = QgsPgUtil::instance();
849
// show the dialog for adjusting reserved words. Reserved
850
// words are displayed differently so they are easy to spot
851
QgsEditReservedWordsDialog *srw = new QgsEditReservedWordsDialog( this );
852
srw->setCaption( fileList[ row ] ->getTable().upper() + tr(" - Edit Column Names") );
853
// set the description to indicate that we are editing column names, not
854
// necessarily dealing with reserved words (although that is a possibility too)
855
srw->setDescription(tr("Use the table below to edit column names. Make sure that none of the columns are named using a PostgreSQL reserved word"));
856
// load the reserved words list
857
srw->setReservedWords( pgu->reservedWords() );
858
// load the columns and set their status
859
for ( int i = 0; i < fileList[ row ] ->column_names.size(); i++ )
861
srw->addColumn( fileList[ row ] ->column_names[ i ],
862
pgu->isReserved( fileList[ row ] ->column_names[ i ] ), i );
866
// get the new column specs from the listview control
867
// and replace the existing column spec for the shapefile
868
fileList[ row ] ->setColumnNames( srw->columnNames() );
873
void QgsSpit::editShapefile(int row, int col, int button, const QPoint& mousePos)
875
// FIXME Is this necessary any more?
877
if (ColFEATURECLASS == col || ColDBRELATIONNAME == col)
879
tblShapefiles->editCell(row, col, FALSE);
885
QWidget *ShapefileTableDelegate::createEditor(QWidget *parent,
886
const QStyleOptionViewItem &,
887
const QModelIndex & index) const
889
switch(index.column())
893
QComboBox* editor = new QComboBox(parent);
894
editor->setSizeAdjustPolicy(QComboBox::AdjustToContents);
895
editor->installEventFilter(const_cast<ShapefileTableDelegate*>(this));
902
QLineEdit* editor = new QLineEdit(parent);
903
editor->installEventFilter(const_cast<ShapefileTableDelegate*>(this));
910
void ShapefileTableDelegate::setEditorData(QWidget *editor,
911
const QModelIndex &index) const
913
switch(index.column())
917
// Create a combobox and populate with the list of schemas
918
QComboBox *comboBox = static_cast<QComboBox*>(editor);
919
comboBox->insertItems(0, mSchemaList);
920
// Get the text from the table and use to set the selected text
922
QString text = index.model()->data(index, Qt::DisplayRole).toString();
923
comboBox->setCurrentIndex(mSchemaList.indexOf(text));
929
QString text = index.model()->data(index, Qt::DisplayRole).toString();
930
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
931
lineEdit->setText(text);
937
void ShapefileTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
938
const QModelIndex &index) const
940
switch(index.column())
944
QComboBox *comboBox = static_cast<QComboBox*>(editor);
945
QString text = comboBox->currentText();
946
model->setData(index, text);
952
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
953
QString text = lineEdit->text();
955
model->setData(index, text);
961
void ShapefileTableDelegate::updateEditorGeometry(QWidget *editor,
962
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
964
editor->setGeometry(option.rect);