-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add widget input validation/adaptation and add halo to Napari GUI
- Loading branch information
Showing
3 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Widget input validation""" | ||
|
||
from psygnal import Signal | ||
from functools import wraps | ||
|
||
def change_handler(*widgets, init=True, debug=False): | ||
def decorator_change_handler(handler): | ||
@wraps(handler) | ||
def wrapper(*args): | ||
source = Signal.sender() | ||
emitter = Signal.current_emitter() | ||
if debug: | ||
# print(f"{emitter}: {source} = {args!r}") | ||
print(f"EVENT '{str(emitter.name)}': {source.name:>20} = {args!r}") | ||
# print(f" {source.name:>14}.value = {source.value}") | ||
return handler(*args) | ||
|
||
for widget in widgets: | ||
widget.changed.connect(wrapper) | ||
if init: | ||
widget.changed(widget.value) | ||
return wrapper | ||
|
||
return decorator_change_handler | ||
|
||
|
||
def get_image_volume_from_layer(image): | ||
"""Used for widget parameter validation in `change_handler`s.""" | ||
image = image.data[0] if image.multiscale else image.data | ||
if not all(hasattr(image, attr) for attr in ("shape", "ndim", "__getitem__")): | ||
image = np.asanyarray(image) | ||
return image | ||
|
||
|
||
def widgets_inactive(*widgets, active): | ||
"""Toggle visibility of widgets.""" | ||
for widget in widgets: | ||
widget.visible = active | ||
|
||
|
||
def widgets_valid(*widgets, valid): | ||
"""Toggle background warning color of widgets.""" | ||
for widget in widgets: | ||
widget.native.setStyleSheet("" if valid else "background-color: lightcoral") | ||
|
||
|