Skip to content

Commit

Permalink
STY: appease pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nstelter-slac committed May 1, 2024
1 parent 31a665e commit c77d28a
Showing 1 changed file with 1 addition and 279 deletions.
280 changes: 1 addition & 279 deletions pydm/widgets/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def __init__(self, parent=None, init_channel=None):
super(PyDMDrawingLineBase, self).__init__(parent, init_channel)
self.penStyle = Qt.SolidLine
self.penWidth = 1
self._arrow_size = 6 # 6 is arbitrary size that looked good for default, not in any specific 'units'
self._arrow_size = 6 # 6 is arbitrary size that looked good for default, not in any specific 'units'
self._arrow_end_point_selection = False
self._arrow_start_point_selection = False
self._arrow_mid_point_selection = False
Expand Down Expand Up @@ -1380,284 +1380,6 @@ def draw_item(self, painter):
painter.drawPolygon(QPolygonF(poly))


class PyDMDrawingPolyline(PyDMDrawing, new_properties=_penRuleProperties):
"""
A widget with a multi-segment, piecewise-linear line drawn in it.
This class inherits from PyDMDrawing.
Parameters
----------
parent : QWidget
The parent widget for the Label
init_channel : str, optional
The channel to be used by the widget.
"""

def __init__(self, parent=None, init_channel=None):
super(PyDMDrawingPolyline, self).__init__(parent, init_channel)
self._arrow_end_point_selection = False
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 = []

def draw_item(self, painter):
"""
Draws the segmented line after setting up the canvas with a call to
``PyDMDrawing.draw_item``.
"""
super(PyDMDrawingPolyline, self).draw_item(painter)
x, y, w, h = self.get_bounds()

def p2d(pt):
"convert point to drawing coordinates"
# drawing coordinates are centered: (0,0) is in center
# our points are absolute: (0,0) is upper-left corner
if isinstance(pt, str):
# 2022-05-11: needed for backwards compatibility support
# PyDM releases up to v1.15.1
# adl2pydm tags up to 0.0.2
pt = tuple(map(int, pt.split(",")))
u, v = pt
return QPointF(u + x, v + y)

if len(self._points) > 1:
for i, p1 in enumerate(self._points[:-1]):
painter.drawLine(p2d(p1), p2d(self._points[i + 1]))
if self._arrow_mid_point_selection:
point1 = p2d(p1)
point2 = p2d(self._points[i + 1])
if self._arrow_mid_point_flipped:
point1, point2 = point2, point1 # swap values

# 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, 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]), 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]),
self._arrow_size,
self._arrow_size,
)
painter.drawPolygon(points)

def getPoints(self):
"""Convert internal points representation for use as QStringList."""
points = [f"{pt[0]}, {pt[1]}" for pt in self._points]
return points

def _validator(self, value):
"""
ensure that `value` has correct form
Parameters
----------
value : [ordered pairs]
List of strings representing ordered pairs
of integer coordinates. Each ordered pair
is a tuple or list.
Returns
----------
verified : [ordered pairs]
List of `tuple(number, number)`.
"""

def isfloat(value):
if isinstance(value, str):
value = value.strip()
try:
float(value)
return True
except Exception:
return False

def validate_point(i, point):
"""Ignore (instead of fail on) any of these pathologies."""
if isinstance(point, str):
try:
point = ast.literal_eval(point)
except SyntaxError:
logger.error(
"point %d must be two numbers, comma-separated, received '%s'",
i,
pt,
)
return
if not isinstance(point, (list, tuple)) or len(point) != 2:
logger.error(
"point %d must be two numbers, comma-separated, received '%s'",
i,
pt,
)
return
try:
point = list(map(float, point)) # ensure all values are float
except ValueError:
logger.error("point %d content must be numeric, received '%s'", i, pt)
return

return point

verified = []
for i, pt in enumerate(value, start=1):
point = validate_point(i, pt)
if point is not None:
verified.append(point)

return verified

def setPoints(self, value):
verified = self._validator(value)
if verified is not None:
if len(verified) < 2:
logger.error("Must have two or more points")
return

self._points = verified
self.update()

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):
"""
If True, an arrow will be drawn at the start of the last polyline segment.
Returns
-------
bool
"""
return self._arrow_end_point_selection

@arrowEndPoint.setter
def arrowEndPoint(self, new_selection):
"""
If True, an arrow will be drawn at the start of the last polyline segment.
Parameters
-------
new_selection : bool
"""
if self._arrow_end_point_selection != new_selection:
self._arrow_end_point_selection = new_selection
self.update()

@Property(bool)
def arrowStartPoint(self):
"""
If True, an arrow will be drawn at the start of the first polyline segment.
Returns
-------
bool
"""
return self._arrow_start_point_selection

@arrowStartPoint.setter
def arrowStartPoint(self, new_selection):
"""
If True, an arrow will be drawn at the start of the first polyline segment.
Parameters
-------
new_selection : bool
"""
if self._arrow_start_point_selection != new_selection:
self._arrow_start_point_selection = new_selection
self.update()

@Property(bool)
def arrowMidPoint(self):
"""
If True, an arrows will be drawn at the midpoints of the segments of the polyline.
Returns
-------
bool
"""
return self._arrow_mid_point_selection

@arrowMidPoint.setter
def arrowMidPoint(self, new_selection):
"""
If True, an arrows will be drawn at the midpoints of the segments of the polyline.
Parameters
-------
new_selection : bool
"""
if self._arrow_mid_point_selection != new_selection:
self._arrow_mid_point_selection = new_selection
self.update()

@Property(bool)
def flipMidPointArrow(self):
"""
Flips the direction of the midpoint arrows.
Returns
-------
bool
"""
return self._arrow_mid_point_flipped

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

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


class PyDMDrawingIrregularPolygon(PyDMDrawingPolyline):
"""
A widget contains an irregular polygon (arbitrary number of vertices, arbitrary lengths).
Expand Down

0 comments on commit c77d28a

Please sign in to comment.