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

Add more advanced actions to processing dialog, including copy as qgis_process command, copy/paste as json #46514

Merged
merged 11 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
5 changes: 5 additions & 0 deletions python/core/auto_additions/qgsprocessingcontext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The following has been generated automatically from src/core/processing/qgsprocessingcontext.h
# monkey patching scoped based enum
QgsProcessingContext.ProcessArgumentFlag.IncludeProjectPath.__doc__ = "Include the associated project path argument"
QgsProcessingContext.ProcessArgumentFlag.__doc__ = 'Flags controlling the results given by :py:func:`~QgsProcessingContext.asQgisProcessArguments`.\n\n.. versionadded:: 3.24\n\n' + '* ``IncludeProjectPath``: ' + QgsProcessingContext.ProcessArgumentFlag.IncludeProjectPath.__doc__
# --
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,30 @@ using the specified ``parameters``.

Algorithms which cannot be run from a Python command should return an empty
string.
%End

virtual QString asQgisProcessCommand( const QVariantMap &parameters, QgsProcessingContext &context, bool &ok /Out/ ) const;
%Docstring
Returns a command string which will execute the algorithm using the specified ``parameters``
via the command line qgis_process tool.

Note that some combinations of parameter types and values cannot be represented as a qgis_process string.

:param parameters: algorithm parameters
:param context: processing context

:return: - equivalent qgis_process command
- ok: will be set to ``True`` if the command was successfully generated


.. versionadded:: 3.24
%End

virtual QVariantMap asMap( const QVariantMap &parameters, QgsProcessingContext &context ) const;
%Docstring
Returns a JSON serializable variant map containing the specified ``parameters`` and ``context`` settings.

.. versionadded:: 3.24
%End

void setProvider( QgsProcessingProvider *provider );
Expand Down
23 changes: 23 additions & 0 deletions python/core/auto_generated/processing/qgsprocessingcontext.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,27 @@ Sets the logging ``level`` for algorithms to use when pushing feedback messages
.. seealso:: :py:func:`logLevel`

.. versionadded:: 3.20
%End

QVariantMap exportToMap() const;
%Docstring
Exports the context's settings to a variant map.

.. versionadded:: 3.24
%End

enum class ProcessArgumentFlag
{
IncludeProjectPath,
};
typedef QFlags<QgsProcessingContext::ProcessArgumentFlag> ProcessArgumentFlags;


QStringList asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags = QgsProcessingContext::ProcessArgumentFlags() ) const;
%Docstring
Returns list of the equivalent qgis_process arguments representing the settings from the context.

.. versionadded:: 3.24
%End

private:
Expand All @@ -593,6 +614,8 @@ Sets the logging ``level`` for algorithms to use when pushing feedback messages

QFlags<QgsProcessingContext::Flag> operator|(QgsProcessingContext::Flag f1, QFlags<QgsProcessingContext::Flag> f2);

QFlags<QgsProcessingContext::ProcessArgumentFlag> operator|(QgsProcessingContext::ProcessArgumentFlag f1, QFlags<QgsProcessingContext::ProcessArgumentFlag> f2);



class QgsProcessingLayerPostProcessorInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,63 @@ layers and other factors within the context.
%Docstring
Returns a string version of the parameter input ``value``, which is suitable for use as an input
parameter value when running an algorithm directly from a Python command.

.. seealso:: :py:func:`valueAsJsonObject`

.. seealso:: :py:func:`valueAsString`
%End

virtual QVariant valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const;
%Docstring
Returns a version of the parameter input ``value``, which is suitable for use in a JSON object.

This method must return only simple values which can be losslessly encapsulated in a serialized
JSON map. For instance, and QGIS class values (such as :py:class:`QgsCoordinateReferenceSystem`) must be
converted to a simple string or numeric value equivalent.

.. seealso:: :py:func:`valueAsPythonString`

.. seealso:: :py:func:`valueAsString`

.. versionadded:: 3.24
%End

virtual QString valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok /Out/ ) const;
%Docstring
Returns a string version of the parameter input ``value`` (if possible).

:param value: value to convert
:param context: processing context

:return: - value converted to string
- ok: will be set to ``True`` if value could be represented as a string.

.. seealso:: :py:func:`valueAsStringList`

.. seealso:: :py:func:`valueAsJsonObject`

.. seealso:: :py:func:`valueAsPythonString`

.. versionadded:: 3.24
%End

virtual QStringList valueAsStringList( const QVariant &value, QgsProcessingContext &context, bool &ok /Out/ ) const;
%Docstring
Returns a string list version of the parameter input ``value`` (if possible).

:param value: value to convert
:param context: processing context

:return: - value converted to string list
- ok: will be set to ``True`` if value could be represented as a string list

.. seealso:: :py:func:`valueAsString`

.. seealso:: :py:func:`valueAsJsonObject`

.. seealso:: :py:func:`valueAsPythonString`

.. versionadded:: 3.24
%End

virtual QString valueAsPythonComment( const QVariant &value, QgsProcessingContext &context ) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ Sets the logging ``level`` to use when running algorithms from the dialog.
.. seealso:: :py:func:`logLevel`

.. versionadded:: 3.20
%End

virtual void setParameters( const QVariantMap &values );
%Docstring
Sets the parameter ``values`` to show in the dialog.

.. versionadded:: 3.24
%End

public slots:
Expand Down
58 changes: 58 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,64 @@ QString QgsProcessingAlgorithm::asPythonCommand( const QVariantMap &parameters,
return s;
}

QString QgsProcessingAlgorithm::asQgisProcessCommand( const QVariantMap &parameters, QgsProcessingContext &context, bool &ok ) const
{
ok = true;
QStringList parts;
parts.append( QStringLiteral( "qgis_process" ) );
parts.append( QStringLiteral( "run" ) );
parts.append( id() );

QgsProcessingContext::ProcessArgumentFlags argumentFlags;
// we only include the project path argument if a project is actually required by the algorithm
if ( flags() & FlagRequiresProject )
argumentFlags |= QgsProcessingContext::ProcessArgumentFlag::IncludeProjectPath;

parts.append( context.asQgisProcessArguments( argumentFlags ) );

for ( const QgsProcessingParameterDefinition *def : mParameters )
{
if ( def->flags() & QgsProcessingParameterDefinition::FlagHidden )
continue;

if ( !parameters.contains( def->name() ) )
continue;

const QStringList partValues = def->valueAsStringList( parameters.value( def->name() ), context, ok );
if ( !ok )
return QString();

for ( const QString &partValue : partValues )
parts << QStringLiteral( "--%1=%2" ).arg( def->name(), partValue );
}

return parts.join( ' ' );
}

QVariantMap QgsProcessingAlgorithm::asMap( const QVariantMap &parameters, QgsProcessingContext &context ) const
{
QVariantMap properties = context.exportToMap();

// we only include the project path argument if a project is actually required by the algorithm
if ( !( flags() & FlagRequiresProject ) )
properties.remove( QStringLiteral( "project_path" ) );

QVariantMap paramValues;
for ( const QgsProcessingParameterDefinition *def : mParameters )
{
if ( def->flags() & QgsProcessingParameterDefinition::FlagHidden )
continue;

if ( !parameters.contains( def->name() ) )
continue;

paramValues.insert( def->name(), def->valueAsJsonObject( parameters.value( def->name() ), context ) );
}

properties.insert( QStringLiteral( "inputs" ), paramValues );
return properties;
}

bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *definition, bool createOutput )
{
if ( !definition )
Expand Down
23 changes: 23 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,29 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;

/**
* Returns a command string which will execute the algorithm using the specified \a parameters
* via the command line qgis_process tool.
*
* Note that some combinations of parameter types and values cannot be represented as a qgis_process string.
*
* \param parameters algorithm parameters
* \param context processing context
* \param ok will be set to TRUE if the command was successfully generated
*
* \returns equivalent qgis_process command
*
* \since QGIS 3.24
*/
virtual QString asQgisProcessCommand( const QVariantMap &parameters, QgsProcessingContext &context, bool &ok SIP_OUT ) const;

/**
* Returns a JSON serializable variant map containing the specified \a parameters and \a context settings.
*
* \since QGIS 3.24
*/
virtual QVariantMap asMap( const QVariantMap &parameters, QgsProcessingContext &context ) const;

/**
* Associates this algorithm with its provider. No transfer of ownership is involved.
*/
Expand Down
33 changes: 33 additions & 0 deletions src/core/processing/qgsprocessingcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,39 @@ void QgsProcessingContext::setLogLevel( LogLevel level )
mLogLevel = level;
}

QVariantMap QgsProcessingContext::exportToMap() const
{
QVariantMap res;
if ( mDistanceUnit != QgsUnitTypes::DistanceUnknownUnit )
res.insert( QStringLiteral( "distance_units" ), QgsUnitTypes::encodeUnit( mDistanceUnit ) );
if ( mAreaUnit != QgsUnitTypes::AreaUnknownUnit )
res.insert( QStringLiteral( "area_units" ), QgsUnitTypes::encodeUnit( mAreaUnit ) );
if ( !mEllipsoid.isEmpty() )
res.insert( QStringLiteral( "ellipsoid" ), mEllipsoid );
if ( mProject )
res.insert( QStringLiteral( "project_path" ), mProject->fileName() );

return res;
}

QStringList QgsProcessingContext::asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags ) const
{
QStringList res;
if ( mDistanceUnit != QgsUnitTypes::DistanceUnknownUnit )
res << QStringLiteral( "--distance_units=%1" ).arg( QgsUnitTypes::encodeUnit( mDistanceUnit ) );
if ( mAreaUnit != QgsUnitTypes::AreaUnknownUnit )
res << QStringLiteral( "--area_units=%1" ).arg( QgsUnitTypes::encodeUnit( mAreaUnit ) );
if ( !mEllipsoid.isEmpty() )
res << QStringLiteral( "--ellipsoid=%1" ).arg( mEllipsoid );

if ( mProject && flags & ProcessArgumentFlag::IncludeProjectPath )
{
res << QStringLiteral( "--project_path=%1" ).arg( mProject->fileName() );
}

return res;
}

QgsDateTimeRange QgsProcessingContext::currentTimeRange() const
{
return mCurrentTimeRange;
Expand Down
26 changes: 26 additions & 0 deletions src/core/processing/qgsprocessingcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,31 @@ class CORE_EXPORT QgsProcessingContext
*/
void setLogLevel( LogLevel level );

/**
* Exports the context's settings to a variant map.
*
* \since QGIS 3.24
*/
QVariantMap exportToMap() const;

/**
* Flags controlling the results given by asQgisProcessArguments().
*
* \since QGIS 3.24
*/
enum class ProcessArgumentFlag : int
{
IncludeProjectPath = 1 << 0, //!< Include the associated project path argument
};
Q_DECLARE_FLAGS( ProcessArgumentFlags, ProcessArgumentFlag )

/**
* Returns list of the equivalent qgis_process arguments representing the settings from the context.
*
* \since QGIS 3.24
*/
QStringList asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags = QgsProcessingContext::ProcessArgumentFlags() ) const;

private:

QgsProcessingContext::Flags mFlags = QgsProcessingContext::Flags();
Expand Down Expand Up @@ -694,6 +719,7 @@ class CORE_EXPORT QgsProcessingContext
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::Flags )
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingContext::ProcessArgumentFlags )


/**
Expand Down
Loading