27 #include <QTextStream>
28 #include <QHeaderView>
29 #include <QResizeEvent>
30 #include <QMessageBox>
38 : QWidget( parent, fl )
39 , mProjListDone( false )
40 , mUserProjListDone( false )
41 , mRecentProjListDone( false )
42 , mCRSNameSelectionPending( false )
43 , mCRSIDSelectionPending( false )
44 , mAuthIDSelectionPending( false )
48 connect( lstCoordinateSystems, SIGNAL( currentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ),
53 lstCoordinateSystems->header()->setResizeMode(
AUTHID_COLUMN, QHeaderView::Stretch );
55 lstCoordinateSystems->header()->setResizeMode(
QGIS_CRS_ID_COLUMN, QHeaderView::Fixed );
57 lstRecent->header()->setResizeMode(
AUTHID_COLUMN, QHeaderView::Stretch );
61 cbxAuthority->addItem( tr(
"All" ) );
70 QStringList projectionsProj4 = settings.value(
"/UI/recentProjectionsProj4" ).toStringList();
71 QStringList projectionsAuthId = settings.value(
"/UI/recentProjectionsAuthId" ).toStringList();
76 QgsDebugMsg(
"Use popular projection list from AuthId/Proj4 saved state" );
78 for (
int i = 0; i < projectionsAuthId.size(); i++ )
120 QStringList projectionsProj4;
121 QStringList projectionsAuthId;
126 if ( ! crs.isValid() )
131 projectionsProj4 << crs.
toProj4();
132 projectionsAuthId << crs.authid();
134 settings.setValue(
"/UI/recentProjectionsProj4", projectionsProj4 );
135 settings.setValue(
"/UI/recentProjectionsAuthId", projectionsAuthId );
142 lstCoordinateSystems->header()->resizeSection(
NAME_COLUMN, theEvent->size().width() - 240 );
143 lstCoordinateSystems->header()->resizeSection(
AUTHID_COLUMN, 240 );
146 lstRecent->header()->resizeSection(
NAME_COLUMN, theEvent->size().width() - 240 );
181 QWidget::showEvent( theEvent );
186 QString sqlExpression =
"1";
187 QMap<QString, QStringList> authParts;
191 return sqlExpression;
212 foreach( QString auth_id, crsFilter->values() )
214 QStringList parts = auth_id.split(
":" );
216 if ( parts.size() < 2 )
219 authParts[ parts.at( 0 ).toUpper()].append( parts.at( 1 ).toUpper() );
222 if ( authParts.isEmpty() )
223 return sqlExpression;
225 if ( authParts.size() > 0 )
227 QString prefix =
" AND (";
228 foreach( QString auth_name, authParts.keys() )
230 sqlExpression += QString(
"%1(upper(auth_name)='%2' AND upper(auth_id) IN ('%3'))" )
233 .arg( authParts[auth_name].join(
"','" ) );
236 sqlExpression +=
")";
239 QgsDebugMsg(
"exiting with '" + sqlExpression +
"'." );
241 return sqlExpression;
296 QList<QTreeWidgetItem*> nodes;
301 nodes = lstCoordinateSystems->findItems(
mCRSNameSelection, Qt::MatchExactly | Qt::MatchRecursive, 0 );
319 nodes = lstCoordinateSystems->findItems( myCRSIDString, Qt::MatchExactly | Qt::MatchRecursive,
QGIS_CRS_ID_COLUMN );
324 if ( nodes.count() > 0 )
326 lstCoordinateSystems->setCurrentItem( nodes.first() );
327 lstCoordinateSystems->scrollToItem( lstCoordinateSystems->currentItem(), QAbstractItemView::PositionAtCenter );
331 lstCoordinateSystems->clearSelection();
332 teProjection->setText(
"" );
341 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( QString::number( theCrsId ), Qt::MatchExactly | Qt::MatchRecursive,
QGIS_CRS_ID_COLUMN );
343 if ( nodes.count() == 0 )
346 lstRecent->insertTopLevelItem( 0,
new QTreeWidgetItem( lstRecent, QStringList()
356 QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
359 return lvi->text( 0 );
363 return QString::null;
375 QTreeWidgetItem *myItem = lstCoordinateSystems->currentItem();
381 QString myDatabaseFileName;
393 QFileInfo myFileInfo;
394 myFileInfo.setFile( myDatabaseFileName );
395 if ( !myFileInfo.exists( ) )
398 return QString(
"" );
414 rc = sqlite3_open( myDatabaseFileName.toUtf8().data(), &db );
418 return QString(
"" );
422 sqlite3_stmt *ppStmt;
423 QString sql = QString(
"select parameters from tbl_srs where srs_id = %1" ).arg( mySrsId );
427 rc = sqlite3_prepare( db, sql.toUtf8(), sql.toUtf8().length(), &ppStmt, &pzTail );
429 QString myProjString;
430 if ( rc == SQLITE_OK )
432 if ( sqlite3_step( ppStmt ) == SQLITE_ROW )
434 myProjString = QString::fromUtf8((
char * )sqlite3_column_text( ppStmt, 0 ) );
438 sqlite3_finalize( ppStmt );
441 assert( myProjString.length() > 0 );
447 return QString(
"" );
453 return QString(
"" );
466 QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
472 QString myDatabaseFileName;
480 QFileInfo myFileInfo;
481 myFileInfo.setFile( myDatabaseFileName );
482 if ( !myFileInfo.exists( ) )
484 QgsDebugMsg(
" Projection selector : users qgis.db not found" );
499 rc = sqlite3_open( myDatabaseFileName.toUtf8().data(), &db );
507 sqlite3_stmt *ppStmt;
508 QString sql = QString(
"select %1 from tbl_srs where srs_id=%2" )
512 QgsDebugMsg( QString(
"Finding selected attribute using : %1" ).arg( sql ) );
513 rc = sqlite3_prepare( db, sql.toUtf8(), sql.toUtf8().length(), &ppStmt, &pzTail );
515 QString myAttributeValue;
516 if ( rc == SQLITE_OK )
519 if ( sqlite3_step( ppStmt ) == SQLITE_ROW )
522 myAttributeValue = QString::fromUtf8((
char * )sqlite3_column_text( ppStmt, 0 ) );
526 sqlite3_finalize( ppStmt );
530 return myAttributeValue;
561 return QString(
"USER:%1" ).arg( srid );
569 QTreeWidgetItem* item = lstCoordinateSystems->currentItem();
587 lstCoordinateSystems->clear();
600 mUserProjList =
new QTreeWidgetItem( lstCoordinateSystems, QStringList( tr(
"User Defined Coordinate Systems" ) ) );
603 fontTemp.setItalic(
true );
604 fontTemp.setBold(
true );
613 QFileInfo myFileInfo;
614 myFileInfo.setFile( myDatabaseFileName );
616 if ( !myFileInfo.exists( ) )
618 QgsDebugMsg(
"Users qgis.db not found...skipping" );
626 sqlite3_stmt *myPreparedStatement;
629 myResult = sqlite3_open( QString( myDatabaseFileName ).toUtf8().data(), &myDatabase );
641 QString mySql = QString(
"select description, srs_id from vw_srs where %1" ).arg( sqlFilter );
643 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
645 if ( myResult == SQLITE_OK )
647 QTreeWidgetItem *newItem;
648 while ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
650 newItem =
new QTreeWidgetItem(
mUserProjList, QStringList( QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) ) ) );
656 newItem->setText(
QGIS_CRS_ID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 1 ) ) );
660 sqlite3_finalize( myPreparedStatement );
661 sqlite3_close( myDatabase );
675 mGeoList =
new QTreeWidgetItem( lstCoordinateSystems, QStringList( tr(
"Geographic Coordinate Systems" ) ) );
677 QFont fontTemp =
mGeoList->font( 0 );
678 fontTemp.setItalic(
true );
679 fontTemp.setBold(
true );
684 mProjList =
new QTreeWidgetItem( lstCoordinateSystems, QStringList( tr(
"Projected Coordinate Systems" ) ) );
687 fontTemp.setItalic(
true );
688 fontTemp.setBold(
true );
697 QFileInfo myFileInfo;
699 if ( !myFileInfo.exists( ) )
718 sqlite3_stmt *ppStmt;
720 QString sql =
"select count(*) from tbl_srs";
722 rc = sqlite3_prepare( db, sql.toUtf8(), sql.toUtf8().length(), &ppStmt, &pzTail );
723 assert( rc == SQLITE_OK );
724 sqlite3_step( ppStmt );
726 sqlite3_finalize( ppStmt );
731 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" )
734 rc = sqlite3_prepare( db, sql.toUtf8(), sql.toUtf8().length(), &ppStmt, &pzTail );
736 if ( rc == SQLITE_OK )
738 QTreeWidgetItem *newItem;
741 QString previousSrsType(
"" );
742 QTreeWidgetItem* previousSrsTypeNode = NULL;
744 while ( sqlite3_step( ppStmt ) == SQLITE_ROW )
747 int isGeo = sqlite3_column_int( ppStmt, 3 );
752 newItem =
new QTreeWidgetItem(
mGeoList, QStringList( QString::fromUtf8((
char * )sqlite3_column_text( ppStmt, 0 ) ) ) );
755 newItem->setText(
AUTHID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( ppStmt, 2 ) ) );
758 newItem->setText(
QGIS_CRS_ID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( ppStmt, 1 ) ) );
763 QTreeWidgetItem *node;
764 QString srsType = QString::fromUtf8((
char* )sqlite3_column_text( ppStmt, 4 ) );
767 if ( srsType == previousSrsType )
769 node = previousSrsTypeNode;
773 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( srsType, Qt::MatchExactly | Qt::MatchRecursive, 0 );
774 if ( nodes.count() == 0 )
778 node =
new QTreeWidgetItem(
mProjList, QStringList( srsType ) );
780 QFont fontTemp = node->font( 0 );
781 fontTemp.setItalic(
true );
782 node->setFont( 0, fontTemp );
786 node = nodes.first();
789 previousSrsType = srsType;
790 previousSrsTypeNode = node;
793 newItem =
new QTreeWidgetItem( node, QStringList( QString::fromUtf8((
char * )sqlite3_column_text( ppStmt, 0 ) ) ) );
795 newItem->setText(
AUTHID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( ppStmt, 2 ) ) );
797 newItem->setText(
QGIS_CRS_ID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( ppStmt, 1 ) ) );
802 newItem->setData( 0, Qt::UserRole, QString::fromUtf8((
char * )sqlite3_column_text( ppStmt, 6 ) ) );
803 newItem->setHidden( cbxHideDeprecated->isChecked() );
808 sqlite3_finalize( ppStmt );
821 if ( theItem && theItem->childCount() == 0 )
824 QString myDescription;
827 lstCoordinateSystems->scrollToItem( theItem );
828 teProjection->setText( myProjString );
830 lstRecent->clearSelection();
836 theItem->setSelected(
false );
837 teProjection->setText(
"" );
843 if ( item->data( 0, Qt::UserRole ).toBool() )
845 item->setHidden( cbxHideDeprecated->isChecked() );
846 if ( item->isSelected() && item->isHidden() )
848 item->setSelected(
false );
849 teProjection->setText(
"" );
853 for (
int i = 0; i < item->childCount(); i++ )
859 for (
int i = 0; i < lstCoordinateSystems->topLevelItemCount(); i++ )
865 Q_UNUSED( previous );
877 QString mySql =
"select srs_id from tbl_srs where ";
878 if ( cbxAuthority->currentIndex() > 0 )
880 mySql += QString(
"auth_name='%1' AND " ).arg( cbxAuthority->currentText() );
883 if ( cbxHideDeprecated->isChecked() )
885 mySql +=
"not deprecated AND ";
888 if ( cbxMode->currentIndex() == 0 )
890 mySql += QString(
"auth_id='%1'" ).arg( mySearchString );
894 mySql +=
"upper(description) like '%" + mySearchString.toUpper() +
"%' ";
896 long myLargestSrsId =
getLargestCRSIDMatch( QString(
"%1 order by srs_id desc limit 1" ).arg( mySql ) );
897 QgsDebugMsg( QString(
"Largest CRSID%1" ).arg( myLargestSrsId ) );
903 mySql = QString(
"%1 order by srs_id limit 1" ).arg( mySql );
908 mySql = QString(
"%1 and srs_id > %2 order by srs_id limit 1" ).arg( mySql ).arg(
selectedCrsId() );
911 QgsDebugMsg( QString(
" Search sql: %1" ).arg( mySql ) );
919 sqlite3_stmt *myPreparedStatement;
933 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
935 if ( myResult == SQLITE_OK )
937 myResult = sqlite3_step( myPreparedStatement );
938 if ( myResult == SQLITE_ROW )
940 QString mySrsId = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) );
943 sqlite3_finalize( myPreparedStatement );
944 sqlite3_close( myDatabase );
950 QFileInfo myFileInfo;
951 myFileInfo.setFile( myDatabaseFileName );
952 if ( !myFileInfo.exists( ) )
954 QgsDebugMsg( QString(
"%1\nUser db does not exist" ).arg( myDatabaseFileName ) );
957 myResult = sqlite3_open( myDatabaseFileName.toUtf8().data(), &myDatabase );
960 QgsDebugMsg( QString(
"Can't open * user * database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
965 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
967 if ( myResult == SQLITE_OK )
969 myResult = sqlite3_step( myPreparedStatement );
970 if ( myResult == SQLITE_ROW )
972 QString mySrsId = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) );
975 sqlite3_finalize( myPreparedStatement );
976 sqlite3_close( myDatabase );
981 QMessageBox::information(
this, tr(
"Find projection" ), tr(
"No matching projection found." ) );
982 lstCoordinateSystems->clearSelection();
983 teProjection->setText(
"" );
995 sqlite3_stmt *myPreparedStatement;
1002 QFileInfo myFileInfo;
1003 myFileInfo.setFile( myDatabaseFileName );
1004 if ( myFileInfo.exists( ) )
1006 myResult = sqlite3_open( myDatabaseFileName.toUtf8().data(), &myDatabase );
1018 myResult = sqlite3_prepare( myDatabase, theSql.toUtf8(), theSql.toUtf8().length(), &myPreparedStatement, &myTail );
1020 if ( myResult == SQLITE_OK )
1022 myResult = sqlite3_step( myPreparedStatement );
1023 if ( myResult == SQLITE_ROW )
1025 QString mySrsIdString = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) );
1026 mySrsId = mySrsIdString.toLong();
1028 sqlite3_finalize( myPreparedStatement );
1029 sqlite3_close( myDatabase );
1041 QgsDebugMsg( QString(
"Can't open * user * database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
1046 myResult = sqlite3_prepare( myDatabase, theSql.toUtf8(), theSql.toUtf8().length(), &myPreparedStatement, &myTail );
1048 if ( myResult == SQLITE_OK )
1050 myResult = sqlite3_step( myPreparedStatement );
1051 if ( myResult == SQLITE_ROW )
1053 QString mySrsIdString = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) );
1054 mySrsId = mySrsIdString.toLong();
1056 sqlite3_finalize( myPreparedStatement );
1057 sqlite3_close( myDatabase );
1067 sqlite3_stmt *myPreparedStatement;
1073 QgsDebugMsg( QString(
"Can't open * user * database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
1075 return QStringList();
1078 QString theSql =
"select distinct auth_name from tbl_srs";
1079 myResult = sqlite3_prepare( myDatabase, theSql.toUtf8(), theSql.toUtf8().length(), &myPreparedStatement, &myTail );
1083 if ( myResult == SQLITE_OK )
1085 while ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
1087 authorities << QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) );
1091 sqlite3_finalize( myPreparedStatement );
1092 sqlite3_close( myDatabase );
1109 QString myRetval = theSQL;
1110 myRetval.replace(
"\\",
"\\\\" );
1111 myRetval.replace(
'\"',
"\\\"" );
1112 myRetval.replace(
"\'",
"\\'" );
1113 myRetval.replace(
"%",
"\\%" );
1120 QMessageBox::critical(
this, tr(
"Resource Location Error" ),
1121 tr(
"Error reading database file from: \n %1\n"
1122 "Because of this the projection selector will not work..." )
1123 .arg( theFileName ) );