Skip to content
Permalink
Browse files

Use signals to avoid storing arrows in graphic item

  • Loading branch information
nyalldawson committed Mar 2, 2020
1 parent 0df4e35 commit 6c98448a7f01f1638fed62a60a3b30aa66421d4a
@@ -23,7 +23,7 @@

import os

from qgis.PyQt.QtCore import Qt, QPointF, QRectF
from qgis.PyQt.QtCore import Qt, QPointF, QRectF, pyqtSignal
from qgis.PyQt.QtGui import QFont, QFontMetricsF, QPen, QBrush, QColor, QPicture, QPainter, QPalette
from qgis.PyQt.QtWidgets import QApplication, QGraphicsItem, QMessageBox, QMenu
from qgis.PyQt.QtSvg import QSvgRenderer
@@ -54,6 +54,9 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):
BUTTON_WIDTH = 16
BUTTON_HEIGHT = 16

repaintArrows = pyqtSignal()
updateArrowPaths = pyqtSignal()

def __init__(self, element, model):
super().__init__(element, model, None)
self.box_width = ModelerGraphicItem.BOX_WIDTH
@@ -63,7 +66,6 @@ def __init__(self, element, model):
self.pixmap = None
self.picture = None
self.hover_over_item = False
self.arrows = []

svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'edit.svg'))
picture = QPicture()
@@ -88,9 +90,6 @@ def __init__(self, element, model):
self.deleteButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt)
self.deleteButton.clicked.connect(self.removeComponent)

def addArrow(self, arrow):
self.arrows.append(arrow)

def boundingRect(self):
fm = QFontMetricsF(self.item_font)
unfolded = isinstance(self.component(),
@@ -123,7 +122,7 @@ def hoverLeaveEvent(self, event):
if self.hover_over_item:
self.hover_over_item = False
self.update()
self.repaintArrows()
self.repaintArrows.emit()

def updateToolTip(self, event):
prev_status = self.hover_over_item
@@ -135,7 +134,7 @@ def updateToolTip(self, event):
self.hover_over_item = False
if self.hover_over_item != prev_status:
self.update()
self.repaintArrows()
self.repaintArrows.emit()

def getAdjustedText(self, text):
fm = QFontMetricsF(self.item_font)
@@ -256,14 +255,9 @@ def getLinkPointForOutput(self, outputIndex):
else:
return QPointF(0, 0)

def repaintArrows(self):
for arrow in self.arrows:
arrow.update()

def itemChange(self, change, value):
if change == QGraphicsItem.ItemPositionHasChanged:
for arrow in self.arrows:
arrow.updatePath()
self.updateArrowPaths.emit()
self.component().setPosition(self.pos())

# also need to update the model's stored component's position
@@ -275,7 +269,7 @@ def itemChange(self, change, value):
self.model().childAlgorithm(self.component().childId()).modelOutput(
self.component().name()).setPosition(self.pos())
elif change == QGraphicsItem.ItemSelectedChange:
self.repaintArrows()
self.repaintArrows.emit()

return super().itemChange(change, value)

@@ -398,17 +392,16 @@ def foldInput(self, folded):
pt = self.getLinkPointForOutput(-1)
pt = QPointF(0, pt.y())
self.outButton.position = pt
for arrow in self.arrows:
arrow.updatePath()

self.updateArrowPaths.emit()
self.update()

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.prepareGeometryChange()
for arrow in self.arrows:
arrow.updatePath()
self.updateArrowPaths.emit()
self.update()

def editComponent(self):
@@ -129,8 +129,12 @@ def paintModel(self, model):
input_item = self.paramItems[input_name]
parent_item = self.paramItems[parent_name]
arrow = ModelerArrowItem(parent_item, -1, input_item, -1)
input_item.addArrow(arrow)
parent_item.addArrow(arrow)

input_item.repaintArrows.connect(arrow.update)
input_item.updateArrowPaths.connect(arrow.updatePath)
parent_item.repaintArrows.connect(arrow.update)
parent_item.updateArrowPaths.connect(arrow.updatePath)

arrow.setPenStyle(Qt.DotLine)
arrow.updatePath()
self.addItem(arrow)
@@ -159,16 +163,25 @@ def paintModel(self, model):
sourceItems = self.getItemsFromParamValue(source, alg.childId(), context)
for sourceItem, sourceIdx in sourceItems:
arrow = ModelerArrowItem(sourceItem, sourceIdx, self.algItems[alg.childId()], idx)
sourceItem.addArrow(arrow)
self.algItems[alg.childId()].addArrow(arrow)

sourceItem.repaintArrows.connect(arrow.update)
sourceItem.updateArrowPaths.connect(arrow.updatePath)

self.algItems[alg.childId()].repaintArrows.connect(arrow.update)
self.algItems[alg.childId()].updateArrowPaths.connect(arrow.updatePath)

arrow.updatePath()
self.addItem(arrow)
idx += 1
for depend in alg.dependencies():
arrow = ModelerArrowItem(self.algItems[depend], -1,
self.algItems[alg.childId()], -1)
self.algItems[depend].addArrow(arrow)
self.algItems[alg.childId()].addArrow(arrow)
self.algItems[depend].repaintArrows.connect(arrow.update)
self.algItems[depend].updateArrowPaths.connect(arrow.updatePath)

self.algItems[alg.childId()].repaintArrows.connect(arrow.update)
self.algItems[alg.childId()].updateArrowPaths.connect(arrow.updatePath)

arrow.updatePath()
self.addItem(arrow)

@@ -201,8 +214,13 @@ def paintModel(self, model):
outputItems[key] = item
arrow = ModelerArrowItem(self.algItems[alg.childId()], idx, item,
-1)
self.algItems[alg.childId()].addArrow(arrow)
item.addArrow(arrow)

self.algItems[alg.childId()].repaintArrows.connect(arrow.update)
self.algItems[alg.childId()].updateArrowPaths.connect(arrow.updatePath)

item.repaintArrows.connect(arrow.update)
item.updateArrowPaths.connect(arrow.updatePath)

arrow.updatePath()
self.addItem(arrow)
else:

0 comments on commit 6c98448

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