Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78f1d2a

Browse files
authored
add 'Deleted' as a graphic feature (#404)
1 parent cf4a04b commit 78f1d2a

File tree

7 files changed

+60
-7
lines changed

7 files changed

+60
-7
lines changed

fastplotlib/graphics/_base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from pygfx import WorldObject
1010

11-
from ._features import GraphicFeature, PresentFeature, GraphicFeatureIndexable
11+
from ._features import GraphicFeature, PresentFeature, GraphicFeatureIndexable, Deleted
1212

1313
# dict that holds all world objects for a given python kernel/session
1414
# Graphic objects only use proxies to WorldObjects
@@ -45,6 +45,12 @@ def __init_subclass__(cls, **kwargs):
4545

4646

4747
class Graphic(BaseGraphic):
48+
feature_events = {}
49+
50+
def __init_subclass__(cls, **kwargs):
51+
# all graphics give off a feature event when deleted
52+
cls.feature_events = {*cls.feature_events, "deleted"}
53+
4854
def __init__(
4955
self,
5056
name: str = None,
@@ -72,6 +78,8 @@ def __init__(
7278
# store hex id str of Graphic instance mem location
7379
self.loc: str = hex(id(self))
7480

81+
self.deleted = Deleted(self, False)
82+
7583
@property
7684
def world_object(self) -> WorldObject:
7785
"""Associated pygfx WorldObject. Always returns a proxy, real object cannot be accessed directly."""
@@ -168,6 +176,7 @@ def _cleanup(self):
168176
pass
169177

170178
def __del__(self):
179+
self.deleted = True
171180
del WORLD_OBJECTS[self.loc]
172181

173182

fastplotlib/graphics/_features/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ._thickness import ThicknessFeature
66
from ._base import GraphicFeature, GraphicFeatureIndexable, FeatureEvent, to_gpu_supported_dtype
77
from ._selection_features import LinearSelectionFeature, LinearRegionSelectionFeature
8+
from ._deleted import Deleted
89

910
__all__ = [
1011
"ColorFeature",
@@ -23,4 +24,5 @@
2324
"to_gpu_supported_dtype",
2425
"LinearSelectionFeature",
2526
"LinearRegionSelectionFeature",
27+
"Deleted",
2628
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from ._base import GraphicFeature, FeatureEvent
2+
3+
4+
class Deleted(GraphicFeature):
5+
"""
6+
Used when a graphic is deleted, triggers events that can be useful to indicate this graphic has been deleted
7+
8+
**event pick info:**
9+
10+
==================== ======================== =========================================================================
11+
key type description
12+
==================== ======================== =========================================================================
13+
"collection-index" int the index of the graphic within the collection that triggered the event
14+
"world_object" pygfx.WorldObject world object
15+
==================== ======================== =========================================================================
16+
"""
17+
18+
def __init__(self, parent, value: bool):
19+
super(Deleted, self).__init__(parent, value)
20+
21+
def _set(self, value: bool):
22+
value = self._parse_set_value(value)
23+
self._feature_changed(key=None, new_data=value)
24+
25+
def _feature_changed(self, key, new_data):
26+
# this is a non-indexable feature so key=None
27+
28+
pick_info = {
29+
"index": None,
30+
"collection-index": self._collection_index,
31+
"world_object": self._parent.world_object,
32+
"new_data": new_data,
33+
}
34+
35+
event_data = FeatureEvent(type="deleted", pick_info=pick_info)
36+
37+
self._call_event_handlers(event_data)
38+
39+
def __repr__(self) -> str:
40+
s = f"DeletedFeature for {self._parent}"
41+
return s

fastplotlib/graphics/image.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def _add_plot_area_hook(self, plot_area):
196196

197197

198198
class ImageGraphic(Graphic, Interaction, _AddSelectorsMixin):
199-
feature_events = ("data", "cmap", "present")
199+
feature_events = {"data", "cmap", "present"}
200200

201201
def __init__(
202202
self,
@@ -345,10 +345,11 @@ def col_chunk_index(self, index: int):
345345

346346

347347
class HeatmapGraphic(Graphic, Interaction, _AddSelectorsMixin):
348-
feature_events = (
348+
feature_events = {
349349
"data",
350350
"cmap",
351-
)
351+
"present"
352+
}
352353

353354
def __init__(
354355
self,

fastplotlib/graphics/line.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
class LineGraphic(Graphic, Interaction):
15-
feature_events = ("data", "colors", "cmap", "thickness", "present")
15+
feature_events = {"data", "colors", "cmap", "thickness", "present"}
1616

1717
def __init__(
1818
self,

fastplotlib/graphics/line_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class LineCollection(GraphicCollection, Interaction):
1717
child_type = LineGraphic.__name__
18-
feature_events = ("data", "colors", "cmap", "thickness", "present")
18+
feature_events = {"data", "colors", "cmap", "thickness", "present"}
1919

2020
def __init__(
2121
self,

fastplotlib/graphics/scatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
class ScatterGraphic(Graphic):
12-
feature_events = ("data", "sizes", "colors", "cmap", "present")
12+
feature_events = {"data", "sizes", "colors", "cmap", "present"}
1313

1414
def __init__(
1515
self,

0 commit comments

Comments
 (0)