Skip to content

Commit bdd0867

Browse files
committed
Using PythonQwt 0.12 new QwtLinearColormap object
1 parent c65ba1e commit bdd0867

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

plotpy/widgets/colormap/widget.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ class EditableColormap(QwtLinearColorMap):
5858

5959
def __init__(self, *args, name: str | None = None) -> None:
6060
super().__init__(*args)
61-
# TODO: Add this feature in a release of QwtPython
62-
# pylint: disable=no-member
63-
self.stops: list[ColorStop] = (
64-
self._QwtLinearColorMap__data.colorStops._ColorStops__stops # type: ignore
65-
)
6661
self.name = name or "temporary"
6762

63+
@property
64+
def color_stop_values(self) -> list[float]:
65+
"""Returns the position values of the color stops.
66+
67+
Returns:
68+
List of color stop positions.
69+
"""
70+
return [stop.pos for stop in self.colorStops()]
71+
6872
def is_boundary_stop_index(self, stop_index: int) -> bool:
6973
"""Checks if the given index is a boundary index (first or last).
7074
@@ -88,7 +92,7 @@ def _can_update_next_stop(self, stop_index: int) -> bool:
8892
Returns:
8993
True if the next color stop can be updated, False otherwise.
9094
"""
91-
return stop_index < (len(self.stops) - 1)
95+
return stop_index < (len(self.colorStops()) - 1)
9296

9397
def _can_update_previous_stop(self, stop_index: int) -> bool:
9498
"""Checks if the previous color stop can be updated.
@@ -110,12 +114,12 @@ def update_stops_steps(self, stop_index: int) -> None:
110114
Args:
111115
stop_index: current color stop index to update.
112116
"""
113-
current_stop = self.stops[stop_index]
117+
current_stop = self.colorStops()[stop_index]
114118
if self._can_update_previous_stop(stop_index):
115-
prev_stop = self.stops[stop_index - 1]
119+
prev_stop: ColorStop = self.colorStops()[stop_index - 1]
116120
prev_stop.updateSteps(current_stop)
117121
if self._can_update_next_stop(stop_index):
118-
next_stop = self.stops[stop_index + 1]
122+
next_stop: ColorStop = self.colorStops()[stop_index + 1]
119123
current_stop.updateSteps(next_stop)
120124

121125
@classmethod
@@ -159,7 +163,9 @@ def to_tuples(self) -> tuple[tuple[float, str], ...]:
159163
Returns:
160164
Tuple of tuples composed of a float position and a hex color string.
161165
"""
162-
return tuple((stop.pos, QG.QColor(stop.rgb).name()) for stop in self.stops)
166+
return tuple(
167+
(stop.pos, QG.QColor(stop.rgb).name()) for stop in self.colorStops()
168+
)
163169

164170
def move_color_stop(
165171
self, stop_index: int, new_pos: float, new_color: QG.QColor | None = None
@@ -174,7 +180,7 @@ def move_color_stop(
174180
Defaults to None.
175181
"""
176182
try:
177-
stop: ColorStop | None = self.stops[stop_index]
183+
stop: ColorStop | None = self.colorStops()[stop_index]
178184
except IndexError as e:
179185
print(e)
180186
return
@@ -185,7 +191,7 @@ def move_color_stop(
185191

186192
new_color = new_color or QG.QColor(stop.rgb)
187193
new_stop = ColorStop(new_pos, new_color)
188-
self.stops[stop_index] = new_stop
194+
self.colorStops()[stop_index] = new_stop
189195
self.update_stops_steps(stop_index)
190196

191197
def delete_stop(self, stop_index: int) -> None:
@@ -196,24 +202,9 @@ def delete_stop(self, stop_index: int) -> None:
196202
stop_index: color stop index to delete
197203
"""
198204
if not self.is_boundary_stop_index(stop_index):
199-
self.stops.pop(stop_index)
200-
self.stops[stop_index - 1].updateSteps(self.stops[stop_index])
201-
202-
def setColorInterval(
203-
self, color1: QColorInitTypes, color2: QColorInitTypes
204-
) -> None:
205-
"""Overload of QwtLinearColorMap.setColorInterval to update the stops list
206-
attribute.
207-
208-
Args:
209-
color1: first color of the interval
210-
color2: last color of the interval
211-
"""
212-
super().setColorInterval(color1, color2)
213-
# pylint: disable=no-member
214-
self.stops: list[
215-
ColorStop
216-
] = self._QwtLinearColorMap__data.colorStops._ColorStops__stops # type: ignore
205+
cstops: list[ColorStop] = self.colorStops()
206+
cstops.pop(stop_index)
207+
cstops[stop_index - 1].updateSteps(cstops[stop_index])
217208

218209
def get_stop_color(self, index: int) -> QG.QColor:
219210
"""Returns the color of the given color stop index.
@@ -224,8 +215,9 @@ def get_stop_color(self, index: int) -> QG.QColor:
224215
Returns:
225216
Color of the given color stop index.
226217
"""
227-
index = min(index, len(self.stops))
228-
return QG.QColor(self.stops[index].rgb)
218+
index = min(index, len(self.colorStops()))
219+
cstop: ColorStop = self.colorStops()[index]
220+
return QG.QColor(cstop.rgb)
229221

230222

231223
class ColorMapWidget(QW.QWidget):
@@ -293,7 +285,7 @@ def __init__(
293285
self._colormap = EditableColormap(color1, color2)
294286
else:
295287
self._colormap = colormap
296-
self.set_handles_values(colormap.colorStops())
288+
self.set_handles_values(colormap.color_stop_values)
297289

298290
self.colortable = self._colormap.colorTable(self.qwt_color_interval)
299291

@@ -324,9 +316,8 @@ def set_colormap(self, colormap: EditableColormap) -> None:
324316
Args:
325317
colormap: replacement colormap
326318
"""
327-
new_values = colormap.colorStops()
328319
self._colormap = colormap
329-
self.set_handles_values(new_values)
320+
self.set_handles_values(colormap.color_stop_values)
330321
self.COLORMAP_CHANGED.emit()
331322

332323
def get_colormap(self) -> EditableColormap:
@@ -426,7 +417,7 @@ def edit_color_stop(
426417
new_color = QG.QColor(self.colortable[new_color])
427418

428419
self._colormap.move_color_stop(index, new_pos, new_color)
429-
self.set_handles_values(self._colormap.colorStops())
420+
self.set_handles_values(self._colormap.color_stop_values)
430421
self.COLORMAP_CHANGED.emit()
431422

432423
def _edit_color_map_on_slider_change(self, raw_values: tuple[float, ...]) -> None:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ classifiers = [
3131
requires-python = ">=3.8, <4"
3232
dependencies = [
3333
"guidata>=3.1",
34-
"PythonQwt>=0.10",
34+
"PythonQwt>=0.12",
3535
"NumPy>=1.17",
3636
"SciPy>=1.3",
3737
"Pillow",

0 commit comments

Comments
 (0)