Example #1
0
 def drawLine(self, *args):
     if len(args) == 3:
         painter, p1, p2 = args
         if isinstance(p1, QPointF):
             p1 = p1.toPoint()
         if isinstance(p2, QPointF):
             p2 = p2.toPoint()
         deviceClipping, clipRect = qwtIsClippingNeeded(painter)
         if deviceClipping and not clipRect.contains(p1)\
            and not clipRect.contains(p2):
             polygon = QPolygonF()
             polygon += p1
             polygon += p2
             self.drawPolyline(painter, polygon)
             return
         painter.drawLine(p1, p2)
     elif len(args) == 5:
         painter, x1, y1, x2, y2 = args
         self.drawLine(painter, QPointF(x1, y1), QPointF(x2, y2))
     elif len(args) == 2:
         painter, line = args
         self.drawLine(painter, line.p1(), line.p2())
     else:
         raise TypeError("QwtPainter.drawLine() takes 2, 3 or 5 argument"\
                         "(s) (%s given)" % len(args))
Example #2
0
 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)
Example #3
0
    def fitParametric(self, points):
        size = points.size()
        fittedPoints = QPolygonF(self.__data.splineSize)
        splinePointsX = QPolygonF(size)
        splinePointsY = QPolygonF(size)
#        p = points.data()
        spX = splinePointsX#.data()
        spY = splinePointsY#.data()
        param = 0.
        for i in range(size):
            x = points[i].x()
            y = points[i].y()
            if i > 0:
                delta = np.sqrt((x-spX[i-1].y())**2+(y-spY[i-1].y())**2)
                param += max([delta, 1.])
            spX[i] = QPointF(param, x)
            spY[i] = QPointF(param, y)
        self.__data.spline.setPoints(splinePointsX)
        if not self.__data.spline.isValid():
            return points
        deltaX = splinePointsX[size-1].x()/(self.__data.splineSize-1)
        for i in range(self.__data.splineSize):
            dtmp = i*deltaX
            fittedPoints[i] = QPointF(self.__data.spline.value(dtmp),
                                      fittedPoints[i].y())
        self.__data.spline.setPoints(splinePointsY)
        if not self.__data.spline.isValid():
            return points
        deltaY = splinePointsY[size-1].x()/(self.__data.splineSize-1)
        for i in range(self.__data.splineSize):
            dtmp = i*deltaY
            fittedPoints[i] = QPointF(fittedPoints[i].x(),
                                      self.__data.spline.value(dtmp))
        return fittedPoints
Example #4
0
    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
        baseline = self.__data.baseline
        if self.orientation() == Qt.Horizontal:
            if yMap.transformation():
                baseline = yMap.transformation().bounded(baseline)
            refY = yMap.transform(baseline)
            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)
            polygon += QPointF(refX, polygon.last().y())
            polygon += QPointF(refX, polygon.first().y())
Example #5
0
def qwtDrawStar1Symbols(painter, points, numPoints, symbol):
    size =symbol.size()
    painter.setPen(symbol.pen())
    sqrt1_2 = np.sqrt(.5)
    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)
        painter.drawLine(r.left()+d1, r.top()+d1, r.right()-d1, r.bottom()-d1)
        painter.drawLine(r.left()+d1, r.bottom()-d1, r.right()-d1, r.top()+d1)
        painter.drawLine(c.x(), r.top(), c.x(), r.bottom())
        painter.drawLine(r.left(), c.y(), r.right(), c.y())
Example #6
0
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)
Example #7
0
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 labelPosition(self, value):
        """
        Find the position, where to paint a label

        The position has a distance that depends on the length of the ticks 
        in direction of the `alignment()`.

        :param float value: Value
        :return: Position, where to paint a label
        """
        tval = self.scaleMap().transform(value)
        dist = self.spacing()
        if self.hasComponent(QwtAbstractScaleDraw.Backbone):
            dist += max([1, self.penWidth()])
        if self.hasComponent(QwtAbstractScaleDraw.Ticks):
            dist += self.tickLength(QwtScaleDiv.MajorTick)
        
        px = 0
        py = 0
        if self.alignment() == self.RightScale:
            px = self.__data.pos.x() + dist
            py = tval
        elif self.alignment() == self.LeftScale:
            px = self.__data.pos.x() - dist
            py = tval
        elif self.alignment() == self.BottomScale:
            px = tval
            py = self.__data.pos.y() + dist
        elif self.alignment() == self.TopScale:
            px = tval
            py = self.__data.pos.y() - dist
        
        return QPointF(px, py)
 def transform(self, *args):
     """
     Transform a rectangle from scale to paint coordinates
     
     .. py:method:: transform(scalar)
     
         :param float scalar: Scalar
     
     .. py:method:: transform(xMap, yMap, rect)
     
         Transform a rectangle from scale to paint coordinates
     
         :param qwt.scale_map.QwtScaleMap xMap: X map
         :param qwt.scale_map.QwtScaleMap yMap: Y map
         :param QRectF rect: Rectangle in paint coordinates
     
     .. py:method:: transform(xMap, yMap, pos)
     
         Transform a point from scale to paint coordinates
     
         :param qwt.scale_map.QwtScaleMap xMap: X map
         :param qwt.scale_map.QwtScaleMap yMap: Y map
         :param QPointF pos: Position in scale coordinates
         
     Scalar: scalemap.transform(scalar)
     Point (QPointF): scalemap.transform(xMap, yMap, pos)
     Rectangle (QRectF): scalemap.transform(xMap, yMap, rect)
     
     .. seealso::
     
         :py:meth:`invTransform()`
     """
     if len(args) == 1:
         # Scalar transform
         return self.transform_scalar(args[0])
     elif len(args) == 3 and isinstance(args[2], QPointF):
         xMap, yMap, pos = args
         return QPointF(xMap.transform(pos.x()), yMap.transform(pos.y()))
     elif len(args) == 3 and isinstance(args[2], QRectF):
         xMap, yMap, rect = args
         x1 = xMap.transform(rect.left())
         x2 = xMap.transform(rect.right())
         y1 = yMap.transform(rect.top())
         y2 = yMap.transform(rect.bottom())
         if x2 < x1:
             x1, x2 = x2, x1
         if y2 < y1:
             y1, y2 = y2, y1
         if qwtFuzzyCompare(x1, 0., x2 - x1) == 0:
             x1 = 0.
         if qwtFuzzyCompare(x2, 0., x2 - x1) == 0:
             x2 = 0.
         if qwtFuzzyCompare(y1, 0., y2 - y1) == 0:
             y1 = 0.
         if qwtFuzzyCompare(y2, 0., y2 - y1) == 0:
             y2 = 0.
         return QRectF(x1, y1, x2 - x1 + 1, y2 - y1 + 1)
     else:
         raise TypeError("%s().transform() takes 1 or 3 argument(s) (%s "\
                         "given)" % (self.__class__.__name__, len(args)))
Example #10
0
    def __init__(self):
        self.len = 0
        self.alignment = QwtScaleDraw.BottomScale
        self.labelAlignment = 0
        self.labelRotation = 0.

        self.pos = QPointF()
Example #11
0
 def transform(self, *args):
     """Transform from scale to paint coordinates
     
     Scalar: scalemap.transform(scalar)
     Point (QPointF): scalemap.transform(xMap, yMap, pos)
     Rectangle (QRectF): scalemap.transform(xMap, yMap, rect)
     """
     if len(args) == 1:
         # Scalar transform
         return self.transform_scalar(args[0])
     elif len(args) == 3 and isinstance(args[2], QPointF):
         xMap, yMap, pos = args
         return QPointF(xMap.transform(pos.x()), yMap.transform(pos.y()))
     elif len(args) == 3 and isinstance(args[2], QRectF):
         xMap, yMap, rect = args
         x1 = xMap.transform(rect.left())
         x2 = xMap.transform(rect.right())
         y1 = yMap.transform(rect.top())
         y2 = yMap.transform(rect.bottom())
         if x2 < x1:
             x1, x2 = x2, x1
         if y2 < y1:
             y1, y2 = y2, y1
         if qwtFuzzyCompare(x1, 0., x2 - x1) == 0:
             x1 = 0.
         if qwtFuzzyCompare(x2, 0., x2 - x1) == 0:
             x2 = 0.
         if qwtFuzzyCompare(y1, 0., y2 - y1) == 0:
             y1 = 0.
         if qwtFuzzyCompare(y2, 0., y2 - y1) == 0:
             y2 = 0.
         return QRectF(x1, y1, x2 - x1 + 1, y2 - y1 + 1)
     else:
         raise TypeError("%s().transform() takes 1 or 3 argument(s) (%s "\
                         "given)" % (self.__class__.__name__, len(args)))
Example #12
0
    def __init__(self, st, br, pn ,sz):
        self.style = st
        self.size = sz
        self.brush = br
        self.pen = pn
        self.isPinPointEnabled = False
        self.pinPoint = QPointF()

        class Path(object):
            def __init__(self):
                self.path = QPainterPath()
                self.graphic = QwtGraphic()
        self.path = Path()
        
        class Pixmap(object):
            def __init__(self):
                self.pixmap = QPixmap()
        self.pixmap = None  #Pixmap()
        
        class Graphic(object):
            def __init__(self):
                self.graphic = QwtGraphic()
        self.graphic = Graphic()
        
        class SVG(object):
            def __init__(self):
                self.renderer = QSvgRenderer()
        self.svg = SVG()
        
        class PaintCache(object):
            def __init__(self):
                self.policy = 0
                self.pixmap = None  #QPixmap()
        self.cache = PaintCache()
Example #13
0
 def drawText(self, *args):
     if len(args) == 4:
         if isinstance(args[1], (QRectF, QRect)):
             painter, rect, flags, text = args
             painter.save()
             qwtUnscaleFont(painter)
             painter.drawText(rect, flags, text)
             painter.restore()
         else:
             painter, x, y, text = args
             self.drawText(painter, QPointF(x, y), text)
     elif len(args) == 3:
         painter, pos, text = args
         deviceClipping, clipRect = qwtIsClippingNeeded(painter)
         if deviceClipping and not clipRect.contains(pos):
             return
         painter.save()
         qwtUnscaleFont(painter)
         painter.drawText(pos, text)
         painter.restore()
     elif len(args) == 7:
         painter, x, y, w, h, flags, text = args
         self.drawText(painter, QRectF(x, y, w, h), flags, text)
     else:
         raise TypeError("QwtPainter.drawText() takes 3, 4 or 7 argument"\
                         "(s) (%s given)" % len(args))
Example #14
0
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())
Example #15
0
def qwtDrawDiamondSymbols(painter, points, numPoints, symbol):
    size = symbol.size()
    pen = QPen(symbol.pen())
    pen.setJoinStyle(Qt.MiterJoin)
    painter.setPen(pen)
    painter.setBrush(symbol.brush())
    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())
        painter.drawPolygon(polygon)
Example #16
0
 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)
Example #17
0
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))
Example #18
0
 def __init__(self, size):
     super(QwtStyleSheetRecorder, self).__init__()
     self.__size = size
     self.__pen = QPen()
     self.__brush = QBrush()
     self.__origin = QPointF()
     self.clipRects = []
     self.border = Border()
     self.background = Background()
Example #19
0
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))
Example #20
0
 def draw(self, painter, *args):
     warnings.warn("`draw` has been removed in Qwt6: "\
                   "please rely on `drawSymbol` and `drawSymbols` instead",
                   RuntimeWarning)
     from qwt.qt.QtCore import QPointF
     if len(args) == 2:
         self.drawSymbols(painter, [QPointF(*args)])
     else:
         self.drawSymbol(painter, *args)
Example #21
0
    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()
Example #22
0
 def drawPath(self, path):
     rect = QRectF(QPointF(0., 0.), self.__size)
     if path.controlPointRect().contains(rect.center()):
         self.setCornerRects(path)
         self.alignCornerRects(rect)
         self.background.path = path
         self.background.brush = self.__brush
         self.background.origin = self.__origin
     else:
         self.border.pathlist += [path]
Example #23
0
 def labelRect(self, font, value):
     lbl = self.tickLabel(font, value)
     if not lbl or lbl.isEmpty():
         return QRectF(0., 0., 0., 0.)
     pos = self.labelPosition(value)
     labelSize = lbl.textSize(font)
     transform = self.labelTransformation(pos, labelSize)
     br = transform.mapRect(QRectF(QPointF(0, 0), labelSize))
     br.translate(-pos.x(), -pos.y())
     return br
Example #24
0
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()))
Example #25
0
    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()
Example #26
0
 def move(self, *args):
     if len(args) == 2:
         x, y = args
         self.move(QPointF(x, y))
     elif len(args) == 1:
         pos, = args
         self.__data.pos = pos
         self.updateMap()
     else:
         raise TypeError("%s().move() takes 1 or 2 argument(s) (%s given)"\
                         % (self.__class__.__name__, len(args)))
Example #27
0
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 move(self, *args):
     """
     Move the position of the scale
     
     The meaning of the parameter pos depends on the alignment:
     
       * `QwtScaleDraw.LeftScale`:
       
         The origin is the topmost point of the backbone. The backbone is a 
         vertical line. Scale marks and labels are drawn at the left of the 
         backbone.
         
       * `QwtScaleDraw.RightScale`:
       
         The origin is the topmost point of the backbone. The backbone is a 
         vertical line. Scale marks and labels are drawn at the right of 
         the backbone.
       
       * `QwtScaleDraw.TopScale`:
       
         The origin is the leftmost point of the backbone. The backbone is 
         a horizontal line. Scale marks and labels are drawn above the 
         backbone.
       
       * `QwtScaleDraw.BottomScale`:
       
         The origin is the leftmost point of the backbone. The backbone is 
         a horizontal line Scale marks and labels are drawn below the 
         backbone.
     
     .. py:method:: move(x, y)
     
         :param float x: X coordinate
         :param float y: Y coordinate
     
     .. py:method:: move(pos)
     
         :param QPointF pos: position
     
     .. seealso::
     
         :py:meth:`pos()`, :py:meth:`setLength()`
     """
     if len(args) == 2:
         x, y = args
         self.move(QPointF(x, y))
     elif len(args) == 1:
         pos, = args
         self.__data.pos = pos
         self.updateMap()
     else:
         raise TypeError("%s().move() takes 1 or 2 argument(s) (%s given)"\
                         % (self.__class__.__name__, len(args)))
Example #29
0
 def closePolyline(self, painter, xMap, yMap, polygon):
     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())
Example #30
0
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))
Example #31
0
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()
    for pos in points:
        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)
        ]
        painter.drawPolygon(QPolygonF(hexa))
Example #32
0
 def draw(self, painter, xMap, yMap, canvasRect):
     pos = QPointF(xMap.transform(self.__data.xValue),
                   yMap.transform(self.__data.yValue))
     self.drawLines(painter, canvasRect, pos)
     if self.__data.symbol and\
        self.__data.symbol.style() != QwtSymbol.NoSymbol:
         sz = self.__data.symbol.size()
         clipRect = QRectF(
             canvasRect.adjusted(-sz.width(), -sz.height(), sz.width(),
                                 sz.height()))
         if clipRect.contains(pos):
             self.__data.symbol.drawSymbols(painter, [pos])
     self.drawLabel(painter, canvasRect, pos)
Example #33
0
def qwtDrawGraphicSymbols(painter, points, numPoint, graphic, symbol):
    pointRect = QRectF(graphic.controlPointRect())
    if pointRect.isEmpty():
        return
    sx = 1.
    sy = 1.
    sz = symbol.size()
    if sz.isValid():
        sx = sz.width() / pointRect.width()
        sy = sz.height() / pointRect.height()
    pinPoint = QPointF(pointRect.center())
    if symbol.isPinPointEnabled():
        pinPoint = symbol.pinPoint()
    transform = QTransform(painter.transform())
    for pos in points:
        tr = QTransform(transform)
        tr.translate(pos.x(), pos.y())
        tr.scale(sx, sy)
        tr.translate(-pinPoint.x(), -pinPoint.y())
        painter.setTransform(tr)
        graphic.render(painter)
    painter.setTransform(transform)
Example #34
0
def qwtDrawGraphicSymbols(painter, points, numPoint, graphic, symbol):
    pointRect = QRectF(graphic.controlPointRect())
    if pointRect.isEmpty():
        return
    sx = 1.
    sy = 1.
    sz = symbol.size()
    if sz.isValid():
        sx = sz.width()/pointRect.width()
        sy = sz.height()/pointRect.height()
    pinPoint = QPointF(pointRect.center())
    if symbol.isPinPointEnabled():
        pinPoint = symbol.pinPoint()
    transform = QTransform(painter.transform())
    for pos in points:
        tr = QTransform(transform)
        tr.translate(pos.x(), pos.y())
        tr.scale(sx, sy)
        tr.translate(-pinPoint.x(), -pinPoint.y())
        painter.setTransform(tr)
        graphic.render(painter)
    painter.setTransform(transform)
Example #35
0
 def setCornerRects(self, path):
     pos = QPointF(0., 0.)
     for i in range(path.elementCount()):
         el = path.elementAt(i)
         if el.type in (QPainterPath.MoveToElement,
                        QPainterPath.LineToElement):
             pos.setX(el.x)
             pos.setY(el.y)
         elif el.type == QPainterPath.CurveToElement:
             r = QRectF(pos, QPointF(el.x, el.y))
             self.clipRects += [r.normalized()]
             pos.setX(el.x)
             pos.setY(el.y)
         elif el.type == QPainterPath.CurveToDataElement:
             if self.clipRects:
                 r = self.clipRects[-1]
                 r.setCoords(min([r.left(), el.x]),
                             min([r.top(), el.y]),
                             max([r.right(), el.x]),
                             max([r.bottom(), el.y]))
                 self.clipRects[-1] = r.normalized()
Example #36
0
 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)
Example #37
0
 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)