123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- from PyQt5.QtWidgets import QGraphicsItemGroup, QGraphicsRectItem, QGraphicsEllipseItem
- from PyQt5.QtCore import QRect
- from graphics_sketch_line import SketchedLineItem
- class SketchGroup(QGraphicsItemGroup):
- def __init__(self):
- QGraphicsItemGroup.__init__(self)
- self._prev_pos = self.scenePos() # empty initially (why?)
- def get_item_coord_relative(self, item):
- """
- Returns the item coordinate description, relative to the group.
- Depending on the type of item, the return type will differ (rectangle for rectangle and ellipse, two points
- for line)
- """
- if item not in self.childItems():
- return None
- sketch_brect = self.sceneBoundingRect()
- if isinstance(item, QGraphicsRectItem) or isinstance(item, QGraphicsEllipseItem):
- item_brect = item.sceneBoundingRect()
- top_left = (item_brect.topLeft() - sketch_brect.topLeft()).toPoint()
- return QRect(top_left.x(), top_left.y(), int(item_brect.width()), int(item_brect.height()))
- elif isinstance(item, SketchedLineItem):
- return (item.get_p1() - sketch_brect.topLeft()).toPoint(), (item.get_p2() - sketch_brect.topLeft()).toPoint()
- else:
- return None
- def mousePressEvent(self, event):
- self._prev_pos = self.scenePos()
- #print("Mouse pressed on group, coord {}".format(self._prev_pos))
- QGraphicsItemGroup.mousePressEvent(self, event)
- def mouseReleaseEvent(self, event):
- cur_pos = self.scenePos()
- #print("Mouse released on group, coord {}".format(cur_pos))
- diff_point = cur_pos - self._prev_pos
- #print("Diff is {}".format(diff_point))
- # update line coordinates since we moved the group
- for item in self.childItems():
- if isinstance(item, SketchedLineItem):
- #print("Updating coordinates of line")
- new_p1 = item.get_p1() + diff_point
- new_p2 = item.get_p2() + diff_point
- item.set_p1(new_p1)
- item.set_p2(new_p2)
- QGraphicsItemGroup.mouseReleaseEvent(self, event)
|