@@ -31,12 +31,13 @@ email : sherman at mrcc.com
31
31
#include < QTextOStream>
32
32
#include < QTableWidgetItem>
33
33
#include < QHeaderView>
34
+ #include < QStringList>
34
35
35
36
#include < cassert>
36
37
#include < iostream>
37
38
38
39
QgsDbSourceSelect::QgsDbSourceSelect (QgisApp *app, Qt::WFlags fl)
39
- : QDialog(app, fl), qgisApp(app), mColumnTypeThread(NULL )
40
+ : QDialog(app, fl), qgisApp(app), mColumnTypeThread(NULL ), pd( 0 )
40
41
{
41
42
setupUi (this );
42
43
btnAdd->setEnabled (false );
@@ -104,11 +105,12 @@ QgsDbSourceSelect::QgsDbSourceSelect(QgisApp *app, Qt::WFlags fl)
104
105
105
106
// Do some things that couldn't be done in designer
106
107
lstTables->horizontalHeader ()->setStretchLastSection (true );
107
- QStringList labels;
108
108
// Set the column count to 3 for the type, name, and sql
109
109
lstTables->setColumnCount (3 );
110
- labels += tr (" Type" ); labels += tr (" Name" ); labels += tr (" Sql" );
111
- lstTables->setHorizontalHeaderLabels (labels);
110
+ mColumnLabels += tr (" Type" );
111
+ mColumnLabels += tr (" Name" );
112
+ mColumnLabels += tr (" Sql" );
113
+ lstTables->setHorizontalHeaderLabels (mColumnLabels );
112
114
lstTables->verticalHeader ()->hide ();
113
115
}
114
116
/* * Autoconnected SLOTS **/
@@ -324,43 +326,65 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
324
326
{
325
327
m_connInfo = connString; // host + " " + database + " " + username + " " + password;
326
328
// qDebug(m_connInfo);
329
+ // Tidy up an existing connection if one exists.
330
+ if (pd != 0 )
331
+ PQfinish (pd);
332
+
327
333
pd = PQconnectdb (m_connInfo.toLocal8Bit ().data ());
328
334
// std::cout << pd->ErrorMessage();
329
335
if (PQstatus (pd) == CONNECTION_OK)
330
336
{
331
- // create the pixmaps for the layer types
332
- QString myThemePath = QgsApplication::themePath ();
333
- mLayerIcons .insert (QString (" POINT" ),
334
- qMakePair (tr (" Point layer" ),
335
- QIcon (myThemePath+" /mIconPointLayer.png" )));
336
- mLayerIcons .insert (QString (" MULTIPOINT" ),
337
- qMakePair (tr (" Multi-point layer" ),
338
- QIcon (myThemePath+" /mIconPointLayer.png" )));
339
- mLayerIcons .insert (QString (" LINESTRING" ),
340
- qMakePair (tr (" Linestring layer" ),
341
- QIcon (myThemePath+" /mIconLineLayer.png" )));
342
- mLayerIcons .insert (QString (" MULTILINESTRING" ),
343
- qMakePair (tr (" Multi-linestring layer" ),
344
- QIcon (myThemePath+" /mIconLineLayer.png" )));
345
- mLayerIcons .insert (QString (" POLYGON" ),
346
- qMakePair (tr (" Polygon layer" ),
347
- QIcon (myThemePath+" /mIconPolygonLayer.png" )));
348
- mLayerIcons .insert (QString (" MULTIPOLYGON" ),
349
- qMakePair (tr (" Multi-polygon layer" ),
350
- QIcon (myThemePath+" /mIconPolygonLayer.png" )));
351
- mLayerIcons .insert (QString (" WAITING" ),
352
- qMakePair (tr (" Waiting for layer type" ),
353
- QIcon (myThemePath+" /mIconWaitingForLayerType.png" )));
354
- mLayerIcons .insert (QString (" UNKNOWN" ),
355
- qMakePair (tr (" Unknown layer type" ),
356
- QIcon (myThemePath+" /mIconUnknownLayerType.png" )));
357
-
337
+ // create the pixmaps for the layer types if we haven't already
338
+ // done so.
339
+ if (mLayerIcons .count () == 0 )
340
+ {
341
+ QString myThemePath = QgsApplication::themePath ();
342
+ mLayerIcons .insert (" POINT" ,
343
+ qMakePair (tr (" Point layer" ),
344
+ QIcon (myThemePath+" /mIconPointLayer.png" )));
345
+ mLayerIcons .insert (" MULTIPOINT" ,
346
+ qMakePair (tr (" Multi-point layer" ),
347
+ mLayerIcons .value (" POINT" ).second ));
348
+
349
+ mLayerIcons .insert (" LINESTRING" ,
350
+ qMakePair (tr (" Linestring layer" ),
351
+ QIcon (myThemePath+" /mIconLineLayer.png" )));
352
+ mLayerIcons .insert (" MULTILINESTRING" ,
353
+ qMakePair (tr (" Multi-linestring layer" ),
354
+ mLayerIcons .value (" LINESTRING" ).second ));
355
+
356
+ mLayerIcons .insert (" POLYGON" ,
357
+ qMakePair (tr (" Polygon layer" ),
358
+ QIcon (myThemePath+" /mIconPolygonLayer.png" )));
359
+ mLayerIcons .insert (" MULTIPOLYGON" ,
360
+ qMakePair (tr (" Multi-polygon layer" ),
361
+ mLayerIcons .value (" POLYGON" ).second ));
362
+
363
+ mLayerIcons .insert (" GEOMETRY" ,
364
+ qMakePair (tr (" Mixed geometry layer" ),
365
+ QIcon (myThemePath+" /mIconGeometryLayer.png" )));
366
+ mLayerIcons .insert (" GEOMETRYCOLLECTION" ,
367
+ qMakePair (tr (" Geometry collection layer" ),
368
+ mLayerIcons .value (" GEOMETRY" ).second ));
369
+
370
+ mLayerIcons .insert (" WAITING" ,
371
+ qMakePair (tr (" Waiting for layer type" ),
372
+ QIcon (myThemePath+" /mIconWaitingForLayerType.png" )));
373
+ mLayerIcons .insert (" UNKNOWN" ,
374
+ qMakePair (tr (" Unknown layer type" ),
375
+ QIcon (myThemePath+" /mIconUnknownLayerType.png" )));
376
+ }
358
377
// qDebug("Connection succeeded");
359
378
// tell the DB that we want text encoded in UTF8
360
379
PQsetClientEncoding (pd, " UNICODE" );
361
380
362
- // clear the existing entries
381
+ // Here's an annoying thing... calling clear() removes the
382
+ // header items too, so we need to reinstate them after calling
383
+ // clear().
363
384
lstTables->clear ();
385
+ lstTables->setRowCount (0 );
386
+ lstTables->setHorizontalHeaderLabels (mColumnLabels );
387
+
364
388
// get the list of suitable tables and columns and populate the UI
365
389
geomCol details;
366
390
if (getGeometryColumnInfo (pd, details))
@@ -395,13 +419,6 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
395
419
lstTables->setItem (row, 1 , textItem);
396
420
}
397
421
}
398
- // For some reason not clear to me, the table header labels
399
- // set in the constructor have reverted to '1', '2', and '3'
400
- // by here. Hence we reset them. This seems like a bug in Qt
401
- // (4.1.1).
402
- QStringList labels;
403
- labels += tr (" Type" ); labels += tr (" Name" ); labels += tr (" Sql" );
404
- lstTables->setHorizontalHeaderLabels (labels);
405
422
406
423
// And tidy up the columns & rows
407
424
lstTables->resizeColumnsToContents ();
@@ -420,7 +437,7 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
420
437
// completes, nor the qgis process ending before the
421
438
// thread completes, nor does the thread know to stop working
422
439
// when the user chooses a layer.
423
- // mColumnTypeThread->run ();
440
+ // mColumnTypeThread->start ();
424
441
425
442
// do it in this process for the moment.
426
443
mColumnTypeThread ->getLayerTypes ();
0 commit comments