Skip to content

Commit f68b099

Browse files
committedJan 19, 2015
Change projection selection widget to use a combo box
With options for layer CRS, project CRS and default CRS.
1 parent 36f363b commit f68b099

8 files changed

+220
-78
lines changed
 

‎python/gui/qgsprojectionselectionwidget.sip

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ class QgsProjectionSelectionWidget : QWidget
1212

1313
public:
1414

15+
/** Predefined CRS options shown in widget
16+
*/
17+
enum CrsOption
18+
{
19+
LayerCrs, /*< optional layer CRS */
20+
ProjectCrs, /*< current project CRS (if OTF reprojection enabled) */
21+
CurrentCrs, /*< current user selected CRS */
22+
DefaultCrs /*< global default QGIS CRS */
23+
};
24+
1525
explicit QgsProjectionSelectionWidget( QWidget *parent /TransferThis/ = 0 );
1626

1727
/* Returns a pointer to the projection selector dialog used by the widget. Can be used
@@ -25,6 +35,12 @@ class QgsProjectionSelectionWidget : QWidget
2535
*/
2636
QgsCoordinateReferenceSystem crs() const;
2737

38+
/* Sets whether a predefined CRS option should be shown in the widget.
39+
* @param option CRS option to show/hide
40+
* @param visible whether the option should be shown
41+
*/
42+
void setOptionVisible( const CrsOption option, const bool visible );
43+
2844
signals:
2945

3046
/* Emitted when the selected CRS is changed
@@ -36,9 +52,16 @@ class QgsProjectionSelectionWidget : QWidget
3652
/* Sets the current CRS for the widget
3753
* @param crs new CRS
3854
*/
39-
void setCrs( QgsCoordinateReferenceSystem crs );
55+
void setCrs( const QgsCoordinateReferenceSystem& crs );
56+
57+
/* Sets the layer CRS for the widget. If set, this will be added as an option
58+
* to the preset CRSes shown in the widget.
59+
* @param crs layer CRS
60+
*/
61+
void setLayerCrs( const QgsCoordinateReferenceSystem& crs );
4062

4163
/* Opens the dialog for selecting a new CRS
4264
*/
4365
void selectCrs();
66+
4467
};

‎src/app/ogr/qgsvectorlayersaveasdialog.cpp

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ void QgsVectorLayerSaveAsDialog::setup()
7777
idx = 0;
7878
}
7979

80-
mCRSSelection->clear();
81-
mCRSSelection->addItems( QStringList() << tr( "Layer CRS" ) << tr( "Project CRS" ) << tr( "Selected CRS" ) );
82-
8380
QgsCoordinateReferenceSystem srs( mCRS, QgsCoordinateReferenceSystem::InternalCrsId );
8481
mCrsSelector->setCrs( srs );
82+
mCrsSelector->setLayerCrs( srs );
8583
mCrsSelector->dialog()->setMessage( tr( "Select the coordinate reference system for the vector file. "
8684
"The data points will be transformed from the layer coordinate reference system." ) );
8785

@@ -186,26 +184,6 @@ void QgsVectorLayerSaveAsDialog::accept()
186184
QDialog::accept();
187185
}
188186

189-
void QgsVectorLayerSaveAsDialog::on_mCRSSelection_currentIndexChanged( int idx )
190-
{
191-
mCrsSelector->setEnabled( idx == 2 );
192-
193-
QgsCoordinateReferenceSystem crs;
194-
if ( mCRSSelection->currentIndex() == 0 )
195-
{
196-
crs = mLayerCrs;
197-
}
198-
else if ( mCRSSelection->currentIndex() == 1 )
199-
{
200-
crs = mExtentGroupBox->currentCrs();
201-
}
202-
else // custom CRS
203-
{
204-
crs.createFromId( mCRS, QgsCoordinateReferenceSystem::InternalCrsId );
205-
}
206-
mExtentGroupBox->setOutputCrs( crs );
207-
}
208-
209187
void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx )
210188
{
211189
Q_UNUSED( idx );
@@ -307,7 +285,6 @@ void QgsVectorLayerSaveAsDialog::on_browseFilename_clicked()
307285
void QgsVectorLayerSaveAsDialog::on_mCrsSelector_crsChanged( QgsCoordinateReferenceSystem crs )
308286
{
309287
mCRS = crs.srsid();
310-
mCRSSelection->setCurrentIndex( 2 );
311288
mExtentGroupBox->setOutputCrs( crs );
312289
}
313290

@@ -328,18 +305,7 @@ QString QgsVectorLayerSaveAsDialog::format() const
328305

329306
long QgsVectorLayerSaveAsDialog::crs() const
330307
{
331-
if ( mCRSSelection->currentIndex() == 0 )
332-
{
333-
return -1; // Layer CRS
334-
}
335-
else if ( mCRSSelection->currentIndex() == 1 )
336-
{
337-
return -2; // Project CRS
338-
}
339-
else
340-
{
341-
return mCRS;
342-
}
308+
return mCRS;
343309
}
344310

345311
QStringList QgsVectorLayerSaveAsDialog::datasourceOptions() const

‎src/app/ogr/qgsvectorlayersaveasdialog.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class QgsVectorLayerSaveAsDialog : public QDialog, private Ui::QgsVectorLayerSav
6868
private slots:
6969
void on_mFormatComboBox_currentIndexChanged( int idx );
7070
void on_leFilename_textChanged( const QString& text );
71-
void on_mCRSSelection_currentIndexChanged( int idx );
7271
void on_browseFilename_clicked();
7372
void on_mCrsSelector_crsChanged( QgsCoordinateReferenceSystem crs );
7473
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5088,21 +5088,7 @@ void QgisApp::saveAsVectorFileGeneral( QgsVectorLayer* vlayer, bool symbologyOpt
50885088
QStringList datasourceOptions = dialog->datasourceOptions();
50895089

50905090
QgsCoordinateTransform* ct = 0;
5091-
5092-
switch ( dialog->crs() )
5093-
{
5094-
case -2: // Project CRS
5095-
destCRS = mMapCanvas->mapSettings().destinationCrs();
5096-
5097-
break;
5098-
case -1: // Layer CRS
5099-
destCRS = vlayer->crs();
5100-
break;
5101-
5102-
default: // Selected CRS
5103-
destCRS = QgsCoordinateReferenceSystem( dialog->crs(), QgsCoordinateReferenceSystem::InternalCrsId );
5104-
break;
5105-
}
5091+
destCRS = QgsCoordinateReferenceSystem( dialog->crs(), QgsCoordinateReferenceSystem::InternalCrsId );
51065092

51075093
if ( destCRS.isValid() && destCRS != vlayer->crs() )
51085094
{

‎src/app/qgsoptions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
383383
QString myDefaultCrs = settings.value( "/Projections/projectDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString();
384384
mDefaultCrs.createFromOgcWmsCrs( myDefaultCrs );
385385
leProjectGlobalCrs->setCrs( mDefaultCrs );
386+
leProjectGlobalCrs->setOptionVisible( QgsProjectionSelectionWidget::DefaultCrs, false );
386387

387388
//default datum transformations
388389
settings.beginGroup( "/Projections" );

‎src/gui/qgsprojectionselectionwidget.cpp

Lines changed: 140 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "qgsprojectionselectionwidget.h"
2020
#include "qgsapplication.h"
2121
#include "qgsgenericprojectionselector.h"
22+
#include "qgsproject.h"
23+
#include <QSettings>
2224

2325
QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent ) :
2426
QWidget( parent )
@@ -30,9 +32,29 @@ QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent ) :
3032
layout->setSpacing( 0 );
3133
setLayout( layout );
3234

33-
mCrsLineEdit = new QLineEdit( tr( "invalid projection" ), this );
34-
mCrsLineEdit->setReadOnly( true );
35-
layout->addWidget( mCrsLineEdit );
35+
mCrsComboBox = new QComboBox( this );
36+
mCrsComboBox->addItem( tr( "invalid projection" ), QgsProjectionSelectionWidget::CurrentCrs );
37+
38+
if ( QgsProject::instance()->readNumEntry( "SpatialRefSys", "/ProjectionsEnabled", 0 ) )
39+
{
40+
//only show project CRS if OTF reprojection is enabled - otherwise the
41+
//CRS stored in the project can be misleading
42+
QString projectCrsString = QgsProject::instance()->readEntry( "SpatialRefSys", "/ProjectCrs" );
43+
mProjectCrs.createFromOgcWmsCrs( projectCrsString );
44+
addProjectCrsOption();
45+
}
46+
47+
QSettings settings;
48+
QString defCrsString = settings.value( "/Projections/projectDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString();
49+
mDefaultCrs.createFromOgcWmsCrs( defCrsString );
50+
if ( mDefaultCrs.authid() != mProjectCrs.authid() )
51+
{
52+
//only show default CRS option if it's different to the project CRS, avoids
53+
//needlessly cluttering the widget
54+
addDefaultCrsOption();
55+
}
56+
57+
layout->addWidget( mCrsComboBox );
3658

3759
mButton = new QToolButton( this );
3860
mButton->setIcon( QgsApplication::getThemeIcon( "mActionSetProjection.svg" ) );
@@ -43,6 +65,56 @@ QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent ) :
4365
setFocusProxy( mButton );
4466

4567
connect( mButton, SIGNAL( clicked() ), this, SLOT( selectCrs() ) );
68+
connect( mCrsComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( comboIndexChanged( int ) ) );
69+
}
70+
71+
QgsCoordinateReferenceSystem QgsProjectionSelectionWidget::crs() const
72+
{
73+
switch (( CrsOption )mCrsComboBox->itemData( mCrsComboBox->currentIndex() ).toInt() )
74+
{
75+
case QgsProjectionSelectionWidget::LayerCrs:
76+
return mLayerCrs;
77+
case QgsProjectionSelectionWidget::ProjectCrs:
78+
return mProjectCrs ;
79+
case QgsProjectionSelectionWidget::DefaultCrs:
80+
return mDefaultCrs ;
81+
case QgsProjectionSelectionWidget::CurrentCrs:
82+
return mCrs;
83+
}
84+
return mCrs;
85+
}
86+
87+
void QgsProjectionSelectionWidget::setOptionVisible( const QgsProjectionSelectionWidget::CrsOption option, const bool visible )
88+
{
89+
int optionIndex = mCrsComboBox->findData( option );
90+
91+
if ( visible && optionIndex < 0 )
92+
{
93+
//add missing CRS option
94+
switch ( option )
95+
{
96+
case QgsProjectionSelectionWidget::LayerCrs:
97+
{
98+
setLayerCrs( mLayerCrs );
99+
return;
100+
}
101+
case QgsProjectionSelectionWidget::ProjectCrs:
102+
{
103+
addProjectCrsOption();
104+
return;
105+
}
106+
case QgsProjectionSelectionWidget::DefaultCrs:
107+
{
108+
addDefaultCrsOption();
109+
return;
110+
}
111+
}
112+
}
113+
else if ( !visible && optionIndex >= 0 )
114+
{
115+
//remove CRS option
116+
mCrsComboBox->removeItem( optionIndex );
117+
}
46118
}
47119

48120
void QgsProjectionSelectionWidget::selectCrs()
@@ -55,6 +127,9 @@ void QgsProjectionSelectionWidget::selectCrs()
55127

56128
if ( mDialog->exec() )
57129
{
130+
mCrsComboBox->blockSignals( true );
131+
mCrsComboBox->setCurrentIndex( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ) );
132+
mCrsComboBox->blockSignals( false );
58133
QgsCoordinateReferenceSystem crs;
59134
crs.createFromOgcWmsCrs( mDialog->selectedAuthId() );
60135
setCrs( crs );
@@ -66,16 +141,76 @@ void QgsProjectionSelectionWidget::selectCrs()
66141
}
67142
}
68143

144+
void QgsProjectionSelectionWidget::comboIndexChanged( int idx )
145+
{
146+
switch (( CrsOption )mCrsComboBox->itemData( idx ).toInt() )
147+
{
148+
case QgsProjectionSelectionWidget::LayerCrs:
149+
emit crsChanged( mLayerCrs );
150+
break;
151+
case QgsProjectionSelectionWidget::ProjectCrs:
152+
emit crsChanged( mProjectCrs );
153+
break;
154+
case QgsProjectionSelectionWidget::CurrentCrs:
155+
emit crsChanged( mCrs );
156+
break;
157+
case QgsProjectionSelectionWidget::DefaultCrs:
158+
emit crsChanged( mDefaultCrs );
159+
break;
160+
}
161+
}
69162

70163
void QgsProjectionSelectionWidget::setCrs( const QgsCoordinateReferenceSystem& crs )
71164
{
72165
if ( crs.isValid() )
73166
{
74-
mCrsLineEdit->setText( crs.authid() + " - " + crs.description() );
167+
mCrsComboBox->setItemText( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ),
168+
tr( "Selected CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ) );
169+
mCrsComboBox->blockSignals( true );
170+
mCrsComboBox->setCurrentIndex( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ) );
171+
mCrsComboBox->blockSignals( false );
75172
}
76173
else
77174
{
78-
mCrsLineEdit->setText( tr( "invalid projection" ) );
175+
mCrsComboBox->setItemText( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ),
176+
tr( "invalid projection" ) );
79177
}
80178
mCrs = crs;
81179
}
180+
181+
void QgsProjectionSelectionWidget::setLayerCrs( const QgsCoordinateReferenceSystem &crs )
182+
{
183+
int layerItemIndex = mCrsComboBox->findData( QgsProjectionSelectionWidget::LayerCrs );
184+
if ( crs.isValid() )
185+
{
186+
if ( layerItemIndex > -1 )
187+
{
188+
mCrsComboBox->setItemText( layerItemIndex, tr( "Layer CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ) );
189+
}
190+
else
191+
{
192+
mCrsComboBox->addItem( tr( "Layer CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ), QgsProjectionSelectionWidget::LayerCrs );
193+
}
194+
}
195+
else
196+
{
197+
if ( layerItemIndex > -1 )
198+
{
199+
mCrsComboBox->removeItem( layerItemIndex );
200+
}
201+
}
202+
mLayerCrs = crs;
203+
}
204+
205+
void QgsProjectionSelectionWidget::addProjectCrsOption()
206+
{
207+
if ( mProjectCrs.isValid() )
208+
{
209+
mCrsComboBox->addItem( tr( "Project CRS (%1 - %2)" ).arg( mProjectCrs.authid() ).arg( mProjectCrs.description() ), QgsProjectionSelectionWidget::ProjectCrs );
210+
}
211+
}
212+
213+
void QgsProjectionSelectionWidget::addDefaultCrsOption()
214+
{
215+
mCrsComboBox->addItem( tr( "Default CRS (%1 - %2)" ).arg( mDefaultCrs.authid() ).arg( mDefaultCrs.description() ), QgsProjectionSelectionWidget::DefaultCrs );
216+
}

0 commit comments

Comments
 (0)
Please sign in to comment.