diff --git a/src/erlab/interactive/imagetool/core.py b/src/erlab/interactive/imagetool/core.py index 0fabf4e..b4f5779 100644 --- a/src/erlab/interactive/imagetool/core.py +++ b/src/erlab/interactive/imagetool/core.py @@ -527,8 +527,6 @@ def __init__( if self.bench: print("\n") - self.flush_history() - @property def colormap_properties(self) -> ColorMapState: prop = copy.deepcopy(self._colormap_properties) @@ -799,21 +797,25 @@ def refresh_current(self, axes: tuple[int, ...] | None = None): def refresh(self, cursor: int, axes: tuple[int, ...] | None = None): self.sigIndexChanged.emit(cursor, axes) + @record_history def view_all(self): for ax in self.axes: ax.vb.enableAutoRange() ax.vb.updateAutoRange() @link_slicer + @record_history def center_all_cursors(self): for i in range(self.n_cursors): self.array_slicer.center_cursor(i) @link_slicer + @record_history def center_cursor(self): self.array_slicer.center_cursor(self.current_cursor) @link_slicer + @record_history def set_current_cursor(self, cursor: int, update: bool = True): cursor = cursor % self.n_cursors if cursor > self.n_cursors - 1: @@ -893,6 +895,7 @@ def set_data( self.set_colormap(update=True) self._colorbar.cb.setImageItem() self.lock_levels(False) + self.flush_history() def update_values(self, values: npt.NDArray | xr.DataArray, update: bool = True): """Update only the values of the data. @@ -1283,6 +1286,8 @@ def changeEvent(self, evt: QtCore.QEvent | None): class ItoolCursorLine(pg.InfiniteLine): + sigDragStarted = QtCore.Signal(object) + def __init__(self, *args, **kargs): super().__init__(*args, **kargs) @@ -1314,7 +1319,7 @@ def mouseDragEvent(self, ev: mouseEvents.MouseDragEvent): ev.buttonDownPos() ) self.startPosition = self.pos() - self.slicer_area.sigWriteHistory.emit() + self.sigDragStarted.emit(self) ev.accept() if not self.moving: @@ -1720,6 +1725,7 @@ def add_cursor(self, update: bool = True): lambda v, *, line=c, axis=ax: self.line_drag(line, v.temp_value, axis) ) c.sigClicked.connect(lambda *, line=c: self.line_click(line)) + c.sigDragStarted.connect(lambda: self.slicer_area.sigWriteHistory.emit()) if update: self.refresh_cursor(new_cursor) diff --git a/src/erlab/interactive/imagetool/slicer.py b/src/erlab/interactive/imagetool/slicer.py index fd3b409..f309012 100644 --- a/src/erlab/interactive/imagetool/slicer.py +++ b/src/erlab/interactive/imagetool/slicer.py @@ -609,9 +609,11 @@ def index_of_value( def isel_args( self, cursor: int, disp: Sequence[int], int_if_one: bool = False ) -> dict[str, slice | int]: - axis = sorted(set(range(self._obj.ndim)) - set(disp)) + axis: list[int] = sorted(set(range(self._obj.ndim)) - set(disp)) return { - str(self._obj.dims[ax]): self._bin_slice(cursor, ax, int_if_one) + str(self._obj.dims[ax]).rstrip("_idx") + if ax in self._nonuniform_axes + else str(self._obj.dims[ax]): self._bin_slice(cursor, ax, int_if_one) for ax in axis } @@ -647,7 +649,9 @@ def qsel_args(self, cursor: int, disp: Sequence[int]) -> dict: return out def qsel_code(self, cursor: int, disp: Sequence[int]) -> str: - if self._nonuniform_axes: + if any( + a in self._nonuniform_axes for a in set(range(self._obj.ndim)) - set(disp) + ): # Has non-uniform axes, fallback to isel return self.isel_code(cursor, disp) diff --git a/tests/interactive/test_imagetool.py b/tests/interactive/test_imagetool.py index f97be36..da2e4a6 100644 --- a/tests/interactive/test_imagetool.py +++ b/tests/interactive/test_imagetool.py @@ -60,6 +60,9 @@ def test_itool(qtbot): win.array_slicer.set_bin(0, 1, 2, update=True) move_and_compare_values(qtbot, win, [9.0, 8.0, 3.0, 4.0]) + # Test code generation + assert win.array_slicer.qsel_code(0, (0,)) == ".qsel(x=1.5, x_width=2.0)" + # Set colormap and gamma win.slicer_area.set_colormap( "ColdWarm", gamma=1.5, reversed=True, high_contrast=True, zero_centered=True