def drawColorBar(self, painter, rect): h1, s1, v1, _ = self.__light.getHsv() h2, s2, v2, _ = self.__dark.getHsv() painter.save() painter.setClipRect(rect) painter.setClipping(True) painter.fillRect(rect, QBrush(self.__dark)) sectionSize = 2 if self.__orientation == Qt.Horizontal: numIntervals = rect.width() / sectionSize else: numIntervals = rect.height() / sectionSize section = QRect() for i in range(int(numIntervals)): if self.__orientation == Qt.Horizontal: section.setRect(rect.x() + i * sectionSize, rect.y(), sectionSize, rect.heigh()) else: section.setRect(rect.x(), rect.y() + i * sectionSize, rect.width(), sectionSize) ratio = float(i) / float(numIntervals) color = QColor() color.setHsv( h1 + int(ratio * (h2 - h1) + 0.5), s1 + int(ratio * (s2 - s1) + 0.5), v1 + int(ratio * (v2 - v1) + 0.5) ) painter.fillRect(section, color) painter.restore()
def drawPoints(self, painter, points, pointCount): deviceClipping, clipRect = qwtIsClippingNeeded(painter) if isinstance(points[0], QPointF): if deviceClipping: clippedPolygon = QPolygonF(pointCount) clippedData = clippedPolygon.data() numClippedPoints = 0 for point in points: if clipRect.contains(point): clippedData[numClippedPoints] = point numClippedPoints += 1 painter.drawPoints(clippedData, numClippedPoints) else: painter.drawPoints(points, pointCount) else: if deviceClipping: minX = np.ceil(clipRect.left()) maxX = np.floor(clipRect.right()) minY = np.ceil(clipRect.top()) maxY = np.floor(clipRect.bottom()) r = QRect(minX, minY, maxX-minX, maxY-minY) clippedPolygon = QPolygon(pointCount) clippedData = clippedPolygon.data() numClippedPoints = 0 for point in points: if r.contains(point): clippedData[numClippedPoints] = point numClippedPoints += 1 painter.drawPoints(clippedData, numClippedPoints) else: painter.drawPoints(points, pointCount)
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 drawPoints(self, painter, points, pointCount): deviceClipping, clipRect = qwtIsClippingNeeded(painter) if isinstance(points[0], QPointF): if deviceClipping: clippedPolygon = QPolygonF(pointCount) clippedData = clippedPolygon.data() numClippedPoints = 0 for point in points: if clipRect.contains(point): clippedData[numClippedPoints] = point numClippedPoints += 1 painter.drawPoints(clippedData, numClippedPoints) else: painter.drawPoints(points, pointCount) else: if deviceClipping: minX = np.ceil(clipRect.left()) maxX = np.floor(clipRect.right()) minY = np.ceil(clipRect.top()) maxY = np.floor(clipRect.bottom()) r = QRect(minX, minY, maxX - minX, maxY - minY) clippedPolygon = QPolygon(pointCount) clippedData = clippedPolygon.data() numClippedPoints = 0 for point in points: if r.contains(point): clippedData[numClippedPoints] = point numClippedPoints += 1 painter.drawPoints(clippedData, numClippedPoints) else: painter.drawPoints(points, pointCount)
def renderLegend(self, painter, rect, fillBackground): if self.__data.itemMap.isEmpty(): return if fillBackground: if self.autoFillBackground() or\ self.testAttribute(Qt.WA_StyledBackground): QwtPainter.drawBackground(painter, rect, self) # const QwtDynGridLayout *legendLayout = # qobject_cast<QwtDynGridLayout *>( contentsWidget()->layout() ); #TODO: not the exact same implementation legendLayout = self.__data.view.contentsWidget.layout() if legendLayout is None: return left, right, top, bottom = self.getContentsMargins() layoutRect = QRect() layoutRect.setLeft(np.ceil(rect.left())+left) layoutRect.setTop(np.ceil(rect.top())+top) layoutRect.setRight(np.ceil(rect.right())-right) layoutRect.setBottom(np.ceil(rect.bottom())-bottom) numCols = legendLayout.columnsForWidth(layoutRect.width()) itemRects = legendLayout.layoutItems(layoutRect, numCols) index = 0 for i in range(legendLayout.count()): item = legendLayout.itemAt(i) w = item.widget() if w is not None: painter.save() painter.setClipRect(itemRects[index], Qt.IntersectClip) self.renderItem(painter, w, itemRects[index], fillBackground) index += 1 painter.restore()
def draw(self, painter, xMap, yMap, rect): iData = self.data() painter.setPen(self.color()) x0 = xMap.transform(self.baseline()) y0 = yMap.transform(self.baseline()) for i in range(iData.size()): if self.testHistogramAttribute(HistogramItem.Xfy): x2 = xMap.transform(iData.sample(i).value) if x2 == x0: continue y1 = yMap.transform(iData.sample(i).interval.minValue()) y2 = yMap.transform(iData.sample(i).interval.maxValue()) if y1 > y2: y1, y2 = y2, y1 if i < iData.size() - 2: yy1 = yMap.transform( iData.sample(i + 1).interval.minValue()) yy2 = yMap.transform( iData.sample(i + 1).interval.maxValue()) if y2 == min(yy1, yy2): xx2 = xMap.transform( iData.sample(i + 1).interval.minValue()) if xx2 != x0 and ((xx2 < x0 and x2 < x0) or (xx2 > x0 and x2 > x0)): # One pixel distance between neighboured bars y2 += 1 self.drawBar(painter, Qt.Horizontal, QRect(x0, y1, x2 - x0, y2 - y1)) else: y2 = yMap.transform(iData.sample(i).value) if y2 == y0: continue x1 = xMap.transform(iData.sample(i).interval.minValue()) x2 = xMap.transform(iData.sample(i).interval.maxValue()) if x1 > x2: x1, x2 = x2, x1 if i < iData.size() - 2: xx1 = xMap.transform( iData.sample(i + 1).interval.minValue()) xx2 = xMap.transform( iData.sample(i + 1).interval.maxValue()) x2 = min(xx1, xx2) yy2 = yMap.transform(iData.sample(i + 1).value) if x2 == min(xx1, xx2): if yy2 != 0 and ((yy2 < y0 and y2 < y0) or (yy2 > y0 and y2 > y0)): # One pixel distance between neighboured bars x2 -= 1 self.drawBar(painter, Qt.Vertical, QRect(x1, y0, x2 - x1, y2 - y0))
def boundingLabelRect(self, font, value): lbl = self.tickLabel(font, value) if lbl.isEmpty(): return QRect() pos = self.labelPosition(value) labelSize = lbl.textSize(font) transform = self.labelTransformation(pos, labelSize) return transform.mapRect(QRect(QPoint(0, 0), labelSize.toSize()))
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 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 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 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 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 qwtCanvasClip(canvas, canvasRect): x1 = np.ceil(canvasRect.left()) x2 = np.floor(canvasRect.right()) y1 = np.ceil(canvasRect.top()) y2 = np.floor(canvasRect.bottom()) r = QRect(x1, y1, x2 - x1 - 1, y2 - y1 - 1) return canvas.borderPath(r)
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 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 __scaleRect(self, scale): bld = scale.margin() mjt = scale.scaleDraw().tickLength(QwtScaleDiv.MajorTick) sbd = scale.startBorderDist() ebd = scale.endBorderDist() if scale.alignment() == QwtScaleDraw.LeftScale: return QRect(scale.width() - bld - mjt, sbd, mjt, scale.height() - sbd - ebd) elif scale.alignment() == QwtScaleDraw.RightScale: return QRect(bld, sbd,mjt, scale.height() - sbd - ebd) elif scale.alignment() == QwtScaleDraw.BottomScale: return QRect(sbd, bld, scale.width() - sbd - ebd, mjt) elif scale.alignment() == QwtScaleDraw.TopScale: return QRect(sbd, scale.height() - bld - mjt, scale.width() - sbd - ebd, mjt) else: return QRect()
def qwtDrawPixmapSymbols(painter, points, numPoints, symbol): size = symbol.size() if size.isEmpty(): size = symbol.pixmap().size() transform = QTransform(painter.transform()) if transform.isScaling(): r = QRect(0, 0, size.width(), size.height()) size = transform.mapRect(r).size() pm = QPixmap(symbol.pixmap()) if pm.size() != size: pm = pm.scaled(size) pinPoint = QPointF(.5 * size.width(), .5 * size.height()) if symbol.isPinPointEnabled(): pinPoint = symbol.pinPoint() painter.resetTransform() for pos in points: pos = QPointF(transform.map(pos)) - pinPoint QwtPainter.drawPixmap(painter, QRect(pos.toPoint(), pm.size(), pm))
def draw(self, painter, xMap, yMap, rect): margin = 5 pieRect = QRect() pieRect.setX(rect.x() + margin) pieRect.setY(rect.y() + margin) pieRect.setHeight(yMap.transform(80.0)) pieRect.setWidth(pieRect.height()) angle = 3*5760/4 for key in ["User", "System", "Idle"]: curve = self.plot().cpuPlotCurve(key) if curve.dataSize(): value = int(5760*curve.sample(0).y()/100.0) painter.save() painter.setBrush(QBrush(curve.pen().color(), Qt.SolidPattern)) painter.drawPie(pieRect, -angle, -value) painter.restore() angle += value
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 drawLabel(self, painter, value): lbl = self.tickLabel(painter.font(), value) if lbl is None or lbl.isEmpty(): return pos = self.labelPosition(value) labelSize = lbl.textSize(painter.font()) transform = self.labelTransformation(pos, labelSize) painter.save() painter.setWorldTransform(transform, True) lbl.draw(painter, QRect(QPoint(0, 0), labelSize.toSize())) painter.restore()
def qwtCanvasClip(canvas, canvasRect): """ The clip region is calculated in integers To avoid too much rounding errors better calculate it in target device resolution """ x1 = np.ceil(canvasRect.left()) x2 = np.floor(canvasRect.right()) y1 = np.ceil(canvasRect.top()) y2 = np.floor(canvasRect.bottom()) r = QRect(x1, y1, x2 - x1 - 1, y2 - y1 - 1) return canvas.borderPath(r)
def drawColorBar(self, painter, rect): h1, s1, v1, _ = self.__light.getHsv() h2, s2, v2, _ = self.__dark.getHsv() painter.save() painter.setClipRect(rect) painter.setClipping(True) painter.fillRect(rect, QBrush(self.__dark)) sectionSize = 2 if (self.__orientation == Qt.Horizontal): numIntervals = rect.width()/sectionSize else: numIntervals = rect.height()/sectionSize section = QRect() for i in range(int(numIntervals)): if self.__orientation == Qt.Horizontal: section.setRect(rect.x() + i*sectionSize, rect.y(), sectionSize, rect.heigh()) else: section.setRect(rect.x(), rect.y() + i*sectionSize, rect.width(), sectionSize) ratio = float(i)/float(numIntervals) color = QColor() color.setHsv(h1 + int(ratio*(h2-h1) + 0.5), s1 + int(ratio*(s2-s1) + 0.5), v1 + int(ratio*(v2-v1) + 0.5)) painter.fillRect(section, color) painter.restore()
def boundingLabelRect(self, font, value): """ Find the bounding rectangle for the label. The coordinates of the rectangle are absolute (calculated from `pos()`) in direction of the tick. :param QFont font: Font used for painting :param float value: Value :return: Bounding rectangle .. seealso:: :py:meth:`labelRect()` """ lbl = self.tickLabel(font, value) if lbl.isEmpty(): return QRect() pos = self.labelPosition(value) labelSize = lbl.textSize(font) transform = self.labelTransformation(pos, labelSize) return transform.mapRect(QRect(QPoint(0, 0), labelSize.toSize()))
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 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 draw(self, painter, xMap, yMap, rect): c = QColor(Qt.white) r = QRect(rect) for i in range(100, 0, -10): r.setBottom(yMap.transform(i - 10)) r.setTop(yMap.transform(i)) painter.fillRect(r, c) c = c.darker(110)
def clipPolygon(self, clipRect, polygon, closePolygon): raise NotImplementedError( "Nearly impossible to implement in pure Python") #XXX: the only viable option would be to use Qt's intersected method # but unfortunately it's closing systematically the output polygon # (how to test it: polygon.intersected(QPolygonF(clipRect))) if isinstance(clipRect, QRectF): minX = np.ceil(clipRect.left()) maxX = np.floor(clipRect.right()) minY = np.ceil(clipRect.top()) maxY = np.floor(clipRect.bottom()) clipRect = QRect(minX, minY, maxX - minX, maxY - minY) clipper = QwtPolygonClipper(clipRect) return clipper.clipPolygon(polygon, closePolygon)
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 layoutItems(self, rect, numColumns): """ Calculate the geometries of the layout items for a layout with numColumns columns and a given rectangle. """ itemGeometries = [] if numColumns == 0 or self.isEmpty(): return itemGeometries numRows = int(self.itemCount()/numColumns) if numColumns % self.itemCount(): numRows += 1 if numRows == 0: return itemGeometries rowHeight = [0]*numRows colWidth = [0]*numColumns self.layoutGrid(numColumns, rowHeight, colWidth) expandH = self.expandingDirections() & Qt.Horizontal expandV = self.expandingDirections() & Qt.Vertical if expandH or expandV: self.stretchGrid(rect, numColumns, rowHeight, colWidth) maxColumns = self.__data.maxColumns self.__data.maxColumns = numColumns alignedRect = self.alignmentRect(rect) self.__data.maxColumns = maxColumns xOffset = 0 if expandH else alignedRect.x() yOffset = 0 if expandV else alignedRect.y() colX = [0] * numColumns rowY = [0] * numRows xySpace = self.spacing() margins = self.contentsMargins() rowY[0] = yOffset + margins.bottom() for r in range(1, numRows): rowY[r] = rowY[r-1] + rowHeight[r-1] + xySpace colX[0] = xOffset + margins.left() for c in range(1, numColumns): colX[c] = colX[c-1] + colWidth[c-1] + xySpace itemCount = len(self.__data.itemList) for i in range(itemCount): row = int(i/numColumns) col = i % numColumns itemGeometry = QRect(colX[col], rowY[row], colWidth[col], rowHeight[row]) itemGeometries.append(itemGeometry) return itemGeometries
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 drawLabel(self, painter, value): """ Draws the label for a major scale tick :param QPainter painter: Painter :param float value: Value .. seealso:: :py:meth:`drawTick()`, :py:meth:`drawBackbone()`, :py:meth:`boundingLabelRect()` """ lbl = self.tickLabel(painter.font(), value) if lbl is None or lbl.isEmpty(): return pos = self.labelPosition(value) labelSize = lbl.textSize(painter.font()) transform = self.labelTransformation(pos, labelSize) painter.save() painter.setWorldTransform(transform, True) lbl.draw(painter, QRect(QPoint(0, 0), labelSize.toSize())) painter.restore()
def drawPoints(self, painter, *args): if len(args) == 2: points, pointCount = args else: polygon, = args points, pointCount = polygon.data(), polygon.size() if isinstance(polygon, QPolygonF): points.setsize(pointCount * np.finfo(float).dtype.itemsize) else: points.setsize(pointCount * np.iinfo(int).dtype.itemsize) deviceClipping, clipRect = qwtIsClippingNeeded(painter) if deviceClipping: if isinstance(polygon, QPointF): minX = np.ceil(clipRect.left()) maxX = np.floor(clipRect.right()) minY = np.ceil(clipRect.top()) maxY = np.floor(clipRect.bottom()) clipRect = QRect(minX, minY, maxX - minX, maxY - minY) clippedPolygon = polygon.intersected(QPolygon(clipRect)) painter.drawPoints(clippedPolygon) else: painter.drawPoints(polygon)
def legendIcon(self, index, size): 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() QwtPainter.drawLine(painter, 0., y, size.width(), y) if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross): x = .5 * size.width() QwtPainter.drawLine(painter, 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 paintEvent(self, e): cr = self.contentsRect() painter = QPainter(self) painter.setClipRegion(e.region()) if self.__data.isDown: qDrawWinButton(painter, 0, 0, self.width(), self.height(), self.palette(), True) painter.save() if self.__data.isDown: shiftSize = buttonShift(self) painter.translate(shiftSize.width(), shiftSize.height()) painter.setClipRect(cr) self.drawContents(painter) if not self.__data.icon.isNull(): iconRect = QRect(cr) iconRect.setX(iconRect.x() + self.margin()) if self.__data.itemMode != QwtLegendData.ReadOnly: iconRect.setX(iconRect.x() + BUTTONFRAME) iconRect.setSize(self.__data.icon.size()) iconRect.moveCenter(QPoint(iconRect.center().x(), cr.center().y())) painter.drawPixmap(iconRect, self.__data.icon) painter.restore()
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 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 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 paintEvent(self, e): cr = self.contentsRect() painter = QPainter(self) painter.setClipRegion(e.region()) if self.__data.isDown: qDrawWinButton(painter, 0, 0, self.width(), self.height(), self.palette(), True) painter.save() if self.__data.isDown: shiftSize = buttonShift(self) painter.translate(shiftSize.width(), shiftSize.height()) painter.setClipRect(cr) self.drawContents(painter) if not self.__data.icon.isNull(): iconRect = QRect(cr) iconRect.setX(iconRect.x()+self.margin()) if self.__data.itemMode != QwtLegendData.ReadOnly: iconRect.setX(iconRect.x()+BUTTONFRAME) iconRect.setSize(self.__data.icon.size()) iconRect.moveCenter(QPoint(iconRect.center().x(), cr.center().y())) painter.drawPixmap(iconRect, self.__data.icon) painter.restore()
def renderLegend(self, painter, rect, fillBackground): """ Render the legend into a given rectangle. :param QPainter painter: Painter :param QRectF rect: Bounding rectangle :param bool fillBackground: When true, fill rect with the widget background """ if self.__data.itemMap.isEmpty(): return if fillBackground: if self.autoFillBackground() or\ self.testAttribute(Qt.WA_StyledBackground): QwtPainter.drawBackground(painter, rect, self) # const QwtDynGridLayout *legendLayout = # qobject_cast<QwtDynGridLayout *>( contentsWidget()->layout() ); #TODO: not the exact same implementation legendLayout = self.__data.view.contentsWidget.layout() if legendLayout is None: return left, right, top, bottom = self.getContentsMargins() layoutRect = QRect() layoutRect.setLeft(np.ceil(rect.left())+left) layoutRect.setTop(np.ceil(rect.top())+top) layoutRect.setRight(np.ceil(rect.right())-right) layoutRect.setBottom(np.ceil(rect.bottom())-bottom) numCols = legendLayout.columnsForWidth(layoutRect.width()) itemRects = legendLayout.layoutItems(layoutRect, numCols) index = 0 for i in range(legendLayout.count()): item = legendLayout.itemAt(i) w = item.widget() if w is not None: painter.save() painter.setClipRect(itemRects[index], Qt.IntersectClip) self.renderItem(painter, w, itemRects[index], fillBackground) index += 1 painter.restore()