def renderDocument(self, plot, filename, sizeMM=(300, 200), resolution=85, format_=None): if isinstance(sizeMM, tuple): sizeMM = QSizeF(*sizeMM) if format_ is None: ext = osp.splitext(filename)[1] if not ext: raise TypeError( "Unable to determine target format from filename") format_ = ext[1:] if plot is None or sizeMM.isEmpty() or resolution <= 0: return title = plot.title().text() if not title: title = "Plot Document" mmToInch = 1. / 25.4 size = sizeMM * mmToInch * resolution documentRect = QRectF(0.0, 0.0, size.width(), size.height()) fmt = format_.lower() if fmt in ("pdf", "ps"): printer = QPrinter() if fmt == "pdf": printer.setOutputFormat(QPrinter.PdfFormat) else: printer.setOutputFormat(QPrinter.PostScriptFormat) printer.setColorMode(QPrinter.Color) printer.setFullPage(True) printer.setPaperSize(sizeMM, QPrinter.Millimeter) printer.setDocName(title) printer.setOutputFileName(filename) printer.setResolution(resolution) painter = QPainter(printer) self.render(plot, painter, documentRect) painter.end() elif fmt == "svg": generator = QSvgGenerator() generator.setTitle(title) generator.setFileName(filename) generator.setResolution(resolution) generator.setViewBox(documentRect) painter = QPainter(generator) self.render(plot, painter, documentRect) painter.end() elif fmt in QImageWriter.supportedImageFormats(): imageRect = documentRect.toRect() dotsPerMeter = int(round(resolution * mmToInch * 1000.)) image = QImage(imageRect.size(), QImage.Format_ARGB32) image.setDotsPerMeterX(dotsPerMeter) image.setDotsPerMeterY(dotsPerMeter) image.fill(QColor(Qt.white).rgb()) painter = QPainter(image) self.render(plot, painter, imageRect) painter.end() image.save(filename, fmt) else: raise TypeError("Unsupported file format '%s'" % fmt)
def toImage(self, *args): if len(args) == 0: if self.isNull(): return QImage() sz = self.defaultSize() w = np.ceil(sz.width()) h = np.ceil(sz.height()) image = QImage(w, h, QImage.Format_ARGB32) image.fill(0) r = QRect(0, 0, sz.width(), sz.height()) painter = QPainter(image) self.render(painter, r, Qt.KeepAspectRatio) painter.end() return image elif len(args) in (1, 2): size = args[0] aspectRatioMode = Qt.IgnoreAspectRatio if len(args) == 2: aspectRatioMode = args[-1] image = QImage(size, QImage.Format_ARGB32_Premultiplied) image.fill(0) r = QRect(0, 0, size.width(), size.height()) painter = QPainter(image) self.render(painter, r, aspectRatioMode) return image
def toPixmap(self, *args): if len(args) == 0: if self.isNull(): return QPixmap() sz = self.defaultSize() w = np.ceil(sz.width()) h = np.ceil(sz.height()) pixmap = QPixmap(w, h) pixmap.fill(Qt.transparent) r = QRectF(0., 0., sz.width(), sz.height()) painter = QPainter(pixmap) self.render(painter, r, Qt.KeepAspectRatio) painter.end() return pixmap elif len(args) in (1, 2): size = args[0] aspectRatioMode = Qt.IgnoreAspectRatio if len(args) == 2: aspectRatioMode = args[-1] pixmap = QPixmap(size) pixmap.fill(Qt.transparent) r = QRect(0, 0, size.width(), size.height()) painter = QPainter(pixmap) self.render(painter, r, aspectRatioMode) painter.end() return pixmap
def toImage(self, *args): """ .. py:method:: toImage() Convert the graphic to a `QImage` All pixels of the image get initialized by 0 ( transparent ) before the graphic is scaled and rendered on it. The format of the image is `QImage.Format_ARGB32_Premultiplied`. The size of the image is the default size ( ceiled to integers ) of the graphic. :return: The graphic as image in default size .. py:method:: toImage(size, [aspectRatioMode=Qt.IgnoreAspectRatio]) Convert the graphic to a `QImage` All pixels of the image get initialized by 0 ( transparent ) before the graphic is scaled and rendered on it. The format of the image is `QImage.Format_ARGB32_Premultiplied`. :param QSize size: Size of the image :param `Qt.AspectRatioMode` aspectRatioMode: Aspect ratio how to scale the graphic :return: The graphic as image .. seealso:: :py:meth:`toPixmap()`, :py:meth:`render()` """ if len(args) == 0: if self.isNull(): return QImage() sz = self.defaultSize() w = np.ceil(sz.width()) h = np.ceil(sz.height()) image = QImage(w, h, QImage.Format_ARGB32) image.fill(0) r = QRect(0, 0, sz.width(), sz.height()) painter = QPainter(image) self.render(painter, r, Qt.KeepAspectRatio) painter.end() return image elif len(args) in (1, 2): size = args[0] aspectRatioMode = Qt.IgnoreAspectRatio if len(args) == 2: aspectRatioMode = args[-1] image = QImage(size, QImage.Format_ARGB32_Premultiplied) image.fill(0) r = QRect(0, 0, size.width(), size.height()) painter = QPainter(image) self.render(painter, r, aspectRatioMode) return image
def paintEvent(self, event): painter = QPainter(self) painter.setClipRegion(event.region()) if self.testPaintAttribute(self.BackingStore) and\ self.__data.backingStore is not None: bs = self.__data.backingStore if bs.size() != self.size(): bs = QwtPainter.backingStore(self, self.size()) if self.testAttribute(Qt.WA_StyledBackground): p = QPainter(bs) qwtFillBackground(p, self) self.drawCanvas(p, True) else: p = QPainter() if self.__data.borderRadius <= 0.: # print('**DEBUG: QwtPlotCanvas.paintEvent') QwtPainter.fillPixmap(self, bs) p.begin(bs) self.drawCanvas(p, False) else: p.begin(bs) qwtFillBackground(p, self) self.drawCanvas(p, True) if self.frameWidth() > 0: self.drawBorder(p) p.end() painter.drawPixmap(0, 0, self.__data.backingStore) else: if self.testAttribute(Qt.WA_StyledBackground): if self.testAttribute(Qt.WA_OpaquePaintEvent): qwtFillBackground(painter, self) self.drawCanvas(painter, True) else: self.drawCanvas(painter, False) else: if self.testAttribute(Qt.WA_OpaquePaintEvent): if self.autoFillBackground(): qwtFillBackground(painter, self) qwtDrawBackground(painter, self) else: if self.borderRadius() > 0.: clipPath = QPainterPath() clipPath.addRect(self.rect()) clipPath = clipPath.subtracted( self.borderPath(self.rect())) painter.save() painter.setClipPath(clipPath, Qt.IntersectClip) qwtFillBackground(painter, self) qwtDrawBackground(painter, self) painter.restore() self.drawCanvas(painter, False) if self.frameWidth() > 0: self.drawBorder(painter) if self.hasFocus() and self.focusIndicator( ) == self.CanvasFocusIndicator: self.drawFocusIndicator(painter)
def legendIcon(self, index, size): """ :param int index: Index of the legend entry (ignored as there is only one) :param QSizeF size: Icon size :return: Icon representing the marker on the legend .. seealso:: :py:meth:`qwt.plot.QwtPlotItem.setLegendIconSize()`, :py:meth:`qwt.plot.QwtPlotItem.legendData()` """ if size.isEmpty(): return QwtGraphic() icon = QwtGraphic() icon.setDefaultSize(size) icon.setRenderHint(QwtGraphic.RenderPensUnscaled, True) painter = QPainter(icon) painter.setRenderHint( QPainter.Antialiasing, self.testRenderHint(QwtPlotItem.RenderAntialiased)) if self.__data.style != QwtPlotMarker.NoLine: painter.setPen(self.__data.pen) if self.__data.style in (QwtPlotMarker.HLine, QwtPlotMarker.Cross): y = .5 * size.height() painter.drawLine(0., y, size.width(), y) if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross): x = .5 * size.width() painter.drawLine(x, 0., x, size.height()) if self.__data.symbol: r = QRect(0, 0, size.width(), size.height()) self.__data.symbol.drawSymbol(painter, r) return icon
def fillPixmap(self, widget, pixmap, offset=None): """ Fill a pixmap with the content of a widget In Qt >= 5.0 `QPixmap.fill()` is a nop, in Qt 4.x it is buggy for backgrounds with gradients. Thus `fillPixmap()` offers an alternative implementation. :param QWidget widget: Widget :param QPixmap pixmap: Pixmap to be filled :param QPoint offset: Offset .. seealso:: :py:meth:`QPixmap.fill()` """ if offset is None: offset = QPoint() rect = QRect(offset, pixmap.size()) painter = QPainter(pixmap) painter.translate(-offset) autoFillBrush = widget.palette().brush(widget.backgroundRole()) if not (widget.autoFillBackground() and autoFillBrush.isOpaque()): bg = widget.palette().brush(QPalette.Window) qwtFillRect(widget, painter, rect, bg) if widget.autoFillBackground(): qwtFillRect(widget, painter, rect, autoFillBrush) if widget.testAttribute(Qt.WA_StyledBackground): painter.setClipRegion(rect) opt = QStyleOption() opt.initFrom(widget) widget.style().drawPrimitive(QStyle.PE_Widget, opt, painter, widget)
def legendIcon(self, index, size): if size.isEmpty(): return QwtGraphic() graphic = QwtGraphic() graphic.setDefaultSize(size) graphic.setRenderHint(QwtGraphic.RenderPensUnscaled, True) painter = QPainter(graphic) painter.setRenderHint( QPainter.Antialiasing, self.testRenderHint(QwtPlotItem.RenderAntialiased)) if self.__data.legendAttributes == 0 or\ (self.__data.legendAttributes & QwtPlotCurve.LegendShowBrush): brush = self.__data.brush if brush.style( ) == Qt.NoBrush and self.__data.legendAttributes == 0: if self.style() != QwtPlotCurve.NoCurve: brush = QBrush(self.pen().color()) elif self.__data.symbol and\ self.__data.symbol.style() != QwtSymbol.NoSymbol: brush = QBrush(self.__data.symbol.pen().color()) if brush.style() != Qt.NoBrush: r = QRectF(0, 0, size.width(), size.height()) painter.fillRect(r, brush) if self.__data.legendAttributes & QwtPlotCurve.LegendShowLine: if self.pen() != Qt.NoPen: pn = self.pen() # pn.setCapStyle(Qt.FlatCap) painter.setPen(pn) y = .5 * size.height() QwtPainter.drawLine(painter, 0., y, size.width(), y) if self.__data.legendAttributes & QwtPlotCurve.LegendShowSymbol: if self.__data.symbol: r = QRectF(0, 0, size.width(), size.height()) self.__data.symbol.drawSymbol(painter, r) return graphic
def drawColorBar(self, painter, colorMap, interval, scaleMap, orientation, rect): colorTable = [] if colorMap.format() == QwtColorMap.Indexed: colorTable = colorMap.colorTable(interval) c = QColor() devRect = rect.toAlignedRect() pixmap = QPixmap(devRect.size()) pixmap.fill(Qt.transparent) pmPainter = QPainter(pixmap) pmPainter.translate(-devRect.x(), -devRect.y()) if orientation == Qt.Horizontal: sMap = scaleMap sMap.setPaintInterval(rect.left(), rect.right()) for x in range(devRect.left(), devRect.right() + 1): value = sMap.invTransform(x) if colorMap.format() == QwtColorMap.RGB: c.setRgba(colorMap.rgb(interval, value)) else: c = colorTable[colorMap.colorIndex(interval, value)] pmPainter.setPen(c) pmPainter.drawLine(x, devRect.top(), devRect.bottom()) else: sMap = scaleMap sMap.setPaintInterval(rect.bottom(), rect.top()) for y in range(devRect.top(), devRect.bottom() + 1): value = sMap.invTransform(y) if colorMap.format() == QwtColorMap.RGB: c.setRgba(colorMap.rgb(interval, value)) else: c = colorTable[colorMap.colorIndex(interval, value)] pmPainter.setPen(c) pmPainter.drawLine(devRect.left(), y, devRect.right(), y) pmPainter.end() self.drawPixmap(painter, rect, pixmap)
def paintEvent(self, event): painter = QPainter(self) painter.setClipRegion(event.region()) opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) self.draw(painter)
def borderPath(self, rect): """ Calculate the painter path for a styled or rounded border When the canvas has no styled background or rounded borders the painter path is empty. :param QRect rect: Bounding rectangle of the canvas :return: Painter path, that can be used for clipping """ if self.testAttribute(Qt.WA_StyledBackground): recorder = QwtStyleSheetRecorder(rect.size()) painter = QPainter(recorder) opt = QStyleOption() opt.initFrom(self) opt.rect = rect self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) painter.end() if not recorder.background.path.isEmpty(): return recorder.background.path if not recorder.border.rectList.isEmpty(): return qwtCombinePathList(rect, recorder.border.pathlist) elif self.__data.borderRadius > 0.: fw2 = self.frameWidth() * .5 r = QRectF(rect).adjusted(fw2, fw2, -fw2, -fw2) path = QPainterPath() path.addRoundedRect(r, self.__data.borderRadius, self.__data.borderRadius) return path return QPainterPath()
def defaultIcon(brush, size): icon = QwtGraphic() if not size.isEmpty(): icon.setDefaultSize(size) r = QRectF(0, 0, size.width(), size.height()) painter = QPainter(icon) painter.fillRect(r, brush) return icon
def __init__(self): self.attributes = 0 self.hasClipping = False self.seriesItem = None # QwtPlotSeriesItem self.clipRegion = QRegion() self.painter = QPainter() self.from_ = None self.to = None
def qwtPathGraphic(path, pen, brush): graphic = QwtGraphic() graphic.setRenderHint(QwtGraphic.RenderPensUnscaled) painter = QPainter(graphic) painter.setPen(pen) painter.setBrush(brush) painter.drawPath(path) painter.end() return graphic
def paintEvent(self, event): painter = QPainter(self) if not self.contentsRect().contains(event.rect()): painter.save() painter.setClipRegion(event.region() & self.frameRect()) self.drawFrame(painter) painter.restore() painter.setClipRegion(event.region() & self.contentsRect()) self.drawContents(painter)
def drawSeries(self, seriesItem, from_, to): """When observing an measurement while it is running, new points have to be added to an existing seriesItem. drawSeries() can be used to display them avoiding a complete redraw of the canvas. Setting plot().canvas().setAttribute(Qt.WA_PaintOutsidePaintEvent, True) will result in faster painting, if the paint engine of the canvas widget supports this feature.""" if seriesItem is None or seriesItem.plot() is None: return canvas = seriesItem.plot().canvas() canvasRect = canvas.contentsRect() plotCanvas = canvas #XXX: cast to QwtPlotCanvas if plotCanvas and qwtHasBackingStore(plotCanvas): painter = QPainter(plotCanvas.backingStore()) #XXX: cast plotCanvas.backingStore() to QPixmap if self.__data.hasClipping: painter.setClipRegion(self.__data.clipRegion) qwtRenderItem(painter, canvasRect, seriesItem, from_, to) if self.testAttribute(self.FullRepaint): plotCanvas.repaint() return immediatePaint = True if not canvas.testAttribute(Qt.WA_WState_InPaintEvent): if QT_VERSION >= 0x050000 or\ not canvas.testAttribute(Qt.WA_PaintOutsidePaintEvent): immediatePaint = False if immediatePaint: if not self.__data.painter.isActive(): self.reset() self.__data.painter.begin(canvas) canvas.installEventFilter(self) if self.__data.hasClipping: self.__data.painter.setClipRegion( QRegion(canvasRect) & self.__data.clipRegion) elif not self.__data.painter.hasClipping(): self.__data.painter.setClipRect(canvasRect) qwtRenderItem(self.__data.painter, canvasRect, seriesItem, from_, to) if self.__data.attributes & self.AtomicPainter: self.reset() elif self.__data.hasClipping: self.__data.painter.setClipping(False) else: self.reset() self.__data.seriesItem = seriesItem self.__data.from_ = from_ self.__data.to = to clipRegion = QRegion(canvasRect) if self.__data.hasClipping: clipRegion &= self.__data.clipRegion canvas.installEventFilter(self) canvas.repaint(clipRegion) canvas.removeEventFilter(self) self.__data.seriesItem = None
def drawSymbols(self, painter, points, numPoints=None): """ Render an array of symbols Painting several symbols is more effective than drawing symbols one by one, as a couple of layout calculations and setting of pen/brush can be done once for the complete array. :param QPainter painter: Painter :param QPolygonF points: Positions of the symbols in screen coordinates """ #TODO: remove argument numPoints (not necessary in `PythonQwt`) if numPoints is not None and numPoints <= 0: return useCache = False if QwtPainter.roundingAlignment(painter) and\ not painter.transform().isScaling(): if self.__data.cache.policy == QwtSymbol.Cache: useCache = True elif self.__data.cache.policy == QwtSymbol.AutoCache: if painter.paintEngine().type() == QPaintEngine.Raster: useCache = True else: if self.__data.style in (QwtSymbol.XCross, QwtSymbol.HLine, QwtSymbol.VLine, QwtSymbol.Cross): pass elif self.__data.style == QwtSymbol.Pixmap: if not self.__data.size.isEmpty() and\ self.__data.size != self.__data.pixmap.pixmap.size(): useCache = True else: useCache = True if useCache: br = QRect(self.boundingRect()) rect = QRect(0, 0, br.width(), br.height()) if self.__data.cache.pixmap.isNull(): self.__data.cache.pixmap = QwtPainter.backingStore( None, br.size()) self.__data.cache.pixmap.fill(Qt.transparent) p = QPainter(self.__data.cache.pixmap) p.setRenderHints(painter.renderHints()) p.translate(-br.topLeft()) pos = QPointF() self.renderSymbols(p, pos, 1) dx = br.left() dy = br.top() for point in points: left = round(point.x()) + dx top = round(point.y()) + dy painter.drawPixmap(left, top, self.__data.cache.pixmap) else: painter.save() self.renderSymbols(painter, points, numPoints) painter.restore()
def toPixmap(self, *args): """ Convert the graphic to a `QPixmap` All pixels of the pixmap get initialized by `Qt.transparent` before the graphic is scaled and rendered on it. The size of the pixmap is the default size ( ceiled to integers ) of the graphic. :return: The graphic as pixmap in default size .. seealso:: :py:meth:`defaultSize()`, :py:meth:`toImage()`, :py:meth:`render()` """ if len(args) == 0: if self.isNull(): return QPixmap() sz = self.defaultSize() w = np.ceil(sz.width()) h = np.ceil(sz.height()) pixmap = QPixmap(w, h) pixmap.fill(Qt.transparent) r = QRectF(0., 0., sz.width(), sz.height()) painter = QPainter(pixmap) self.render(painter, r, Qt.KeepAspectRatio) painter.end() return pixmap elif len(args) in (1, 2): size = args[0] aspectRatioMode = Qt.IgnoreAspectRatio if len(args) == 2: aspectRatioMode = args[-1] pixmap = QPixmap(size) pixmap.fill(Qt.transparent) r = QRect(0, 0, size.width(), size.height()) painter = QPainter(pixmap) self.render(painter, r, aspectRatioMode) painter.end() return pixmap
def qwtFillBackground(*args): if len(args) == 2: painter, canvas = args rects = [] if canvas.testAttribute(Qt.WA_StyledBackground): recorder = QwtStyleSheetRecorder(canvas.size()) p = QPainter(recorder) qwtDrawStyledBackground(canvas, p) p.end() if recorder.background.brush.isOpaque(): rects = recorder.clipRects else: rects += [canvas.rect()] else: r = canvas.rect() radius = canvas.borderRadius() if radius > 0.: sz = QSizeF(radius, radius) rects += [ QRectF(r.topLeft(), sz), QRectF(r.topRight() - QPointF(radius, 0), sz), QRectF(r.bottomRight() - QPointF(radius, radius), sz), QRectF(r.bottomLeft() - QPointF(0, radius), sz) ] qwtFillBackground(painter, canvas, rects) elif len(args) == 3: painter, widget, fillRects = args if not fillRects: return if painter.hasClipping(): clipRegion = painter.transform().map(painter.clipRegion()) else: clipRegion = widget.contentsRect() bgWidget = qwtBackgroundWidget(widget.parentWidget()) for fillRect in fillRects: rect = fillRect.toAlignedRect() if clipRegion.intersects(rect): pm = QPixmap(rect.size()) QwtPainter.fillPixmap(bgWidget, pm, widget.mapTo(bgWidget, rect.topLeft())) painter.drawPixmap(rect, pm) else: raise TypeError("%s() takes 2 or 3 argument(s) (%s given)"\ % ("qwtFillBackground", len(args)))
def toImage(self, xMap, yMap, series, from_, to, pen, antialiased, numThreads): if USE_THREADS: if numThreads == 0: numThreads = QThread.idealThreadCount() if numThreads <= 0: numThreads = 1 rect = self.__data.boundingRect.toAlignedRect() image = QImage(rect.size(), QImage.Format_ARGB32) image.fill(Qt.transparent) if pen.width() <= 1 and pen.color().alpha() == 255: command = QwtDotsCommand() command.series = series command.rgb = pen.color().rgba() if USE_THREADS: numPoints = int((to - from_ + 1) / numThreads) futures = [] for i in range(numThreads): pos = rect.topLeft() index0 = from_ + i * numPoints if i == numThreads - 1: command.from_ = index0 command.to = to qwtRenderDots(xMap, yMap, command, pos, image) else: command.from_ = index0 command.to = index0 + numPoints - 1 futures += [ QtConcurrent.run(qwtRenderDots, xMap, yMap, command, pos, image) ] for future in futures: future.waitForFinished() else: command.from_ = from_ command.to = to qwtRenderDots(xMap, yMap, command, rect.topLeft(), image) else: painter = QPainter(image) painter.setPen(pen) painter.setRenderHint(QPainter.Antialiasing, antialiased) chunkSize = 1000 for i in range(chunkSize): indexTo = min([i + chunkSize - 1, to]) points = self.toPoints(xMap, yMap, series, i, indexTo) painter.drawPoints(points) return image
def qwtBackgroundWidget(w): if w.parentWidget() is None: return w if w.autoFillBackground(): brush = w.palette().brush(w.backgroundRole()) if brush.color().alpha() > 0: return w if w.testAttribute(Qt.WA_StyledBackground): image = QImage(1, 1, QImage.Format_ARGB32) image.fill(Qt.transparent) painter = QPainter(image) painter.translate(-w.rect().center()) qwtDrawStyledBackground(w, painter) painter.end() if qAlpha(image.pixel(0, 0)) != 0: return w return qwtBackgroundWidget(w.parentWidget())
def drawColorBar(self, painter, colorMap, interval, scaleMap, orientation, rect): """ Draw a color bar into a rectangle :param QPainter painter: Painter :param qwt.color_map.QwtColorMap colorMap: Color map :param qwt.interval.QwtInterval interval: Value range :param qwt.scalemap.QwtScaleMap scaleMap: Scale map :param Qt.Orientation orientation: Orientation :param QRectF rect: Target rectangle """ colorTable = [] if colorMap.format() == QwtColorMap.Indexed: colorTable = colorMap.colorTable(interval) c = QColor() devRect = rect.toAlignedRect() pixmap = QPixmap(devRect.size()) pixmap.fill(Qt.transparent) pmPainter = QPainter(pixmap) pmPainter.translate(-devRect.x(), -devRect.y()) if orientation == Qt.Horizontal: sMap = QwtScaleMap(scaleMap) sMap.setPaintInterval(rect.left(), rect.right()) for x in range(devRect.left(), devRect.right() + 1): value = sMap.invTransform(x) if colorMap.format() == QwtColorMap.RGB: c.setRgba(colorMap.rgb(interval, value)) else: c = colorTable[colorMap.colorIndex(interval, value)] pmPainter.setPen(c) pmPainter.drawLine(x, devRect.top(), x, devRect.bottom()) else: sMap = QwtScaleMap(scaleMap) sMap.setPaintInterval(rect.bottom(), rect.top()) for y in range(devRect.top(), devRect.bottom() + 1): value = sMap.invTransform(y) if colorMap.format() == QwtColorMap.RGB: c.setRgba(colorMap.rgb(interval, value)) else: c = colorTable[colorMap.colorIndex(interval, value)] pmPainter.setPen(c) pmPainter.drawLine(devRect.left(), y, devRect.right(), y) pmPainter.end() self.drawPixmap(painter, rect, pixmap)
def legendIcon(self, index, size): """ :param int index: Index of the legend entry (ignored as there is only one) :param QSizeF size: Icon size :return: Icon representing the curve on the legend .. seealso:: :py:meth:`qwt.plot.QwtPlotItem.setLegendIconSize()`, :py:meth:`qwt.plot.QwtPlotItem.legendData()` """ if size.isEmpty(): return QwtGraphic() graphic = QwtGraphic() graphic.setDefaultSize(size) graphic.setRenderHint(QwtGraphic.RenderPensUnscaled, True) painter = QPainter(graphic) painter.setRenderHint( QPainter.Antialiasing, self.testRenderHint(QwtPlotItem.RenderAntialiased)) if self.__data.legendAttributes == 0 or\ (self.__data.legendAttributes & QwtPlotCurve.LegendShowBrush): brush = self.__data.brush if brush.style( ) == Qt.NoBrush and self.__data.legendAttributes == 0: if self.style() != QwtPlotCurve.NoCurve: brush = QBrush(self.pen().color()) elif self.__data.symbol and\ self.__data.symbol.style() != QwtSymbol.NoSymbol: brush = QBrush(self.__data.symbol.pen().color()) if brush.style() != Qt.NoBrush: r = QRectF(0, 0, size.width(), size.height()) painter.fillRect(r, brush) if self.__data.legendAttributes & QwtPlotCurve.LegendShowLine: if self.pen() != Qt.NoPen: pn = self.pen() # pn.setCapStyle(Qt.FlatCap) painter.setPen(pn) y = .5 * size.height() painter.drawLine(0., y, size.width(), y) if self.__data.legendAttributes & QwtPlotCurve.LegendShowSymbol: if self.__data.symbol: r = QRectF(0, 0, size.width(), size.height()) self.__data.symbol.drawSymbol(painter, r) return graphic
def fillPixmap(self, widget, pixmap, offset=None): if offset is None: offset = QPoint() rect = QRect(offset, pixmap.size()) painter = QPainter(pixmap) painter.translate(-offset) autoFillBrush = widget.palette().brush(widget.backgroundRole()) if not (widget.autoFillBackground() and autoFillBrush.isOpaque()): bg = widget.palette().brush(QPalette.Window) qwtFillRect(widget, painter, rect, bg) if widget.autoFillBackground(): qwtFillRect(widget, painter, rect, autoFillBrush) if widget.testAttribute(Qt.WA_StyledBackground): painter.setClipRegion(rect) opt = QStyleOption() opt.initFrom(widget) widget.style().drawPrimitive(QStyle.PE_Widget, opt, painter, widget)
def renderTo(self, plot, dest): """ Render a plot to a file Supported formats are: - pdf: Portable Document Format PDF - ps: Postcript - svg: Scalable Vector Graphics SVG - all image formats supported by Qt, see QImageWriter.supportedImageFormats() Scalable vector graphic formats like PDF or SVG are superior to raster graphics formats. :param qwt.plot.QwtPlot plot: Plot widget :param str fileName: Path of the file, where the document will be stored :param str format: Format for the document :param QSizeF sizeMM: Size for the document in millimeters. :param int resolution: Resolution in dots per Inch (dpi) .. seealso:: :py:meth:`renderTo()`, :py:meth:`render()`, :py:meth:`qwt.painter.QwtPainter.setRoundingAlignment()` """ if isinstance(dest, QPaintDevice): w = dest.width() h = dest.height() rect = QRectF(0, 0, w, h) elif isinstance(dest, QPrinter): w = dest.width() h = dest.height() rect = QRectF(0, 0, w, h) aspect = rect.width() / rect.height() if aspect < 1.: rect.setHeight(aspect * rect.width()) elif isinstance(dest, QSvgGenerator): rect = dest.viewBoxF() if rect.isEmpty(): rect.setRect(0, 0, dest.width(), dest.height()) if rect.isEmpty(): rect.setRect(0, 0, 800, 600) p = QPainter(dest) self.render(plot, p, rect)
def qwtDrawBackground(painter, canvas): painter.save() borderClip = canvas.borderPath(canvas.rect()) if not borderClip.isEmpty(): painter.setClipPath(borderClip, Qt.IntersectClip) brush = canvas.palette().brush(canvas.backgroundRole()) if brush.style() == Qt.TexturePattern: pm = QPixmap(canvas.size()) QwtPainter.fillPixmap(canvas, pm) painter.drawPixmap(0, 0, pm) elif brush.gradient(): rects = [] if brush.gradient().coordinateMode() == QGradient.ObjectBoundingMode: rects += [canvas.rect()] else: rects += [painter.clipRegion().rects()] useRaster = False if painter.paintEngine().type() == QPaintEngine.X11: useRaster = True if useRaster: format_ = QImage.Format_RGB32 stops = brush.gradient().stops() for stop in stops: if stop.second.alpha() != 255: format_ = QImage.Format_ARGB32 break image = QImage(canvas.size(), format_) p = QPainter(image) p.setPen(Qt.NoPen) p.setBrush(brush) p.drawRects(_rects_conv_PyQt5(rects)) p.end() painter.drawImage(0, 0, image) else: painter.setPen(Qt.NoPen) painter.setBrush(brush) painter.drawRects(_rects_conv_PyQt5(rects)) else: painter.setPen(Qt.NoPen) painter.setBrush(brush) painter.drawRects(_rects_conv_PyQt5(painter.clipRegion().rects())) painter.restore()
def drawSymbols(self, painter, points, numPoints=None): #TODO: remove argument numPoints (not necessary in Python's qwt) if numPoints is not None and numPoints <= 0: return useCache = False if QwtPainter.roundingAlignment(painter) and\ not painter.transform().isScaling(): if self.__data.cache.policy == QwtSymbol.Cache: useCache = True elif self.__data.cache.policy == QwtSymbol.AutoCache: if painter.paintEngine().type() == QPaintEngine.Raster: useCache = True else: if self.__data.style in (QwtSymbol.XCross, QwtSymbol.HLine, QwtSymbol.VLine, QwtSymbol.Cross): pass elif self.__data.style == QwtSymbol.Pixmap: if not self.__data.size.isEmpty() and\ self.__data.size != self.__data.pixmap.pixmap.size(): useCache = True else: useCache = True if useCache: br = QRect(self.boundingRect()) rect = QRect(0, 0, br.width(), br.height()) if self.__data.cache.pixmap.isNull(): self.__data.cache.pixmap = QwtPainter.backingStore(None, br.size()) self.__data.cache.pixmap.fill(Qt.transparent) p = QPainter(self.__data.cache.pixmap) p.setRenderHints(painter.renderHints()) p.translate(-br.topLeft()) pos = QPointF() self.renderSymbols(p, pos, 1) dx = br.left() dy = br.top() for point in points: left = round(point.x())+dx top = round(point.y())+dy painter.drawPixmap(left, top, self.__data.cache.pixmap) else: painter.save() self.renderSymbols(painter, points, numPoints) painter.restore()
def updateStyleSheetInfo(self): if not self.testAttribute(Qt.WA_StyledBackground): return recorder = QwtStyleSheetRecorder(self.size()) painter = QPainter(recorder) opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) painter.end() self.__data.styleSheet.hasBorder = not recorder.border.rectList.isEmpty( ) self.__data.styleSheet.cornerRects = recorder.clipRects if recorder.background.path.isEmpty(): if not recorder.border.rectList.isEmpty(): self.__data.styleSheet.borderPath =\ qwtCombinePathList(self.rect(), recorder.border.pathlist) else: self.__data.styleSheet.borderPath = recorder.background.path self.__data.styleSheet.background.brush = recorder.background.brush self.__data.styleSheet.background.origin = recorder.background.origin
def renderTo(self, plot, dest): if isinstance(dest, QPaintDevice): w = dest.width() h = dest.height() rect = QRectF(0, 0, w, h) elif isinstance(dest, QPrinter): w = dest.width() h = dest.height() rect = QRectF(0, 0, w, h) aspect = rect.width() / rect.height() if aspect < 1.: rect.setHeight(aspect * rect.width()) elif isinstance(dest, QSvgGenerator): rect = dest.viewBoxF() if rect.isEmpty(): rect.setRect(0, 0, dest.width(), dest.height()) if rect.isEmpty(): rect.setRect(0, 0, 800, 600) p = QPainter(dest) self.render(plot, p, rect)
def borderPath(self, rect): if self.testAttribute(Qt.WA_StyledBackground): recorder = QwtStyleSheetRecorder(rect.size()) painter = QPainter(recorder) opt = QStyleOption() opt.initFrom(self) opt.rect = rect self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) painter.end() if not recorder.background.path.isEmpty(): return recorder.background.path if not recorder.border.rectList.isEmpty(): return qwtCombinePathList(rect, recorder.border.pathlist) elif self.__data.borderRadius > 0.: fw2 = self.frameWidth() * .5 r = QRectF(rect).adjusted(fw2, fw2, -fw2, -fw2) path = QPainterPath() path.addRoundedRect(r, self.__data.borderRadius, self.__data.borderRadius) return path return QPainterPath()