@@ -108,9 +108,7 @@ QgsDbSourceSelect::QgsDbSourceSelect(QgisApp *app, Qt::WFlags fl)
108
108
lstTables->horizontalHeader ()->setStretchLastSection (true );
109
109
// Set the column count
110
110
lstTables->setColumnCount (dbssColumns);
111
- mColumnLabels += " " ;
112
111
mColumnLabels += tr (" Type" );
113
- mColumnLabels += tr (" Import As" );
114
112
mColumnLabels += tr (" Name" );
115
113
mColumnLabels += tr (" Sql" );
116
114
lstTables->setHorizontalHeaderLabels (mColumnLabels );
@@ -158,33 +156,42 @@ void QgsDbSourceSelect::on_cmbConnections_activated(int)
158
156
dbChanged ();
159
157
}
160
158
161
- void QgsDbSourceSelect::updateImportAsInfo (int row, const QString & type)
159
+ void QgsDbSourceSelect::updateTypeInfo (int row, QString type)
162
160
{
163
- if (type==" WAITING" )
164
- return ;
165
-
166
- QComboBox *cb = static_cast <QComboBox *>(lstTables->cellWidget (row,dbssImport));
161
+ QComboBox *cb = static_cast <QComboBox *>(lstTables->cellWidget (row,dbssType));
167
162
if (cb)
168
- lstTables->removeCellWidget (row, dbssImportAs );
163
+ lstTables->removeCellWidget (row, dbssType );
169
164
else
170
165
{
171
- QTableWidgetItem *item = lstTables->takeItem (row,dbssImportAs );
166
+ QTableWidgetItem *item = lstTables->takeItem (row,dbssType );
172
167
delete item;
173
168
}
174
-
175
- if ( type== " GEOMETRY " )
169
+
170
+ if ( type. contains ( " , " ) )
176
171
{
172
+ QStringList types = type.split (" ," );
173
+
177
174
cb = new QComboBox (lstTables);
178
- cb-> addItem ( mLayerIcons . value ( " POINT " ). second , mLayerIcons . value ( " POINT " ). first );
179
- cb->addItem ( mLayerIcons .value (" LINESTRING " ).second , mLayerIcons .value (" LINESTRING " ).first );
180
- cb-> addItem ( mLayerIcons . value ( " POLYGON " ). second , mLayerIcons . value ( " POLYGON " ). first );
175
+ for ( int i= 0 ; i<types. size (); i++) {
176
+ cb->addItem ( mLayerIcons .value (types[i] ).second , mLayerIcons .value (types[i] ).first );
177
+ }
181
178
cb->setCurrentIndex (0 );
182
179
cb->setToolTip ( tr (" select import type for multi type layer" ) );
183
- lstTables->setCellWidget (row, dbssImportAs, cb);
180
+ cb->setMinimumContentsLength (mCbMinLength );
181
+ cb->setSizeAdjustPolicy (QComboBox::AdjustToMinimumContentsLengthWithIcon);
182
+ lstTables->setCellWidget (row, dbssType, cb);
184
183
}
185
184
else
186
185
{
187
- lstTables->setItem (row, dbssImportAs, new QTableWidgetItem (*lstTables->item (row,dbssType)));
186
+ if (!mLayerIcons .contains (type))
187
+ type=" UNKNOWN" ;
188
+
189
+ QTableWidgetItem *iconItem = new QTableWidgetItem ();
190
+ iconItem->setIcon ( mLayerIcons .value (type).second );
191
+ iconItem->setToolTip ( mLayerIcons .value (type).first );
192
+ lstTables->setItem (row, dbssType, iconItem);
193
+
194
+ lstTables->setItem (row, dbssType, new QTableWidgetItem (*lstTables->item (row,dbssType)));
188
195
}
189
196
}
190
197
@@ -198,44 +205,25 @@ void QgsDbSourceSelect::setLayerType(QString schema,
198
205
// was put into the Name column.
199
206
QString full_desc = fullDescription (schema, table, column);
200
207
201
- QList<QTableWidgetItem*> ii = lstTables->findItems (full_desc,
202
- Qt::MatchExactly);
208
+ QList<QTableWidgetItem*> ii = lstTables->findItems (full_desc, Qt::MatchExactly);
203
209
204
210
if (ii.count () > 0 )
205
211
{
206
- int row = lstTables->row (ii.at (0 )); // just use the first one
207
- QTableWidgetItem* iconItem = lstTables->item (row, dbssType);
208
-
209
- // Get the icon and tooltip text
210
- const QIcon* p;
211
- QString toolTipText;
212
- if (mLayerIcons .contains (type))
213
- {
214
- p = &(mLayerIcons .value (type).second );
215
- toolTipText = mLayerIcons .value (type).first ;
216
- }
217
- else
218
- {
219
- QgsDebugMsg (" Unknown geometry type of '" + type + " '." );
220
- p = &(mLayerIcons .value (" UNKNOWN" ).second );
221
- toolTipText = mLayerIcons .value (" UNKNOWN" ).first ;
222
- }
223
-
224
- iconItem->setIcon (*p);
225
- iconItem->setToolTip (toolTipText);
226
-
227
- updateImportAsInfo (row, type);
212
+ updateTypeInfo ( lstTables->row (ii.at (0 )), type);
213
+ lstTables->resizeColumnToContents (dbssType);
228
214
}
229
215
}
230
216
231
217
QString QgsDbSourceSelect::makeGeomQuery (QString schema,
232
218
QString table, QString column)
233
219
{
234
- QString query = " select GeometryType(\" " + column + " \" ) from " ;
235
- if (schema.length () > 0 )
236
- query += " \" " + schema + " \" ." ;
237
- query += " \" " + table + " \" where \" " + column + " \" is not null limit 1" ;
238
- return query;
220
+ return QString (" select distinct "
221
+ " case"
222
+ " when geometrytype(%1) IN ('POINT','MULTIPOINT') THEN 'POINT'"
223
+ " when geometrytype(%1) IN ('LINESTRING','MULTILINESTRING') THEN 'LINESTRING'"
224
+ " when geometrytype(%1) IN ('POLYGON','MULTIPOLYGON') THEN 'POLYGON'"
225
+ " end "
226
+ " from \" %2\" .\" %3\" " ).arg (column).arg (schema).arg (table);
239
227
}
240
228
241
229
QgsDbSourceSelect::~QgsDbSourceSelect ()
@@ -303,29 +291,38 @@ void QgsDbSourceSelect::addTables()
303
291
{
304
292
// store the table info
305
293
306
- for (int i = 0 ; i < lstTables->rowCount (); ++i )
294
+ for (int i = 0 ; i < lstTables->rowCount ();)
307
295
{
308
- if ( static_cast <QCheckBox *> (lstTables->cellWidget (i, dbssImport))-> isChecked ( ) )
296
+ if ( lstTables-> isItemSelected (lstTables->item (i, dbssDetail) ) )
309
297
{
310
298
QString table = lstTables->item (i,dbssDetail)->text ();
311
299
QString query = table + " sql=" ;
312
300
313
- QComboBox *cb = static_cast <QComboBox *>( lstTables->cellWidget (i, dbssImportAs ) );
301
+ QComboBox *cb = static_cast <QComboBox *>( lstTables->cellWidget (i, dbssType ) );
314
302
if (cb) {
315
303
int i = table.find (" (" );
316
304
int j = table.find (" )" );
317
305
QString column = table.mid (i+1 ,j-i-1 );
306
+ QString type;
318
307
319
- switch (cb->currentIndex () ) {
320
- case 0 :
308
+ QMapIterator <QString, QPair<QString, QIcon>> it (mLayerIcons );
309
+ while ( it.hasNext () ) {
310
+ it.next ();
311
+
312
+ if ( it.value ().first == cb->currentText () ) {
313
+ type=it.key ();
314
+ break ;
315
+ }
316
+ }
317
+
318
+ if ( type==" POINT" ) {
321
319
query += QString (" GeometryType(\" %1\" ) IN ('POINT','MULTIPOINT')" ).arg (column);
322
- break ;
323
- case 1 :
320
+ } else if (type==" LINESTRING" ) {
324
321
query += QString (" GeometryType(\" %1\" ) IN ('LINESTRING','MULTILINESTRING')" ).arg (column);
325
- break ;
326
- case 2 :
322
+ } else if (type==" POLYGON" ) {
327
323
query += QString (" GeometryType(\" %1\" ) IN ('POLYGON','MULTIPOLYGON')" ).arg (column);
328
- break ;
324
+ } else {
325
+ continue ;
329
326
}
330
327
}
331
328
@@ -340,6 +337,7 @@ void QgsDbSourceSelect::addTables()
340
337
341
338
m_selectedTables += query;
342
339
}
340
+ i++;
343
341
}
344
342
345
343
// BEGIN CHANGES ECOS
@@ -442,6 +440,14 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
442
440
mLayerIcons .insert (" UNKNOWN" ,
443
441
qMakePair (tr (" Unknown layer type" ),
444
442
QIcon (myThemePath+" /mIconUnknownLayerType.png" )));
443
+
444
+ mCbMinLength = 0 ;
445
+ QMapIterator <QString, QPair<QString, QIcon>> it (mLayerIcons );
446
+ while ( it.hasNext () ) {
447
+ it.next ();
448
+ int len = it.value ().first .length ();;
449
+ mCbMinLength = mCbMinLength <len ? len : mCbMinLength ;
450
+ }
445
451
}
446
452
// qDebug("Connection succeeded");
447
453
// tell the DB that we want text encoded in UTF8
@@ -463,41 +469,14 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
463
469
geomCol::const_iterator iter = details.begin ();
464
470
for (; iter != details.end (); ++iter)
465
471
{
466
- QString toolTipText;
467
- const QIcon* p;
468
-
469
- if (mLayerIcons .contains (iter->second ))
470
- {
471
- p = &(mLayerIcons .value (iter->second ).second );
472
- toolTipText = mLayerIcons .value (iter->second ).first ;
473
- }
474
- else
475
- {
476
- QgsDebugMsg (" Unknown geometry type of '" + iter->second + " '." );
477
- p = &(mLayerIcons .value (" UNKNOWN" ).second );
478
- toolTipText = mLayerIcons .value (" UNKNOWN" ).first ;
479
- }
480
-
481
- if (p != 0 )
482
- {
483
- int row = lstTables->rowCount ();
484
- lstTables->setRowCount (row+1 );
485
-
486
- QCheckBox *cb = new QCheckBox ();
487
- cb->setToolTip (tr (" check to import layer" ));
488
- lstTables->setCellWidget (row, dbssImport, cb);
489
-
490
- QTableWidgetItem *iconItem = new QTableWidgetItem ();
491
- iconItem->setIcon (*p);
492
- iconItem->setToolTip (toolTipText);
493
- lstTables->setItem (row, dbssType, iconItem);
494
-
495
- QTableWidgetItem *textItem = new QTableWidgetItem (iter->first );
496
- textItem->setToolTip ( tr (" double click to open PostgreSQL query builder" ) );
497
- lstTables->setItem (row, dbssDetail, textItem);
498
-
499
- updateImportAsInfo (row, iter->second );
500
- }
472
+ int row = lstTables->rowCount ();
473
+ lstTables->setRowCount (row+1 );
474
+
475
+ QTableWidgetItem *textItem = new QTableWidgetItem (iter->first );
476
+ textItem->setToolTip ( tr (" double click to open PostgreSQL query builder" ) );
477
+ lstTables->setItem (row, dbssDetail, textItem);
478
+
479
+ updateTypeInfo (row, iter->second );
501
480
}
502
481
503
482
// And tidy up the columns & rows
@@ -532,7 +511,8 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
532
511
if (cmbConnections->count () > 0 )
533
512
btnAdd->setEnabled (true );
534
513
// END CHANGES ECOS
535
- } else
514
+ }
515
+ else
536
516
{
537
517
QMessageBox::warning (this , tr (" Connection failed" ),
538
518
tr
@@ -586,6 +566,17 @@ void QgsDbSourceSelect::setSql(QTableWidgetItem *item)
586
566
delete pgb;
587
567
}
588
568
569
+ void QgsDbSourceSelect::addSearchGeometryColumn (const QString &schema, const QString &table, const QString &column)
570
+ {
571
+ // store the column details and do the query in a thread
572
+ if (mColumnTypeThread == NULL )
573
+ {
574
+ mColumnTypeThread = new QgsGeomColumnTypeThread ();
575
+ mColumnTypeThread ->setConnInfo (m_connInfo);
576
+ }
577
+ mColumnTypeThread ->addGeometryColumn (schema, table, column);
578
+ }
579
+
589
580
bool QgsDbSourceSelect::getGeometryColumnInfo (PGconn *pg,
590
581
geomCol& details, bool searchGeometryColumnsOnly,
591
582
bool searchPublicOnly)
@@ -620,6 +611,8 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
620
611
PGresult* exists = PQexec (pg, sql.toLocal8Bit ().data ());
621
612
if (PQntuples (exists) == 1 )
622
613
{
614
+ QString column = PQgetvalue (result, idx, PQfnumber (result, " f_geometry_column" ));
615
+ QString type = PQgetvalue (result, idx, PQfnumber (result, " type" ));
623
616
QString v = " " ;
624
617
625
618
if (schemaName.length () > 0 )
@@ -632,11 +625,16 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
632
625
v += ' "' ;
633
626
v += tableName;
634
627
v += " \" (" ;
635
- v += PQgetvalue (result, idx, PQfnumber (result, " f_geometry_column " )) ;
628
+ v += column ;
636
629
v += " )" ;
637
630
638
- QString type = PQgetvalue (result, idx, PQfnumber (result, " type" ));
639
- details.push_back (geomPair (v, type));
631
+
632
+ if (type==" GEOMETRY" && !searchGeometryColumnsOnly) {
633
+ addSearchGeometryColumn (schemaName, tableName, column);
634
+ type=" WAITING" ;
635
+ }
636
+
637
+ details.push_back (geomPair (v, type));
640
638
}
641
639
PQclear (exists);
642
640
}
@@ -682,34 +680,9 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
682
680
683
681
QString full_desc = fullDescription (schema, table, column);
684
682
685
- QString type = " UNKNOWN" ;
686
- if (relkind == " r" )
687
- {
688
- QString query = makeGeomQuery (schema, table, column);
689
- PGresult* gresult = PQexec (pg, query.toLocal8Bit ().data ());
690
- if (PQresultStatus (gresult) != PGRES_TUPLES_OK)
691
- {
692
- QString myError = (tr (" Access to relation " ) + table + tr (" using sql;\n " ) + query +
693
- tr (" \n has failed. The database said:\n " ));
694
- qDebug (myError + QString (PQresultErrorMessage (gresult)));
695
- }
696
- else if (PQntuples (gresult) > 0 )
697
- type = PQgetvalue (gresult, 0 , 0 ); // GeometryType
698
- PQclear (gresult);
699
- }
700
- else // view
701
- {
702
- // store the column details and do the query in a thread
703
- if (mColumnTypeThread == NULL )
704
- {
705
- mColumnTypeThread = new QgsGeomColumnTypeThread ();
706
- mColumnTypeThread ->setConnInfo (m_connInfo);
707
- }
708
- mColumnTypeThread ->setGeometryColumn (schema, table, column);
709
- type = " WAITING" ;
710
- }
683
+ addSearchGeometryColumn (schema, table, column);
711
684
712
- details.push_back (geomPair (full_desc, type ));
685
+ details.push_back (geomPair (full_desc, " WAITING " ));
713
686
}
714
687
ok = true ;
715
688
@@ -778,7 +751,7 @@ void QgsGeomColumnTypeThread::setConnInfo(QString s)
778
751
mConnInfo = s;
779
752
}
780
753
781
- void QgsGeomColumnTypeThread::setGeometryColumn (QString schema, QString table, QString column)
754
+ void QgsGeomColumnTypeThread::addGeometryColumn (QString schema, QString table, QString column)
782
755
{
783
756
schemas.push_back (schema);
784
757
tables.push_back (table);
@@ -797,10 +770,19 @@ void QgsGeomColumnTypeThread::getLayerTypes()
797
770
QString query = QgsDbSourceSelect::makeGeomQuery (schemas[i],
798
771
tables[i],
799
772
columns[i]);
773
+ QgsDebugMsg (" Running SQL:" + query);
800
774
PGresult* gresult = PQexec (pd, query.toLocal8Bit ().data ());
801
775
QString type;
802
- if (PQresultStatus (gresult) == PGRES_TUPLES_OK)
803
- type = PQgetvalue (gresult, 0 , 0 );
776
+ if (PQresultStatus (gresult) == PGRES_TUPLES_OK) {
777
+ QStringList types;
778
+
779
+ for (int j=0 ; j<PQntuples (gresult); j++) {
780
+ QString type = PQgetvalue (gresult, j, 0 );
781
+ types += type!=" " ? type : " UNKNOWN" ;
782
+ }
783
+
784
+ type = types.join (" ," );
785
+ }
804
786
PQclear (gresult);
805
787
806
788
// Now tell the layer list dialog box...
0 commit comments