mirror of https://gitlab.com/pamhyr/pamhyr2
Scenario: Fix line display.
parent
abe7046d4f
commit
3035ea8d8b
|
|
@ -80,6 +80,9 @@ class ScenarioItem(QGraphicsTextItem):
|
||||||
|
|
||||||
super(ScenarioItem, self).paint(painter, option, widget)
|
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):
|
class EdgeItem(QGraphicsItem):
|
||||||
Type = QGraphicsItem.UserType + 11
|
Type = QGraphicsItem.UserType + 11
|
||||||
|
|
@ -97,9 +100,14 @@ class EdgeItem(QGraphicsItem):
|
||||||
return EdgeItem.Type
|
return EdgeItem.Type
|
||||||
|
|
||||||
def boundingRect(self):
|
def boundingRect(self):
|
||||||
pen_width = 2.0
|
extra = 0.0
|
||||||
extra = (pen_width + 5) / 2.0
|
for item in [self.item, self.parent_item]:
|
||||||
|
rect = item.boundingRect()
|
||||||
|
extra = max(
|
||||||
|
[rect.width(), rect.height(), extra]
|
||||||
|
)
|
||||||
|
|
||||||
|
extra /= 2
|
||||||
return QRectF(
|
return QRectF(
|
||||||
self.parent_item.pos(),
|
self.parent_item.pos(),
|
||||||
QSizeF(
|
QSizeF(
|
||||||
|
|
@ -108,50 +116,42 @@ class EdgeItem(QGraphicsItem):
|
||||||
)
|
)
|
||||||
).normalized().adjusted(-extra, -extra, extra, extra)
|
).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
|
@timer
|
||||||
def paint(self, painter, option, widget):
|
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:
|
if line.length() == 0.0:
|
||||||
return
|
return
|
||||||
|
|
||||||
color = Qt.black
|
color = Qt.black
|
||||||
|
arrow = self.get_arrow_path(line, color)
|
||||||
|
|
||||||
|
brush = QBrush()
|
||||||
|
path = QPainterPath()
|
||||||
|
brush.setColor(color)
|
||||||
|
brush.setStyle(Qt.SolidPattern)
|
||||||
|
|
||||||
painter.setPen(
|
painter.setPen(
|
||||||
QPen(
|
QPen(
|
||||||
color, 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin
|
color, 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
# Draw the line
|
|
||||||
painter.drawLine(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)
|
path.addPolygon(arrow)
|
||||||
|
|
||||||
painter.drawPolygon(arrow)
|
painter.drawPolygon(arrow)
|
||||||
painter.fillPath(path, brush)
|
painter.fillPath(path, brush)
|
||||||
|
|
||||||
def get_arrow_path(self, line, color):
|
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(
|
line_center = QPointF(
|
||||||
(self.parent_item.pos().x() + self.item.pos().x()) / 2,
|
(pp.x() + p.x()) / 2,
|
||||||
(self.parent_item.pos().y() + self.item.pos().y()) / 2,
|
(pp.y() + p.y()) / 2,
|
||||||
)
|
)
|
||||||
|
|
||||||
angle = math.acos(line.dx() / line.length())
|
angle = math.acos(line.dx() / line.length())
|
||||||
|
|
@ -171,6 +171,13 @@ class EdgeItem(QGraphicsItem):
|
||||||
|
|
||||||
return poly
|
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):
|
class GraphWidget(QGraphicsView):
|
||||||
changeEdge = pyqtSignal(object)
|
changeEdge = pyqtSignal(object)
|
||||||
|
|
@ -193,7 +200,6 @@ class GraphWidget(QGraphicsView):
|
||||||
|
|
||||||
self.scenarios_items = {}
|
self.scenarios_items = {}
|
||||||
self.edge_items = []
|
self.edge_items = []
|
||||||
self.texts = {}
|
|
||||||
|
|
||||||
self.m_origin_x = 0.0
|
self.m_origin_x = 0.0
|
||||||
self.m_origin_y = 0.0
|
self.m_origin_y = 0.0
|
||||||
|
|
@ -249,6 +255,10 @@ class GraphWidget(QGraphicsView):
|
||||||
self.scene().addItem(iedge)
|
self.scene().addItem(iedge)
|
||||||
self.edge_items.append(iedge)
|
self.edge_items.append(iedge)
|
||||||
|
|
||||||
|
def edges_display_update(self):
|
||||||
|
for e in self.edge_items:
|
||||||
|
e.update()
|
||||||
|
|
||||||
def display_update(self):
|
def display_update(self):
|
||||||
"""Clear the scene and redraw it
|
"""Clear the scene and redraw it
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue