Skip to content

Commit

Permalink
Fix edit stream and make popup more robust (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 authored Jun 28, 2024
1 parent f9deb64 commit 0301194
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
1 change: 1 addition & 0 deletions holonote/annotate/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ def overlay(self, indicators=True, editor=True) -> hv.Overlay:
active_tools += ["box_select"]
elif self.region_format == "point-point":
active_tools += ["tap"]

layers.append(self._element.opts(tools=self.edit_tools, active_tools=active_tools))

if indicators:
Expand Down
68 changes: 43 additions & 25 deletions holonote/app/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,51 +224,67 @@ def _callback_apply(self, event):
elif self._widget_mode_group.value == "-" and selected_ind is not None:
self.annotator.delete_annotation(selected_ind)

def _get_layout(self, name):
def _add_layout(self, name):
"""
Add a layout to the panel, by cloning the root layout, linking the close button,
and returning it visibly.
"""

def close_layout(event):
layout.visible = False

layout = self._layouts.get(name)
if not layout:
layout = self._layout.clone(visible=False)
layout = self._layout.clone(visible=True)
self._widget_apply_button.on_click(close_layout)
self._layouts[name] = layout
return layout

def _hide_layouts(self):
for layout in self._layouts.values():
layout.visible = False
def _hide_layouts_except(self, desired_name) -> pn.Column:
"""
Prevents multiple layouts from being visible at the same time.
"""
desired_layout = None
for name, layout in self._layouts.items():
if name == desired_name:
layout.visible = True
desired_layout = layout
elif name != "__panel__":
layout.visible = False

# If the desired layout is not found, create it
if desired_name is not None and desired_layout is None:
desired_layout = self._add_layout(desired_name)
return desired_layout

def _register_stream_popup(self, stream):
def _popup(*args, **kwargs):
layout = self._get_layout(stream.name)
with param.parameterized.batch_call_watchers(self):
self._hide_layouts()
self._widget_mode_group.value = "+"
layout.visible = True
return layout
# If the annotation widgets are laid out on the side in a Column/Row/etc,
# while as_popup=True, do not show the popup during subtract or edit mode
widgets_on_side = any(name == "__panel__" for name in self._layouts)
if widgets_on_side and self._widget_mode_group.value in ("-", "✏"):
return
self._widget_mode_group.value = "+"
return self._hide_layouts_except(stream.name)

stream.popup = _popup

def _register_tap_popup(self, display):
def tap_popup(x, y) -> None: # Tap tool must be enabled on the element
layout = self._get_layout("tap")
if self.annotator.selection_enabled:
with param.parameterized.batch_call_watchers(self):
self._hide_layouts()
layout.visible = True
return layout
if self.annotator.selected_indices:
return self._hide_layouts_except("tap")

display._tap_stream.popup = tap_popup

def _register_double_tap_clear(self, display):
def double_tap_toggle(x, y):
layout = self._get_layout("doubletap")
if layout.visible:
with param.parameterized.batch_call_watchers(self):
self._hide_layouts()
layout.visible = True
return layout
# Toggle the visibility of the doubletap layout
if any(layout.visible for layout in self._layouts.values()):
# Clear all open layouts
self._hide_layouts_except(None)
else:
# Open specifically the doubletap layout
return self._hide_layouts_except("doubletap")

try:
tools = display._element.opts["tools"]
Expand All @@ -284,7 +300,6 @@ def _watcher_selected_indices(self, event):
if len(event.new) != 1:
return
selected_index = event.new[0]
# if self._widget_mode_group.value == '✏':
for name, widget in self._fields_widgets.items():
value = self.annotator.annotation_table._field_df.loc[selected_index][name]
widget.value = value
Expand All @@ -293,6 +308,7 @@ def _watcher_mode_group(self, event):
with param.parameterized.batch_call_watchers(self):
if event.new in ("-", "✏"):
self.annotator.selection_enabled = True
self.annotator.editable_enabled = False
elif event.new == "+":
self.annotator.editable_enabled = True
self.annotator.selection_enabled = False
Expand All @@ -308,4 +324,6 @@ def _set_standard_callbacks(self):
self._widget_mode_group.param.watch(self._watcher_mode_group, "value")

def __panel__(self):
return self._layout.clone(visible=True)
layout = self._layout.clone(visible=True)
self._layouts["__panel__"] = layout
return layout

0 comments on commit 0301194

Please sign in to comment.