Skip to content

Commit

Permalink
add more guards for check if in a proper thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Czaki committed Oct 16, 2022
1 parent 7f59296 commit 0bcb053
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions package/PartSeg/common_gui/napari_image_view.py
Expand Up @@ -17,7 +17,7 @@
from napari.utils.colormaps.colormap import ColormapInterpolationMode
from nme import register_class
from packaging.version import parse as parse_version
from qtpy.QtCore import QEvent, QPoint, Qt, QTimer, Signal
from qtpy.QtCore import QEvent, QPoint, Qt, QThread, QTimer, Signal
from qtpy.QtWidgets import QApplication, QCheckBox, QHBoxLayout, QLabel, QMenu, QSpinBox, QToolTip, QVBoxLayout, QWidget
from scipy.ndimage import binary_dilation
from superqt import QEnumComboBox, ensure_main_thread
Expand Down Expand Up @@ -177,6 +177,8 @@ def __init__(
self.mask_label = QLabel("Mask:")
self.mask_label.setVisible(False)

self.prepare_layers = thread_worker(_prepare_layers)

self.btn_layout = QHBoxLayout()
self.btn_layout.addWidget(self.reset_view_button)
self.btn_layout.addWidget(self.ndim_btn)
Expand Down Expand Up @@ -386,6 +388,8 @@ def update_points(self):
self.points_layer.data = np.empty((0, 4))

def set_roi(self, roi_info: Optional[ROIInfo] = None, image: Optional[Image] = None) -> None:
if QApplication.instance().thread() != QThread.currentThread():
raise RuntimeError("This method should be called from main thread")
image = self.get_image(image)
if roi_info is None:
roi_info = self.settings.roi_info
Expand Down Expand Up @@ -601,6 +605,9 @@ def calc_filter(j, layer_):
def _add_image(self, image_data: Tuple[ImageInfo, bool]):
self._remove_worker(self.sender())

if QApplication.instance().thread() != QThread.currentThread():
raise RuntimeError("Not in main thread")

image_info, replace = image_data
image = image_info.image
if replace:
Expand Down Expand Up @@ -674,7 +681,7 @@ def add_image(self, image: Optional[Image], replace=False):
return image

def _prepare_layers(self, image, parameters, replace):
worker = prepare_layers(image, parameters, replace)
worker = self.prepare_layers(image, parameters, replace)
worker.returned.connect(self._add_image)
self.worker_list.append(worker)
worker.start()
Expand Down
11 changes: 11 additions & 0 deletions package/PartSeg/common_gui/napari_viewer_wrap.py
Expand Up @@ -3,6 +3,7 @@

from napari import Viewer as NViewer
from napari.utils.colormaps import Colormap
from qtpy.QtCore import QCoreApplication, QThread
from qtpy.QtWidgets import QCheckBox, QFormLayout, QPushButton, QWidget

from PartSeg.common_backend.base_settings import BaseSettings
Expand Down Expand Up @@ -244,3 +245,13 @@ def create_initial_layers(
self._sync_widget.sync_additional()
if points:
self._sync_widget.sync_points()

def add_layer(self, layer):
if QCoreApplication.instance().thread() != QThread.currentThread():
raise RuntimeError("add_layer must be called from the main thread")
super().add_layer(layer)

def add_labels(self, *args, **kwargs):
if QCoreApplication.instance().thread() != QThread.currentThread():
raise RuntimeError("add_labels must be called from the main thread")
return super().add_labels(*args, **kwargs)

0 comments on commit 0bcb053

Please sign in to comment.