Skip to content
Permalink
Browse files

Use signals to avoid hard dependancy between component graphic items …

…and scene
  • Loading branch information
nyalldawson committed Mar 2, 2020
1 parent 1d25155 commit fe4352c236fbb29ac3e8b005797e0ce282ca77f8
@@ -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

};
@@ -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()
@@ -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'
@@ -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())
@@ -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)
@@ -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
@@ -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()):
@@ -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()

@@ -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;

0 comments on commit fe4352c

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