Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs relating to empty GroupLayers #38

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ In order to preserve the user's intuitive understanding of "layers higher up in

### Known bugs and issues (breaking)

- Seg-fault when adding an empty group to another empty group <https://github.com/brainglobe/napari-experimental/issues/12>

### Development Tasks

- Create a standalone docs site that expands on the implementation details section.
Expand Down
35 changes: 35 additions & 0 deletions src/napari_experimental/group_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class GroupLayer(Group[GroupLayerNode], GroupLayerNode):
are any such Nodes).
"""

__next_uid: int = -1
_uid: int

@property
def name(self) -> str:
"""
Expand All @@ -98,10 +101,33 @@ def visible(self, value: bool) -> None:
item.layer.visible = value
self._visible = value

@property
def uid(self) -> int:
"""
Unique ID of this instance of GroupLayer.
Assigned on instantiation and cannot be overwritten.
"""
return self._uid

def __eq__(self, other: GroupLayer) -> bool:
"""
GroupLayers are only equal if we are pointing to the same object.
"""
return isinstance(other, GroupLayer) and self.uid == other.uid

def __hash__(self) -> int:
"""
Since GroupLayers are assigned a unique ID on creation, we can use
this value as the hash of a particular instance.
"""
return self._uid

def __init__(
self,
*items_to_include: Layer | GroupLayerNode | GroupLayer,
):
# Assign me a unique uid
self._uid = GroupLayer._next_uid()
# Python seems to understand that since GroupLayerNode inherits from
# Node, and Group also inherits from Node, that GroupLayerNode
# "wins".
Expand Down Expand Up @@ -137,6 +163,15 @@ def __init__(
# Default to group being visible
self._visible = True

@classmethod
def _next_uid(cls) -> int:
"""
Return the next free unique ID that can be assigned to an instance of
this class, then increment the counter to the next available index.
"""
cls.__next_uid += 1
return cls.__next_uid

@staticmethod
def _revise_indices_based_on_previous_moves(
original_index: NestedIndex,
Expand Down