Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Show recently used projections in QgsProjectionSelectionWidget
  • Loading branch information
nyalldawson committed Jan 19, 2015
1 parent 953b39e commit 1310b87
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 45 deletions.
6 changes: 6 additions & 0 deletions python/core/qgscoordinatereferencesystem.sip
Expand Up @@ -304,4 +304,10 @@ class QgsCoordinateReferenceSystem

/**Returns auth id of related geographic CRS*/
QString geographicCRSAuthId() const;

/**Returns a list of recently used projections
* @returns list of srsid for recently used projections
* @note added in QGIS 2.7
*/
static QStringList recentProjections();
};
3 changes: 2 additions & 1 deletion python/gui/qgsprojectionselectionwidget.sip
Expand Up @@ -19,7 +19,8 @@ class QgsProjectionSelectionWidget : QWidget
LayerCrs, /*< optional layer CRS */
ProjectCrs, /*< current project CRS (if OTF reprojection enabled) */
CurrentCrs, /*< current user selected CRS */
DefaultCrs /*< global default QGIS CRS */
DefaultCrs, /*< global default QGIS CRS */
RecentCrs /*< recently used CRS */
};

explicit QgsProjectionSelectionWidget( QWidget *parent /TransferThis/ = 0 );
Expand Down
42 changes: 42 additions & 0 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -2167,3 +2167,45 @@ QString QgsCoordinateReferenceSystem::geographicCRSAuthId() const
return "";
}
}

QStringList QgsCoordinateReferenceSystem::recentProjections()
{
QStringList projections;

// Read settings from persistent storage
QSettings settings;
projections = settings.value( "/UI/recentProjections" ).toStringList();
/*** The reading (above) of internal id from persistent storage should be removed sometime in the future */
/*** This is kept now for backwards compatibility */

QStringList projectionsProj4 = settings.value( "/UI/recentProjectionsProj4" ).toStringList();
QStringList projectionsAuthId = settings.value( "/UI/recentProjectionsAuthId" ).toStringList();
if ( projectionsAuthId.size() >= projections.size() )
{
// We had saved state with AuthId and Proj4. Use that instead
// to find out the crs id
projections.clear();
for ( int i = 0; i < projectionsAuthId.size(); i++ )
{
// Create a crs from the EPSG
QgsCoordinateReferenceSystem crs;
crs.createFromOgcWmsCrs( projectionsAuthId.at( i ) );
if ( ! crs.isValid() )
{
// Couldn't create from EPSG, try the Proj4 string instead
if ( i >= projectionsProj4.size() || !crs.createFromProj4( projectionsProj4.at( i ) ) )
{
// No? Skip this entry
continue;
}
//If the CRS can be created but do not correspond to a CRS in the database, skip it (for example a deleted custom CRS)
if ( crs.srsid() == 0 )
{
continue;
}
}
projections << QString::number( crs.srsid() );
}
}
return projections;
}
6 changes: 6 additions & 0 deletions src/core/qgscoordinatereferencesystem.h
Expand Up @@ -350,6 +350,12 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
/**Returns auth id of related geographic CRS*/
QString geographicCRSAuthId() const;

/**Returns a list of recently used projections
* @returns list of srsid for recently used projections
* @note added in QGIS 2.7
*/
static QStringList recentProjections();

// Mutators -----------------------------------
// We don't want to expose these to the public api since they wont create
// a fully valid crs. Programmers should use the createFrom* methods rather
Expand Down
77 changes: 71 additions & 6 deletions src/gui/qgsprojectionselectionwidget.cpp
Expand Up @@ -54,6 +54,8 @@ QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent ) :
addDefaultCrsOption();
}

addRecentCrs();

layout->addWidget( mCrsComboBox );

mButton = new QToolButton( this );
Expand All @@ -80,6 +82,13 @@ QgsCoordinateReferenceSystem QgsProjectionSelectionWidget::crs() const
return mDefaultCrs ;
case QgsProjectionSelectionWidget::CurrentCrs:
return mCrs;
case QgsProjectionSelectionWidget::RecentCrs:
{
long srsid = mCrsComboBox->itemData( mCrsComboBox->currentIndex(), Qt::UserRole + 1 ).toLongLong();
QgsCoordinateReferenceSystem crs;
crs.createFromSrsId( srsid );
return crs;
}
}
return mCrs;
}
Expand Down Expand Up @@ -109,7 +118,8 @@ void QgsProjectionSelectionWidget::setOptionVisible( const QgsProjectionSelectio
return;
}
case QgsProjectionSelectionWidget::CurrentCrs:
//current CRS option cannot be readded
case QgsProjectionSelectionWidget::RecentCrs:
//current/recently used CRS option cannot be readded
return;
}
}
Expand Down Expand Up @@ -150,16 +160,24 @@ void QgsProjectionSelectionWidget::comboIndexChanged( int idx )
{
case QgsProjectionSelectionWidget::LayerCrs:
emit crsChanged( mLayerCrs );
break;
return;
case QgsProjectionSelectionWidget::ProjectCrs:
emit crsChanged( mProjectCrs );
break;
return;
case QgsProjectionSelectionWidget::CurrentCrs:
emit crsChanged( mCrs );
break;
return;
case QgsProjectionSelectionWidget::DefaultCrs:
emit crsChanged( mDefaultCrs );
break;
return;
case QgsProjectionSelectionWidget::RecentCrs:
{
long srsid = mCrsComboBox->itemData( idx, Qt::UserRole + 1 ).toLongLong();
QgsCoordinateReferenceSystem crs;
crs.createFromSrsId( srsid );
emit crsChanged( crs );
return;
}
}
}

Expand Down Expand Up @@ -192,7 +210,7 @@ void QgsProjectionSelectionWidget::setLayerCrs( const QgsCoordinateReferenceSyst
}
else
{
mCrsComboBox->addItem( tr( "Layer CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ), QgsProjectionSelectionWidget::LayerCrs );
mCrsComboBox->insertItem( firstRecentCrsIndex(), tr( "Layer CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ), QgsProjectionSelectionWidget::LayerCrs );
}
}
else
Expand All @@ -217,3 +235,50 @@ void QgsProjectionSelectionWidget::addDefaultCrsOption()
{
mCrsComboBox->addItem( tr( "Default CRS (%1 - %2)" ).arg( mDefaultCrs.authid() ).arg( mDefaultCrs.description() ), QgsProjectionSelectionWidget::DefaultCrs );
}

void QgsProjectionSelectionWidget::addRecentCrs()
{
QStringList recentProjections = QgsCoordinateReferenceSystem::recentProjections();
int i = 0;
foreach ( QString projection, recentProjections )
{
long srsid = projection.toLong();

//check if already shown
if ( crsIsShown( srsid ) )
{
continue;
}

i++;
QgsCoordinateReferenceSystem crs;
crs.createFromSrsId( srsid );
if ( crs.isValid() )
{
mCrsComboBox->addItem( tr( "%1 - %2" ).arg( crs.authid() ).arg( crs.description() ), QgsProjectionSelectionWidget::RecentCrs );
mCrsComboBox->setItemData( mCrsComboBox->count() - 1, QVariant(( long long )srsid ), Qt::UserRole + 1 );
}
if ( i >= 4 )
{
//limit to 4 recent projections to avoid clutter
break;
}
}
}

bool QgsProjectionSelectionWidget::crsIsShown( const long srsid ) const
{
return srsid == mLayerCrs.srsid() || srsid == mDefaultCrs.srsid() || srsid == mProjectCrs.srsid();
}

int QgsProjectionSelectionWidget::firstRecentCrsIndex() const
{
for ( int i = 0; i < mCrsComboBox->count(); ++i )
{
if (( CrsOption )mCrsComboBox->itemData( i ).toInt() == RecentCrs )
{
return i;
}
}
return -1;
}
7 changes: 6 additions & 1 deletion src/gui/qgsprojectionselectionwidget.h
Expand Up @@ -44,7 +44,8 @@ class GUI_EXPORT QgsProjectionSelectionWidget : public QWidget
LayerCrs, /*< optional layer CRS */
ProjectCrs, /*< current project CRS (if OTF reprojection enabled) */
CurrentCrs, /*< current user selected CRS */
DefaultCrs /*< global default QGIS CRS */
DefaultCrs, /*< global default QGIS CRS */
RecentCrs /*< recently used CRS */
};

explicit QgsProjectionSelectionWidget( QWidget *parent = 0 );
Expand Down Expand Up @@ -100,6 +101,10 @@ class GUI_EXPORT QgsProjectionSelectionWidget : public QWidget

void addProjectCrsOption();
void addDefaultCrsOption();
void addRecentCrs();
bool crsIsShown( const long srsid ) const;

int firstRecentCrsIndex() const;

private slots:

Expand Down
38 changes: 1 addition & 37 deletions src/gui/qgsprojectionselector.cpp
Expand Up @@ -60,42 +60,7 @@ QgsProjectionSelector::QgsProjectionSelector( QWidget* parent, const char *name,
// Hide (internal) ID column
lstRecent->setColumnHidden( QGIS_CRS_ID_COLUMN, true );

// Read settings from persistent storage
QSettings settings;
mRecentProjections = settings.value( "/UI/recentProjections" ).toStringList();
/*** The reading (above) of internal id from persistent storage should be removed sometime in the future */
/*** This is kept now for backwards compatibility */

QStringList projectionsProj4 = settings.value( "/UI/recentProjectionsProj4" ).toStringList();
QStringList projectionsAuthId = settings.value( "/UI/recentProjectionsAuthId" ).toStringList();
if ( projectionsAuthId.size() >= mRecentProjections.size() )
{
// We had saved state with AuthId and Proj4. Use that instead
// to find out the crs id
QgsDebugMsg( "Use popular projection list from AuthId/Proj4 saved state" );
mRecentProjections.clear();
for ( int i = 0; i < projectionsAuthId.size(); i++ )
{
// Create a crs from the EPSG
QgsCoordinateReferenceSystem crs;
crs.createFromOgcWmsCrs( projectionsAuthId.at( i ) );
if ( ! crs.isValid() )
{
// Couldn't create from EPSG, try the Proj4 string instead
if ( i >= projectionsProj4.size() || !crs.createFromProj4( projectionsProj4.at( i ) ) )
{
// No? Skip this entry
continue;
}
//If the CRS can be created but do not correspond to a CRS in the database, skip it (for example a deleted custom CRS)
if ( crs.srsid() == 0 )
{
continue;
}
}
mRecentProjections << QString::number( crs.srsid() );
}
}
mRecentProjections = QgsCoordinateReferenceSystem::recentProjections();
}

QgsProjectionSelector::~QgsProjectionSelector()
Expand Down Expand Up @@ -468,7 +433,6 @@ QString QgsProjectionSelector::selectedAuthId()
return getSelectedExpression( "upper(auth_name||':'||auth_id)" );
}


long QgsProjectionSelector::selectedCrsId()
{
QTreeWidgetItem* item = lstCoordinateSystems->currentItem();
Expand Down

0 comments on commit 1310b87

Please sign in to comment.