diff --git a/src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp b/src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp index c793ecf1de6d..91006795a001 100644 --- a/src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp +++ b/src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp @@ -68,6 +68,8 @@ #include "qgsprocessingpointcloudexpressionlineedit.h" #include "qgsprocessingrastercalculatorexpressionlineedit.h" #include "qgsunittypes.h" +#include "qgsgeometrywidget.h" + #include #include #include @@ -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( 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(); } @@ -4462,13 +4465,13 @@ 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; @@ -4476,22 +4479,31 @@ QWidget *QgsProcessingGeometryWidgetWrapper::createWidget() 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 diff --git a/src/gui/processing/qgsprocessingwidgetwrapperimpl.h b/src/gui/processing/qgsprocessingwidgetwrapperimpl.h index bb3052e2152a..db31a36cf34f 100644 --- a/src/gui/processing/qgsprocessingwidgetwrapperimpl.h +++ b/src/gui/processing/qgsprocessingwidgetwrapperimpl.h @@ -75,6 +75,7 @@ class QgsProcessingPointCloudExpressionLineEdit; class QgsProcessingRasterCalculatorExpressionLineEdit; class QgsRubberBand; class QgsHighlightableLineEdit; +class QgsGeometryWidget; ///@cond PRIVATE @@ -1238,7 +1239,7 @@ class GUI_EXPORT QgsProcessingGeometryParameterDefinitionWidget : public QgsProc private: - QLineEdit *mDefaultLineEdit = nullptr; + QgsGeometryWidget *mGeometryWidget = nullptr; }; @@ -1274,7 +1275,7 @@ class GUI_EXPORT QgsProcessingGeometryWidgetWrapper : public QgsAbstractProcessi QString modelerExpressionFormatString() const override; private: - QLineEdit *mLineEdit = nullptr; + QgsGeometryWidget *mGeometryWidget = nullptr; friend class TestProcessingGui; }; diff --git a/tests/src/gui/testprocessinggui.cpp b/tests/src/gui/testprocessinggui.cpp index 262291feb431..ad5dbdcc9d0c 100644 --- a/tests/src/gui/testprocessinggui.cpp +++ b/tests/src/gui/testprocessinggui.cpp @@ -105,6 +105,7 @@ #include "qgsprocessingalignrasterlayerswidgetwrapper.h" #include "qgsprocessingrasteroptionswidgetwrapper.h" #include "qgsrasterformatsaveoptionswidget.h" +#include "qgsgeometrywidget.h" class TestParamType : public QgsProcessingParameterDefinition @@ -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 ) @@ -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; @@ -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; };