Skip to content

Commit

Permalink
[needs-docs] Add a "Validate" button to custom projection dialog
Browse files Browse the repository at this point in the history
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 20, 2019
1 parent d81e0af commit 16fbc0c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
43 changes: 43 additions & 0 deletions src/app/qgscustomprojectiondialog.cpp
Expand Up @@ -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 );

Expand Down Expand Up @@ -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!
Expand Down
1 change: 1 addition & 0 deletions src/app/qgscustomprojectiondialog.h
Expand Up @@ -45,6 +45,7 @@ class APP_EXPORT QgsCustomProjectionDialog : public QDialog, private Ui::QgsCust
private slots:

void updateListFromCurrentItem();
void validateCurrent();

private:

Expand Down
43 changes: 27 additions & 16 deletions src/ui/qgscustomprojectiondialogbase.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand All @@ -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">
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 16fbc0c

Please sign in to comment.