Skip to content

Commit 90eca7b

Browse files
committed
make OpenGL backend understand (offset, (dash pattern)) line style
1 parent 9705e1d commit 90eca7b

File tree

1 file changed

+53
-18
lines changed

1 file changed

+53
-18
lines changed

src/silx/gui/plot/backends/BackendOpenGL.py

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,17 @@
5858

5959
class _ShapeItem(dict):
6060
def __init__(
61-
self, x, y, shape, color, fill, overlay, linewidth, dashpattern, gapcolor
61+
self,
62+
x,
63+
y,
64+
shape,
65+
color,
66+
fill,
67+
overlay,
68+
linewidth,
69+
dashoffset,
70+
dashpattern,
71+
gapcolor,
6272
):
6373
super(_ShapeItem, self).__init__()
6474

@@ -85,6 +95,7 @@ def __init__(
8595
"x": x,
8696
"y": y,
8797
"linewidth": linewidth,
98+
"dashoffset": dashoffset,
8899
"dashpattern": dashpattern,
89100
"gapcolor": gapcolor,
90101
}
@@ -100,6 +111,7 @@ def __init__(
100111
color,
101112
symbol,
102113
linewidth,
114+
dashoffset,
103115
dashpattern,
104116
constraint,
105117
yaxis,
@@ -125,6 +137,7 @@ def __init__(
125137
"constraint": constraint if isConstraint else None,
126138
"symbol": symbol,
127139
"linewidth": linewidth,
140+
"dashoffset": dashoffset,
128141
"dashpattern": dashpattern,
129142
"yaxis": yaxis,
130143
"font": font,
@@ -588,6 +601,7 @@ def _renderItems(self, overlay=False):
588601
color=item["color"],
589602
gapColor=item["gapcolor"],
590603
width=item["linewidth"],
604+
dashOffset=item["dashoffset"],
591605
dashPattern=item["dashpattern"],
592606
)
593607
context.matrix = self.matScreenProj
@@ -638,6 +652,7 @@ def _renderItems(self, overlay=False):
638652
(pixelPos[1], pixelPos[1]),
639653
color=color,
640654
width=item["linewidth"],
655+
dashOffset=item["dashoffset"],
641656
dashPattern=item["dashpattern"],
642657
)
643658
context.matrix = self.matScreenProj
@@ -671,6 +686,7 @@ def _renderItems(self, overlay=False):
671686
(0, height),
672687
color=color,
673688
width=item["linewidth"],
689+
dashOffset=item["dashoffset"],
674690
dashPattern=item["dashpattern"],
675691
)
676692
context.matrix = self.matScreenProj
@@ -859,21 +875,28 @@ def _castArrayTo(v):
859875
else:
860876
raise ValueError("Unsupported data type")
861877

862-
_DASH_PATTERNS = { # Convert from linestyle to dash pattern
863-
"": None,
864-
" ": None,
865-
"-": (),
866-
"--": (3.7, 1.6, 3.7, 1.6),
867-
"-.": (6.4, 1.6, 1, 1.6),
868-
":": (1, 1.65, 1, 1.65),
869-
None: None,
878+
_DASH_PATTERNS = { # Convert from linestyle to offset and dash pattern
879+
"": (0.0, None),
880+
" ": (0.0, None),
881+
"-": (0.0, ()),
882+
"--": (0.0, (3.7, 1.6, 3.7, 1.6)),
883+
"-.": (0.0, (6.4, 1.6, 1, 1.6)),
884+
":": (0.0, (1, 1.65, 1, 1.65)),
885+
None: (0.0, None),
870886
}
871887

872-
def _lineStyleToDashPattern(
873-
self, style: str | None
874-
) -> tuple[float, float, float, float] | tuple[()] | None:
875-
"""Convert a linestyle to its corresponding dash pattern"""
876-
return self._DASH_PATTERNS[style]
888+
def _lineStyleToDashOffsetPattern(
889+
self, style
890+
) -> tuple[float, tuple[float, float, float, float] | tuple[()] | None]:
891+
"""Convert a linestyle to its corresponding offset and dash pattern"""
892+
if style is None or isinstance(style, str):
893+
return self._DASH_PATTERNS[style]
894+
895+
# (offset, (dash pattern))
896+
offset, pattern = style
897+
if len(pattern) == 2:
898+
pattern = pattern * 2
899+
return offset, pattern
877900

878901
def addCurve(
879902
self,
@@ -994,6 +1017,7 @@ def addCurve(
9941017
if fill is True:
9951018
fillColor = color
9961019

1020+
dashoffset, dashpattern = self._lineStyleToDashOffsetPattern(linestyle)
9971021
curve = glutils.GLPlotCurve2D(
9981022
x,
9991023
y,
@@ -1003,7 +1027,8 @@ def addCurve(
10031027
lineColor=color,
10041028
lineGapColor=gapcolor,
10051029
lineWidth=linewidth,
1006-
lineDashPattern=self._lineStyleToDashPattern(linestyle),
1030+
lineDashOffset=dashoffset,
1031+
lineDashPattern=dashpattern,
10071032
marker=symbol,
10081033
markerColor=color,
10091034
markerSize=symbolsize,
@@ -1108,9 +1133,18 @@ def addShape(
11081133
if self._plotFrame.yAxis.isLog and y.min() <= 0.0:
11091134
raise RuntimeError("Cannot add item with Y <= 0 with Y axis log scale")
11101135

1111-
dashpattern = self._lineStyleToDashPattern(linestyle)
1136+
dashoffset, dashpattern = self._lineStyleToDashOffsetPattern(linestyle)
11121137
return _ShapeItem(
1113-
x, y, shape, color, fill, overlay, linewidth, dashpattern, gapcolor
1138+
x,
1139+
y,
1140+
shape,
1141+
color,
1142+
fill,
1143+
overlay,
1144+
linewidth,
1145+
dashoffset,
1146+
dashpattern,
1147+
gapcolor,
11141148
)
11151149

11161150
def addMarker(
@@ -1128,14 +1162,15 @@ def addMarker(
11281162
bgcolor: RGBAColorType | None,
11291163
):
11301164
font = qt.QApplication.instance().font() if font is None else font
1131-
dashpattern = self._lineStyleToDashPattern(linestyle)
1165+
dashoffset, dashpattern = self._lineStyleToDashOffsetPattern(linestyle)
11321166
return _MarkerItem(
11331167
x,
11341168
y,
11351169
text,
11361170
color,
11371171
symbol,
11381172
linewidth,
1173+
dashoffset,
11391174
dashpattern,
11401175
constraint,
11411176
yaxis,

0 commit comments

Comments
 (0)