Skip to content
Permalink
Browse files
Set default layer CRS according to behaviour radio (fix #11889)
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 0500865b3f6b7599985e428cb235826910e71c81
@@ -11,4 +11,5 @@ class QgsNewGeoPackageLayerDialog : QDialog

~QgsNewGeoPackageLayerDialog();

void setCrs( const QgsCoordinateReferenceSystem &crs );
};
@@ -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*/
@@ -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();
@@ -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() );
@@ -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;
@@ -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() )
{
@@ -5026,7 +5047,7 @@ void QgisApp::newVectorLayer()

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

if ( newLayer )
{
@@ -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();
}

@@ -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 -----------------
@@ -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 );
@@ -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() );

@@ -19,6 +19,7 @@

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

#include "qgis.h"
@@ -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:
@@ -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() ) );
@@ -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();
@@ -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() );
}
@@ -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();
@@ -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;
@@ -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" ) );
}

@@ -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();
@@ -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
@@ -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() ) );

@@ -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()
@@ -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( "" );
@@ -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;
@@ -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;
@@ -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();
@@ -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();

0 comments on commit 0500865

Please sign in to comment.