Skip to content

Commit 44a0028

Browse files
authored
Merge pull request #924 from gaphor/custom-item-and-handle-finder
Custom item and handle finder
2 parents 4d0c362 + 27e2ef4 commit 44a0028

File tree

7 files changed

+279
-248
lines changed

7 files changed

+279
-248
lines changed

gaphas/handlemove.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
from functools import singledispatch
55
from operator import itemgetter
6-
from typing import TYPE_CHECKING, Iterable, Sequence
6+
from typing import TYPE_CHECKING, Iterable, Sequence, Iterator
77

88
from gaphas.connector import ConnectionSink, ConnectionSinkType, Connector
99
from gaphas.handle import Handle
@@ -163,7 +163,7 @@ def item_at_point(
163163
pos: Pos,
164164
distance: float = 0.5,
165165
exclude: Sequence[Item] = (),
166-
) -> Iterable[Item]:
166+
) -> Iterator[Item]:
167167
"""Return the topmost item located at ``pos`` (x, y).
168168
169169
Parameters:

gaphas/tool/hover.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
from gi.repository import Gdk, Gtk
22

3-
from gaphas.cursor import cursor
4-
from gaphas.tool.itemtool import find_item_and_handle_at_point
5-
3+
from typing import Callable, Union
64

7-
def hover_tool() -> Gtk.EventController:
5+
from gaphas.cursor import cursor
6+
from gaphas.tool.itemtool import default_find_item_and_handle_at_point
7+
from gaphas.connector import Handle
8+
from gaphas.item import Item
9+
from gaphas.types import Pos
10+
from gaphas.view import GtkView
11+
12+
13+
def hover_tool(
14+
find_item_and_handle_at_point: Callable[
15+
[GtkView, Pos], Union[tuple[Item, Union[Handle, None]], tuple[None, None]]
16+
] = default_find_item_and_handle_at_point,
17+
) -> Gtk.EventController:
818
"""Highlight the currently hovered item."""
919
ctrl = Gtk.EventControllerMotion.new()
10-
ctrl.connect("motion", on_motion)
20+
ctrl.connect("motion", on_motion, find_item_and_handle_at_point)
1121
ctrl.cursor_name = ""
1222
return ctrl
1323

1424

15-
def on_motion(ctrl, x, y):
25+
def on_motion(ctrl, x, y, find_item_and_handle_at_point):
1626
view = ctrl.get_widget()
1727
pos = (x, y)
1828
item, handle = find_item_and_handle_at_point(view, pos)

gaphas/tool/itemtool.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4+
from typing import Callable, Union
45

56
from gi.repository import Gdk, Gtk
67

@@ -17,11 +18,24 @@
1718
log = logging.getLogger(__name__)
1819

1920

20-
def item_tool() -> Gtk.GestureDrag:
21+
def default_find_item_and_handle_at_point(
22+
view: GtkView, pos: Pos
23+
) -> Union[tuple[Item, Union[Handle, None]], tuple[None, None]]:
24+
item, handle = handle_at_point(view, pos)
25+
return item or next(item_at_point(view, pos), None), handle # type: ignore[return-value]
26+
27+
28+
def item_tool(
29+
find_item_and_handle_at_point: Callable[
30+
[GtkView, Pos], tuple[Item, Handle | None] | tuple[None, None]
31+
] = default_find_item_and_handle_at_point,
32+
) -> Gtk.GestureDrag:
2133
"""Handle item movement and movement of handles."""
2234
gesture = Gtk.GestureDrag.new()
2335
drag_state = DragState()
24-
gesture.connect("drag-begin", on_drag_begin, drag_state)
36+
gesture.connect(
37+
"drag-begin", on_drag_begin, drag_state, find_item_and_handle_at_point
38+
)
2539
gesture.connect("drag-update", on_drag_update, drag_state)
2640
gesture.connect("drag-end", on_drag_end, drag_state)
2741
return gesture
@@ -44,7 +58,7 @@ def moving(self):
4458
yield self.moving_handle
4559

4660

47-
def on_drag_begin(gesture, start_x, start_y, drag_state):
61+
def on_drag_begin(gesture, start_x, start_y, drag_state, find_item_and_handle_at_point):
4862
view = gesture.get_widget()
4963
pos = (start_x, start_y)
5064
selection = view.selection
@@ -90,13 +104,6 @@ def on_drag_begin(gesture, start_x, start_y, drag_state):
90104
moving.start_move((start_x, start_y))
91105

92106

93-
def find_item_and_handle_at_point(
94-
view: GtkView, pos: Pos
95-
) -> tuple[Item | None, Handle | None]:
96-
item, handle = handle_at_point(view, pos)
97-
return item or next(item_at_point(view, pos), None), handle # type: ignore[call-overload]
98-
99-
100107
def moving_items(view):
101108
"""Filter the items that should eventually be moved.
102109

0 commit comments

Comments
 (0)