-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_edge_snapping.py
More file actions
56 lines (44 loc) · 2.22 KB
/
Copy pathnode_edge_snapping.py
File metadata and controls
56 lines (44 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""
A module containing the Edge Snapping functions which are used in :class:`~nodeeditor.node_graphics_view.QDMGraphicsView` class.
"""
from qtpy.QtCore import QPointF, QRectF
from nodeeditor.node_graphics_socket import QDMGraphicsSocket
class EdgeSnapping():
def __init__(self, grView: 'QGraphicsView', snapping_radius: float = 24):
self.grView = grView
self.grScene = self.grView.grScene
self.edge_snapping_radius = snapping_radius
def getSnappedSocketItem(self, event: 'QMouseEvent') -> 'QDMGraphicsSocket':
"""Returns :class:`~nodeeditor.node_graphics_socket.QDMGraphicsSocket` which we should snap to"""
scenepos = self.grView.mapToScene(event.pos())
grSocket, pos = self.getSnappedToSocketPosition(scenepos)
return grSocket
def getSnappedToSocketPosition(self, scenepos: QPointF) -> ('QDMGraphicsSocket', QPointF):
"""
Returns grSocket and Scene position to nearest Socket or original position if no nearby Socket found
:param scenepos: From which point should I snap?
:type scenepos: ``QPointF``
:return: grSocket and Scene postion to nearest socket
"""
scanrect = QRectF(
scenepos.x() - self.edge_snapping_radius, scenepos.y() - self.edge_snapping_radius,
self.edge_snapping_radius * 2, self.edge_snapping_radius * 2
)
items = self.grScene.items(scanrect)
items = list(filter(lambda x: isinstance(x, QDMGraphicsSocket), items))
if len(items) == 0:
return None, scenepos
selected_item = items[0]
if len(items) > 1:
# calculate the nearest socket
nearest = 10000000000
for grsock in items:
grsock_scenepos = grsock.socket.node.getSocketScenePosition(grsock.socket)
qpdist = QPointF(*grsock_scenepos) - scenepos
dist = qpdist.x() * qpdist.x() + qpdist.y() * qpdist.y()
if dist < nearest:
nearest, selected_item = dist, grsock
selected_item.isHighlighted = True
calcpos = selected_item.socket.node.getSocketScenePosition(selected_item.socket)
return selected_item, QPointF(*calcpos)