Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bit more Python to c++ porting
  • Loading branch information
nyalldawson committed Mar 5, 2020
1 parent 889563e commit 64868ce
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 24 deletions.
Expand Up @@ -35,6 +35,16 @@ should be set to a QgsProcessingAlgorithm algorithm ID.
virtual QgsProcessingModelChildAlgorithm *clone() const /Factory/;


void copyNonDefinitionPropertiesFromModel( QgsProcessingModelAlgorithm *model );
%Docstring
Copies all non-specific definition properties from the the matching component from a ``model``.

This includes properties like the size and position of the component, but not properties
like the specific algorithm or input details.

.. versionadded:: 3.14
%End

QString childId() const;
%Docstring
Returns the child algorithm's unique ID string, used the identify
Expand Down
Expand Up @@ -134,6 +134,16 @@ Saves the component properties to a QVariantMap.
Restores the component properties from a QVariantMap.

.. seealso:: :py:func:`saveCommonProperties`
%End

void copyNonDefinitionProperties( const QgsProcessingModelComponent &other );
%Docstring
Copies all non-specific definition properties from the ``other`` component definition.

This includes properties like the size and position of the component, but not properties
like the specific algorithm or input details.

.. versionadded:: 3.14
%End

};
Expand Down
27 changes: 3 additions & 24 deletions python/plugins/processing/modeler/ModelerGraphicItem.py
Expand Up @@ -29,8 +29,7 @@
QgsProcessingParameterWidgetContext,
QgsModelParameterGraphicItem,
QgsModelChildAlgorithmGraphicItem,
QgsModelOutputGraphicItem,
QgsModelCommentGraphicItem
QgsModelOutputGraphicItem
)
from processing.modeler.ModelerParameterDefinitionDialog import ModelerParameterDefinitionDialog
from processing.modeler.ModelerParametersDialog import ModelerParametersDialog
Expand Down Expand Up @@ -129,7 +128,8 @@ def edit(self, edit_comment=False):
if dlg.exec_():
alg = dlg.createAlgorithm()
alg.setChildId(self.component().childId())
self.updateAlgorithm(alg)
alg.copyNonDefinitionPropertiesFromModel(self.model())
self.model().setChildAlgorithm(alg)
self.requestModelRepaint.emit()
self.changed.emit()

Expand All @@ -139,27 +139,6 @@ def editComponent(self):
def editComment(self):
self.edit(edit_comment=True)

def updateAlgorithm(self, alg):
existing_child = self.model().childAlgorithm(alg.childId())
alg.setPosition(existing_child.position())
alg.setLinksCollapsed(Qt.TopEdge, existing_child.linksCollapsed(Qt.TopEdge))
alg.setLinksCollapsed(Qt.BottomEdge, existing_child.linksCollapsed(Qt.BottomEdge))
alg.comment().setPosition(existing_child.comment().position()
or alg.position() + QPointF(
self.component().size().width(),
-1.5 * self.component().size().height())
)
alg.comment().setSize(existing_child.comment().size())
for i, out in enumerate(alg.modelOutputs().keys()):
alg.modelOutput(out).setPosition(existing_child.modelOutput(out).position()
or alg.position() + QPointF(
self.component().size().width(),
(i + 1.5) * self.component().size().height()))
alg.modelOutput(out).comment().setDescription(existing_child.modelOutput(out).comment().description())
alg.modelOutput(out).comment().setSize(existing_child.modelOutput(out).comment().size())
alg.modelOutput(out).comment().setPosition(existing_child.modelOutput(out).comment().position())
self.model().setChildAlgorithm(alg)


class ModelerOutputGraphicItem(QgsModelOutputGraphicItem):
"""
Expand Down
26 changes: 26 additions & 0 deletions src/core/processing/models/qgsprocessingmodelchildalgorithm.cpp
Expand Up @@ -59,6 +59,32 @@ QgsProcessingModelChildAlgorithm *QgsProcessingModelChildAlgorithm::clone() cons
return new QgsProcessingModelChildAlgorithm( *this );
}

void QgsProcessingModelChildAlgorithm::copyNonDefinitionPropertiesFromModel( QgsProcessingModelAlgorithm *model )
{
const QgsProcessingModelChildAlgorithm existingChild = model->childAlgorithm( mId );
copyNonDefinitionProperties( existingChild );

int i = 0;
for ( auto it = mModelOutputs.begin(); it != mModelOutputs.end(); ++it )
{
if ( !existingChild.modelOutputs().value( it.key() ).position().isNull() )
it.value().setPosition( existingChild.modelOutputs().value( it.key() ).position() );
else
it.value().setPosition( position() + QPointF( size().width(), ( i + 1.5 ) * size().height() ) );

if ( QgsProcessingModelComment *comment = it.value().comment() )
{
if ( const QgsProcessingModelComment *existingComment = existingChild.modelOutputs().value( it.key() ).comment() )
{
comment->setDescription( existingComment->description() );
comment->setSize( existingComment->size() );
comment->setPosition( existingComment->position() );
}
}
i++;
}
}

const QgsProcessingAlgorithm *QgsProcessingModelChildAlgorithm::algorithm() const
{
return mAlgorithm.get();
Expand Down
10 changes: 10 additions & 0 deletions src/core/processing/models/qgsprocessingmodelchildalgorithm.h
Expand Up @@ -51,6 +51,16 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo

QgsProcessingModelChildAlgorithm *clone() const override SIP_FACTORY;

/**
* Copies all non-specific definition properties from the the matching component from a \a model.
*
* This includes properties like the size and position of the component, but not properties
* like the specific algorithm or input details.
*
* \since QGIS 3.14
*/
void copyNonDefinitionPropertiesFromModel( QgsProcessingModelAlgorithm *model );

/**
* Returns the child algorithm's unique ID string, used the identify
* this child algorithm within its parent model.
Expand Down
16 changes: 16 additions & 0 deletions src/core/processing/models/qgsprocessingmodelcomponent.cpp
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsprocessingmodelcomponent.h"
#include "qgsprocessingmodelcomment.h"

///@cond NOT_STABLE

Expand Down Expand Up @@ -117,4 +118,19 @@ void QgsProcessingModelComponent::restoreCommonProperties( const QVariantMap &ma
mBottomEdgeLinksCollapsed = map.value( QStringLiteral( "outputs_collapsed" ) ).toBool();
}

void QgsProcessingModelComponent::copyNonDefinitionProperties( const QgsProcessingModelComponent &other )
{
setPosition( other.position() );
setLinksCollapsed( Qt::TopEdge, other.linksCollapsed( Qt::TopEdge ) );
setLinksCollapsed( Qt::BottomEdge, other.linksCollapsed( Qt::BottomEdge ) );
if ( comment() && other.comment() )
{
if ( !other.comment()->position().isNull() )
comment()->setPosition( other.comment()->position() );
else
comment()->setPosition( other.position() + QPointF( size().width(), -1.5 * size().height() ) );
comment()->setSize( other.comment()->size() );
}
}

///@endcond
10 changes: 10 additions & 0 deletions src/core/processing/models/qgsprocessingmodelcomponent.h
Expand Up @@ -137,6 +137,16 @@ class CORE_EXPORT QgsProcessingModelComponent
*/
void restoreCommonProperties( const QVariantMap &map );

/**
* Copies all non-specific definition properties from the \a other component definition.
*
* This includes properties like the size and position of the component, but not properties
* like the specific algorithm or input details.
*
* \since QGIS 3.14
*/
void copyNonDefinitionProperties( const QgsProcessingModelComponent &other );

private:

static constexpr double DEFAULT_COMPONENT_WIDTH = 200;
Expand Down

0 comments on commit 64868ce

Please sign in to comment.