|
@@ -1,7 +1,7 @@
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
from typing import Callable
|
|
from typing import Callable
|
|
import traceback
|
|
import traceback
|
|
-from ..events import ImageClickEventArguments, handle_event
|
|
|
|
|
|
+from ..events import MouseEventArguments, handle_event
|
|
from .custom_view import CustomView
|
|
from .custom_view import CustomView
|
|
from .element import Element
|
|
from .element import Element
|
|
|
|
|
|
@@ -9,32 +9,34 @@ CustomView.use(__file__)
|
|
|
|
|
|
class AnnotationToolView(CustomView):
|
|
class AnnotationToolView(CustomView):
|
|
|
|
|
|
- def __init__(self, source: str, on_click: Callable):
|
|
|
|
- super().__init__('annotation_tool', source=source)
|
|
|
|
- self.allowed_events = ['onClick']
|
|
|
|
- self.initialize(onClick=on_click)
|
|
|
|
|
|
+ def __init__(self, source: str, on_mouse: Callable, events: list[str]):
|
|
|
|
+ super().__init__('annotation_tool', source=source, events=events)
|
|
|
|
+ self.allowed_events = ['onMouse']
|
|
|
|
+ self.initialize(onMouse=on_mouse)
|
|
|
|
|
|
class AnnotationTool(Element):
|
|
class AnnotationTool(Element):
|
|
|
|
|
|
- def __init__(self, source: str, on_click: Callable):
|
|
|
|
|
|
+ def __init__(self, source: str, on_mouse: Callable, events: list[str] = ['click']):
|
|
"""Annotation Tool
|
|
"""Annotation Tool
|
|
|
|
|
|
Create a special image that handles mouse clicks and yields image coordinates.
|
|
Create a special image that handles mouse clicks and yields image coordinates.
|
|
|
|
|
|
:param source: the source of the image; can be an url or a base64 string
|
|
:param source: the source of the image; can be an url or a base64 string
|
|
- :param on_click: callback for when the user clicks on the image (yields `image_x` and `image_y`)
|
|
|
|
|
|
+ :param on_mouse: callback for mouse events (yields `type`, `image_x` and `image_y`)
|
|
|
|
+ :param events: list of JavaScript events to subscribe to
|
|
"""
|
|
"""
|
|
- self.click_handler = on_click
|
|
|
|
- super().__init__(AnnotationToolView(source, self.handle_click))
|
|
|
|
|
|
+ self.mouse_handler = on_mouse
|
|
|
|
+ super().__init__(AnnotationToolView(source, self.handle_mouse, events))
|
|
|
|
|
|
- def handle_click(self, msg):
|
|
|
|
|
|
+ def handle_mouse(self, msg):
|
|
try:
|
|
try:
|
|
- arguments = ImageClickEventArguments(
|
|
|
|
|
|
+ arguments = MouseEventArguments(
|
|
sender=self,
|
|
sender=self,
|
|
socket=msg.get('websocket'),
|
|
socket=msg.get('websocket'),
|
|
|
|
+ type=msg.get('mouse_event_type'),
|
|
image_x=msg.get('image_x'),
|
|
image_x=msg.get('image_x'),
|
|
image_y=msg.get('image_y'),
|
|
image_y=msg.get('image_y'),
|
|
)
|
|
)
|
|
- handle_event(self.click_handler, arguments)
|
|
|
|
|
|
+ handle_event(self.mouse_handler, arguments)
|
|
except:
|
|
except:
|
|
traceback.print_exc()
|
|
traceback.print_exc()
|