Skip to content

Commit

Permalink
Rename classes to be closer to development language
Browse files Browse the repository at this point in the history
  • Loading branch information
willGraham01 committed Jun 18, 2024
1 parent 5683696 commit 788af0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/napari_experimental/_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from qtpy.QtWidgets import QPushButton, QVBoxLayout, QWidget

from napari_experimental.names_only import (
LayerNamesTracker,
GroupLayer,
QtLayerNamesTreeModel,
QtLayerNamesTreeView,
)
Expand All @@ -29,7 +29,7 @@ def __init__(self, viewer: "napari.viewer.Viewer"):

self.viewer = viewer

self.layer_names = LayerNamesTracker(self.global_layers)
self.layer_names = GroupLayer(self.global_layers)
self.layer_names_model = QtLayerNamesTreeModel(
self.layer_names, parent=self
)
Expand Down
53 changes: 28 additions & 25 deletions src/napari_experimental/names_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def random_string(k: int = 5) -> str:
return "".join(random.choices(string.ascii_uppercase + string.digits, k=5))


class LayerDisplayNode(Node):
class NodeWrappingLayer(Node):

__default_name: str = "LayerDisplay"
__default_name: str = "Node[None]"

_tracking_layer: Layer

Expand All @@ -39,7 +39,10 @@ def layer(self, new_ptr: Layer) -> None:

@property
def name(self) -> str:
return self.layer.name
if self.is_tracking:
return self.layer.name
else:
return self.__default_name

def __init__(
self,
Expand All @@ -50,20 +53,20 @@ def __init__(
self.layer = layer_ptr

def __str__(self) -> str:
return f"LayerDisplayNode tracking {self.name}"
return f"Node[{self.name}]"

def __repr__(self) -> str:
return self.__str__()


class LayerNamesTracker(Group[LayerDisplayNode]):
class GroupLayer(Group[NodeWrappingLayer]):

def __init__(self, current_layers: Iterable[Layer] = ()):
Group.__init__(
self,
children=(LayerDisplayNode(layer) for layer in current_layers),
name=f"LayerNamesTracker-{random_string()}",
basetype=LayerDisplayNode,
children=(NodeWrappingLayer(layer) for layer in current_layers),
name=f"GroupLayer-{random_string()}",
basetype=NodeWrappingLayer,
)

def _check_already_tracking(self, layer_ptr: Layer) -> bool:
Expand All @@ -79,7 +82,7 @@ def _check_already_tracking(self, layer_ptr: Layer) -> bool:
"""
for tracked_layer in self:
if (
isinstance(tracked_layer, LayerDisplayNode)
isinstance(tracked_layer, NodeWrappingLayer)
and tracked_layer.layer is layer_ptr
):
return True
Expand All @@ -92,10 +95,10 @@ def add_new_item(
layer_ptr: Optional[Layer] = None,
) -> None:
"""
Insert a new LayerDisplayNode, or LayerNamesTracker, into the instance.
Insert a new NodeWrappingLayer, or GroupLayer, into the instance.
By default, it is assumed that a new LayerNamesTracker (Group)
is being added. Groups added in this way may themselves be empty.
By default, it is assumed that a new GroupLayer is being added.
Groups added in this way may themselves be empty.
To add a new Node, provide the layer_ptr argument.
Adding a new Node without providing the layer it should track is
Expand All @@ -107,10 +110,10 @@ def add_new_item(
insert_at = len(self)

if layer_ptr is None:
self.insert(insert_at, LayerNamesTracker())
self.insert(insert_at, GroupLayer())
elif isinstance(layer_ptr, Layer):
if not self._check_already_tracking(layer_ptr):
self.insert(insert_at, LayerDisplayNode(layer_ptr=layer_ptr))
self.insert(insert_at, NodeWrappingLayer(layer_ptr=layer_ptr))
else:
raise ValueError(
f"Already tracking {layer_ptr}, "
Expand All @@ -119,14 +122,14 @@ def add_new_item(

def remove_layer_item(self, layer_ptr: Layer, prune: bool = True) -> None:
"""
Removes the LayerDisplayNode item tracking (all instances of)
the given Layer from the tree model.
Removes (all instances of) NodeWrappingLayers tracking the given
Layer from the tree model.
If removing a layer would result in one of the branches of the
tree being barren, then the empty branch is also removed from
the model. This can be toggled with the prune argument.
If removing a layer would result in one of the Group being empty,
then the empty Group is also removed from the model.
This can be toggled with the `prune` argument.
Note that the IS keyword is used to determine equality between
Note that the `is` keyword is used to determine equality between
the layer provided and the layers that are tracked by the Nodes.
This ensures that we only remove references to layers we are
tracking from the model, rather than removing the Layer from
Expand All @@ -138,25 +141,25 @@ def remove_layer_item(self, layer_ptr: Layer, prune: bool = True) -> None:
layer in question will also be removed.
"""
for tracked_layer in self:
if isinstance(tracked_layer, LayerNamesTracker):
if isinstance(tracked_layer, GroupLayer):
tracked_layer.remove_layer_item(layer_ptr)
if prune and len(tracked_layer) == 0:
self.remove(tracked_layer)
elif tracked_layer.layer is layer_ptr:
self.remove(tracked_layer)


class QtLayerNamesTreeModel(QtNodeTreeModel[LayerNamesTracker]):
class QtLayerNamesTreeModel(QtNodeTreeModel[GroupLayer]):

def __init__(self, root: LayerNamesTracker, parent: QWidget = None):
def __init__(self, root: GroupLayer, parent: QWidget = None):
super().__init__(root, parent)
self.setRoot(root)


class QtLayerNamesTreeView(QtNodeTreeView):
_root: LayerNamesTracker
_root: GroupLayer
model_class = QtLayerNamesTreeModel

def __init__(self, root: LayerNamesTracker, parent: QWidget = None):
def __init__(self, root: GroupLayer, parent: QWidget = None):
super().__init__(root, parent)
self.setRoot(root)

0 comments on commit 788af0f

Please sign in to comment.