Use signals to avoid hard dependancy between component graphic items and scene

This commit is contained in:
Nyall Dawson 2020-03-02 11:13:02 +10:00
parent 1d25155d15
commit fe4352c236
4 changed files with 61 additions and 21 deletions

View File

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

View File

@ -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 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):
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,7 +271,7 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):
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())
@ -280,7 +279,7 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):
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 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):
'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)

View File

@ -88,18 +88,25 @@ class ModelerScene(QgsModelGraphicsScene):
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 @@ class ModelerScene(QgsModelGraphicsScene):
# 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 @@ class ModelerScene(QgsModelGraphicsScene):
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()

View File

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