Skip to content

Commit

Permalink
Set default layer CRS according to behaviour radio (fix #11889)
Browse files Browse the repository at this point in the history
Makes the new layer dialogs correctly respect the current project
CRS choice when set to follow project CRS.
  • Loading branch information
nyalldawson committed Mar 22, 2017
1 parent 1042b44 commit 0500865
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 44 deletions.
1 change: 1 addition & 0 deletions python/gui/qgsnewgeopackagelayerdialog.sip
Expand Up @@ -11,4 +11,5 @@ class QgsNewGeoPackageLayerDialog : QDialog

~QgsNewGeoPackageLayerDialog();

void setCrs( const QgsCoordinateReferenceSystem &crs );
};
2 changes: 1 addition & 1 deletion python/gui/qgsnewmemorylayerdialog.sip
Expand Up @@ -18,7 +18,7 @@ class QgsNewMemoryLayerDialog : QDialog
/** Returns the selected geometry type*/
QgsWkbTypes::Type selectedType() const;

/** Returns the selected crs*/
void setCrs( const QgsCoordinateReferenceSystem &crs );
QgsCoordinateReferenceSystem crs() const;

/** Returns the layer name*/
Expand Down
5 changes: 3 additions & 2 deletions python/gui/qgsnewvectorlayerdialog.sip
Expand Up @@ -20,8 +20,9 @@ class QgsNewVectorLayerDialog : QDialog
QString selectedFileFormat() const;
/** Returns the file format for storage*/
QString selectedFileEncoding() const;
/** Returns the selected crs id*/
int selectedCrsId() const;

QgsCoordinateReferenceSystem crs() const;
void setCrs( const QgsCoordinateReferenceSystem &crs );

protected slots:
void on_mAddAttributeButton_clicked();
Expand Down
30 changes: 26 additions & 4 deletions src/app/qgisapp.cpp
Expand Up @@ -560,7 +560,7 @@ void QgisApp::validateCrs( QgsCoordinateReferenceSystem &srs )
else if ( myDefaultProjectionOption == QLatin1String( "useProject" ) )
{
// XXX TODO: Change project to store selected CS as 'projectCRS' not 'selectedWkt'
sAuthId = QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs().authid();
sAuthId = QgsProject::instance()->crs().authid();
srs.createFromOgcWmsCrs( sAuthId );
QgsDebugMsg( "Layer srs set from project: " + sAuthId );
messageBar()->pushMessage( tr( "CRS was undefined" ), tr( "defaulting to project CRS %1 - %2" ).arg( sAuthId, srs.description() ), QgsMessageBar::WARNING, messageTimeout() );
Expand Down Expand Up @@ -1649,6 +1649,27 @@ void QgisApp::applyDefaultSettingsToCanvas( QgsMapCanvas *canvas )
canvas->setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType( settings.value( QStringLiteral( "qgis/segmentationToleranceType" ), "0" ).toInt() ) );
}

QgsCoordinateReferenceSystem QgisApp::defaultCrsForNewLayers() const
{
QgsSettings settings;
QgsCoordinateReferenceSystem defaultCrs;
if ( settings.value( QStringLiteral( "/Projections/defaultBehavior" ), QStringLiteral( "prompt" ) ).toString() == QStringLiteral( "useProject" )
|| settings.value( QStringLiteral( "/Projections/defaultBehavior" ), QStringLiteral( "prompt" ) ).toString() == QStringLiteral( "prompt" ) )
{
// for new layers if the new layer crs method is set to either prompt or use project, then we use the project crs
// (since "prompt" has no meaning here - the prompt will always be shown, it's just deciding on the default choice in the prompt!)
defaultCrs = QgsProject::instance()->crs();
}
else
{
// global crs
QString layerDefaultCrs = settings.value( QStringLiteral( "/Projections/layerDefaultCrs" ), GEO_EPSG_CRS_AUTHID ).toString();
defaultCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( layerDefaultCrs );
}

return defaultCrs;
}

void QgisApp::readSettings()
{
QgsSettings settings;
Expand Down Expand Up @@ -5004,7 +5025,7 @@ void QgisApp::fileNewFromTemplateAction( QAction *qAction )
void QgisApp::newVectorLayer()
{
QString enc;
QString fileName = QgsNewVectorLayerDialog::runAndCreateLayer( this, &enc );
QString fileName = QgsNewVectorLayerDialog::runAndCreateLayer( this, &enc, defaultCrsForNewLayers() );

if ( !fileName.isEmpty() )
{
Expand All @@ -5026,7 +5047,7 @@ void QgisApp::newVectorLayer()

void QgisApp::newMemoryLayer()
{
QgsVectorLayer *newLayer = QgsNewMemoryLayerDialog::runAndCreateLayer( this );
QgsVectorLayer *newLayer = QgsNewMemoryLayerDialog::runAndCreateLayer( this, defaultCrsForNewLayers() );

if ( newLayer )
{
Expand All @@ -5041,13 +5062,14 @@ void QgisApp::newMemoryLayer()

void QgisApp::newSpatialiteLayer()
{
QgsNewSpatialiteLayerDialog spatialiteDialog( this );
QgsNewSpatialiteLayerDialog spatialiteDialog( this, QgisGui::ModalDialogFlags, defaultCrsForNewLayers() );
spatialiteDialog.exec();
}

void QgisApp::newGeoPackageLayer()
{
QgsNewGeoPackageLayerDialog dialog( this );
dialog.setCrs( defaultCrsForNewLayers() );
dialog.exec();
}

Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -1627,6 +1627,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
*/
void applyDefaultSettingsToCanvas( QgsMapCanvas *canvas );

QgsCoordinateReferenceSystem defaultCrsForNewLayers() const;

QgisAppStyleSheet *mStyleSheetBuilder = nullptr;

// actions for menus and toolbars -----------------
Expand Down
8 changes: 3 additions & 5 deletions src/app/qgsnewspatialitelayerdialog.cpp
Expand Up @@ -40,7 +40,7 @@

#include <spatialite.h>

QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::WindowFlags fl )
QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::WindowFlags fl, const QgsCoordinateReferenceSystem &defaultCrs )
: QDialog( parent, fl )
{
setupUi( this );
Expand Down Expand Up @@ -73,10 +73,8 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
mOkButton->setEnabled( false );

// Set the SRID box to a default of WGS84
QgsCoordinateReferenceSystem srs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( settings.value( QStringLiteral( "Projections/layerDefaultCrs" ), GEO_EPSG_CRS_AUTHID ).toString() );
srs.validate();
mCrsId = srs.authid();
leSRID->setText( srs.authid() + " - " + srs.description() );
mCrsId = defaultCrs.authid();
leSRID->setText( defaultCrs.authid() + " - " + defaultCrs.description() );

pbnFindSRID->setEnabled( mDatabaseComboBox->count() );

Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsnewspatialitelayerdialog.h
Expand Up @@ -19,6 +19,7 @@

#include "ui_qgsnewspatialitelayerdialogbase.h"
#include "qgisgui.h"
#include "qgscoordinatereferencesystem.h"
#include "qgshelp.h"

#include "qgis.h"
Expand All @@ -35,7 +36,7 @@ class APP_EXPORT QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNew
Q_OBJECT

public:
QgsNewSpatialiteLayerDialog( QWidget *parent = nullptr, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
QgsNewSpatialiteLayerDialog( QWidget *parent = nullptr, Qt::WindowFlags fl = QgisGui::ModalDialogFlags, const QgsCoordinateReferenceSystem &defaultCrs = QgsCoordinateReferenceSystem() );
~QgsNewSpatialiteLayerDialog();

protected slots:
Expand Down
15 changes: 7 additions & 8 deletions src/gui/qgsnewgeopackagelayerdialog.cpp
Expand Up @@ -86,11 +86,6 @@ QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::W
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
mOkButton->setEnabled( false );

// Set the SRID box to a default of WGS84
QgsCoordinateReferenceSystem defaultCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( settings.value( QStringLiteral( "Projections/layerDefaultCrs" ), GEO_EPSG_CRS_AUTHID ).toString() );
defaultCrs.validate();
mCrsSelector->setCrs( defaultCrs );

connect( mFieldNameEdit, SIGNAL( textChanged( const QString & ) ), this, SLOT( fieldNameChanged( QString ) ) );
connect( mAttributeView, SIGNAL( itemSelectionChanged() ), this, SLOT( selectionChanged() ) );
connect( mTableNameEdit, SIGNAL( textChanged( const QString & ) ), this, SLOT( checkOk() ) );
Expand All @@ -108,6 +103,11 @@ QgsNewGeoPackageLayerDialog::~QgsNewGeoPackageLayerDialog()
settings.setValue( QStringLiteral( "Windows/NewGeoPackageLayer/geometry" ), saveGeometry() );
}

void QgsNewGeoPackageLayerDialog::setCrs( const QgsCoordinateReferenceSystem &crs )
{
mCrsSelector->setCrs( crs );
}

void QgsNewGeoPackageLayerDialog::on_mFieldTypeBox_currentIndexChanged( int )
{
QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
Expand Down Expand Up @@ -351,10 +351,9 @@ bool QgsNewGeoPackageLayerDialog::apply()

OGRSpatialReferenceH hSRS = nullptr;
// consider spatial reference system of the layer
int crsId = mCrsSelector->crs().srsid();
if ( wkbType != wkbNone && crsId > 0 )
QgsCoordinateReferenceSystem srs = mCrsSelector->crs();
if ( wkbType != wkbNone && srs.isValid() )
{
QgsCoordinateReferenceSystem srs = QgsCoordinateReferenceSystem::fromSrsId( crsId );
QString srsWkt = srs.toWkt();
hSRS = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
}
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsnewgeopackagelayerdialog.h
Expand Up @@ -35,6 +35,12 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
QgsNewGeoPackageLayerDialog( QWidget *parent = nullptr, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
~QgsNewGeoPackageLayerDialog();

/**
* Sets the \a crs value for the new layer in the dialog.
* @note added in QGIS 3.0
*/
void setCrs( const QgsCoordinateReferenceSystem &crs );

private slots:
void on_mAddAttributeButton_clicked();
void on_mRemoveAttributeButton_clicked();
Expand Down
13 changes: 7 additions & 6 deletions src/gui/qgsnewmemorylayerdialog.cpp
Expand Up @@ -30,9 +30,10 @@
#include <QUuid>
#include <QFileDialog>

QgsVectorLayer *QgsNewMemoryLayerDialog::runAndCreateLayer( QWidget *parent )
QgsVectorLayer *QgsNewMemoryLayerDialog::runAndCreateLayer( QWidget *parent, const QgsCoordinateReferenceSystem &defaultCrs )
{
QgsNewMemoryLayerDialog dialog( parent );
dialog.setCrs( defaultCrs );
if ( dialog.exec() == QDialog::Rejected )
{
return nullptr;
Expand Down Expand Up @@ -63,11 +64,6 @@ QgsNewMemoryLayerDialog::QgsNewMemoryLayerDialog( QWidget *parent, Qt::WindowFla
restoreGeometry( settings.value( QStringLiteral( "Windows/NewMemoryLayer/geometry" ) ).toByteArray() );

mPointRadioButton->setChecked( true );

QgsCoordinateReferenceSystem defaultCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( settings.value( QStringLiteral( "Projections/layerDefaultCrs" ), GEO_EPSG_CRS_AUTHID ).toString() );
defaultCrs.validate();
mCrsSelector->setCrs( defaultCrs );

mNameLineEdit->setText( tr( "New scratch layer" ) );
}

Expand Down Expand Up @@ -115,6 +111,11 @@ QgsWkbTypes::Type QgsNewMemoryLayerDialog::selectedType() const
return wkbType;
}

void QgsNewMemoryLayerDialog::setCrs( const QgsCoordinateReferenceSystem &crs )
{
mCrsSelector->setCrs( crs );
}

QgsCoordinateReferenceSystem QgsNewMemoryLayerDialog::crs() const
{
return mCrsSelector->crs();
Expand Down
15 changes: 13 additions & 2 deletions src/gui/qgsnewmemorylayerdialog.h
Expand Up @@ -35,17 +35,28 @@ class GUI_EXPORT QgsNewMemoryLayerDialog: public QDialog, private Ui::QgsNewMemo

/** Runs the dialoag and creates a new memory layer
* @param parent parent widget
* @param defaultCrs default layer CRS to show in dialog
* @returns new memory layer
*/
static QgsVectorLayer *runAndCreateLayer( QWidget *parent = nullptr );
static QgsVectorLayer *runAndCreateLayer( QWidget *parent = nullptr, const QgsCoordinateReferenceSystem &defaultCrs = QgsCoordinateReferenceSystem() );

QgsNewMemoryLayerDialog( QWidget *parent = nullptr, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
~QgsNewMemoryLayerDialog();

//! Returns the selected geometry type
QgsWkbTypes::Type selectedType() const;

//! Returns the selected crs
/**
* Sets the \a crs value for the new layer in the dialog.
* @note added in QGIS 3.0
* @see crs()
*/
void setCrs( const QgsCoordinateReferenceSystem &crs );

/**
* Returns the selected CRS for the new layer.
* @see setCrs()
*/
QgsCoordinateReferenceSystem crs() const;

//! Returns the layer name
Expand Down
20 changes: 10 additions & 10 deletions src/gui/qgsnewvectorlayerdialog.cpp
Expand Up @@ -84,11 +84,6 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
mOkButton = buttonBox->button( QDialogButtonBox::Ok );

mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << QStringLiteral( "id" ) << QStringLiteral( "Integer" ) << QStringLiteral( "10" ) << QLatin1String( "" ) ) );

QgsCoordinateReferenceSystem defaultCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( settings.value( QStringLiteral( "Projections/layerDefaultCrs" ), GEO_EPSG_CRS_AUTHID ).toString() );
defaultCrs.validate();
mCrsSelector->setCrs( defaultCrs );

connect( mNameEdit, SIGNAL( textChanged( QString ) ), this, SLOT( nameChanged( QString ) ) );
connect( mAttributeView, SIGNAL( itemSelectionChanged() ), this, SLOT( selectionChanged() ) );

Expand Down Expand Up @@ -165,9 +160,14 @@ QgsWkbTypes::Type QgsNewVectorLayerDialog::selectedType() const
return wkbType;
}

int QgsNewVectorLayerDialog::selectedCrsId() const
QgsCoordinateReferenceSystem QgsNewVectorLayerDialog::crs() const
{
return mCrsSelector->crs();
}

void QgsNewVectorLayerDialog::setCrs( const QgsCoordinateReferenceSystem &crs )
{
return mCrsSelector->crs().srsid();
mCrsSelector->setCrs( crs );
}

void QgsNewVectorLayerDialog::on_mAddAttributeButton_clicked()
Expand Down Expand Up @@ -231,9 +231,10 @@ void QgsNewVectorLayerDialog::selectionChanged()


// this is static
QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pEnc )
QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pEnc, const QgsCoordinateReferenceSystem &crs )
{
QgsNewVectorLayerDialog geomDialog( parent );
geomDialog.setCrs( crs );
if ( geomDialog.exec() == QDialog::Rejected )
{
return QLatin1String( "" );
Expand All @@ -242,7 +243,6 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pE
QgsWkbTypes::Type geometrytype = geomDialog.selectedType();
QString fileformat = geomDialog.selectedFileFormat();
QString enc = geomDialog.selectedFileEncoding();
int crsId = geomDialog.selectedCrsId();
QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );

QList< QPair<QString, QString> > attributes;
Expand Down Expand Up @@ -280,7 +280,7 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pE
{
if ( geometrytype != QgsWkbTypes::Unknown )
{
QgsCoordinateReferenceSystem srs = QgsCoordinateReferenceSystem::fromSrsId( crsId );
QgsCoordinateReferenceSystem srs = geomDialog.crs();
if ( !createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, srs ) )
{
return QString::null;
Expand Down
23 changes: 18 additions & 5 deletions src/gui/qgsnewvectorlayerdialog.h
Expand Up @@ -33,9 +33,11 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect

public:

// run the dialog, create the layer.
// @return fileName on success, empty string use aborted, QString::null if creation failed
static QString runAndCreateLayer( QWidget *parent = nullptr, QString *enc = nullptr );
/**
* Runs the dialog and creates a layer matching the dialog parameters.
* @returns fileName on success, empty string use aborted, QString::null if creation failed
*/
static QString runAndCreateLayer( QWidget *parent = nullptr, QString *enc = nullptr, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );

QgsNewVectorLayerDialog( QWidget *parent = nullptr, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
~QgsNewVectorLayerDialog();
Expand All @@ -47,8 +49,19 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
QString selectedFileFormat() const;
//! Returns the file format for storage
QString selectedFileEncoding() const;
//! Returns the selected crs id
int selectedCrsId() const;

/**
* Returns the selected CRS for the new layer.
* @see setCrs()
*/
QgsCoordinateReferenceSystem crs() const;

/**
* Sets the \a crs value for the new layer in the dialog.
* @note added in QGIS 3.0
* @see crs()
*/
void setCrs( const QgsCoordinateReferenceSystem &crs );

protected slots:
void on_mAddAttributeButton_clicked();
Expand Down

0 comments on commit 0500865

Please sign in to comment.