24 #include <QHeaderView>
25 #include <QResizeEvent>
26 #include <QMessageBox>
30 : QWidget( parent, fl )
31 , mProjListDone( false )
32 , mUserProjListDone( false )
33 , mRecentProjListDone( false )
34 , mSearchColumn( NONE )
35 , mSkipFirstRecent( true )
43 lstCoordinateSystems->header()->setResizeMode(
AUTHID_COLUMN, QHeaderView::Stretch );
45 lstCoordinateSystems->header()->setResizeMode(
QGIS_CRS_ID_COLUMN, QHeaderView::Fixed );
50 lstRecent->header()->setResizeMode(
AUTHID_COLUMN, QHeaderView::Stretch );
63 QStringList projectionsProj4 = settings.value(
"/UI/recentProjectionsProj4" ).toStringList();
64 QStringList projectionsAuthId = settings.value(
"/UI/recentProjectionsAuthId" ).toStringList();
69 QgsDebugMsg(
"Use popular projection list from AuthId/Proj4 saved state" );
71 for (
int i = 0; i < projectionsAuthId.size(); i++ )
79 if ( i >= projectionsProj4.size() || !crs.
createFromProj4( projectionsProj4.at( i ) ) )
85 if ( crs.
srsid() == 0 )
117 QStringList projectionsProj4;
118 QStringList projectionsAuthId;
123 if ( ! crs.isValid() )
128 projectionsProj4 << crs.
toProj4();
129 projectionsAuthId << crs.authid();
131 settings.setValue(
"/UI/recentProjectionsProj4", projectionsProj4 );
132 settings.setValue(
"/UI/recentProjectionsAuthId", projectionsAuthId );
137 lstCoordinateSystems->header()->resizeSection(
NAME_COLUMN, theEvent->size().width() - 240 );
138 lstCoordinateSystems->header()->resizeSection(
AUTHID_COLUMN, 240 );
141 lstRecent->header()->resizeSection(
NAME_COLUMN, theEvent->size().width() - 240 );
164 QWidget::showEvent( theEvent );
169 QString sqlExpression =
"1";
170 QMap<QString, QStringList> authParts;
173 return sqlExpression;
193 foreach ( QString auth_id, crsFilter->values() )
195 QStringList parts = auth_id.split(
":" );
197 if ( parts.size() < 2 )
200 authParts[ parts.at( 0 ).toUpper()].append( parts.at( 1 ).toUpper() );
203 if ( authParts.isEmpty() )
204 return sqlExpression;
206 if ( authParts.size() > 0 )
208 QString prefix =
" AND (";
209 foreach ( QString auth_name, authParts.keys() )
211 sqlExpression += QString(
"%1(upper(auth_name)='%2' AND upper(auth_id) IN ('%3'))" )
214 .arg( authParts[auth_name].join(
"','" ) );
217 sqlExpression +=
")";
220 QgsDebugMsg(
"exiting with '" + sqlExpression +
"'." );
222 return sqlExpression;
250 if ( column ==
NONE )
260 if ( column ==
NONE )
263 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( value, Qt::MatchExactly | Qt::MatchRecursive, column );
264 if ( nodes.count() > 0 )
266 QgsDebugMsg( QString(
"found %1,%2" ).arg( column ).arg( value ) );
267 lstCoordinateSystems->setCurrentItem( nodes.first() );
271 QgsDebugMsg( QString(
"nothing found for %1,%2" ).arg( column ).arg( value ) );
273 lstCoordinateSystems->clearSelection();
274 lstRecent->clearSelection();
275 teProjection->setText(
"" );
276 teSelected->setText(
"" );
285 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( QString::number( theCrsId ), Qt::MatchExactly | Qt::MatchRecursive,
QGIS_CRS_ID_COLUMN );
286 if ( nodes.count() == 0 )
289 lstRecent->insertTopLevelItem( 0,
new QTreeWidgetItem( lstRecent, QStringList()
299 QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
300 return lvi ? lvi->text(
NAME_COLUMN ) : QString::null;
312 QTreeWidgetItem *item = lstCoordinateSystems->currentItem();
325 QString databaseFileName;
329 if ( !QFileInfo( databaseFileName ).exists() )
330 return QString(
"" );
340 int rc = sqlite3_open_v2( databaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
350 QString sql = QString(
"select parameters from tbl_srs where srs_id=%1" ).arg( srsId );
354 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
357 if ( rc == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
359 projString = QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
363 sqlite3_finalize( stmt );
365 sqlite3_close( database );
367 Q_ASSERT( !projString.isEmpty() );
381 QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
389 QString databaseFileName;
393 if ( !QFileInfo( databaseFileName ).exists() )
409 int rc = sqlite3_open_v2( databaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
419 QString sql = QString(
"select %1 from tbl_srs where srs_id=%2" )
423 QgsDebugMsg( QString(
"Finding selected attribute using : %1" ).arg( sql ) );
424 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
426 QString attributeValue;
427 if ( rc == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
430 attributeValue = QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
434 sqlite3_finalize( stmt );
436 sqlite3_close( database );
439 return attributeValue;
453 return QString(
"USER:%1" ).arg( srid );
461 QTreeWidgetItem* item = lstCoordinateSystems->currentItem();
475 lstCoordinateSystems->clear();
490 mUserProjList =
new QTreeWidgetItem( lstCoordinateSystems, QStringList(
tr(
"User Defined Coordinate Systems" ) ) );
493 fontTemp.setItalic(
true );
494 fontTemp.setBold(
true );
505 if ( !QFileInfo( databaseFileName ).exists( ) )
507 QgsDebugMsg(
"Users qgis.db not found...skipping" );
516 int result = sqlite3_open_v2( databaseFileName.toUtf8().constData(), &database, SQLITE_OPEN_READONLY, NULL );
528 QString sql = QString(
"select description, srs_id from vw_srs where %1" ).arg( sqlFilter );
530 result = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
532 if ( result == SQLITE_OK )
534 QTreeWidgetItem *newItem;
535 while ( sqlite3_step( stmt ) == SQLITE_ROW )
537 newItem =
new QTreeWidgetItem(
mUserProjList, QStringList( QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) ) ) );
543 newItem->setText(
QGIS_CRS_ID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 1 ) ) );
544 newItem->setText(
AUTHID_COLUMN, QString(
"USER:%1" ).arg( QString::fromUtf8((
char * )sqlite3_column_text( stmt, 1 ) ).toInt() ) );
548 sqlite3_finalize( stmt );
549 sqlite3_close( database );
566 mGeoList =
new QTreeWidgetItem( lstCoordinateSystems, QStringList(
tr(
"Geographic Coordinate Systems" ) ) );
568 QFont fontTemp =
mGeoList->font( 0 );
569 fontTemp.setItalic(
true );
570 fontTemp.setBold(
true );
575 mProjList =
new QTreeWidgetItem( lstCoordinateSystems, QStringList(
tr(
"Projected Coordinate Systems" ) ) );
578 fontTemp.setItalic(
true );
579 fontTemp.setBold(
true );
596 int rc = sqlite3_open_v2(
mSrsDatabaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
608 QString sql =
"select count(*) from tbl_srs";
610 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
611 Q_ASSERT( rc == SQLITE_OK );
612 sqlite3_step( stmt );
613 sqlite3_finalize( stmt );
618 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" )
621 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
623 if ( rc == SQLITE_OK )
625 QTreeWidgetItem *newItem;
628 QString previousSrsType(
"" );
629 QTreeWidgetItem* previousSrsTypeNode = 0;
631 while ( sqlite3_step( stmt ) == SQLITE_ROW )
634 int isGeo = sqlite3_column_int( stmt, 3 );
639 newItem =
new QTreeWidgetItem(
mGeoList, QStringList( QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) ) ) );
642 newItem->setText(
AUTHID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 2 ) ) );
645 newItem->setText(
QGIS_CRS_ID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 1 ) ) );
650 QTreeWidgetItem *node;
651 QString srsType = QString::fromUtf8((
char* )sqlite3_column_text( stmt, 4 ) );
654 if ( srsType == previousSrsType )
656 node = previousSrsTypeNode;
660 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( srsType, Qt::MatchExactly | Qt::MatchRecursive,
NAME_COLUMN );
661 if ( nodes.count() == 0 )
665 node =
new QTreeWidgetItem(
mProjList, QStringList( srsType ) );
666 QFont fontTemp = node->font( 0 );
667 fontTemp.setItalic(
true );
668 node->setFont( 0, fontTemp );
672 node = nodes.first();
675 previousSrsType = srsType;
676 previousSrsTypeNode = node;
679 newItem =
new QTreeWidgetItem( node, QStringList( QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) ) ) );
681 newItem->setText(
AUTHID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 2 ) ) );
683 newItem->setText(
QGIS_CRS_ID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 1 ) ) );
685 newItem->parent()->setExpanded(
true );
689 newItem->setData( 0, Qt::UserRole, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 6 ) ) );
690 newItem->setHidden( cbxHideDeprecated->isChecked() );
696 sqlite3_finalize( stmt );
698 sqlite3_close( database );
714 lstCoordinateSystems->scrollToItem( current );
718 if ( current->childCount() == 0 )
727 if ( nodes.count() > 0 )
730 lstRecent->setCurrentItem( nodes.first() );
735 lstRecent->clearSelection();
736 lstCoordinateSystems->setFocus( Qt::OtherFocusReason );
742 current->setSelected(
false );
743 teProjection->setText(
"" );
744 teSelected->setText(
"" );
745 lstRecent->clearSelection();
765 lstRecent->scrollToItem( current );
768 if ( nodes.count() > 0 )
769 lstCoordinateSystems->setCurrentItem( nodes.first() );
774 if ( item->data( 0, Qt::UserRole ).toBool() )
776 item->setHidden( cbxHideDeprecated->isChecked() );
777 if ( item->isSelected() && item->isHidden() )
779 item->setSelected(
false );
780 teProjection->setText(
"" );
781 teSelected->setText(
"" );
785 for (
int i = 0; i < item->childCount(); i++ )
791 for (
int i = 0; i < lstCoordinateSystems->topLevelItemCount(); i++ )
797 QString filterTxt = theFilterTxt;
798 filterTxt.replace( QRegExp(
"\\s+" ),
".*" );
799 QRegExp re( filterTxt, Qt::CaseInsensitive );
802 QTreeWidgetItemIterator itr( lstRecent );
805 if (( *itr )->childCount() == 0 )
811 ( *itr )->setHidden(
false );
812 QTreeWidgetItem * parent = ( *itr )->parent();
815 parent->setExpanded(
true );
816 parent->setHidden(
false );
817 parent = parent->parent();
822 ( *itr )->setHidden(
true );
827 ( *itr )->setHidden(
true );
833 QTreeWidgetItemIterator it( lstCoordinateSystems );
836 if (( *it )->childCount() == 0 )
842 ( *it )->setHidden(
false );
843 QTreeWidgetItem * parent = ( *it )->parent();
846 parent->setExpanded(
true );
847 parent->setHidden(
false );
848 parent = parent->parent();
853 ( *it )->setHidden(
true );
858 ( *it )->setHidden(
true );
882 if ( QFileInfo( databaseFileName ).exists() )
884 result = sqlite3_open_v2( databaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
895 result = sqlite3_prepare( database, theSql.toUtf8(), theSql.toUtf8().length(), &stmt, &tail );
897 if ( result == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
899 QString srsIdString = QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
900 srsId = srsIdString.toLong();
902 sqlite3_finalize( stmt );
903 sqlite3_close( database );
910 result = sqlite3_open_v2(
mSrsDatabaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
913 QgsDebugMsg( QString(
"Can't open * user * database: %1" ).arg( sqlite3_errmsg( database ) ) );
919 result = sqlite3_prepare( database, theSql.toUtf8(), theSql.toUtf8().length(), &stmt, &tail );
921 if ( result == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
923 QString srsIdString = QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
924 srsId = srsIdString.toLong();
928 sqlite3_finalize( stmt );
929 sqlite3_close( database );
940 int result = sqlite3_open_v2(
mSrsDatabaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
943 QgsDebugMsg( QString(
"Can't open * user * database: %1" ).arg( sqlite3_errmsg( database ) ) );
945 return QStringList();
948 QString theSql =
"select distinct auth_name from tbl_srs";
949 result = sqlite3_prepare( database, theSql.toUtf8(), theSql.toUtf8().length(), &stmt, &tail );
952 if ( result == SQLITE_OK )
954 while ( sqlite3_step( stmt ) == SQLITE_ROW )
956 authorities << QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
962 sqlite3_finalize( stmt );
963 sqlite3_close( database );
979 QString retval = theSQL;
980 retval.replace(
"\\",
"\\\\" );
981 retval.replace(
'\"',
"\\\"" );
982 retval.replace(
"\'",
"\\'" );
983 retval.replace(
"%",
"\\%" );
990 QMessageBox::critical(
this,
tr(
"Resource Location Error" ),
991 tr(
"Error reading database file from: \n %1\n"
992 "Because of this the projection selector will not work..." )
993 .arg( theFileName ) );
QTreeWidgetItem * mGeoList
GEOGCS node.
const QString sqlSafeString(const QString theSQL)
Make the string safe for use in SQL statements. This involves escaping single quotes, double quotes, backslashes, and optionally, percentage symbols. Percentage symbols are used as wildcards sometimes and so when using the string as part of the LIKE phrase of a select statement, should be escaped.
bool mUserProjListDone
Has the User Projection List been populated?
void showEvent(QShowEvent *theEvent)
Used to ensure the projection list view is actually populated.
static const QString activeThemePath()
Returns the path to the currently active theme directory.
void setOgcWmsCrsFilter(QSet< QString > crsFilter)
filters this widget by the given CRSs
void applySelection(int column=NONE, QString value=QString::null)
does the legwork of applying CRS Selection
void setSelectedCrsId(long theCRSID)
QSet< QString > mCrsFilter
The set of OGC WMS CRSs that want to be applied to this widget.
QString selectedProj4String()
QString ogcWmsCrsFilterAsSqlExpression(QSet< QString > *crsFilter)
converts the CRS group to a SQL expression fragment
long selectedPostgresSrId()
Gets the current PostGIS-style projection identifier.
void loadUserCrsList(QSet< QString > *crsFilter=0)
Populate the proj tree view with user defined projection names...
QStringList mRecentProjections
Most recently used projections (trimmed at 25 entries)
void setSelectedCrsName(QString theCRSName)
long getLargestCRSIDMatch(QString theSql)
Utility method used in conjunction with name based searching tool.
void on_lstRecent_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *prev)
bool createFromOgcWmsCrs(QString theCrs)
Set up this CRS from the given OGC CRS.
QString selectedAuthId()
Gets the current authority-style projection identifier.
QTreeWidgetItem * mUserProjList
User defined projections node.
QString getSelectedExpression(QString e)
gets an arbitrary sqlite3 expression from the selection
void setSelectedAuthId(QString authId)
QString mSrsDatabaseFileName
File name of the sqlite3 database.
void on_cbxHideDeprecated_stateChanged()
void on_lstCoordinateSystems_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *prev)
QStringList authorities()
get list of authorities
void sridSelected(QString theSRID)
void on_leSearch_textChanged(const QString &)
void resizeEvent(QResizeEvent *theEvent)
Used to manage column sizes.
bool mRecentProjListDone
Has the Recent Projection List been populated?
const int USER_CRS_START_ID
Magick number that determines whether a projection crsid is a system (srs.db) or user (~/...
void loadCrsList(QSet< QString > *crsFilter=0)
Populate the proj tree view with system projection names...
QgsProjectionSelector(QWidget *parent, const char *name="", Qt::WFlags fl=0)
void hideDeprecated(QTreeWidgetItem *item)
hide deprecated CRSes
Class for storing a coordinate reference system (CRS)
static const QString srsDbFilePath()
Returns the path to the srs.db file.
void showDBMissingWarning(const QString theFileName)
Show the user a warning if the srs database could not be found.
QTreeWidgetItem * mProjList
PROJCS node.
void insertRecent(long theCrsId)
add recently used CRS
static const QString qgisUserDbFilePath()
Returns the path to the user qgis.db file.
bool mProjListDone
Has the Projection List been populated?
bool createFromProj4(const QString &theProjString)
QString toProj4() const
Get the Proj Proj4 string representation of this srs.
long selectedCrsId()
Gets the current QGIS projection identfier.