Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change projection selection widget to use a combo box
With options for layer CRS, project CRS and default CRS.
  • Loading branch information
nyalldawson committed Jan 19, 2015
1 parent 36f363b commit f68b099
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 78 deletions.
25 changes: 24 additions & 1 deletion python/gui/qgsprojectionselectionwidget.sip
Expand Up @@ -12,6 +12,16 @@ class QgsProjectionSelectionWidget : QWidget

public:

/** Predefined CRS options shown in widget
*/
enum CrsOption
{
LayerCrs, /*< optional layer CRS */
ProjectCrs, /*< current project CRS (if OTF reprojection enabled) */
CurrentCrs, /*< current user selected CRS */
DefaultCrs /*< global default QGIS CRS */
};

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

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

/* Sets whether a predefined CRS option should be shown in the widget.
* @param option CRS option to show/hide
* @param visible whether the option should be shown
*/
void setOptionVisible( const CrsOption option, const bool visible );

signals:

/* Emitted when the selected CRS is changed
Expand All @@ -36,9 +52,16 @@ class QgsProjectionSelectionWidget : QWidget
/* Sets the current CRS for the widget
* @param crs new CRS
*/
void setCrs( QgsCoordinateReferenceSystem crs );
void setCrs( const QgsCoordinateReferenceSystem& crs );

/* Sets the layer CRS for the widget. If set, this will be added as an option
* to the preset CRSes shown in the widget.
* @param crs layer CRS
*/
void setLayerCrs( const QgsCoordinateReferenceSystem& crs );

/* Opens the dialog for selecting a new CRS
*/
void selectCrs();

};
38 changes: 2 additions & 36 deletions src/app/ogr/qgsvectorlayersaveasdialog.cpp
Expand Up @@ -77,11 +77,9 @@ void QgsVectorLayerSaveAsDialog::setup()
idx = 0;
}

mCRSSelection->clear();
mCRSSelection->addItems( QStringList() << tr( "Layer CRS" ) << tr( "Project CRS" ) << tr( "Selected CRS" ) );

QgsCoordinateReferenceSystem srs( mCRS, QgsCoordinateReferenceSystem::InternalCrsId );
mCrsSelector->setCrs( srs );
mCrsSelector->setLayerCrs( srs );
mCrsSelector->dialog()->setMessage( tr( "Select the coordinate reference system for the vector file. "
"The data points will be transformed from the layer coordinate reference system." ) );

Expand Down Expand Up @@ -186,26 +184,6 @@ void QgsVectorLayerSaveAsDialog::accept()
QDialog::accept();
}

void QgsVectorLayerSaveAsDialog::on_mCRSSelection_currentIndexChanged( int idx )
{
mCrsSelector->setEnabled( idx == 2 );

QgsCoordinateReferenceSystem crs;
if ( mCRSSelection->currentIndex() == 0 )
{
crs = mLayerCrs;
}
else if ( mCRSSelection->currentIndex() == 1 )
{
crs = mExtentGroupBox->currentCrs();
}
else // custom CRS
{
crs.createFromId( mCRS, QgsCoordinateReferenceSystem::InternalCrsId );
}
mExtentGroupBox->setOutputCrs( crs );
}

void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx )
{
Q_UNUSED( idx );
Expand Down Expand Up @@ -307,7 +285,6 @@ void QgsVectorLayerSaveAsDialog::on_browseFilename_clicked()
void QgsVectorLayerSaveAsDialog::on_mCrsSelector_crsChanged( QgsCoordinateReferenceSystem crs )
{
mCRS = crs.srsid();
mCRSSelection->setCurrentIndex( 2 );
mExtentGroupBox->setOutputCrs( crs );
}

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

long QgsVectorLayerSaveAsDialog::crs() const
{
if ( mCRSSelection->currentIndex() == 0 )
{
return -1; // Layer CRS
}
else if ( mCRSSelection->currentIndex() == 1 )
{
return -2; // Project CRS
}
else
{
return mCRS;
}
return mCRS;
}

QStringList QgsVectorLayerSaveAsDialog::datasourceOptions() const
Expand Down
1 change: 0 additions & 1 deletion src/app/ogr/qgsvectorlayersaveasdialog.h
Expand Up @@ -68,7 +68,6 @@ class QgsVectorLayerSaveAsDialog : public QDialog, private Ui::QgsVectorLayerSav
private slots:
void on_mFormatComboBox_currentIndexChanged( int idx );
void on_leFilename_textChanged( const QString& text );
void on_mCRSSelection_currentIndexChanged( int idx );
void on_browseFilename_clicked();
void on_mCrsSelector_crsChanged( QgsCoordinateReferenceSystem crs );
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
Expand Down
16 changes: 1 addition & 15 deletions src/app/qgisapp.cpp
Expand Up @@ -5088,21 +5088,7 @@ void QgisApp::saveAsVectorFileGeneral( QgsVectorLayer* vlayer, bool symbologyOpt
QStringList datasourceOptions = dialog->datasourceOptions();

QgsCoordinateTransform* ct = 0;

switch ( dialog->crs() )
{
case -2: // Project CRS
destCRS = mMapCanvas->mapSettings().destinationCrs();

break;
case -1: // Layer CRS
destCRS = vlayer->crs();
break;

default: // Selected CRS
destCRS = QgsCoordinateReferenceSystem( dialog->crs(), QgsCoordinateReferenceSystem::InternalCrsId );
break;
}
destCRS = QgsCoordinateReferenceSystem( dialog->crs(), QgsCoordinateReferenceSystem::InternalCrsId );

if ( destCRS.isValid() && destCRS != vlayer->crs() )
{
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -383,6 +383,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
QString myDefaultCrs = settings.value( "/Projections/projectDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString();
mDefaultCrs.createFromOgcWmsCrs( myDefaultCrs );
leProjectGlobalCrs->setCrs( mDefaultCrs );
leProjectGlobalCrs->setOptionVisible( QgsProjectionSelectionWidget::DefaultCrs, false );

//default datum transformations
settings.beginGroup( "/Projections" );
Expand Down
145 changes: 140 additions & 5 deletions src/gui/qgsprojectionselectionwidget.cpp
Expand Up @@ -19,6 +19,8 @@
#include "qgsprojectionselectionwidget.h"
#include "qgsapplication.h"
#include "qgsgenericprojectionselector.h"
#include "qgsproject.h"
#include <QSettings>

QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent ) :
QWidget( parent )
Expand All @@ -30,9 +32,29 @@ QgsProjectionSelectionWidget::QgsProjectionSelectionWidget( QWidget *parent ) :
layout->setSpacing( 0 );
setLayout( layout );

mCrsLineEdit = new QLineEdit( tr( "invalid projection" ), this );
mCrsLineEdit->setReadOnly( true );
layout->addWidget( mCrsLineEdit );
mCrsComboBox = new QComboBox( this );
mCrsComboBox->addItem( tr( "invalid projection" ), QgsProjectionSelectionWidget::CurrentCrs );

if ( QgsProject::instance()->readNumEntry( "SpatialRefSys", "/ProjectionsEnabled", 0 ) )
{
//only show project CRS if OTF reprojection is enabled - otherwise the
//CRS stored in the project can be misleading
QString projectCrsString = QgsProject::instance()->readEntry( "SpatialRefSys", "/ProjectCrs" );
mProjectCrs.createFromOgcWmsCrs( projectCrsString );
addProjectCrsOption();
}

QSettings settings;
QString defCrsString = settings.value( "/Projections/projectDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString();
mDefaultCrs.createFromOgcWmsCrs( defCrsString );
if ( mDefaultCrs.authid() != mProjectCrs.authid() )
{
//only show default CRS option if it's different to the project CRS, avoids
//needlessly cluttering the widget
addDefaultCrsOption();
}

layout->addWidget( mCrsComboBox );

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

connect( mButton, SIGNAL( clicked() ), this, SLOT( selectCrs() ) );
connect( mCrsComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( comboIndexChanged( int ) ) );
}

QgsCoordinateReferenceSystem QgsProjectionSelectionWidget::crs() const
{
switch (( CrsOption )mCrsComboBox->itemData( mCrsComboBox->currentIndex() ).toInt() )
{
case QgsProjectionSelectionWidget::LayerCrs:
return mLayerCrs;
case QgsProjectionSelectionWidget::ProjectCrs:
return mProjectCrs ;
case QgsProjectionSelectionWidget::DefaultCrs:
return mDefaultCrs ;
case QgsProjectionSelectionWidget::CurrentCrs:
return mCrs;
}
return mCrs;
}

void QgsProjectionSelectionWidget::setOptionVisible( const QgsProjectionSelectionWidget::CrsOption option, const bool visible )
{
int optionIndex = mCrsComboBox->findData( option );

if ( visible && optionIndex < 0 )
{
//add missing CRS option
switch ( option )
{
case QgsProjectionSelectionWidget::LayerCrs:
{
setLayerCrs( mLayerCrs );
return;
}
case QgsProjectionSelectionWidget::ProjectCrs:
{
addProjectCrsOption();
return;
}
case QgsProjectionSelectionWidget::DefaultCrs:
{
addDefaultCrsOption();
return;
}
}
}
else if ( !visible && optionIndex >= 0 )
{
//remove CRS option
mCrsComboBox->removeItem( optionIndex );
}
}

void QgsProjectionSelectionWidget::selectCrs()
Expand All @@ -55,6 +127,9 @@ void QgsProjectionSelectionWidget::selectCrs()

if ( mDialog->exec() )
{
mCrsComboBox->blockSignals( true );
mCrsComboBox->setCurrentIndex( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ) );
mCrsComboBox->blockSignals( false );
QgsCoordinateReferenceSystem crs;
crs.createFromOgcWmsCrs( mDialog->selectedAuthId() );
setCrs( crs );
Expand All @@ -66,16 +141,76 @@ void QgsProjectionSelectionWidget::selectCrs()
}
}

void QgsProjectionSelectionWidget::comboIndexChanged( int idx )
{
switch (( CrsOption )mCrsComboBox->itemData( idx ).toInt() )
{
case QgsProjectionSelectionWidget::LayerCrs:
emit crsChanged( mLayerCrs );
break;
case QgsProjectionSelectionWidget::ProjectCrs:
emit crsChanged( mProjectCrs );
break;
case QgsProjectionSelectionWidget::CurrentCrs:
emit crsChanged( mCrs );
break;
case QgsProjectionSelectionWidget::DefaultCrs:
emit crsChanged( mDefaultCrs );
break;
}
}

void QgsProjectionSelectionWidget::setCrs( const QgsCoordinateReferenceSystem& crs )
{
if ( crs.isValid() )
{
mCrsLineEdit->setText( crs.authid() + " - " + crs.description() );
mCrsComboBox->setItemText( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ),
tr( "Selected CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ) );
mCrsComboBox->blockSignals( true );
mCrsComboBox->setCurrentIndex( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ) );
mCrsComboBox->blockSignals( false );
}
else
{
mCrsLineEdit->setText( tr( "invalid projection" ) );
mCrsComboBox->setItemText( mCrsComboBox->findData( QgsProjectionSelectionWidget::CurrentCrs ),
tr( "invalid projection" ) );
}
mCrs = crs;
}

void QgsProjectionSelectionWidget::setLayerCrs( const QgsCoordinateReferenceSystem &crs )
{
int layerItemIndex = mCrsComboBox->findData( QgsProjectionSelectionWidget::LayerCrs );
if ( crs.isValid() )
{
if ( layerItemIndex > -1 )
{
mCrsComboBox->setItemText( layerItemIndex, tr( "Layer CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ) );
}
else
{
mCrsComboBox->addItem( tr( "Layer CRS (%1, %2)" ).arg( crs.authid() ).arg( crs.description() ), QgsProjectionSelectionWidget::LayerCrs );
}
}
else
{
if ( layerItemIndex > -1 )
{
mCrsComboBox->removeItem( layerItemIndex );
}
}
mLayerCrs = crs;
}

void QgsProjectionSelectionWidget::addProjectCrsOption()
{
if ( mProjectCrs.isValid() )
{
mCrsComboBox->addItem( tr( "Project CRS (%1 - %2)" ).arg( mProjectCrs.authid() ).arg( mProjectCrs.description() ), QgsProjectionSelectionWidget::ProjectCrs );
}
}

void QgsProjectionSelectionWidget::addDefaultCrsOption()
{
mCrsComboBox->addItem( tr( "Default CRS (%1 - %2)" ).arg( mDefaultCrs.authid() ).arg( mDefaultCrs.description() ), QgsProjectionSelectionWidget::DefaultCrs );
}

0 comments on commit f68b099

Please sign in to comment.