9 |
9 |
10 |
10 |
11 import math |
11 import math |
12 |
12 |
13 from PyQt5.QtCore import QPointF, QRectF, QSizeF, QLineF, Qt |
13 from PyQt5.QtCore import QPointF, QRectF, QSizeF, QLineF, Qt |
14 from PyQt5.QtGui import QPen, QPolygonF |
14 from PyQt5.QtGui import QPen, QPolygonF, QColor |
15 from PyQt5.QtWidgets import QAbstractGraphicsShapeItem, QGraphicsItem, QStyle |
15 from PyQt5.QtWidgets import QAbstractGraphicsShapeItem, QGraphicsItem, QStyle |
16 |
16 |
17 NormalArrow = 1 |
17 NormalArrow = 1 |
18 WideArrow = 2 |
18 WideArrow = 2 |
19 |
19 |
24 class E5ArrowItem(QAbstractGraphicsShapeItem): |
24 class E5ArrowItem(QAbstractGraphicsShapeItem): |
25 """ |
25 """ |
26 Class implementing an arrow graphics item subclass. |
26 Class implementing an arrow graphics item subclass. |
27 """ |
27 """ |
28 def __init__(self, origin=None, end=None, |
28 def __init__(self, origin=None, end=None, |
29 filled=False, arrowType=NormalArrow, parent=None): |
29 filled=False, arrowType=NormalArrow, colors=None, |
|
30 parent=None): |
30 """ |
31 """ |
31 Constructor |
32 Constructor |
32 |
33 |
33 @param origin origin of the arrow (QPointF) |
34 @param origin origin of the arrow |
34 @param end end point of the arrow (QPointF) |
35 @type QPointF |
35 @param filled flag indicating a filled arrow head (boolean) |
36 @param end end point of the arrow |
36 @param arrowType arrow type (NormalArrow, WideArrow) |
37 @type QPointF |
37 @keyparam parent reference to the parent object (QGraphicsItem) |
38 @param filled flag indicating a filled arrow head |
|
39 @type bool |
|
40 @param arrowType arrow type |
|
41 @type int, one of NormalArrow, WideArrow |
|
42 @param colors tuple containing the foreground and background colors |
|
43 @type tuple of (QColor, QColor) |
|
44 @param parent reference to the parent object |
|
45 @type QGraphicsItem |
38 """ |
46 """ |
39 super(E5ArrowItem, self).__init__(parent) |
47 super(E5ArrowItem, self).__init__(parent) |
40 |
48 |
41 self._origin = QPointF() if origin is None else QPointF(origin) |
49 self._origin = QPointF() if origin is None else QPointF(origin) |
42 self._end = QPointF() if end is None else QPointF(end) |
50 self._end = QPointF() if end is None else QPointF(end) |
43 self._filled = filled |
51 self._filled = filled |
44 self._type = arrowType |
52 self._type = arrowType |
|
53 |
|
54 if colors is None: |
|
55 self._colors = (QColor(Qt.black), QColor(Qt.white)) |
|
56 else: |
|
57 self._colors = colors |
45 |
58 |
46 self._halfLength = 13.0 |
59 self._halfLength = 13.0 |
47 |
60 |
48 self.setFlag(QGraphicsItem.ItemIsMovable, True) |
61 self.setFlag(QGraphicsItem.ItemIsMovable, True) |
49 self.setFlag(QGraphicsItem.ItemIsSelectable, True) |
62 self.setFlag(QGraphicsItem.ItemIsSelectable, True) |
112 width = 1 |
125 width = 1 |
113 |
126 |
114 # draw the line first |
127 # draw the line first |
115 line = QLineF(self._origin, self._end) |
128 line = QLineF(self._origin, self._end) |
116 painter.setPen( |
129 painter.setPen( |
117 QPen(Qt.black, width, Qt.SolidLine, Qt.FlatCap, Qt.MiterJoin)) |
130 QPen(self._colors[0], width, Qt.SolidLine, Qt.FlatCap, |
|
131 Qt.MiterJoin)) |
118 painter.drawLine(line) |
132 painter.drawLine(line) |
119 |
133 |
120 # draw the arrow head |
134 # draw the arrow head |
121 arrowAngle = self._type * ArrowheadAngleFactor |
135 arrowAngle = self._type * ArrowheadAngleFactor |
122 slope = math.atan2(line.dy(), line.dx()) |
136 slope = math.atan2(line.dy(), line.dx()) |
130 arrowSlope = slope - arrowAngle |
144 arrowSlope = slope - arrowAngle |
131 a2 = QPointF(self._end.x() - self._halfLength * math.cos(arrowSlope), |
145 a2 = QPointF(self._end.x() - self._halfLength * math.cos(arrowSlope), |
132 self._end.y() - self._halfLength * math.sin(arrowSlope)) |
146 self._end.y() - self._halfLength * math.sin(arrowSlope)) |
133 |
147 |
134 if self._filled: |
148 if self._filled: |
135 painter.setBrush(Qt.black) |
149 painter.setBrush(self._colors[0]) |
136 else: |
150 else: |
137 painter.setBrush(Qt.white) |
151 painter.setBrush(self._colors[1]) |
138 polygon = QPolygonF() |
152 polygon = QPolygonF() |
139 polygon.append(line.p2()) |
153 polygon.append(line.p2()) |
140 polygon.append(a1) |
154 polygon.append(a1) |
141 polygon.append(a2) |
155 polygon.append(a2) |
142 painter.drawPolygon(polygon) |
156 painter.drawPolygon(polygon) |