From 3035ea8d8b450af75525c096d6164b819f3899a1 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Mon, 2 Sep 2024 09:50:42 +0200 Subject: [PATCH] Scenario: Fix line display. --- src/View/Scenarios/GraphWidget.py | 66 ++++++++++++++++++------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/View/Scenarios/GraphWidget.py b/src/View/Scenarios/GraphWidget.py index 5c982365..f0c63882 100644 --- a/src/View/Scenarios/GraphWidget.py +++ b/src/View/Scenarios/GraphWidget.py @@ -80,6 +80,9 @@ class ScenarioItem(QGraphicsTextItem): super(ScenarioItem, self).paint(painter, option, widget) + def itemChange(self, change, value): + self.graph.edges_display_update() + return super(ScenarioItem, self).itemChange(change, value) class EdgeItem(QGraphicsItem): Type = QGraphicsItem.UserType + 11 @@ -97,9 +100,14 @@ class EdgeItem(QGraphicsItem): return EdgeItem.Type def boundingRect(self): - pen_width = 2.0 - extra = (pen_width + 5) / 2.0 + extra = 0.0 + for item in [self.item, self.parent_item]: + rect = item.boundingRect() + extra = max( + [rect.width(), rect.height(), extra] + ) + extra /= 2 return QRectF( self.parent_item.pos(), QSizeF( @@ -108,50 +116,42 @@ class EdgeItem(QGraphicsItem): ) ).normalized().adjusted(-extra, -extra, extra, extra) - def shape(self): - vec = self.item.pos() - self.parent_item.pos() - vec = vec * (5 / math.sqrt(QPointF.dotProduct(vec, vec))) - extra = QPointF(vec.y(), -vec.x()) - - result = QPainterPath(self.parent_item.pos() - vec + extra) - result.lineTo(self.parent_item.pos() - vec - extra) - result.lineTo(self.item.pos() + vec - extra) - result.lineTo(self.item.pos() + vec + extra) - result.closeSubpath() - - return result - @timer def paint(self, painter, option, widget): - line = QLineF(self.parent_item.pos(), self.item.pos()) + line = QLineF( + self.get_line_item_point(self.parent_item), + self.get_line_item_point(self.item) + ) + if line.length() == 0.0: return color = Qt.black + arrow = self.get_arrow_path(line, color) + + brush = QBrush() + path = QPainterPath() + brush.setColor(color) + brush.setStyle(Qt.SolidPattern) + painter.setPen( QPen( color, 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin ) ) - # Draw the line painter.drawLine(line) - - arrow = self.get_arrow_path(line, color) - - brush = QBrush() - brush.setColor(color) - brush.setStyle(Qt.SolidPattern) - - path = QPainterPath() path.addPolygon(arrow) painter.drawPolygon(arrow) painter.fillPath(path, brush) def get_arrow_path(self, line, color): + pp = self.get_line_item_point(self.parent_item) + p = self.get_line_item_point(self.item) + line_center = QPointF( - (self.parent_item.pos().x() + self.item.pos().x()) / 2, - (self.parent_item.pos().y() + self.item.pos().y()) / 2, + (pp.x() + p.x()) / 2, + (pp.y() + p.y()) / 2, ) angle = math.acos(line.dx() / line.length()) @@ -171,6 +171,13 @@ class EdgeItem(QGraphicsItem): return poly + def get_line_item_point(self, item): + rect = item.boundingRect() + return QPointF( + item.pos().x() + (rect.width() / 2.0), + item.pos().y() + (rect.height() / 2.0) + ) + class GraphWidget(QGraphicsView): changeEdge = pyqtSignal(object) @@ -193,7 +200,6 @@ class GraphWidget(QGraphicsView): self.scenarios_items = {} self.edge_items = [] - self.texts = {} self.m_origin_x = 0.0 self.m_origin_y = 0.0 @@ -249,6 +255,10 @@ class GraphWidget(QGraphicsView): self.scene().addItem(iedge) self.edge_items.append(iedge) + def edges_display_update(self): + for e in self.edge_items: + e.update() + def display_update(self): """Clear the scene and redraw it