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 drawSymbol(self, painter, point_or_rect): """ Draw the symbol into a rectangle The symbol is painted centered and scaled into the target rectangle. It is always painted uncached and the pin point is ignored. This method is primarily intended for drawing a symbol to the legend. :param QPainter painter: Painter :param point_or_rect: Position or target rectangle of the symbol in screen coordinates :type point_or_rect: QPointF or QPoint or QRectF """ if isinstance(point_or_rect, (QPointF, QPoint)): # drawSymbol( QPainter *, const QPointF & ) self.drawSymbols(painter, [point_or_rect]) return # drawSymbol( QPainter *, const QRectF & ) rect = point_or_rect assert isinstance(rect, QRectF) if self.__data.style == QwtSymbol.NoSymbol: return if self.__data.style == QwtSymbol.Graphic: self.__data.graphic.graphic.render(painter, rect, Qt.KeepAspectRatio) elif self.__data.style == QwtSymbol.Path: if self.__data.path.graphic.isNull(): self.__data.path.graphic = qwtPathGraphic( self.__data.path.path, self.__data.pen, self.__data.brush) self.__data.path.graphic.render(painter, rect, Qt.KeepAspectRatio) return elif self.__data.style == QwtSymbol.SvgDocument: if self.__data.svg.renderer is not None: scaledRect = QRectF() sz = QSizeF(self.__data.svg.renderer.viewBoxF().size()) if not sz.isEmpty(): sz.scale(rect.size(), Qt.KeepAspectRatio) scaledRect.setSize(sz) scaledRect.moveCenter(rect.center()) else: scaledRect = rect self.__data.svg.renderer.render(painter, scaledRect) else: br = QRect(self.boundingRect()) ratio = min( [rect.width() / br.width(), rect.height() / br.height()]) painter.save() painter.translate(rect.center()) painter.scale(ratio, ratio) isPinPointEnabled = self.__data.isPinPointEnabled self.__data.isPinPointEnabled = False pos = QPointF() self.renderSymbols(painter, pos, 1) self.__data.isPinPointEnabled = isPinPointEnabled painter.restore()
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 drawSymbol(self, painter, point_or_rect): """ Draw the symbol into a rectangle The symbol is painted centered and scaled into the target rectangle. It is always painted uncached and the pin point is ignored. This method is primarily intended for drawing a symbol to the legend. :param QPainter painter: Painter :param point_or_rect: Position or target rectangle of the symbol in screen coordinates :type point_or_rect: QPointF or QPoint or QRectF """ if isinstance(point_or_rect, (QPointF, QPoint)): # drawSymbol( QPainter *, const QPointF & ) self.drawSymbols(painter, [point_or_rect]) return # drawSymbol( QPainter *, const QRectF & ) rect = point_or_rect assert isinstance(rect, QRectF) if self.__data.style == QwtSymbol.NoSymbol: return if self.__data.style == QwtSymbol.Graphic: self.__data.graphic.graphic.render(painter, rect, Qt.KeepAspectRatio) elif self.__data.style == QwtSymbol.Path: if self.__data.path.graphic.isNull(): self.__data.path.graphic = qwtPathGraphic( self.__data.path.path, self.__data.pen, self.__data.brush) self.__data.path.graphic.render(painter, rect, Qt.KeepAspectRatio) return elif self.__data.style == QwtSymbol.SvgDocument: if self.__data.svg.renderer is not None: scaledRect = QRectF() sz = QSizeF(self.__data.svg.renderer.viewBoxF().size()) if not sz.isEmpty(): sz.scale(rect.size(), Qt.KeepAspectRatio) scaledRect.setSize(sz) scaledRect.moveCenter(rect.center()) else: scaledRect = rect self.__data.svg.renderer.render(painter, scaledRect) else: br = QRect(self.boundingRect()) ratio = min([rect.width()/br.width(), rect.height()/br.height()]) painter.save() painter.translate(rect.center()) painter.scale(ratio, ratio) isPinPointEnabled = self.__data.isPinPointEnabled self.__data.isPinPointEnabled = False pos = QPointF() self.renderSymbols(painter, pos, 1) self.__data.isPinPointEnabled = isPinPointEnabled painter.restore()
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 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 exportTo(self, plot, documentname, sizeMM=None, resolution=85): if plot is None: return if sizeMM is None: sizeMM = QSizeF(300, 200) filename = documentname imageFormats = QImageWriter.supportedImageFormats() filter_ = [ "PDF documents (*.pdf)", "SVG documents (*.svg)", "Postscript documents (*.ps)" ] if imageFormats: imageFilter = "Images" imageFilter += " (" for idx, fmt in enumerate(imageFormats): if idx > 0: imageFilter += " " imageFilter += "*." + str(fmt) imageFilter += ")" filter_ += [imageFilter] filename, _s = getsavefilename( plot, "Export File Name", filename, ";;".join(filter_), options=QFileDialog.DontConfirmOverwrite) if not filename: return False self.renderDocument(plot, filename, sizeMM, resolution) return True
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 drawSymbol(self, painter, point_or_rect): if isinstance(point_or_rect, (QPointF, QPoint)): # drawSymbol( QPainter *, const QPointF & ) self.drawSymbols(painter, [point_or_rect], 1) return # drawSymbol( QPainter *, const QRectF & ) rect = point_or_rect assert isinstance(rect, QRectF) if self.__data.style == QwtSymbol.NoSymbol: return if self.__data.style == QwtSymbol.Graphic: self.__data.graphic.graphic.render(painter, rect, Qt.KeepAspectRatio) elif self.__data.style == QwtSymbol.Path: if self.__data.path.graphic.isNull(): self.__data.path.graphic = qwtPathGraphic( self.__data.path.path, self.__data.pen, self.__data.brush) self.__data.path.graphic.render(painter, rect, Qt.KeepAspectRatio) return elif self.__data.style == QwtSymbol.SvgDocument: if self.__data.svg.renderer is not None: scaledRect = QRectF() sz = QSizeF(self.__data.svg.renderer.viewBoxF().size()) if not sz.isEmpty(): sz.scale(rect.size(), Qt.KeepAspectRatio) scaledRect.setSize(sz) scaledRect.moveCenter(rect.center()) else: scaledRect = rect self.__data.svg.renderer.render(painter, scaledRect) else: br = QRect(self.boundingRect()) ratio = min([rect.width()/br.width(), rect.height()/br.height()]) painter.save() painter.translate(rect.center()) painter.scale(ratio, ratio) isPinPointEnabled = self.__data.isPinPointEnabled self.__data.isPinPointEnabled = False pos = QPointF() self.renderSymbols(painter, pos, 1) self.__data.isPinPointEnabled = isPinPointEnabled painter.restore()
def heightForWidth(self, font, flags, text, width): """ Find the height for a given width :param QFont font: Font of the text :param int flags: Bitwise OR of the flags used like in QPainter::drawText :param str text: Text to be rendered :param float width: Width :return: Calculated height """ doc = QwtRichTextDocument(text, flags, font) doc.setPageSize(QSizeF(width, QWIDGETSIZE_MAX)) return doc.documentLayout().documentSize().height()
def textSize(self, defaultFont): font = QFont(self.usedFont(defaultFont), self.desktopwidget) if not self.__layoutCache.textSize.isValid() or\ self.__layoutCache.font is not font: self.__layoutCache.textSize =\ self.__data.textEngine.textSize(font, self.__data.renderFlags, self.__data.text) self.__layoutCache.font = font sz = self.__layoutCache.textSize if self.__data.layoutAttributes & self.MinimumLayout: (left, right, top, bottom) = self.__data.textEngine.textMargins(font) sz -= QSizeF(left + right, top + bottom) return sz
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 minimumSizeHint(self): sz = self.__data.text.textSize(self.font()) mw = 2 * (self.frameWidth() + self.__data.margin) mh = mw indent = self.__data.indent if indent <= 0: indent = self.defaultIndent() if indent > 0: align = self.__data.text.renderFlags() if align & Qt.AlignLeft or align & Qt.AlignRight: mw += self.__data.indent elif align & Qt.AlignTop or align & Qt.AlignBottom: mh += self.__data.indent sz += QSizeF(mw, mh) return QSize(np.ceil(sz.width()), np.ceil(sz.height()))
def setDefaultSize(self, size): """ The default size is used in all methods rendering the graphic, where no size is explicitly specified. Assigning an empty size means, that the default size will be calculated from the bounding rectangle. :param QSizeF size: Default size .. seealso:: :py:meth:`defaultSize()`, :py:meth:`boundingRect()` """ w = max([0., size.width()]) h = max([0., size.height()]) self.__data.defaultSize = QSizeF(w, h)
def exportTo(self, plot, documentname, sizeMM=None, resolution=85): """ Execute a file dialog and render the plot to the selected file :param qwt.plot.QwtPlot plot: Plot widget :param str documentName: Default document name :param QSizeF sizeMM: Size for the document in millimeters :param int resolution: Resolution in dots per Inch (dpi) :return: True, when exporting was successful .. seealso:: :py:meth:`renderDocument()` """ if plot is None: return if sizeMM is None: sizeMM = QSizeF(300, 200) filename = documentname imageFormats = QImageWriter.supportedImageFormats() filter_ = [ "PDF documents (*.pdf)", "SVG documents (*.svg)", "Postscript documents (*.ps)" ] if imageFormats: imageFilter = "Images" imageFilter += " (" for idx, fmt in enumerate(imageFormats): if idx > 0: imageFilter += " " imageFilter += "*." + str(fmt) imageFilter += ")" filter_ += [imageFilter] filename, _s = getsavefilename( plot, "Export File Name", filename, ";;".join(filter_), options=QFileDialog.DontConfirmOverwrite) if not filename: return False self.renderDocument(plot, filename, sizeMM, resolution) return True
def textSize(self, defaultFont): """ Returns the size, that is needed to render text :param QFont defaultFont Font, used for the calculation if the text has no font :return: Caluclated size """ font = QFont(self.usedFont(defaultFont), self._desktopwidget) if not self.__layoutCache.textSize.isValid() or\ self.__layoutCache.font is not font: self.__layoutCache.textSize =\ self.__data.textEngine.textSize(font, self.__data.renderFlags, self.__data.text) self.__layoutCache.font = font sz = self.__layoutCache.textSize if self.__data.layoutAttributes & self.MinimumLayout: (left, right, top, bottom) = self.__data.textEngine.textMargins(font) sz -= QSizeF(left + right, top + bottom) return sz
def qwtDrawSvgSymbols(painter, points, numPoints, renderer, symbol): if renderer is None or not renderer.isValid(): return viewBox = QRectF(renderer.viewBoxF()) if viewBox.isEmpty(): return sz = QSizeF(symbol.size()) if not sz.isValid(): sz = viewBox.size() sx = sz.width() / viewBox.width() sy = sz.height() / viewBox.height() pinPoint = QPointF(viewBox.center()) if symbol.isPinPointEnabled(): pinPoint = symbol.pinPoint() dx = sx * (pinPoint.x() - viewBox.left()) dy = sy * (pinPoint.y() - viewBox.top()) for pos in points: x = pos.x() - dx y = pos.y() - dy renderer.render(painter, QRectF(x, y, sz.width(), sz.height()))
def draw(self, painter, rect, flags, text): """ Draw the text in a clipping rectangle :param QPainter painter: Painter :param QRectF rect: Clipping rectangle :param int flags: Bitwise OR of the flags like in for QPainter::drawText() :param str text: Text to be rendered """ txt = QwtRichTextDocument(text, flags, painter.font()) 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 qwtDrawSvgSymbols(painter, points, numPoints, renderer, symbol): if renderer is None or not renderer.isValid(): return viewBox = QRectF(renderer.viewBoxF()) if viewBox.isEmpty(): return sz = QSizeF(symbol.size()) if not sz.isValid(): sz = viewBox.size() sx = sz.width()/viewBox.width() sy = sz.height()/viewBox.height() pinPoint = QPointF(viewBox.center()) if symbol.isPinPointEnabled(): pinPoint = symbol.pinPoint() dx = sx*(pinPoint.x()-viewBox.left()) dy = sy*(pinPoint.y()-viewBox.top()) for pos in points: x = pos.x()-dx y = pos.y()-dy renderer.render(painter, QRectF(x, y, sz.width(), sz.height()))
def boundingRect(self): """ Calculate the bounding rectangle for a symbol at position (0,0). :return: Bounding rectangle """ rect = QRectF() pinPointTranslation = False if self.__data.style in (QwtSymbol.Ellipse, QwtSymbol.Rect, QwtSymbol.Hexagon): pw = 0. if self.__data.pen.style() != Qt.NoPen: pw = max([self.__data.pen.widthF(), 1.]) rect.setSize(self.__data.size+QSizeF(pw, pw)) rect.moveCenter(QPointF(0., 0.)) elif self.__data.style in (QwtSymbol.XCross, QwtSymbol.Diamond, QwtSymbol.Triangle, QwtSymbol.UTriangle, QwtSymbol.DTriangle, QwtSymbol.RTriangle, QwtSymbol.LTriangle, QwtSymbol.Star1, QwtSymbol.Star2): pw = 0. if self.__data.pen.style() != Qt.NoPen: pw = max([self.__data.pen.widthF(), 1.]) rect.setSize(QSizeF(self.__data.size)+QSizeF(2*pw, 2*pw)) rect.moveCenter(QPointF(0., 0.)) elif self.__data.style == QwtSymbol.Path: if self.__data.path.graphic.isNull(): self.__data.path.graphic = qwtPathGraphic( self.__data.path.path, self.__data.pen, self.__data.brush) rect = qwtScaleBoundingRect(self.__data.path.graphic, self.__data.size) pinPointTranslation = True elif self.__data.style == QwtSymbol.Pixmap: if self.__data.size.isEmpty(): rect.setSize(self.__data.pixmap.pixmap.size()) else: rect.setSize(self.__data.size) pinPointTranslation = True elif self.__data.style == QwtSymbol.Graphic: rect = qwtScaleBoundingRect(self.__data.graphic.graphic, self.__data.size) pinPointTranslation = True elif self.__data.style == QwtSymbol.SvgDocument: if self.__data.svg.renderer is not None: rect = self.__data.svg.renderer.viewBoxF() if self.__data.size.isValid() and not rect.isEmpty(): sz = QSizeF(rect.size()) sx = self.__data.size.width()/sz.width() sy = self.__data.size.height()/sz.height() transform = QTransform() transform.scale(sx, sy) rect = transform.mapRect(rect) pinPointTranslation = True else: rect.setSize(self.__data.size) rect.moveCenter(QPointF(0., 0.)) if pinPointTranslation: pinPoint = QPointF(0., 0.) if self.__data.isPinPointEnabled: pinPoint = rect.center()-self.__data.pinPoint rect.moveCenter(pinPoint) r = QRect() r.setLeft(np.floor(rect.left())) r.setTop(np.floor(rect.top())) r.setRight(np.floor(rect.right())) r.setBottom(np.floor(rect.bottom())) if self.__data.style != QwtSymbol.Pixmap: r.adjust(-1, -1, 1, 1) return r
def drawLabel(self, painter, canvasRect, pos): """ Align and draw the text label of the marker :param QPainter painter: Painter :param QRectF canvasRect: Contents rectangle of the canvas in painter coordinates :param QPointF pos: Position of the marker, translated into widget coordinates .. seealso:: :py:meth:`drawLabel()`, :py:meth:`qwt.symbol.QwtSymbol.drawSymbol()` """ if self.__data.label.isEmpty(): return align = Qt.Alignment(self.__data.labelAlignment) alignPos = QPointF(pos) symbolOff = QSizeF(0, 0) if self.__data.style == QwtPlotMarker.VLine: # In VLine-style the y-position is pointless and # the alignment flags are relative to the canvas if bool(self.__data.labelAlignment & Qt.AlignTop): alignPos.setY(canvasRect.top()) align &= ~Qt.AlignTop align |= Qt.AlignBottom elif bool(self.__data.labelAlignment & Qt.AlignBottom): # In HLine-style the x-position is pointless and # the alignment flags are relative to the canvas alignPos.setY(canvasRect.bottom() - 1) align &= ~Qt.AlignBottom align |= Qt.AlignTop else: alignPos.setY(canvasRect.center().y()) elif self.__data.style == QwtPlotMarker.HLine: if bool(self.__data.labelAlignment & Qt.AlignLeft): alignPos.setX(canvasRect.left()) align &= ~Qt.AlignLeft align |= Qt.AlignRight elif bool(self.__data.labelAlignment & Qt.AlignRight): alignPos.setX(canvasRect.right() - 1) align &= ~Qt.AlignRight align |= Qt.AlignLeft else: alignPos.setX(canvasRect.center().x()) else: if self.__data.symbol and\ self.__data.symbol.style() != QwtSymbol.NoSymbol: symbolOff = self.__data.symbol.size() + QSizeF(1, 1) symbolOff /= 2 pw2 = self.__data.pen.widthF() / 2. if pw2 == 0.: pw2 = .5 spacing = self.__data.spacing xOff = max([pw2, symbolOff.width()]) yOff = max([pw2, symbolOff.height()]) textSize = self.__data.label.textSize(painter.font()) if align & Qt.AlignLeft: alignPos.setX(alignPos.x() - (xOff + spacing)) if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height()) else: alignPos.setX(alignPos.x() - textSize.width()) elif align & Qt.AlignRight: alignPos.setX(alignPos.x() + xOff + spacing) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height() / 2) else: alignPos.setX(alignPos.x() - textSize.width() / 2) if align & Qt.AlignTop: alignPos.setY(alignPos.y() - (yOff + spacing)) if self.__data.labelOrientation != Qt.Vertical: alignPos.setY(alignPos.y() - textSize.height()) elif align & Qt.AlignBottom: alignPos.setY(alignPos.y() + yOff + spacing) if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width()) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width() / 2) else: alignPos.setY(alignPos.y() - textSize.height() / 2) painter.translate(alignPos.x(), alignPos.y()) if self.__data.labelOrientation == Qt.Vertical: painter.rotate(-90.) textRect = QRectF(0, 0, textSize.width(), textSize.height()) self.__data.label.draw(painter, textRect)
def boundingRect(self): """ Calculate the bounding rectangle for a symbol at position (0,0). :return: Bounding rectangle """ rect = QRectF() pinPointTranslation = False if self.__data.style in (QwtSymbol.Ellipse, QwtSymbol.Rect, QwtSymbol.Hexagon): pw = 0. if self.__data.pen.style() != Qt.NoPen: pw = max([self.__data.pen.widthF(), 1.]) rect.setSize(self.__data.size + QSizeF(pw, pw)) rect.moveCenter(QPointF(0., 0.)) elif self.__data.style in (QwtSymbol.XCross, QwtSymbol.Diamond, QwtSymbol.Triangle, QwtSymbol.UTriangle, QwtSymbol.DTriangle, QwtSymbol.RTriangle, QwtSymbol.LTriangle, QwtSymbol.Star1, QwtSymbol.Star2): pw = 0. if self.__data.pen.style() != Qt.NoPen: pw = max([self.__data.pen.widthF(), 1.]) rect.setSize(QSizeF(self.__data.size) + QSizeF(2 * pw, 2 * pw)) rect.moveCenter(QPointF(0., 0.)) elif self.__data.style == QwtSymbol.Path: if self.__data.path.graphic.isNull(): self.__data.path.graphic = qwtPathGraphic( self.__data.path.path, self.__data.pen, self.__data.brush) rect = qwtScaleBoundingRect(self.__data.path.graphic, self.__data.size) pinPointTranslation = True elif self.__data.style == QwtSymbol.Pixmap: if self.__data.size.isEmpty(): rect.setSize(self.__data.pixmap.pixmap.size()) else: rect.setSize(self.__data.size) pinPointTranslation = True elif self.__data.style == QwtSymbol.Graphic: rect = qwtScaleBoundingRect(self.__data.graphic.graphic, self.__data.size) pinPointTranslation = True elif self.__data.style == QwtSymbol.SvgDocument: if self.__data.svg.renderer is not None: rect = self.__data.svg.renderer.viewBoxF() if self.__data.size.isValid() and not rect.isEmpty(): sz = QSizeF(rect.size()) sx = self.__data.size.width() / sz.width() sy = self.__data.size.height() / sz.height() transform = QTransform() transform.scale(sx, sy) rect = transform.mapRect(rect) pinPointTranslation = True else: rect.setSize(self.__data.size) rect.moveCenter(QPointF(0., 0.)) if pinPointTranslation: pinPoint = QPointF(0., 0.) if self.__data.isPinPointEnabled: pinPoint = rect.center() - self.__data.pinPoint rect.moveCenter(pinPoint) r = QRect() r.setLeft(np.floor(rect.left())) r.setTop(np.floor(rect.top())) r.setRight(np.floor(rect.right())) r.setBottom(np.floor(rect.bottom())) if self.__data.style != QwtSymbol.Pixmap: r.adjust(-1, -1, 1, 1) return r
def drawLabel(self, painter, canvasRect, pos): if self.__data.label.isEmpty(): return align = Qt.Alignment(self.__data.labelAlignment) alignPos = QPointF(pos) symbolOff = QSizeF(0, 0) if self.__data.style == QwtPlotMarker.VLine: if bool(self.__data.labelAlignment & Qt.AlignTop): alignPos.setY(canvasRect.top()) align &= ~Qt.AlignTop align |= Qt.AlignBottom elif bool(self.__data.labelAlignment & Qt.AlignBottom): alignPos.setY(canvasRect.bottom() - 1) align &= ~Qt.AlignBottom align |= Qt.AlignTop else: alignPos.setY(canvasRect.center().y()) elif self.__data.style == QwtPlotMarker.HLine: if bool(self.__data.labelAlignment & Qt.AlignLeft): alignPos.setX(canvasRect.left()) align &= ~Qt.AlignLeft align |= Qt.AlignRight elif bool(self.__data.labelAlignment & Qt.AlignRight): alignPos.setX(canvasRect.right() - 1) align &= ~Qt.AlignRight align |= Qt.AlignLeft else: alignPos.setX(canvasRect.center().x()) else: if self.__data.symbol and\ self.__data.symbol.style() != QwtSymbol.NoSymbol: symbolOff = self.__data.symbol.size() + QSizeF(1, 1) symbolOff /= 2 pw2 = self.__data.pen.widthF() / 2. if pw2 == 0.: pw2 = .5 spacing = self.__data.spacing xOff = max([pw2, symbolOff.width()]) yOff = max([pw2, symbolOff.height()]) textSize = self.__data.label.textSize(painter.font()) if align & Qt.AlignLeft: alignPos.setX(alignPos.x() - (xOff + spacing)) if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height()) else: alignPos.setX(alignPos.x() - textSize.width()) elif align & Qt.AlignRight: alignPos.setX(alignPos.x() + xOff + spacing) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height() / 2) else: alignPos.setX(alignPos.x() - textSize.width() / 2) if align & Qt.AlignTop: alignPos.setY(alignPos.y() - (yOff + spacing)) if self.__data.labelOrientation != Qt.Vertical: alignPos.setY(alignPos.y() - textSize.height()) elif align & Qt.AlignBottom: alignPos.setY(alignPos.y() + yOff + spacing) if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width()) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width() / 2) else: alignPos.setY(alignPos.y() - textSize.height() / 2) painter.translate(alignPos.x(), alignPos.y()) if self.__data.labelOrientation == Qt.Vertical: painter.rotate(-90.) textRect = QRectF(0, 0, textSize.width(), textSize.height()) self.__data.label.draw(painter, textRect)
def drawLabel(self, painter, canvasRect, pos): if self.__data.label.isEmpty(): return align = Qt.Alignment(self.__data.labelAlignment) alignPos = QPointF(pos) symbolOff = QSizeF(0, 0) if self.__data.style == QwtPlotMarker.VLine: if bool(self.__data.labelAlignment & Qt.AlignTop): alignPos.setY(canvasRect.top()) align &= ~Qt.AlignTop align |= Qt.AlignBottom elif bool(self.__data.labelAlignment & Qt.AlignBottom): alignPos.setY(canvasRect.bottom()-1) align &= ~Qt.AlignBottom align |= Qt.AlignTop else: alignPos.setY(canvasRect.center().y()) elif self.__data.style == QwtPlotMarker.HLine: if bool(self.__data.labelAlignment & Qt.AlignLeft): alignPos.setX(canvasRect.left()) align &= ~Qt.AlignLeft align |= Qt.AlignRight elif bool(self.__data.labelAlignment & Qt.AlignRight): alignPos.setX(canvasRect.right()-1) align &= ~Qt.AlignRight align |= Qt.AlignLeft else: alignPos.setX(canvasRect.center().x()) else: if self.__data.symbol and\ self.__data.symbol.style() != QwtSymbol.NoSymbol: symbolOff = self.__data.symbol.size()+QSizeF(1, 1) symbolOff /= 2 pw2 = self.__data.pen.widthF()/2. if pw2 == 0.: pw2 = .5 spacing = self.__data.spacing xOff = max([pw2, symbolOff.width()]) yOff = max([pw2, symbolOff.height()]) textSize = self.__data.label.textSize(painter.font()) if align & Qt.AlignLeft: alignPos.setX(alignPos.x()-(xOff+spacing)) if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x()-textSize.height()) else: alignPos.setX(alignPos.x()-textSize.width()) elif align & Qt.AlignRight: alignPos.setX(alignPos.x()+xOff+spacing) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x()-textSize.height()/2) else: alignPos.setX(alignPos.x()-textSize.width()/2) if align & Qt.AlignTop: alignPos.setY(alignPos.y()-(yOff+spacing)) if self.__data.labelOrientation != Qt.Vertical: alignPos.setY(alignPos.y()-textSize.height()) elif align & Qt.AlignBottom: alignPos.setY(alignPos.y()+yOff+spacing) if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y()+textSize.width()) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y()+textSize.width()/2) else: alignPos.setY(alignPos.y()-textSize.height()/2) painter.translate(alignPos.x(), alignPos.y()) if self.__data.labelOrientation == Qt.Vertical: painter.rotate(-90.) textRect = QRectF(0, 0, textSize.width(), textSize.height()) self.__data.label.draw(painter, textRect)
def setDefaultSize(self, size): w = max([0., size.width()]) h = max([0., size.height()]) self.__data.defaultSize = QSizeF(w, h)
def heightForWidth(self, font, flags, text, width): doc = QwtRichTextDocument(text, flags, font) doc.setPageSize(QSizeF(width, QWIDGETSIZE_MAX)) return doc.documentLayout().documentSize().height()
def drawLabel(self, painter, canvasRect, pos): """ Align and draw the text label of the marker :param QPainter painter: Painter :param QRectF canvasRect: Contents rectangle of the canvas in painter coordinates :param QPointF pos: Position of the marker, translated into widget coordinates .. seealso:: :py:meth:`drawLabel()`, :py:meth:`qwt.symbol.QwtSymbol.drawSymbol()` """ if self.__data.label.isEmpty(): return align = Qt.Alignment(self.__data.labelAlignment) alignPos = QPointF(pos) symbolOff = QSizeF(0, 0) if self.__data.style == QwtPlotMarker.VLine: # In VLine-style the y-position is pointless and # the alignment flags are relative to the canvas if bool(self.__data.labelAlignment & Qt.AlignTop): alignPos.setY(canvasRect.top()) align &= ~Qt.AlignTop align |= Qt.AlignBottom elif bool(self.__data.labelAlignment & Qt.AlignBottom): # In HLine-style the x-position is pointless and # the alignment flags are relative to the canvas alignPos.setY(canvasRect.bottom()-1) align &= ~Qt.AlignBottom align |= Qt.AlignTop else: alignPos.setY(canvasRect.center().y()) elif self.__data.style == QwtPlotMarker.HLine: if bool(self.__data.labelAlignment & Qt.AlignLeft): alignPos.setX(canvasRect.left()) align &= ~Qt.AlignLeft align |= Qt.AlignRight elif bool(self.__data.labelAlignment & Qt.AlignRight): alignPos.setX(canvasRect.right()-1) align &= ~Qt.AlignRight align |= Qt.AlignLeft else: alignPos.setX(canvasRect.center().x()) else: if self.__data.symbol and\ self.__data.symbol.style() != QwtSymbol.NoSymbol: symbolOff = self.__data.symbol.size()+QSizeF(1, 1) symbolOff /= 2 pw2 = self.__data.pen.widthF()/2. if pw2 == 0.: pw2 = .5 spacing = self.__data.spacing xOff = max([pw2, symbolOff.width()]) yOff = max([pw2, symbolOff.height()]) textSize = self.__data.label.textSize(painter.font()) if align & Qt.AlignLeft: alignPos.setX(alignPos.x()-(xOff+spacing)) if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x()-textSize.height()) else: alignPos.setX(alignPos.x()-textSize.width()) elif align & Qt.AlignRight: alignPos.setX(alignPos.x()+xOff+spacing) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x()-textSize.height()/2) else: alignPos.setX(alignPos.x()-textSize.width()/2) if align & Qt.AlignTop: alignPos.setY(alignPos.y()-(yOff+spacing)) if self.__data.labelOrientation != Qt.Vertical: alignPos.setY(alignPos.y()-textSize.height()) elif align & Qt.AlignBottom: alignPos.setY(alignPos.y()+yOff+spacing) if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y()+textSize.width()) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y()+textSize.width()/2) else: alignPos.setY(alignPos.y()-textSize.height()/2) painter.translate(alignPos.x(), alignPos.y()) if self.__data.labelOrientation == Qt.Vertical: painter.rotate(-90.) textRect = QRectF(0, 0, textSize.width(), textSize.height()) self.__data.label.draw(painter, textRect)
def __init__(self): self.textSize = QSizeF() self.font = None
def reset(self): 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 invalidate(self): self.textSize = QSizeF()