From 0042024f750aac7f979a9aa7ac64b4b95b0613a8 Mon Sep 17 00:00:00 2001 From: Peter Sobolewski Date: Fri, 11 Nov 2022 22:46:52 +0100 Subject: [PATCH 1/4] Make scene settings persistent via magic_factory --- napari_aicsimageio/core.py | 69 +++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/napari_aicsimageio/core.py b/napari_aicsimageio/core.py index c2fa461..4e0a44b 100644 --- a/napari_aicsimageio/core.py +++ b/napari_aicsimageio/core.py @@ -4,6 +4,7 @@ from functools import partial from logging import getLogger +from magicgui import magic_factory from pathlib import Path from typing import TYPE_CHECKING, Any, Dict, List, Optional @@ -26,8 +27,8 @@ ############################################################################### AICSIMAGEIO_CHOICES = "AICSImageIO Scene Management" -CLEAR_LAYERS_ON_SELECT = "Clear All Layers on New Scene Selection" -UNPACK_CHANNELS_TO_LAYERS = "Unpack Channels as Layers" +CLEAR_LAYERS_ON_SELECT = False +UNPACK_CHANNELS_TO_LAYERS = False SCENE_LABEL_DELIMITER = " :: " @@ -121,20 +122,24 @@ def _get_meta(path: "PathLike", data: xr.DataArray, img: AICSImage) -> Dict[str, return meta -def _widget_is_checked(widget_name: str) -> bool: - import napari - - # 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 +@magic_factory(call_button="Apply Scene Settings", + info_label=dict( + widget_type="Label", + label="

For each napari session, \ +
to use the settings: \ +
press the Apply button!

", + ), + persist=True, + ) +def set_scene_settings( + info_label: str, + clear_layers: bool = CLEAR_LAYERS_ON_SELECT, + unpack_channels: bool = UNPACK_CHANNELS_TO_LAYERS, + ) -> None: + global CLEAR_LAYERS_ON_SELECT + CLEAR_LAYERS_ON_SELECT = clear_layers + global UNPACK_CHANNELS_TO_LAYERS + UNPACK_CHANNELS_TO_LAYERS = unpack_channels # Function to handle multi-scene files. @@ -143,30 +148,16 @@ def _get_scenes(path: "PathLike", img: AICSImage, in_memory: bool) -> None: # Get napari viewer from current process viewer = napari.current_viewer() + scene_settings_widget = set_scene_settings() + # 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, - ) + viewer.window.add_dock_widget(scene_settings_widget, + area = 'right', + name = AICSIMAGEIO_CHOICES + ) + # Create the list widget and populate with the ids & scenes in the file list_widget = QListWidget() @@ -195,11 +186,11 @@ def open_scene(item: QListWidgetItem) -> None: meta = _get_meta("", data, img) # Optionally clear layers - if _widget_is_checked(CLEAR_LAYERS_ON_SELECT): + if CLEAR_LAYERS_ON_SELECT: viewer.layers.clear() # Optionally remove channel axis - if not _widget_is_checked(UNPACK_CHANNELS_TO_LAYERS): + if not UNPACK_CHANNELS_TO_LAYERS: meta["name"] = scene_text meta.pop("channel_axis", None) From be80320deca21b3c8560cfc14dabd214d94268c8 Mon Sep 17 00:00:00 2001 From: Peter Sobolewski Date: Fri, 11 Nov 2022 23:02:56 +0100 Subject: [PATCH 2/4] Tweak height --- napari_aicsimageio/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/napari_aicsimageio/core.py b/napari_aicsimageio/core.py index 4e0a44b..36679f9 100644 --- a/napari_aicsimageio/core.py +++ b/napari_aicsimageio/core.py @@ -149,13 +149,13 @@ def _get_scenes(path: "PathLike", img: AICSImage, in_memory: bool) -> None: # Get napari viewer from current process viewer = napari.current_viewer() scene_settings_widget = set_scene_settings() - + scene_settings_widget.max_height = 200 # Add a checkbox widget if not present if AICSIMAGEIO_CHOICES not in viewer.window._dock_widgets: viewer.window.add_dock_widget(scene_settings_widget, - area = 'right', - name = AICSIMAGEIO_CHOICES + area = 'right', + name = AICSIMAGEIO_CHOICES, ) From 96f0d700625537942fbd420c3d53dc0d91671cc5 Mon Sep 17 00:00:00 2001 From: Peter Sobolewski Date: Fri, 11 Nov 2022 23:31:32 +0100 Subject: [PATCH 3/4] lint --- napari_aicsimageio/core.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/napari_aicsimageio/core.py b/napari_aicsimageio/core.py index 36679f9..49e2836 100644 --- a/napari_aicsimageio/core.py +++ b/napari_aicsimageio/core.py @@ -11,11 +11,8 @@ from aicsimageio import AICSImage, exceptions from aicsimageio.dimensions import DimensionNames from qtpy.QtWidgets import ( - QCheckBox, - QGroupBox, QListWidget, QListWidgetItem, - QVBoxLayout, ) if TYPE_CHECKING: @@ -122,20 +119,21 @@ def _get_meta(path: "PathLike", data: xr.DataArray, img: AICSImage) -> Dict[str, return meta -@magic_factory(call_button="Apply Scene Settings", +@magic_factory( + call_button="Apply Scene Settings", info_label=dict( - widget_type="Label", - label="

For each napari session, \ + widget_type="Label", + label="

For each napari session, \
to use the settings: \
press the Apply button!

", - ), - persist=True, - ) + ), + persist=True, +) def set_scene_settings( info_label: str, clear_layers: bool = CLEAR_LAYERS_ON_SELECT, unpack_channels: bool = UNPACK_CHANNELS_TO_LAYERS, - ) -> None: +) -> None: global CLEAR_LAYERS_ON_SELECT CLEAR_LAYERS_ON_SELECT = clear_layers global UNPACK_CHANNELS_TO_LAYERS @@ -150,14 +148,14 @@ def _get_scenes(path: "PathLike", img: AICSImage, in_memory: bool) -> None: viewer = napari.current_viewer() scene_settings_widget = set_scene_settings() scene_settings_widget.max_height = 200 - + # Add a checkbox widget if not present if AICSIMAGEIO_CHOICES not in viewer.window._dock_widgets: - viewer.window.add_dock_widget(scene_settings_widget, - area = 'right', - name = AICSIMAGEIO_CHOICES, - ) - + viewer.window.add_dock_widget( + scene_settings_widget, + area="right", + name=AICSIMAGEIO_CHOICES, + ) # Create the list widget and populate with the ids & scenes in the file list_widget = QListWidget() From 25ca230634fae416b983f307a30bac64b1a894a6 Mon Sep 17 00:00:00 2001 From: Peter Sobolewski Date: Fri, 11 Nov 2022 23:39:06 +0100 Subject: [PATCH 4/4] isort lint --- napari_aicsimageio/core.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/napari_aicsimageio/core.py b/napari_aicsimageio/core.py index 49e2836..b7d59e8 100644 --- a/napari_aicsimageio/core.py +++ b/napari_aicsimageio/core.py @@ -4,16 +4,13 @@ from functools import partial from logging import getLogger -from magicgui import magic_factory from pathlib import Path from typing import TYPE_CHECKING, Any, Dict, List, Optional from aicsimageio import AICSImage, exceptions from aicsimageio.dimensions import DimensionNames -from qtpy.QtWidgets import ( - QListWidget, - QListWidgetItem, -) +from magicgui import magic_factory +from qtpy.QtWidgets import QListWidget, QListWidgetItem if TYPE_CHECKING: import xarray as xr