25
25
#include < Q3ProgressDialog>
26
26
#include < QTextStream>
27
27
28
+ #define QGISDEBUG
28
29
29
30
QgsProjectionSelector::QgsProjectionSelector ( QWidget* parent , const char * name , Qt::WFlags fl )
30
31
: QWidget(parent, fl)
31
32
{
32
33
setupUi (this );
33
- connect (lstCoordinateSystems, SIGNAL (currentChanged (Q3ListViewItem *)),
34
- this , SLOT (coordinateSystemSelected (Q3ListViewItem *)));
34
+ connect (lstCoordinateSystems, SIGNAL (currentItemChanged (QTreeWidgetItem*,QTreeWidgetItem *)),
35
+ this , SLOT (coordinateSystemSelected (QTreeWidgetItem *)));
35
36
connect (leSearch, SIGNAL (returnPressed ()), pbnFind, SLOT (animateClick ()));
36
37
37
38
// Get the full path name to the sqlite3 spatial reference database.
@@ -49,42 +50,39 @@ void QgsProjectionSelector::setSelectedSRSName(QString theSRSNAme)
49
50
#ifdef QGISDEBUG
50
51
std::cout << " QgsProjectionSelector::setSelectedSRSName called with \n " << theSRSNAme.toLocal8Bit ().data () << std::endl;
51
52
#endif
52
- // now delegate off to the rest of the work
53
- Q3ListViewItemIterator myIterator (lstCoordinateSystems);
54
- while (myIterator.current ())
55
- {
56
- if (myIterator.current ()->text (0 )==theSRSNAme)
57
- {
58
- lstCoordinateSystems->setCurrentItem (myIterator.current ());
59
- lstCoordinateSystems->ensureItemVisible (myIterator.current ());
60
- return ;
61
- }
62
- ++myIterator;
53
+ QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems (theSRSNAme, Qt::MatchExactly|Qt::MatchRecursive, 0 );
54
+
55
+ if (nodes.count () == 1 )
56
+ lstCoordinateSystems->setCurrentItem (nodes.first ());
57
+ else // unselect the selected item to avoid confusing the user
58
+ {
59
+ if (lstCoordinateSystems->currentItem () != NULL )
60
+ lstCoordinateSystems->setItemSelected (lstCoordinateSystems->currentItem (), false );
61
+ teProjection->setText (" " );
63
62
}
64
63
}
65
64
66
65
void QgsProjectionSelector::setSelectedSRSID (long theSRSID)
67
66
{
68
67
QString mySRSIDString=QString::number (theSRSID);
69
- Q3ListViewItemIterator myIterator (lstCoordinateSystems);
70
- while (myIterator.current ())
71
- {
72
- if (myIterator.current ()->text (1 )==mySRSIDString)
73
- {
74
- lstCoordinateSystems->setCurrentItem (myIterator.current ());
75
- lstCoordinateSystems->ensureItemVisible (myIterator.current ());
76
- return ;
77
- }
78
- ++myIterator;
79
- }
80
68
69
+ QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems (mySRSIDString, Qt::MatchExactly|Qt::MatchRecursive, 1 );
70
+
71
+ if (nodes.count () == 1 )
72
+ lstCoordinateSystems->setCurrentItem (nodes.first ());
73
+ else // unselect the selected item to avoid confusing the user
74
+ {
75
+ if (lstCoordinateSystems->currentItem () != NULL )
76
+ lstCoordinateSystems->setItemSelected (lstCoordinateSystems->currentItem (), false );
77
+ teProjection->setText (" " );
78
+ }
81
79
}
82
80
83
81
// note this line just returns the projection name!
84
82
QString QgsProjectionSelector::getSelectedName ()
85
83
{
86
84
// return the selected wkt name from the list view
87
- Q3ListViewItem *lvi = lstCoordinateSystems->currentItem ();
85
+ QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem ();
88
86
if (lvi)
89
87
{
90
88
return lvi->text (0 );
@@ -103,7 +101,7 @@ QString QgsProjectionSelector::getCurrentProj4String()
103
101
// system
104
102
//
105
103
// Get the selected node
106
- Q3ListViewItem *myItem = lstCoordinateSystems->currentItem ();
104
+ QTreeWidgetItem *myItem = lstCoordinateSystems->currentItem ();
107
105
if (myItem)
108
106
{
109
107
@@ -198,7 +196,7 @@ long QgsProjectionSelector::getCurrentSRID()
198
196
// system
199
197
//
200
198
// Get the selected node
201
- Q3ListViewItem *lvi = lstCoordinateSystems->currentItem ();
199
+ QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem ();
202
200
if (lvi)
203
201
{
204
202
// Make sure the selected node is a srs and not a top-level projection node
@@ -295,7 +293,7 @@ void QgsProjectionSelector::getUserProjList()
295
293
std::cout << " Fetching user projection list..." << std::endl;
296
294
#endif
297
295
// User defined coordinate system node
298
- mUserProjList = new Q3ListViewItem (lstCoordinateSystems," User Defined Coordinate System" );
296
+ mUserProjList = new QTreeWidgetItem (lstCoordinateSystems,QStringList ( " User Defined Coordinate System" ) );
299
297
// determine where the user proj database lives for this user. If none is found an empty
300
298
// now only will be shown
301
299
QString myQGisSettingsDir = QDir::homeDirPath () + " /.qgis/" ;
@@ -337,10 +335,10 @@ void QgsProjectionSelector::getUserProjList()
337
335
// XXX Need to free memory from the error msg if one is set
338
336
if (myResult == SQLITE_OK)
339
337
{
340
- Q3ListViewItem *newItem;
338
+ QTreeWidgetItem *newItem;
341
339
while (sqlite3_step (myPreparedStatement) == SQLITE_ROW)
342
340
{
343
- newItem = new Q3ListViewItem (mUserProjList , QString::fromUtf8 ((char *)sqlite3_column_text (myPreparedStatement,0 )));
341
+ newItem = new QTreeWidgetItem (mUserProjList , QStringList ( QString::fromUtf8 ((char *)sqlite3_column_text (myPreparedStatement,0 ) )));
344
342
// display the qgis srs_id in the second column of the list view
345
343
newItem->setText (1 ,QString::fromUtf8 ((char *)sqlite3_column_text (myPreparedStatement, 1 )));
346
344
}
@@ -355,9 +353,9 @@ void QgsProjectionSelector::getProjList()
355
353
// Create the top-level nodes for the list view of projections
356
354
//
357
355
// Geographic coordinate system node
358
- mGeoList = new Q3ListViewItem (lstCoordinateSystems," Geographic Coordinate System" );
356
+ mGeoList = new QTreeWidgetItem (lstCoordinateSystems,QStringList ( " Geographic Coordinate System" ) );
359
357
// Projected coordinate system node
360
- mProjList = new Q3ListViewItem (lstCoordinateSystems," Projected Coordinate System" );
358
+ mProjList = new QTreeWidgetItem (lstCoordinateSystems,QStringList ( " Projected Coordinate System" ) );
361
359
362
360
// bail out in case the projections db does not exist
363
361
// this is neccessary in case the pc is running linux with a
@@ -413,11 +411,16 @@ void QgsProjectionSelector::getProjList()
413
411
std::cout << " SQL for projection list executed ok..." << std::endl;
414
412
#endif
415
413
416
- Q3ListViewItem *newItem;
414
+ QTreeWidgetItem *newItem;
417
415
// set up the progress dialog
418
416
int myProgress = 1 ;
419
417
Q3ProgressDialog myProgressBar ( tr (" Building Projections List..." ), 0 , myEntriesCount,
420
418
this , " progress" , TRUE );
419
+ // Cache some stuff to speed up creating of the list of projected
420
+ // spatial reference systems
421
+ QString previousSrsType (" " );
422
+ QTreeWidgetItem* previousSrsTypeNode = NULL ;
423
+
421
424
// set initial value to 1
422
425
myProgressBar.setProgress (myProgress);
423
426
while (sqlite3_step (ppStmt) == SQLITE_ROW)
@@ -433,7 +436,7 @@ void QgsProjectionSelector::getProjList()
433
436
{
434
437
// this is a geographic coordinate system
435
438
// Add it to the tree
436
- newItem = new Q3ListViewItem (mGeoList , QString::fromUtf8 ((char *)sqlite3_column_text (ppStmt,0 )));
439
+ newItem = new QTreeWidgetItem (mGeoList , QStringList ( QString::fromUtf8 ((char *)sqlite3_column_text (ppStmt,0 ) )));
437
440
438
441
// display the qgis srs_id in the second column of the list view
439
442
newItem->setText (1 ,QString::fromUtf8 ((char *)sqlite3_column_text (ppStmt, 1 )));
@@ -442,18 +445,32 @@ void QgsProjectionSelector::getProjList()
442
445
{
443
446
// This is a projected srs
444
447
445
- Q3ListViewItem *node;
446
- // Fine the node for this type and add the projection to it
448
+ QTreeWidgetItem *node;
449
+ QString srsType = QString::fromUtf8 ((char *)sqlite3_column_text (ppStmt, 3 ));
450
+ // Find the node for this type and add the projection to it
447
451
// If the node doesn't exist, create it
448
- node = lstCoordinateSystems->findItem (QString::fromUtf8 ((char *)sqlite3_column_text (ppStmt, 3 )),0 );
449
- if (node == 0 )
450
- {
451
- // the node doesn't exist -- create it
452
- node = new Q3ListViewItem (mProjList , QString::fromUtf8 ((char *)sqlite3_column_text (ppStmt, 3 )));
453
- }
454
-
452
+ if (srsType == previousSrsType)
453
+ {
454
+ node = previousSrsTypeNode;
455
+ }
456
+ else
457
+ { // Different from last one, need to search
458
+ QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems (srsType,Qt::MatchExactly|Qt::MatchRecursive,0 );
459
+ if (nodes.count () == 0 )
460
+ {
461
+ // the node doesn't exist -- create it
462
+ node = new QTreeWidgetItem (mProjList , QStringList (srsType));
463
+ }
464
+ else
465
+ {
466
+ node = nodes.first ();
467
+ }
468
+ // Update the cache.
469
+ previousSrsType = srsType;
470
+ previousSrsTypeNode = node;
471
+ }
455
472
// add the item, setting the projection name in the first column of the list view
456
- newItem = new Q3ListViewItem (node, QString::fromUtf8 ((char *)sqlite3_column_text (ppStmt,0 )));
473
+ newItem = new QTreeWidgetItem (node, QStringList ( QString::fromUtf8 ((char *)sqlite3_column_text (ppStmt,0 ) )));
457
474
// set the srs_id in the second column on the list view
458
475
newItem->setText (1 ,QString::fromUtf8 ((char *)sqlite3_column_text (ppStmt, 1 )));
459
476
}
@@ -466,7 +483,9 @@ void QgsProjectionSelector::getProjList()
466
483
// see a progress dialog end at 99%)
467
484
myProgressBar.setProgress (myEntriesCount);
468
485
}
486
+ #ifdef QGISDEBUG
469
487
std::cout << " Size of projection list widget : " << sizeof (*lstCoordinateSystems) << std::endl;
488
+ #endif
470
489
// close the sqlite3 statement
471
490
sqlite3_finalize (ppStmt);
472
491
// close the database
@@ -537,17 +556,24 @@ void QgsProjectionSelector::updateProjAndEllipsoidAcronyms(int theSrsid,QString
537
556
}
538
557
539
558
// New coordinate system selected from the list
540
- void QgsProjectionSelector::coordinateSystemSelected ( Q3ListViewItem * theItem )
559
+ void QgsProjectionSelector::coordinateSystemSelected ( QTreeWidgetItem * theItem)
541
560
{
542
- QString myDescription = tr (" QGIS SRSID: " ) + QString::number (getCurrentSRSID ()) +" \n " ;
543
- myDescription += tr (" PostGIS SRID: " ) + QString::number (getCurrentSRID ()) +" \n " ;
544
- emit sridSelected (QString::number (getCurrentSRSID ()));
545
- QString myProjString = getCurrentProj4String ();
546
- if (!myProjString.isNull ())
547
- {
548
- myDescription+=(myProjString);
561
+ // If the item has children, it's not an end node in the tree, and
562
+ // hence is just a grouping thingy, not an actual SRS.
563
+ if (theItem->childCount () == 0 )
564
+ {
565
+ QString myDescription = tr (" QGIS SRSID: " ) + QString::number (getCurrentSRSID ()) +" \n " ;
566
+ myDescription += tr (" PostGIS SRID: " ) + QString::number (getCurrentSRID ()) +" \n " ;
567
+ emit sridSelected (QString::number (getCurrentSRSID ()));
568
+ QString myProjString = getCurrentProj4String ();
569
+ if (!myProjString.isNull ())
570
+ {
571
+ myDescription+=(myProjString);
572
+ }
573
+ teProjection->setText (myDescription);
549
574
}
550
- teProjection->setText (myDescription);
575
+ else
576
+ teProjection->setText (" " );
551
577
}
552
578
553
579
void QgsProjectionSelector::on_pbnFind_clicked ()
@@ -600,7 +626,7 @@ void QgsProjectionSelector::on_pbnFind_clicked()
600
626
return ;
601
627
}
602
628
#ifdef QGISDEBUG
603
- std::cout << " Search sql" << mySql.toLocal8Bit ().data () << std::endl;
629
+ std::cout << " Search sql: " << mySql.toLocal8Bit ().data () << std::endl;
604
630
#endif
605
631
606
632
//
0 commit comments