Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add API for storing group boxes in processing models
  • Loading branch information
nyalldawson committed Apr 1, 2020
1 parent b93292b commit a7a64d4
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 2 deletions.
Expand Up @@ -10,6 +10,7 @@




class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
{
%Docstring
Expand Down Expand Up @@ -281,6 +282,37 @@ this name a new component will be added to the model and returned.
Updates the model's parameter definitions to include all relevant destination
parameters as required by child algorithm ModelOutputs.
Must be called whenever child algorithm ModelOutputs are altered.
%End

void addGroupBox( const QgsProcessingModelGroupBox &groupBox );
%Docstring
Adds a new group ``box`` to the model.

If an existing group box with the same uuid already exists then its definition will be replaced.

.. seealso:: :py:func:`groupBoxes`

.. versionadded:: 3.14
%End

QList< QgsProcessingModelGroupBox > groupBoxes() const;
%Docstring
Returns a list of the group boxes within the model.

.. seealso:: :py:func:`addGroupBox`

.. versionadded:: 3.14
%End

void removeGroupBox( const QString &uuid );
%Docstring
Removes the group box with matching ``uuid`` from the model.

.. seealso:: :py:func:`addGroupBox`

.. seealso:: :py:func:`groupBoxes`

.. versionadded:: 3.14
%End

bool toFile( const QString &path ) const;
Expand Down
35 changes: 33 additions & 2 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsapplication.h"
#include "qgsprocessingparametertype.h"
#include "qgsexpressioncontextutils.h"
#include "qgsprocessingmodelgroupbox.h"

#include <QFile>
#include <QTextStream>
Expand Down Expand Up @@ -1190,6 +1191,21 @@ void QgsProcessingModelAlgorithm::updateDestinationParameters()
}
}

void QgsProcessingModelAlgorithm::addGroupBox( const QgsProcessingModelGroupBox &groupBox )
{
mGroupBoxes.insert( groupBox.uuid(), groupBox );
}

QList<QgsProcessingModelGroupBox> QgsProcessingModelAlgorithm::groupBoxes() const
{
return mGroupBoxes.values();
}

void QgsProcessingModelAlgorithm::removeGroupBox( const QString &uuid )
{
mGroupBoxes.remove( uuid );
}

QVariant QgsProcessingModelAlgorithm::toVariant() const
{
QVariantMap map;
Expand All @@ -1214,13 +1230,19 @@ QVariant QgsProcessingModelAlgorithm::toVariant() const
map.insert( QStringLiteral( "parameters" ), paramMap );

QVariantMap paramDefMap;
const auto constMParameters = mParameters;
for ( const QgsProcessingParameterDefinition *def : constMParameters )
for ( const QgsProcessingParameterDefinition *def : mParameters )
{
paramDefMap.insert( def->name(), def->toVariantMap() );
}
map.insert( QStringLiteral( "parameterDefinitions" ), paramDefMap );

QVariantList groupBoxDefs;
for ( auto it = mGroupBoxes.constBegin(); it != mGroupBoxes.constEnd(); ++it )
{
groupBoxDefs.append( it.value().toVariant() );
}
map.insert( QStringLiteral( "groupBoxes" ), groupBoxDefs );

map.insert( QStringLiteral( "modelVariables" ), mVariables );

map.insert( QStringLiteral( "designerParameterValues" ), mDesignerParameterValues );
Expand Down Expand Up @@ -1289,6 +1311,15 @@ bool QgsProcessingModelAlgorithm::loadVariant( const QVariant &model )
}
}

mGroupBoxes.clear();
const QVariantList groupBoxList = map.value( QStringLiteral( "groupBoxes" ) ).toList();
for ( const QVariant &groupBoxDef : groupBoxList )
{
QgsProcessingModelGroupBox groupBox;
groupBox.loadVariant( groupBoxDef.toMap() );
mGroupBoxes.insert( groupBox.uuid(), groupBox );
}

updateDestinationParameters();

return true;
Expand Down
31 changes: 31 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.h
Expand Up @@ -25,6 +25,8 @@
#include "qgsprocessingmodelparameter.h"
#include "qgsprocessingmodelchildparametersource.h"

class QgsProcessingModelGroupBox;

///@cond NOT_STABLE

/**
Expand Down Expand Up @@ -241,6 +243,33 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
*/
void updateDestinationParameters();

/**
* Adds a new group \a box to the model.
*
* If an existing group box with the same uuid already exists then its definition will be replaced.
*
* \see groupBoxes()
* \since QGIS 3.14
*/
void addGroupBox( const QgsProcessingModelGroupBox &groupBox );

/**
* Returns a list of the group boxes within the model.
*
* \see addGroupBox()
* \since QGIS 3.14
*/
QList< QgsProcessingModelGroupBox > groupBoxes() const;

/**
* Removes the group box with matching \a uuid from the model.
*
* \see addGroupBox()
* \see groupBoxes()
* \since QGIS 3.14
*/
void removeGroupBox( const QString &uuid );

/**
* Writes the model to a file, at the specified \a path.
* \see fromFile()
Expand Down Expand Up @@ -452,6 +481,8 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm

QVariantMap mDesignerParameterValues;

QMap< QString, QgsProcessingModelGroupBox > mGroupBoxes;

void dependsOnChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;
void dependentChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;

Expand Down
8 changes: 8 additions & 0 deletions src/core/processing/models/qgsprocessingmodelgroupbox.cpp
Expand Up @@ -21,6 +21,7 @@

QgsProcessingModelGroupBox::QgsProcessingModelGroupBox( const QString &description )
: QgsProcessingModelComponent( description )
, mUuid( QUuid::createUuid().toString() )
{
setSize( QSizeF( 100, 60 ) );
}
Expand All @@ -33,15 +34,22 @@ QgsProcessingModelGroupBox *QgsProcessingModelGroupBox::clone() const
QVariant QgsProcessingModelGroupBox::toVariant() const
{
QVariantMap map;
map.insert( QStringLiteral( "uuid" ), mUuid );
saveCommonProperties( map );
return map;
}

bool QgsProcessingModelGroupBox::loadVariant( const QVariantMap &map )
{
restoreCommonProperties( map );
mUuid = map.value( QStringLiteral( "uuid" ) ).toString();
return true;
}

QString QgsProcessingModelGroupBox::uuid() const
{
return mUuid;
}


///@endcond
9 changes: 9 additions & 0 deletions src/core/processing/models/qgsprocessingmodelgroupbox.h
Expand Up @@ -52,6 +52,15 @@ class CORE_EXPORT QgsProcessingModelGroupBox : public QgsProcessingModelComponen
* \see toVariant()
*/
bool loadVariant( const QVariantMap &map );

/**
* Returns the unique ID associated with this group box.
*/
QString uuid() const;

private:

QString mUuid;
};

///@endcond
Expand Down
1 change: 1 addition & 0 deletions src/gui/processing/models/qgsmodeldesignerdialog.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsmodelviewtoolselect.h"
#include "qgsmodelgraphicsscene.h"
#include "qgsmodelcomponentgraphicitem.h"
#include "processing/models/qgsprocessingmodelgroupbox.h"

#include <QShortcut>
#include <QDesktopWidget>
Expand Down
45 changes: 45 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsprocessingcontext.h"
#include "qgsprocessingparametertype.h"
#include "qgsprocessingmodelalgorithm.h"
#include "qgsprocessingmodelgroupbox.h"
#include "qgsnativealgorithms.h"
#include <QObject>
#include <QtTest/QSignalSpy>
Expand Down Expand Up @@ -8281,6 +8282,31 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( comment2.description(), QStringLiteral( "a comment" ) );
QCOMPARE( comment2.color(), QColor( 123, 45, 67 ) );

// group boxes
QgsProcessingModelGroupBox groupBox;
groupBox.setSize( QSizeF( 9, 8 ) );
QCOMPARE( groupBox.size(), QSizeF( 9, 8 ) );
groupBox.setPosition( QPointF( 11, 14 ) );
QCOMPARE( groupBox.position(), QPointF( 11, 14 ) );
groupBox.setDescription( QStringLiteral( "a comment" ) );
QCOMPARE( groupBox.description(), QStringLiteral( "a comment" ) );
groupBox.setColor( QColor( 123, 45, 67 ) );
QCOMPARE( groupBox.color(), QColor( 123, 45, 67 ) );
std::unique_ptr< QgsProcessingModelGroupBox > groupClone( groupBox.clone() );
QCOMPARE( groupClone->toVariant(), groupBox.toVariant() );
QCOMPARE( groupClone->size(), QSizeF( 9, 8 ) );
QCOMPARE( groupClone->position(), QPointF( 11, 14 ) );
QCOMPARE( groupClone->description(), QStringLiteral( "a comment" ) );
QCOMPARE( groupClone->color(), QColor( 123, 45, 67 ) );
QCOMPARE( groupClone->uuid(), groupBox.uuid() );
QgsProcessingModelGroupBox groupBox2;
groupBox2.loadVariant( groupBox.toVariant().toMap() );
QCOMPARE( groupBox2.size(), QSizeF( 9, 8 ) );
QCOMPARE( groupBox2.position(), QPointF( 11, 14 ) );
QCOMPARE( groupBox2.description(), QStringLiteral( "a comment" ) );
QCOMPARE( groupBox2.color(), QColor( 123, 45, 67 ) );
QCOMPARE( groupBox2.uuid(), groupBox.uuid() );

QMap< QString, QString > friendlyOutputNames;
QgsProcessingModelChildAlgorithm child( QStringLiteral( "some_id" ) );
QCOMPARE( child.algorithmId(), QStringLiteral( "some_id" ) );
Expand Down Expand Up @@ -8453,6 +8479,17 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( alg.shortDescription(), QStringLiteral( "short" ) );
QCOMPARE( alg.helpUrl(), QStringLiteral( "url" ) );

QVERIFY( alg.groupBoxes().isEmpty() );
alg.addGroupBox( groupBox );
QCOMPARE( alg.groupBoxes().size(), 1 );
QCOMPARE( alg.groupBoxes().at( 0 ).uuid(), groupBox.uuid() );
QCOMPARE( alg.groupBoxes().at( 0 ).uuid(), groupBox.uuid() );
alg.removeGroupBox( QStringLiteral( "a" ) );
QCOMPARE( alg.groupBoxes().size(), 1 );
alg.removeGroupBox( groupBox.uuid() );
QVERIFY( alg.groupBoxes().isEmpty() );


QVariantMap lastParams;
lastParams.insert( QStringLiteral( "a" ), 2 );
lastParams.insert( QStringLiteral( "b" ), 4 );
Expand Down Expand Up @@ -8790,6 +8827,7 @@ void TestQgsProcessing::modelerAlgorithm()
QgsProcessingModelAlgorithm alg5( "test", "testGroup" );
alg5.helpContent().insert( "author", "me" );
alg5.helpContent().insert( "usage", "run" );
alg5.addGroupBox( groupBox );
QVariantMap variables;
variables.insert( QStringLiteral( "v1" ), 5 );
variables.insert( QStringLiteral( "v2" ), QStringLiteral( "aabbccd" ) );
Expand Down Expand Up @@ -8849,6 +8887,13 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( alg6.helpContent(), alg5.helpContent() );
QCOMPARE( alg6.variables(), variables );
QCOMPARE( alg6.designerParameterValues(), lastParams );

QCOMPARE( alg6.groupBoxes().size(), 1 );
QCOMPARE( alg6.groupBoxes().at( 0 ).size(), QSizeF( 9, 8 ) );
QCOMPARE( alg6.groupBoxes().at( 0 ).position(), QPointF( 11, 14 ) );
QCOMPARE( alg6.groupBoxes().at( 0 ).description(), QStringLiteral( "a comment" ) );
QCOMPARE( alg6.groupBoxes().at( 0 ).color(), QColor( 123, 45, 67 ) );

QgsProcessingModelChildAlgorithm alg6c1 = alg6.childAlgorithm( "cx1" );
QCOMPARE( alg6c1.childId(), QStringLiteral( "cx1" ) );
QCOMPARE( alg6c1.algorithmId(), QStringLiteral( "buffer" ) );
Expand Down
1 change: 1 addition & 0 deletions tests/src/gui/testprocessinggui.cpp
Expand Up @@ -40,6 +40,7 @@
#include "qgsprocessingmaplayercombobox.h"
#include "qgsnativealgorithms.h"
#include "processing/models/qgsprocessingmodelalgorithm.h"
#include "processing/models/qgsprocessingmodelgroupbox.h"
#include "qgsxmlutils.h"
#include "qgspropertyoverridebutton.h"
#include "qgsprojectionselectionwidget.h"
Expand Down

0 comments on commit a7a64d4

Please sign in to comment.