Skip to content

Commit

Permalink
replace use of linestyle by dashpattern to centralize the conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
t20100 committed Dec 19, 2023
1 parent a21a3c4 commit 4d3c520
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/silx/gui/plot/backends/BackendOpenGL.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

class _ShapeItem(dict):
def __init__(
self, x, y, shape, color, fill, overlay, linestyle, linewidth, gapcolor
self, x, y, shape, color, fill, overlay, linewidth, dashpattern, gapcolor
):
super(_ShapeItem, self).__init__()

Expand All @@ -84,8 +84,8 @@ def __init__(
"fill": "hatch" if fill else None,
"x": x,
"y": y,
"linestyle": linestyle,
"linewidth": linewidth,
"dashpattern": dashpattern,
"gapcolor": gapcolor,
}
)
Expand All @@ -99,8 +99,8 @@ def __init__(
text,
color,
symbol,
linestyle,
linewidth,
dashpattern,
constraint,
yaxis,
font,
Expand All @@ -124,8 +124,8 @@ def __init__(
"color": colors.rgba(color),
"constraint": constraint if isConstraint else None,
"symbol": symbol,
"linestyle": linestyle,
"linewidth": linewidth,
"dashpattern": dashpattern,
"yaxis": yaxis,
"font": font,
"bgcolor": bgcolor,
Expand Down Expand Up @@ -575,7 +575,7 @@ def _renderItems(self, overlay=False):
)

# Draw the stroke
if item["linestyle"] not in ("", " ", None):
if item["dashpattern"] is not None:
if item["shape"] != "polylines":
# close the polyline
points = numpy.append(
Expand All @@ -585,10 +585,10 @@ def _renderItems(self, overlay=False):
lines = glutils.GLLines2D(
points[:, 0],
points[:, 1],
style=item["linestyle"],
color=item["color"],
gapColor=item["gapcolor"],
width=item["linewidth"],
dashPattern=item["dashpattern"],
)
context.matrix = self.matScreenProj
lines.render(context)
Expand Down Expand Up @@ -636,9 +636,9 @@ def _renderItems(self, overlay=False):
lines = glutils.GLLines2D(
(0, width),
(pixelPos[1], pixelPos[1]),
style=item["linestyle"],
color=color,
width=item["linewidth"],
dashPattern=item["dashpattern"],
)
context.matrix = self.matScreenProj
lines.render(context)
Expand Down Expand Up @@ -669,9 +669,9 @@ def _renderItems(self, overlay=False):
lines = glutils.GLLines2D(
(pixelPos[0], pixelPos[0]),
(0, height),
style=item["linestyle"],
color=color,
width=item["linewidth"],
dashPattern=item["dashpattern"],
)
context.matrix = self.matScreenProj
lines.render(context)
Expand Down Expand Up @@ -859,6 +859,22 @@ def _castArrayTo(v):
else:
raise ValueError("Unsupported data type")

_DASH_PATTERNS = { # Convert from linestyle to dash pattern
"": None,
" ": None,
"-": (),
"--": (3.7, 1.6, 3.7, 1.6),
"-.": (6.4, 1.6, 1, 1.6),
":": (1, 1.65, 1, 1.65),
None: None,
}

def _lineStyleToDashPattern(
self, style: str | None
) -> tuple[float, float, float, float] | tuple[()] | None:
"""Convert a linestyle to its corresponding dash pattern"""
return self._DASH_PATTERNS[style]

def addCurve(
self,
x,
Expand Down Expand Up @@ -977,16 +993,17 @@ def addCurve(
fillColor = None
if fill is True:
fillColor = color

curve = glutils.GLPlotCurve2D(
x,
y,
colorArray,
xError=xerror,
yError=yerror,
lineStyle=linestyle,
lineColor=color,
lineGapColor=gapcolor,
lineWidth=linewidth,
lineDashPattern=self._lineStyleToDashPattern(linestyle),
marker=symbol,
markerColor=color,
markerSize=symbolsize,
Expand Down Expand Up @@ -1091,8 +1108,9 @@ def addShape(
if self._plotFrame.yAxis.isLog and y.min() <= 0.0:
raise RuntimeError("Cannot add item with Y <= 0 with Y axis log scale")

dashpattern = self._lineStyleToDashPattern(linestyle)
return _ShapeItem(
x, y, shape, color, fill, overlay, linestyle, linewidth, gapcolor
x, y, shape, color, fill, overlay, linewidth, dashpattern, gapcolor
)

def addMarker(
Expand All @@ -1110,14 +1128,15 @@ def addMarker(
bgcolor: RGBAColorType | None,
):
font = qt.QApplication.instance().font() if font is None else font
dashpattern = self._lineStyleToDashPattern(linestyle)
return _MarkerItem(
x,
y,
text,
color,
symbol,
linestyle,
linewidth,
dashpattern,
constraint,
yaxis,
font,
Expand Down Expand Up @@ -1209,7 +1228,7 @@ def __pickCurves(self, item, x, y):
qtDpi = self.getDotsPerInch() / self.getDevicePixelRatio()
size = item.markerSize / 72.0 * qtDpi
offset = max(size / 2.0, offset)
if item.lineStyle is not None:
if item.lineDashPattern is not None:
# Convert line width from points to qt pixels
qtDpi = self.getDotsPerInch() / self.getDevicePixelRatio()
lineWidth = item.lineWidth / 72.0 * qtDpi
Expand Down

0 comments on commit 4d3c520

Please sign in to comment.