Skip to content

Commit

Permalink
Switch to confuse for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Sep 7, 2022
1 parent e12d6f8 commit 0739c9d
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 37 deletions.
2 changes: 1 addition & 1 deletion dev-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ channels:
- defaults
dependencies:
# Project dependencies
- dynaconf
- confuse
- labeling >= 0.1.12
- magicgui >= 0.5.1
- napari
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ channels:
- defaults
dependencies:
# Project depenencies
- dynaconf
- confuse
- labeling >= 0.1.12
- magicgui >= 0.5.1
- napari
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dev =
autopep8
black
build
confuse
flake8
isort
pyqt5
Expand Down
4 changes: 4 additions & 0 deletions src/napari_imagej/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
napari-imagej is built upon the PyImageJ project:
https://pyimagej.readthedocs.io/en/latest/
"""
import confuse

__version__ = "0.0.1.dev0"

settings = confuse.Configuration(appname="napari-imagej", modname=__name__)
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# napari-imagej Settings
# napari-imagej Default Settings

# USERS BEWARE:
# This toml file will soon be replaced with napari's contribution configuration.
# This yaml file will soon be replaced with napari's contribution configuration.

# Path to a local ImageJ2 installation (e.g. /Applications/Fiji.app),
# OR version of net.imagej:imagej artifact to launch (e.g. 2.3.0),
# OR endpoint of another artifact built on ImageJ2 (e.g. sc.fiji:fiji),
# OR list of Maven artifacts to include (e.g.
# ['net.imagej:imagej:2.3.0', 'net.imagej:imagej-legacy', 'net.preibisch:BigStitcher']).
# The default is the latest version of ImageJ2.
imagej_directory_or_endpoint = "net.imagej:imagej"
# The default (null) will use the latest version of ImageJ2, downloading it if needed.
imagej_directory_or_endpoint: 'net.imagej:imagej'

# This can be used to identify whether transferred data between ImageJ2 and napari
# should be selected via activation or by user selection via a dialog.
# By default, the active layer/window is chosen for transfer between applications.
# By setting this value to false, a popup will be shown instead.
choose_active_layer = true
choose_active_layer: true
8 changes: 4 additions & 4 deletions src/napari_imagej/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from jpype import JClass
from scyjava import config, jimport

from napari_imagej.settings import preferences
from napari_imagej import settings
from napari_imagej.utilities.logging import log_debug

# -- ImageJ API -- #
Expand Down Expand Up @@ -74,14 +74,14 @@ def _imagej_init():
log_debug("Completed JVM Configuration")

# Configure PyImageJ settings
settings = {
"ij_dir_or_version_or_endpoint": preferences.imagej_directory_or_endpoint,
ij_settings = {
"ij_dir_or_version_or_endpoint": settings["imagej_directory_or_endpoint"].get(),
"mode": get_mode(),
"add_legacy": False,
}

# Launch PyImageJ
_ij = imagej.init(**settings)
_ij = imagej.init(**ij_settings)
log_debug(f"Initialized at version {_ij.getVersion()}")

# Return the ImageJ gateway
Expand Down
13 changes: 0 additions & 13 deletions src/napari_imagej/settings.py

This file was deleted.

44 changes: 39 additions & 5 deletions src/napari_imagej/widgets/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from qtpy.QtGui import QIcon, QPixmap
from qtpy.QtWidgets import QHBoxLayout, QMessageBox, QPushButton, QWidget

from napari_imagej import settings
from napari_imagej.java import ensure_jvm_started, ij, jc, log_debug, running_headless
from napari_imagej.settings import preferences
from napari_imagej.utilities._module_utils import _get_layers_hack
from napari_imagej.widgets.resources import resource_path

Expand All @@ -35,6 +35,9 @@ def __init__(self, viewer: Viewer):
self.gui_button: GUIButton = GUIButton()
self.layout().addWidget(self.gui_button)

self.settings_button: SettingsButton = SettingsButton(viewer)
self.layout().addWidget(self.settings_button)

if running_headless():
self.gui_button.clicked.connect(self.gui_button.disable_popup)
else:
Expand Down Expand Up @@ -128,10 +131,11 @@ def __init__(self, viewer: Viewer):
self.setEnabled(False)
icon = QColoredSVGIcon.from_resources("long_right_arrow")
self.setIcon(icon.colored(theme=viewer.theme))
self.setToolTip("Export active napari layer to ImageJ2")
if preferences.choose_active_layer:
if settings["choose_active_layer"].get():
self.setToolTip("Export active napari layer to ImageJ2")
self.clicked.connect(self.send_active_layer)
else:
self.setToolTip("Export napari layer to ImageJ2")
self.clicked.connect(self.send_chosen_layer)

def _set_icon(self, path: str):
Expand Down Expand Up @@ -173,10 +177,11 @@ def __init__(self, viewer: Viewer):
self.setEnabled(False)
icon = QColoredSVGIcon.from_resources("long_left_arrow")
self.setIcon(icon.colored(theme=viewer.theme))
self.setToolTip("Import active ImageJ2 Dataset to napari")
if preferences.choose_active_layer:
if settings["choose_active_layer"].get():
self.setToolTip("Import active ImageJ2 Dataset to napari")
self.clicked.connect(self.get_active_layer)
else:
self.setToolTip("Import ImageJ2 Dataset to napari")
self.clicked.connect(self.get_chosen_layer)

def _set_icon(self, path: str):
Expand Down Expand Up @@ -281,3 +286,32 @@ def disable_popup(self):
msg.setTextFormat(Qt.RichText)
msg.setTextInteractionFlags(Qt.TextBrowserInteraction)
msg.exec()


class SettingsButton(QPushButton):
def __init__(self, viewer: Viewer):
super().__init__()
self.viewer = viewer

icon = QColoredSVGIcon(resource_path("gear"))
self.setIcon(icon.colored(theme=viewer.theme))

self.clicked.connect(self._update_settings)

def _update_settings(self):
args = {}
for k, v in settings.items():
args[k] = {}
args[k]["value"] = v.get()
choices = request_values(title="napari-imagej Settings", values=args)
if choices is not None:
any_changed = False
for k, v in choices.items():
if v != settings[k].get():
any_changed = True
settings[k].set(v)

if any_changed:
output = settings.dump()
with open(settings.user_config_path(), "w") as f:
f.write(output)
Loading

0 comments on commit 0739c9d

Please sign in to comment.