def setLine(self, line): """ Set the arrow base line (a `QLineF` in object coordinates). """ if self.__line != line: self.__line = line # local item coordinate system geom = self.geometry().translated(-self.pos()) if geom.isNull() and not line.isNull(): geom = QRectF(0, 0, 1, 1) arrow_shape = arrow_path_concave(line, self.lineWidth()) arrow_rect = arrow_shape.boundingRect() if not (geom.contains(arrow_rect)): geom = geom.united(arrow_rect) if self.__autoAdjustGeometry: # Shrink the geometry if required. geom = geom.intersected(arrow_rect) # topLeft can move changing the local coordinates. diff = geom.topLeft() line = QLineF(line.p1() - diff, line.p2() - diff) self.__arrowItem.setLine(line) self.__line = line # parent item coordinate system geom.translate(self.pos()) self.setGeometry(geom)
def updateSelectionRect(self, event): pos = event.scenePos() buttonDownPos = event.buttonDownScenePos(Qt.LeftButton) rect = QRectF(pos, buttonDownPos).normalized() rect = rect.intersected(self.sceneRect()) if not self.selectionRect: self.selectionRect = QGraphicsRectItem() self.selectionRect.setBrush(QColor(10, 10, 10, 20)) self.selectionRect.setPen(QPen(QColor(200, 200, 200, 200))) self.addItem(self.selectionRect) self.selectionRect.setRect(rect) if event.modifiers() & Qt.ControlModifier or \ event.modifiers() & Qt.ShiftModifier: path = self.selectionArea() else: path = QPainterPath() path.addRect(rect) self.setSelectionArea(path) self.selectionRectPointChanged.emit(pos)
def mouseReleaseEvent(self, event): # Reimplemented if event.button() == Qt.LeftButton: if self.__selectionRect is not None: self.__selectionRect.setParentItem(None) if self.scene() is not None: self.scene().removeItem(self.__selectionRect) self.__selectionRect = None event.accept() rect = QRectF(event.buttonDownPos(Qt.LeftButton), event.pos()).normalized() if not rect.isValid(): rect = rect.adjusted(-1e-7, -1e-7, 1e-7, 1e-7) rect = rect.intersected(self.contentsRect()) action = action = self.__selstate.action & ~SelectAction.Current self.__setSelectionRect(rect, action) self.__selstate = None
def mouseMoveEvent(self, event): # Reimplemented if event.buttons() & Qt.LeftButton: assert self.__selstate is not None if self.__selectionRect is None: self.__selectionRect = QtGui.QGraphicsRectItem(self) rect = QRectF(event.buttonDownPos(Qt.LeftButton), event.pos()).normalized() if not rect.width(): rect = rect.adjusted(-1e-7, -1e-7, 1e-7, 1e-7) rect = rect.intersected(self.contentsRect()) self.__selectionRect.setRect(rect) self.__selstate.rect = rect self.__selstate.action |= SelectAction.Current self.__setSelectionRect(rect, self.__selstate.action) event.accept()
def adjustGeometry(self): """ Adjust the widget geometry to exactly fit the arrow inside while preserving the arrow path scene geometry. """ # local system coordinate geom = self.geometry().translated(-self.pos()) line = self.__line arrow_rect = self.__arrowItem.shape().boundingRect() if geom.isNull() and not line.isNull(): geom = QRectF(0, 0, 1, 1) if not (geom.contains(arrow_rect)): geom = geom.united(arrow_rect) geom = geom.intersected(arrow_rect) diff = geom.topLeft() line = QLineF(line.p1() - diff, line.p2() - diff) geom.translate(self.pos()) self.setGeometry(geom) self.setLine(line)