Add internal version handling for model algorithms

Allows us to change behaviour of model algorithms (e.g. output names)
for newly created models only, without risk of breaking existing
scripts
This commit is contained in:
Nyall Dawson 2022-02-28 10:25:00 +10:00
parent 8ceaf00f98
commit 1d97b4548a
4 changed files with 40 additions and 0 deletions

View File

@ -21,6 +21,9 @@ Model based algorithm with processing.
%TypeHeaderCode
#include "qgsprocessingmodelalgorithm.h"
%End
public:
static const QMetaObject staticMetaObject;
public:
QgsProcessingModelAlgorithm( const QString &name = QString(), const QString &group = QString(), const QString &groupId = QString() );
@ -575,6 +578,7 @@ run through the designer.
.. versionadded:: 3.14
%End
protected:
virtual QgsProcessingAlgorithm *createInstance() const /Factory/;

View File

@ -1344,6 +1344,7 @@ QVariant QgsProcessingModelAlgorithm::toVariant() const
map.insert( QStringLiteral( "model_name" ), mModelName );
map.insert( QStringLiteral( "model_group" ), mModelGroup );
map.insert( QStringLiteral( "help" ), mHelpContent );
map.insert( QStringLiteral( "internal_version" ), qgsEnumValueToKey( mInternalVersion ) );
QVariantMap childMap;
QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
@ -1393,6 +1394,8 @@ bool QgsProcessingModelAlgorithm::loadVariant( const QVariant &model )
mModelGroupId = map.value( QStringLiteral( "model_group" ) ).toString();
mHelpContent = map.value( QStringLiteral( "help" ) ).toMap();
mInternalVersion = qgsEnumKeyToValue( map.value( QStringLiteral( "internal_version" ) ).toString(), InternalVersion::Version1 );
mVariables = map.value( QStringLiteral( "modelVariables" ) ).toMap();
mDesignerParameterValues = map.value( QStringLiteral( "designerParameterValues" ) ).toMap();

View File

@ -37,6 +37,8 @@
*/
class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
{
Q_GADGET
public:
/**
@ -523,6 +525,18 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
*/
void setDesignerParameterValues( const QVariantMap &values ) { mDesignerParameterValues = values; }
#ifndef SIP_RUN
//! Internal model versions
enum class InternalVersion
{
Version1, //!< Created in < 3.26
Version2, //!< Created in >= 3.26
};
Q_ENUM( InternalVersion )
#endif
protected:
QgsProcessingAlgorithm *createInstance() const override SIP_FACTORY;
@ -531,6 +545,8 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
private:
InternalVersion mInternalVersion = InternalVersion::Version2;
QString mModelName;
QString mModelGroup;
QString mModelGroupId;

View File

@ -105,6 +105,7 @@ class TestQgsProcessingModelAlgorithm: public QObject
void modelSource();
void modelNameMatchesFileName();
void renameModelParameter();
void internalVersion();
private:
@ -2204,5 +2205,21 @@ void TestQgsProcessingModelAlgorithm::renameModelParameter()
QCOMPARE( m.childAlgorithm( QStringLiteral( "cx1" ) ).parameterSources()[ QStringLiteral( "EXPRESSION" ) ].constFirst().expression(), QStringLiteral( "@apricot * 2 + @int2" ) );
}
void TestQgsProcessingModelAlgorithm::internalVersion()
{
// test internal version handling
QgsProcessingModelAlgorithm model;
// load older model, should be version 1
QVERIFY( model.fromFile( TEST_DATA_DIR + QStringLiteral( "/test_model.model3" ) ) );
QCOMPARE( model.mInternalVersion, QgsProcessingModelAlgorithm::InternalVersion::Version1 );
// create new model and save/restore, should be version 2
QgsProcessingModelAlgorithm model2;
QgsProcessingModelAlgorithm model3;
QVERIFY( model3.loadVariant( model2.toVariant() ) );
QCOMPARE( model3.mInternalVersion, QgsProcessingModelAlgorithm::InternalVersion::Version2 );
}
QGSTEST_MAIN( TestQgsProcessingModelAlgorithm )
#include "testqgsprocessingmodelalgorithm.moc"