def drawSteps(self, painter, xMap, yMap, canvasRect, from_, to): doAlign = QwtPainter.roundingAlignment(painter) polygon = QPolygonF(2 * (to - from_) + 1) inverted = self.orientation() == Qt.Vertical if self.__data.attributes & self.Inverted: inverted = not inverted series = self.data() ip = 0 for i in range(from_, to + 1): sample = series.sample(i) xi = xMap.transform(sample.x()) yi = yMap.transform(sample.y()) if doAlign: xi = round(xi) yi = round(yi) if ip > 0: p0 = polygon[ip - 2] if inverted: polygon[ip - 1] = QPointF(p0.x(), yi) else: polygon[ip - 1] = QPointF(xi, p0.y()) polygon[ip] = QPointF(xi, yi) ip += 2 if self.__data.paintAttributes & self.ClipPolygons: clipped = QwtClipper().clipPolygonF(canvasRect, polygon, False) QwtPainter.drawPolyline(painter, clipped) else: QwtPainter.drawPolyline(painter, polygon) if self.__data.brush.style() != Qt.NoBrush: self.fillCurve(painter, xMap, yMap, canvasRect, polygon)
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 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 qwtDrawTriangleSymbols(painter, type, points, numPoint, symbol): size = symbol.size() pen = QPen(symbol.pen()) pen.setJoinStyle(Qt.MiterJoin) painter.setPen(pen) painter.setBrush(symbol.brush()) doAlign = QwtPainter.roundingAlignment(painter) sw2 = .5 * size.width() sh2 = .5 * size.height() if doAlign: sw2 = np.floor(sw2) sh2 = np.floor(sh2) for pos in points: x = pos.x() y = pos.y() if doAlign: x = round(x) y = round(y) x1 = x - sw2 x2 = x1 + size.width() y1 = y - sh2 y2 = y1 + size.height() if type == QwtTriangle.Left: triangle = [QPointF(x2, y1), QPointF(x1, y), QPointF(x2, y2)] elif type == QwtTriangle.Right: triangle = [QPointF(x1, y1), QPointF(x2, y), QPointF(x1, y2)] elif type == QwtTriangle.Up: triangle = [QPointF(x1, y2), QPointF(x, y1), QPointF(x2, y2)] elif type == QwtTriangle.Down: triangle = [QPointF(x1, y1), QPointF(x, y2), QPointF(x2, y1)] QwtPainter.drawPolygon(painter, QPolygonF(triangle))
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 drawColorBar(self, painter, rect): if not self.__data.colorBar.interval.isValid(): return sd = self.__data.scaleDraw QwtPainter.drawColorBar(painter, self.__data.colorBar.colorMap, self.__data.colorBar.interval.normalized(), sd.scaleMap(), sd.orientation(), rect)
def fillCurve(self, painter, xMap, yMap, canvasRect, polygon): """ Fill the area between the curve and the baseline with the curve brush :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 QPolygonF polygon: Polygon - will be modified ! .. seealso:: :py:meth:`setBrush()`, :py:meth:`setBaseline()`, :py:meth:`setStyle()` """ if self.__data.brush.style() == Qt.NoBrush: return self.closePolyline(painter, xMap, yMap, polygon) if polygon.count() <= 2: return brush = self.__data.brush if not brush.color().isValid(): brush.setColor(self.__data.pen.color()) if self.__data.paintAttributes & self.ClipPolygons: polygon = QwtClipper().clipPolygonF(canvasRect, polygon, True) painter.save() painter.setPen(Qt.NoPen) painter.setBrush(brush) QwtPainter.drawPolygon(painter, polygon) painter.restore()
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 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() 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 qwtDrawHexagonSymbols(painter, points, numPoints, symbol): painter.setBrush(symbol.brush()) painter.setPen(symbol.pen()) cos30 = np.cos(30 * np.pi / 180.) dx = .5 * (symbol.size().width() - cos30) dy = .25 * symbol.size().height() doAlign = QwtPainter.roundingAlignment(painter) for pos in points: if doAlign: x = round(pos.x()) y = round(pos.y()) x1 = np.ceil(x - dx) y1 = np.ceil(y - 2 * dy) else: x = pos.x() y = pos.y() x1 = x - dx y1 = y - 2 * dy x2 = x1 + 1 * dx x3 = x1 + 2 * dx y2 = y1 + 1 * dy y3 = y1 + 3 * dy y4 = y1 + 4 * dy hexa = [ QPointF(x2, y1), QPointF(x3, y2), QPointF(x3, y3), QPointF(x2, y4), QPointF(x1, y3), QPointF(x1, y2) ] QwtPainter.drawPolygon(painter, QPolygonF(hexa))
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 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 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) legendLayout = self.__data.view.contentsWidget.layout() if legendLayout is None: return left, right, top, bottom = self.getContentsMargins() layoutRect = QRect() layoutRect.setLeft(math.ceil(rect.left()) + left) layoutRect.setTop(math.ceil(rect.top()) + top) layoutRect.setRight(math.ceil(rect.right()) - right) layoutRect.setBottom(math.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 drawSteps(self, painter, xMap, yMap, canvasRect, from_, to): doAlign = QwtPainter.roundingAlignment(painter) polygon = QPolygonF(2*(to-from_)+1) inverted = self.orientation() == Qt.Vertical if self.__data.attributes & self.Inverted: inverted = not inverted series = self.data() ip = 0 for i in range(from_, to+1): sample = series.sample(i) xi = xMap.transform(sample.x()) yi = yMap.transform(sample.y()) if doAlign: xi = round(xi) yi = round(yi) if ip > 0: p0 = polygon[ip-2] if inverted: polygon[ip-1] = QPointF(p0.x(), yi) else: polygon[ip-1] = QPointF(xi, p0.y()) polygon[ip] = QPointF(xi, yi) ip += 2 if self.__data.paintAttributes & self.ClipPolygons: clipped = QwtClipper().clipPolygonF(canvasRect, polygon, False) QwtPainter.drawPolyline(painter, clipped) else: QwtPainter.drawPolyline(painter, polygon) if self.__data.brush.style() != Qt.NoBrush: self.fillCurve(painter, xMap, yMap, canvasRect, polygon)
def draw(self, painter, rect): if self.__data.paintAttributes & self.PaintBackground: if self.__data.borderPen != Qt.NoPen or\ self.__data.backgroundBrush != Qt.NoBrush: painter.save() painter.setPen(self.__data.borderPen) painter.setBrush(self.__data.backgroundBrush) if self.__data.borderRadius == 0: QwtPainter.drawRect(painter, rect) else: painter.setRenderHint(QPainter.Antialiasing, True) painter.drawRoundedRect(rect, self.__data.borderRadius, self.__data.borderRadius) painter.restore() painter.save() if self.__data.paintAttributes & self.PaintUsingTextFont: painter.setFont(self.__data.font) if self.__data.paintAttributes & self.PaintUsingTextColor: if self.__data.color.isValid(): painter.setPen(self.__data.color) expandedRect = rect if self.__data.layoutAttributes & self.MinimumLayout: font = QFont(painter.font(), self.desktopwidget) (left, right, top, bottom ) = self.__data.textEngine.textMargins(font) expandedRect.setTop(rect.top()-top) expandedRect.setBottom(rect.bottom()+bottom) expandedRect.setLeft(rect.left()-left) expandedRect.setRight(rect.right()+right) self.__data.textEngine.draw(painter, expandedRect, self.__data.renderFlags, self.__data.text) painter.restore()
def qwtDrawTriangleSymbols(painter, type, points, numPoint, symbol): size =symbol.size() pen = QPen(symbol.pen()) pen.setJoinStyle(Qt.MiterJoin) painter.setPen(pen) painter.setBrush(symbol.brush()) doAlign = QwtPainter.roundingAlignment(painter) sw2 = .5*size.width() sh2 = .5*size.height() if doAlign: sw2 = np.floor(sw2) sh2 = np.floor(sh2) for pos in points: x = pos.x() y = pos.y() if doAlign: x = round(x) y = round(y) x1 = x-sw2 x2 = x1+size.width() y1 = y-sh2 y2 = y1+size.height() if type == QwtTriangle.Left: triangle = [QPointF(x2, y1), QPointF(x1, y), QPointF(x2, y2)] elif type == QwtTriangle.Right: triangle = [QPointF(x1, y1), QPointF(x2, y), QPointF(x1, y2)] elif type == QwtTriangle.Up: triangle = [QPointF(x1, y2), QPointF(x, y1), QPointF(x2, y2)] elif type == QwtTriangle.Down: triangle = [QPointF(x1, y1), QPointF(x, y2), QPointF(x2, y1)] QwtPainter.drawPolygon(painter, QPolygonF(triangle))
def qwtDrawHexagonSymbols(painter, points, numPoints, symbol): painter.setBrush(symbol.brush()) painter.setPen(symbol.pen()) cos30 = np.cos(30*np.pi/180.) dx = .5*(symbol.size().width()-cos30) dy = .25*symbol.size().height() doAlign = QwtPainter.roundingAlignment(painter) for pos in points: if doAlign: x = round(pos.x()) y = round(pos.y()) x1 = np.ceil(x-dx) y1 = np.ceil(y-2*dy) else: x = pos.x() y = pos.y() x1 = x-dx y1 = y-2*dy x2 = x1+1*dx x3 = x1+2*dx y2 = y1+1*dy y3 = y1+3*dy y4 = y1+4*dy hexa = [QPointF(x2, y1), QPointF(x3, y2), QPointF(x3, y3), QPointF(x2, y4), QPointF(x1, y3), QPointF(x1, y2)] QwtPainter.drawPolygon(painter, QPolygonF(hexa))
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() 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 drawLines(self, painter, canvasRect, pos): """ Draw the lines 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.style == self.NoLine: return doAlign = QwtPainter.roundingAlignment(painter) painter.setPen(self.__data.pen) if self.__data.style in (QwtPlotMarker.HLine, QwtPlotMarker.Cross): y = pos.y() if doAlign: y = round(y) QwtPainter.drawLine(painter, canvasRect.left(), y, canvasRect.right() - 1., y) if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross): x = pos.x() if doAlign: x = round(x) QwtPainter.drawLine(painter, x, canvasRect.top(), x, canvasRect.bottom() - 1.)
def drawLines(self, painter, canvasRect, pos): """ Draw the lines 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.style == self.NoLine: return doAlign = QwtPainter.roundingAlignment(painter) painter.setPen(self.__data.pen) if self.__data.style in (QwtPlotMarker.HLine, QwtPlotMarker.Cross): y = pos.y() if doAlign: y = round(y) QwtPainter.drawLine(painter, canvasRect.left(), y, canvasRect.right()-1., y) if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross): x = pos.x() if doAlign: x = round(x) QwtPainter.drawLine(painter, x, canvasRect.top(), x, canvasRect.bottom()-1.)
def qwtDrawDiamondSymbols(painter, points, numPoints, symbol): size =symbol.size() pen = QPen(symbol.pen()) pen.setJoinStyle(Qt.MiterJoin) painter.setPen(pen) painter.setBrush(symbol.brush()) if QwtPainter.roundingAlignment(painter): for pos in points: x = round(pos.x()) y = round(pos.y()) x1 = x-size.width()//2 y1 = y-size.height()//2 x2 = x1+size.width() y2 = y1+size.height() polygon = QPolygonF() polygon += QPointF(x, y1) polygon += QPointF(x1, y) polygon += QPointF(x, y2) polygon += QPointF(x2, y) QwtPainter.drawPolygon(painter, polygon) else: for pos in points: x1 = pos.x()-.5*size.width() y1 = pos.y()-.5*size.height() x2 = x1+size.width() y2 = y1+size.height() polygon = QPolygonF() polygon += QPointF(pos.x(), y1) polygon += QPointF(x1, pos.y()) polygon += QPointF(pos.x(), y2) polygon += QPointF(x2, pos.y()) QwtPainter.drawPolygon(painter, polygon)
def qwtDrawDiamondSymbols(painter, points, numPoints, symbol): size = symbol.size() pen = QPen(symbol.pen()) pen.setJoinStyle(Qt.MiterJoin) painter.setPen(pen) painter.setBrush(symbol.brush()) if QwtPainter.roundingAlignment(painter): for pos in points: x = round(pos.x()) y = round(pos.y()) x1 = x - size.width() // 2 y1 = y - size.height() // 2 x2 = x1 + size.width() y2 = y1 + size.height() polygon = QPolygonF() polygon += QPointF(x, y1) polygon += QPointF(x1, y) polygon += QPointF(x, y2) polygon += QPointF(x2, y) QwtPainter.drawPolygon(painter, polygon) else: for pos in points: x1 = pos.x() - .5 * size.width() y1 = pos.y() - .5 * size.height() x2 = x1 + size.width() y2 = y1 + size.height() polygon = QPolygonF() polygon += QPointF(pos.x(), y1) polygon += QPointF(x1, pos.y()) polygon += QPointF(pos.x(), y2) polygon += QPointF(x2, pos.y()) QwtPainter.drawPolygon(painter, polygon)
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 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 draw(self, painter, rect): if self.__data.paintAttributes & self.PaintBackground: if self.__data.borderPen != Qt.NoPen or\ self.__data.backgroundBrush != Qt.NoBrush: painter.save() painter.setPen(self.__data.borderPen) painter.setBrush(self.__data.backgroundBrush) if self.__data.borderRadius == 0: QwtPainter.drawRect(painter, rect) else: painter.setRenderHint(QPainter.Antialiasing, True) painter.drawRoundedRect(rect, self.__data.borderRadius, self.__data.borderRadius) painter.restore() painter.save() if self.__data.paintAttributes & self.PaintUsingTextFont: painter.setFont(self.__data.font) if self.__data.paintAttributes & self.PaintUsingTextColor: if self.__data.color.isValid(): painter.setPen(self.__data.color) expandedRect = rect if self.__data.layoutAttributes & self.MinimumLayout: font = QFont(painter.font(), self.desktopwidget) (left, right, top, bottom) = self.__data.textEngine.textMargins(font) expandedRect.setTop(rect.top() - top) expandedRect.setBottom(rect.bottom() + bottom) expandedRect.setLeft(rect.left() - left) expandedRect.setRight(rect.right() + right) self.__data.textEngine.draw(painter, expandedRect, self.__data.renderFlags, self.__data.text) painter.restore()
def paintEvent(self, event): painter = QPainter(self) painter.setClipRegion(event.region()) if ( self.testPaintAttribute(self.BackingStore) and self.__data.backingStore is not None ): bs = self.__data.backingStore if QT_MAJOR_VERSION >= 5: pixelRatio = bs.devicePixelRatio() else: pixelRatio = 1.0 if bs.size() != self.size() * pixelRatio: bs = QwtPainter.backingStore(self, self.size()) if self.testAttribute(Qt.WA_StyledBackground): p = QPainter(bs) qwtFillBackground(p, self) self.drawCanvas(p, True) else: p = QPainter() if self.__data.borderRadius <= 0.0: # print('**DEBUG: QwtPlotCanvas.paintEvent') QwtPainter.fillPixmap(self, bs) p.begin(bs) self.drawCanvas(p, False) else: p.begin(bs) qwtFillBackground(p, self) self.drawCanvas(p, True) if self.frameWidth() > 0: self.drawBorder(p) p.end() painter.drawPixmap(0, 0, self.__data.backingStore) else: if self.testAttribute(Qt.WA_StyledBackground): if self.testAttribute(Qt.WA_OpaquePaintEvent): qwtFillBackground(painter, self) self.drawCanvas(painter, True) else: self.drawCanvas(painter, False) else: if self.testAttribute(Qt.WA_OpaquePaintEvent): if self.autoFillBackground(): qwtFillBackground(painter, self) qwtDrawBackground(painter, self) else: if self.borderRadius() > 0.0: clipPath = QPainterPath() clipPath.addRect(self.rect()) clipPath = clipPath.subtracted(self.borderPath(self.rect())) painter.save() painter.setClipPath(clipPath, Qt.IntersectClip) qwtFillBackground(painter, self) qwtDrawBackground(painter, self) painter.restore() self.drawCanvas(painter, False) if self.frameWidth() > 0: self.drawBorder(painter) if self.hasFocus() and self.focusIndicator() == self.CanvasFocusIndicator: self.drawFocusIndicator(painter)
def drawFocusIndicator(self, painter): margin = 1 focusRect = self.contentsRect() focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin, focusRect.width() - 2 * margin, focusRect.height() - 2 * margin) QwtPainter.drawFocusRect(painter, self, focusRect)
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 """ QwtPainter.drawText(painter, rect, flags, text)
def qwtDrawXCrossSymbols(painter, points, numPoints, symbol): size = symbol.size() off = 0 pen = QPen(symbol.pen()) if pen.width() > 1: pen.setCapStyle(Qt.FlatCap) off = 1 painter.setPen(pen) if QwtPainter.roundingAlignment(painter): sw = np.floor(size.width()) sh = np.floor(size.height()) sw2 = size.width() // 2 sh2 = size.height() // 2 for pos in points: x = round(pos.x()) y = round(pos.y()) x1 = x - sw2 x2 = x1 + sw + off y1 = y - sh2 y2 = y1 + sh + off QwtPainter.drawLine(painter, x1, y1, x2, y2) QwtPainter.drawLine(painter, x2, y1, x1, y2) else: sw = size.width() sh = size.height() sw2 = .5 * size.width() sh2 = .5 * size.height() for pos in points: x1 = pos.x() - sw2 x2 = x1 + sw y1 = pos.y() - sh2 y2 = y1 + sh QwtPainter.drawLine(painter, x1, y1, x2, y2) QwtPainter.drawLine(painter, x2, y1, x1, y2)
def qwtDrawXCrossSymbols(painter, points, numPoints, symbol): size =symbol.size() off = 0 pen = QPen(symbol.pen()) if pen.width() > 1: pen.setCapStyle(Qt.FlatCap) off = 1 painter.setPen(pen) if QwtPainter.roundingAlignment(painter): sw = np.floor(size.width()) sh = np.floor(size.height()) sw2 = size.width()//2 sh2 = size.height()//2 for pos in points: x = round(pos.x()) y = round(pos.y()) x1 = x-sw2 x2 = x1+sw+off y1 = y-sh2 y2 = y1+sh+off QwtPainter.drawLine(painter, x1, y1, x2, y2) QwtPainter.drawLine(painter, x2, y1, x1, y2) else: sw = size.width() sh = size.height() sw2 = .5*size.width() sh2 = .5*size.height() for pos in points: x1 = pos.x()-sw2 x2 = x1+sw y1 = pos.y()-sh2 y2 = y1+sh QwtPainter.drawLine(painter, x1, y1, x2, y2) QwtPainter.drawLine(painter, x2, y1, x1, y2)
def drawBackbone(self, painter): doAlign = QwtPainter.roundingAlignment(painter) pos = self.__data.pos len_ = self.__data.len pw = max([self.penWidth(), 1]) if doAlign: if self.alignment() in (self.LeftScale, self.TopScale): off = (pw-1)/2 else: off = pw/2 else: off = .5*self.penWidth() if self.alignment() == self.LeftScale: x = pos.x() - off if doAlign: x = round(x) QwtPainter.drawLine(painter, x, pos.y(), x, pos.y()+len_) elif self.alignment() == self.RightScale: x = pos.x() + off if doAlign: x = round(x) QwtPainter.drawLine(painter, x, pos.y(), x, pos.y()+len_) elif self.alignment() == self.TopScale: y = pos.y() - off if doAlign: y = round(y) QwtPainter.drawLine(painter, pos.x(), y, pos.x()+len_, y) elif self.alignment() == self.BottomScale: y = pos.y() + off if doAlign: y = round(y) QwtPainter.drawLine(painter, pos.x(), y, pos.x()+len_, y)
def drawBackbone(self, painter): doAlign = QwtPainter.roundingAlignment(painter) pos = self.__data.pos len_ = self.__data.len pw = max([self.penWidth(), 1]) if doAlign: if self.alignment() in (self.LeftScale, self.TopScale): off = (pw - 1) / 2 else: off = pw / 2 else: off = .5 * self.penWidth() if self.alignment() == self.LeftScale: x = pos.x() - off if doAlign: x = round(x) QwtPainter.drawLine(painter, x, pos.y(), x, pos.y() + len_) elif self.alignment() == self.RightScale: x = pos.x() + off if doAlign: x = round(x) QwtPainter.drawLine(painter, x, pos.y(), x, pos.y() + len_) elif self.alignment() == self.TopScale: y = pos.y() - off if doAlign: y = round(y) QwtPainter.drawLine(painter, pos.x(), y, pos.x() + len_, y) elif self.alignment() == self.BottomScale: y = pos.y() + off if doAlign: y = round(y) QwtPainter.drawLine(painter, pos.x(), y, pos.x() + len_, y)
def drawContents(self, painter): r = self.textRect() if r.isEmpty(): return painter.setFont(self.font()) painter.setPen(self.palette().color(QPalette.Active, QPalette.Text)) self.drawText(painter, QRectF(r)) if self.hasFocus(): m = 2 focusRect = self.contentsRect().adjusted(m, m, -m+1, -m+1) QwtPainter.drawFocusRect(painter, self, focusRect)
def paintEvent(self, event): painter = QPainter(self) painter.setClipRegion(event.region()) if self.testPaintAttribute(self.BackingStore) and\ self.__data.backingStore is not None: bs = self.__data.backingStore if bs.size() != self.size(): bs = QwtPainter.backingStore(self, self.size()) if self.testAttribute(Qt.WA_StyledBackground): p = QPainter(bs) qwtFillBackground(p, self) self.drawCanvas(p, True) else: p = QPainter() if self.__data.borderRadius <= 0.: # print('**DEBUG: QwtPlotCanvas.paintEvent') QwtPainter.fillPixmap(self, bs) p.begin(bs) self.drawCanvas(p, False) else: p.begin(bs) qwtFillBackground(p, self) self.drawCanvas(p, True) if self.frameWidth() > 0: self.drawBorder(p) p.end() painter.drawPixmap(0, 0, self.__data.backingStore) else: if self.testAttribute(Qt.WA_StyledBackground): if self.testAttribute(Qt.WA_OpaquePaintEvent): qwtFillBackground(painter, self) self.drawCanvas(painter, True) else: self.drawCanvas(painter, False) else: if self.testAttribute(Qt.WA_OpaquePaintEvent): if self.autoFillBackground(): qwtFillBackground(painter, self) qwtDrawBackground(painter, self) else: if self.borderRadius() > 0.: clipPath = QPainterPath() clipPath.addRect(self.rect()) clipPath = clipPath.subtracted(self.borderPath(self.rect())) painter.save() painter.setClipPath(clipPath, Qt.IntersectClip) qwtFillBackground(painter, self) qwtDrawBackground(painter, self) painter.restore() self.drawCanvas(painter, False) if self.frameWidth() > 0: self.drawBorder(painter) if self.hasFocus() and self.focusIndicator() == self.CanvasFocusIndicator: self.drawFocusIndicator(painter)
def 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 """ doc = QwtRichTextDocument(text, flags, painter.font()) QwtPainter.drawSimpleRichText(painter, rect, flags, doc)
def drawContents(self, painter): r = self.textRect() if r.isEmpty(): return painter.setFont(self.font()) painter.setPen(self.palette().color(QPalette.Active, QPalette.Text)) self.drawText(painter, QRectF(r)) if self.hasFocus(): m = 2 focusRect = self.contentsRect().adjusted(m, m, -m + 1, -m + 1) QwtPainter.drawFocusRect(painter, self, focusRect)
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 drawFocusIndicator(self, painter): """ Draw the focus indication :param QPainter painter: Painter """ margin = 1 focusRect = self.contentsRect() focusRect.setRect(focusRect.x()+margin, focusRect.y()+margin, focusRect.width()-2*margin, focusRect.height()-2*margin) QwtPainter.drawFocusRect(painter, self, focusRect)
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 drawColumn(self, painter, rect, sample): if self.__data.symbol and\ self.__data.symbol.style() != QwtColumnSymbol.NoStyle: self.__data.symbol.draw(painter, rect) else: r = QRectF(rect.toRect()) if QwtPainter.roundingAlignment(painter): r.setLeft(round(r.left())) r.setRight(round(r.right())) r.setTop(round(r.top())) r.setBottom(round(r.bottom())) QwtPainter.drawRect(painter, r)
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.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 = QRectF(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 drawFocusIndicator(self, painter): """ Draw the focus indication :param QPainter painter: Painter """ margin = 1 focusRect = self.contentsRect() focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin, focusRect.width() - 2 * margin, focusRect.height() - 2 * margin) QwtPainter.drawFocusRect(painter, self, focusRect)
def drawBorder(self, painter): """ Draw the border of the plot canvas :param QPainter painter: Painter .. seealso:: :py:meth:`setBorderRadius()` """ 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 PYQT5: from qtpy.QtWidgets import QStyleOptionFrame else: try: from PyQt4.QtGui import QStyleOptionFrameV3 as QStyleOptionFrame except ImportError: from PySide2.QtWidgets import 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)
def drawBorder(self, painter): """ Draw the border of the plot canvas :param QPainter painter: Painter .. seealso:: :py:meth:`setBorderRadius()` """ 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: opt = QStyleOptionFrame() opt.initFrom(self) try: shape_mask = QFrame.Shape_Mask.value shadow_mask = QFrame.Shadow_Mask.value except AttributeError: shape_mask = QFrame.Shape_Mask shadow_mask = QFrame.Shadow_Mask frameShape = self.frameStyle() & shape_mask frameShadow = self.frameStyle() & 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)
def drawTick(self, painter, value, len_): """ Draw a tick :param QPainter painter: Painter :param float value: Value of the tick :param float len: Length of the tick .. seealso:: :py:meth:`drawBackbone()`, :py:meth:`drawLabel()` """ if len_ <= 0: return roundingAlignment = QwtPainter.roundingAlignment(painter) pos = self.__data.pos tval = self.scaleMap().transform(value) if roundingAlignment: tval = round(tval) pw = self.penWidth() a = 0 if pw > 1 and roundingAlignment: a = 1 if self.alignment() == self.LeftScale: x1 = pos.x() + a x2 = pos.x() + a - pw - len_ if roundingAlignment: x1 = round(x1) x2 = round(x2) QwtPainter.drawLine(painter, x1, tval, x2, tval) elif self.alignment() == self.RightScale: x1 = pos.x() x2 = pos.x() + pw + len_ if roundingAlignment: x1 = round(x1) x2 = round(x2) QwtPainter.drawLine(painter, x1, tval, x2, tval) elif self.alignment() == self.BottomScale: y1 = pos.y() y2 = pos.y() + pw + len_ if roundingAlignment: y1 = round(y1) y2 = round(y2) QwtPainter.drawLine(painter, tval, y1, tval, y2) elif self.alignment() == self.TopScale: y1 = pos.y() + a y2 = pos.y() - pw - len_ + a if roundingAlignment: y1 = round(y1) y2 = round(y2) QwtPainter.drawLine(painter, tval, y1, tval, y2)
def fillCurve(self, painter, xMap, yMap, canvasRect, polygon): if self.__data.brush.style() == Qt.NoBrush: return self.closePolyline(painter, xMap, yMap, polygon) if polygon.count() <= 2: return brush = self.__data.brush if not brush.color().isValid(): brush.setColor(self.__data.pen.color()) if self.__data.paintAttributes & self.ClipPolygons: polygon = QwtClipper().clipPolygonF(canvasRect, polygon, True) painter.save() painter.setPen(Qt.NoPen) painter.setBrush(brush) QwtPainter.drawPolygon(painter, polygon) painter.restore()
def closePolyline(self, painter, xMap, yMap, polygon): """ Complete a polygon to be a closed polygon including the area between the original polygon and the baseline. :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 QPolygonF polygon: Polygon to be completed """ if polygon.size() < 2: return doAlign = QwtPainter.roundingAlignment(painter) baseline = self.__data.baseline if self.orientation() == Qt.Vertical: if yMap.transformation(): baseline = yMap.transformation().bounded(baseline) refY = yMap.transform(baseline) if doAlign: refY = round(refY) polygon += QPointF(polygon.last().x(), refY) polygon += QPointF(polygon.first().x(), refY) else: if xMap.transformation(): baseline = xMap.transformation().bounded(baseline) refX = xMap.transform(baseline) if doAlign: refX = round(refX) polygon += QPointF(refX, polygon.last().y()) polygon += QPointF(refX, polygon.first().y())
def drawSymbols(self, painter, symbol, xMap, yMap, canvasRect, from_, to): """ Draw symbols :param QPainter painter: Painter :param qwt.symbol.QwtSymbol symbol: Curve symbol :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:`setSymbol()`, :py:meth:`drawSeries()`, :py:meth:`drawCurve()` """ mapper = QwtPointMapper() mapper.setFlag(QwtPointMapper.RoundPoints, QwtPainter.roundingAlignment(painter)) mapper.setFlag(QwtPointMapper.WeedOutPoints, self.testPaintAttribute(QwtPlotCurve.FilterPoints)) mapper.setBoundingRect(canvasRect) chunkSize = 500 for i in range(from_, to + 1, chunkSize): n = min([chunkSize, to - i + 1]) points = mapper.toPointsF(xMap, yMap, self.data(), i, i + n - 1) if points.size() > 0: symbol.drawSymbols(painter, points)
def drawSymbols(self, painter, symbol, xMap, yMap, canvasRect, from_, to): """ Draw symbols :param QPainter painter: Painter :param qwt.symbol.QwtSymbol symbol: Curve symbol :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:`setSymbol()`, :py:meth:`drawSeries()`, :py:meth:`drawCurve()` """ mapper = QwtPointMapper() mapper.setFlag(QwtPointMapper.RoundPoints, QwtPainter.roundingAlignment(painter)) mapper.setFlag(QwtPointMapper.WeedOutPoints, self.testPaintAttribute(QwtPlotCurve.FilterPoints)) mapper.setBoundingRect(canvasRect) chunkSize = 500 for i in range(from_, to+1, chunkSize): n = min([chunkSize, to-i+1]) points = mapper.toPointsF(xMap, yMap, self.data(), i, i+n-1) if points.size() > 0: symbol.drawSymbols(painter, points)
def drawColorBar(self, painter, rect): """ Draw the color bar of the scale widget :param QPainter painter: Painter :param QRectF rect: Bounding rectangle for the color bar .. seealso:: :py:meth:`setColorBarEnabled()` """ if not self.__data.colorBar.interval.isValid(): return sd = self.__data.scaleDraw QwtPainter.drawColorBar(painter, self.__data.colorBar.colorMap, self.__data.colorBar.interval.normalized(), sd.scaleMap(), sd.orientation(), rect)
def drawLines(self, painter, canvasRect, pos): if self.__data.style == self.NoLine: return doAlign = QwtPainter.roundingAlignment(painter) painter.setPen(self.__data.pen) if self.__data.style in (QwtPlotMarker.HLine, QwtPlotMarker.Cross): y = pos.y() if doAlign: y = round(y) QwtPainter.drawLine(painter, canvasRect.left(), y, canvasRect.right()-1., y) if self.__data.style in (QwtPlotMarker.VLine, QwtPlotMarker.Cross): x = pos.x() if doAlign: x = round(x) QwtPainter.drawLine(painter, x, canvasRect.top(), x, canvasRect.bottom()-1.)
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 drawSteps(self, painter, xMap, yMap, canvasRect, from_, to): """ Draw steps :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:`drawSticks()`, :py:meth:`drawDots()`, :py:meth:`drawLines()` """ doAlign = QwtPainter.roundingAlignment(painter) polygon = QPolygonF(2*(to-from_)+1) inverted = self.orientation() == Qt.Vertical if self.__data.attributes & self.Inverted: inverted = not inverted series = self.data() ip = 0 for i in range(from_, to+1): sample = series.sample(i) xi = xMap.transform(sample.x()) yi = yMap.transform(sample.y()) if doAlign: xi = round(xi) yi = round(yi) if ip > 0: p0 = polygon[ip-2] if inverted: polygon[ip-1] = QPointF(p0.x(), yi) else: polygon[ip-1] = QPointF(xi, p0.y()) polygon[ip] = QPointF(xi, yi) ip += 2 if self.__data.paintAttributes & self.ClipPolygons: clipped = QwtClipper().clipPolygonF(canvasRect, polygon, False) QwtPainter.drawPolyline(painter, clipped) else: QwtPainter.drawPolyline(painter, polygon) if self.__data.brush.style() != Qt.NoBrush: self.fillCurve(painter, xMap, yMap, canvasRect, polygon)