Skip to content

Commit

Permalink
create a rectangle around mouse position to check for interesection w…
Browse files Browse the repository at this point in the history
…ith edges to snap vertex to edge
  • Loading branch information
RazinShaikh committed Jul 31, 2024
1 parent 3cafc15 commit ac2d41a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions zxlive/graphscene.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

from typing import Optional, Iterator, Iterable

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

from pyzx.graph.base import EdgeType
from pyzx.graph import GraphDiff

from .common import VT, ET, GraphT, ToolType, pos_from_view, OFFSET_X, OFFSET_Y
from .common import SCALE, VT, ET, GraphT, ToolType, pos_from_view, OFFSET_X, OFFSET_Y
from .vitem import VItem
from .eitem import EItem, EDragItem
from .settings import display_setting


class GraphScene(QGraphicsScene):
Expand Down Expand Up @@ -293,8 +294,11 @@ def mouseReleaseEvent(self, e: QGraphicsSceneMouseEvent) -> None:

def add_vertex(self, e: QGraphicsSceneMouseEvent) -> None:
p = e.scenePos()
# create a rectangle around the mouse position which will be used to check of edge intersections
snap = display_setting.SNAP_DIVISION
rect = QRectF(p.x() - SCALE/(2*snap), p.y() - SCALE/(2*snap), SCALE/snap, SCALE/snap)
# edges under current mouse position
edges: list[EItem] = [e for e in self.items(p, deviceTransform=QTransform()) if isinstance(e,EItem)]
edges: list[EItem] = [e for e in self.items(rect, deviceTransform=QTransform()) if isinstance(e,EItem)]
self.vertex_added.emit(*pos_from_view(p.x(), p.y()), edges)

def add_edge(self, e: QGraphicsSceneMouseEvent) -> None:
Expand Down

0 comments on commit ac2d41a

Please sign in to comment.