Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[processing] Fix clipping of long WKT strings for geometry parameters by using our geometry widget #59209

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
#include "qgsprocessingpointcloudexpressionlineedit.h"
#include "qgsprocessingrastercalculatorexpressionlineedit.h"
#include "qgsunittypes.h"
#include "qgsgeometrywidget.h"

#include <QToolButton>
#include <QLabel>
#include <QHBoxLayout>
Expand Down Expand Up @@ -4427,23 +4429,24 @@ QgsProcessingGeometryParameterDefinitionWidget::QgsProcessingGeometryParameterDe

vlayout->addWidget( new QLabel( tr( "Default value" ) ) );

mDefaultLineEdit = new QLineEdit();
mDefaultLineEdit->setToolTip( tr( "Geometry as WKT" ) );
mDefaultLineEdit->setPlaceholderText( tr( "Geometry as WKT" ) );
mGeometryWidget = new QgsGeometryWidget();
if ( const QgsProcessingParameterGeometry *geometryParam = dynamic_cast<const QgsProcessingParameterGeometry *>( definition ) )
{
QgsGeometry g = QgsProcessingParameters::parameterAsGeometry( geometryParam, geometryParam->defaultValueForGui(), context );
if ( !g.isNull() )
mDefaultLineEdit->setText( g.asWkt() );
{
mGeometryWidget->setGeometryValue( QgsReferencedGeometry( g, QgsCoordinateReferenceSystem() ) );
}
}

vlayout->addWidget( mDefaultLineEdit );
vlayout->addWidget( mGeometryWidget );
setLayout( vlayout );
}

QgsProcessingParameterDefinition *QgsProcessingGeometryParameterDefinitionWidget::createParameter( const QString &name, const QString &description, Qgis::ProcessingParameterFlags flags ) const
{
auto param = std::make_unique< QgsProcessingParameterGeometry >( name, description, mDefaultLineEdit->text() );
const QgsReferencedGeometry geometry = mGeometryWidget->geometryValue();
auto param = std::make_unique< QgsProcessingParameterGeometry >( name, description, geometry.isEmpty() ? QVariant() : geometry.asWkt() );
param->setFlags( flags );
return param.release();
}
Expand All @@ -4462,36 +4465,45 @@ QWidget *QgsProcessingGeometryWidgetWrapper::createWidget()
case QgsProcessingGui::Modeler:
case QgsProcessingGui::Batch:
{
mLineEdit = new QLineEdit();
mLineEdit->setToolTip( parameterDefinition()->toolTip() );
connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]
mGeometryWidget = new QgsGeometryWidget();
mGeometryWidget->setToolTip( parameterDefinition()->toolTip() );
connect( mGeometryWidget, &QgsGeometryWidget::geometryValueChanged, this, [ = ]( const QgsReferencedGeometry & )
{
emit widgetValueHasChanged( this );
} );
return mLineEdit;
return mGeometryWidget;
}
}
return nullptr;
}

void QgsProcessingGeometryWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
if ( mLineEdit )
if ( mGeometryWidget )
{
QgsGeometry g = QgsProcessingParameters::parameterAsGeometry( parameterDefinition(), value, context );
if ( !g.isNull() )
mLineEdit->setText( g.asWkt() );
{
mGeometryWidget->setGeometryValue( QgsReferencedGeometry( g, QgsCoordinateReferenceSystem() ) );
}
else
mLineEdit->clear();
{
mGeometryWidget->clearGeometry();
}
}
}

QVariant QgsProcessingGeometryWidgetWrapper::widgetValue() const
{
if ( mLineEdit )
return mLineEdit->text().isEmpty() ? QVariant() : mLineEdit->text();
if ( mGeometryWidget )
{
const QgsReferencedGeometry geometry = mGeometryWidget->geometryValue();
return geometry.isEmpty() ? QVariant() : geometry.asWkt();
}
else
{
return QVariant();
}
}

QStringList QgsProcessingGeometryWidgetWrapper::compatibleParameterTypes() const
Expand Down
5 changes: 3 additions & 2 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class QgsProcessingPointCloudExpressionLineEdit;
class QgsProcessingRasterCalculatorExpressionLineEdit;
class QgsRubberBand;
class QgsHighlightableLineEdit;
class QgsGeometryWidget;

///@cond PRIVATE

Expand Down Expand Up @@ -1238,7 +1239,7 @@ class GUI_EXPORT QgsProcessingGeometryParameterDefinitionWidget : public QgsProc

private:

QLineEdit *mDefaultLineEdit = nullptr;
QgsGeometryWidget *mGeometryWidget = nullptr;

};

Expand Down Expand Up @@ -1274,7 +1275,7 @@ class GUI_EXPORT QgsProcessingGeometryWidgetWrapper : public QgsAbstractProcessi
QString modelerExpressionFormatString() const override;
private:

QLineEdit *mLineEdit = nullptr;
QgsGeometryWidget *mGeometryWidget = nullptr;

friend class TestProcessingGui;
};
Expand Down
17 changes: 9 additions & 8 deletions tests/src/gui/testprocessinggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
#include "qgsprocessingalignrasterlayerswidgetwrapper.h"
#include "qgsprocessingrasteroptionswidgetwrapper.h"
#include "qgsrasterformatsaveoptionswidget.h"
#include "qgsgeometrywidget.h"


class TestParamType : public QgsProcessingParameterDefinition
Expand Down Expand Up @@ -6069,15 +6070,15 @@ void TestProcessingGui::testGeometryWrapper()
QgsProcessingContext context;
QWidget *w = wrapper.createWrappedWidget( context );

QSignalSpy spy( &wrapper, &QgsProcessingLayoutItemWidgetWrapper::widgetValueHasChanged );
QSignalSpy spy( &wrapper, &QgsProcessingGeometryWidgetWrapper::widgetValueHasChanged );
wrapper.setWidgetValue( QStringLiteral( "POINT (1 2)" ), context );
QCOMPARE( spy.count(), 1 );
QCOMPARE( wrapper.widgetValue().toString().toLower(), QStringLiteral( "point (1 2)" ) );
QCOMPARE( static_cast< QLineEdit * >( wrapper.wrappedWidget() )->text().toLower(), QStringLiteral( "point (1 2)" ).toLower() );
QCOMPARE( static_cast< QgsGeometryWidget * >( wrapper.wrappedWidget() )->geometryValue().asWkt().toLower(), QStringLiteral( "point (1 2)" ).toLower() );
wrapper.setWidgetValue( QString(), context );
QCOMPARE( spy.count(), 2 );
QVERIFY( wrapper.widgetValue().toString().isEmpty() );
QVERIFY( static_cast< QLineEdit * >( wrapper.wrappedWidget() )->text().isEmpty() );
QVERIFY( static_cast< QgsGeometryWidget * >( wrapper.wrappedWidget() )->geometryValue().asWkt().isEmpty() );

QLabel *l = wrapper.createWrappedLabel();
if ( wrapper.type() != QgsProcessingGui::Batch )
Expand All @@ -6093,9 +6094,9 @@ void TestProcessingGui::testGeometryWrapper()
}

// check signal
static_cast< QLineEdit * >( wrapper.wrappedWidget() )->setText( QStringLiteral( "b" ) );
static_cast< QgsGeometryWidget * >( wrapper.wrappedWidget() )->setGeometryValue( QgsReferencedGeometry( QgsGeometry::fromWkt( "point(0 0)" ), QgsCoordinateReferenceSystem() ) );
QCOMPARE( spy.count(), 3 );
static_cast< QLineEdit * >( wrapper.wrappedWidget() )->clear();
static_cast< QgsGeometryWidget * >( wrapper.wrappedWidget() )->clearGeometry();
QCOMPARE( spy.count(), 4 );

delete w;
Expand All @@ -6112,19 +6113,19 @@ void TestProcessingGui::testGeometryWrapper()
wrapper2.setWidgetValue( "POINT (1 2)", context );
QCOMPARE( spy2.count(), 1 );
QCOMPARE( wrapper2.widgetValue().toString().toLower(), QStringLiteral( "point (1 2)" ) );
QCOMPARE( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text().toLower(), QStringLiteral( "point (1 2)" ) );
QCOMPARE( static_cast< QgsGeometryWidget * >( wrapper2.wrappedWidget() )->geometryValue().asWkt().toLower(), QStringLiteral( "point (1 2)" ) );

wrapper2.setWidgetValue( QVariant(), context );
QCOMPARE( spy2.count(), 2 );
QVERIFY( !wrapper2.widgetValue().isValid() );
QVERIFY( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text().isEmpty() );
QVERIFY( static_cast< QgsGeometryWidget * >( wrapper2.wrappedWidget() )->geometryValue().asWkt().isEmpty() );

wrapper2.setWidgetValue( "POINT (1 3)", context );
QCOMPARE( spy2.count(), 3 );
wrapper2.setWidgetValue( "", context );
QCOMPARE( spy2.count(), 4 );
QVERIFY( !wrapper2.widgetValue().isValid() );
QVERIFY( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text().isEmpty() );
QVERIFY( static_cast< QgsGeometryWidget * >( wrapper2.wrappedWidget() )->geometryValue().asWkt().isEmpty() );

delete w;
};
Expand Down
Loading