Skip to content

Commit

Permalink
Merge pull request slaclab#1072 from nstelter-slac/arrow_size_option
Browse files Browse the repository at this point in the history
ENH: Add option to change size of arrows on drawn lines
  • Loading branch information
YektaY authored Apr 30, 2024
2 parents ec62b26 + e09b52b commit 3059b12
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 18 deletions.
41 changes: 31 additions & 10 deletions pydm/tests/widgets/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,21 +1230,42 @@ def test_pydmdrawingpolyline_arrows(qapp, qtbot, points, num_points):
num_points :int
The actual number of vertices of the polygon.
"""
drawing = PyDMDrawingPolyline()
qtbot.addWidget(drawing)
# try with polyline
polyLine = PyDMDrawingPolyline()
qtbot.addWidget(polyLine)

# make sure points seutp correctly before drawing arrows
drawing.setPoints(points)
assert len(drawing.getPoints()) == num_points
polyLine.setPoints(points)
assert len(polyLine.getPoints()) == num_points

# enable all arrow options
drawing._arrow_end_point_selection = True
drawing._arrow_start_point_selection = True
drawing._arrow_mid_point_selection = True
drawing._arrow_mid_point_flipped = True
drawing.draw_item(drawing._painter)
polyLine._arrow_end_point_selection = True
polyLine._arrow_start_point_selection = True
polyLine._arrow_mid_point_selection = True
polyLine._arrow_mid_point_flipped = True
polyLine.draw_item(polyLine._painter)
polyLine.show()

# just be sure size prop exists and can be changed without breaking things
polyLine._arrow_size += 3
polyLine.draw_item(polyLine._painter)
polyLine.show()

# now try with line
line = PyDMDrawingLine()
qtbot.addWidget(line)

drawing.show()
# enable all arrow options
line._arrow_end_point_selection = True
line._arrow_start_point_selection = True
line._arrow_mid_point_selection = True
line._arrow_mid_point_flipped = True
line.draw_item(polyLine._painter)
line.show()

line._arrow_size += 3
line.draw_item(polyLine._painter)
line.show()


# # ---------------------------
Expand Down
68 changes: 60 additions & 8 deletions pydm/widgets/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ def __init__(self, parent=None, init_channel=None):
self._arrow_start_point_selection = False
self._arrow_mid_point_selection = False
self._mid_point_arrow_flipped = False
self._arrow_size = 6 # 6 is arbitrary size that looked good for default, not in any specific 'units'
self.rotation = 0
self.penStyle = Qt.SolidLine
self.penWidth = 1
Expand Down Expand Up @@ -554,20 +555,44 @@ def draw_item(self, painter):

# Draw the arrows
if self._arrow_end_point_selection:
points = self._arrow_points(start_point, end_point, 6, 6)
points = self._arrow_points(start_point, end_point, self._arrow_size, self._arrow_size)
painter.drawPolygon(points)

if self._arrow_start_point_selection:
points = self._arrow_points(end_point, start_point, 6, 6)
points = self._arrow_points(end_point, start_point, self._arrow_size, self._arrow_size)
painter.drawPolygon(points)

if self._arrow_mid_point_selection:
if self._mid_point_arrow_flipped:
points = self._arrow_points(start_point, mid_point, 6, 6)
points = self._arrow_points(start_point, mid_point, self._arrow_size, self._arrow_size)
else:
points = self._arrow_points(end_point, mid_point, 6, 6)
points = self._arrow_points(end_point, mid_point, self._arrow_size, self._arrow_size)
painter.drawPolygon(points)

@Property(int)
def arrowSize(self):
"""
Size to render line arrows.
Returns
-------
bool
"""
return self._arrow_size

@arrowSize.setter
def arrowSize(self, new_size):
"""
Size to render line arrows.
Parameters
-------
new_selection : bool
"""
if self._arrow_size != new_size:
self._arrow_size = new_size
self.update()

@Property(bool)
def arrowEndPoint(self):
"""
Expand Down Expand Up @@ -1205,6 +1230,7 @@ def __init__(self, parent=None, init_channel=None):
self._arrow_start_point_selection = False
self._arrow_mid_point_selection = False
self._arrow_mid_point_flipped = False
self._arrow_size = 6 # 6 is arbitrary size that looked good for default, not in any specific 'units'
self.penStyle = Qt.SolidLine
self.penWidth = 1
self._points = []
Expand Down Expand Up @@ -1242,20 +1268,22 @@ def p2d(pt):
midpoint_x = (point1.x() + point2.x()) / 2
midpoint_y = (point1.y() + point2.y()) / 2
midpoint = QPointF(midpoint_x, midpoint_y)
points = PyDMDrawingLine._arrow_points(point1, midpoint, 6, 6) # 6 = arbitrary arrow size
points = PyDMDrawingLine._arrow_points(point1, midpoint, self._arrow_size, self._arrow_size)
painter.drawPolygon(points)

# Draw the arrows
if self._arrow_end_point_selection and (len(self._points[1]) >= 2):
points = PyDMDrawingLine._arrow_points(p2d(self._points[1]), p2d(self._points[0]), 6, 6)
points = PyDMDrawingLine._arrow_points(
p2d(self._points[1]), p2d(self._points[0]), self._arrow_size, self._arrow_size
)
painter.drawPolygon(points)

if self._arrow_start_point_selection and (len(self._points[1]) >= 2):
points = PyDMDrawingLine._arrow_points(
p2d(self._points[len(self._points) - 2]),
p2d(self._points[len(self._points) - 1]),
6,
6,
self._arrow_size,
self._arrow_size,
)
painter.drawPolygon(points)

Expand Down Expand Up @@ -1340,6 +1368,30 @@ def resetPoints(self):
self._points = []
self.update()

@Property(int)
def arrowSize(self):
"""
Size to render line arrows.
Returns
-------
bool
"""
return self._arrow_size

@arrowSize.setter
def arrowSize(self, new_size):
"""
Size to render line arrows.
Parameters
-------
new_selection : bool
"""
if self._arrow_size != new_size:
self._arrow_size = new_size
self.update()

@Property(bool)
def arrowEndPoint(self):
"""
Expand Down

0 comments on commit 3059b12

Please sign in to comment.