Skip to content
Permalink
Browse files

Move model storage for graphic items to c++ base class

  • Loading branch information
nyalldawson committed Mar 2, 2020
1 parent 167a8d3 commit b567ddeccd9cf62b41fe85c2a1837c47963e1ffe
@@ -28,21 +28,124 @@ Base class for graphic items representing model components in the model designer
%End
public:

QgsModelComponentGraphicItem( QgsProcessingModelComponent *component /Transfer/, QGraphicsItem *parent /TransferThis/ );
QgsModelComponentGraphicItem( QgsProcessingModelComponent *component /Transfer/,
QgsProcessingModelAlgorithm *model,
QGraphicsItem *parent /TransferThis/ );
%Docstring
Constructor for QgsModelComponentGraphicItem for the specified ``component``, with the specified ``parent`` item.

The ``model`` argument specifies the associated processing model. Ownership of ``model`` is not transferred, and
it must exist for the lifetime of this object.

Ownership of ``component`` is transferred to the item.
%End

QgsProcessingModelComponent *component();
%Docstring
Returns the model component associated with this item.
%End

QgsProcessingModelAlgorithm *model();
%Docstring
Returns the model associated with this item.
%End

};

class QgsModelParameterGraphicItem : QgsModelComponentGraphicItem
{
%Docstring
A graphic item representing a model parameter (input) in the model designer.

.. warning::

Not stable API

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsmodelcomponentgraphicitem.h"
%End
public:

QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter /Transfer/,
QgsProcessingModelAlgorithm *model,
QGraphicsItem *parent /TransferThis/ );
%Docstring
Constructor for QgsModelParameterGraphicItem for the specified ``parameter``, with the specified ``parent`` item.

The ``model`` argument specifies the associated processing model. Ownership of ``model`` is not transferred, and
it must exist for the lifetime of this object.

Ownership of ``parameter`` is transferred to the item.
%End

};

class QgsModelChildAlgorithmGraphicItem : QgsModelComponentGraphicItem
{
%Docstring
A graphic item representing a child algorithm in the model designer.

.. warning::

Not stable API

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsmodelcomponentgraphicitem.h"
%End
public:

QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child /Transfer/,
QgsProcessingModelAlgorithm *model,
QGraphicsItem *parent /TransferThis/ );
%Docstring
Constructor for QgsModelChildAlgorithmGraphicItem for the specified ``child``, with the specified ``parent`` item.

The ``model`` argument specifies the associated processing model. Ownership of ``model`` is not transferred, and
it must exist for the lifetime of this object.

Ownership of ``child`` is transferred to the item.
%End

};


class QgsModelOutputGraphicItem : QgsModelComponentGraphicItem
{
%Docstring
A graphic item representing a model output in the model designer.

.. warning::

Not stable API

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsmodelcomponentgraphicitem.h"
%End
public:

QgsModelOutputGraphicItem( QgsProcessingModelOutput *output /Transfer/,
QgsProcessingModelAlgorithm *model,
QGraphicsItem *parent /TransferThis/ );
%Docstring
Constructor for QgsModelOutputGraphicItem for the specified ``output``, with the specified ``parent`` item.

The ``model`` argument specifies the associated processing model. Ownership of ``model`` is not transferred, and
it must exist for the lifetime of this object.

Ownership of ``output`` is transferred to the item.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
@@ -57,9 +57,8 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):
BUTTON_HEIGHT = 16

def __init__(self, element, model, controls, scene=None):
super().__init__(element, None)
super().__init__(element, model, None)
self.controls = controls
self.model = model
self.scene = scene
self.box_width = ModelerGraphicItem.BOX_WIDTH
self.box_height = ModelerGraphicItem.BOX_HEIGHT
@@ -74,7 +73,7 @@ def __init__(self, element, model, controls, scene=None):
painter = QPainter(self.picture)
svg.render(painter)
painter.end()
paramDef = self.model.parameterDefinition(element.parameterName())
paramDef = self.model().parameterDefinition(element.parameterName())
if paramDef:
self.text = paramDef.description()
else:
@@ -144,7 +143,7 @@ def __init__(self, element, model, controls, scene=None):
def foldInput(self, folded):
self.component().setParametersCollapsed(folded)
#also need to update the model's stored component
self.model.childAlgorithm(self.component().childId()).setParametersCollapsed(folded)
self.model().childAlgorithm(self.component().childId()).setParametersCollapsed(folded)
self.prepareGeometryChange()
if self.component().algorithm().outputDefinitions():
pt = self.getLinkPointForOutput(-1)
@@ -157,7 +156,7 @@ def foldInput(self, folded):
def foldOutput(self, folded):
self.component().setOutputsCollapsed(folded)
# also need to update the model's stored component
self.model.childAlgorithm(self.component().childId()).setOutputsCollapsed(folded)
self.model().childAlgorithm(self.component().childId()).setOutputsCollapsed(folded)
self.prepareGeometryChange()
for arrow in self.arrows:
arrow.updatePath()
@@ -227,11 +226,11 @@ def contextMenuEvent(self, event):
popupmenu.exec_(event.screenPos())

def deactivateAlgorithm(self):
self.model.deactivateChildAlgorithm(self.component().childId())
self.model().deactivateChildAlgorithm(self.component().childId())
self.scene.dialog.repaintModel()

def activateAlgorithm(self):
if self.model.activateChildAlgorithm(self.component().childId()):
if self.model().activateChildAlgorithm(self.component().childId()):
self.scene.dialog.repaintModel()
else:
QMessageBox.warning(None, 'Could not activate Algorithm',
@@ -246,16 +245,16 @@ def create_widget_context(self):
widget_context.setProject(QgsProject.instance())
if iface is not None:
widget_context.setMapCanvas(iface.mapCanvas())
widget_context.setModel(self.model)
widget_context.setModel(self.model())
return widget_context

def editElement(self):
if isinstance(self.component(), QgsProcessingModelParameter):
existing_param = self.model.parameterDefinition(self.component().parameterName())
existing_param = self.model().parameterDefinition(self.component().parameterName())
new_param = None
if ModelerParameterDefinitionDialog.use_legacy_dialog(param=existing_param):
# boo, old api
dlg = ModelerParameterDefinitionDialog(self.model,
dlg = ModelerParameterDefinitionDialog(self.model(),
param=existing_param)
if dlg.exec_():
new_param = dlg.param
@@ -267,40 +266,40 @@ def editElement(self):
context=context,
widgetContext=widget_context,
definition=existing_param,
algorithm=self.model)
algorithm=self.model())
if dlg.exec_():
new_param = dlg.createParameter(existing_param.name())

if new_param is not None:
self.model.removeModelParameter(self.component().parameterName())
self.model().removeModelParameter(self.component().parameterName())
self.component().setParameterName(new_param.name())
self.component().setDescription(new_param.name())
self.model.addModelParameter(new_param, self.component())
self.model().addModelParameter(new_param, self.component())
self.text = new_param.description()
self.scene.dialog.repaintModel()
elif isinstance(self.component(), QgsProcessingModelChildAlgorithm):
elemAlg = self.component().algorithm()
dlg = ModelerParametersDialog(elemAlg, self.model, self.component().childId(), self.component().configuration())
dlg = ModelerParametersDialog(elemAlg, self.model(), self.component().childId(), self.component().configuration())
if dlg.exec_():
alg = dlg.createAlgorithm()
alg.setChildId(self.component().childId())
self.updateAlgorithm(alg)
self.scene.dialog.repaintModel()

elif isinstance(self.component(), QgsProcessingModelOutput):
child_alg = self.model.childAlgorithm(self.component().childId())
child_alg = self.model().childAlgorithm(self.component().childId())
param_name = '{}:{}'.format(self.component().childId(), self.component().name())
dlg = ModelerParameterDefinitionDialog(self.model,
param=self.model.parameterDefinition(param_name))
dlg = ModelerParameterDefinitionDialog(self.model(),
param=self.model().parameterDefinition(param_name))
if dlg.exec_() and dlg.param is not None:
model_output = child_alg.modelOutput(self.component().name())
model_output.setDescription(dlg.param.description())
model_output.setDefaultValue(dlg.param.defaultValue())
model_output.setMandatory(not (dlg.param.flags() & QgsProcessingParameterDefinition.FlagOptional))
self.model.updateDestinationParameters()
self.model().updateDestinationParameters()

def updateAlgorithm(self, alg):
existing_child = self.model.childAlgorithm(alg.childId())
existing_child = self.model().childAlgorithm(alg.childId())
alg.setPosition(existing_child.position())
alg.setParametersCollapsed(existing_child.parametersCollapsed())
alg.setOutputsCollapsed(existing_child.outputsCollapsed())
@@ -309,33 +308,33 @@ def updateAlgorithm(self, alg):
alg.position() + QPointF(
self.box_width,
(i + 1.5) * self.box_height))
self.model.setChildAlgorithm(alg)
self.model().setChildAlgorithm(alg)

def removeElement(self):
if isinstance(self.component(), QgsProcessingModelParameter):
if self.model.childAlgorithmsDependOnParameter(self.component().parameterName()):
if self.model().childAlgorithmsDependOnParameter(self.component().parameterName()):
QMessageBox.warning(None, 'Could not remove input',
'Algorithms depend on the selected input.\n'
'Remove them before trying to remove it.')
elif self.model.otherParametersDependOnParameter(self.component().parameterName()):
elif self.model().otherParametersDependOnParameter(self.component().parameterName()):
QMessageBox.warning(None, 'Could not remove input',
'Other inputs depend on the selected input.\n'
'Remove them before trying to remove it.')
else:
self.model.removeModelParameter(self.component().parameterName())
self.model().removeModelParameter(self.component().parameterName())
self.scene.dialog.haschanged = True
self.scene.dialog.repaintModel()
elif isinstance(self.component(), QgsProcessingModelChildAlgorithm):
if not self.model.removeChildAlgorithm(self.component().childId()):
if not self.model().removeChildAlgorithm(self.component().childId()):
QMessageBox.warning(None, 'Could not remove element',
'Other elements depend on the selected one.\n'
'Remove them before trying to remove it.')
else:
self.scene.dialog.haschanged = True
self.scene.dialog.repaintModel()
elif isinstance(self.component(), QgsProcessingModelOutput):
self.model.childAlgorithm(self.component().childId()).removeModelOutput(self.component().name())
self.model.updateDestinationParameters()
self.model().childAlgorithm(self.component().childId()).removeModelOutput(self.component().name())
self.model().updateDestinationParameters()
self.scene.dialog.haschanged = True
self.scene.dialog.repaintModel()

@@ -469,11 +468,11 @@ def itemChange(self, change, value):

# also need to update the model's stored component's position
if isinstance(self.component(), QgsProcessingModelChildAlgorithm):
self.model.childAlgorithm(self.component().childId()).setPosition(self.pos())
self.model().childAlgorithm(self.component().childId()).setPosition(self.pos())
elif isinstance(self.component(), QgsProcessingModelParameter):
self.model.parameterComponent(self.component().parameterName()).setPosition(self.pos())
self.model().parameterComponent(self.component().parameterName()).setPosition(self.pos())
elif isinstance(self.component(), QgsProcessingModelOutput):
self.model.childAlgorithm(self.component().childId()).modelOutput(self.component().name()).setPosition(self.pos())
self.model().childAlgorithm(self.component().childId()).modelOutput(self.component().name()).setPosition(self.pos())
elif change == QGraphicsItem.ItemSelectedChange:
self.repaintArrows()

@@ -17,7 +17,6 @@
***************************************************************************
"""


__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
@@ -27,7 +26,12 @@
from qgis.core import (QgsProcessingParameterDefinition,
QgsProcessingModelChildParameterSource,
QgsExpression)
from qgis.gui import QgsModelGraphicsScene
from qgis.gui import (
QgsModelGraphicsScene,
QgsModelParameterGraphicItem,
QgsModelChildAlgorithmGraphicItem,
QgsModelOutputGraphicItem
)
from processing.modeler.ModelerGraphicItem import ModelerGraphicItem
from processing.modeler.ModelerArrowItem import ModelerArrowItem
from processing.tools.dataobjects import createContext
@@ -15,13 +15,17 @@

#include "qgsmodelcomponentgraphicitem.h"
#include "qgsprocessingmodelcomponent.h"
#include "qgsprocessingmodelparameter.h"
#include "qgsprocessingmodelchildalgorithm.h"
#include "qgsprocessingmodeloutput.h"
#include "qgsmodelgraphicsscene.h"

///@cond NOT_STABLE

QgsModelComponentGraphicItem::QgsModelComponentGraphicItem( QgsProcessingModelComponent *component, QGraphicsItem *parent )
QgsModelComponentGraphicItem::QgsModelComponentGraphicItem( QgsProcessingModelComponent *component, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent )
: QGraphicsObject( parent )
, mComponent( component )
, mModel( model )
{
setAcceptHoverEvents( true );
setFlag( QGraphicsItem::ItemIsMovable, true );
@@ -35,4 +39,29 @@ QgsProcessingModelComponent *QgsModelComponentGraphicItem::component()
return mComponent.get();
}

QgsProcessingModelAlgorithm *QgsModelComponentGraphicItem::model()
{
return mModel;
}

QgsModelParameterGraphicItem::QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent )
: QgsModelComponentGraphicItem( parameter, model, parent )
{

}



QgsModelChildAlgorithmGraphicItem::QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent )
: QgsModelComponentGraphicItem( child, model, parent )
{

}


QgsModelOutputGraphicItem::QgsModelOutputGraphicItem( QgsProcessingModelOutput *output, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent )
: QgsModelComponentGraphicItem( output, model, parent )
{
}

///@endcond

0 comments on commit b567dde

Please sign in to comment.
You can’t perform that action at this time.