Skip to content

Commit

Permalink
Now newly added edges also snap to all underlying vertices!
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdwetering committed Aug 1, 2024
1 parent 6720156 commit e11734d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
18 changes: 17 additions & 1 deletion zxlive/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,23 @@ def undo(self) -> None:
self.update_graph_view()

def redo(self) -> None:
self.g.add_edge(((self.u, self.v)), self.ety)
self.g.add_edge((self.u, self.v), self.ety)
self.update_graph_view()

@dataclass
class AddEdges(BaseCommand):
"""Adds multiple edges of the same type to a graph."""
pairs: list[tuple[VT,VT]]
ety: EdgeType

def undo(self) -> None:
for u, v in self.pairs:
self.g.remove_edge((u, v, self.ety))
self.update_graph_view()

def redo(self) -> None:
for u, v in self.pairs:
self.g.add_edge((u, v), self.ety)
self.update_graph_view()


Expand Down
27 changes: 23 additions & 4 deletions zxlive/editor_base_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
from zxlive.sfx import SFXEnum

from .base_panel import BasePanel, ToolbarSection
from .commands import (AddEdge, AddNode, AddNodeSnapped, AddWNode, ChangeEdgeColor,
from .commands import (AddEdge, AddEdges, AddNode, AddNodeSnapped, AddWNode, ChangeEdgeColor,
ChangeNodeType, ChangePhase, MoveNode, SetGraph,
UpdateGraph)
from .common import VT, GraphT, ToolType, get_data
from .dialogs import show_error_msg
from .eitem import EItem, HAD_EDGE_BLUE, EItemAnimation
from .vitem import VItem, BLACK
from .graphscene import EditGraphScene
from .settings import display_setting
from .vitem import BLACK

from . import animations


Expand Down Expand Up @@ -179,15 +180,33 @@ def add_vert(self, x: float, y: float, edges: list[EItem]) -> None:
self.play_sound_signal.emit(SFXEnum.THATS_A_SPIDER)
self.undo_stack.push(cmd)

def add_edge(self, u: VT, v: VT) -> None:
def add_edge(self, u: VT, v: VT, verts: list[VItem]) -> None:
"""Add an edge between vertices u and v. `verts` is a list of VItems that collide with the edge.
"""
graph = self.graph_view.graph_scene.g
if vertex_is_w(graph.type(u)) and get_w_partner(graph, u) == v:
return None
if graph.type(u) == VertexType.W_INPUT and len(graph.neighbors(u)) >= 2 or \
graph.type(v) == VertexType.W_INPUT and len(graph.neighbors(v)) >= 2:
return None
cmd = AddEdge(self.graph_view, u, v, self._curr_ety)
if not self.snap_vertex_edge or not verts:
cmd = AddEdge(self.graph_view, u, v, self._curr_ety)
self.undo_stack.push(cmd)
return

ux, uy = graph.row(u), graph.qubit(u)
# Line was drawn from u to v, we want to order vs with the earlier items first.
def dist(vitem: VItem) -> float:
return (graph.row(vitem.v) - ux)**2 + (graph.row(vitem.v) - uy)**2
verts.sort(key=dist)
vs = [vitem.v for vitem in verts]
pairs = [(u, vs[0])]
for i in range(1, len(vs)):
pairs.append((vs[i-1],vs[i]))
pairs.append((vs[-1],v))
cmd = AddEdges(self.graph_view, pairs, self._curr_ety)
self.undo_stack.push(cmd)


def vert_moved(self, vs: list[tuple[VT, float, float]]) -> None:
self.undo_stack.push(MoveNode(self.graph_view, vs))
Expand Down
19 changes: 15 additions & 4 deletions zxlive/graphscene.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import Optional, Iterator, Iterable

from PySide6.QtCore import Qt, Signal, QRectF
from PySide6.QtGui import QBrush, QColor, QTransform
from PySide6.QtGui import QBrush, QColor, QTransform, QPainterPath
from PySide6.QtWidgets import QGraphicsScene, QGraphicsSceneMouseEvent, QGraphicsItem

from pyzx.graph.base import EdgeType
Expand Down Expand Up @@ -235,7 +235,7 @@ class EditGraphScene(GraphScene):
# Note that we have to set the argument types to `object`,
# otherwise it doesn't work for some reason...
vertex_added = Signal(object, object, object) # Actual types: float, float, list[EItem]
edge_added = Signal(object, object) # Actual types: VT, VT
edge_added = Signal(object, object, object) # Actual types: VT, VT, list[VItem]

# Currently selected edge type for preview when dragging
# to add a new edge
Expand Down Expand Up @@ -304,7 +304,18 @@ def add_vertex(self, e: QGraphicsSceneMouseEvent) -> None:
def add_edge(self, e: QGraphicsSceneMouseEvent) -> None:
assert self._drag is not None
self.removeItem(self._drag)
v1 = self._drag.start
self._drag = None
for it in self.items(e.scenePos(), deviceTransform=QTransform()):
if isinstance(it, VItem):
self.edge_added.emit(self._drag.start.v, it.v)
self._drag = None
v2 = it
#self.edge_added.emit(self._drag.start.v, it.v)
assert v2 is not None
path = QPainterPath(v1.pos())
path.lineTo(e.scenePos())
colliding_verts = []
for it in self.items(path, Qt.IntersectsItemShape, Qt.DescendingOrder, deviceTransform=QTransform()):
if isinstance(it, VItem) and it not in (v1,v2):
colliding_verts.append(it)
self.edge_added.emit(v1.v,v2.v,colliding_verts)

0 comments on commit e11734d

Please sign in to comment.