From a6814bfd4c6a9eb228a8939d1dee0dd0b5d16786 Mon Sep 17 00:00:00 2001 From: brenda-br Date: Tue, 21 Feb 2023 14:07:35 +0530 Subject: Fix #33 Restrict Movable area of Node Items --- src/main/python/shapes/shapes.py | 17 ++++++++++++++++- src/main/python/utils/graphics.py | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/python/shapes/shapes.py b/src/main/python/shapes/shapes.py index f579b1e..e0d6617 100644 --- a/src/main/python/shapes/shapes.py +++ b/src/main/python/shapes/shapes.py @@ -625,6 +625,7 @@ class NodeItem(QGraphicsSvgItem): def itemChange(self, change, value): """Overloads and extends QGraphicsSvgItem to also update grip items """ + newPos = value # check if item selected is changed if change == QGraphicsItem.ItemSelectedHasChanged: # show grips if selected @@ -642,7 +643,21 @@ class NodeItem(QGraphicsSvgItem): # update grips self.updateLineGripItem() self.updateSizeGripItem() - return + + #restrict movable area of Node Item + + if self.parent() is not None: + rect = self.parent().sceneRect() + width = self.boundingRect().width() + height = self.boundingRect().height() + eWH1 = QPointF(newPos.x()+width,newPos.y()+height) + eWH2 = QPointF(newPos.x()-width,newPos.y()-height) + if not rect.__contains__(eWH1) or not rect.__contains__(eWH2) : + newPos.setX(min(rect.right()-width+40, max(newPos.x(), rect.left()+90))) + newPos.setY(min(rect.bottom()-height, max(newPos.y(), rect.top()+70))) + self.setPos(newPos) + return super(NodeItem,self).itemChange(change, newPos) + # check if item is add on scene if change == QGraphicsItem.ItemSceneHasChanged and self.scene(): # add grips and update them diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index c4e1843..1b797d5 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -44,6 +44,7 @@ class CustomView(QGraphicsView): graphic = getattr(shapes, obj[0])(*map(lambda x: int(x) if x.isdigit() else x, obj[1:])) graphic.setPos(QDropEvent.pos().x(), QDropEvent.pos().y()) self.scene().addItemPlus(graphic) + graphic.setParent(self) QDropEvent.acceptProposedAction() def wheelEvent(self, QWheelEvent): -- cgit