src/eric7/EricGraphics/EricArrowItem.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
20 20
21 class EricArrowType(enum.Enum): 21 class EricArrowType(enum.Enum):
22 """ 22 """
23 Class defining the arrow types. 23 Class defining the arrow types.
24 """ 24 """
25
25 NORMAL = 1 26 NORMAL = 1
26 WIDE = 2 27 WIDE = 2
27 28
28 29
29 class EricArrowItem(QAbstractGraphicsShapeItem): 30 class EricArrowItem(QAbstractGraphicsShapeItem):
30 """ 31 """
31 Class implementing an arrow graphics item subclass. 32 Class implementing an arrow graphics item subclass.
32 """ 33 """
33 def __init__(self, origin=None, end=None, 34
34 filled=False, arrowType=EricArrowType.NORMAL, colors=None, 35 def __init__(
35 parent=None): 36 self,
37 origin=None,
38 end=None,
39 filled=False,
40 arrowType=EricArrowType.NORMAL,
41 colors=None,
42 parent=None,
43 ):
36 """ 44 """
37 Constructor 45 Constructor
38 46
39 @param origin origin of the arrow 47 @param origin origin of the arrow
40 @type QPointF 48 @type QPointF
41 @param end end point of the arrow 49 @param end end point of the arrow
42 @type QPointF 50 @type QPointF
43 @param filled flag indicating a filled arrow head 51 @param filled flag indicating a filled arrow head
48 @type tuple of (QColor, QColor) 56 @type tuple of (QColor, QColor)
49 @param parent reference to the parent object 57 @param parent reference to the parent object
50 @type QGraphicsItem 58 @type QGraphicsItem
51 """ 59 """
52 super().__init__(parent) 60 super().__init__(parent)
53 61
54 self._origin = QPointF() if origin is None else QPointF(origin) 62 self._origin = QPointF() if origin is None else QPointF(origin)
55 self._end = QPointF() if end is None else QPointF(end) 63 self._end = QPointF() if end is None else QPointF(end)
56 self._filled = filled 64 self._filled = filled
57 self.__type = arrowType 65 self.__type = arrowType
58 66
59 if colors is None: 67 if colors is None:
60 self._colors = (QColor(Qt.GlobalColor.black), 68 self._colors = (QColor(Qt.GlobalColor.black), QColor(Qt.GlobalColor.white))
61 QColor(Qt.GlobalColor.white))
62 else: 69 else:
63 self._colors = colors 70 self._colors = colors
64 71
65 self._halfLength = 13.0 72 self._halfLength = 13.0
66 73
67 self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsMovable, True) 74 self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsMovable, True)
68 self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsSelectable, True) 75 self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsSelectable, True)
69 76
70 def setPoints(self, xa, ya, xb, yb): 77 def setPoints(self, xa, ya, xb, yb):
71 """ 78 """
72 Public method to set the start and end points of the line. 79 Public method to set the start and end points of the line.
73 80
74 <b>Note:</b> This method does not redraw the item. 81 <b>Note:</b> This method does not redraw the item.
75 82
76 @param xa x-coordinate of the start point (float) 83 @param xa x-coordinate of the start point (float)
77 @param ya y-coordinate of the start point (float) 84 @param ya y-coordinate of the start point (float)
78 @param xb x-coordinate of the end point (float) 85 @param xb x-coordinate of the end point (float)
79 @param yb y-coordinate of the end point (float) 86 @param yb y-coordinate of the end point (float)
80 """ 87 """
81 self._origin = QPointF(xa, ya) 88 self._origin = QPointF(xa, ya)
82 self._end = QPointF(xb, yb) 89 self._end = QPointF(xb, yb)
83 90
84 def setStartPoint(self, x, y): 91 def setStartPoint(self, x, y):
85 """ 92 """
86 Public method to set the start point. 93 Public method to set the start point.
87 94
88 <b>Note:</b> This method does not redraw the item. 95 <b>Note:</b> This method does not redraw the item.
89 96
90 @param x x-coordinate of the start point (float) 97 @param x x-coordinate of the start point (float)
91 @param y y-coordinate of the start point (float) 98 @param y y-coordinate of the start point (float)
92 """ 99 """
93 self._origin = QPointF(x, y) 100 self._origin = QPointF(x, y)
94 101
95 def setEndPoint(self, x, y): 102 def setEndPoint(self, x, y):
96 """ 103 """
97 Public method to set the end point. 104 Public method to set the end point.
98 105
99 <b>Note:</b> This method does not redraw the item. 106 <b>Note:</b> This method does not redraw the item.
100 107
101 @param x x-coordinate of the end point (float) 108 @param x x-coordinate of the end point (float)
102 @param y y-coordinate of the end point (float) 109 @param y y-coordinate of the end point (float)
103 """ 110 """
104 self._end = QPointF(x, y) 111 self._end = QPointF(x, y)
105 112
106 def boundingRect(self): 113 def boundingRect(self):
107 """ 114 """
108 Public method to return the bounding rectangle. 115 Public method to return the bounding rectangle.
109 116
110 @return bounding rectangle (QRectF) 117 @return bounding rectangle (QRectF)
111 """ 118 """
112 extra = self._halfLength / 2.0 119 extra = self._halfLength / 2.0
113 return QRectF(self._origin, QSizeF(self._end.x() - self._origin.x(), 120 return (
114 self._end.y() - self._origin.y()) 121 QRectF(
115 ).normalized().adjusted(-extra, -extra, extra, extra) 122 self._origin,
116 123 QSizeF(
124 self._end.x() - self._origin.x(), self._end.y() - self._origin.y()
125 ),
126 )
127 .normalized()
128 .adjusted(-extra, -extra, extra, extra)
129 )
130
117 def paint(self, painter, option, widget=None): 131 def paint(self, painter, option, widget=None):
118 """ 132 """
119 Public method to paint the item in local coordinates. 133 Public method to paint the item in local coordinates.
120 134
121 @param painter reference to the painter object (QPainter) 135 @param painter reference to the painter object (QPainter)
122 @param option style options (QStyleOptionGraphicsItem) 136 @param option style options (QStyleOptionGraphicsItem)
123 @param widget optional reference to the widget painted on (QWidget) 137 @param widget optional reference to the widget painted on (QWidget)
124 """ 138 """
125 width = 2 if ( 139 width = (
126 (option.state & QStyle.StateFlag.State_Selected) == 140 2
127 QStyle.StateFlag.State_Selected 141 if (
128 ) else 1 142 (option.state & QStyle.StateFlag.State_Selected)
129 143 == QStyle.StateFlag.State_Selected
144 )
145 else 1
146 )
147
130 # draw the line first 148 # draw the line first
131 line = QLineF(self._origin, self._end) 149 line = QLineF(self._origin, self._end)
132 painter.setPen( 150 painter.setPen(
133 QPen(self._colors[0], width, Qt.PenStyle.SolidLine, 151 QPen(
134 Qt.PenCapStyle.FlatCap, Qt.PenJoinStyle.MiterJoin)) 152 self._colors[0],
153 width,
154 Qt.PenStyle.SolidLine,
155 Qt.PenCapStyle.FlatCap,
156 Qt.PenJoinStyle.MiterJoin,
157 )
158 )
135 painter.drawLine(line) 159 painter.drawLine(line)
136 160
137 # draw the arrow head 161 # draw the arrow head
138 arrowAngle = ( 162 arrowAngle = (
139 ArrowheadAngleFactor 163 ArrowheadAngleFactor
140 if self.__type == EricArrowType.NORMAL else 164 if self.__type == EricArrowType.NORMAL
141 2 * ArrowheadAngleFactor 165 else 2 * ArrowheadAngleFactor
142 ) 166 )
143 slope = math.atan2(line.dy(), line.dx()) 167 slope = math.atan2(line.dy(), line.dx())
144 168
145 # Calculate left arrow point 169 # Calculate left arrow point
146 arrowSlope = slope + arrowAngle 170 arrowSlope = slope + arrowAngle
147 a1 = QPointF(self._end.x() - self._halfLength * math.cos(arrowSlope), 171 a1 = QPointF(
148 self._end.y() - self._halfLength * math.sin(arrowSlope)) 172 self._end.x() - self._halfLength * math.cos(arrowSlope),
149 173 self._end.y() - self._halfLength * math.sin(arrowSlope),
174 )
175
150 # Calculate right arrow point 176 # Calculate right arrow point
151 arrowSlope = slope - arrowAngle 177 arrowSlope = slope - arrowAngle
152 a2 = QPointF(self._end.x() - self._halfLength * math.cos(arrowSlope), 178 a2 = QPointF(
153 self._end.y() - self._halfLength * math.sin(arrowSlope)) 179 self._end.x() - self._halfLength * math.cos(arrowSlope),
154 180 self._end.y() - self._halfLength * math.sin(arrowSlope),
181 )
182
155 if self._filled: 183 if self._filled:
156 painter.setBrush(self._colors[0]) 184 painter.setBrush(self._colors[0])
157 else: 185 else:
158 painter.setBrush(self._colors[1]) 186 painter.setBrush(self._colors[1])
159 polygon = QPolygonF() 187 polygon = QPolygonF()

eric ide

mercurial