def paint(self, painter, option, widget=None): if not self.__icon.isNull(): if option.state & QStyle.State_Selected: mode = QIcon.Selected elif option.state & QStyle.State_Enabled: mode = QIcon.Normal elif option.state & QStyle.State_Active: mode = QIcon.Active else: mode = QIcon.Disabled transform = self.sceneTransform() if widget is not None: # 'widget' is the QGraphicsView.viewport() view = widget.parent() if isinstance(view, QGraphicsView): # Combine the scene transform with the view transform. view_transform = view.transform() transform = view_transform * view_transform lod = option.levelOfDetailFromTransform(transform) w, h = self.__iconSize.width(), self.__iconSize.height() target = QRectF(0, 0, w, h) source = QRectF(0, 0, w * lod, w * lod).toRect() # The actual size of the requested pixmap can be smaller. size = self.__icon.actualSize(source.size(), mode=mode) source.setSize(size) pixmap = self.__icon.pixmap(source.size(), mode=mode) painter.setRenderHint( QPainter.SmoothPixmapTransform, self.__transformationMode == Qt.SmoothTransformation ) painter.drawPixmap(target, pixmap, QRectF(source))
def paint(self, painter, option, widget=None): if not self.__icon.isNull(): if option.state & QStyle.State_Selected: mode = QIcon.Selected elif option.state & QStyle.State_Enabled: mode = QIcon.Normal elif option.state & QStyle.State_Active: mode = QIcon.Active else: mode = QIcon.Disabled transform = self.sceneTransform() if widget is not None: # 'widget' is the QGraphicsView.viewport() view = widget.parent() if isinstance(view, QGraphicsView): # Combine the scene transform with the view transform. view_transform = view.transform() transform = view_transform * view_transform lod = option.levelOfDetailFromTransform(transform) w, h = self.__iconSize.width(), self.__iconSize.height() target = QRectF(0, 0, w, h) source = QRectF(0, 0, w * lod, w * lod).toRect() # The actual size of the requested pixmap can be smaller. size = self.__icon.actualSize(source.size(), mode=mode) source.setSize(size) pixmap = self.__icon.pixmap(source.size(), mode=mode) painter.setRenderHint( QPainter.SmoothPixmapTransform, self.__transformationMode == Qt.SmoothTransformation) painter.drawPixmap(target, pixmap, QRectF(source))
def grab_svg(scene): # type: (QGraphicsScene) -> str """ Return a SVG rendering of the scene contents. Parameters ---------- scene : :class:`CanvasScene` """ svg_buffer = QBuffer() gen = _QSvgGenerator() gen.setOutputDevice(svg_buffer) items_rect = scene.itemsBoundingRect().adjusted(-10, -10, 10, 10) if items_rect.isNull(): items_rect = QRectF(0, 0, 10, 10) width, height = items_rect.width(), items_rect.height() rect_ratio = float(width) / height # Keep a fixed aspect ratio. aspect_ratio = 1.618 if rect_ratio > aspect_ratio: height = int(height * rect_ratio / aspect_ratio) else: width = int(width * aspect_ratio / rect_ratio) target_rect = QRectF(0, 0, width, height) source_rect = QRectF(0, 0, width, height) source_rect.moveCenter(items_rect.center()) gen.setSize(target_rect.size().toSize()) gen.setViewBox(target_rect) painter = QPainter(gen) # Draw background. painter.setPen(Qt.NoPen) painter.setBrush(scene.palette().base()) painter.drawRect(target_rect) # Render the scene scene.render(painter, target_rect, source_rect) painter.end() buffer_str = bytes(svg_buffer.buffer()) return buffer_str.decode("utf-8")
def grab_svg(scene): """ Return a SVG rendering of the scene contents. Parameters ---------- scene : :class:`CanvasScene` """ from AnyQt.QtSvg import QSvgGenerator svg_buffer = QBuffer() gen = QSvgGenerator() gen.setOutputDevice(svg_buffer) items_rect = scene.itemsBoundingRect().adjusted(-10, -10, 10, 10) if items_rect.isNull(): items_rect = QRectF(0, 0, 10, 10) width, height = items_rect.width(), items_rect.height() rect_ratio = float(width) / height # Keep a fixed aspect ratio. aspect_ratio = 1.618 if rect_ratio > aspect_ratio: height = int(height * rect_ratio / aspect_ratio) else: width = int(width * aspect_ratio / rect_ratio) target_rect = QRectF(0, 0, width, height) source_rect = QRectF(0, 0, width, height) source_rect.moveCenter(items_rect.center()) gen.setSize(target_rect.size().toSize()) gen.setViewBox(target_rect) painter = QPainter(gen) # Draw background. painter.setBrush(QBrush(Qt.white)) painter.drawRect(target_rect) # Render the scene scene.render(painter, target_rect, source_rect) painter.end() buffer_str = bytes(svg_buffer.buffer()) return buffer_str.decode("utf-8")
def setGeometry(self, rect: QRectF) -> None: resized = rect.size() != self.geometry() super().setGeometry(rect) if resized and self.__resizeContents: self.__updateScale() self.__layout()