eric6/E5Graphics/E5ArrowItem.py

changeset 8268
6b8128e0c9d1
parent 8257
28146736bbfc
equal deleted inserted replaced
8267:6baca884c73a 8268:6b8128e0c9d1
5 5
6 """ 6 """
7 Module implementing a graphics item subclass for an arrow. 7 Module implementing a graphics item subclass for an arrow.
8 """ 8 """
9 9
10 import enum
10 import math 11 import math
11 12
12 from PyQt5.QtCore import QPointF, QRectF, QSizeF, QLineF, Qt 13 from PyQt5.QtCore import QPointF, QRectF, QSizeF, QLineF, Qt
13 from PyQt5.QtGui import QPen, QPolygonF, QColor 14 from PyQt5.QtGui import QPen, QPolygonF, QColor
14 from PyQt5.QtWidgets import QAbstractGraphicsShapeItem, QGraphicsItem, QStyle 15 from PyQt5.QtWidgets import QAbstractGraphicsShapeItem, QGraphicsItem, QStyle
15 16
16 NormalArrow = 1
17 WideArrow = 2
18
19 ArrowheadAngleFactor = 0.26179938779914941 17 ArrowheadAngleFactor = 0.26179938779914941
20 # That is: 0.5 * math.atan(math.sqrt(3.0) / 3.0) 18 # That is: 0.5 * math.atan(math.sqrt(3.0) / 3.0)
19
20
21 class E5ArrowType(enum.Enum):
22 """
23 Class defining the arrow types.
24 """
25 NORMAL = 1
26 WIDE = 2
21 27
22 28
23 class E5ArrowItem(QAbstractGraphicsShapeItem): 29 class E5ArrowItem(QAbstractGraphicsShapeItem):
24 """ 30 """
25 Class implementing an arrow graphics item subclass. 31 Class implementing an arrow graphics item subclass.
26 """ 32 """
27 def __init__(self, origin=None, end=None, 33 def __init__(self, origin=None, end=None,
28 filled=False, arrowType=NormalArrow, colors=None, 34 filled=False, arrowType=E5ArrowType.NORMAL, colors=None,
29 parent=None): 35 parent=None):
30 """ 36 """
31 Constructor 37 Constructor
32 38
33 @param origin origin of the arrow 39 @param origin origin of the arrow
35 @param end end point of the arrow 41 @param end end point of the arrow
36 @type QPointF 42 @type QPointF
37 @param filled flag indicating a filled arrow head 43 @param filled flag indicating a filled arrow head
38 @type bool 44 @type bool
39 @param arrowType arrow type 45 @param arrowType arrow type
40 @type int, one of NormalArrow, WideArrow 46 @type E5ArrowType
41 @param colors tuple containing the foreground and background colors 47 @param colors tuple containing the foreground and background colors
42 @type tuple of (QColor, QColor) 48 @type tuple of (QColor, QColor)
43 @param parent reference to the parent object 49 @param parent reference to the parent object
44 @type QGraphicsItem 50 @type QGraphicsItem
45 """ 51 """
46 super().__init__(parent) 52 super().__init__(parent)
47 53
48 self._origin = QPointF() if origin is None else QPointF(origin) 54 self._origin = QPointF() if origin is None else QPointF(origin)
49 self._end = QPointF() if end is None else QPointF(end) 55 self._end = QPointF() if end is None else QPointF(end)
50 self._filled = filled 56 self._filled = filled
51 self._type = arrowType 57 self.__type = arrowType
52 58
53 if colors is None: 59 if colors is None:
54 self._colors = (QColor(Qt.GlobalColor.black), 60 self._colors = (QColor(Qt.GlobalColor.black),
55 QColor(Qt.GlobalColor.white)) 61 QColor(Qt.GlobalColor.white))
56 else: 62 else:
127 QPen(self._colors[0], width, Qt.PenStyle.SolidLine, 133 QPen(self._colors[0], width, Qt.PenStyle.SolidLine,
128 Qt.PenCapStyle.FlatCap, Qt.PenJoinStyle.MiterJoin)) 134 Qt.PenCapStyle.FlatCap, Qt.PenJoinStyle.MiterJoin))
129 painter.drawLine(line) 135 painter.drawLine(line)
130 136
131 # draw the arrow head 137 # draw the arrow head
132 arrowAngle = self._type * ArrowheadAngleFactor 138 arrowAngle = (
139 ArrowheadAngleFactor
140 if self.__type == E5ArrowType.NORMAL else
141 2 * ArrowheadAngleFactor
142 )
133 slope = math.atan2(line.dy(), line.dx()) 143 slope = math.atan2(line.dy(), line.dx())
134 144
135 # Calculate left arrow point 145 # Calculate left arrow point
136 arrowSlope = slope + arrowAngle 146 arrowSlope = slope + arrowAngle
137 a1 = QPointF(self._end.x() - self._halfLength * math.cos(arrowSlope), 147 a1 = QPointF(self._end.x() - self._halfLength * math.cos(arrowSlope),

eric ide

mercurial