00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <qgsprojectionselector.h>
00012
00013
00014 #include <sqlite3.h>
00015
00016
00017 #include "qgis.h"
00018 #include "qgsapplication.h"
00019 #include "qgslogger.h"
00020 #include "qgscoordinatereferencesystem.h"
00021
00022
00023 #include <QFileInfo>
00024 #include <QHeaderView>
00025 #include <QResizeEvent>
00026 #include <QMessageBox>
00027 #include <QSettings>
00028
00029 QgsProjectionSelector::QgsProjectionSelector( QWidget* parent, const char *name, Qt::WFlags fl )
00030 : QWidget( parent, fl )
00031 , mProjListDone( false )
00032 , mUserProjListDone( false )
00033 , mRecentProjListDone( false )
00034 , mSearchColumn( NONE )
00035 {
00036 Q_UNUSED( name );
00037 setupUi( this );
00038
00039
00040 mSrsDatabaseFileName = QgsApplication::srsDbFilePath();
00041
00042 lstCoordinateSystems->header()->setResizeMode( AUTHID_COLUMN, QHeaderView::Stretch );
00043 lstCoordinateSystems->header()->resizeSection( QGIS_CRS_ID_COLUMN, 0 );
00044 lstCoordinateSystems->header()->setResizeMode( QGIS_CRS_ID_COLUMN, QHeaderView::Fixed );
00045
00046
00047 lstCoordinateSystems->setColumnHidden( QGIS_CRS_ID_COLUMN, true );
00048
00049 lstRecent->header()->setResizeMode( AUTHID_COLUMN, QHeaderView::Stretch );
00050 lstRecent->header()->resizeSection( QGIS_CRS_ID_COLUMN, 0 );
00051 lstRecent->header()->setResizeMode( QGIS_CRS_ID_COLUMN, QHeaderView::Fixed );
00052
00053
00054 lstRecent->setColumnHidden( QGIS_CRS_ID_COLUMN, true );
00055
00056
00057 QSettings settings;
00058 mRecentProjections = settings.value( "/UI/recentProjections" ).toStringList();
00059
00060
00061
00062 QStringList projectionsProj4 = settings.value( "/UI/recentProjectionsProj4" ).toStringList();
00063 QStringList projectionsAuthId = settings.value( "/UI/recentProjectionsAuthId" ).toStringList();
00064 if ( projectionsAuthId.size() >= mRecentProjections.size() )
00065 {
00066
00067
00068 QgsDebugMsg( "Use popular projection list from AuthId/Proj4 saved state" );
00069 mRecentProjections.clear();
00070 for ( int i = 0; i < projectionsAuthId.size(); i++ )
00071 {
00072
00073 QgsCoordinateReferenceSystem crs;
00074 crs.createFromOgcWmsCrs( projectionsAuthId.at( i ) );
00075 if ( ! crs.isValid() )
00076 {
00077
00078 if ( ! crs.createFromProj4( projectionsProj4.at( i ) ) )
00079 {
00080
00081 continue;
00082 }
00083 }
00084 mRecentProjections << QString::number( crs.srsid() );
00085 }
00086 }
00087 }
00088
00089 QgsProjectionSelector::~QgsProjectionSelector()
00090 {
00091
00092 long crsId = selectedCrsId();
00093 if ( crsId == 0 )
00094 return;
00095
00096
00097 mRecentProjections.removeAll( QString::number( crsId ) );
00098 mRecentProjections.prepend( QString::number( crsId ) );
00099
00100 while ( mRecentProjections.size() > 8 )
00101 {
00102 mRecentProjections.removeLast();
00103 }
00104
00105
00106 QSettings settings;
00107 settings.setValue( "/UI/recentProjections", mRecentProjections );
00108
00109
00110
00111 QStringList projectionsProj4;
00112 QStringList projectionsAuthId;
00113 for ( int i = 0; i < mRecentProjections.size(); i++ )
00114 {
00115
00116 QgsCoordinateReferenceSystem crs( mRecentProjections.at( i ).toLong(), QgsCoordinateReferenceSystem::InternalCrsId );
00117 if ( ! crs.isValid() )
00118 {
00119
00120 continue;
00121 }
00122 projectionsProj4 << crs.toProj4();
00123 projectionsAuthId << crs.authid();
00124 }
00125 settings.setValue( "/UI/recentProjectionsProj4", projectionsProj4 );
00126 settings.setValue( "/UI/recentProjectionsAuthId", projectionsAuthId );
00127 }
00128
00129 void QgsProjectionSelector::resizeEvent( QResizeEvent * theEvent )
00130 {
00131 lstCoordinateSystems->header()->resizeSection( NAME_COLUMN, theEvent->size().width() - 240 );
00132 lstCoordinateSystems->header()->resizeSection( AUTHID_COLUMN, 240 );
00133 lstCoordinateSystems->header()->resizeSection( QGIS_CRS_ID_COLUMN, 0 );
00134
00135 lstRecent->header()->resizeSection( NAME_COLUMN, theEvent->size().width() - 240 );
00136 lstRecent->header()->resizeSection( AUTHID_COLUMN, 240 );
00137 lstRecent->header()->resizeSection( QGIS_CRS_ID_COLUMN, 0 );
00138 }
00139
00140 void QgsProjectionSelector::showEvent( QShowEvent * theEvent )
00141 {
00142
00143
00144 loadCrsList( &mCrsFilter );
00145 loadUserCrsList( &mCrsFilter );
00146
00147 if ( !mRecentProjListDone )
00148 {
00149 for ( int i = mRecentProjections.size() - 1; i >= 0; i-- )
00150 insertRecent( mRecentProjections.at( i ).toLong() );
00151 mRecentProjListDone = true;
00152 }
00153
00154
00155 applySelection();
00156
00157
00158 QWidget::showEvent( theEvent );
00159 }
00160
00161 QString QgsProjectionSelector::ogcWmsCrsFilterAsSqlExpression( QSet<QString> * crsFilter )
00162 {
00163 QString sqlExpression = "1";
00164 QMap<QString, QStringList> authParts;
00165
00166 if ( !crsFilter )
00167 return sqlExpression;
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 foreach( QString auth_id, crsFilter->values() )
00188 {
00189 QStringList parts = auth_id.split( ":" );
00190
00191 if ( parts.size() < 2 )
00192 continue;
00193
00194 authParts[ parts.at( 0 ).toUpper()].append( parts.at( 1 ).toUpper() );
00195 }
00196
00197 if ( authParts.isEmpty() )
00198 return sqlExpression;
00199
00200 if ( authParts.size() > 0 )
00201 {
00202 QString prefix = " AND (";
00203 foreach( QString auth_name, authParts.keys() )
00204 {
00205 sqlExpression += QString( "%1(upper(auth_name)='%2' AND upper(auth_id) IN ('%3'))" )
00206 .arg( prefix )
00207 .arg( auth_name )
00208 .arg( authParts[auth_name].join( "','" ) );
00209 prefix = " OR ";
00210 }
00211 sqlExpression += ")";
00212 }
00213
00214 QgsDebugMsg( "exiting with '" + sqlExpression + "'." );
00215
00216 return sqlExpression;
00217 }
00218
00219 void QgsProjectionSelector::setSelectedCrsName( QString theCRSName )
00220 {
00221 applySelection( NAME_COLUMN, theCRSName );
00222 }
00223
00224 void QgsProjectionSelector::setSelectedCrsId( long theCRSID )
00225 {
00226 applySelection( QGIS_CRS_ID_COLUMN, QString::number( theCRSID ) );
00227 }
00228
00229 void QgsProjectionSelector::setSelectedEpsg( long id )
00230 {
00231 setSelectedAuthId( QString( "EPSG:%1" ).arg( id ) );
00232 }
00233
00234 void QgsProjectionSelector::setSelectedAuthId( QString id )
00235 {
00236 applySelection( AUTHID_COLUMN, id );
00237 }
00238
00239 void QgsProjectionSelector::applySelection( int column, QString value )
00240 {
00241 if ( !mProjListDone || !mUserProjListDone )
00242 {
00243
00244 mSearchColumn = column;
00245 mSearchValue = value;
00246 return;
00247 }
00248
00249 if ( column == NONE )
00250 {
00251
00252 column = mSearchColumn;
00253 value = mSearchValue;
00254 }
00255
00256 if ( column == NONE )
00257 return;
00258
00259 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( value, Qt::MatchExactly | Qt::MatchRecursive, column );
00260 if ( nodes.count() > 0 )
00261 {
00262 QgsDebugMsg( QString( "found %1,%2" ).arg( column ).arg( value ) );
00263 lstCoordinateSystems->setCurrentItem( nodes.first() );
00264 }
00265 else
00266 {
00267 QgsDebugMsg( "nothing found" );
00268
00269 lstCoordinateSystems->clearSelection();
00270 lstRecent->clearSelection();
00271 teProjection->setText( "" );
00272 }
00273 }
00274
00275 void QgsProjectionSelector::insertRecent( long theCrsId )
00276 {
00277 if ( !mProjListDone || !mUserProjListDone )
00278 return;
00279
00280 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( QString::number( theCrsId ), Qt::MatchExactly | Qt::MatchRecursive, QGIS_CRS_ID_COLUMN );
00281 if ( nodes.count() == 0 )
00282 return;
00283
00284 lstRecent->insertTopLevelItem( 0, new QTreeWidgetItem( lstRecent, QStringList()
00285 << nodes.first()->text( NAME_COLUMN )
00286 << nodes.first()->text( AUTHID_COLUMN )
00287 << nodes.first()->text( QGIS_CRS_ID_COLUMN ) ) );
00288 }
00289
00290
00291 QString QgsProjectionSelector::selectedName()
00292 {
00293
00294 QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
00295 return lvi ? lvi->text( NAME_COLUMN ) : QString::null;
00296 }
00297
00298
00299 QString QgsProjectionSelector::selectedProj4String()
00300 {
00301
00302
00303
00304
00305
00306
00307 QTreeWidgetItem *item = lstCoordinateSystems->currentItem();
00308 if ( !item || item->text( QGIS_CRS_ID_COLUMN ).isEmpty() )
00309 return "";
00310
00311 QString srsId = item->text( QGIS_CRS_ID_COLUMN );
00312
00313 QgsDebugMsg( "srsId = " + srsId );
00314 QgsDebugMsg( "USER_CRS_START_ID = " + QString::number( USER_CRS_START_ID ) );
00315
00316
00317
00318
00319
00320 QString databaseFileName;
00321 if ( srsId.toLong() >= USER_CRS_START_ID )
00322 {
00323 databaseFileName = QgsApplication::qgisUserDbFilePath();
00324 if ( !QFileInfo( databaseFileName ).exists() )
00325 return QString( "" );
00326 }
00327 else
00328 {
00329 databaseFileName = mSrsDatabaseFileName;
00330 }
00331
00332 QgsDebugMsg( "db = " + databaseFileName );
00333
00334 sqlite3 *database;
00335 int rc = sqlite3_open( databaseFileName.toUtf8().data(), &database );
00336 if ( rc )
00337 {
00338 showDBMissingWarning( databaseFileName );
00339 return "";
00340 }
00341
00342
00343 const char *tail;
00344 sqlite3_stmt *stmt;
00345 QString sql = QString( "select parameters from tbl_srs where srs_id=%1" ).arg( srsId );
00346
00347 QgsDebugMsg( "Selection sql: " + sql );
00348
00349 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
00350
00351 QString projString;
00352 if ( rc == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
00353 {
00354 projString = QString::fromUtf8(( char * )sqlite3_column_text( stmt, 0 ) );
00355 }
00356
00357
00358 sqlite3_finalize( stmt );
00359
00360 sqlite3_close( database );
00361
00362 Q_ASSERT( !projString.isEmpty() );
00363
00364 return projString;
00365 }
00366
00367 QString QgsProjectionSelector::getSelectedExpression( QString expression )
00368 {
00369
00370
00371
00372
00373
00374
00375
00376 QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
00377 if ( !lvi || lvi->text( QGIS_CRS_ID_COLUMN ).isEmpty() )
00378 return 0;
00379
00380
00381
00382
00383
00384 QString databaseFileName;
00385 if ( lvi->text( QGIS_CRS_ID_COLUMN ).toLong() >= USER_CRS_START_ID )
00386 {
00387 databaseFileName = QgsApplication::qgisUserDbFilePath();
00388 if ( !QFileInfo( databaseFileName ).exists() )
00389 {
00390 return 0;
00391 }
00392 }
00393 else
00394 {
00395 databaseFileName = mSrsDatabaseFileName;
00396 }
00397
00398
00399
00400
00401
00402
00403 sqlite3 *database;
00404 int rc = sqlite3_open( databaseFileName.toUtf8().data(), &database );
00405 if ( rc )
00406 {
00407 showDBMissingWarning( databaseFileName );
00408 return 0;
00409 }
00410
00411
00412 const char *tail;
00413 sqlite3_stmt *stmt;
00414 QString sql = QString( "select %1 from tbl_srs where srs_id=%2" )
00415 .arg( expression )
00416 .arg( lvi->text( QGIS_CRS_ID_COLUMN ) );
00417
00418 QgsDebugMsg( QString( "Finding selected attribute using : %1" ).arg( sql ) );
00419 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
00420
00421 QString attributeValue;
00422 if ( rc == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
00423 {
00424
00425 attributeValue = QString::fromUtf8(( char * )sqlite3_column_text( stmt, 0 ) );
00426 }
00427
00428
00429 sqlite3_finalize( stmt );
00430
00431 sqlite3_close( database );
00432
00433
00434 return attributeValue;
00435 }
00436
00437 long QgsProjectionSelector::selectedEpsg()
00438 {
00439 if ( getSelectedExpression( "auth_name" ).compare( "EPSG", Qt::CaseInsensitive ) == 0 )
00440 {
00441 return getSelectedExpression( "auth_id" ).toLong();
00442 }
00443 else
00444 {
00445 QgsDebugMsg( "selected projection is NOT EPSG" );
00446 return 0;
00447 }
00448 }
00449
00450 long QgsProjectionSelector::selectedPostgresSrId()
00451 {
00452 return getSelectedExpression( "srid" ).toLong();
00453 }
00454
00455
00456 QString QgsProjectionSelector::selectedAuthId()
00457 {
00458 int srid = getSelectedExpression( "srs_id" ).toLong();
00459 if ( srid >= USER_CRS_START_ID )
00460 return QString( "USER:%1" ).arg( srid );
00461 else
00462 return getSelectedExpression( "upper(auth_name||':'||auth_id)" );
00463 }
00464
00465
00466 long QgsProjectionSelector::selectedCrsId()
00467 {
00468 QTreeWidgetItem* item = lstCoordinateSystems->currentItem();
00469
00470 if ( item && !item->text( QGIS_CRS_ID_COLUMN ).isEmpty() )
00471 return lstCoordinateSystems->currentItem()->text( QGIS_CRS_ID_COLUMN ).toLong();
00472 else
00473 return 0;
00474 }
00475
00476
00477 void QgsProjectionSelector::setOgcWmsCrsFilter( QSet<QString> crsFilter )
00478 {
00479 mCrsFilter = crsFilter;
00480 mProjListDone = false;
00481 mUserProjListDone = false;
00482 lstCoordinateSystems->clear();
00483 }
00484
00485 void QgsProjectionSelector::loadUserCrsList( QSet<QString> *crsFilter )
00486 {
00487 if ( mUserProjListDone )
00488 return;
00489
00490 QgsDebugMsg( "Fetching user projection list..." );
00491
00492
00493 QString sqlFilter = ogcWmsCrsFilterAsSqlExpression( crsFilter );
00494
00495
00496
00497 mUserProjList = new QTreeWidgetItem( lstCoordinateSystems, QStringList( tr( "User Defined Coordinate Systems" ) ) );
00498
00499 QFont fontTemp = mUserProjList->font( 0 );
00500 fontTemp.setItalic( true );
00501 fontTemp.setBold( true );
00502 mUserProjList->setFont( 0, fontTemp );
00503 mUserProjList->setIcon( 0, QIcon( QgsApplication::activeThemePath() + "user.png" ) );
00504
00505
00506
00507 QString databaseFileName = QgsApplication::qgisUserDbFilePath();
00508
00509
00510
00511
00512 if ( !QFileInfo( databaseFileName ).exists( ) )
00513 {
00514 QgsDebugMsg( "Users qgis.db not found...skipping" );
00515 mUserProjListDone = true;
00516 return;
00517 }
00518
00519 sqlite3 *database;
00520 const char *tail;
00521 sqlite3_stmt *stmt;
00522
00523 int result = sqlite3_open( databaseFileName.toUtf8().constData(), &database );
00524 if ( result )
00525 {
00526
00527
00528
00529
00530 showDBMissingWarning( databaseFileName );
00531 return;
00532 }
00533
00534
00535 QString sql = QString( "select description, srs_id from vw_srs where %1" ).arg( sqlFilter );
00536
00537 result = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
00538
00539 if ( result == SQLITE_OK )
00540 {
00541 QTreeWidgetItem *newItem;
00542 while ( sqlite3_step( stmt ) == SQLITE_ROW )
00543 {
00544 newItem = new QTreeWidgetItem( mUserProjList, QStringList( QString::fromUtf8(( char * )sqlite3_column_text( stmt, 0 ) ) ) );
00545
00546
00547
00548
00549
00550 newItem->setText( QGIS_CRS_ID_COLUMN, QString::fromUtf8(( char * )sqlite3_column_text( stmt, 1 ) ) );
00551 }
00552 }
00553
00554 sqlite3_finalize( stmt );
00555 sqlite3_close( database );
00556
00557 mUserProjListDone = true;
00558 }
00559
00560 void QgsProjectionSelector::loadCrsList( QSet<QString> *crsFilter )
00561 {
00562 if ( mProjListDone )
00563 return;
00564
00565
00566 QString sqlFilter = ogcWmsCrsFilterAsSqlExpression( crsFilter );
00567
00568
00569
00570
00571
00572 mGeoList = new QTreeWidgetItem( lstCoordinateSystems, QStringList( tr( "Geographic Coordinate Systems" ) ) );
00573
00574 QFont fontTemp = mGeoList->font( 0 );
00575 fontTemp.setItalic( true );
00576 fontTemp.setBold( true );
00577 mGeoList->setFont( 0, fontTemp );
00578 mGeoList->setIcon( 0, QIcon( QgsApplication::activeThemePath() + "geographic.png" ) );
00579
00580
00581 mProjList = new QTreeWidgetItem( lstCoordinateSystems, QStringList( tr( "Projected Coordinate Systems" ) ) );
00582
00583 fontTemp = mProjList->font( 0 );
00584 fontTemp.setItalic( true );
00585 fontTemp.setBold( true );
00586 mProjList->setFont( 0, fontTemp );
00587 mProjList->setIcon( 0, QIcon( QgsApplication::activeThemePath() + "transformed.png" ) );
00588
00589
00590
00591
00592
00593
00594 if ( !QFileInfo( mSrsDatabaseFileName ).exists() )
00595 {
00596 mProjListDone = true;
00597 return;
00598 }
00599
00600
00601 sqlite3 *database;
00602 int rc = sqlite3_open( mSrsDatabaseFileName.toUtf8().data(), &database );
00603 if ( rc )
00604 {
00605
00606
00607 showDBMissingWarning( mSrsDatabaseFileName );
00608 return;
00609 }
00610
00611 const char *tail;
00612 sqlite3_stmt *stmt;
00613
00614 QString sql = "select count(*) from tbl_srs";
00615
00616 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
00617 Q_ASSERT( rc == SQLITE_OK );
00618 sqlite3_step( stmt );
00619 sqlite3_finalize( stmt );
00620
00621
00622
00623
00624 sql = QString( "select description, srs_id, upper(auth_name||':'||auth_id), is_geo, name, parameters, deprecated from vw_srs where %1 order by name,description" )
00625 .arg( sqlFilter );
00626
00627 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
00628
00629 if ( rc == SQLITE_OK )
00630 {
00631 QTreeWidgetItem *newItem;
00632
00633
00634 QString previousSrsType( "" );
00635 QTreeWidgetItem* previousSrsTypeNode = 0;
00636
00637 while ( sqlite3_step( stmt ) == SQLITE_ROW )
00638 {
00639
00640 int isGeo = sqlite3_column_int( stmt, 3 );
00641 if ( isGeo )
00642 {
00643
00644
00645 newItem = new QTreeWidgetItem( mGeoList, QStringList( QString::fromUtf8(( char * )sqlite3_column_text( stmt, 0 ) ) ) );
00646
00647
00648 newItem->setText( AUTHID_COLUMN, QString::fromUtf8(( char * )sqlite3_column_text( stmt, 2 ) ) );
00649
00650
00651 newItem->setText( QGIS_CRS_ID_COLUMN, QString::fromUtf8(( char * )sqlite3_column_text( stmt, 1 ) ) );
00652 }
00653 else
00654 {
00655
00656 QTreeWidgetItem *node;
00657 QString srsType = QString::fromUtf8(( char* )sqlite3_column_text( stmt, 4 ) );
00658
00659
00660 if ( srsType == previousSrsType )
00661 {
00662 node = previousSrsTypeNode;
00663 }
00664 else
00665 {
00666 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( srsType, Qt::MatchExactly | Qt::MatchRecursive, NAME_COLUMN );
00667 if ( nodes.count() == 0 )
00668 {
00669
00670
00671 node = new QTreeWidgetItem( mProjList, QStringList( srsType ) );
00672 QFont fontTemp = node->font( 0 );
00673 fontTemp.setItalic( true );
00674 node->setFont( 0, fontTemp );
00675 }
00676 else
00677 {
00678 node = nodes.first();
00679 }
00680
00681 previousSrsType = srsType;
00682 previousSrsTypeNode = node;
00683 }
00684
00685 newItem = new QTreeWidgetItem( node, QStringList( QString::fromUtf8(( char * )sqlite3_column_text( stmt, 0 ) ) ) );
00686
00687 newItem->setText( AUTHID_COLUMN, QString::fromUtf8(( char * )sqlite3_column_text( stmt, 2 ) ) );
00688
00689 newItem->setText( QGIS_CRS_ID_COLUMN, QString::fromUtf8(( char * )sqlite3_column_text( stmt, 1 ) ) );
00690
00691 newItem->parent()->setExpanded( true );
00692 }
00693
00694
00695 newItem->setData( 0, Qt::UserRole, QString::fromUtf8(( char * )sqlite3_column_text( stmt, 6 ) ) );
00696 newItem->setHidden( cbxHideDeprecated->isChecked() );
00697 }
00698 mProjList->setExpanded( true );
00699 }
00700
00701
00702 sqlite3_finalize( stmt );
00703
00704 sqlite3_close( database );
00705
00706 mProjListDone = true;
00707 }
00708
00709
00710 void QgsProjectionSelector::on_lstCoordinateSystems_currentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem * )
00711 {
00712 QgsDebugMsg( "Entered." );
00713
00714 if ( !current )
00715 {
00716 QgsDebugMsg( "no current item" );
00717 return;
00718 }
00719
00720 lstCoordinateSystems->scrollToItem( current );
00721
00722
00723
00724 if ( current->childCount() == 0 )
00725 {
00726
00727 emit sridSelected( QString::number( selectedCrsId() ) );
00728
00729 teProjection->setText( selectedProj4String() );
00730
00731 QList<QTreeWidgetItem*> nodes = lstRecent->findItems( current->text( QGIS_CRS_ID_COLUMN ), Qt::MatchExactly, QGIS_CRS_ID_COLUMN );
00732 if ( nodes.count() > 0 )
00733 {
00734 QgsDebugMsg( QString( "found srs %1 in recent" ).arg( current->text( QGIS_CRS_ID_COLUMN ) ) );
00735 lstRecent->setCurrentItem( nodes.first() );
00736 }
00737 else
00738 {
00739 QgsDebugMsg( QString( "srs %1 not recent" ).arg( current->text( QGIS_CRS_ID_COLUMN ) ) );
00740 lstRecent->clearSelection();
00741 }
00742 }
00743 else
00744 {
00745
00746 current->setSelected( false );
00747 teProjection->setText( "" );
00748 lstRecent->clearSelection();
00749 }
00750 }
00751
00752 void QgsProjectionSelector::on_lstRecent_currentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem * )
00753 {
00754 QgsDebugMsg( "Entered." );
00755
00756 if ( !current )
00757 {
00758 QgsDebugMsg( "no current item" );
00759 return;
00760 }
00761
00762 lstRecent->scrollToItem( current );
00763
00764 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( current->text( QGIS_CRS_ID_COLUMN ), Qt::MatchExactly | Qt::MatchRecursive, QGIS_CRS_ID_COLUMN );
00765 if ( nodes.count() > 0 )
00766 lstCoordinateSystems->setCurrentItem( nodes.first() );
00767 }
00768
00769 void QgsProjectionSelector::hideDeprecated( QTreeWidgetItem *item )
00770 {
00771 if ( item->data( 0, Qt::UserRole ).toBool() )
00772 {
00773 item->setHidden( cbxHideDeprecated->isChecked() );
00774 if ( item->isSelected() && item->isHidden() )
00775 {
00776 item->setSelected( false );
00777 teProjection->setText( "" );
00778 }
00779 }
00780
00781 for ( int i = 0; i < item->childCount(); i++ )
00782 hideDeprecated( item->child( i ) );
00783 }
00784
00785 void QgsProjectionSelector::on_cbxHideDeprecated_stateChanged()
00786 {
00787 for ( int i = 0; i < lstCoordinateSystems->topLevelItemCount(); i++ )
00788 hideDeprecated( lstCoordinateSystems->topLevelItem( i ) );
00789 }
00790
00791 void QgsProjectionSelector::on_leSearch_textChanged( const QString & theFilterTxt )
00792 {
00793
00794 QTreeWidgetItemIterator itr( lstRecent );
00795 while ( *itr )
00796 {
00797 if (( *itr )->childCount() == 0 )
00798 {
00799 if (( *itr )->text( NAME_COLUMN ).contains( theFilterTxt, Qt::CaseInsensitive )
00800 || ( *itr )->text( AUTHID_COLUMN ).contains( theFilterTxt, Qt::CaseInsensitive )
00801 )
00802 {
00803 ( *itr )->setHidden( false );
00804 QTreeWidgetItem * parent = ( *itr )->parent();
00805 while ( parent )
00806 {
00807 parent->setExpanded( true );
00808 parent->setHidden( false );
00809 parent = parent->parent();
00810 }
00811 }
00812 else
00813 {
00814 ( *itr )->setHidden( true );
00815 }
00816 }
00817 else
00818 {
00819 ( *itr )->setHidden( true );
00820 }
00821 ++itr;
00822 }
00823
00824
00825 QTreeWidgetItemIterator it( lstCoordinateSystems );
00826 while ( *it )
00827 {
00828 if (( *it )->childCount() == 0 )
00829 {
00830 if (( *it )->text( NAME_COLUMN ).contains( theFilterTxt, Qt::CaseInsensitive )
00831 || ( *it )->text( AUTHID_COLUMN ).contains( theFilterTxt, Qt::CaseInsensitive )
00832 )
00833 {
00834 ( *it )->setHidden( false );
00835 QTreeWidgetItem * parent = ( *it )->parent();
00836 while ( parent )
00837 {
00838 parent->setExpanded( true );
00839 parent->setHidden( false );
00840 parent = parent->parent();
00841 }
00842 }
00843 else
00844 {
00845 ( *it )->setHidden( true );
00846 }
00847 }
00848 else
00849 {
00850 ( *it )->setHidden( true );
00851 }
00852 ++it;
00853 }
00854 }
00855
00856
00857 long QgsProjectionSelector::getLargestCRSIDMatch( QString theSql )
00858 {
00859 long srsId = 0;
00860
00861
00862
00863
00864
00865 sqlite3 *database;
00866 const char *tail;
00867 sqlite3_stmt *stmt;
00868 int result;
00869
00870
00871
00872
00873 QString databaseFileName = QgsApplication::qgisUserDbFilePath();
00874 if ( QFileInfo( databaseFileName ).exists() )
00875 {
00876 result = sqlite3_open( databaseFileName.toUtf8().data(), &database );
00877 if ( result )
00878 {
00879
00880
00881
00882
00883 showDBMissingWarning( databaseFileName );
00884 return 0;
00885 }
00886
00887 result = sqlite3_prepare( database, theSql.toUtf8(), theSql.toUtf8().length(), &stmt, &tail );
00888
00889 if ( result == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
00890 {
00891 QString srsIdString = QString::fromUtf8(( char * )sqlite3_column_text( stmt, 0 ) );
00892 srsId = srsIdString.toLong();
00893
00894 sqlite3_finalize( stmt );
00895 sqlite3_close( database );
00896 return srsId;
00897 }
00898 }
00899 else
00900 {
00901
00902 result = sqlite3_open( mSrsDatabaseFileName.toUtf8().data(), &database );
00903 if ( result )
00904 {
00905 QgsDebugMsg( QString( "Can't open * user * database: %1" ).arg( sqlite3_errmsg( database ) ) );
00906
00907 return 0;
00908 }
00909 }
00910
00911 result = sqlite3_prepare( database, theSql.toUtf8(), theSql.toUtf8().length(), &stmt, &tail );
00912
00913 if ( result == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
00914 {
00915 QString srsIdString = QString::fromUtf8(( char * )sqlite3_column_text( stmt, 0 ) );
00916 srsId = srsIdString.toLong();
00917 }
00918
00919
00920 sqlite3_finalize( stmt );
00921 sqlite3_close( database );
00922
00923 return srsId;
00924 }
00925
00926 QStringList QgsProjectionSelector::authorities()
00927 {
00928 sqlite3 *database;
00929 const char *tail;
00930 sqlite3_stmt *stmt;
00931
00932 int result = sqlite3_open( mSrsDatabaseFileName.toUtf8().data(), &database );
00933 if ( result )
00934 {
00935 QgsDebugMsg( QString( "Can't open * user * database: %1" ).arg( sqlite3_errmsg( database ) ) );
00936
00937 return QStringList();
00938 }
00939
00940 QString theSql = "select distinct auth_name from tbl_srs";
00941 result = sqlite3_prepare( database, theSql.toUtf8(), theSql.toUtf8().length(), &stmt, &tail );
00942
00943 QStringList authorities;
00944 if ( result == SQLITE_OK )
00945 {
00946 while ( sqlite3_step( stmt ) == SQLITE_ROW )
00947 {
00948 authorities << QString::fromUtf8(( char * )sqlite3_column_text( stmt, 0 ) );
00949 }
00950
00951 }
00952
00953
00954 sqlite3_finalize( stmt );
00955 sqlite3_close( database );
00956
00957 return authorities;
00958 }
00959
00969 const QString QgsProjectionSelector::sqlSafeString( const QString theSQL )
00970 {
00971 QString retval = theSQL;
00972 retval.replace( "\\", "\\\\" );
00973 retval.replace( '\"', "\\\"" );
00974 retval.replace( "\'", "\\'" );
00975 retval.replace( "%", "\\%" );
00976 return retval;
00977 }
00978
00979 void QgsProjectionSelector::showDBMissingWarning( const QString theFileName )
00980 {
00981
00982 QMessageBox::critical( this, tr( "Resource Location Error" ),
00983 tr( "Error reading database file from: \n %1\n"
00984 "Because of this the projection selector will not work..." )
00985 .arg( theFileName ) );
00986 }