Skip to content
Permalink
Browse files

[processing] Port CRS widget wrapper to c++ classes

  • Loading branch information
nyalldawson committed Sep 6, 2018
1 parent d79d911 commit 06d37fced46ba5f89e1a98dc786cd6b034493aa9
@@ -105,7 +105,7 @@ If no factory is registered which handles the given ``parameter``, a None will b

QgsProcessingModelerParameterWidget *createModelerParameterWidget( QgsProcessingModelAlgorithm *model,
const QString &childId,
const QgsProcessingParameterDefinition *parameter, const QgsProcessingContext &context ) /Factory/;
const QgsProcessingParameterDefinition *parameter, QgsProcessingContext &context ) /Factory/;
%Docstring
Creates a new modeler parameter widget for the given ``parameter``. This widget allows
configuration of the parameter's value when used inside a Processing ``model``.
@@ -39,7 +39,7 @@ QgsGui.processingGuiRegistry()->createModelerParameterWidget().
QgsProcessingModelerParameterWidget( QgsProcessingModelAlgorithm *model,
const QString &childId,
const QgsProcessingParameterDefinition *parameter,
const QgsProcessingContext &context,
QgsProcessingContext &context,
QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsProcessingModelerParameterWidget, for the specified
@@ -73,7 +73,7 @@ Constructor for QgsAbstractProcessingParameterWidgetWrapper, for the specified
Returns the dialog type for which widgets and labels will be created by this wrapper.
%End

QWidget *createWrappedWidget( const QgsProcessingContext &context ) /Factory/;
QWidget *createWrappedWidget( QgsProcessingContext &context ) /Factory/;
%Docstring
Creates and return a new wrapped widget which allows customization of the parameter's value.

@@ -118,7 +118,7 @@ Returns the current wrapped label, if any.
Returns the parameter definition associated with this wrapper.
%End

void setParameterValue( const QVariant &value, const QgsProcessingContext &context );
void setParameterValue( const QVariant &value, QgsProcessingContext &context );
%Docstring
Sets the current ``value`` for the parameter.

@@ -178,7 +178,7 @@ widget itself).
.. seealso:: :py:func:`createWidget`
%End

virtual void setWidgetValue( const QVariant &value, const QgsProcessingContext &context ) = 0;
virtual void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) = 0;
%Docstring
Sets the current ``value`` for the parameter to show in the widget.

@@ -237,7 +237,7 @@ The ``type`` argument indicates the dialog type to create a wrapper for.
virtual QgsProcessingModelerParameterWidget *createModelerWidgetWrapper( QgsProcessingModelAlgorithm *model,
const QString &childId,
const QgsProcessingParameterDefinition *parameter,
const QgsProcessingContext &context );
QgsProcessingContext &context );
%Docstring
Creates a new modeler parameter widget for the given ``parameter``. This widget allows
configuration of the parameter's value when used inside a Processing ``model``.
@@ -28,6 +28,7 @@ QgsProcessingGuiRegistry::QgsProcessingGuiRegistry()
addAlgorithmConfigurationWidgetFactory( new QgsFilterAlgorithmConfigurationWidgetFactory() );

addParameterWidgetFactory( new QgsProcessingBooleanWidgetWrapper() );
addParameterWidgetFactory( new QgsProcessingCrsWidgetWrapper() );
addParameterWidgetFactory( new QgsProcessingStringWidgetWrapper() );
}

@@ -101,7 +102,7 @@ QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingGuiRegistry::createPar
return mParameterWidgetFactories.value( parameterType )->createWidgetWrapper( parameter, type );
}

QgsProcessingModelerParameterWidget *QgsProcessingGuiRegistry::createModelerParameterWidget( QgsProcessingModelAlgorithm *model, const QString &childId, const QgsProcessingParameterDefinition *parameter, const QgsProcessingContext &context )
QgsProcessingModelerParameterWidget *QgsProcessingGuiRegistry::createModelerParameterWidget( QgsProcessingModelAlgorithm *model, const QString &childId, const QgsProcessingParameterDefinition *parameter, QgsProcessingContext &context )
{
if ( !parameter )
return nullptr;
@@ -136,7 +136,7 @@ class GUI_EXPORT QgsProcessingGuiRegistry
*/
QgsProcessingModelerParameterWidget *createModelerParameterWidget( QgsProcessingModelAlgorithm *model,
const QString &childId,
const QgsProcessingParameterDefinition *parameter, const QgsProcessingContext &context ) SIP_FACTORY;
const QgsProcessingParameterDefinition *parameter, QgsProcessingContext &context ) SIP_FACTORY;

private:

@@ -31,7 +31,7 @@

QgsProcessingModelerParameterWidget::QgsProcessingModelerParameterWidget( QgsProcessingModelAlgorithm *model,
const QString &childId,
const QgsProcessingParameterDefinition *parameter, const QgsProcessingContext &context,
const QgsProcessingParameterDefinition *parameter, QgsProcessingContext &context,
QWidget *parent )
: QWidget( parent )
, mModel( model )
@@ -77,7 +77,7 @@ class GUI_EXPORT QgsProcessingModelerParameterWidget : public QWidget
QgsProcessingModelerParameterWidget( QgsProcessingModelAlgorithm *model,
const QString &childId,
const QgsProcessingParameterDefinition *parameter,
const QgsProcessingContext &context,
QgsProcessingContext &context,
QWidget *parent SIP_TRANSFERTHIS = nullptr );

~QgsProcessingModelerParameterWidget() override;
@@ -150,7 +150,7 @@ class GUI_EXPORT QgsProcessingModelerParameterWidget : public QWidget
QgsProcessingModelAlgorithm *mModel = nullptr;
const QString mChildId;
const QgsProcessingParameterDefinition *mParameterDefinition = nullptr;
const QgsProcessingContext &mContext;
QgsProcessingContext &mContext;

QVariant mStaticValue;
QString mModelInputParameterName;
@@ -35,7 +35,7 @@ QgsProcessingGui::WidgetType QgsAbstractProcessingParameterWidgetWrapper::type()
return mType;
}

QWidget *QgsAbstractProcessingParameterWidgetWrapper::createWrappedWidget( const QgsProcessingContext &context )
QWidget *QgsAbstractProcessingParameterWidgetWrapper::createWrappedWidget( QgsProcessingContext &context )
{
if ( mWidget )
return mWidget;
@@ -86,7 +86,7 @@ const QgsProcessingParameterDefinition *QgsAbstractProcessingParameterWidgetWrap
return mParameterDefinition;
}

void QgsAbstractProcessingParameterWidgetWrapper::setParameterValue( const QVariant &value, const QgsProcessingContext &context )
void QgsAbstractProcessingParameterWidgetWrapper::setParameterValue( const QVariant &value, QgsProcessingContext &context )
{
if ( mPropertyButton && value.canConvert< QgsProperty >() )
{
@@ -207,7 +207,7 @@ void QgsAbstractProcessingParameterWidgetWrapper::setDynamicParentLayerParameter
}
}

QgsProcessingModelerParameterWidget *QgsProcessingParameterWidgetFactoryInterface::createModelerWidgetWrapper( QgsProcessingModelAlgorithm *model, const QString &childId, const QgsProcessingParameterDefinition *parameter, const QgsProcessingContext &context )
QgsProcessingModelerParameterWidget *QgsProcessingParameterWidgetFactoryInterface::createModelerWidgetWrapper( QgsProcessingModelAlgorithm *model, const QString &childId, const QgsProcessingParameterDefinition *parameter, QgsProcessingContext &context )
{
std::unique_ptr< QgsProcessingModelerParameterWidget > widget = qgis::make_unique< QgsProcessingModelerParameterWidget >( model, childId, parameter, context );
widget->populateSources( compatibleParameterTypes(), compatibleOutputTypes(), compatibleDataTypes() );
@@ -106,7 +106,7 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject
*
* \see createWrappedLabel()
*/
QWidget *createWrappedWidget( const QgsProcessingContext &context ) SIP_FACTORY;
QWidget *createWrappedWidget( QgsProcessingContext &context ) SIP_FACTORY;

/**
* Creates and returns a new label to accompany widgets created by the wrapper.
@@ -147,7 +147,7 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject
*
* \see parameterValue()
*/
void setParameterValue( const QVariant &value, const QgsProcessingContext &context );
void setParameterValue( const QVariant &value, QgsProcessingContext &context );

/**
* Returns the current value of the parameter.
@@ -209,7 +209,7 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject
*
* \see widgetValue()
*/
virtual void setWidgetValue( const QVariant &value, const QgsProcessingContext &context ) = 0;
virtual void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) = 0;

/**
* Returns the current value of the parameter.
@@ -293,7 +293,7 @@ class GUI_EXPORT QgsProcessingParameterWidgetFactoryInterface
virtual QgsProcessingModelerParameterWidget *createModelerWidgetWrapper( QgsProcessingModelAlgorithm *model,
const QString &childId,
const QgsProcessingParameterDefinition *parameter,
const QgsProcessingContext &context );
QgsProcessingContext &context );

protected:

@@ -18,6 +18,7 @@
#include "qgsprocessingwidgetwrapperimpl.h"
#include "qgsprocessingparameters.h"
#include "qgsprocessingoutputs.h"
#include "qgsprojectionselectionwidget.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QCheckBox>
@@ -85,7 +86,7 @@ QLabel *QgsProcessingBooleanWidgetWrapper::createLabel()
return QgsAbstractProcessingParameterWidgetWrapper::createLabel();
}

void QgsProcessingBooleanWidgetWrapper::setWidgetValue( const QVariant &value, const QgsProcessingContext &context )
void QgsProcessingBooleanWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
switch ( type() )
{
@@ -162,6 +163,127 @@ QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingBooleanWidgetWrapper::
}


//
// QgsProcessingCrsWidgetWrapper
//

QgsProcessingCrsWidgetWrapper::QgsProcessingCrsWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
: QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
{

}

QWidget *QgsProcessingCrsWidgetWrapper::createWidget()
{
mProjectionSelectionWidget = new QgsProjectionSelectionWidget();
mProjectionSelectionWidget->setToolTip( parameterDefinition()->toolTip() );

if ( parameterDefinition()->flags() & QgsProcessingParameterDefinition::FlagOptional )
mProjectionSelectionWidget->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, true );
else
mProjectionSelectionWidget->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, false );

connect( mProjectionSelectionWidget, &QgsProjectionSelectionWidget::crsChanged, this, [ = ]
{
emit widgetValueHasChanged( this );
} );

switch ( type() )
{
case QgsProcessingGui::Standard:
case QgsProcessingGui::Batch:
{
return mProjectionSelectionWidget;
};

case QgsProcessingGui::Modeler:
{
QWidget *w = new QWidget();
w->setToolTip( parameterDefinition()->toolTip() );

QVBoxLayout *vl = new QVBoxLayout();
vl->setMargin( 0 );
vl->setContentsMargins( 0, 0, 0, 0 );
w->setLayout( vl );

mUseProjectCrsCheckBox = new QCheckBox( tr( "Use project CRS" ) );
mUseProjectCrsCheckBox->setToolTip( tr( "Always use the current project CRS when running the model" ) );
vl->addWidget( mUseProjectCrsCheckBox );
connect( mUseProjectCrsCheckBox, &QCheckBox::toggled, mProjectionSelectionWidget, &QgsProjectionSelectionWidget::setDisabled );

vl->addWidget( mProjectionSelectionWidget );

return w;
}
}
return nullptr;
}

void QgsProcessingCrsWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
if ( mUseProjectCrsCheckBox )
{
if ( value.toString().compare( QLatin1String( "ProjectCrs" ), Qt::CaseInsensitive ) == 0 )
{
mUseProjectCrsCheckBox->setChecked( true );
return;
}
else
{
mUseProjectCrsCheckBox->setChecked( false );
}
}

const QgsCoordinateReferenceSystem v = QgsProcessingParameters::parameterAsCrs( parameterDefinition(), value, context );
if ( mProjectionSelectionWidget )
mProjectionSelectionWidget->setCrs( v );
}

QVariant QgsProcessingCrsWidgetWrapper::widgetValue() const
{
if ( mUseProjectCrsCheckBox && mUseProjectCrsCheckBox->isChecked() )
return QStringLiteral( "ProjectCrs" );
else if ( mProjectionSelectionWidget )
return mProjectionSelectionWidget->crs().isValid() ? mProjectionSelectionWidget->crs() : QVariant();
else
return QVariant();
}

QStringList QgsProcessingCrsWidgetWrapper::compatibleParameterTypes() const
{
return QStringList()
<< QgsProcessingParameterCrs::typeName()
<< QgsProcessingParameterExpression::typeName()
<< QgsProcessingParameterString::typeName()
<< QgsProcessingParameterRasterLayer::typeName()
<< QgsProcessingParameterVectorLayer::typeName()
<< QgsProcessingParameterFeatureSource::typeName();
}

QStringList QgsProcessingCrsWidgetWrapper::compatibleOutputTypes() const
{
return QStringList() << QgsProcessingOutputVectorLayer::typeName()
<< QgsProcessingOutputRasterLayer::typeName()
<< QgsProcessingOutputMapLayer::typeName()
<< QgsProcessingOutputString::typeName();
}

QList<int> QgsProcessingCrsWidgetWrapper::compatibleDataTypes() const
{
return QList< int >();
}

QString QgsProcessingCrsWidgetWrapper::parameterType() const
{
return QgsProcessingParameterCrs::typeName();
}

QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingCrsWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
{
return new QgsProcessingCrsWidgetWrapper( parameter, type );
}



//
// QgsProcessingStringWidgetWrapper
@@ -219,7 +341,7 @@ QWidget *QgsProcessingStringWidgetWrapper::createWidget()
return nullptr;
}

void QgsProcessingStringWidgetWrapper::setWidgetValue( const QVariant &value, const QgsProcessingContext &context )
void QgsProcessingStringWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
const QString v = QgsProcessingParameters::parameterAsString( parameterDefinition(), value, context );
if ( mLineEdit )
@@ -26,7 +26,7 @@ class QCheckBox;
class QComboBox;
class QLineEdit;
class QPlainTextEdit;

class QgsProjectionSelectionWidget;

///@cond PRIVATE

@@ -49,7 +49,7 @@ class GUI_EXPORT QgsProcessingBooleanWidgetWrapper : public QgsAbstractProcessin

protected:

void setWidgetValue( const QVariant &value, const QgsProcessingContext &context ) override;
void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
QVariant widgetValue() const override;

QStringList compatibleParameterTypes() const override;
@@ -66,6 +66,40 @@ class GUI_EXPORT QgsProcessingBooleanWidgetWrapper : public QgsAbstractProcessin
friend class TestProcessingGui;
};

class GUI_EXPORT QgsProcessingCrsWidgetWrapper : public QgsAbstractProcessingParameterWidgetWrapper, public QgsProcessingParameterWidgetFactoryInterface
{
Q_OBJECT

public:

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

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;

private:

QgsProjectionSelectionWidget *mProjectionSelectionWidget = nullptr;
QCheckBox *mUseProjectCrsCheckBox = nullptr;

friend class TestProcessingGui;
};

class GUI_EXPORT QgsProcessingStringWidgetWrapper : public QgsAbstractProcessingParameterWidgetWrapper, public QgsProcessingParameterWidgetFactoryInterface
{
@@ -85,7 +119,7 @@ class GUI_EXPORT QgsProcessingStringWidgetWrapper : public QgsAbstractProcessing

protected:

void setWidgetValue( const QVariant &value, const QgsProcessingContext &context ) override;
void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
QVariant widgetValue() const override;

QStringList compatibleParameterTypes() const override;

0 comments on commit 06d37fc

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