Skip to content
Permalink
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
  • Loading branch information
nyalldawson committed Dec 16, 2019
1 parent c2b55eb commit aa0b9b05470d710b2c40b6ac5270b1a1e54f8534
@@ -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() );
}

#if PROJ_VERSION_MAJOR>=6
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 );
}

#endif

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

#if PROJ_VERSION_MAJOR>=6
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." ) );
}
else
{
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;

#endif
}

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

private:

@@ -55,7 +55,7 @@
<property name="title">
<string>Define</string>
</property>
<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 @@
</layout>
</item>
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPlainTextEdit" name="teParameters">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<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>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>70</height>
</size>
<property name="text">
<string>&amp;Validate</string>
</property>
</widget>
</item>
<item>
<item row="0" column="1">
<widget class="QPushButton" name="pbnCopyCRS">
<property name="toolTip">
<string>Copy parameters from existing CRS</string>
@@ -178,6 +172,22 @@
</property>
</widget>
</item>
<item row="1" column="2">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0" rowspan="2">
<widget class="QPlainTextEdit" name="teParameters"/>
</item>
</layout>
</item>
<item row="2" column="0">
@@ -208,7 +218,7 @@
<property name="title">
<string>Test</string>
</property>
<property name="collapsed">
<property name="collapsed" stdset="0">
<bool>true</bool>
</property>
<layout class="QGridLayout">
@@ -333,6 +343,7 @@
<tabstop>leName</tabstop>
<tabstop>teParameters</tabstop>
<tabstop>pbnCopyCRS</tabstop>
<tabstop>mButtonValidate</tabstop>
<tabstop>northWGS84</tabstop>
<tabstop>eastWGS84</tabstop>
<tabstop>pbnCalculate</tabstop>

0 comments on commit aa0b9b0

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