Skip to content

Commit

Permalink
[processing] Port CRS widget wrapper to c++ classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 6, 2018
1 parent d79d911 commit 06d37fc
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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``.
Expand Down
3 changes: 2 additions & 1 deletion src/gui/processing/qgsprocessingguiregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ QgsProcessingGuiRegistry::QgsProcessingGuiRegistry()
addAlgorithmConfigurationWidgetFactory( new QgsFilterAlgorithmConfigurationWidgetFactory() );

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

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/processing/qgsprocessingguiregistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion src/gui/processing/qgsprocessingmodelerparameterwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
4 changes: 2 additions & 2 deletions src/gui/processing/qgsprocessingmodelerparameterwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/gui/processing/qgsprocessingwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 >() )
{
Expand Down Expand Up @@ -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() );
Expand Down
8 changes: 4 additions & 4 deletions src/gui/processing/qgsprocessingwidgetwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:

Expand Down
126 changes: 124 additions & 2 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgsprocessingwidgetwrapperimpl.h"
#include "qgsprocessingparameters.h"
#include "qgsprocessingoutputs.h"
#include "qgsprojectionselectionwidget.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QCheckBox>
Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand Down
40 changes: 37 additions & 3 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class QCheckBox;
class QComboBox;
class QLineEdit;
class QPlainTextEdit;

class QgsProjectionSelectionWidget;

///@cond PRIVATE

Expand All @@ -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;
Expand All @@ -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
{
Expand All @@ -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;
Expand Down

0 comments on commit 06d37fc

Please sign in to comment.