Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
interactivity works, image -> lines -> lines with colors
  • Loading branch information
kushalkolar committed Dec 29, 2022
commit 118b3a851ea12ef196a0d70adfd0672921c13d3f
36 changes: 27 additions & 9 deletions fastplotlib/graphics/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass


class BaseGraphic:
def __init_subclass__(cls, **kwargs):
"""set the type of the graphic in lower case like "image", "line_collection", etc."""
Expand Down Expand Up @@ -94,9 +95,15 @@ def _reset_feature(self, feature: str):
def link(self, event_type: str, target: Any, feature: str, new_data: Any, callback_function: callable = None):
if event_type in self.pygfx_events:
self.world_object.add_event_handler(self.event_handler, event_type)

elif event_type in self.feature_events:
feature = getattr(self, event_type)
feature.add_event_handler(self.event_handler, event_type)
if isinstance(self, GraphicCollection):
feature_instance = getattr(self[:], event_type)
else:
feature_instance = getattr(self, event_type)

feature_instance.add_event_handler(self.event_handler)

else:
raise ValueError("event not possible")

Expand All @@ -116,9 +123,15 @@ def event_handler(self, event):
target_info.callback_function(source=self, target=target_info.target, event=event, new_data=target_info.new_data)
elif isinstance(self, GraphicCollection):
# if target is a GraphicCollection, then indices will be stored in collection_index
# indices = event.pick_info["collection_index"]
# target_info.target._set_feature(feature=target_info.feature, new_data=target_info.new_data, indices=indices)
print(event.pick_info)
if event.type in self.feature_events:
indices = event.pick_info["collection-index"]

# for now we only have line collections so this works
else:
for i, item in enumerate(self._items):
if item.world_object is event.pick_info["world_object"]:
indices = i
target_info.target._set_feature(feature=target_info.feature, new_data=target_info.new_data, indices=indices)
else:
# if target is a single graphic, then indices do not matter
target_info.target._set_feature(feature=target_info.feature, new_data=target_info.new_data,
Expand All @@ -136,15 +149,17 @@ class CallbackData:
@dataclass
class PreviouslyModifiedData:
"""Class for keeping track of previously modified data at indices"""
previous_data: Any
previous_indices: Any
data: Any
indices: Any


class GraphicCollection(BaseGraphic):
"""Graphic Collection base class"""

pygfx_events = [
"click"
]
"""Graphic Collection base class"""

def __init__(self, name: str = None):
self.name = name
self._items: List[Graphic] = list()
Expand Down Expand Up @@ -294,7 +309,6 @@ def __setitem__(self, key, value):
else:
for fi in self._feature_instances:
fi._set(value)
key = None

def add_event_handler(self, handler: callable):
for fi in self._feature_instances:
Expand All @@ -304,5 +318,9 @@ def remove_event_handler(self, handler: callable):
for fi in self._feature_instances:
fi.remove_event_handler(handler)

def block_events(self, b: bool):
for fi in self._feature_instances:
fi.block_events(b)

def __repr__(self):
return f"Collection feature for: <{self._feature}>"
8 changes: 8 additions & 0 deletions fastplotlib/graphics/features/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def __init__(self, parent, data: Any, collection_index: int = None):
self._collection_index = collection_index
self._event_handlers = list()

self._block_events = False

def block_events(self, b: bool):
self._block_events = b

@property
def feature_data(self):
"""graphic feature data managed by fastplotlib, do not modify directly"""
Expand Down Expand Up @@ -98,6 +103,9 @@ def _feature_changed(self, key: Union[int, slice, Tuple[slice]], new_data: Any):
pass

def _call_event_handlers(self, event_data: FeatureEvent):
if self._block_events:
return

for func in self._event_handlers:
try:
if len(getfullargspec(func).args) > 0:
Expand Down
2 changes: 1 addition & 1 deletion fastplotlib/graphics/features/_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _feature_changed(self, key, new_data):
"new_data": new_data,
}

event_data = FeatureEvent(type="color", pick_info=pick_info)
event_data = FeatureEvent(type="colors", pick_info=pick_info)

self._call_event_handlers(event_data)

Expand Down
13 changes: 7 additions & 6 deletions fastplotlib/graphics/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class LineGraphic(Graphic, Interaction):
"data",
"colors",
]

def __init__(
self,
data: Any,
Expand Down Expand Up @@ -102,15 +103,15 @@ def _set_feature(self, feature: str, new_data: Any, indices: Any = None):
previous = feature_instance._data.copy()
feature_instance._set(new_data)
if feature in self._previous_data.keys():
self._previous_data[feature].previous_data = previous
self._previous_data[feature].previous_indices = indices
self._previous_data[feature].data = previous
self._previous_data[feature].indices = indices
else:
self._previous_data[feature] = PreviouslyModifiedData(previous_data=previous, previous_indices=indices)
self._previous_data[feature] = PreviouslyModifiedData(data=previous, indices=indices)

def _reset_feature(self, feature: str):
prev_ixs = self._previous_data[feature].previous_indices
prev_ixs = self._previous_data[feature].indices
feature_instance = getattr(self, feature)
if prev_ixs is not None:
feature_instance[prev_ixs] = self._previous_data[feature].previous_data
feature_instance[prev_ixs] = self._previous_data[feature].data
else:
feature_instance._set(self._previous_data[feature].previous_data)
feature_instance._set(self._previous_data[feature].data)
18 changes: 11 additions & 7 deletions fastplotlib/graphics/linecollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from ._base import Interaction, PreviouslyModifiedData, GraphicCollection
from .line import LineGraphic
from ..utils import get_colors
from typing import *
from copy import deepcopy


Expand All @@ -16,6 +15,7 @@ class LineCollection(GraphicCollection, Interaction):
"data",
"colors",
]

def __init__(
self,
data: List[np.ndarray],
Expand Down Expand Up @@ -130,17 +130,21 @@ def _set_feature(self, feature: str, new_data: Any, indices: Any):
for fea in coll_feature._feature_instances:
data.append(fea._data)

previous = data[0].copy()
# later we can think about multi-index events
previous = deepcopy(data[0])
coll_feature._set(new_data)

if feature in self._previous_data.keys():
self._previous_data[feature].previous_data = previous
self._previous_data[feature].previous_indices = indices
self._previous_data[feature].data = previous
self._previous_data[feature].indices = indices
else:
self._previous_data[feature] = PreviouslyModifiedData(previous_data=previous, previous_indices=indices)
self._previous_data[feature] = PreviouslyModifiedData(data=previous, indices=indices)

def _reset_feature(self, feature: str):
# implemented for a single index at moment
prev_ixs = self._previous_data[feature].previous_indices
prev_ixs = self._previous_data[feature].indices
coll_feature = getattr(self[prev_ixs], feature)
coll_feature._set(self._previous_data[feature].previous_data)

coll_feature.block_events(True)
coll_feature._set(self._previous_data[feature].data)
coll_feature.block_events(False)