mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
[processing] Port point widget wrapper to new API
And switch to a snapping canvas tool when selecting points from canvas
This commit is contained in:
parent
37faa0d883
commit
719a225bca
@ -875,6 +875,15 @@ If ``crs`` is set then the point will be automatically reprojected so that it is
|
||||
Returns the coordinate reference system associated with an point parameter value.
|
||||
|
||||
.. seealso:: :py:func:`parameterAsPoint`
|
||||
%End
|
||||
|
||||
static QgsCoordinateReferenceSystem parameterAsPointCrs( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context );
|
||||
%Docstring
|
||||
Returns the coordinate reference system associated with an point parameter value.
|
||||
|
||||
.. seealso:: :py:func:`parameterAsPoint`
|
||||
|
||||
.. versionadded:: 3.8
|
||||
%End
|
||||
|
||||
static QString parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context );
|
||||
|
@ -119,6 +119,13 @@ Sets the current ``value`` for the parameter.
|
||||
Returns the current value of the parameter.
|
||||
|
||||
.. seealso:: :py:func:`setWidgetValue`
|
||||
%End
|
||||
|
||||
void setDialog( QDialog *dialog );
|
||||
%Docstring
|
||||
Sets the parent ``dialog`` in which the widget is shown.
|
||||
|
||||
.. versionadded:: 3.8
|
||||
%End
|
||||
|
||||
virtual QgsExpressionContext createExpressionContext() const;
|
||||
|
@ -263,6 +263,13 @@ allowing the wrapper to connect to the wrappers of other, related parameters.
|
||||
virtual QgsExpressionContext createExpressionContext() const;
|
||||
|
||||
|
||||
virtual void setDialog( QDialog *dialog );
|
||||
%Docstring
|
||||
Sets the parent ``dialog`` in which the wrapper is shown.
|
||||
|
||||
.. versionadded:: 3.8
|
||||
%End
|
||||
|
||||
signals:
|
||||
|
||||
|
||||
|
@ -1848,6 +1848,7 @@ class WidgetWrapperFactory:
|
||||
QgsProcessingGui.Standard)
|
||||
wrapper = QgsGui.processingGuiRegistry().createParameterWidgetWrapper(param, dialog_type)
|
||||
if wrapper is not None:
|
||||
wrapper.setDialog(dialog)
|
||||
return wrapper
|
||||
|
||||
# fallback to Python registry
|
||||
|
@ -1189,10 +1189,14 @@ QgsPointXY QgsProcessingParameters::parameterAsPoint( const QgsProcessingParamet
|
||||
QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsPointCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context )
|
||||
{
|
||||
QVariant val = parameters.value( definition->name() );
|
||||
return parameterAsPointCrs( definition, val, context );
|
||||
}
|
||||
|
||||
if ( val.canConvert< QgsReferencedPointXY >() )
|
||||
QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsPointCrs( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context )
|
||||
{
|
||||
if ( value.canConvert< QgsReferencedPointXY >() )
|
||||
{
|
||||
QgsReferencedPointXY rr = val.value<QgsReferencedPointXY>();
|
||||
QgsReferencedPointXY rr = value.value<QgsReferencedPointXY>();
|
||||
if ( rr.crs().isValid() )
|
||||
{
|
||||
return rr.crs();
|
||||
@ -1201,7 +1205,7 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsPointCrs( const
|
||||
|
||||
QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
|
||||
|
||||
QString valueAsString = parameterAsString( definition, parameters, context );
|
||||
QString valueAsString = parameterAsString( definition, value, context );
|
||||
QRegularExpressionMatch match = rx.match( valueAsString );
|
||||
if ( match.hasMatch() )
|
||||
{
|
||||
|
@ -936,6 +936,14 @@ class CORE_EXPORT QgsProcessingParameters
|
||||
*/
|
||||
static QgsCoordinateReferenceSystem parameterAsPointCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context );
|
||||
|
||||
/**
|
||||
* Returns the coordinate reference system associated with an point parameter value.
|
||||
*
|
||||
* \see parameterAsPoint()
|
||||
* \since QGIS 3.8
|
||||
*/
|
||||
static QgsCoordinateReferenceSystem parameterAsPointCrs( const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context );
|
||||
|
||||
/**
|
||||
* Evaluates the parameter with matching \a definition to a file/folder name.
|
||||
*/
|
||||
|
@ -41,6 +41,7 @@ QgsProcessingGuiRegistry::QgsProcessingGuiRegistry()
|
||||
addParameterWidgetFactory( new QgsProcessingEnumWidgetWrapper() );
|
||||
addParameterWidgetFactory( new QgsProcessingLayoutWidgetWrapper() );
|
||||
addParameterWidgetFactory( new QgsProcessingLayoutItemWidgetWrapper() );
|
||||
addParameterWidgetFactory( new QgsProcessingPointWidgetWrapper() );
|
||||
}
|
||||
|
||||
QgsProcessingGuiRegistry::~QgsProcessingGuiRegistry()
|
||||
|
@ -180,6 +180,12 @@ QgsProcessingModelChildParameterSource QgsProcessingModelerParameterWidget::valu
|
||||
return QgsProcessingModelChildParameterSource();
|
||||
}
|
||||
|
||||
void QgsProcessingModelerParameterWidget::setDialog( QDialog *dialog )
|
||||
{
|
||||
if ( mStaticWidgetWrapper )
|
||||
mStaticWidgetWrapper->setDialog( dialog );
|
||||
}
|
||||
|
||||
QgsExpressionContext QgsProcessingModelerParameterWidget::createExpressionContext() const
|
||||
{
|
||||
QgsExpressionContext c = mContext.expressionContext();
|
||||
|
@ -148,6 +148,13 @@ class GUI_EXPORT QgsProcessingModelerParameterWidget : public QWidget, public Qg
|
||||
*/
|
||||
virtual QgsProcessingModelChildParameterSource value() const;
|
||||
|
||||
/**
|
||||
* Sets the parent \a dialog in which the widget is shown.
|
||||
*
|
||||
* \since QGIS 3.8
|
||||
*/
|
||||
void setDialog( QDialog *dialog );
|
||||
|
||||
QgsExpressionContext createExpressionContext() const override;
|
||||
|
||||
private slots:
|
||||
|
@ -238,6 +238,11 @@ QgsExpressionContext QgsAbstractProcessingParameterWidgetWrapper::createExpressi
|
||||
return QgsProcessingGuiUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
|
||||
}
|
||||
|
||||
void QgsAbstractProcessingParameterWidgetWrapper::setDialog( QDialog * )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged( QgsAbstractProcessingParameterWidgetWrapper *wrapper )
|
||||
{
|
||||
if ( wrapper )
|
||||
|
@ -303,6 +303,13 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject, p
|
||||
|
||||
QgsExpressionContext createExpressionContext() const override;
|
||||
|
||||
/**
|
||||
* Sets the parent \a dialog in which the wrapper is shown.
|
||||
*
|
||||
* \since QGIS 3.8
|
||||
*/
|
||||
virtual void setDialog( QDialog *dialog );
|
||||
|
||||
signals:
|
||||
|
||||
// TODO QGIS 4.0 - remove wrapper parameter - this is kept for compatibility with 3.x API,
|
||||
|
@ -36,6 +36,11 @@
|
||||
#include "qgslayoutitemcombobox.h"
|
||||
#include "qgsprintlayout.h"
|
||||
#include "qgsscalewidget.h"
|
||||
#include "qgssnapindicator.h"
|
||||
#include "qgsmapmouseevent.h"
|
||||
#include "qgsfilterlineedit.h"
|
||||
#include "qgsmapcanvas.h"
|
||||
#include <QToolButton>
|
||||
#include <QLabel>
|
||||
#include <QHBoxLayout>
|
||||
#include <QCheckBox>
|
||||
@ -2101,5 +2106,284 @@ QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingLayoutItemWidgetWrappe
|
||||
return new QgsProcessingLayoutItemWidgetWrapper( parameter, type );
|
||||
}
|
||||
|
||||
///@endcond PRIVATE
|
||||
//
|
||||
// QgsProcessingPointMapTool
|
||||
//
|
||||
|
||||
QgsProcessingPointMapTool::QgsProcessingPointMapTool( QgsMapCanvas *canvas )
|
||||
: QgsMapTool( canvas )
|
||||
{
|
||||
setCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::CapturePoint ) );
|
||||
mSnapIndicator.reset( new QgsSnapIndicator( canvas ) );
|
||||
}
|
||||
|
||||
QgsProcessingPointMapTool::~QgsProcessingPointMapTool() = default;
|
||||
|
||||
void QgsProcessingPointMapTool::deactivate()
|
||||
{
|
||||
mSnapIndicator->setMatch( QgsPointLocator::Match() );
|
||||
QgsMapTool::deactivate();
|
||||
}
|
||||
|
||||
void QgsProcessingPointMapTool::canvasMoveEvent( QgsMapMouseEvent *e )
|
||||
{
|
||||
e->snapPoint();
|
||||
mSnapIndicator->setMatch( e->mapPointMatch() );
|
||||
}
|
||||
|
||||
void QgsProcessingPointMapTool::canvasPressEvent( QgsMapMouseEvent *e )
|
||||
{
|
||||
if ( e->button() == Qt::LeftButton )
|
||||
{
|
||||
QgsPointXY point = e->snapPoint();
|
||||
emit clicked( point );
|
||||
emit complete();
|
||||
}
|
||||
}
|
||||
|
||||
void QgsProcessingPointMapTool::keyPressEvent( QKeyEvent *e )
|
||||
{
|
||||
if ( e->key() == Qt::Key_Escape )
|
||||
{
|
||||
|
||||
// Override default shortcut management in MapCanvas
|
||||
e->ignore();
|
||||
emit complete();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//
|
||||
// QgsProcessingPointPanel
|
||||
//
|
||||
|
||||
QgsProcessingPointPanel::QgsProcessingPointPanel( QWidget *parent )
|
||||
: QWidget( parent )
|
||||
{
|
||||
QHBoxLayout *l = new QHBoxLayout();
|
||||
l->setContentsMargins( 0, 0, 0, 0 );
|
||||
l->setMargin( 0 );
|
||||
mLineEdit = new QgsFilterLineEdit( );
|
||||
mLineEdit->setShowClearButton( false );
|
||||
l->addWidget( mLineEdit, 1 );
|
||||
mButton = new QToolButton();
|
||||
mButton->setText( QStringLiteral( "…" ) );
|
||||
l->addWidget( mButton );
|
||||
setLayout( l );
|
||||
|
||||
connect( mLineEdit, &QLineEdit::textChanged, this, &QgsProcessingPointPanel::changed );
|
||||
connect( mButton, &QToolButton::clicked, this, &QgsProcessingPointPanel::selectOnCanvas );
|
||||
mButton->setVisible( false );
|
||||
}
|
||||
|
||||
void QgsProcessingPointPanel::setMapCanvas( QgsMapCanvas *canvas )
|
||||
{
|
||||
mCanvas = canvas;
|
||||
mButton->setVisible( true );
|
||||
|
||||
mCrs = canvas->mapSettings().destinationCrs();
|
||||
mTool = qgis::make_unique< QgsProcessingPointMapTool >( mCanvas );
|
||||
connect( mTool.get(), &QgsProcessingPointMapTool::clicked, this, &QgsProcessingPointPanel::updatePoint );
|
||||
connect( mTool.get(), &QgsProcessingPointMapTool::complete, this, &QgsProcessingPointPanel::pointPicked );
|
||||
}
|
||||
|
||||
void QgsProcessingPointPanel::setAllowNull( bool allowNull )
|
||||
{
|
||||
mLineEdit->setShowClearButton( allowNull );
|
||||
}
|
||||
|
||||
QVariant QgsProcessingPointPanel::value() const
|
||||
{
|
||||
return mLineEdit->showClearButton() && mLineEdit->text().trimmed().isEmpty() ? QVariant() : QVariant( mLineEdit->text() );
|
||||
}
|
||||
|
||||
void QgsProcessingPointPanel::clear()
|
||||
{
|
||||
mLineEdit->clear();
|
||||
}
|
||||
|
||||
void QgsProcessingPointPanel::setValue( const QgsPointXY &point, const QgsCoordinateReferenceSystem &crs )
|
||||
{
|
||||
QString newText = QStringLiteral( "%1,%2" ).arg( point.x() ).arg( point.y() );
|
||||
mCrs = crs;
|
||||
if ( mCrs.isValid() )
|
||||
{
|
||||
newText += QStringLiteral( " [%1]" ).arg( mCrs.authid() );
|
||||
}
|
||||
mLineEdit->setText( newText );
|
||||
}
|
||||
|
||||
void QgsProcessingPointPanel::selectOnCanvas()
|
||||
{
|
||||
if ( !mCanvas )
|
||||
return;
|
||||
|
||||
mPrevTool = mCanvas->mapTool();
|
||||
mCanvas->setMapTool( mTool.get() );
|
||||
|
||||
emit toggleDialogVisibility( false );
|
||||
}
|
||||
|
||||
void QgsProcessingPointPanel::updatePoint( const QgsPointXY &point )
|
||||
{
|
||||
setValue( point, mCanvas->mapSettings().destinationCrs() );
|
||||
}
|
||||
|
||||
void QgsProcessingPointPanel::pointPicked()
|
||||
{
|
||||
if ( !mCanvas )
|
||||
return;
|
||||
|
||||
mCanvas->setMapTool( mPrevTool );
|
||||
|
||||
emit toggleDialogVisibility( true );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// QgsProcessingPointWidgetWrapper
|
||||
//
|
||||
|
||||
QgsProcessingPointWidgetWrapper::QgsProcessingPointWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
|
||||
: QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QWidget *QgsProcessingPointWidgetWrapper::createWidget()
|
||||
{
|
||||
const QgsProcessingParameterPoint *pointParam = dynamic_cast< const QgsProcessingParameterPoint *>( parameterDefinition() );
|
||||
switch ( type() )
|
||||
{
|
||||
case QgsProcessingGui::Standard:
|
||||
case QgsProcessingGui::Batch:
|
||||
{
|
||||
mPanel = new QgsProcessingPointPanel( nullptr );
|
||||
if ( widgetContext().mapCanvas() )
|
||||
mPanel->setMapCanvas( widgetContext().mapCanvas() );
|
||||
|
||||
if ( pointParam->flags() & QgsProcessingParameterDefinition::FlagOptional )
|
||||
mPanel->setAllowNull( true );
|
||||
|
||||
mPanel->setToolTip( parameterDefinition()->toolTip() );
|
||||
|
||||
connect( mPanel, &QgsProcessingPointPanel::changed, this, [ = ]
|
||||
{
|
||||
emit widgetValueHasChanged( this );
|
||||
} );
|
||||
|
||||
if ( mDialog )
|
||||
setDialog( mDialog ); // setup connections to panel - dialog was previously set before the widget was created
|
||||
return mPanel;
|
||||
}
|
||||
|
||||
case QgsProcessingGui::Modeler:
|
||||
{
|
||||
mLineEdit = new QLineEdit();
|
||||
mLineEdit->setToolTip( tr( "Point as 'x,y'" ) );
|
||||
connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & )
|
||||
{
|
||||
emit widgetValueHasChanged( this );
|
||||
} );
|
||||
return mLineEdit;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void QgsProcessingPointWidgetWrapper::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
|
||||
{
|
||||
QgsAbstractProcessingParameterWidgetWrapper::setWidgetContext( context );
|
||||
if ( mPanel && context.mapCanvas() )
|
||||
mPanel->setMapCanvas( context.mapCanvas() );
|
||||
}
|
||||
|
||||
void QgsProcessingPointWidgetWrapper::setDialog( QDialog *dialog )
|
||||
{
|
||||
mDialog = dialog;
|
||||
if ( mPanel )
|
||||
{
|
||||
connect( mPanel, &QgsProcessingPointPanel::toggleDialogVisibility, mDialog, [ = ]( bool visible )
|
||||
{
|
||||
if ( !visible )
|
||||
mDialog->showMinimized();
|
||||
else
|
||||
{
|
||||
mDialog->showNormal();
|
||||
mDialog->raise();
|
||||
mDialog->activateWindow();
|
||||
}
|
||||
} );
|
||||
}
|
||||
QgsAbstractProcessingParameterWidgetWrapper::setDialog( dialog );
|
||||
}
|
||||
|
||||
void QgsProcessingPointWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
|
||||
{
|
||||
if ( mPanel )
|
||||
{
|
||||
if ( !value.isValid() || ( value.type() == QVariant::String && value.toString().isEmpty() ) )
|
||||
mPanel->clear();
|
||||
else
|
||||
{
|
||||
QgsPointXY p = QgsProcessingParameters::parameterAsPoint( parameterDefinition(), value, context );
|
||||
QgsCoordinateReferenceSystem crs = QgsProcessingParameters::parameterAsPointCrs( parameterDefinition(), value, context );
|
||||
mPanel->setValue( p, crs );
|
||||
}
|
||||
}
|
||||
else if ( mLineEdit )
|
||||
{
|
||||
const QString v = QgsProcessingParameters::parameterAsString( parameterDefinition(), value, context );
|
||||
mLineEdit->setText( v );
|
||||
}
|
||||
}
|
||||
|
||||
QVariant QgsProcessingPointWidgetWrapper::widgetValue() const
|
||||
{
|
||||
if ( mPanel )
|
||||
{
|
||||
return mPanel->value();
|
||||
}
|
||||
else if ( mLineEdit )
|
||||
return mLineEdit->text().isEmpty() ? QVariant() : mLineEdit->text();
|
||||
else
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QStringList QgsProcessingPointWidgetWrapper::compatibleParameterTypes() const
|
||||
{
|
||||
return QStringList()
|
||||
<< QgsProcessingParameterPoint::typeName()
|
||||
<< QgsProcessingParameterString::typeName();
|
||||
}
|
||||
|
||||
QStringList QgsProcessingPointWidgetWrapper::compatibleOutputTypes() const
|
||||
{
|
||||
return QStringList()
|
||||
<< QgsProcessingOutputString::typeName();
|
||||
}
|
||||
|
||||
QList<int> QgsProcessingPointWidgetWrapper::compatibleDataTypes() const
|
||||
{
|
||||
return QList<int>();
|
||||
}
|
||||
|
||||
QString QgsProcessingPointWidgetWrapper::modelerExpressionFormatString() const
|
||||
{
|
||||
return tr( "string of the format 'x,y'" );
|
||||
}
|
||||
|
||||
QString QgsProcessingPointWidgetWrapper::parameterType() const
|
||||
{
|
||||
return QgsProcessingParameterPoint::typeName();
|
||||
}
|
||||
|
||||
QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingPointWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
|
||||
{
|
||||
return new QgsProcessingPointWidgetWrapper( parameter, type );
|
||||
}
|
||||
|
||||
///@endcond PRIVATE
|
||||
|
@ -21,6 +21,8 @@
|
||||
|
||||
#define SIP_NO_FILE
|
||||
#include "qgsprocessingwidgetwrapper.h"
|
||||
#include "qgsmaptool.h"
|
||||
|
||||
#include <QAbstractButton>
|
||||
|
||||
class QCheckBox;
|
||||
@ -42,6 +44,8 @@ class QgsLayoutComboBox;
|
||||
class QgsLayoutItemComboBox;
|
||||
class QgsPrintLayout;
|
||||
class QgsScaleWidget;
|
||||
class QgsSnapIndicator;
|
||||
class QgsFilterLineEdit;
|
||||
|
||||
///@cond PRIVATE
|
||||
|
||||
@ -616,6 +620,102 @@ class GUI_EXPORT QgsProcessingLayoutItemWidgetWrapper : public QgsAbstractProces
|
||||
friend class TestProcessingGui;
|
||||
};
|
||||
|
||||
class GUI_EXPORT QgsProcessingPointMapTool : public QgsMapTool
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
QgsProcessingPointMapTool( QgsMapCanvas *canvas );
|
||||
~QgsProcessingPointMapTool() override;
|
||||
void deactivate() override;
|
||||
void canvasMoveEvent( QgsMapMouseEvent *e ) override;
|
||||
void canvasPressEvent( QgsMapMouseEvent *e ) override;
|
||||
void keyPressEvent( QKeyEvent *e ) override;
|
||||
|
||||
signals:
|
||||
|
||||
void clicked( const QgsPointXY &point );
|
||||
void complete();
|
||||
|
||||
private:
|
||||
|
||||
std::unique_ptr<QgsSnapIndicator> mSnapIndicator;
|
||||
friend class TestProcessingGui;
|
||||
};
|
||||
|
||||
class GUI_EXPORT QgsProcessingPointPanel : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
QgsProcessingPointPanel( QWidget *parent );
|
||||
void setMapCanvas( QgsMapCanvas *canvas );
|
||||
void setAllowNull( bool allowNull );
|
||||
|
||||
QVariant value() const;
|
||||
void clear();
|
||||
void setValue( const QgsPointXY &point, const QgsCoordinateReferenceSystem &crs );
|
||||
|
||||
signals:
|
||||
|
||||
void toggleDialogVisibility( bool visible );
|
||||
void changed();
|
||||
|
||||
private slots:
|
||||
|
||||
void selectOnCanvas();
|
||||
void updatePoint( const QgsPointXY &point );
|
||||
void pointPicked();
|
||||
|
||||
private:
|
||||
|
||||
QgsFilterLineEdit *mLineEdit = nullptr;
|
||||
QToolButton *mButton = nullptr;
|
||||
QgsMapCanvas *mCanvas = nullptr;
|
||||
QgsCoordinateReferenceSystem mCrs;
|
||||
QPointer< QgsMapTool > mPrevTool;
|
||||
std::unique_ptr< QgsProcessingPointMapTool > mTool;
|
||||
friend class TestProcessingGui;
|
||||
};
|
||||
|
||||
|
||||
class GUI_EXPORT QgsProcessingPointWidgetWrapper : public QgsAbstractProcessingParameterWidgetWrapper, public QgsProcessingParameterWidgetFactoryInterface
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
QgsProcessingPointWidgetWrapper( const QgsProcessingParameterDefinition *parameter = nullptr,
|
||||
QgsProcessingGui::WidgetType type = QgsProcessingGui::Standard, QWidget *parent = nullptr );
|
||||
|
||||
// QgsProcessingParameterWidgetFactoryInterface
|
||||
QString parameterType() const override;
|
||||
QgsAbstractProcessingParameterWidgetWrapper *createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type ) override;
|
||||
|
||||
// QgsProcessingParameterWidgetWrapper interface
|
||||
QWidget *createWidget() override SIP_FACTORY;
|
||||
void setWidgetContext( const QgsProcessingParameterWidgetContext &context ) override;
|
||||
void setDialog( QDialog *dialog ) override;
|
||||
|
||||
protected:
|
||||
|
||||
void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
|
||||
QVariant widgetValue() const override;
|
||||
|
||||
QStringList compatibleParameterTypes() const override;
|
||||
|
||||
QStringList compatibleOutputTypes() const override;
|
||||
|
||||
QList< int > compatibleDataTypes() const override;
|
||||
QString modelerExpressionFormatString() const override;
|
||||
private:
|
||||
|
||||
QgsProcessingPointPanel *mPanel = nullptr;
|
||||
QLineEdit *mLineEdit = nullptr;
|
||||
QDialog *mDialog = nullptr;
|
||||
|
||||
friend class TestProcessingGui;
|
||||
};
|
||||
|
||||
///@endcond PRIVATE
|
||||
|
||||
|
@ -179,6 +179,8 @@ class TestProcessingGui : public QObject
|
||||
void testEnumWrapper();
|
||||
void testLayoutWrapper();
|
||||
void testLayoutItemWrapper();
|
||||
void testPointPanel();
|
||||
void testPointWrapper();
|
||||
|
||||
private:
|
||||
|
||||
@ -2820,6 +2822,185 @@ void TestProcessingGui::testLayoutItemWrapper()
|
||||
|
||||
}
|
||||
|
||||
void TestProcessingGui::testPointPanel()
|
||||
{
|
||||
std::unique_ptr< QgsProcessingPointPanel > panel = qgis::make_unique< QgsProcessingPointPanel >( nullptr );
|
||||
QSignalSpy spy( panel.get(), &QgsProcessingPointPanel::changed );
|
||||
|
||||
panel->setValue( QgsPointXY( 100, 150 ), QgsCoordinateReferenceSystem() );
|
||||
QCOMPARE( panel->value().toString(), QStringLiteral( "100,150" ) );
|
||||
QCOMPARE( spy.count(), 1 );
|
||||
|
||||
panel->setValue( QgsPointXY( 200, 250 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) );
|
||||
QCOMPARE( panel->value().toString(), QStringLiteral( "200,250 [EPSG:3111]" ) );
|
||||
QCOMPARE( spy.count(), 2 );
|
||||
|
||||
QVERIFY( !panel->mLineEdit->showClearButton() );
|
||||
panel->setAllowNull( true );
|
||||
QVERIFY( panel->mLineEdit->showClearButton() );
|
||||
panel->clear();
|
||||
QVERIFY( !panel->value().isValid() );
|
||||
QCOMPARE( spy.count(), 3 );
|
||||
|
||||
QgsMapCanvas canvas;
|
||||
canvas.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:28356" ) ) );
|
||||
panel->setMapCanvas( &canvas );
|
||||
panel->updatePoint( QgsPointXY( 1.5, -3.5 ) );
|
||||
QCOMPARE( panel->value().toString(), QStringLiteral( "1.5,-3.5 [EPSG:28356]" ) );
|
||||
QCOMPARE( spy.count(), 4 );
|
||||
|
||||
panel.reset();
|
||||
}
|
||||
|
||||
void TestProcessingGui::testPointWrapper()
|
||||
{
|
||||
auto testWrapper = []( QgsProcessingGui::WidgetType type )
|
||||
{
|
||||
// non optional
|
||||
QgsProcessingParameterPoint param( QStringLiteral( "point" ), QStringLiteral( "point" ), false );
|
||||
|
||||
QgsProcessingPointWidgetWrapper wrapper( ¶m, type );
|
||||
|
||||
QgsProcessingContext context;
|
||||
QWidget *w = wrapper.createWrappedWidget( context );
|
||||
|
||||
QSignalSpy spy( &wrapper, &QgsProcessingLayoutItemWidgetWrapper::widgetValueHasChanged );
|
||||
wrapper.setWidgetValue( "1,2", context );
|
||||
QCOMPARE( spy.count(), 1 );
|
||||
if ( type != QgsProcessingGui::Modeler )
|
||||
{
|
||||
QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "1,2" ) );
|
||||
QCOMPARE( static_cast< QgsProcessingPointPanel * >( wrapper.wrappedWidget() )->mLineEdit->text(), QStringLiteral( "1,2" ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "1,2" ) );
|
||||
QCOMPARE( static_cast< QLineEdit * >( wrapper.wrappedWidget() )->text(), QStringLiteral( "1,2" ) );
|
||||
}
|
||||
wrapper.setWidgetValue( "1,2 [EPSG:3111]", context );
|
||||
QCOMPARE( spy.count(), 2 );
|
||||
if ( type != QgsProcessingGui::Modeler )
|
||||
{
|
||||
QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "1,2 [EPSG:3111]" ) );
|
||||
QCOMPARE( static_cast< QgsProcessingPointPanel * >( wrapper.wrappedWidget() )->mLineEdit->text(), QStringLiteral( "1,2 [EPSG:3111]" ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "1,2 [EPSG:3111]" ) );
|
||||
QCOMPARE( static_cast< QLineEdit * >( wrapper.wrappedWidget() )->text(), QStringLiteral( "1,2 [EPSG:3111]" ) );
|
||||
}
|
||||
|
||||
// check signal
|
||||
if ( type != QgsProcessingGui::Modeler )
|
||||
{
|
||||
static_cast< QgsProcessingPointPanel * >( wrapper.wrappedWidget() )->mLineEdit->setText( QStringLiteral( "b" ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
static_cast< QLineEdit * >( wrapper.wrappedWidget() )->setText( QStringLiteral( "aaaa" ) );
|
||||
}
|
||||
QCOMPARE( spy.count(), 3 );
|
||||
|
||||
|
||||
QLabel *l = wrapper.createWrappedLabel();
|
||||
if ( wrapper.type() != QgsProcessingGui::Batch )
|
||||
{
|
||||
QVERIFY( l );
|
||||
QCOMPARE( l->text(), QStringLiteral( "point" ) );
|
||||
QCOMPARE( l->toolTip(), param.toolTip() );
|
||||
delete l;
|
||||
}
|
||||
else
|
||||
{
|
||||
QVERIFY( !l );
|
||||
}
|
||||
|
||||
delete w;
|
||||
|
||||
// optional
|
||||
|
||||
QgsProcessingParameterPoint param2( QStringLiteral( "point" ), QStringLiteral( "point" ), QVariant(), true );
|
||||
|
||||
QgsProcessingPointWidgetWrapper wrapper2( ¶m2, type );
|
||||
w = wrapper2.createWrappedWidget( context );
|
||||
|
||||
QSignalSpy spy2( &wrapper2, &QgsProcessingLayoutItemWidgetWrapper::widgetValueHasChanged );
|
||||
wrapper2.setWidgetValue( "1,2", context );
|
||||
QCOMPARE( spy2.count(), 1 );
|
||||
if ( type != QgsProcessingGui::Modeler )
|
||||
{
|
||||
QCOMPARE( static_cast< QgsProcessingPointPanel * >( wrapper2.wrappedWidget() )->mLineEdit->text(), QStringLiteral( "1,2" ) );
|
||||
QCOMPARE( wrapper2.widgetValue().toString(), QStringLiteral( "1,2" ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
QCOMPARE( wrapper2.widgetValue().toString(), QStringLiteral( "1,2" ) );
|
||||
QCOMPARE( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text(), QStringLiteral( "1,2" ) );
|
||||
}
|
||||
|
||||
wrapper2.setWidgetValue( "1,2 [EPSG:3111]", context );
|
||||
QCOMPARE( spy2.count(), 2 );
|
||||
if ( type != QgsProcessingGui::Modeler )
|
||||
{
|
||||
QCOMPARE( wrapper2.widgetValue().toString(), QStringLiteral( "1,2 [EPSG:3111]" ) );
|
||||
QCOMPARE( static_cast< QgsProcessingPointPanel * >( wrapper2.wrappedWidget() )->mLineEdit->text(), QStringLiteral( "1,2 [EPSG:3111]" ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
QCOMPARE( wrapper2.widgetValue().toString(), QStringLiteral( "1,2 [EPSG:3111]" ) );
|
||||
QCOMPARE( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text(), QStringLiteral( "1,2 [EPSG:3111]" ) );
|
||||
}
|
||||
wrapper2.setWidgetValue( QVariant(), context );
|
||||
QCOMPARE( spy2.count(), 3 );
|
||||
QVERIFY( !wrapper2.widgetValue().isValid() );
|
||||
if ( type == QgsProcessingGui::Modeler )
|
||||
{
|
||||
QVERIFY( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text().isEmpty() );
|
||||
}
|
||||
else
|
||||
{
|
||||
QVERIFY( static_cast< QgsProcessingPointPanel * >( wrapper2.wrappedWidget() )->mLineEdit->text().isEmpty() );
|
||||
}
|
||||
wrapper2.setWidgetValue( "1,3", context );
|
||||
QCOMPARE( spy2.count(), 4 );
|
||||
wrapper2.setWidgetValue( "", context );
|
||||
QCOMPARE( spy2.count(), 5 );
|
||||
QVERIFY( !wrapper2.widgetValue().isValid() );
|
||||
if ( type == QgsProcessingGui::Modeler )
|
||||
{
|
||||
QVERIFY( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text().isEmpty() );
|
||||
}
|
||||
else
|
||||
{
|
||||
QVERIFY( static_cast< QgsProcessingPointPanel * >( wrapper2.wrappedWidget() )->mLineEdit->text().isEmpty() );
|
||||
}
|
||||
|
||||
// check signals
|
||||
wrapper2.setWidgetValue( "1,3", context );
|
||||
QCOMPARE( spy2.count(), 6 );
|
||||
if ( type == QgsProcessingGui::Modeler )
|
||||
{
|
||||
static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
static_cast< QgsProcessingPointPanel * >( wrapper2.wrappedWidget() )->mLineEdit->clear();
|
||||
}
|
||||
QCOMPARE( spy2.count(), 7 );
|
||||
|
||||
delete w;
|
||||
};
|
||||
|
||||
// standard wrapper
|
||||
testWrapper( QgsProcessingGui::Standard );
|
||||
|
||||
// batch wrapper
|
||||
testWrapper( QgsProcessingGui::Batch );
|
||||
|
||||
// modeler wrapper
|
||||
testWrapper( QgsProcessingGui::Modeler );
|
||||
}
|
||||
|
||||
void TestProcessingGui::cleanupTempDir()
|
||||
{
|
||||
QDir tmpDir = QDir( mTempDir );
|
||||
|
Loading…
x
Reference in New Issue
Block a user