Skip to content

Commit

Permalink
2370 Cancel recon (#2371)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc authored Nov 12, 2024
2 parents 48af91c + 0d4cd4c commit 2c19047
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#2370: Add a cancel button to AsyncTaskDialog progress window
2 changes: 1 addition & 1 deletion mantidimaging/core/utility/progress_reporting/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def update(self, steps: int = 1, msg: str = "", force_continue: bool = False) ->

# Force cancellation on progress update
if self.should_cancel and not force_continue:
raise RuntimeError('Task has been cancelled')
raise StopIteration('Task has been cancelled')

@staticmethod
def calculate_mean_time(progress_history: list[ProgressHistory]) -> float:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def test_cancellation(self):
self.assertTrue(p.should_cancel)

else:
with self.assertRaises(RuntimeError):
with self.assertRaises(StopIteration):
p.update()

self.assertFalse(p.is_completed())
Expand Down
6 changes: 6 additions & 0 deletions mantidimaging/gui/dialogs/async_task/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ def task_is_running(self) -> None:
def progress_update(self) -> None:
msg = self.progress.last_status_message()
self.progress_updated.emit(self.progress.completion(), msg if msg is not None else '')

def show_stop_button(self, show: bool = False) -> None:
self.view.show_cancel_button(show)

def stop_progress(self):
self.progress.cancel("Cancelled by user")
12 changes: 11 additions & 1 deletion mantidimaging/gui/dialogs/async_task/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def __init__(self, parent: QMainWindow):
self.progressBar.setMaximum(1000)

self.show_timer = QTimer(self)
self.cancelButton.clicked.connect(self.presenter.stop_progress)
self.cancelButton.hide()
self.hide()

@property
Expand Down Expand Up @@ -70,13 +72,20 @@ def show_from_timer(self) -> None:
if self._presenter is not None and self.presenter.task_is_running:
self.show()

def show_cancel_button(self, cancelable: bool) -> None:
if cancelable:
self.cancelButton.show()
else:
self.cancelButton.hide()


def start_async_task_view(parent: QMainWindow,
task: Callable,
on_complete: Callable,
kwargs: dict | None = None,
tracker: set[Any] | None = None,
busy: bool | None = False):
busy: bool | None = False,
cancelable: bool = False) -> None:
atd = AsyncTaskDialogView(parent)
if not kwargs:
kwargs = {'progress': Progress()}
Expand All @@ -88,6 +97,7 @@ def start_async_task_view(parent: QMainWindow,
atd.progressBar.setMinimum(0)
atd.progressBar.setMaximum(0)

atd.presenter.show_stop_button(cancelable)
atd.presenter.set_task(task)
atd.presenter.set_on_complete(on_complete)
atd.presenter.set_parameters(**kwargs)
Expand Down
7 changes: 7 additions & 0 deletions mantidimaging/gui/ui/async_task_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
<bool>false</bool>
</property>
</widget>
<item>
<widget class="QPushButton" name="cancelButton">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
</item>
</layout>
</widget>
Expand Down
6 changes: 4 additions & 2 deletions mantidimaging/gui/windows/recon/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def do_reconstruct_volume(self) -> None:
start_async_task_view(self.view,
self.model.run_full_recon,
self._on_volume_recon_done, {'recon_params': self.view.recon_params()},
tracker=self.async_tracker)
tracker=self.async_tracker,
cancelable=True)

def _get_reconstruct_slice(self, cor, slice_idx: int, call_back: Callable[[TaskWorkerThread], None]) -> None:
# If no COR is provided and there are regression results then calculate
Expand All @@ -238,7 +239,8 @@ def _get_reconstruct_slice(self, cor, slice_idx: int, call_back: Callable[[TaskW
'cor': cor,
'recon_params': self.view.recon_params()
},
tracker=self.async_tracker)
tracker=self.async_tracker,
cancelable=True)

def _get_slice_index(self, slice_idx: int | None) -> int:
if slice_idx is None:
Expand Down
3 changes: 2 additions & 1 deletion mantidimaging/gui/windows/recon/test/presenter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def test_do_reconstruct_volume(self, mock_async_task):
self.presenter.model.run_full_recon,
self.presenter._on_volume_recon_done,
{'recon_params': self.view.recon_params()},
tracker=self.presenter.async_tracker)
tracker=self.presenter.async_tracker,
cancelable=True)

@mock.patch('mantidimaging.gui.windows.recon.presenter.CORInspectionDialogView')
def test_do_refine_selected_cor_declined(self, mock_corview):
Expand Down

0 comments on commit 2c19047

Please sign in to comment.