def mousePressEvent(self, event): if self._newRect is not None: self._newRect = None self._point0 = self.pos() parent = self.parentItem() scene = self.scene() # following line prevents dragging along the previously selected # item when resizing another one scene.clearSelection() rect = parent.boundingRect() self._x = rect.x() self._y = rect.y() self._w = rect.width() self._h = rect.height() self._ratio = self._w / self._h if qt.qVersion() < "5.0": self._newRect = qt.QGraphicsRectItem(parent, scene) else: self._newRect = qt.QGraphicsRectItem(parent) self._newRect.setRect(qt.QRectF(self._x, self._y, self._w, self._h)) qt.QGraphicsRectItem.mousePressEvent(self, event)
def _updatePrinter(self): """Resize :attr:`page`, :attr:`scene` and :attr:`view` to :attr:`printer` width and height.""" printer = self.printer assert printer is not None, \ "_updatePrinter should not be called unless a printer is defined" if self.scene is None: self.scene = qt.QGraphicsScene() self.scene.setBackgroundBrush(qt.QColor(qt.Qt.lightGray)) self.scene.setSceneRect( qt.QRectF(0, 0, printer.width(), printer.height())) if self.page is None: self.page = qt.QGraphicsRectItem(0, 0, printer.width(), printer.height()) self.page.setBrush(qt.QColor(qt.Qt.white)) self.scene.addItem(self.page) self.scene.setSceneRect( qt.QRectF(0, 0, printer.width(), printer.height())) self.page.setPos(qt.QPointF(0.0, 0.0)) self.page.setRect(qt.QRectF(0, 0, printer.width(), printer.height())) if self.view is None: self.view = qt.QGraphicsView(self.scene) self.mainLayout.addWidget(self.view) self._buildStatusBar() # self.view.scale(1./self._viewScale, 1./self._viewScale) self.view.fitInView(self.page.rect(), qt.Qt.KeepAspectRatio) self._viewScale = 1.00 self._updateTargetLabel()
def addPixmap(self, pixmap, title=None, comment=None, commentPosition=None): """Add a pixmap to the print preview scene :param QPixmap pixmap: Pixmap to be added to the scene :param str title: Title shown above (centered) the pixmap :param str comment: Comment displayed below the pixmap :param commentPosition: "CENTER" or "LEFT" """ if self._toBeCleared: self._clearAll() self.ensurePrinterIsSet() if self.printer is None: _logger.error("printer is not set, cannot add pixmap to page") return if title is None: title = ' ' * 88 if comment is None: comment = ' ' * 88 if commentPosition is None: commentPosition = "CENTER" if qt.qVersion() < "5.0": rectItem = qt.QGraphicsRectItem(self.page, self.scene) else: rectItem = qt.QGraphicsRectItem(self.page) rectItem.setRect(qt.QRectF(1, 1, pixmap.width(), pixmap.height())) pen = rectItem.pen() color = qt.QColor(qt.Qt.red) color.setAlpha(1) pen.setColor(color) rectItem.setPen(pen) rectItem.setZValue(1) rectItem.setFlag(qt.QGraphicsItem.ItemIsSelectable, True) rectItem.setFlag(qt.QGraphicsItem.ItemIsMovable, True) rectItem.setFlag(qt.QGraphicsItem.ItemIsFocusable, False) rectItemResizeRect = _GraphicsResizeRectItem(rectItem, self.scene) rectItemResizeRect.setZValue(2) if qt.qVersion() < "5.0": pixmapItem = qt.QGraphicsPixmapItem(rectItem, self.scene) else: pixmapItem = qt.QGraphicsPixmapItem(rectItem) pixmapItem.setPixmap(pixmap) pixmapItem.setZValue(0) # I add the title if qt.qVersion() < "5.0": textItem = qt.QGraphicsTextItem(title, rectItem, self.scene) else: textItem = qt.QGraphicsTextItem(title, rectItem) textItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction) offset = 0.5 * textItem.boundingRect().width() textItem.moveBy(0.5 * pixmap.width() - offset, -20) textItem.setZValue(2) # I add the comment if qt.qVersion() < "5.0": commentItem = qt.QGraphicsTextItem(comment, rectItem, self.scene) else: commentItem = qt.QGraphicsTextItem(comment, rectItem) commentItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction) offset = 0.5 * commentItem.boundingRect().width() if commentPosition.upper() == "LEFT": x = 1 else: x = 0.5 * pixmap.width() - offset commentItem.moveBy(x, pixmap.height() + 20) commentItem.setZValue(2) rectItem.moveBy(20, 40)