@@ -156,49 +156,6 @@ void QgsDbSourceSelect::on_cmbConnections_activated(int)
156
156
dbChanged ();
157
157
}
158
158
159
- void QgsDbSourceSelect::updateTypeInfo (int row, QString type)
160
- {
161
- QComboBox *cb = static_cast <QComboBox *>(lstTables->cellWidget (row,dbssType));
162
- if (!cb)
163
- {
164
- QTableWidgetItem *item = lstTables->takeItem (row,dbssType);
165
- delete item;
166
- }
167
- #if 0 // Qt 4.3
168
- else
169
- lstTables->removeCellWidget(row, dbssType);
170
- #endif
171
-
172
- if ( type.contains (" ," ) )
173
- {
174
- QStringList types = type.split (" ," );
175
-
176
- cb = new QComboBox (lstTables);
177
- for (int i=0 ; i<types.size (); i++) {
178
- cb->addItem ( mLayerIcons .value (types[i]).second , mLayerIcons .value (types[i]).first );
179
- }
180
- cb->setCurrentIndex (0 );
181
- cb->setToolTip ( tr (" select import type for multi type layer" ) );
182
- #if 0 // Qt 4.3
183
- cb->setMinimumContentsLength(mCbMinLength);
184
- cb->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
185
- #endif
186
- lstTables->setCellWidget (row, dbssType, cb);
187
- }
188
- else
189
- {
190
- if (!mLayerIcons .contains (type))
191
- type=" UNKNOWN" ;
192
-
193
- QTableWidgetItem *iconItem = new QTableWidgetItem ();
194
- iconItem->setIcon ( mLayerIcons .value (type).second );
195
- iconItem->setToolTip ( mLayerIcons .value (type).first );
196
- lstTables->setItem (row, dbssType, iconItem);
197
-
198
- lstTables->setItem (row, dbssType, new QTableWidgetItem (*lstTables->item (row,dbssType)));
199
- }
200
- }
201
-
202
159
void QgsDbSourceSelect::setLayerType (QString schema,
203
160
QString table, QString column,
204
161
QString type)
@@ -207,14 +164,31 @@ void QgsDbSourceSelect::setLayerType(QString schema,
207
164
208
165
// Find the right row in the table by searching for the text that
209
166
// was put into the Name column.
210
- QString full_desc = fullDescription (schema, table, column);
167
+ QString full_desc = fullDescription (schema, table, column, " WAITING " );
211
168
212
169
QList<QTableWidgetItem*> ii = lstTables->findItems (full_desc, Qt::MatchExactly);
213
170
214
171
if (ii.count () > 0 )
215
172
{
216
- updateTypeInfo ( lstTables->row (ii.at (0 )), type);
217
- lstTables->resizeColumnToContents (dbssType);
173
+ int row = lstTables->row (ii.at (0 ));
174
+
175
+ if (type!=" " )
176
+ {
177
+ QStringList types = type.split (" ," );
178
+ updateRow (row, fullDescription (schema, table, column, " AS " + types[0 ]), types[0 ]);
179
+
180
+ for (int i=1 ; i<types.size (); i++) {
181
+ lstTables->insertRow (row+1 );
182
+ initRow (row+1 );
183
+ updateRow (row+1 , fullDescription (schema, table, column, " AS " + types[i]), types[i]);
184
+ }
185
+
186
+ lstTables->resizeColumnsToContents ();
187
+ }
188
+ else
189
+ {
190
+ lstTables->removeRow (row);
191
+ }
218
192
}
219
193
}
220
194
@@ -249,7 +223,6 @@ void QgsDbSourceSelect::populateConnectionList()
249
223
}
250
224
void QgsDbSourceSelect::addNewConnection ()
251
225
{
252
-
253
226
QgsNewConnection *nc = new QgsNewConnection (this );
254
227
255
228
if (nc->exec ())
@@ -295,31 +268,21 @@ void QgsDbSourceSelect::addTables()
295
268
{
296
269
// store the table info
297
270
298
- for (int i = 0 ; i < lstTables->rowCount ();)
271
+ for (int i= 0 ; i< lstTables->rowCount (); i++ )
299
272
{
300
273
if ( lstTables->isItemSelected (lstTables->item (i, dbssDetail)) )
301
274
{
302
275
QString table = lstTables->item (i,dbssDetail)->text ();
303
- QString query = table + " sql= " ;
276
+ QString query;
304
277
305
- QComboBox *cb = static_cast <QComboBox *>( lstTables->cellWidget (i, dbssType) );
306
- if (cb)
307
- {
278
+ bool applyFilter = table.contains (" ) AS " );
279
+ if ( applyFilter ) {
308
280
int i = table.find (" (" );
309
281
int j = table.find (" )" );
310
282
QString column = table.mid (i+1 ,j-i-1 );
311
- QString type;
312
-
313
- QMapIterator <QString, QPair < QString, QIcon > > it (mLayerIcons );
314
- while ( it.hasNext () ) {
315
- it.next ();
316
-
317
- if ( it.value ().first == cb->currentText () ) {
318
- type=it.key ();
319
- break ;
320
- }
321
- }
322
-
283
+ QString type = table.mid (j+5 );
284
+ query += table.left (j+1 ) + " sql=" ;
285
+
323
286
if ( type==" POINT" ) {
324
287
query += QString (" GeometryType(\" %1\" ) IN ('POINT','MULTIPOINT')" ).arg (column);
325
288
} else if (type==" LINESTRING" ) {
@@ -330,31 +293,61 @@ void QgsDbSourceSelect::addTables()
330
293
continue ;
331
294
}
332
295
}
296
+ else
297
+ {
298
+ query += table + " sql=" ;
299
+ }
333
300
334
301
QTableWidgetItem *sqlItem = lstTables->item (i, dbssSql);
335
302
if (sqlItem && sqlItem->text ()!=" " )
336
303
{
337
- if (cb )
304
+ if (applyFilter )
338
305
query += QString (" AND (%1)" ).arg ( sqlItem->text () );
339
306
else
340
307
query += sqlItem->text ();
341
308
}
342
309
343
310
m_selectedTables += query;
344
311
}
345
- i++;
346
312
}
347
313
348
314
// BEGIN CHANGES ECOS
349
- if (m_selectedTables.empty () == true )
315
+ if ( m_selectedTables.empty () )
350
316
QMessageBox::information (this , tr (" Select Table" ), tr (" You must select a table in order to add a Layer." ));
351
317
else
352
318
accept ();
353
319
// END CHANGES ECOS
354
320
}
355
321
322
+ void QgsDbSourceSelect::initRow (int row)
323
+ {
324
+ QTableWidgetItem *iconItem = new QTableWidgetItem ();
325
+ lstTables->setItem (row, dbssType, iconItem);
326
+
327
+ QTableWidgetItem *textItem = new QTableWidgetItem ();
328
+ textItem->setToolTip ( tr (" double click to open PostgreSQL query builder" ) );
329
+ lstTables->setItem (row, dbssDetail, textItem);
330
+ }
331
+
332
+ void QgsDbSourceSelect::updateRow (int row, QString detail, QString type)
333
+ {
334
+ if (!mLayerIcons .contains (type))
335
+ type=" UNKNOWN" ;
336
+
337
+ QTableWidgetItem *iconItem = lstTables->item (row, dbssType);
338
+ iconItem->setIcon ( mLayerIcons .value (type).second );
339
+ iconItem->setToolTip ( mLayerIcons .value (type).first );
340
+
341
+ lstTables->item (row, dbssDetail)->setText ( detail );
342
+ }
343
+
356
344
void QgsDbSourceSelect::on_btnConnect_clicked ()
357
345
{
346
+ if (mColumnTypeThread )
347
+ {
348
+ mColumnTypeThread ->stop ();
349
+ mColumnTypeThread =0 ;
350
+ }
358
351
// populate the table list
359
352
QSettings settings;
360
353
@@ -445,16 +438,6 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
445
438
mLayerIcons .insert (" UNKNOWN" ,
446
439
qMakePair (tr (" Unknown layer type" ),
447
440
QIcon (myThemePath+" /mIconUnknownLayerType.png" )));
448
-
449
- #if 0 // Qt 4.3
450
- mCbMinLength = 0;
451
- QMapIterator <QString, QPair < QString, QIcon > > it(mLayerIcons);
452
- while( it.hasNext() ) {
453
- it.next();
454
- int len = it.value().first.length();;
455
- mCbMinLength = mCbMinLength<len ? len : mCbMinLength;
456
- }
457
- #endif
458
441
}
459
442
// qDebug("Connection succeeded");
460
443
// tell the DB that we want text encoded in UTF8
@@ -476,14 +459,12 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
476
459
geomCol::const_iterator iter = details.begin ();
477
460
for (; iter != details.end (); ++iter)
478
461
{
462
+ QString type = iter->second ;
479
463
int row = lstTables->rowCount ();
480
464
lstTables->setRowCount (row+1 );
481
465
482
- QTableWidgetItem *textItem = new QTableWidgetItem (iter->first );
483
- textItem->setToolTip ( tr (" double click to open PostgreSQL query builder" ) );
484
- lstTables->setItem (row, dbssDetail, textItem);
485
-
486
- updateTypeInfo (row, iter->second );
466
+ initRow (row);
467
+ updateRow (row, iter->first , type);
487
468
}
488
469
489
470
// And tidy up the columns & rows
@@ -494,19 +475,13 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
494
475
// may take a long time to return
495
476
if (mColumnTypeThread != NULL )
496
477
{
497
- connect (mColumnTypeThread ,
498
- SIGNAL (setLayerType (QString,QString,QString,QString)),
499
- this ,
500
- SLOT (setLayerType (QString,QString,QString,QString)));
501
- // Do it in a thread. Does not yet cope correctly with the
502
- // layer selection dialog box closing before the thread
503
- // completes, nor the qgis process ending before the
504
- // thread completes, nor does the thread know to stop working
505
- // when the user chooses a layer.
506
- // mColumnTypeThread->start();
507
-
508
- // do it in this process for the moment.
509
- mColumnTypeThread ->getLayerTypes ();
478
+ connect (mColumnTypeThread , SIGNAL (setLayerType (QString,QString,QString,QString)),
479
+ this , SLOT (setLayerType (QString,QString,QString,QString)));
480
+ connect (this , SIGNAL (finished ()),
481
+ mColumnTypeThread , SLOT (stop ()) );
482
+
483
+ // Do it in a thread.
484
+ mColumnTypeThread ->start ();
510
485
}
511
486
}
512
487
else
@@ -590,6 +565,8 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
590
565
{
591
566
bool ok = false ;
592
567
568
+ QApplication::setOverrideCursor (Qt::waitCursor);
569
+
593
570
QString sql = " select * from geometry_columns" ;
594
571
// where f_table_schema ='" + settings.readEntry(key + "/database") + "'";
595
572
sql += " order by f_table_schema,f_table_name" ;
@@ -620,35 +597,23 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
620
597
{
621
598
QString column = PQgetvalue (result, idx, PQfnumber (result, " f_geometry_column" ));
622
599
QString type = PQgetvalue (result, idx, PQfnumber (result, " type" ));
623
- QString v = " " ;
624
-
625
- if (schemaName.length () > 0 )
626
- {
627
- v += ' "' ;
628
- v += schemaName;
629
- v += " \" ." ;
630
- }
631
-
632
- v += ' "' ;
633
- v += tableName;
634
- v += " \" (" ;
635
- v += column;
636
- v += " )" ;
637
-
638
600
601
+ QString as = " " ;
639
602
if (type==" GEOMETRY" && !searchGeometryColumnsOnly) {
640
603
addSearchGeometryColumn (schemaName, tableName, column);
641
- type=" WAITING" ;
604
+ as= type=" WAITING" ;
642
605
}
643
606
644
- details.push_back (geomPair (v , type));
607
+ details.push_back (geomPair (fullDescription (schemaName, tableName, column, as) , type));
645
608
}
646
609
PQclear (exists);
647
610
}
648
611
ok = true ;
649
612
}
650
613
PQclear (result);
651
614
615
+ QApplication::restoreOverrideCursor ();
616
+
652
617
if (searchGeometryColumnsOnly)
653
618
return ok;
654
619
@@ -685,11 +650,8 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
685
650
QString column = PQgetvalue (result, i, 2 ); // attname
686
651
QString relkind = PQgetvalue (result, i, 3 ); // relation kind
687
652
688
- QString full_desc = fullDescription (schema, table, column);
689
-
690
653
addSearchGeometryColumn (schema, table, column);
691
-
692
- details.push_back (geomPair (full_desc, " WAITING" ));
654
+ details.push_back (geomPair (fullDescription (schema, table, column, " WAITING" ), " WAITING" ));
693
655
}
694
656
ok = true ;
695
657
@@ -706,12 +668,12 @@ void QgsDbSourceSelect::showHelp()
706
668
QgsContextHelp::run (context_id);
707
669
}
708
670
QString QgsDbSourceSelect::fullDescription (QString schema, QString table,
709
- QString column )
671
+ QString column, QString type )
710
672
{
711
673
QString full_desc = " " ;
712
674
if (schema.length () > 0 )
713
675
full_desc = ' "' + schema + " \" .\" " ;
714
- full_desc += table + " \" (" + column + " )" ;
676
+ full_desc += table + " \" (" + column + " ) " + type ;
715
677
return full_desc;
716
678
}
717
679
void QgsDbSourceSelect::dbChanged ()
@@ -765,33 +727,43 @@ void QgsGeomColumnTypeThread::addGeometryColumn(QString schema, QString table, Q
765
727
columns.push_back (column);
766
728
}
767
729
730
+ void QgsGeomColumnTypeThread::stop ()
731
+ {
732
+ mStopped =true ;
733
+ }
734
+
768
735
void QgsGeomColumnTypeThread::getLayerTypes ()
769
736
{
737
+ mStopped =false ;
738
+
770
739
PGconn *pd = PQconnectdb (mConnInfo .toLocal8Bit ().data ());
771
740
if (PQstatus (pd) == CONNECTION_OK)
772
741
{
773
742
PQsetClientEncoding (pd, " UNICODE" );
774
743
775
- for (uint i = 0 ; i < schemas.size (); ++i )
744
+ for (uint i = 0 ; i< schemas.size (); i++ )
776
745
{
777
746
QString query = QgsDbSourceSelect::makeGeomQuery (schemas[i],
778
747
tables[i],
779
748
columns[i]);
780
- QgsDebugMsg (" Running SQL:" + query);
781
749
PGresult* gresult = PQexec (pd, query.toLocal8Bit ().data ());
782
750
QString type;
783
751
if (PQresultStatus (gresult) == PGRES_TUPLES_OK) {
784
752
QStringList types;
785
753
786
754
for (int j=0 ; j<PQntuples (gresult); j++) {
787
755
QString type = PQgetvalue (gresult, j, 0 );
788
- types += type!=" " ? type : " UNKNOWN" ;
756
+ if (type!=" " )
757
+ types += type;
789
758
}
790
759
791
760
type = types.join (" ," );
792
761
}
793
762
PQclear (gresult);
794
763
764
+ if (mStopped )
765
+ break ;
766
+
795
767
// Now tell the layer list dialog box...
796
768
emit setLayerType (schemas[i], tables[i], columns[i], type);
797
769
}
0 commit comments