mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
feat(image_layer): add default name for image layers
This commit is contained in:
@ -9,6 +9,7 @@ from pydantic import BaseModel, ConfigDict, Field, ValidationError
|
|||||||
from qtpy.QtCore import QPointF, Signal, SignalInstance
|
from qtpy.QtCore import QPointF, Signal, SignalInstance
|
||||||
from qtpy.QtWidgets import QDialog, QVBoxLayout
|
from qtpy.QtWidgets import QDialog, QVBoxLayout
|
||||||
|
|
||||||
|
from bec_widgets.utils.container_utils import WidgetContainerUtils
|
||||||
from bec_widgets.utils.error_popups import SafeProperty, SafeSlot
|
from bec_widgets.utils.error_popups import SafeProperty, SafeSlot
|
||||||
from bec_widgets.utils.side_panel import SidePanel
|
from bec_widgets.utils.side_panel import SidePanel
|
||||||
from bec_widgets.utils.toolbar import MaterialIconAction, SwitchableToolBarAction
|
from bec_widgets.utils.toolbar import MaterialIconAction, SwitchableToolBarAction
|
||||||
@ -93,7 +94,7 @@ class ImageLayerManager:
|
|||||||
|
|
||||||
def add(
|
def add(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str | None = None,
|
||||||
z_position: int | Literal["top", "bottom"] | None = None,
|
z_position: int | Literal["top", "bottom"] | None = None,
|
||||||
sync: ImageLayerSync | None = None,
|
sync: ImageLayerSync | None = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
@ -102,12 +103,16 @@ class ImageLayerManager:
|
|||||||
Add an image layer to the widget.
|
Add an image layer to the widget.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name (str): The name of the image layer.
|
name (str | None): The name of the image layer. If None, a default name is generated.
|
||||||
image (ImageItem): The image layer to add.
|
image (ImageItem): The image layer to add.
|
||||||
z_position (int | None): The z position of the image layer. If None, the layer is added to the top.
|
z_position (int | None): The z position of the image layer. If None, the layer is added to the top.
|
||||||
sync (ImageLayerSync | None): The synchronization settings for the image layer.
|
sync (ImageLayerSync | None): The synchronization settings for the image layer.
|
||||||
**kwargs: ImageLayerSync settings. Only used if sync is None.
|
**kwargs: ImageLayerSync settings. Only used if sync is None.
|
||||||
"""
|
"""
|
||||||
|
if name is None:
|
||||||
|
name = WidgetContainerUtils.generate_unique_name(
|
||||||
|
"image_layer", list(self.layers.keys())
|
||||||
|
)
|
||||||
if name in self.layers:
|
if name in self.layers:
|
||||||
raise ValueError(f"Layer with name '{name}' already exists.")
|
raise ValueError(f"Layer with name '{name}' already exists.")
|
||||||
if sync is None:
|
if sync is None:
|
||||||
|
@ -73,3 +73,8 @@ def test_image_layer_iteration(image_layer_manager):
|
|||||||
|
|
||||||
layer2 = image_layer_manager.add(name="Test Layer 2")
|
layer2 = image_layer_manager.add(name="Test Layer 2")
|
||||||
assert list(image_layer_manager) == [layer, layer2]
|
assert list(image_layer_manager) == [layer, layer2]
|
||||||
|
|
||||||
|
layer3 = image_layer_manager.add()
|
||||||
|
assert list(image_layer_manager) == [layer, layer2, layer3]
|
||||||
|
names = list(image_layer_manager.layers.keys())
|
||||||
|
assert names == ["Test Layer", "Test Layer 2", "image_layer_0"]
|
||||||
|
Reference in New Issue
Block a user