def invalidate(self): self.__data.titleRect = QRectF() self.__data.footerRect = QRectF() self.__data.legendRect = QRectF() self.__data.canvasRect = QRectF() for axis in range(QwtPlot.axisCnt): self.__data.scaleRect[axis] = QRectF()
def reset(self): """Clear all stored commands""" self.__data.commands = [] self.__data.pathInfos = [] self.__data.boundingRect = QRectF(0.0, 0.0, -1.0, -1.0) self.__data.pointRect = QRectF(0.0, 0.0, -1.0, -1.0) self.__data.defaultSize = QSizeF()
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 qwtDrawRectSymbols(painter, points, numPoints, symbol): size = symbol.size() pen = QPen(symbol.pen()) pen.setJoinStyle(Qt.MiterJoin) painter.setPen(pen) painter.setBrush(symbol.brush()) painter.setRenderHint(QPainter.Antialiasing, False) if QwtPainter.roundingAlignment(painter): sw = size.width() sh = size.height() sw2 = size.width() // 2 sh2 = size.height() // 2 for pos in points: x = round(pos.x()) y = round(pos.y()) r = QRectF(x - sw2, y - sh2, sw, sh) QwtPainter.drawRect(painter, r) else: sw = size.width() sh = size.height() sw2 = .5 * size.width() sh2 = .5 * size.height() for pos in points: x = pos.x() y = pos.y() r = QRectF(x - sw2, y - sh2, sw, sh) QwtPainter.drawRect(painter, r)
def renderItem(self, painter, widget, rect, fillBackground): """ Render a legend entry into a given rectangle. :param QPainter painter: Painter :param QWidget widget: Widget representing a legend entry :param QRectF rect: Bounding rectangle :param bool fillBackground: When true, fill rect with the widget background """ if fillBackground: if widget.autoFillBackground() or\ widget.testAttribute(Qt.WA_StyledBackground): QwtPainter.drawBackground(painter, rect, widget) label = widget #TODO: cast to QwtLegendLabel if label is not None: icon = label.data().icon() sz = icon.defaultSize() iconRect = QRectF(rect.x()+label.margin(), rect.center().y()-.5*sz.height(), sz.width(), sz.height()) icon.render(painter, iconRect, Qt.KeepAspectRatio) titleRect = QRectF(rect) titleRect.setX(iconRect.right()+2*label.spacing()) painter.setFont(label.font()) painter.setPen(label.palette().color(QPalette.Text)) label.drawText(painter, titleRect) #TODO: cast label to QwtLegendLabel
def __init__(self): self.boundingRect = QRectF(0.0, 0.0, -1.0, -1.0) self.pointRect = QRectF(0.0, 0.0, -1.0, -1.0) self.initialTransform = None self.defaultSize = QSizeF() self.commands = [] self.pathInfos = [] self.renderHints = 0
def labelRect(self, font, value): lbl = self.tickLabel(font, value) if not lbl or lbl.isEmpty(): return QRectF(0., 0., 0., 0.) pos = self.labelPosition(value) labelSize = lbl.textSize(font) transform = self.labelTransformation(pos, labelSize) br = transform.mapRect(QRectF(QPointF(0, 0), labelSize)) br.translate(-pos.x(), -pos.y()) return br
def __init__(self): self.spacing = 5 self.titleRect = QRectF() self.footerRect = QRectF() self.legendRect = QRectF() self.scaleRect = [QRectF() for _i in range(QwtPlot.axisCnt)] self.canvasRect = QRectF() self.layoutData = QwtPlotLayout_LayoutData() self.legendPos = None self.legendRatio = None self.canvasMargin = [0] * QwtPlot.axisCnt self.alignCanvasToScales = [False] * QwtPlot.axisCnt
def invalidate(self): """ Invalidate the geometry of all components. .. seealso:: :py:meth:`activate()` """ self.__data.titleRect = QRectF() self.__data.footerRect = QRectF() self.__data.legendRect = QRectF() self.__data.canvasRect = QRectF() for axis in QwtPlot.validAxes: self.__data.scaleRect[axis] = QRectF()
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 drawText(self, *args): if len(args) == 4: if isinstance(args[1], (QRectF, QRect)): painter, rect, flags, text = args painter.save() qwtUnscaleFont(painter) painter.drawText(rect, flags, text) painter.restore() else: painter, x, y, text = args self.drawText(painter, QPointF(x, y), text) elif len(args) == 3: painter, pos, text = args deviceClipping, clipRect = qwtIsClippingNeeded(painter) if deviceClipping and not clipRect.contains(pos): return painter.save() qwtUnscaleFont(painter) painter.drawText(pos, text) painter.restore() elif len(args) == 7: painter, x, y, w, h, flags, text = args self.drawText(painter, QRectF(x, y, w, h), flags, text) else: raise TypeError("QwtPainter.drawText() takes 3, 4 or 7 argument"\ "(s) (%s given)" % len(args))
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 layoutLegend(self, options, rect): """ Find the geometry for the legend :param options: Options how to layout the legend :param QRectF rect: Rectangle where to place the legend :return: Geometry for the legend """ hint = self.__data.layoutData.legend.hint if self.__data.legendPos in (QwtPlot.LeftLegend, QwtPlot.RightLegend): dim = min([hint.width(), int(rect.width()*self.__data.legendRatio)]) if not (options & self.IgnoreScrollbars): if hint.height() > rect.height(): dim += self.__data.layoutData.legend.hScrollExtent else: dim = min([hint.height(), int(rect.height()*self.__data.legendRatio)]) dim = max([dim, self.__data.layoutData.legend.vScrollExtent]) legendRect = QRectF(rect) if self.__data.legendPos == QwtPlot.LeftLegend: legendRect.setWidth(dim) elif self.__data.legendPos == QwtPlot.RightLegend: legendRect.setX(rect.right()-dim) legendRect.setWidth(dim) elif self.__data.legendPos == QwtPlot.TopLegend: legendRect.setHeight(dim) elif self.__data.legendPos == QwtPlot.BottomLegend: legendRect.setY(rect.bottom()-dim) legendRect.setHeight(dim) return legendRect
def drawPath(self, path): """ Store a path command in the command list :param QPainterPath path: Painter path .. seealso:: :py:meth:`QPaintEngine.drawPath()` """ painter = self.paintEngine().painter() if painter is None: return self.__data.commands += [QwtPainterCommand(path)] if not path.isEmpty(): scaledPath = painter.transform().map(path) pointRect = scaledPath.boundingRect() boundingRect = QRectF(pointRect) if painter.pen().style() != Qt.NoPen\ and painter.pen().brush().style() != Qt.NoBrush: boundingRect = qwtStrokedPathRect(painter, path) self.updateControlPointRect(pointRect) self.updateBoundingRect(boundingRect) self.__data.pathInfos += [ PathInfo(pointRect, boundingRect, qwtHasScalablePen(painter)) ]
def qwtDrawStar1Symbols(painter, points, numPoints, symbol): size =symbol.size() painter.setPen(symbol.pen()) sqrt1_2 = np.sqrt(.5) if QwtPainter.roundingAlignment(painter): r = QRect(0, 0, size.width(), size.height()) for pos in points: r.moveCenter(pos.toPoint()) d1 = r.width()/2.*(1.-sqrt1_2) QwtPainter.drawLine(painter, round(r.left()+d1), round(r.top()+d1), round(r.right()-d1), round(r.bottom()-d1)) QwtPainter.drawLine(painter, round(r.left()+d1), round(r.bottom()-d1), round(r.right()-d1), round(r.top()+d1)) c = QPoint(r.center()) QwtPainter.drawLine(painter, c.x(), r.top(), c.x(), r.bottom()) QwtPainter.drawLine(painter, r.left(), c.y(), r.right(), c.y()) else: r = QRectF(0, 0, size.width(), size.height()) for pos in points: r.moveCenter(pos.toPoint()) c = QPointF(r.center()) d1 = r.width()/2.*(1.-sqrt1_2) QwtPainter.drawLine(painter, r.left()+d1, r.top()+d1, r.right()-d1, r.bottom()-d1) QwtPainter.drawLine(painter, r.left()+d1, r.bottom()-d1, r.right()-d1, r.top()+d1) QwtPainter.drawLine(painter, c.x(), r.top(), c.x(), r.bottom()) QwtPainter.drawLine(painter, r.left(), c.y(), r.right(), c.y())
def qwtBoundingRectT(series, from_, to): boundingRect = QRectF(1.0, 1.0, -2.0, -2.0) if from_ < 0: from_ = 0 if to < 0: to = series.size() - 1 if to < from_: return boundingRect first_stage = True for i in range(from_, to + 1): rect = qwtBoundingRect(series.sample(i)) if rect.width() >= 0. and rect.height() >= 0.: if first_stage: boundingRect = rect first_stage = False continue else: boundingRect.setLeft(min([boundingRect.left(), rect.left()])) boundingRect.setRight(max([boundingRect.right(), rect.right()])) boundingRect.setTop(min([boundingRect.top(), rect.top()])) boundingRect.setBottom( max([boundingRect.bottom(), rect.bottom()])) return boundingRect
def boundingRect(self): rect = QRectF(self.data().boundingRect()) if not rect.isValid(): return rect if self.orientation() == Qt.Horizontal: rect = QRectF(rect.y(), rect.x(), rect.height(), rect.width()) if rect.left() > self.__data.baseline: rect.setLeft(self.__data.baseline) elif rect.right() < self.__data.baseline: rect.setRight(self.__data.baseline) else: if rect.bottom() < self.__data.baseline: rect.setBottom(self.__data.baseline) elif rect.top() > self.__data.baseline: rect.setTop(self.__data.baseline) return rect
def drawSimpleRichText(self, painter, rect, flags, text): txt = text.clone() painter.save() unscaledRect = QRectF(rect) if painter.font().pixelSize() < 0: res = qwtScreenResolution() pd = painter.device() if pd.logicalDpiX() != res.width()\ or pd.logicalDpiY() != res.height(): transform = QTransform() transform.scale(res.width() / float(pd.logicalDpiX()), res.height() / float(pd.logicalDpiY())) painter.setWorldTransform(transform, True) invtrans, _ok = transform.inverted() unscaledRect = invtrans.mapRect(rect) txt.setDefaultFont(painter.font()) txt.setPageSize(QSizeF(unscaledRect.width(), QWIDGETSIZE_MAX)) layout = txt.documentLayout() height = layout.documentSize().height() y = unscaledRect.y() if flags & Qt.AlignBottom: y += unscaledRect.height() - height elif flags & Qt.AlignVCenter: y += (unscaledRect.height() - height) / 2 context = QAbstractTextDocumentLayout.PaintContext() context.palette.setColor(QPalette.Text, painter.pen().color()) painter.translate(unscaledRect.x(), y) layout.draw(painter, context) painter.restore()
def colorBarRect(self, rect): """ Calculate the the rectangle for the color bar :param QRectF rect: Bounding rectangle for all components of the scale :return: Rectangle for the color bar """ cr = QRectF(rect) if self.__data.scaleDraw.orientation() == Qt.Horizontal: cr.setLeft(cr.left() + self.__data.borderDist[0]) cr.setWidth(cr.width() - self.__data.borderDist[1] + 1) else: cr.setTop(cr.top() + self.__data.borderDist[0]) cr.setHeight(cr.height() - self.__data.borderDist[1] + 1) sda = self.__data.scaleDraw.alignment() if sda == QwtScaleDraw.LeftScale: cr.setLeft(cr.right() - self.__data.margin - self.__data.colorBar.width) cr.setWidth(self.__data.colorBar.width) elif sda == QwtScaleDraw.RightScale: cr.setLeft(cr.left() + self.__data.margin) cr.setWidth(self.__data.colorBar.width) elif sda == QwtScaleDraw.BottomScale: cr.setTop(cr.top() + self.__data.margin) cr.setHeight(self.__data.colorBar.width) elif sda == QwtScaleDraw.TopScale: cr.setTop(cr.bottom() - self.__data.margin - self.__data.colorBar.width) cr.setHeight(self.__data.colorBar.width) return cr
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 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 transform(self, *args): """ Transform a rectangle from scale to paint coordinates .. py:method:: transform(scalar) :param float scalar: Scalar .. py:method:: transform(xMap, yMap, rect) Transform a rectangle from scale to paint coordinates :param qwt.scale_map.QwtScaleMap xMap: X map :param qwt.scale_map.QwtScaleMap yMap: Y map :param QRectF rect: Rectangle in paint coordinates .. py:method:: transform(xMap, yMap, pos) Transform a point from scale to paint coordinates :param qwt.scale_map.QwtScaleMap xMap: X map :param qwt.scale_map.QwtScaleMap yMap: Y map :param QPointF pos: Position in scale coordinates Scalar: scalemap.transform(scalar) Point (QPointF): scalemap.transform(xMap, yMap, pos) Rectangle (QRectF): scalemap.transform(xMap, yMap, rect) .. seealso:: :py:meth:`invTransform()` """ if len(args) == 1: # Scalar transform return self.transform_scalar(args[0]) elif len(args) == 3 and isinstance(args[2], QPointF): xMap, yMap, pos = args return QPointF(xMap.transform(pos.x()), yMap.transform(pos.y())) elif len(args) == 3 and isinstance(args[2], QRectF): xMap, yMap, rect = args x1 = xMap.transform(rect.left()) x2 = xMap.transform(rect.right()) y1 = yMap.transform(rect.top()) y2 = yMap.transform(rect.bottom()) if x2 < x1: x1, x2 = x2, x1 if y2 < y1: y1, y2 = y2, y1 if qwtFuzzyCompare(x1, 0., x2 - x1) == 0: x1 = 0. if qwtFuzzyCompare(x2, 0., x2 - x1) == 0: x2 = 0. if qwtFuzzyCompare(y1, 0., y2 - y1) == 0: y1 = 0. if qwtFuzzyCompare(y2, 0., y2 - y1) == 0: y2 = 0. return QRectF(x1, y1, x2 - x1 + 1, y2 - y1 + 1) else: raise TypeError("%s().transform() takes 1 or 3 argument(s) (%s "\ "given)" % (self.__class__.__name__, len(args)))
def transform(self, *args): """Transform from scale to paint coordinates Scalar: scalemap.transform(scalar) Point (QPointF): scalemap.transform(xMap, yMap, pos) Rectangle (QRectF): scalemap.transform(xMap, yMap, rect) """ if len(args) == 1: # Scalar transform return self.transform_scalar(args[0]) elif len(args) == 3 and isinstance(args[2], QPointF): xMap, yMap, pos = args return QPointF(xMap.transform(pos.x()), yMap.transform(pos.y())) elif len(args) == 3 and isinstance(args[2], QRectF): xMap, yMap, rect = args x1 = xMap.transform(rect.left()) x2 = xMap.transform(rect.right()) y1 = yMap.transform(rect.top()) y2 = yMap.transform(rect.bottom()) if x2 < x1: x1, x2 = x2, x1 if y2 < y1: y1, y2 = y2, y1 if qwtFuzzyCompare(x1, 0., x2 - x1) == 0: x1 = 0. if qwtFuzzyCompare(x2, 0., x2 - x1) == 0: x2 = 0. if qwtFuzzyCompare(y1, 0., y2 - y1) == 0: y1 = 0. if qwtFuzzyCompare(y2, 0., y2 - y1) == 0: y2 = 0. return QRectF(x1, y1, x2 - x1 + 1, y2 - y1 + 1) else: raise TypeError("%s().transform() takes 1 or 3 argument(s) (%s "\ "given)" % (self.__class__.__name__, len(args)))
def _rects_conv_PyQt5(rects): # PyQt5 compatibility: the conversion from QRect to QRectF should not # be necessary but it seems to be anyway... PyQt5 bug? if PYQT5: return [QRectF(rect) for rect in rects] else: return rects
def drawBorder(self, painter): if self.__data.borderRadius > 0: if self.frameWidth() > 0: QwtPainter.drawRoundedFrame(painter, QRectF(self.frameRect()), self.__data.borderRadius, self.__data.borderRadius, self.palette(), self.frameWidth(), self.frameStyle()) else: if QT_VERSION >= 0x040500: if PYQT5: from qwt.qt.QtGui import QStyleOptionFrame else: from qwt.qt.QtGui import QStyleOptionFrameV3 as\ QStyleOptionFrame opt = QStyleOptionFrame() opt.initFrom(self) frameShape = self.frameStyle() & QFrame.Shape_Mask frameShadow = self.frameStyle() & QFrame.Shadow_Mask opt.frameShape = QFrame.Shape(int(opt.frameShape) | frameShape) if frameShape in (QFrame.Box, QFrame.HLine, QFrame.VLine, QFrame.StyledPanel, QFrame.Panel): opt.lineWidth = self.lineWidth() opt.midLineWidth = self.midLineWidth() else: opt.lineWidth = self.frameWidth() if frameShadow == self.Sunken: opt.state |= QStyle.State_Sunken elif frameShadow == self.Raised: opt.state |= QStyle.State_Raised self.style().drawControl(QStyle.CE_ShapedFrame, opt, painter, self) else: self.drawFrame(painter)
def layoutLegend(self, options, rect): hint = self.__data.layoutData.legend.hint if self.__data.legendPos in (QwtPlot.LeftLegend, QwtPlot.RightLegend): dim = min( [hint.width(), int(rect.width() * self.__data.legendRatio)]) if not (options & self.IgnoreScrollbars): if hint.height() > rect.height(): dim += self.__data.layoutData.legend.hScrollExtent else: dim = min( [hint.height(), int(rect.height() * self.__data.legendRatio)]) dim = max([dim, self.__data.layoutData.legend.vScrollExtent]) legendRect = QRectF(rect) if self.__data.legendPos == QwtPlot.LeftLegend: legendRect.setWidth(dim) elif self.__data.legendPos == QwtPlot.RightLegend: legendRect.setX(rect.right() - dim) legendRect.setWidth(dim) elif self.__data.legendPos == QwtPlot.TopLegend: legendRect.setHeight(dim) elif self.__data.legendPos == QwtPlot.BottomLegend: legendRect.setY(rect.bottom() - dim) legendRect.setHeight(dim) return legendRect
def drawLines(self, painter, xMap, yMap, canvasRect, from_, to): """ Draw lines :param QPainter painter: Painter :param qwt.scale_map.QwtScaleMap xMap: Maps x-values into pixel coordinates. :param qwt.scale_map.QwtScaleMap yMap: Maps y-values into pixel coordinates. :param QRectF canvasRect: Contents rectangle of the canvas :param int from_: Index of the first point to be painted :param int to: Index of the last point to be painted. If to < 0 the curve will be painted to its last point. .. seealso:: :py:meth:`draw()`, :py:meth:`drawDots()`, :py:meth:`drawSteps()`, :py:meth:`drawSticks()` """ if from_ > to: return doAlign = QwtPainter.roundingAlignment(painter) doFill = self.__data.brush.style() != Qt.NoBrush\ and self.__data.brush.color().alpha() > 0 clipRect = QRectF() if self.__data.paintAttributes & self.ClipPolygons: pw = max([1., painter.pen().widthF()]) clipRect = canvasRect.adjusted(-pw, -pw, pw, pw) doIntegers = False if QT_VERSION < 0x040800: if painter.paintEngine().type() == QPaintEngine.Raster: if not doFill: doIntegers = True noDuplicates = self.__data.paintAttributes & self.FilterPoints mapper = QwtPointMapper() mapper.setFlag(QwtPointMapper.RoundPoints, doAlign) mapper.setFlag(QwtPointMapper.WeedOutPoints, noDuplicates) mapper.setBoundingRect(canvasRect) if doIntegers: polyline = mapper.toPolygon(xMap, yMap, self.data(), from_, to) if self.__data.paintAttributes & self.ClipPolygons: polyline = QwtClipper().clipPolygon(clipRect.toAlignedRect(), polyline, False) QwtPainter.drawPolyline(painter, polyline) else: polyline = mapper.toPolygonF(xMap, yMap, self.data(), from_, to) if doFill: if painter.pen().style() != Qt.NoPen: filled = QPolygonF(polyline) self.fillCurve(painter, xMap, yMap, canvasRect, filled) filled.clear() if self.__data.paintAttributes & self.ClipPolygons: polyline = QwtClipper().clipPolygonF( clipRect, polyline, False) QwtPainter.drawPolyline(painter, polyline) else: self.fillCurve(painter, xMap, yMap, canvasRect, polyline) else: if self.__data.paintAttributes & self.ClipPolygons: polyline = QwtClipper().clipPolygonF( clipRect, polyline, False) QwtPainter.drawPolyline(painter, polyline)
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 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 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)