Add more types to tools
This commit is contained in:
parent
d12ee26c2b
commit
3230a82fc5
@ -517,7 +517,7 @@ Tool for placing items on the Canvas.
|
||||
|
||||
```python
|
||||
def on_clicked(button):
|
||||
view.tool.grab(PlacementTool(view, factory(view, MyLine), HandleTool(), 1))
|
||||
view.tool.grab(PlacementTool(view, factory(view, MyLine), HandleTool(view), 1))
|
||||
```
|
||||
|
||||
#### Class: `gaphas.aspects.ItemFinder`
|
||||
|
@ -339,7 +339,7 @@ Tool for placing items on the Canvas.
|
||||
.. code-block: python
|
||||
|
||||
def on_clicked(button):
|
||||
view.tool.grab(PlacementTool(view, factory(view, MyLine), HandleTool, 1))
|
||||
view.tool.grab(PlacementTool(view, factory(view, MyLine), HandleTool(view), 1))
|
||||
|
||||
Class: ``gaphas.aspects.ItemFinder``
|
||||
------
|
||||
|
@ -142,7 +142,7 @@ def create_window(canvas, title, zoom=1.0): # noqa too complex
|
||||
|
||||
def on_add_box_clicked(button, view):
|
||||
# view.window.set_cursor(Gdk.Cursor.new(Gdk.CursorType.CROSSHAIR))
|
||||
view.tool.grab(PlacementTool(view, factory(view, MyBox), HandleTool, 2))
|
||||
view.tool.grab(PlacementTool(view, factory(view, MyBox), HandleTool(view), 2))
|
||||
|
||||
b.connect("clicked", on_add_box_clicked, view)
|
||||
v.add(b)
|
||||
@ -150,7 +150,7 @@ def create_window(canvas, title, zoom=1.0): # noqa too complex
|
||||
b = Gtk.Button.new_with_label("Add line")
|
||||
|
||||
def on_add_line_clicked(button):
|
||||
view.tool.grab(PlacementTool(view, factory(view, MyLine), HandleTool, 1))
|
||||
view.tool.grab(PlacementTool(view, factory(view, MyLine), HandleTool(view), 1))
|
||||
|
||||
b.connect("clicked", on_add_line_clicked)
|
||||
v.add(b)
|
||||
|
@ -1,15 +1,34 @@
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from gi.repository import Gdk
|
||||
from typing_extensions import Protocol
|
||||
|
||||
from gaphas.aspect import Connector, HandleFinder, HandleInMotion, HandleSelection
|
||||
from gaphas.connector import Handle
|
||||
from gaphas.item import Item
|
||||
from gaphas.tool.tool import Tool
|
||||
from gaphas.view import GtkView
|
||||
|
||||
Pos = Tuple[float, float]
|
||||
|
||||
|
||||
class HandleInMotionType(Protocol):
|
||||
def __init__(self, item: Item, handle: Handle, view: GtkView):
|
||||
...
|
||||
|
||||
def start_move(self, pos: Pos):
|
||||
...
|
||||
|
||||
def move(self, pos: Pos):
|
||||
...
|
||||
|
||||
def stop_move(self):
|
||||
...
|
||||
|
||||
def glue(self, pos: Pos, distance: float = 0):
|
||||
...
|
||||
|
||||
|
||||
class HandleTool(Tool):
|
||||
"""Tool for moving handles around.
|
||||
|
||||
@ -21,7 +40,7 @@ class HandleTool(Tool):
|
||||
super().__init__(view)
|
||||
self.grabbed_handle: Optional[Handle] = None
|
||||
self.grabbed_item: Optional[Item] = None
|
||||
self.motion_handle = None
|
||||
self.motion_handle: Optional[HandleInMotionType] = None
|
||||
|
||||
def grab_handle(self, item: Item, handle: Handle):
|
||||
"""Grab a specific handle.
|
||||
|
@ -1,7 +1,28 @@
|
||||
from typing import Set, Tuple
|
||||
|
||||
from gi.repository import Gdk
|
||||
from typing_extensions import Protocol
|
||||
|
||||
from gaphas.aspect import InMotion, Selection
|
||||
from gaphas.item import Item
|
||||
from gaphas.tool.tool import Tool
|
||||
from gaphas.view import GtkView
|
||||
|
||||
Pos = Tuple[float, float]
|
||||
|
||||
|
||||
class InMotionType(Protocol):
|
||||
def __init__(self, item: Item, view: GtkView):
|
||||
...
|
||||
|
||||
def start_move(self, pos: Pos):
|
||||
...
|
||||
|
||||
def move(self, pos: Pos):
|
||||
...
|
||||
|
||||
def stop_move(self):
|
||||
...
|
||||
|
||||
|
||||
class ItemTool(Tool):
|
||||
@ -16,7 +37,7 @@ class ItemTool(Tool):
|
||||
def __init__(self, view, buttons=(1,)):
|
||||
super().__init__(view)
|
||||
self._buttons = buttons
|
||||
self._movable_items = set()
|
||||
self._movable_items: Set[InMotionType] = set()
|
||||
|
||||
def get_item(self):
|
||||
return self.view.selection.hovered_item
|
||||
|
@ -1,16 +1,35 @@
|
||||
from typing import Callable
|
||||
|
||||
from typing_extensions import Protocol
|
||||
|
||||
from gaphas.connector import Handle
|
||||
from gaphas.item import Item
|
||||
from gaphas.tool.tool import Tool
|
||||
from gaphas.view import GtkView
|
||||
|
||||
FactoryType = Callable[..., Item] # type: ignore[misc]
|
||||
|
||||
|
||||
class HandleToolType(Protocol):
|
||||
def grab_handle(self, new_item: Item, handle: Handle):
|
||||
...
|
||||
|
||||
|
||||
class PlacementTool(Tool):
|
||||
def __init__(self, view, factory, handle_tool, handle_index):
|
||||
def __init__(
|
||||
self,
|
||||
view: GtkView,
|
||||
factory: FactoryType,
|
||||
handle_tool: HandleToolType,
|
||||
handle_index: int,
|
||||
):
|
||||
super().__init__(view)
|
||||
self._factory = factory
|
||||
self.handle_tool = handle_tool(view)
|
||||
self.handle_tool = handle_tool
|
||||
self._handle_index = handle_index
|
||||
self._new_item = None
|
||||
self.grabbed_handle = None
|
||||
|
||||
# handle_tool = property(lambda s: s._handle_tool, doc="Handle tool")
|
||||
handle_index = property(
|
||||
lambda s: s._handle_index, doc="Index of handle to be used by handle_tool"
|
||||
)
|
||||
|
@ -50,7 +50,7 @@ class Tool:
|
||||
def __init__(self, view: GtkView):
|
||||
self.view = view
|
||||
|
||||
def _dispatch(self, event):
|
||||
def _dispatch(self, event: Gdk.Event):
|
||||
"""Deal with the event.
|
||||
|
||||
The event is dispatched to a specific handler for the event
|
||||
@ -66,7 +66,7 @@ class Tool:
|
||||
return bool(h(event))
|
||||
return False
|
||||
|
||||
def handle(self, event):
|
||||
def handle(self, event: Gdk.Event):
|
||||
return self._dispatch(event)
|
||||
|
||||
def draw(self, context):
|
||||
|
Loading…
x
Reference in New Issue
Block a user