Skip to content

Commit

Permalink
Use signals to avoid hard dependancy between component graphic items …
Browse files Browse the repository at this point in the history
…and scene
  • Loading branch information
nyalldawson committed Mar 2, 2020
1 parent 1d25155 commit fe4352c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ Returns the model component associated with this item.
QgsProcessingModelAlgorithm *model();
%Docstring
Returns the model associated with this item.
%End

signals:


void requestModelRepaint();
%Docstring
Emitted by the item to request a repaint of the parent model scene.
%End


void changed();
%Docstring
Emitted when the definition of the associated component is changed
by the item.
%End

};
Expand Down
23 changes: 11 additions & 12 deletions python/plugins/processing/modeler/ModelerGraphicItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):
BUTTON_WIDTH = 16
BUTTON_HEIGHT = 16

def __init__(self, element, model, scene=None):
def __init__(self, element, model):
super().__init__(element, model, None)
self.scene = scene
self.box_width = ModelerGraphicItem.BOX_WIDTH
self.box_height = ModelerGraphicItem.BOX_HEIGHT
self.item_font = QFont()
Expand Down Expand Up @@ -223,11 +222,11 @@ def contextMenuEvent(self, event):

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

def activateAlgorithm(self):
if self.model().activateChildAlgorithm(self.component().childId()):
self.scene.dialog.repaintModel()
self.requestModelRepaint.emit()
else:
QMessageBox.warning(None, 'Could not activate Algorithm',
'The selected algorithm depends on other currently non-active algorithms.\n'
Expand Down Expand Up @@ -272,15 +271,15 @@ def editElement(self):
self.component().setDescription(new_param.name())
self.model().addModelParameter(new_param, self.component())
self.text = new_param.description()
self.scene.dialog.repaintModel()
self.requestModelRepaint.emit()
elif isinstance(self.component(), QgsProcessingModelChildAlgorithm):
elemAlg = self.component().algorithm()
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()
self.requestModelRepaint.emit()

elif isinstance(self.component(), QgsProcessingModelOutput):
child_alg = self.model().childAlgorithm(self.component().childId())
Expand Down Expand Up @@ -318,21 +317,21 @@ def removeElement(self):
'Remove them before trying to remove it.')
else:
self.model().removeModelParameter(self.component().parameterName())
self.scene.dialog.haschanged = True
self.scene.dialog.repaintModel()
self.changed.emit()
self.requestModelRepaint.emit()
elif isinstance(self.component(), QgsProcessingModelChildAlgorithm):
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()
self.changed.emit()
self.requestModelRepaint.emit()
elif isinstance(self.component(), QgsProcessingModelOutput):
self.model().childAlgorithm(self.component().childId()).removeModelOutput(self.component().name())
self.model().updateDestinationParameters()
self.scene.dialog.haschanged = True
self.scene.dialog.repaintModel()
self.changed.emit()
self.requestModelRepaint.emit()

def getAdjustedText(self, text):
fm = QFontMetricsF(self.item_font)
Expand Down
27 changes: 18 additions & 9 deletions python/plugins/processing/modeler/ModelerScene.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,25 @@ def getItemsFromParamValue(self, value, child_id, context):
items.extend(self.getItemsFromParamValue(variables[v].source, child_id, context))
return items

def requestModelRepaint(self):
self.dialog.repaintModel()

def componentChanged(self):
self.dialog.haschanged = True

def paintModel(self, model):
self.model = model
context = createContext()
# Inputs
for inp in list(model.parameterComponents().values()):
item = ModelerGraphicItem(inp.clone(), model, scene=self)
item.setFlag(QGraphicsItem.ItemIsMovable, True)
item.setFlag(QGraphicsItem.ItemIsSelectable, True)
item = ModelerGraphicItem(inp.clone(), model)
self.addItem(item)
item.setPos(inp.position().x(), inp.position().y())
self.paramItems[inp.parameterName()] = item

item.requestModelRepaint.connect(self.requestModelRepaint)
item.changed.connect(self.componentChanged)

# Input dependency arrows
for input_name in list(model.parameterComponents().keys()):
idx = 0
Expand Down Expand Up @@ -128,13 +135,14 @@ def paintModel(self, model):

# We add the algs
for alg in list(model.childAlgorithms().values()):
item = ModelerGraphicItem(alg.clone(), model, scene=self)
item.setFlag(QGraphicsItem.ItemIsMovable, True)
item.setFlag(QGraphicsItem.ItemIsSelectable, True)
item = ModelerGraphicItem(alg.clone(), model)
self.addItem(item)
item.setPos(alg.position().x(), alg.position().y())
self.algItems[alg.childId()] = item

item.requestModelRepaint.connect(self.requestModelRepaint)
item.changed.connect(self.componentChanged)

# And then the arrows

for alg in list(model.childAlgorithms().values()):
Expand Down Expand Up @@ -169,9 +177,10 @@ def paintModel(self, model):

for key, out in outputs.items():
if out is not None:
item = ModelerGraphicItem(out.clone(), model, scene=self)
item.setFlag(QGraphicsItem.ItemIsMovable, True)
item.setFlag(QGraphicsItem.ItemIsSelectable, True)
item = ModelerGraphicItem(out.clone(), model)
item.requestModelRepaint.connect(self.requestModelRepaint)
item.changed.connect(self.componentChanged)

self.addItem(item)
pos = out.position()

Expand Down
17 changes: 17 additions & 0 deletions src/gui/processing/models/qgsmodelcomponentgraphicitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
*/
QgsProcessingModelAlgorithm *model();

signals:

// TEMPORARY ONLY during refactoring

/**
* Emitted by the item to request a repaint of the parent model scene.
*/
void requestModelRepaint();

// TEMPORARY ONLY during refactoring

/**
* Emitted when the definition of the associated component is changed
* by the item.
*/
void changed();

private:

std::unique_ptr< QgsProcessingModelComponent > mComponent;
Expand Down

0 comments on commit fe4352c

Please sign in to comment.