# -*- coding: utf-8 -*-
"""
A module containing `Graphics View` for NodeEditor
"""
from qtpy.QtWidgets import QGraphicsView, QApplication
from qtpy.QtCore import Signal, QPoint, Qt, QEvent, QPointF, QRectF
from qtpy.QtGui import QPainter, QDragEnterEvent, QDropEvent, QMouseEvent, QKeyEvent, QWheelEvent
from node_editor.node_graphics_socket import QDMGraphicsSocket
from node_editor.node_graphics_edge import QDMGraphicsEdge
from node_editor.node_edge_dragging import EdgeDragging
from node_editor.node_edge_rerouting import EdgeRerouting
from node_editor.node_edge_intersect import EdgeIntersect
from node_editor.node_edge_snapping import EdgeSnapping
from node_editor.node_graphics_cutline import QDMCutLine
from node_editor.utils import dumpException, pp
MODE_NOOP = 1 #: Mode representing ready state
MODE_EDGE_DRAG = 2 #: Mode representing when we drag edge state
MODE_EDGE_CUT = 3 #: Mode representing when we draw a cutting edge
MODE_EDGES_REROUTING = 4 #: Mode representing when we re-route existing edges
MODE_NODE_DRAG = 5 #: Mode representing when we drag a node to calculate dropping on intersecting edge
STATE_STRING = ['', 'Noop', 'Edge Drag', 'Edge Cut', 'Edge Rerouting', 'Node Drag']
#: Distance when click on socket to enable `Drag Edge`
EDGE_DRAG_START_THRESHOLD = 50
#: Enable UnrealEngine style rerouting
EDGE_REROUTING_UE = True
#: Socket snapping distance
EDGE_SNAPPING_RADIUS = 24
#: Enable socket snapping feature
EDGE_SNAPPING = True
DEBUG = False
DEBUG_MMB_SCENE_ITEMS = False
DEBUG_MMB_LAST_SELECTIONS = False
DEBUG_EDGE_INTERSECT = False
DEBUG_STATE = False
[docs]class QDMGraphicsView(QGraphicsView):
"""Class representing NodeEditor's `Graphics View`"""
#: pyqtSignal emitted when cursor position on the `Scene` has changed
scenePosChanged = Signal(int, int)
def __init__(self, grScene: 'QDMGraphicsScene', parent: 'QWidget'=None):
"""
:param grScene: reference to the :class:`~node_editor.node_graphics_scene.QDMGraphicsScene`
:type grScene: :class:`~nodeeditor.node_graphics_scene.QDMGraphicsScene`
:param parent: parent widget
:type parent: ``QWidget``
:Instance Attributes:
- **grScene** - reference to the :class:`~node_editor.node_graphics_scene.QDMGraphicsScene`
- **mode** - state of the `Graphics View`
- **zoomInFactor**- ``float`` - zoom step scaling, default 1.25
- **zoomClamp** - ``bool`` - do we clamp zooming or is it infinite?
- **zoom** - current zoom step
- **zoomStep** - ``int`` - the relative zoom step when zooming in/out
- **zoomRange** - ``[min, max]``
"""
super().__init__(parent)
self.grScene = grScene
self.initUI()
self.setScene(self.grScene)
self.mode = MODE_NOOP
self.editingFlag = False
self.rubberBandDraggingRectangle = False
# edge dragging
self.dragging = EdgeDragging(self)
# edges re-routing
self.rerouting = EdgeRerouting(self)
# drop a node on an existing edge
self.edgeIntersect = EdgeIntersect(self)
# edge snapping
self.snapping = EdgeSnapping(self, snapping_radius=EDGE_SNAPPING_RADIUS)
# cutline
self.cutline = QDMCutLine()
self.grScene.addItem(self.cutline)
self.last_scene_mouse_position = QPoint(0,0)
self.zoomInFactor = 1.25
self.zoomClamp = True
self.zoom = 10
self.zoomStep = 1
self.zoomRange = [0, 10]
# listeners
self._drag_enter_listeners = []
self._drop_listeners = []
[docs] def initUI(self):
"""Set up this ``QGraphicsView``"""
self.setRenderHints(QPainter.Antialiasing | QPainter.HighQualityAntialiasing | QPainter.TextAntialiasing | QPainter.SmoothPixmapTransform)
self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setDragMode(QGraphicsView.RubberBandDrag)
# enable dropping
self.setAcceptDrops(True)
[docs] def isSnappingEnabled(self, event: 'QInputEvent' = None) -> bool:
"""Returns ``True`` if snapping is currently enabled"""
return EDGE_SNAPPING and (event.modifiers() & Qt.CTRL) if event else True
[docs] def resetMode(self):
"""Helper function to re-set the grView's State Machine state to the default"""
self.mode = MODE_NOOP
[docs] def dragEnterEvent(self, event: QDragEnterEvent):
"""Trigger our registered `Drag Enter` events"""
for callback in self._drag_enter_listeners: callback(event)
[docs] def dropEvent(self, event: QDropEvent):
"""Trigger our registered `Drop` events"""
for callback in self._drop_listeners: callback(event)
[docs] def addDragEnterListener(self, callback: 'function'):
"""
Register callback for `Drag Enter` event
:param callback: callback function
"""
self._drag_enter_listeners.append(callback)
[docs] def addDropListener(self, callback: 'function'):
"""
Register callback for `Drop` event
:param callback: callback function
"""
self._drop_listeners.append(callback)
[docs] def mousePressEvent(self, event: QMouseEvent):
"""Dispatch Qt's mousePress event to corresponding function below"""
if event.button() == Qt.MiddleButton:
self.middleMouseButtonPress(event)
elif event.button() == Qt.LeftButton:
self.leftMouseButtonPress(event)
elif event.button() == Qt.RightButton:
self.rightMouseButtonPress(event)
else:
super().mousePressEvent(event)
[docs] def mouseReleaseEvent(self, event: QMouseEvent):
"""Dispatch Qt's mouseRelease event to corresponding function below"""
if event.button() == Qt.MiddleButton:
self.middleMouseButtonRelease(event)
elif event.button() == Qt.LeftButton:
self.leftMouseButtonRelease(event)
elif event.button() == Qt.RightButton:
self.rightMouseButtonRelease(event)
else:
super().mouseReleaseEvent(event)
[docs] def mouseMoveEvent(self, event: QMouseEvent):
"""Overriden Qt's ``mouseMoveEvent`` handling Scene/View logic"""
scenepos = self.mapToScene(event.pos())
try:
modified = self.setSocketHighlights(scenepos, highlighted=False, radius=EDGE_SNAPPING_RADIUS+100)
if self.isSnappingEnabled(event):
_, scenepos = self.snapping.getSnappedToSocketPosition(scenepos)
if modified: self.update()
if self.mode == MODE_EDGE_DRAG:
self.dragging.updateDestination(scenepos.x(), scenepos.y())
if self.mode == MODE_NODE_DRAG:
self.edgeIntersect.update(scenepos.x(), scenepos.y())
if self.mode == MODE_EDGES_REROUTING:
self.rerouting.updateScenePos(scenepos.x(), scenepos.y())
if self.mode == MODE_EDGE_CUT and self.cutline is not None:
self.cutline.line_points.append(scenepos)
self.cutline.update()
except Exception as e:
dumpException()
self.last_scene_mouse_position = scenepos
self.scenePosChanged.emit( int(scenepos.x()), int(scenepos.y()) )
super().mouseMoveEvent(event)
[docs] def keyPressEvent(self, event: QKeyEvent):
"""
.. note::
This overridden Qt's method was used for handling key shortcuts, before we implemented proper
``QWindow`` with Actions and Menu. Still the commented code serves as an example on how to handle
key presses without Qt's framework for Actions and shortcuts. There is also an example on
how to solve the problem when a Node contains Text/LineEdit and we press the `Delete`
key (also serving to delete `Node`)
:param event: Qt's Key event
:type event: ``QKeyEvent``
:return:
"""
# Use this code below if you wanna have shortcuts in this widget.
# You want to use this, when you don't have a window which handles these shortcuts for you
# if event.key() == Qt.Key_Delete:
# if not self.editingFlag:
# self.deleteSelected()
# else:
# super().keyPressEvent(event)
# elif event.key() == Qt.Key_S and event.modifiers() & Qt.ControlModifier:
# self.grScene.scene.saveToFile("graph.json")
# elif event.key() == Qt.Key_L and event.modifiers() & Qt.ControlModifier:
# self.grScene.scene.loadFromFile("graph.json")
# elif event.key() == Qt.Key_Z and event.modifiers() & Qt.ControlModifier and not event.modifiers() & Qt.ShiftModifier:
# self.grScene.scene.history.undo()
# elif event.key() == Qt.Key_Z and event.modifiers() & Qt.ControlModifier and event.modifiers() & Qt.ShiftModifier:
# self.grScene.scene.history.redo()
# elif event.key() == Qt.Key_H:
# print("HISTORY: len(%d)" % len(self.grScene.scene.history.history_stack),
# " -- current_step", self.grScene.scene.history.history_current_step)
# ix = 0
# for item in self.grScene.scene.history.history_stack:
# print("#", ix, "--", item['desc'])
# ix += 1
# else:
super().keyPressEvent(event)
[docs] def cutIntersectingEdges(self):
"""Compare which `Edges` intersect with current `Cut line` and delete them safely"""
for ix in range(len(self.cutline.line_points) - 1):
p1 = self.cutline.line_points[ix]
p2 = self.cutline.line_points[ix + 1]
# @TODO: we could collect all touched nodes, and notify them once after all edges removed
# we could cut 3 edges leading to a single node_editor this will notify it 3x
# maybe we could use some Notifier class with methods collect() and dispatch()
for edge in self.grScene.scene.edges.copy():
if edge.grEdge.intersectsWith(p1, p2):
edge.remove()
self.grScene.scene.history.storeHistory("Delete cutted edges", setModified=True)
[docs] def setSocketHighlights(self, scenepos: QPointF, highlighted: bool = True, radius: float = 50):
"""Set/disable socket highlights in Scene area defined by `scenepos` and `radius`"""
scanrect = QRectF(scenepos.x() - radius, scenepos.y() - radius, radius * 2, radius * 2)
items = self.grScene.items(scanrect)
items = list(filter(lambda x: isinstance(x, QDMGraphicsSocket), items))
for grSocket in items: grSocket.isHighlighted = highlighted
return items
[docs] def deleteSelected(self):
"""Shortcut for safe deleting every object selected in the `Scene`."""
for item in self.grScene.selectedItems():
if isinstance(item, QDMGraphicsEdge):
item.edge.remove()
elif hasattr(item, 'node'):
item.node.remove()
self.grScene.scene.history.storeHistory("Delete selected", setModified=True)
[docs] def debug_modifiers(self, event):
"""Helper function get string if we hold Ctrl, Shift or Alt modifier keys"""
out = "MODS: "
if event.modifiers() & Qt.ShiftModifier: out += "SHIFT "
if event.modifiers() & Qt.ControlModifier: out += "CTRL "
if event.modifiers() & Qt.AltModifier: out += "ALT "
return out
[docs] def getItemAtClick(self, event: QEvent) -> 'QGraphicsItem':
"""Return the object on which we've clicked/release mouse button
:param event: Qt's mouse or key event
:type event: ``QEvent``
:return: ``QGraphicsItem`` which the mouse event happened or ``None``
"""
pos = event.pos()
obj = self.itemAt(pos)
return obj
[docs] def distanceBetweenClickAndReleaseIsOff(self, event:QMouseEvent) -> bool:
""" Measures if we are too far from the last Mouse button click scene position.
This is used for detection if we release too far after we clicked on a `Socket`
:param event: Qt's mouse event
:type event: ``QMouseEvent``
:return: ``True`` if we released too far from where we clicked before
"""
new_lmb_release_scene_pos = self.mapToScene(event.pos())
dist_scene = new_lmb_release_scene_pos - self.last_lmb_click_scene_pos
edge_drag_threshold_sq = EDGE_DRAG_START_THRESHOLD*EDGE_DRAG_START_THRESHOLD
return (dist_scene.x()*dist_scene.x() + dist_scene.y()*dist_scene.y()) > edge_drag_threshold_sq
[docs] def wheelEvent(self, event: QWheelEvent):
"""overridden Qt's ``wheelEvent``. This handles zooming"""
# calculate our zoom Factor
zoomOutFactor = 1 / self.zoomInFactor
# calculate zoom
if event.angleDelta().y() > 0:
zoomFactor = self.zoomInFactor
self.zoom += self.zoomStep
else:
zoomFactor = zoomOutFactor
self.zoom -= self.zoomStep
clamped = False
if self.zoom < self.zoomRange[0]: self.zoom, clamped = self.zoomRange[0], True
if self.zoom > self.zoomRange[1]: self.zoom, clamped = self.zoomRange[1], True
# set scene scale
if not clamped or self.zoomClamp is False:
self.scale(zoomFactor, zoomFactor)