Skip to content

Commit

Permalink
Add method to get HTML formatted versions of output values for
Browse files Browse the repository at this point in the history
processing outputs
  • Loading branch information
nyalldawson committed Jan 9, 2024
1 parent b674a7c commit 3e10cf9
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ Returns a string version of the parameter output ``value`` (if possible).
:return: - value converted to string
- ok: will be set to ``True`` if value could be represented as a string.

.. seealso:: :py:func:`valueAsFormattedString`


.. versionadded:: 3.36
%End

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

By default this will return the same value as :py:func:`~QgsProcessingOutputDefinition.valueAsString`.

: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:`valueAsString`


.. versionadded:: 3.36
%End

Expand Down Expand Up @@ -328,6 +349,8 @@ Constructor for QgsProcessingOutputHtml.
Returns the type name for the output class.
%End
virtual QString type() const;
virtual QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok /Out/ ) const;

};


Expand Down Expand Up @@ -464,6 +487,9 @@ Constructor for QgsProcessingOutputFolder.
Returns the type name for the output class.
%End
virtual QString type() const;
virtual QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok /Out/ ) const;


};

class QgsProcessingOutputFile : QgsProcessingOutputDefinition
Expand All @@ -489,6 +515,9 @@ Constructor for QgsProcessingOutputFile.
Returns the type name for the output class.
%End
virtual QString type() const;
virtual QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok /Out/ ) const;


};

class QgsProcessingOutputConditionalBranch : QgsProcessingOutputDefinition
Expand Down
29 changes: 29 additions & 0 deletions python/core/auto_generated/processing/qgsprocessingoutputs.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ Returns a string version of the parameter output ``value`` (if possible).
:return: - value converted to string
- ok: will be set to ``True`` if value could be represented as a string.

.. seealso:: :py:func:`valueAsFormattedString`


.. versionadded:: 3.36
%End

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

By default this will return the same value as :py:func:`~QgsProcessingOutputDefinition.valueAsString`.

: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:`valueAsString`


.. versionadded:: 3.36
%End

Expand Down Expand Up @@ -328,6 +349,8 @@ Constructor for QgsProcessingOutputHtml.
Returns the type name for the output class.
%End
virtual QString type() const;
virtual QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok /Out/ ) const;

};


Expand Down Expand Up @@ -464,6 +487,9 @@ Constructor for QgsProcessingOutputFolder.
Returns the type name for the output class.
%End
virtual QString type() const;
virtual QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok /Out/ ) const;


};

class QgsProcessingOutputFile : QgsProcessingOutputDefinition
Expand All @@ -489,6 +515,9 @@ Constructor for QgsProcessingOutputFile.
Returns the type name for the output class.
%End
virtual QString type() const;
virtual QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok /Out/ ) const;


};

class QgsProcessingOutputConditionalBranch : QgsProcessingOutputDefinition
Expand Down
41 changes: 41 additions & 0 deletions src/core/processing/qgsprocessingoutputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include "qgsprocessingoutputs.h"
#include "qgsvariantutils.h"

#include <QUrl>
#include <QDir>

QgsProcessingOutputDefinition::QgsProcessingOutputDefinition( const QString &name, const QString &description )
: mName( name )
, mDescription( description )
Expand All @@ -40,6 +43,11 @@ QString QgsProcessingOutputDefinition::valueAsString( const QVariant &value, Qgs
return QString();
}

QString QgsProcessingOutputDefinition::valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
{
return valueAsString( value, context, ok );
}

QgsProcessingOutputVectorLayer::QgsProcessingOutputVectorLayer( const QString &name, const QString &description, QgsProcessing::SourceType type )
: QgsProcessingOutputDefinition( name, description )
, mDataType( type )
Expand Down Expand Up @@ -71,6 +79,17 @@ QgsProcessingOutputHtml::QgsProcessingOutputHtml( const QString &name, const QSt
: QgsProcessingOutputDefinition( name, description )
{}

QString QgsProcessingOutputHtml::valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
{
if ( value.type() == QVariant::String && !value.toString().isEmpty() )
{
ok = true;
return QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( value.toString() ).toString(), QDir::toNativeSeparators( value.toString() ) );
}

return valueAsString( value, context, ok );
}

QgsProcessingOutputNumber::QgsProcessingOutputNumber( const QString &name, const QString &description )
: QgsProcessingOutputDefinition( name, description )
{}
Expand Down Expand Up @@ -116,10 +135,32 @@ QgsProcessingOutputFolder::QgsProcessingOutputFolder( const QString &name, const
: QgsProcessingOutputDefinition( name, description )
{}

QString QgsProcessingOutputFolder::valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
{
if ( value.type() == QVariant::String && !value.toString().isEmpty() )
{
ok = true;
return QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( value.toString() ).toString(), QDir::toNativeSeparators( value.toString() ) );
}

return valueAsString( value, context, ok );
}

QgsProcessingOutputFile::QgsProcessingOutputFile( const QString &name, const QString &description )
: QgsProcessingOutputDefinition( name, description )
{}

QString QgsProcessingOutputFile::valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
{
if ( value.type() == QVariant::String && !value.toString().isEmpty() )
{
ok = true;
return QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( value.toString() ).toString(), QDir::toNativeSeparators( value.toString() ) );
}

return valueAsString( value, context, ok );
}

QgsProcessingOutputMapLayer::QgsProcessingOutputMapLayer( const QString &name, const QString &description )
: QgsProcessingOutputDefinition( name, description )
{}
Expand Down
23 changes: 23 additions & 0 deletions src/core/processing/qgsprocessingoutputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,28 @@ class CORE_EXPORT QgsProcessingOutputDefinition
* \param ok will be set to TRUE if value could be represented as a string.
* \returns value converted to string
*
* \see valueAsFormattedString()
*
* \since QGIS 3.36
*/
virtual QString valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok SIP_OUT ) const;

/**
* Returns a HTML string version of the parameter output \a value (if possible).
*
* By default this will return the same value as valueAsString().
*
* \param value value to convert
* \param context processing context
* \param ok will be set to TRUE if value could be represented as a string.
* \returns value converted to string
*
* \see valueAsString()
*
* \since QGIS 3.36
*/
virtual QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok SIP_OUT ) const;

protected:

//! Output name
Expand Down Expand Up @@ -322,6 +340,7 @@ class CORE_EXPORT QgsProcessingOutputHtml : public QgsProcessingOutputDefinition
*/
static QString typeName() { return QStringLiteral( "outputHtml" ); }
QString type() const override { return typeName(); }
QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok SIP_OUT ) const override;
};


Expand Down Expand Up @@ -439,6 +458,8 @@ class CORE_EXPORT QgsProcessingOutputFolder : public QgsProcessingOutputDefiniti
*/
static QString typeName() { return QStringLiteral( "outputFolder" ); }
QString type() const override { return typeName(); }
QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok SIP_OUT ) const override;

};

/**
Expand All @@ -461,6 +482,8 @@ class CORE_EXPORT QgsProcessingOutputFile : public QgsProcessingOutputDefinition
*/
static QString typeName() { return QStringLiteral( "outputFile" ); }
QString type() const override { return typeName(); }
QString valueAsFormattedString( const QVariant &value, QgsProcessingContext &context, bool &ok SIP_OUT ) const override;

};

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12863,6 +12863,10 @@ void TestQgsProcessing::testOutputs()
QStringLiteral( "/home/test/test.html" ) );
QVERIFY( ok );
ok = false;
QCOMPARE( outputHtml.valueAsFormattedString( QStringLiteral( "/home/test/test.html" ), context, ok ),
QStringLiteral( "<a href=\"file:///home/test/test.html\">/home/test/test.html</a>" ) );
QVERIFY( ok );
ok = false;
QCOMPARE( outputHtml.valueAsString( QVariant(), context, ok ),
QStringLiteral( "NULL" ) );
QVERIFY( ok );
Expand All @@ -12873,6 +12877,10 @@ void TestQgsProcessing::testOutputs()
QStringLiteral( "/home/test/test.txt" ) );
QVERIFY( ok );
ok = false;
QCOMPARE( outputFile.valueAsFormattedString( QStringLiteral( "/home/test/test.txt" ), context, ok ),
QStringLiteral( "<a href=\"file:///home/test/test.txt\">/home/test/test.txt</a>" ) );
QVERIFY( ok );
ok = false;
QCOMPARE( outputFile.valueAsString( QVariant(), context, ok ),
QStringLiteral( "NULL" ) );
QVERIFY( ok );
Expand All @@ -12883,6 +12891,10 @@ void TestQgsProcessing::testOutputs()
QStringLiteral( "/home/test" ) );
QVERIFY( ok );
ok = false;
QCOMPARE( outputFolder.valueAsFormattedString( QStringLiteral( "/home/test" ), context, ok ),
QStringLiteral( "<a href=\"file:///home/test\">/home/test</a>" ) );
QVERIFY( ok );
ok = false;
QCOMPARE( outputFolder.valueAsString( QVariant(), context, ok ),
QStringLiteral( "NULL" ) );
QVERIFY( ok );
Expand Down

0 comments on commit 3e10cf9

Please sign in to comment.