Skip to content

Commit 1310b87

Browse files
committedJan 19, 2015
Show recently used projections in QgsProjectionSelectionWidget
1 parent 953b39e commit 1310b87

7 files changed

+134
-45
lines changed
 

‎python/core/qgscoordinatereferencesystem.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,10 @@ class QgsCoordinateReferenceSystem
304304

305305
/**Returns auth id of related geographic CRS*/
306306
QString geographicCRSAuthId() const;
307+
308+
/**Returns a list of recently used projections
309+
* @returns list of srsid for recently used projections
310+
* @note added in QGIS 2.7
311+
*/
312+
static QStringList recentProjections();
307313
};

‎python/gui/qgsprojectionselectionwidget.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class QgsProjectionSelectionWidget : QWidget
1919
LayerCrs, /*< optional layer CRS */
2020
ProjectCrs, /*< current project CRS (if OTF reprojection enabled) */
2121
CurrentCrs, /*< current user selected CRS */
22-
DefaultCrs /*< global default QGIS CRS */
22+
DefaultCrs, /*< global default QGIS CRS */
23+
RecentCrs /*< recently used CRS */
2324
};
2425

2526
explicit QgsProjectionSelectionWidget( QWidget *parent /TransferThis/ = 0 );

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,3 +2167,45 @@ QString QgsCoordinateReferenceSystem::geographicCRSAuthId() const
21672167
return "";
21682168
}
21692169
}
2170+
2171+
QStringList QgsCoordinateReferenceSystem::recentProjections()
2172+
{
2173+
QStringList projections;
2174+
2175+
// Read settings from persistent storage
2176+
QSettings settings;
2177+
projections = settings.value( "/UI/recentProjections" ).toStringList();
2178+
/*** The reading (above) of internal id from persistent storage should be removed sometime in the future */
2179+
/*** This is kept now for backwards compatibility */
2180+
2181+
QStringList projectionsProj4 = settings.value( "/UI/recentProjectionsProj4" ).toStringList();
2182+
QStringList projectionsAuthId = settings.value( "/UI/recentProjectionsAuthId" ).toStringList();
2183+
if ( projectionsAuthId.size() >= projections.size() )
2184+
{
2185+
// We had saved state with AuthId and Proj4. Use that instead
2186+
// to find out the crs id
2187+
projections.clear();
2188+
for ( int i = 0; i < projectionsAuthId.size(); i++ )
2189+
{
2190+
// Create a crs from the EPSG
2191+
QgsCoordinateReferenceSystem crs;
2192+
crs.createFromOgcWmsCrs( projectionsAuthId.at( i ) );
2193+
if ( ! crs.isValid() )
2194+
{
2195+
// Couldn't create from EPSG, try the Proj4 string instead
2196+
if ( i >= projectionsProj4.size() || !crs.createFromProj4( projectionsProj4.at( i ) ) )
2197+
{
2198+
// No? Skip this entry
2199+
continue;
2200+
}
2201+
//If the CRS can be created but do not correspond to a CRS in the database, skip it (for example a deleted custom CRS)
2202+
if ( crs.srsid() == 0 )
2203+
{
2204+
continue;
2205+
}
2206+
}
2207+
projections << QString::number( crs.srsid() );
2208+
}
2209+
}
2210+
return projections;
2211+
}

‎src/core/qgscoordinatereferencesystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
350350
/**Returns auth id of related geographic CRS*/
351351
QString geographicCRSAuthId() const;
352352

353+
/**Returns a list of recently used projections
354+
* @returns list of srsid for recently used projections
355+
* @note added in QGIS 2.7
356+
*/
357+
static QStringList recentProjections();
358+
353359
// Mutators -----------------------------------
354360
// We don't want to expose these to the public api since they wont create
355361
// a fully valid crs. Programmers should use the createFrom* methods rather

‎src/gui/qgsprojectionselectionwidget.cpp

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent ) :
5454
addDefaultCrsOption();
5555
}
5656

57+
addRecentCrs();
58+
5759
layout->addWidget( mCrsComboBox );
5860

5961
mButton = new QToolButton( this );
@@ -80,6 +82,13 @@ QgsCoordinateReferenceSystem QgsProjectionSelectionWidget::crs() const
8082
return mDefaultCrs ;
8183
case QgsProjectionSelectionWidget::CurrentCrs:
8284
return mCrs;
85+
case QgsProjectionSelectionWidget::RecentCrs:
86+
{
87+
long srsid = mCrsComboBox->itemData( mCrsComboBox->currentIndex(), Qt::UserRole + 1 ).toLongLong();
88+
QgsCoordinateReferenceSystem crs;
89+
crs.createFromSrsId( srsid );
90+
return crs;
91+
}
8392
}
8493
return mCrs;
8594
}
@@ -109,7 +118,8 @@ void QgsProjectionSelectionWidget::setOptionVisible( const QgsProjectionSelectio
109118
return;
110119
}
111120
case QgsProjectionSelectionWidget::CurrentCrs:
112-
//current CRS option cannot be readded
121+
case QgsProjectionSelectionWidget::RecentCrs:
122+
//current/recently used CRS option cannot be readded
113123
return;
114124
}
115125
}
@@ -150,16 +160,24 @@ void QgsProjectionSelectionWidget::comboIndexChanged( int idx )
150160
{
151161
case QgsProjectionSelectionWidget::LayerCrs:
152162
emit crsChanged( mLayerCrs );
153-
break;
163+
return;
154164
case QgsProjectionSelectionWidget::ProjectCrs:
155165
emit crsChanged( mProjectCrs );
156-
break;
166+
return;
157167
case QgsProjectionSelectionWidget::CurrentCrs:
158168
emit crsChanged( mCrs );
159-
break;
169+
return;
160170
case QgsProjectionSelectionWidget::DefaultCrs:
161171
emit crsChanged( mDefaultCrs );
162-
break;
172+
return;
173+
case QgsProjectionSelectionWidget::RecentCrs:
174+
{
175+
long srsid = mCrsComboBox->itemData( idx, Qt::UserRole + 1 ).toLongLong();
176+
QgsCoordinateReferenceSystem crs;
177+
crs.createFromSrsId( srsid );
178+
emit crsChanged( crs );
179+
return;
180+
}
163181
}
164182
}
165183

@@ -192,7 +210,7 @@ void QgsProjectionSelectionWidget::setLayerCrs( const QgsCoordinateReferenceSyst
192210
}
193211
else
194212
{
195-
mCrsComboBox->addItem( tr( "Layer CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ), QgsProjectionSelectionWidget::LayerCrs );
213+
mCrsComboBox->insertItem( firstRecentCrsIndex(), tr( "Layer CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ), QgsProjectionSelectionWidget::LayerCrs );
196214
}
197215
}
198216
else
@@ -217,3 +235,50 @@ void QgsProjectionSelectionWidget::addDefaultCrsOption()
217235
{
218236
mCrsComboBox->addItem( tr( "Default CRS (%1 - %2)" ).arg( mDefaultCrs.authid() ).arg( mDefaultCrs.description() ), QgsProjectionSelectionWidget::DefaultCrs );
219237
}
238+
239+
void QgsProjectionSelectionWidget::addRecentCrs()
240+
{
241+
QStringList recentProjections = QgsCoordinateReferenceSystem::recentProjections();
242+
int i = 0;
243+
foreach ( QString projection, recentProjections )
244+
{
245+
long srsid = projection.toLong();
246+
247+
//check if already shown
248+
if ( crsIsShown( srsid ) )
249+
{
250+
continue;
251+
}
252+
253+
i++;
254+
QgsCoordinateReferenceSystem crs;
255+
crs.createFromSrsId( srsid );
256+
if ( crs.isValid() )
257+
{
258+
mCrsComboBox->addItem( tr( "%1 - %2" ).arg( crs.authid() ).arg( crs.description() ), QgsProjectionSelectionWidget::RecentCrs );
259+
mCrsComboBox->setItemData( mCrsComboBox->count() - 1, QVariant(( long long )srsid ), Qt::UserRole + 1 );
260+
}
261+
if ( i >= 4 )
262+
{
263+
//limit to 4 recent projections to avoid clutter
264+
break;
265+
}
266+
}
267+
}
268+
269+
bool QgsProjectionSelectionWidget::crsIsShown( const long srsid ) const
270+
{
271+
return srsid == mLayerCrs.srsid() || srsid == mDefaultCrs.srsid() || srsid == mProjectCrs.srsid();
272+
}
273+
274+
int QgsProjectionSelectionWidget::firstRecentCrsIndex() const
275+
{
276+
for ( int i = 0; i < mCrsComboBox->count(); ++i )
277+
{
278+
if (( CrsOption )mCrsComboBox->itemData( i ).toInt() == RecentCrs )
279+
{
280+
return i;
281+
}
282+
}
283+
return -1;
284+
}

‎src/gui/qgsprojectionselectionwidget.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class GUI_EXPORT QgsProjectionSelectionWidget : public QWidget
4444
LayerCrs, /*< optional layer CRS */
4545
ProjectCrs, /*< current project CRS (if OTF reprojection enabled) */
4646
CurrentCrs, /*< current user selected CRS */
47-
DefaultCrs /*< global default QGIS CRS */
47+
DefaultCrs, /*< global default QGIS CRS */
48+
RecentCrs /*< recently used CRS */
4849
};
4950

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

101102
void addProjectCrsOption();
102103
void addDefaultCrsOption();
104+
void addRecentCrs();
105+
bool crsIsShown( const long srsid ) const;
106+
107+
int firstRecentCrsIndex() const;
103108

104109
private slots:
105110

‎src/gui/qgsprojectionselector.cpp

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,42 +60,7 @@ QgsProjectionSelector::QgsProjectionSelector( QWidget* parent, const char *name,
6060
// Hide (internal) ID column
6161
lstRecent->setColumnHidden( QGIS_CRS_ID_COLUMN, true );
6262

63-
// Read settings from persistent storage
64-
QSettings settings;
65-
mRecentProjections = settings.value( "/UI/recentProjections" ).toStringList();
66-
/*** The reading (above) of internal id from persistent storage should be removed sometime in the future */
67-
/*** This is kept now for backwards compatibility */
68-
69-
QStringList projectionsProj4 = settings.value( "/UI/recentProjectionsProj4" ).toStringList();
70-
QStringList projectionsAuthId = settings.value( "/UI/recentProjectionsAuthId" ).toStringList();
71-
if ( projectionsAuthId.size() >= mRecentProjections.size() )
72-
{
73-
// We had saved state with AuthId and Proj4. Use that instead
74-
// to find out the crs id
75-
QgsDebugMsg( "Use popular projection list from AuthId/Proj4 saved state" );
76-
mRecentProjections.clear();
77-
for ( int i = 0; i < projectionsAuthId.size(); i++ )
78-
{
79-
// Create a crs from the EPSG
80-
QgsCoordinateReferenceSystem crs;
81-
crs.createFromOgcWmsCrs( projectionsAuthId.at( i ) );
82-
if ( ! crs.isValid() )
83-
{
84-
// Couldn't create from EPSG, try the Proj4 string instead
85-
if ( i >= projectionsProj4.size() || !crs.createFromProj4( projectionsProj4.at( i ) ) )
86-
{
87-
// No? Skip this entry
88-
continue;
89-
}
90-
//If the CRS can be created but do not correspond to a CRS in the database, skip it (for example a deleted custom CRS)
91-
if ( crs.srsid() == 0 )
92-
{
93-
continue;
94-
}
95-
}
96-
mRecentProjections << QString::number( crs.srsid() );
97-
}
98-
}
63+
mRecentProjections = QgsCoordinateReferenceSystem::recentProjections();
9964
}
10065

10166
QgsProjectionSelector::~QgsProjectionSelector()
@@ -468,7 +433,6 @@ QString QgsProjectionSelector::selectedAuthId()
468433
return getSelectedExpression( "upper(auth_name||':'||auth_id)" );
469434
}
470435

471-
472436
long QgsProjectionSelector::selectedCrsId()
473437
{
474438
QTreeWidgetItem* item = lstCoordinateSystems->currentItem();

0 commit comments

Comments
 (0)
Please sign in to comment.