Massage base dragging

- Make dragging nodes not conflict with dragging the base
- Make shift-drag do a rubber-band select
- Fix cursors
- Fix nodes dragging weirdly on scene transition
- De-hack nodes automatically deselecting on expand start
This commit is contained in:
Audrey 2022-11-17 08:32:15 -07:00
parent 58ea517c18
commit 63b1abb130
1 changed files with 28 additions and 9 deletions

View File

@ -7,10 +7,10 @@ import random
from threading import Lock
import logging
from PySide6.QtCore import QRectF, QPointF, QTimeLine, QEasingCurve, QMarginsF, QTimer, Qt
from PySide6.QtCore import QRectF, QPointF, QTimeLine, QEasingCurve, QMarginsF, QTimer, Qt, QEvent
from PySide6.QtGui import QColor, QFont, QBrush, QPainterPath, QPen
from PySide6.QtWidgets import QGraphicsItem, QGraphicsRectItem, QGraphicsSimpleTextItem, QHBoxLayout, QGraphicsScene, \
QGraphicsEllipseItem
QGraphicsEllipseItem, QGraphicsSceneMouseEvent
import networkx
import pygraphviz
@ -120,6 +120,8 @@ class HierarchicalGraphWidget(QZoomableDraggableGraphicsView):
self._layout_animation.timeout.connect(self._iterate_layout)
self._layout_animation_controller.setSingleShot(True)
self._layout_animation_controller.timeout.connect(self._layout_animation.stop)
self.setDragMode(QZoomableDraggableGraphicsView.ScrollHandDrag)
self.viewport().setCursor(Qt.CursorShape.ArrowCursor)
self._on_change_group(src='__init__')
@ -572,17 +574,25 @@ class HierarchicalGraphWidget(QZoomableDraggableGraphicsView):
def mousePressEvent(self, event):
self._mouse_is_down = True
super().mousePressEvent(event)
if event.modifiers() & Qt.ShiftModifier == 0:
self.setDragMode(QZoomableDraggableGraphicsView.ScrollHandDrag)
else:
self.setDragMode(QZoomableDraggableGraphicsView.RubberBandDrag)
self.viewport().setCursor(Qt.CursorShape.CrossCursor)
super(QZoomableDraggableGraphicsView, self).mousePressEvent(event) # lol. lmao even
def mouseReleaseEvent(self, event):
self._mouse_is_down = False
if self.drop_target is not None:
self.confirm_drop()
super().mouseReleaseEvent(event)
self.viewport().setCursor(Qt.CursorShape.ArrowCursor)
# objectcontainer event handlers
def _on_change_group(self, focus=None, **kwargs):
self.scene().clearSelection()
old_group = self._old_group
self._old_group = self.current_group.am_obj
self._layouts[old_group] = {qnode.model: qnode.center for qnode in self.qnodes}
@ -597,8 +607,12 @@ class HierarchicalGraphWidget(QZoomableDraggableGraphicsView):
if focus is None:
focus = old_group
if focus in self.ug_reverse:
self.qnodes[self.ug_reverse[focus]].setSelected(True)
self.centerOn(self.qnodes[self.ug_reverse[focus]].center)
qnode = self.qnodes[self.ug_reverse[focus]]
# deliver a bogus event - prevents the selected node from dragging to the origin
event = QGraphicsSceneMouseEvent(QEvent.GraphicsSceneMouseRelease)
qnode.mouseReleaseEvent(event)
qnode.setSelected(True)
self.centerOn(qnode.center)
def _on_selection_changed(self, src=None, **kwargs):
if src == 'qt':
@ -710,6 +724,9 @@ class HGNode(AnimatableItem):
# qt overrides
def itemChange(self, change, value):
if change == QGraphicsItem.ItemPositionChange and self.exiting:
# refuse to move while exiting?
return self.pos()
if change == QGraphicsItem.ItemPositionHasChanged:
if not self.hgw._layout_event_occurring:
for edge in self.edges.values():
@ -874,9 +891,10 @@ class PropChartHG(HGNode, PropChart):
def begin_expand(self):
if self.timeline.state() == QTimeLine.NotRunning:
# horrifying
QTimer.singleShot(10, lambda: self.hgw.begin_expand(self.model))
self.hgw.begin_expand(self.model)
return True
else:
return False
def paint(self, painter, option, widget=...):
if self.isSelected():
@ -1132,8 +1150,9 @@ class PlusButton(QGraphicsItem):
def mousePressEvent(self, event):
if self.callback():
event.ignore()
super().mousePressEvent(event)
event.accept()
else:
super().mousePressEvent(event)
def hoverEnterEvent(self, event):
self.object_bg.setBrush(PLUS_HOVERED_COLOR)