From 64bef5c2905ec460e4101b7383cb8f5bacd936f2 Mon Sep 17 00:00:00 2001 From: jonathanlurie Date: Thu, 3 Jul 2025 16:38:43 +0200 Subject: [PATCH] adding some more UT --- .../auto_additions/qgsprocessingparameters.py | 10 +- .../processing/qgsprocessingparameters.sip.in | 11 +- .../auto_additions/qgsprocessingparameters.py | 10 +- .../processing/qgsprocessingparameters.sip.in | 11 +- .../processing/qgsprocessingparameters.cpp | 161 +++++++++--------- src/core/processing/qgsprocessingparameters.h | 6 +- tests/src/analysis/testqgsprocessing.cpp | 24 +++ 7 files changed, 142 insertions(+), 91 deletions(-) diff --git a/python/PyQt6/core/auto_additions/qgsprocessingparameters.py b/python/PyQt6/core/auto_additions/qgsprocessingparameters.py index 76b3e836223..4f1fad6548f 100644 --- a/python/PyQt6/core/auto_additions/qgsprocessingparameters.py +++ b/python/PyQt6/core/auto_additions/qgsprocessingparameters.py @@ -97,7 +97,7 @@ except (NameError, AttributeError): try: QgsProcessingParameterGeometry.typeName = staticmethod(QgsProcessingParameterGeometry.typeName) QgsProcessingParameterGeometry.fromScriptCode = staticmethod(QgsProcessingParameterGeometry.fromScriptCode) - QgsProcessingParameterGeometry.__overridden_methods__ = ['clone', 'type', 'checkValueIsAcceptable', 'valueAsPythonString', 'asScriptCode', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterGeometry.__overridden_methods__ = ['clone', 'type', 'checkValueIsAcceptable', 'valueAsPythonString', 'asScriptCode', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterGeometry.__group__ = ['processing'] except (NameError, AttributeError): pass @@ -137,19 +137,19 @@ except (NameError, AttributeError): pass try: QgsProcessingParameterArea.typeName = staticmethod(QgsProcessingParameterArea.typeName) - QgsProcessingParameterArea.__overridden_methods__ = ['clone', 'type', 'dependsOnOtherParameters', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterArea.__overridden_methods__ = ['clone', 'type', 'dependsOnOtherParameters', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterArea.__group__ = ['processing'] except (NameError, AttributeError): pass try: QgsProcessingParameterVolume.typeName = staticmethod(QgsProcessingParameterVolume.typeName) - QgsProcessingParameterVolume.__overridden_methods__ = ['clone', 'type', 'dependsOnOtherParameters', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterVolume.__overridden_methods__ = ['clone', 'type', 'dependsOnOtherParameters', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterVolume.__group__ = ['processing'] except (NameError, AttributeError): pass try: QgsProcessingParameterDuration.typeName = staticmethod(QgsProcessingParameterDuration.typeName) - QgsProcessingParameterDuration.__overridden_methods__ = ['clone', 'type', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterDuration.__overridden_methods__ = ['clone', 'type', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterDuration.__group__ = ['processing'] except (NameError, AttributeError): pass @@ -320,7 +320,7 @@ except (NameError, AttributeError): try: QgsProcessingParameterDateTime.typeName = staticmethod(QgsProcessingParameterDateTime.typeName) QgsProcessingParameterDateTime.fromScriptCode = staticmethod(QgsProcessingParameterDateTime.fromScriptCode) - QgsProcessingParameterDateTime.__overridden_methods__ = ['clone', 'type', 'checkValueIsAcceptable', 'valueAsPythonString', 'toolTip', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterDateTime.__overridden_methods__ = ['clone', 'type', 'checkValueIsAcceptable', 'valueAsPythonString', 'toolTip', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterDateTime.__group__ = ['processing'] except (NameError, AttributeError): pass diff --git a/python/PyQt6/core/auto_generated/processing/qgsprocessingparameters.sip.in b/python/PyQt6/core/auto_generated/processing/qgsprocessingparameters.sip.in index 730a1049f87..07cea353250 100644 --- a/python/PyQt6/core/auto_generated/processing/qgsprocessingparameters.sip.in +++ b/python/PyQt6/core/auto_generated/processing/qgsprocessingparameters.sip.in @@ -2115,7 +2115,8 @@ Sets the allow multipart geometries .. seealso:: :py:func:`allowMultipart` %End - QString userFriendlyString( const QVariant &value ) const; + virtual QString userFriendlyString( const QVariant &value ) const; + static QgsProcessingParameterGeometry *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/; %Docstring @@ -2711,6 +2712,8 @@ Sets the default area ``unit`` for the parameter. virtual bool fromVariantMap( const QVariantMap &map ); + virtual QString userFriendlyString( const QVariant &value ) const; + }; @@ -2801,6 +2804,8 @@ Sets the default volume ``unit`` for the parameter. virtual bool fromVariantMap( const QVariantMap &map ); + virtual QString userFriendlyString( const QVariant &value ) const; + }; @@ -2860,6 +2865,8 @@ Sets the default duration ``unit`` for the parameter. virtual bool fromVariantMap( const QVariantMap &map ); + virtual QString userFriendlyString( const QVariant &value ) const; + }; @@ -4801,6 +4808,8 @@ Sets the acceptable data ``type`` for the parameter. virtual bool fromVariantMap( const QVariantMap &map ); + virtual QString userFriendlyString( const QVariant &value ) const; + static QgsProcessingParameterDateTime *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/; %Docstring diff --git a/python/core/auto_additions/qgsprocessingparameters.py b/python/core/auto_additions/qgsprocessingparameters.py index 76b3e836223..4f1fad6548f 100644 --- a/python/core/auto_additions/qgsprocessingparameters.py +++ b/python/core/auto_additions/qgsprocessingparameters.py @@ -97,7 +97,7 @@ except (NameError, AttributeError): try: QgsProcessingParameterGeometry.typeName = staticmethod(QgsProcessingParameterGeometry.typeName) QgsProcessingParameterGeometry.fromScriptCode = staticmethod(QgsProcessingParameterGeometry.fromScriptCode) - QgsProcessingParameterGeometry.__overridden_methods__ = ['clone', 'type', 'checkValueIsAcceptable', 'valueAsPythonString', 'asScriptCode', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterGeometry.__overridden_methods__ = ['clone', 'type', 'checkValueIsAcceptable', 'valueAsPythonString', 'asScriptCode', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterGeometry.__group__ = ['processing'] except (NameError, AttributeError): pass @@ -137,19 +137,19 @@ except (NameError, AttributeError): pass try: QgsProcessingParameterArea.typeName = staticmethod(QgsProcessingParameterArea.typeName) - QgsProcessingParameterArea.__overridden_methods__ = ['clone', 'type', 'dependsOnOtherParameters', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterArea.__overridden_methods__ = ['clone', 'type', 'dependsOnOtherParameters', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterArea.__group__ = ['processing'] except (NameError, AttributeError): pass try: QgsProcessingParameterVolume.typeName = staticmethod(QgsProcessingParameterVolume.typeName) - QgsProcessingParameterVolume.__overridden_methods__ = ['clone', 'type', 'dependsOnOtherParameters', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterVolume.__overridden_methods__ = ['clone', 'type', 'dependsOnOtherParameters', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterVolume.__group__ = ['processing'] except (NameError, AttributeError): pass try: QgsProcessingParameterDuration.typeName = staticmethod(QgsProcessingParameterDuration.typeName) - QgsProcessingParameterDuration.__overridden_methods__ = ['clone', 'type', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterDuration.__overridden_methods__ = ['clone', 'type', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterDuration.__group__ = ['processing'] except (NameError, AttributeError): pass @@ -320,7 +320,7 @@ except (NameError, AttributeError): try: QgsProcessingParameterDateTime.typeName = staticmethod(QgsProcessingParameterDateTime.typeName) QgsProcessingParameterDateTime.fromScriptCode = staticmethod(QgsProcessingParameterDateTime.fromScriptCode) - QgsProcessingParameterDateTime.__overridden_methods__ = ['clone', 'type', 'checkValueIsAcceptable', 'valueAsPythonString', 'toolTip', 'asPythonString', 'toVariantMap', 'fromVariantMap'] + QgsProcessingParameterDateTime.__overridden_methods__ = ['clone', 'type', 'checkValueIsAcceptable', 'valueAsPythonString', 'toolTip', 'asPythonString', 'toVariantMap', 'fromVariantMap', 'userFriendlyString'] QgsProcessingParameterDateTime.__group__ = ['processing'] except (NameError, AttributeError): pass diff --git a/python/core/auto_generated/processing/qgsprocessingparameters.sip.in b/python/core/auto_generated/processing/qgsprocessingparameters.sip.in index 794799d0673..7bf4918ae9a 100644 --- a/python/core/auto_generated/processing/qgsprocessingparameters.sip.in +++ b/python/core/auto_generated/processing/qgsprocessingparameters.sip.in @@ -2115,7 +2115,8 @@ Sets the allow multipart geometries .. seealso:: :py:func:`allowMultipart` %End - QString userFriendlyString( const QVariant &value ) const; + virtual QString userFriendlyString( const QVariant &value ) const; + static QgsProcessingParameterGeometry *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/; %Docstring @@ -2711,6 +2712,8 @@ Sets the default area ``unit`` for the parameter. virtual bool fromVariantMap( const QVariantMap &map ); + virtual QString userFriendlyString( const QVariant &value ) const; + }; @@ -2801,6 +2804,8 @@ Sets the default volume ``unit`` for the parameter. virtual bool fromVariantMap( const QVariantMap &map ); + virtual QString userFriendlyString( const QVariant &value ) const; + }; @@ -2860,6 +2865,8 @@ Sets the default duration ``unit`` for the parameter. virtual bool fromVariantMap( const QVariantMap &map ); + virtual QString userFriendlyString( const QVariant &value ) const; + }; @@ -4801,6 +4808,8 @@ Sets the acceptable data ``type`` for the parameter. virtual bool fromVariantMap( const QVariantMap &map ); + virtual QString userFriendlyString( const QVariant &value ) const; + static QgsProcessingParameterDateTime *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/; %Docstring diff --git a/src/core/processing/qgsprocessingparameters.cpp b/src/core/processing/qgsprocessingparameters.cpp index a17fe94d572..a96e3f053c3 100644 --- a/src/core/processing/qgsprocessingparameters.cpp +++ b/src/core/processing/qgsprocessingparameters.cpp @@ -3051,23 +3051,7 @@ QString QgsProcessingParameterDefinition::userFriendlyString( const QVariant &va ); } - else if ( value.userType() == qMetaTypeId< QgsGeometry>() ) - { - const QgsGeometry g = value.value(); - return g.typeName(); - } - - else if ( value.userType() == qMetaTypeId() ) - { - const QgsReferencedGeometry g = value.value(); - if ( !g.isNull() ) - { - return QStringLiteral( "[%1] %2" ).arg( g.crs().userFriendlyIdentifier( Qgis::CrsIdentifierType::ShortString ), g.typeName() ); - } - return g.typeName(); - } - - if ( value.userType() == qMetaTypeId() ) + else if ( value.userType() == qMetaTypeId() ) { const QgsGeometry g = QgsGeometry::fromRect( value.value() ); return g.typeName(); @@ -3096,63 +3080,8 @@ QString QgsProcessingParameterDefinition::userFriendlyString( const QVariant &va return fromVar.sink.asExpression(); } } - else if ( value.userType() == QMetaType::Type::QDateTime ) - { - const QDateTime dt = value.toDateTime(); - if ( !dt.isValid() ) - return QObject::tr( "Invalid datetime" ); - else - return QStringLiteral( "%1-%2-%3T%4:%5:%6" ) - .arg( dt.date().year() ) - .arg( dt.date().month() ) - .arg( dt.date().day() ) - .arg( dt.time().hour() ) - .arg( dt.time().minute() ) - .arg( dt.time().second() ); - } - - else if ( value.userType() == QMetaType::Type::QDate ) - { - const QDate dt = value.toDate(); - if ( !dt.isValid() ) - return QObject::tr( "Invalid date" ); - else - return QStringLiteral( "%1-%2-%3" ) - .arg( dt.year() ) - .arg( dt.month() ) - .arg( dt.day() ); - } - - else if ( value.userType() == QMetaType::Type::QTime ) - { - const QTime dt = value.toTime(); - if ( !dt.isValid() ) - return QObject::tr( "Invalid time" ); - else - return QStringLiteral( "%4:%5:%6" ) - .arg( dt.hour() ) - .arg( dt.minute() ) - .arg( dt.second() ); - } - - // else if ( value.userType() == QMetaType::QString ) - // { - // // In the case of a WKT-(string) encoded geometry, the type of geometry is going to be displayed - // // rather than the possibly very long WKT payload - // QgsGeometry g = QgsGeometry::fromWkt( value.toString() ); - // if ( !g.isNull() ) - // { - // return g.typeName(); - // } - // } return value.toString(); - - // return QStringLiteral( "%1 :: %2" ) - // .arg( value.userType() ) - // .arg( value.toString() ); - - // return value.userType() } @@ -4164,15 +4093,35 @@ QgsProcessingParameterGeometry *QgsProcessingParameterGeometry::fromScriptCode( QString QgsProcessingParameterGeometry::userFriendlyString( const QVariant &value ) const { - if ( value.isValid() && value.userType() == QMetaType::QString ) + if ( value.isValid() ) { - // In the case of a WKT-(string) encoded geometry, the type of geometry is going to be displayed - // rather than the possibly very long WKT payload - QgsGeometry g = QgsGeometry::fromWkt( value.toString() ); - if ( !g.isNull() ) + + if ( value.userType() == qMetaTypeId< QgsGeometry>() ) { + const QgsGeometry g = value.value(); return g.typeName(); } + + else if ( value.userType() == qMetaTypeId() ) + { + const QgsReferencedGeometry g = value.value(); + if ( !g.isNull() ) + { + return QStringLiteral( "[%1] %2" ).arg( g.crs().userFriendlyIdentifier( Qgis::CrsIdentifierType::ShortString ), g.typeName() ); + } + return g.typeName(); + } + + else if ( value.userType() == QMetaType::QString ) + { + // In the case of a WKT-(string) encoded geometry, the type of geometry is going to be displayed + // rather than the possibly very long WKT payload + QgsGeometry g = QgsGeometry::fromWkt( value.toString() ); + if ( !g.isNull() ) + { + return g.typeName(); + } + } } return QObject::tr( "Invalid geometry" ); @@ -7905,7 +7854,6 @@ QString QgsProcessingParameterDistance::userFriendlyString( const QVariant &valu } - // // QgsProcessingParameterArea // @@ -7987,6 +7935,11 @@ bool QgsProcessingParameterArea::fromVariantMap( const QVariantMap &map ) } +QString QgsProcessingParameterArea::userFriendlyString( const QVariant &value ) const +{ + return QStringLiteral( "%1 %2" ).arg( value.toString(), QgsUnitTypes::toString( defaultUnit() ) ); +} + // // QgsProcessingParameterVolume @@ -8068,6 +8021,10 @@ bool QgsProcessingParameterVolume::fromVariantMap( const QVariantMap &map ) return true; } +QString QgsProcessingParameterVolume::userFriendlyString( const QVariant &value ) const +{ + return QStringLiteral( "%1 %2" ).arg( value.toString(), QgsUnitTypes::toString( defaultUnit() ) ); +} // // QgsProcessingParameterDuration @@ -8125,6 +8082,11 @@ bool QgsProcessingParameterDuration::fromVariantMap( const QVariantMap &map ) return true; } +QString QgsProcessingParameterDuration::userFriendlyString( const QVariant &value ) const +{ + return QStringLiteral( "%1 %2" ).arg( value.toString(), QgsUnitTypes::toAbbreviatedString( defaultUnit() ) ); +} + // // QgsProcessingParameterScale @@ -8982,6 +8944,49 @@ QgsProcessingParameterDateTime *QgsProcessingParameterDateTime::fromScriptCode( } +QString QgsProcessingParameterDateTime:: userFriendlyString( const QVariant &value ) const +{ + if ( value.userType() == QMetaType::Type::QDateTime ) + { + const QDateTime dt = value.toDateTime(); + if ( !dt.isValid() ) + return QObject::tr( "Invalid datetime" ); + else + return QStringLiteral( "%1-%2-%3T%4:%5:%6" ) + .arg( dt.date().year() ) + .arg( dt.date().month() ) + .arg( dt.date().day() ) + .arg( dt.time().hour() ) + .arg( dt.time().minute() ) + .arg( dt.time().second() ); + } + + else if ( value.userType() == QMetaType::Type::QDate ) + { + const QDate dt = value.toDate(); + if ( !dt.isValid() ) + return QObject::tr( "Invalid date" ); + else + return QStringLiteral( "%1-%2-%3" ) + .arg( dt.year() ) + .arg( dt.month() ) + .arg( dt.day() ); + } + + else if ( value.userType() == QMetaType::Type::QTime ) + { + const QTime dt = value.toTime(); + if ( !dt.isValid() ) + return QObject::tr( "Invalid time" ); + else + return QStringLiteral( "%4:%5:%6" ) + .arg( dt.hour() ) + .arg( dt.minute() ) + .arg( dt.second() ); + } + + return value.toString(); +} // // QgsProcessingParameterProviderConnection diff --git a/src/core/processing/qgsprocessingparameters.h b/src/core/processing/qgsprocessingparameters.h index b5f89d4a9b2..b7c764de946 100644 --- a/src/core/processing/qgsprocessingparameters.h +++ b/src/core/processing/qgsprocessingparameters.h @@ -1978,7 +1978,7 @@ class CORE_EXPORT QgsProcessingParameterGeometry : public QgsProcessingParameter */ void setAllowMultipart( bool allowMultipart ) { mAllowMultipart = allowMultipart; } - QString userFriendlyString( const QVariant &value ) const; + QString userFriendlyString( const QVariant &value ) const override; /** * Creates a new parameter using the definition from a script code. @@ -2501,6 +2501,7 @@ class CORE_EXPORT QgsProcessingParameterArea : public QgsProcessingParameterNumb QVariantMap toVariantMap() const override; bool fromVariantMap( const QVariantMap &map ) override; + QString userFriendlyString( const QVariant &value ) const override; private: @@ -2584,6 +2585,7 @@ class CORE_EXPORT QgsProcessingParameterVolume : public QgsProcessingParameterNu QVariantMap toVariantMap() const override; bool fromVariantMap( const QVariantMap &map ) override; + QString userFriendlyString( const QVariant &value ) const override; private: @@ -2640,6 +2642,7 @@ class CORE_EXPORT QgsProcessingParameterDuration : public QgsProcessingParameter QVariantMap toVariantMap() const override; bool fromVariantMap( const QVariantMap &map ) override; + QString userFriendlyString( const QVariant &value ) const override; private: @@ -4289,6 +4292,7 @@ class CORE_EXPORT QgsProcessingParameterDateTime : public QgsProcessingParameter QVariantMap toVariantMap() const override; bool fromVariantMap( const QVariantMap &map ) override; + QString userFriendlyString( const QVariant &value ) const override; /** * Creates a new parameter using the definition from a script code. diff --git a/tests/src/analysis/testqgsprocessing.cpp b/tests/src/analysis/testqgsprocessing.cpp index 07aa3751b71..17931ab94fe 100644 --- a/tests/src/analysis/testqgsprocessing.cpp +++ b/tests/src/analysis/testqgsprocessing.cpp @@ -4340,6 +4340,10 @@ void TestQgsProcessing::parameterGeometry() // With Srid as string QCOMPARE( def->valueAsPythonString( QgsReferencedGeometry( QgsGeometry::fromWkt( QStringLiteral( "LineString( 10 10, 20 20)" ) ), QgsCoordinateReferenceSystem( "EPSG:4326" ) ), context ), QStringLiteral( "'CRS=EPSG:4326;LineString (10 10, 20 20)'" ) ); + QCOMPARE( def->userFriendlyString( QgsGeometry::fromWkt( QStringLiteral( "Polygon ((11.1 12.2, 13.3 12.2, 13.3 14.4, 11.1 14.4, 11.1 12.2))" ) ) ), QStringLiteral( "Polygon" ) ); + QCOMPARE( def->userFriendlyString( QgsReferencedGeometry( QgsGeometry::fromWkt( QStringLiteral( "LineString( 10 10, 20 20)" ) ), QgsCoordinateReferenceSystem( "EPSG:4326" ) ) ), QStringLiteral( "[EPSG:4326] Line" ) ); + QCOMPARE( def->userFriendlyString( QStringLiteral( "Polygon ((11.1 12.2, 13.3 12.2, 13.3 14.4, 11.1 14.4, 11.1 12.2))" ) ), QStringLiteral( "Polygon" ) ); + QCOMPARE( def->valueAsJsonObject( QVariant(), context ), QVariant() ); QCOMPARE( def->valueAsJsonObject( "LineString( 10 10, 20 20)", context ), QVariant( QStringLiteral( "LineString( 10 10, 20 20)" ) ) ); QCOMPARE( def->valueAsJsonObject( QgsGeometry::fromWkt( QStringLiteral( "LineString( 10 10, 20 20)" ) ), context ), QVariant( QStringLiteral( "LineString (10 10, 20 20)" ) ) ); @@ -4519,6 +4523,8 @@ void TestQgsProcessing::parameterFile() QCOMPARE( def->valueAsJsonObject( "uri='complex' username=\"complex\"", context ), QVariant( QStringLiteral( "uri='complex' username=\"complex\"" ) ) ); QCOMPARE( def->valueAsJsonObject( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QVariant( QStringLiteral( "c:\\test\\new data\\test.dat" ) ) ); + QCOMPARE( def->userFriendlyString( "bricks.bmp" ), QStringLiteral( "bricks.bmp" ) ); + const QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt // ensure valueAsJsonObject doesn't try to load a file path as a layer QCOMPARE( context.temporaryLayerStore()->count(), 0 ); @@ -5270,6 +5276,8 @@ void TestQgsProcessing::parameterDistance() QCOMPARE( def->valueAsPythonString( QStringLiteral( "1.1" ), context ), QStringLiteral( "1.1" ) ); QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) ); + QCOMPARE( def->userFriendlyString( QVariant( 5 ) ), QStringLiteral( "5 ft" ) ); + QCOMPARE( def->valueAsJsonObject( QVariant(), context ), QVariant() ); QCOMPARE( def->valueAsJsonObject( 5, context ), QVariant( 5 ) ); QCOMPARE( def->valueAsJsonObject( QStringLiteral( "1.1" ), context ), QVariant( QStringLiteral( "1.1" ) ) ); @@ -5385,6 +5393,7 @@ void TestQgsProcessing::parameterArea() QCOMPARE( def->valueAsPythonString( 5, context ), QStringLiteral( "5" ) ); QCOMPARE( def->valueAsPythonString( QStringLiteral( "1.1" ), context ), QStringLiteral( "1.1" ) ); QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) ); + QCOMPARE( def->userFriendlyString( QVariant( 5 ) ), QStringLiteral( "5 square feet" ) ); QCOMPARE( def->valueAsJsonObject( QVariant(), context ), QVariant() ); QCOMPARE( def->valueAsJsonObject( 5, context ), QVariant( 5 ) ); @@ -5501,6 +5510,7 @@ void TestQgsProcessing::parameterVolume() QCOMPARE( def->valueAsPythonString( 5, context ), QStringLiteral( "5" ) ); QCOMPARE( def->valueAsPythonString( QStringLiteral( "1.1" ), context ), QStringLiteral( "1.1" ) ); QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) ); + QCOMPARE( def->userFriendlyString( QVariant( 5 ) ), QStringLiteral( "5 cubic feet" ) ); QCOMPARE( def->valueAsJsonObject( QVariant(), context ), QVariant() ); QCOMPARE( def->valueAsJsonObject( 5, context ), QVariant( 5 ) ); @@ -5614,6 +5624,8 @@ void TestQgsProcessing::parameterDuration() QCOMPARE( def->valueAsPythonString( QStringLiteral( "1.1" ), context ), QStringLiteral( "1.1" ) ); QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) ); + QCOMPARE( def->userFriendlyString( QVariant( 5 ) ), QStringLiteral( "5 d" ) ); + QCOMPARE( def->valueAsJsonObject( QVariant(), context ), QVariant() ); QCOMPARE( def->valueAsJsonObject( 5, context ), QVariant( 5 ) ); QCOMPARE( def->valueAsJsonObject( QStringLiteral( "1.1" ), context ), QVariant( QStringLiteral( "1.1" ) ) ); @@ -5717,6 +5729,8 @@ void TestQgsProcessing::parameterScale() QCOMPARE( def->valueAsPythonString( QStringLiteral( "1.1" ), context ), QStringLiteral( "1.1" ) ); QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) ); + QCOMPARE( def->userFriendlyString( QVariant( 5 ) ), QStringLiteral( "5" ) ); + QCOMPARE( def->valueAsJsonObject( QVariant(), context ), QVariant() ); QCOMPARE( def->valueAsJsonObject( 5, context ), QVariant( "5" ) ); QCOMPARE( def->valueAsJsonObject( QStringLiteral( "1.1" ), context ), QVariant( QStringLiteral( "1.1" ) ) ); @@ -5836,6 +5850,8 @@ void TestQgsProcessing::parameterNumber() QCOMPARE( def->valueAsPythonString( QStringLiteral( "1.1" ), context ), QStringLiteral( "1.1" ) ); QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) ); + QCOMPARE( def->userFriendlyString( QVariant( 5 ) ), QStringLiteral( "5" ) ); + QCOMPARE( def->valueAsJsonObject( QVariant(), context ), QVariant() ); QCOMPARE( def->valueAsJsonObject( 5, context ), QVariant( "5" ) ); QCOMPARE( def->valueAsJsonObject( QStringLiteral( "1.1" ), context ), QVariant( QStringLiteral( "1.1" ) ) ); @@ -6664,6 +6680,8 @@ void TestQgsProcessing::parameterString() QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\"complex\"'" ) ); QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) ); + QCOMPARE( def->userFriendlyString( QVariant( "Hello" ) ), QStringLiteral( "Hello" ) ); + QCOMPARE( def->valueAsJsonObject( QVariant(), context ), QVariant() ); QCOMPARE( def->valueAsJsonObject( 5, context ), QVariant( 5 ) ); QCOMPARE( def->valueAsJsonObject( QStringLiteral( "abc" ), context ), QVariant( QStringLiteral( "abc" ) ) ); @@ -9681,6 +9699,8 @@ void TestQgsProcessing::parameterColor() QCOMPARE( def->valueAsPythonString( QColor( 255, 0, 0 ), context ), QStringLiteral( "QColor(255, 0, 0)" ) ); QCOMPARE( def->valueAsPythonString( QColor( 255, 0, 0, 100 ), context ), QStringLiteral( "QColor(255, 0, 0, 100)" ) ); + QCOMPARE( def->userFriendlyString( QColor( 255, 0, 0 ) ), QStringLiteral( "#ff0000" ) ); + QCOMPARE( def->valueAsJsonObject( QVariant(), context ), QVariant() ); QCOMPARE( def->valueAsJsonObject( QStringLiteral( "#ff0000" ), context ), QVariant( QStringLiteral( "#ff0000" ) ) ); QCOMPARE( def->valueAsJsonObject( QColor(), context ), QVariant( QString() ) ); @@ -11144,6 +11164,10 @@ void TestQgsProcessing::parameterDateTime() QCOMPARE( def->valueAsString( QDate( 2014, 12, 31 ), context, ok ), QStringLiteral( "2014-12-31" ) ); QVERIFY( ok ); + QCOMPARE( def->userFriendlyString( QDate( 2014, 12, 31 ) ), QStringLiteral( "2014-12-31" ) ); + QCOMPARE( def->userFriendlyString( QTime( 12, 11, 10 ) ), QStringLiteral( "12:11:10" ) ); + QCOMPARE( def->userFriendlyString( QDateTime( QDate( 2014, 12, 31 ), QTime( 12, 11, 10 ) ) ), QStringLiteral( "2014-12-31T12:11:10" ) ); + pythonCode = def->asPythonString(); QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterDateTime('non_optional', '', type=QgsProcessingParameterDateTime.Date, minValue=QDateTime(QDate(2015, 1, 1), QTime(0, 0, 0)), maxValue=QDateTime(QDate(2015, 12, 31), QTime(0, 0, 0)), defaultValue=QDate(2010, 4, 3))" ) );