def test_magicgui_get_viewer(make_napari_viewer): """Test that annotating with napari.Viewer gets the Viewer""" # Make two DIFFERENT viewers viewer1 = make_napari_viewer() viewer2 = make_napari_viewer() assert viewer2 is not viewer1 # Ensure one is returned by napari.current_viewer() from napari import current_viewer assert current_viewer() is viewer2 @magicgui def func(v: Viewer): return v def func_returns(v: Viewer) -> bool: """Helper function determining whether func() returns v""" func_viewer = func() assert isinstance(func_viewer, PublicOnlyProxy) return func_viewer.__wrapped__ is v # We expect func's Viewer to be current_viewer, not viewer assert func_returns(viewer2) assert not func_returns(viewer1) # With viewer as parent, it should be returned instead viewer1.window.add_dock_widget(func) assert func_returns(viewer1) assert not func_returns(viewer2) # no widget should be shown assert not func.v.visible # ensure that viewer2 is still the current viewer assert current_viewer() is viewer2
def test_current_viewer(make_napari_viewer): """Test that the viewer made last is the "current_viewer()" until another is activated""" # Make two DIFFERENT viewers viewer1: Viewer = make_napari_viewer() viewer2: Viewer = make_napari_viewer() assert viewer2 is not viewer1 # Ensure one is returned by napari.current_viewer() from napari import current_viewer assert current_viewer() is viewer2 assert current_viewer() is not viewer1 viewer1.window.activate() assert current_viewer() is viewer1 assert current_viewer() is not viewer2
def _widget_is_checked(widget_name: str) -> bool: # Get napari viewer from current process viewer = napari.current_viewer() # Get scene management widget scene_manager_choices_widget = viewer.window._dock_widgets[ AICSIMAGEIO_CHOICES] for child in scene_manager_choices_widget.widget().children(): if isinstance(child, QCheckBox): if child.text() == widget_name: return child.isChecked() return False
def get_or_create_viewer(): import napari return napari.current_viewer() or napari.Viewer()
def _get_scenes(path: "PathLike", img: AICSImage, in_memory: bool) -> None: # Get napari viewer from current process viewer = napari.current_viewer() # Add a checkbox widget if not present if AICSIMAGEIO_CHOICES not in viewer.window._dock_widgets: # Create a checkbox widget to set "Clear On Scene Select" or not scene_clear_checkbox = QCheckBox(CLEAR_LAYERS_ON_SELECT) scene_clear_checkbox.setChecked(False) # Create a checkbox widget to set "Unpack Channels" or not channel_unpack_checkbox = QCheckBox(UNPACK_CHANNELS_TO_LAYERS) channel_unpack_checkbox.setChecked(False) # Add all scene management state to a single box scene_manager_group = QGroupBox() scene_manager_group_layout = QVBoxLayout() scene_manager_group_layout.addWidget(scene_clear_checkbox) scene_manager_group_layout.addWidget(channel_unpack_checkbox) scene_manager_group.setLayout(scene_manager_group_layout) scene_manager_group.setFixedHeight(100) viewer.window.add_dock_widget( scene_manager_group, area="right", name=AICSIMAGEIO_CHOICES, ) # Create the list widget and populate with the ids & scenes in the file list_widget = QListWidget() for i, scene in enumerate(img.scenes): list_widget.addItem(f"{i}{SCENE_LABEL_DELIMITER}{scene}") # Add this files scenes widget to viewer viewer.window.add_dock_widget( list_widget, area="right", name=f"{Path(path).name}{SCENE_LABEL_DELIMITER}Scenes", ) # Function to create image layer from a scene selected in the list widget def open_scene(item: QListWidgetItem) -> None: scene_text = item.text() # Use scene indexes to cover for duplicate names scene_index = int(scene_text.split(SCENE_LABEL_DELIMITER)[0]) # Update scene on image and get data img.set_scene(scene_index) data = _get_full_image_data(img=img, in_memory=in_memory) # Get metadata and add to image meta = _get_meta(data, img) # Optionally clear layers if _widget_is_checked(CLEAR_LAYERS_ON_SELECT): viewer.layers.clear() # Optionally remove channel axis if not _widget_is_checked(UNPACK_CHANNELS_TO_LAYERS): meta["name"] = scene_text meta.pop("channel_axis", None) viewer.add_image(data, **meta) list_widget.currentItemChanged.connect(open_scene) # type: ignore
def test_top_level_availability(make_napari_viewer): """Current viewer should be available at napari.current_viewer.""" viewer = make_napari_viewer() assert viewer == napari.current_viewer()
""" Get a reference to the current napari viewer. Whilst this example is contrived, it can be useful to get a reference to the viewer when the viewer is out of scope. """ import napari # create viewer viewer = napari.Viewer() # lose reference to viewer viewer = 'oops no viewer here' # get that reference again viewer = napari.current_viewer()