Skip to content

Commit

Permalink
fix(interactive.imagetool): fix crash while linking more than 3 tools
Browse files Browse the repository at this point in the history
  • Loading branch information
kmnhan committed Jul 1, 2024
1 parent 6fcf2ab commit d5f8a30
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/erlab/interactive/imagetool/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import functools
import inspect
import os
import queue
import time
import warnings
import weakref
Expand Down Expand Up @@ -454,9 +453,13 @@ def __init__(

self.bench: bool = bench

# LIFO queues to handle undo and redo
self._prev_states: queue.LifoQueue = queue.LifoQueue(maxsize=1000)
self._next_states: queue.LifoQueue = queue.LifoQueue(maxsize=1000)
# Queues to handle undo and redo
self._prev_states: collections.deque[ImageSlicerState] = collections.deque(
maxlen=1000
)
self._next_states: collections.deque[ImageSlicerState] = collections.deque(
maxlen=1000
)

# Flag to prevent writing history when restoring state
self._write_history: bool = True
Expand Down Expand Up @@ -695,11 +698,11 @@ def data(self) -> xr.DataArray:

@property
def undoable(self) -> bool:
return not self._prev_states.empty()
return len(self._prev_states) > 0

@property
def redoable(self) -> bool:
return not self._next_states.empty()
return len(self._next_states) > 0

@contextlib.contextmanager
def history_suppressed(self):
Expand All @@ -722,10 +725,7 @@ def write_state(self) -> None:
if not self._write_history:
return

# with self._prev_states.mutex:
last_state = (
self._prev_states.queue[-1] if not self._prev_states.empty() else None
)
last_state = self._prev_states[-1] if self.undoable else None
curr_state = self.state

# Don't store splitter sizes in history
Expand All @@ -734,36 +734,33 @@ def write_state(self) -> None:
curr_state.pop("splitter_sizes", None)

if last_state is None or last_state != curr_state:
self._prev_states.put(curr_state)
with self._next_states.mutex:
self._next_states.queue.clear()
self._prev_states.append(curr_state)
self._next_states.clear()
self.sigHistoryChanged.emit()

@QtCore.Slot()
@suppress_history
def flush_history(self) -> None:
with self._prev_states.mutex:
self._prev_states.queue.clear()
with self._next_states.mutex:
self._next_states.queue.clear()
self._prev_states.clear()
self._next_states.clear()
self.sigHistoryChanged.emit()

@QtCore.Slot()
@suppress_history
def undo(self) -> None:
if not self.undoable:
raise RuntimeError("Nothing to undo")
self._next_states.put(self.state)
self.state = self._prev_states.get()
self._next_states.append(self.state)
self.state = self._prev_states.pop()
self.sigHistoryChanged.emit()

@QtCore.Slot()
@suppress_history
def redo(self) -> None:
if not self.redoable:
raise RuntimeError("Nothing to redo")
self._prev_states.put(self.state)
self.state = self._next_states.get()
self._prev_states.append(self.state)
self.state = self._next_states.pop()
self.sigHistoryChanged.emit()

def connect_axes_signals(self):
Expand Down

0 comments on commit d5f8a30

Please sign in to comment.