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 , mPushProjectionToFront( false )
40 if ( qobject_cast<QDialog*>( parent ) )
49 lstCoordinateSystems->header()->setResizeMode(
AUTHID_COLUMN, QHeaderView::Stretch );
51 lstCoordinateSystems->header()->setResizeMode(
QGIS_CRS_ID_COLUMN, QHeaderView::Fixed );
56 lstRecent->header()->setResizeMode(
AUTHID_COLUMN, QHeaderView::Stretch );
69 QStringList projectionsProj4 = settings.value(
"/UI/recentProjectionsProj4" ).toStringList();
70 QStringList projectionsAuthId = settings.value(
"/UI/recentProjectionsAuthId" ).toStringList();
75 QgsDebugMsg(
"Use popular projection list from AuthId/Proj4 saved state" );
77 for (
int i = 0; i < projectionsAuthId.size(); i++ )
85 if ( i >= projectionsProj4.size() || !crs.
createFromProj4( projectionsProj4.at( i ) ) )
91 if ( crs.
srsid() == 0 )
128 QStringList projectionsProj4;
129 QStringList projectionsAuthId;
134 if ( ! crs.isValid() )
139 projectionsProj4 << crs.
toProj4();
140 projectionsAuthId << crs.authid();
142 settings.setValue(
"/UI/recentProjectionsProj4", projectionsProj4 );
143 settings.setValue(
"/UI/recentProjectionsAuthId", projectionsAuthId );
148 lstCoordinateSystems->header()->resizeSection(
NAME_COLUMN, theEvent->size().width() - 240 );
149 lstCoordinateSystems->header()->resizeSection(
AUTHID_COLUMN, 240 );
152 lstRecent->header()->resizeSection(
NAME_COLUMN, theEvent->size().width() - 240 );
177 QWidget::showEvent( theEvent );
182 QString sqlExpression =
"1";
183 QMap<QString, QStringList> authParts;
186 return sqlExpression;
206 foreach ( QString auth_id, crsFilter->values() )
208 QStringList parts = auth_id.split(
":" );
210 if ( parts.size() < 2 )
213 authParts[ parts.at( 0 ).toUpper()].append( parts.at( 1 ).toUpper() );
216 if ( authParts.isEmpty() )
217 return sqlExpression;
219 if ( authParts.size() > 0 )
221 QString prefix =
" AND (";
222 foreach ( QString auth_name, authParts.keys() )
224 sqlExpression += QString(
"%1(upper(auth_name)='%2' AND upper(auth_id) IN ('%3'))" )
227 .arg( authParts[auth_name].join(
"','" ) );
230 sqlExpression +=
")";
233 QgsDebugMsg(
"exiting with '" + sqlExpression +
"'." );
235 return sqlExpression;
263 if ( column ==
NONE )
273 if ( column ==
NONE )
276 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( value, Qt::MatchExactly | Qt::MatchRecursive, column );
277 if ( nodes.count() > 0 )
279 QgsDebugMsg( QString(
"found %1,%2" ).arg( column ).arg( value ) );
280 lstCoordinateSystems->setCurrentItem( nodes.first() );
284 QgsDebugMsg( QString(
"nothing found for %1,%2" ).arg( column ).arg( value ) );
286 lstCoordinateSystems->clearSelection();
287 lstRecent->clearSelection();
288 teProjection->setText(
"" );
289 teSelected->setText(
"" );
298 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( QString::number( theCrsId ), Qt::MatchExactly | Qt::MatchRecursive,
QGIS_CRS_ID_COLUMN );
299 if ( nodes.count() == 0 )
302 lstRecent->insertTopLevelItem( 0,
new QTreeWidgetItem( lstRecent, QStringList()
312 QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
313 return lvi ? lvi->text(
NAME_COLUMN ) : QString::null;
325 QTreeWidgetItem *item = lstCoordinateSystems->currentItem();
338 QString databaseFileName;
342 if ( !QFileInfo( databaseFileName ).exists() )
343 return QString(
"" );
353 int rc = sqlite3_open_v2( databaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
363 QString sql = QString(
"select parameters from tbl_srs where srs_id=%1" ).arg( srsId );
367 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
370 if ( rc == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
372 projString = QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
376 sqlite3_finalize( stmt );
378 sqlite3_close( database );
380 Q_ASSERT( !projString.isEmpty() );
394 QTreeWidgetItem *lvi = lstCoordinateSystems->currentItem();
402 QString databaseFileName;
406 if ( !QFileInfo( databaseFileName ).exists() )
422 int rc = sqlite3_open_v2( databaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
432 QString sql = QString(
"select %1 from tbl_srs where srs_id=%2" )
436 QgsDebugMsg( QString(
"Finding selected attribute using : %1" ).arg( sql ) );
437 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
439 QString attributeValue;
440 if ( rc == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
443 attributeValue = QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
447 sqlite3_finalize( stmt );
449 sqlite3_close( database );
452 return attributeValue;
466 return QString(
"USER:%1" ).arg( srid );
474 QTreeWidgetItem* item = lstCoordinateSystems->currentItem();
488 lstCoordinateSystems->clear();
503 mUserProjList =
new QTreeWidgetItem( lstCoordinateSystems, QStringList(
tr(
"User Defined Coordinate Systems" ) ) );
506 fontTemp.setItalic(
true );
507 fontTemp.setBold(
true );
518 if ( !QFileInfo( databaseFileName ).exists( ) )
520 QgsDebugMsg(
"Users qgis.db not found...skipping" );
529 int result = sqlite3_open_v2( databaseFileName.toUtf8().constData(), &database, SQLITE_OPEN_READONLY, NULL );
541 QString sql = QString(
"select description, srs_id from vw_srs where %1" ).arg( sqlFilter );
543 result = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
545 if ( result == SQLITE_OK )
547 QTreeWidgetItem *newItem;
548 while ( sqlite3_step( stmt ) == SQLITE_ROW )
550 newItem =
new QTreeWidgetItem(
mUserProjList, QStringList( QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) ) ) );
556 newItem->setText(
QGIS_CRS_ID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 1 ) ) );
557 newItem->setText(
AUTHID_COLUMN, QString(
"USER:%1" ).arg( QString::fromUtf8((
char * )sqlite3_column_text( stmt, 1 ) ).toInt() ) );
561 sqlite3_finalize( stmt );
562 sqlite3_close( database );
579 mGeoList =
new QTreeWidgetItem( lstCoordinateSystems, QStringList(
tr(
"Geographic Coordinate Systems" ) ) );
581 QFont fontTemp =
mGeoList->font( 0 );
582 fontTemp.setItalic(
true );
583 fontTemp.setBold(
true );
588 mProjList =
new QTreeWidgetItem( lstCoordinateSystems, QStringList(
tr(
"Projected Coordinate Systems" ) ) );
591 fontTemp.setItalic(
true );
592 fontTemp.setBold(
true );
609 int rc = sqlite3_open_v2(
mSrsDatabaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
621 QString sql =
"select count(*) from tbl_srs";
623 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
624 Q_ASSERT( rc == SQLITE_OK );
625 sqlite3_step( stmt );
626 sqlite3_finalize( stmt );
631 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" )
634 rc = sqlite3_prepare( database, sql.toUtf8(), sql.toUtf8().length(), &stmt, &tail );
636 if ( rc == SQLITE_OK )
638 QTreeWidgetItem *newItem;
641 QString previousSrsType(
"" );
642 QTreeWidgetItem* previousSrsTypeNode = 0;
644 while ( sqlite3_step( stmt ) == SQLITE_ROW )
647 int isGeo = sqlite3_column_int( stmt, 3 );
652 newItem =
new QTreeWidgetItem(
mGeoList, QStringList( QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) ) ) );
655 newItem->setText(
AUTHID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 2 ) ) );
658 newItem->setText(
QGIS_CRS_ID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 1 ) ) );
663 QTreeWidgetItem *node;
664 QString srsType = QString::fromUtf8((
char* )sqlite3_column_text( stmt, 4 ) );
667 if ( srsType == previousSrsType )
669 node = previousSrsTypeNode;
673 QList<QTreeWidgetItem*> nodes = lstCoordinateSystems->findItems( srsType, Qt::MatchExactly | Qt::MatchRecursive,
NAME_COLUMN );
674 if ( nodes.count() == 0 )
678 node =
new QTreeWidgetItem(
mProjList, QStringList( srsType ) );
679 QFont fontTemp = node->font( 0 );
680 fontTemp.setItalic(
true );
681 node->setFont( 0, fontTemp );
685 node = nodes.first();
688 previousSrsType = srsType;
689 previousSrsTypeNode = node;
692 newItem =
new QTreeWidgetItem( node, QStringList( QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) ) ) );
694 newItem->setText(
AUTHID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 2 ) ) );
696 newItem->setText(
QGIS_CRS_ID_COLUMN, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 1 ) ) );
698 newItem->parent()->setExpanded(
true );
702 newItem->setData( 0, Qt::UserRole, QString::fromUtf8((
char * )sqlite3_column_text( stmt, 6 ) ) );
703 newItem->setHidden( cbxHideDeprecated->isChecked() );
709 sqlite3_finalize( stmt );
711 sqlite3_close( database );
727 lstCoordinateSystems->scrollToItem( current );
731 if ( current->childCount() == 0 )
740 if ( nodes.count() > 0 )
743 lstRecent->setCurrentItem( nodes.first() );
748 lstRecent->clearSelection();
749 lstCoordinateSystems->setFocus( Qt::OtherFocusReason );
755 current->setSelected(
false );
756 teProjection->setText(
"" );
757 teSelected->setText(
"" );
758 lstRecent->clearSelection();
772 lstRecent->scrollToItem( current );
775 if ( nodes.count() > 0 )
776 lstCoordinateSystems->setCurrentItem( nodes.first() );
781 if ( item->data( 0, Qt::UserRole ).toBool() )
783 item->setHidden( cbxHideDeprecated->isChecked() );
784 if ( item->isSelected() && item->isHidden() )
786 item->setSelected(
false );
787 teProjection->setText(
"" );
788 teSelected->setText(
"" );
792 for (
int i = 0; i < item->childCount(); i++ )
798 for (
int i = 0; i < lstCoordinateSystems->topLevelItemCount(); i++ )
804 QString filterTxt = theFilterTxt;
805 filterTxt.replace( QRegExp(
"\\s+" ),
".*" );
806 QRegExp re( filterTxt, Qt::CaseInsensitive );
809 QTreeWidgetItemIterator itr( lstRecent );
812 if (( *itr )->childCount() == 0 )
818 ( *itr )->setHidden(
false );
819 QTreeWidgetItem * parent = ( *itr )->parent();
822 parent->setExpanded(
true );
823 parent->setHidden(
false );
824 parent = parent->parent();
829 ( *itr )->setHidden(
true );
834 ( *itr )->setHidden(
true );
840 QTreeWidgetItemIterator it( lstCoordinateSystems );
843 if (( *it )->childCount() == 0 )
849 ( *it )->setHidden(
false );
850 QTreeWidgetItem * parent = ( *it )->parent();
853 parent->setExpanded(
true );
854 parent->setHidden(
false );
855 parent = parent->parent();
860 ( *it )->setHidden(
true );
865 ( *it )->setHidden(
true );
896 if ( QFileInfo( databaseFileName ).exists() )
898 result = sqlite3_open_v2( databaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
909 result = sqlite3_prepare( database, theSql.toUtf8(), theSql.toUtf8().length(), &stmt, &tail );
911 if ( result == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
913 QString srsIdString = QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
914 srsId = srsIdString.toLong();
916 sqlite3_finalize( stmt );
917 sqlite3_close( database );
924 result = sqlite3_open_v2(
mSrsDatabaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
927 QgsDebugMsg( QString(
"Can't open * user * database: %1" ).arg( sqlite3_errmsg( database ) ) );
933 result = sqlite3_prepare( database, theSql.toUtf8(), theSql.toUtf8().length(), &stmt, &tail );
935 if ( result == SQLITE_OK && sqlite3_step( stmt ) == SQLITE_ROW )
937 QString srsIdString = QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
938 srsId = srsIdString.toLong();
942 sqlite3_finalize( stmt );
943 sqlite3_close( database );
954 int result = sqlite3_open_v2(
mSrsDatabaseFileName.toUtf8().data(), &database, SQLITE_OPEN_READONLY, NULL );
957 QgsDebugMsg( QString(
"Can't open * user * database: %1" ).arg( sqlite3_errmsg( database ) ) );
959 return QStringList();
962 QString theSql =
"select distinct auth_name from tbl_srs";
963 result = sqlite3_prepare( database, theSql.toUtf8(), theSql.toUtf8().length(), &stmt, &tail );
966 if ( result == SQLITE_OK )
968 while ( sqlite3_step( stmt ) == SQLITE_ROW )
970 authorities << QString::fromUtf8((
char * )sqlite3_column_text( stmt, 0 ) );
976 sqlite3_finalize( stmt );
977 sqlite3_close( database );
993 QString retval = theSQL;
994 retval.replace(
"\\",
"\\\\" );
995 retval.replace(
'\"',
"\\\"" );
996 retval.replace(
"\'",
"\\'" );
997 retval.replace(
"%",
"\\%" );
1004 QMessageBox::critical(
this,
tr(
"Resource Location Error" ),
1005 tr(
"Error reading database file from: \n %1\n"
1006 "Because of this the projection selector will not work..." )
1007 .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
QgsProjectionSelector(QWidget *parent, const char *name="", Qt::WindowFlags fl=0)
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...
bool mPushProjectionToFront
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 pushProjectionToFront()
mark selected projection for push to front
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...
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 initialized()
Notify others that the widget is now fully initialized, including deferred selection of projection...
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.