Skip to content

Commit 6a1cb99

Browse files
committed
improved testing and small optimizations and bug corrections
1 parent 50c69b3 commit 6a1cb99

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

plotpy/tests/features/test_colormap_editor.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import qtpy.QtCore as QC
1818
import qtpy.QtGui as QG
1919
import qtpy.QtWidgets as QW
20+
from guidata.env import execenv
2021

2122
from plotpy.widgets.colormap_editor import ColorMapEditor
2223
from plotpy.widgets.colormap_widget import CustomQwtLinearColormap
@@ -26,6 +27,7 @@
2627
app = QW.QApplication([])
2728
editor = ColorMapEditor(None)
2829
red = QG.QColor(QC.Qt.GlobalColor.red)
30+
green = QG.QColor(QC.Qt.GlobalColor.green)
2931
editor.colormap_widget.add_handle_at_relative_pos(0.5, red)
3032
editor.show()
3133
app.exec_()
@@ -57,11 +59,25 @@
5759
cmap_tuples = tuple((int(val * 255 + 1), color) for val, color in cmap_tuples)
5860
print(
5961
"Initialization of a new default colormap editor, "
60-
"modified post-initialization with the previous colormap with stops scaled by * 255 + 1: ",
62+
"modified post-initialization with the previous colormap with stops scaled by "
63+
"255 + 1: ",
6164
cmap_tuples,
6265
)
6366
new_cmap = CustomQwtLinearColormap.from_iterable(cmap_tuples)
6467
editor = ColorMapEditor(None)
6568
editor.set_colormap(new_cmap)
6669
editor.show()
6770
app.exec_()
71+
72+
print(
73+
"Initialization of a new default colormap editor, "
74+
"modified post-initialization with the previous colormap where the red stop is "
75+
"replaced with a green stop: ",
76+
cmap_tuples,
77+
)
78+
79+
editor = ColorMapEditor(None)
80+
editor.set_colormap(new_cmap)
81+
editor.colormap_widget.edit_color_stop(1, None, green)
82+
editor.show()
83+
app.exec_()

plotpy/widgets/colormap_editor.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ def update_colormap_widget(self):
269269

270270
if current_index > 0 and current_index < self.tabs.count() - 1:
271271
relative_pos = current_dataset.get_position()
272-
new_slider_values[current_index] = relative_pos
273-
self.colormap_widget.set_handles_values(new_slider_values)
274272
else:
275273
relative_pos = new_slider_values[current_index]
276274
current_dataset.set_position(relative_pos)

plotpy/widgets/colormap_manager.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(
4747

4848
if active_colormap is None or active_colormap.lower() not in ALL_COLORMAPS:
4949
active_colormap = next(iter(ALL_COLORMAPS))
50-
active_colormap = active_colormap.lower()
50+
# active_colormap = active_colormap.lower()
5151
self._cmap_choice = QW.QComboBox(
5252
self,
5353
)
@@ -231,9 +231,7 @@ class ColorMapManagerDialog(QW.QDialog):
231231
exists, will defaults to the first colormap in the list. Defaults to None
232232
"""
233233

234-
def __init__(
235-
self, parent: QWidget | None, active_colormap: str = "viridis"
236-
) -> None:
234+
def __init__(self, parent: QWidget | None, active_colormap: str = "jet") -> None:
237235
super().__init__(parent)
238236
self.setWindowIcon(get_icon("edit.png"))
239237
self.setWindowTitle(_("Colormap manager"))

plotpy/widgets/colormap_widget.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def edit_color_stop(
401401
new_color: QG.QColor | int | None = None,
402402
):
403403
"""Edit an existing color stop in the current colormap. Mutates the colormap
404-
object.
404+
object. Also edits the slider handle position.
405405
406406
Args:
407407
index: color stop index to mutate
@@ -416,6 +416,7 @@ def edit_color_stop(
416416
new_color = QG.QColor(self.colortable[new_color])
417417

418418
self._colormap.move_color_stop(index, new_pos, new_color)
419+
self.set_handles_values(self._colormap.colorStops())
419420
self.COLORMAP_CHANGED.emit()
420421

421422
def _edit_color_map_on_slider_change(self, raw_values: tuple[float, ...]):
@@ -460,7 +461,7 @@ def _get_neighbour_positions(self, pos: float) -> tuple[float, float]:
460461
"""
461462
values = self.get_handles_tuple()
462463
previous_pos = max(
463-
filter(lambda val: val < pos, values[::-1]), default=self.min
464+
filter(lambda val: val <= pos, values[::-1]), default=self.min
464465
)
465466
next_pos = min(filter(lambda val: val > pos, values), default=self.max)
466467

@@ -558,7 +559,9 @@ def add_handle_at_relative_pos(
558559
self, relative_pos: float, new_color: QG.QColor | int | None = None
559560
):
560561
"""insert a handle in the widget at the relative position (between 0. and 1.).
561-
Mutates the colormap object.
562+
Mutates the colormap object. If the relative position is already occupied by a
563+
handle, the new handle will be inserted at the closest available position then
564+
will be moved back to the requested position.
562565
563566
Args:
564567
relative_pos: insertion position
@@ -567,15 +570,16 @@ def add_handle_at_relative_pos(
567570
if new_color is None:
568571
new_color = self._colormap.color(self.qwt_color_interval, relative_pos)
569572

570-
self._colormap.addColorStop(relative_pos, new_color)
573+
new_relative_pos = self._new_available_pos(relative_pos)
574+
self._colormap.addColorStop(new_relative_pos, new_color)
571575

572576
values = self.get_handles_list()
573-
values.append(relative_pos)
577+
values.append(new_relative_pos)
574578
values.sort()
575-
self.set_handles_values(values)
579+
new_value_index = values.index(new_relative_pos)
576580

577-
self.COLORMAP_CHANGED.emit()
578-
self.HANDLE_ADDED.emit(values.index(relative_pos), relative_pos)
581+
self.edit_color_stop(new_value_index, relative_pos, None)
582+
self.HANDLE_ADDED.emit(new_value_index, relative_pos)
579583

580584
def get_handles_count(self) -> int:
581585
"""Number of slider handles.

0 commit comments

Comments
 (0)