Skip to content

Commit

Permalink
TST: Add test for rendering PyDMDrawingPolyline with arrow options.
Browse files Browse the repository at this point in the history
Also:
-minor change to member variable name to be more consistent.
-minor change to comments
  • Loading branch information
nstelter-slac committed Aug 15, 2023
1 parent 5f4441f commit 45a80a2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
35 changes: 35 additions & 0 deletions pydm/tests/widgets/test_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,41 @@ def test_pydmdrawingpolyline_setpoints(qapp, qtbot, monkeypatch, width, height,

drawing.show()

@pytest.mark.parametrize("points, num_points", [
([[-1, 18], [-1, -1], [97, -1]], 3),
([[-1, 18], [-1, -1]], 2)
])
def test_pydmdrawingpolyline_arrows(qapp, qtbot, points, num_points):
"""
Test the rendering of a PyDMDrawingPolyline widget with arrow options enabled.
Expectations:
The drawing of the widget takes place without any problems.
Parameters
----------
qtbot : fixture
Window for widget testing
points : [(float, float)]
Requested vertices of the polygon.
num_points :int
The actual number of vertices of the polygon.
"""
drawing = PyDMDrawingPolyline()
qtbot.addWidget(drawing)

# make sure points seutp correctly before drawing arrows
drawing.setPoints(points)
assert len(drawing.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)

drawing.show()

# # ---------------------------
# # PyDMDrawingIrregularPolygon
Expand Down
5 changes: 3 additions & 2 deletions pydm/widgets/designer_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,18 @@ def __init__(self, values=None, *args, parent=None, **kwargs):
self.customContextMenuRequested.connect(self._context_menu)
self.values

#we let user swap points by dragging one on-top of the other
# we let user swap points by dragging one on-top of the other
self.setDragEnabled(True)
self.setAcceptDrops(True)
self.drag_source_row = None

def startDrag(self, event):
self.drag_source_row = self.currentRow()
#call super() since we use the default dragging functionality
# call super() since we use the default dragging functionality
super().startDrag(event)

def dropEvent(self, event):
# don't call super() here, functionality messes with our swapping!
source_row = self.drag_source_row
target_row = self.indexAt(event.pos()).row()
if target_row >= 0 and source_row != target_row:
Expand Down
34 changes: 17 additions & 17 deletions pydm/widgets/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ def __init__(self, parent=None, init_channel=None):
self._arrow_end_point_selection = False
self._arrow_start_point_selection = False
self._arrow_mid_point_selection = False
self._mid_point_arrow_flipped = False
self._arrow_mid_point_flipped = False
self.penStyle = Qt.SolidLine
self.penWidth = 1
self._points = []
Expand Down Expand Up @@ -1222,22 +1222,22 @@ def p2d(pt):
if self._arrow_mid_point_selection:
point1 = p2d(p1)
point2 = p2d(self._points[i+1])
if self._mid_point_arrow_flipped:
if self._arrow_mid_point_flipped:
point1, point2 = point2, point1 #swap values

#arrow points at midpoint of line
# arrow points at midpoint of line
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, 6, 6) # 6 = arbitrary arrow size
painter.drawPolygon(points)

# Draw the arrows
if self._arrow_end_point_selection:
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)
painter.drawPolygon(points)

if self._arrow_start_point_selection:
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)
painter.drawPolygon(points)

Expand Down Expand Up @@ -1319,7 +1319,7 @@ def resetPoints(self):
@Property(bool)
def arrowEndPoint(self):
"""
If True, an arrow will be drawn at the end of the line.
If True, an arrow will be drawn at the start of the last polyline segment.
Returns
-------
Expand All @@ -1330,7 +1330,7 @@ def arrowEndPoint(self):
@arrowEndPoint.setter
def arrowEndPoint(self, new_selection):
"""
If True, an arrow will be drawn at the end of the line.
If True, an arrow will be drawn at the start of the last polyline segment.
Parameters
-------
Expand All @@ -1343,7 +1343,7 @@ def arrowEndPoint(self, new_selection):
@Property(bool)
def arrowStartPoint(self):
"""
If True, an arrow will be drawn at the start of the line.
If True, an arrow will be drawn at the start of the first polyline segment.
Returns
-------
Expand All @@ -1354,7 +1354,7 @@ def arrowStartPoint(self):
@arrowStartPoint.setter
def arrowStartPoint(self, new_selection):
"""
If True, an arrow will be drawn at the start of the line.
If True, an arrow will be drawn at the start of the first polyline segment.
Parameters
-------
Expand All @@ -1367,7 +1367,7 @@ def arrowStartPoint(self, new_selection):
@Property(bool)
def arrowMidPoint(self):
"""
If True, an arrow will be drawn at the midpoint of the line.
If True, an arrows will be drawn at the midpoints of the segments of the polyline.
Returns
-------
bool
Expand All @@ -1377,7 +1377,7 @@ def arrowMidPoint(self):
@arrowMidPoint.setter
def arrowMidPoint(self, new_selection):
"""
If True, an arrow will be drawn at the midpoint of the line.
If True, an arrows will be drawn at the midpoints of the segments of the polyline.
Parameters
-------
new_selection : bool
Expand All @@ -1389,25 +1389,25 @@ def arrowMidPoint(self, new_selection):
@Property(bool)
def flipMidPointArrow(self):
"""
Flips the direction of the midpoint arrow.
Flips the direction of the midpoint arrows.
Returns
-------
bool
"""
return self._mid_point_arrow_flipped
return self._arrow_mid_point_flipped

@flipMidPointArrow.setter
def flipMidPointArrow(self, new_selection):
"""
Flips the direction of the midpoint arrow.
Flips the direction of the midpoint arrows.
Parameters
-------
new_selection : bool
"""
if self._mid_point_arrow_flipped != new_selection:
self._mid_point_arrow_flipped = new_selection
if self._arrow_mid_point_flipped != new_selection:
self._arrow_mid_point_flipped = new_selection
self.update()

points = Property("QStringList", getPoints, setPoints, resetPoints)
Expand Down

0 comments on commit 45a80a2

Please sign in to comment.