From 3e10cf9338c34733482140fc648c6be7b1f0e689 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 9 Jan 2024 11:57:30 +1000 Subject: [PATCH] Add method to get HTML formatted versions of output values for processing outputs --- .../processing/qgsprocessingoutputs.sip.in | 29 +++++++++++++ .../processing/qgsprocessingoutputs.sip.in | 29 +++++++++++++ src/core/processing/qgsprocessingoutputs.cpp | 41 +++++++++++++++++++ src/core/processing/qgsprocessingoutputs.h | 23 +++++++++++ tests/src/analysis/testqgsprocessing.cpp | 12 ++++++ 5 files changed, 134 insertions(+) diff --git a/python/PyQt6/core/auto_generated/processing/qgsprocessingoutputs.sip.in b/python/PyQt6/core/auto_generated/processing/qgsprocessingoutputs.sip.in index 02d014c5a9fa..7e50b0438f00 100644 --- a/python/PyQt6/core/auto_generated/processing/qgsprocessingoutputs.sip.in +++ b/python/PyQt6/core/auto_generated/processing/qgsprocessingoutputs.sip.in @@ -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 @@ -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; + }; @@ -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 @@ -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 diff --git a/python/core/auto_generated/processing/qgsprocessingoutputs.sip.in b/python/core/auto_generated/processing/qgsprocessingoutputs.sip.in index 02d014c5a9fa..7e50b0438f00 100644 --- a/python/core/auto_generated/processing/qgsprocessingoutputs.sip.in +++ b/python/core/auto_generated/processing/qgsprocessingoutputs.sip.in @@ -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 @@ -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; + }; @@ -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 @@ -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 diff --git a/src/core/processing/qgsprocessingoutputs.cpp b/src/core/processing/qgsprocessingoutputs.cpp index 0ca41aeb870a..ccc449ef99af 100644 --- a/src/core/processing/qgsprocessingoutputs.cpp +++ b/src/core/processing/qgsprocessingoutputs.cpp @@ -18,6 +18,9 @@ #include "qgsprocessingoutputs.h" #include "qgsvariantutils.h" +#include +#include + QgsProcessingOutputDefinition::QgsProcessingOutputDefinition( const QString &name, const QString &description ) : mName( name ) , mDescription( description ) @@ -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 ) @@ -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( "%2" ).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 ) {} @@ -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( "%2" ).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( "%2" ).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 ) {} diff --git a/src/core/processing/qgsprocessingoutputs.h b/src/core/processing/qgsprocessingoutputs.h index f79e83190204..bbaff8db44f5 100644 --- a/src/core/processing/qgsprocessingoutputs.h +++ b/src/core/processing/qgsprocessingoutputs.h @@ -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 @@ -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; }; @@ -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; + }; /** @@ -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; + }; /** diff --git a/tests/src/analysis/testqgsprocessing.cpp b/tests/src/analysis/testqgsprocessing.cpp index aead5cf5c4d0..7e5283d93059 100644 --- a/tests/src/analysis/testqgsprocessing.cpp +++ b/tests/src/analysis/testqgsprocessing.cpp @@ -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( "/home/test/test.html" ) ); + QVERIFY( ok ); + ok = false; QCOMPARE( outputHtml.valueAsString( QVariant(), context, ok ), QStringLiteral( "NULL" ) ); QVERIFY( ok ); @@ -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( "/home/test/test.txt" ) ); + QVERIFY( ok ); + ok = false; QCOMPARE( outputFile.valueAsString( QVariant(), context, ok ), QStringLiteral( "NULL" ) ); QVERIFY( ok ); @@ -12883,6 +12891,10 @@ void TestQgsProcessing::testOutputs() QStringLiteral( "/home/test" ) ); QVERIFY( ok ); ok = false; + QCOMPARE( outputFolder.valueAsFormattedString( QStringLiteral( "/home/test" ), context, ok ), + QStringLiteral( "/home/test" ) ); + QVERIFY( ok ); + ok = false; QCOMPARE( outputFolder.valueAsString( QVariant(), context, ok ), QStringLiteral( "NULL" ) ); QVERIFY( ok );