Skip to content
Browse files

[needs-docs] Add a "Validate" button to custom projection dialog

to allow users to validate the currently edited CRS definition
without having to accept the whole dialog

If the validation fails, the proj error messages are shown
to help the user determine why the string is invalid

(cherry picked from commit aa0b9b0)
  • Loading branch information
nyalldawson committed Dec 16, 2019
1 parent d81e0af commit 16fbc0c7f45f1151f57607189766eaf104822cc8
@@ -58,6 +58,7 @@ QgsCustomProjectionDialog::QgsCustomProjectionDialog( QWidget *parent, Qt::Windo
connect( leNameList, &QTreeWidget::currentItemChanged, this, &QgsCustomProjectionDialog::leNameList_currentItemChanged );
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsCustomProjectionDialog::buttonBox_accepted );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsCustomProjectionDialog::showHelp );
connect( mButtonValidate, &QPushButton::clicked, this, &QgsCustomProjectionDialog::validateCurrent );

leNameList->setSelectionMode( QAbstractItemView::ExtendedSelection );

@@ -462,6 +463,48 @@ void QgsCustomProjectionDialog::updateListFromCurrentItem()
item->setText( QgisCrsParametersColumn, teParameters->toPlainText() );

static void proj_collecting_logger( void *user_data, int /*level*/, const char *message )
QStringList *dest = reinterpret_cast< QStringList * >( user_data );
QString messageString( message );
messageString.replace( QStringLiteral( "internal_proj_create: " ), QString() );
dest->append( messageString );


void QgsCustomProjectionDialog::validateCurrent()
const QString projDef = teParameters->toPlainText();

PJ_CONTEXT *context = proj_context_create();

QStringList projErrors;
proj_log_func( context, &projErrors, proj_collecting_logger );

const QString projCrsString = projDef + ( projDef.contains( QStringLiteral( "+type=crs" ) ) ? QString() : QStringLiteral( " +type=crs" ) );
QgsProjUtils::proj_pj_unique_ptr crs( proj_create( context, projCrsString.toLatin1().constData() ) );
if ( crs )
QMessageBox::information( this, tr( "Custom Coordinate Reference System" ),
tr( "This proj projection definition is valid." ) );
QMessageBox::warning( this, tr( "Custom Coordinate Reference System" ),
tr( "This proj projection definition is not valid:" ) + QStringLiteral( "\n\n" ) + projErrors.join( '\n' ) );

// reset logger to terminal output
proj_log_func( context, nullptr, nullptr );
proj_context_destroy( context );
context = nullptr;


void QgsCustomProjectionDialog::pbnCalculate_clicked()
// We must check the prj def is valid!
@@ -45,6 +45,7 @@ class APP_EXPORT QgsCustomProjectionDialog : public QDialog, private Ui::QgsCust
private slots:

void updateListFromCurrentItem();
void validateCurrent();


@@ -55,7 +55,7 @@
<property name="title">
<layout class="QGridLayout" name="gridLayout_3">
<layout class="QGridLayout" name="gridLayout_3" rowstretch="0,3,0,1">
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
@@ -147,24 +147,18 @@
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<widget class="QPlainTextEdit" name="teParameters">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="2">
<widget class="QPushButton" name="mButtonValidate">
<property name="toolTip">
<string>Validate the current CRS definition and test whether it is an acceptable projection definition</string>
<property name="maximumSize">
<property name="text">
<item row="0" column="1">
<widget class="QPushButton" name="pbnCopyCRS">
<property name="toolTip">
<string>Copy parameters from existing CRS</string>
@@ -178,6 +172,22 @@
<item row="1" column="2">
<spacer name="verticalSpacer_2">
<property name="orientation">
<property name="sizeHint" stdset="0">
<item row="0" column="0" rowspan="2">
<widget class="QPlainTextEdit" name="teParameters"/>
<item row="2" column="0">
@@ -208,7 +218,7 @@
<property name="title">
<property name="collapsed">
<property name="collapsed" stdset="0">
<layout class="QGridLayout">
@@ -333,6 +343,7 @@

0 comments on commit 16fbc0c

Please sign in to comment.
You can’t perform that action at this time.