コード例 #1
0
ファイル: legend.py プロジェクト: gyenney/Tools
    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
コード例 #2
0
ファイル: legend.py プロジェクト: petebachant/python-qwt
    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()
コード例 #3
0
    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()
コード例 #4
0
ファイル: legend.py プロジェクト: petebachant/python-qwt
    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()
コード例 #5
0
ファイル: legend.py プロジェクト: petebachant/python-qwt
 def renderItem(self, painter, widget, rect, fillBackground):
     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
コード例 #6
0
ファイル: legend.py プロジェクト: petebachant/python-qwt
 def renderItem(self, painter, widget, rect, fillBackground):
     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
コード例 #7
0
 def renderCanvas(self, plot, painter, canvasRect, maps):
     canvas = plot.canvas()
     r = canvasRect.adjusted(0., 0., -1., 1.)
     if self.__data.layoutFlags & self.FrameWithScales:
         painter.save()
         r.adjust(-1., -1., 1., 1.)
         painter.setPen(QPen(Qt.black))
         if not (self.__data.discardFlags & self.DiscardCanvasBackground):
             bgBrush = canvas.palette().brush(plot.backgroundRole())
             painter.setBrush(bgBrush)
         QwtPainter.drawRect(painter, r)
         painter.restore()
         painter.save()
         painter.setClipRect(canvasRect)
         plot.drawItems(painter, canvasRect, maps)
         painter.restore()
     elif canvas.testAttribute(Qt.WA_StyledBackground):
         clipPath = QPainterPath()
         painter.save()
         if not self.__data.discardFlags & self.DiscardCanvasBackground:
             QwtPainter.drawBackground(painter, r, canvas)
             clipPath = qwtCanvasClip(canvas, canvasRect)
         painter.restore()
         painter.save()
         if clipPath.isEmpty():
             painter.setClipRect(canvasRect)
         else:
             painter.setClipPath(clipPath)
         plot.drawItems(painter, canvasRect, maps)
         painter.restore()
     else:
         clipPath = QPainterPath()
         frameWidth = 0
         if not self.__data.discardFlags & self.DiscardCanvasFrame:
             frameWidth = canvas.frameWidth()
             clipPath = qwtCanvasClip(canvas, canvasRect)
         innerRect = canvasRect.adjusted(frameWidth, frameWidth,
                                         -frameWidth, -frameWidth)
         painter.save()
         if clipPath.isEmpty():
             painter.setClipRect(innerRect)
         else:
             painter.setClipPath(clipPath)
         if not self.__data.discardFlags & self.DiscardCanvasBackground:
             QwtPainter.drawBackground(painter, innerRect, canvas)
         plot.drawItems(painter, innerRect, maps)
         painter.restore()
         if frameWidth > 0:
             painter.save()
             frameStyle = canvas.frameShadow() | canvas.frameShape()
             frameWidth = canvas.frameWidth()
             borderRadius = canvas.borderRadius()
             if borderRadius > 0.:
                 QwtPainter.drawRoundedFrame(painter, canvasRect, r, r,
                                             canvas.palette(), frameWidth,
                                             frameStyle)
             else:
                 midLineWidth = canvas.midLineWidth()
                 QwtPainter.drawFrame(painter, canvasRect, canvas.palette(),
                                      canvas.foregroundRole(), frameWidth,
                                      midLineWidth, frameStyle)
             painter.restore()
コード例 #8
0
    def render(self, plot, painter, plotRect):
        if painter == 0 or not painter.isActive() or not plotRect.isValid()\
           or plot.size().isNull():
            return
        if not self.__data.discardFlags & self.DiscardBackground:
            QwtPainter.drawBackground(painter, plotRect, plot)

        transform = QTransform()
        transform.scale(float(painter.device().logicalDpiX())/plot.logicalDpiX(),
                        float(painter.device().logicalDpiY())/plot.logicalDpiY())
        
        invtrans, _ok = transform.inverted()
        layoutRect = invtrans.mapRect(plotRect)
        if not (self.__data.discardFlags & self.DiscardBackground):
            left, top, right, bottom = plot.getContentsMargins()
            layoutRect.adjust(left, top, -right, -bottom)

        layout = plot.plotLayout()
        baseLineDists = [None]*QwtPlot.axisCnt
        canvasMargins = [None]*QwtPlot.axisCnt

        for axisId in range(QwtPlot.axisCnt):
            canvasMargins[axisId] = layout.canvasMargin(axisId)
            if self.__data.layoutFlags & self.FrameWithScales:
                scaleWidget = plot.axisWidget(axisId)
                if scaleWidget:
                    baseLineDists[axisId] = scaleWidget.margin()
                    scaleWidget.setMargin(0)
                if not plot.axisEnabled(axisId):
                    left, right, top, bottom = 0, 0, 0, 0
                    if axisId == QwtPlot.yLeft:
                        layoutRect.adjust(1, 0, 0, 0)
                    elif axisId == QwtPlot.yRight:
                        layoutRect.adjust(0, 0, -1, 0)
                    elif axisId == QwtPlot.xTop:
                        layoutRect.adjust(0, 1, 0, 0)
                    elif axisId == QwtPlot.xBottom:
                        layoutRect.adjust(0, 0, 0, -1)
                    layoutRect.adjust(left, top, right, bottom)
        
        layoutOptions = QwtPlotLayout.IgnoreScrollbars
        
        if self.__data.layoutFlags & self.FrameWithScales or\
           self.__data.discardFlags & self.DiscardCanvasFrame:
            layoutOptions |= QwtPlotLayout.IgnoreFrames
        
        if self.__data.discardFlags & self.DiscardLegend:
            layoutOptions |= QwtPlotLayout.IgnoreLegend
        if self.__data.discardFlags & self.DiscardTitle:
            layoutOptions |= QwtPlotLayout.IgnoreTitle
        if self.__data.discardFlags & self.DiscardFooter:
            layoutOptions |= QwtPlotLayout.IgnoreFooter
        
        layout.activate(plot, layoutRect, layoutOptions)

        maps = self.buildCanvasMaps(plot, layout.canvasRect())
        if self.updateCanvasMargins(plot, layout.canvasRect(), maps):
            layout.activate(plot, layoutRect, layoutOptions)
            maps = self.buildCanvasMaps(plot, layout.canvasRect())
        
        painter.save()
        painter.setWorldTransform(transform, True)
        
        self.renderCanvas(plot, painter, layout.canvasRect(), maps)
        
        if (not self.__data.discardFlags & self.DiscardTitle) and\
           plot.titleLabel().text():
            self.renderTitle(plot, painter, layout.titleRect())
        
        if (not self.__data.discardFlags & self.DiscardFooter) and\
           plot.titleLabel().text():
            self.renderFooter(plot, painter, layout.footerRect())
            
        if (not self.__data.discardFlags & self.DiscardLegend) and\
           plot.titleLabel().text():
            self.renderLegend(plot, painter, layout.legendRect())
            
        for axisId in range(QwtPlot.axisCnt):
            scaleWidget = plot.axisWidget(axisId)
            if scaleWidget:
                baseDist = scaleWidget.margin()
                startDist, endDist = scaleWidget.getBorderDistHint()
                self.renderScale(plot, painter, axisId, startDist, endDist,
                                 baseDist, layout.scaleRect(axisId))
        
        painter.restore()
        
        for axisId in range(QwtPlot.axisCnt):
            if self.__data.layoutFlags & self.FrameWithScales:
                scaleWidget = plot.axisWidget(axisId)
                if scaleWidget:
                    scaleWidget.setMargin(baseLineDists[axisId])
            layout.setCanvasMargin(canvasMargins[axisId])

        layout.invalidate()
コード例 #9
0
ファイル: plot_renderer.py プロジェクト: gyenney/Tools
    def renderCanvas(self, plot, painter, canvasRect, maps):
        """
        Render the canvas into a given rectangle.

        :param qwt.plot.QwtPlot plot: Plot widget
        :param QPainter painter: Painter
        :param qwt.scale_map.QwtScaleMap maps: mapping between plot and paint device coordinates
        :param QRectF rect: Bounding rectangle
        """
        canvas = plot.canvas()
        r = canvasRect.adjusted(0., 0., -1., 1.)
        if self.__data.layoutFlags & self.FrameWithScales:
            painter.save()
            r.adjust(-1., -1., 1., 1.)
            painter.setPen(QPen(Qt.black))
            if not (self.__data.discardFlags & self.DiscardCanvasBackground):
                bgBrush = canvas.palette().brush(plot.backgroundRole())
                painter.setBrush(bgBrush)
            QwtPainter.drawRect(painter, r)
            painter.restore()
            painter.save()
            painter.setClipRect(canvasRect)
            plot.drawItems(painter, canvasRect, maps)
            painter.restore()
        elif canvas.testAttribute(Qt.WA_StyledBackground):
            clipPath = QPainterPath()
            painter.save()
            if not self.__data.discardFlags & self.DiscardCanvasBackground:
                QwtPainter.drawBackground(painter, r, canvas)
                clipPath = qwtCanvasClip(canvas, canvasRect)
            painter.restore()
            painter.save()
            if clipPath.isEmpty():
                painter.setClipRect(canvasRect)
            else:
                painter.setClipPath(clipPath)
            plot.drawItems(painter, canvasRect, maps)
            painter.restore()
        else:
            clipPath = QPainterPath()
            frameWidth = 0
            if not self.__data.discardFlags & self.DiscardCanvasFrame:
                frameWidth = canvas.frameWidth()
                clipPath = qwtCanvasClip(canvas, canvasRect)
            innerRect = canvasRect.adjusted(frameWidth, frameWidth,
                                            -frameWidth, -frameWidth)
            painter.save()
            if clipPath.isEmpty():
                painter.setClipRect(innerRect)
            else:
                painter.setClipPath(clipPath)
            if not self.__data.discardFlags & self.DiscardCanvasBackground:
                QwtPainter.drawBackground(painter, innerRect, canvas)
            plot.drawItems(painter, innerRect, maps)
            painter.restore()
            if frameWidth > 0:
                painter.save()
                frameStyle = canvas.frameShadow() | canvas.frameShape()
                frameWidth = canvas.frameWidth()
                borderRadius = canvas.borderRadius()
                if borderRadius > 0.:
                    QwtPainter.drawRoundedFrame(painter, canvasRect, r, r,
                                                canvas.palette(), frameWidth,
                                                frameStyle)
                else:
                    midLineWidth = canvas.midLineWidth()
                    QwtPainter.drawFrame(painter, canvasRect, canvas.palette(),
                                         canvas.foregroundRole(), frameWidth,
                                         midLineWidth, frameStyle)
                painter.restore()
コード例 #10
0
ファイル: plot_renderer.py プロジェクト: gyenney/Tools
    def render(self, plot, painter, plotRect):
        """
        Paint the contents of a QwtPlot instance into a given rectangle.

        :param qwt.plot.QwtPlot plot: Plot to be rendered
        :param QPainter painter: Painter
        :param str format: Format for the document
        :param QRectF plotRect: Bounding rectangle
        
        .. seealso::
        
            :py:meth:`renderDocument()`, :py:meth:`renderTo()`, 
            :py:meth:`qwt.painter.QwtPainter.setRoundingAlignment()`
        """
        if painter == 0 or not painter.isActive() or not plotRect.isValid()\
           or plot.size().isNull():
            return
        if not self.__data.discardFlags & self.DiscardBackground:
            QwtPainter.drawBackground(painter, plotRect, plot)

        #  The layout engine uses the same methods as they are used
        #  by the Qt layout system. Therefore we need to calculate the
        #  layout in screen coordinates and paint with a scaled painter.
        transform = QTransform()
        transform.scale(
            float(painter.device().logicalDpiX()) / plot.logicalDpiX(),
            float(painter.device().logicalDpiY()) / plot.logicalDpiY())

        invtrans, _ok = transform.inverted()
        layoutRect = invtrans.mapRect(plotRect)
        if not (self.__data.discardFlags & self.DiscardBackground):
            left, top, right, bottom = plot.getContentsMargins()
            layoutRect.adjust(left, top, -right, -bottom)

        layout = plot.plotLayout()
        baseLineDists = [None] * QwtPlot.axisCnt
        canvasMargins = [None] * QwtPlot.axisCnt

        for axisId in range(QwtPlot.axisCnt):
            canvasMargins[axisId] = layout.canvasMargin(axisId)
            if self.__data.layoutFlags & self.FrameWithScales:
                scaleWidget = plot.axisWidget(axisId)
                if scaleWidget:
                    baseLineDists[axisId] = scaleWidget.margin()
                    scaleWidget.setMargin(0)
                if not plot.axisEnabled(axisId):
                    left, right, top, bottom = 0, 0, 0, 0
                    #  When we have a scale the frame is painted on
                    #  the position of the backbone - otherwise we
                    #  need to introduce a margin around the canvas
                    if axisId == QwtPlot.yLeft:
                        layoutRect.adjust(1, 0, 0, 0)
                    elif axisId == QwtPlot.yRight:
                        layoutRect.adjust(0, 0, -1, 0)
                    elif axisId == QwtPlot.xTop:
                        layoutRect.adjust(0, 1, 0, 0)
                    elif axisId == QwtPlot.xBottom:
                        layoutRect.adjust(0, 0, 0, -1)
                    layoutRect.adjust(left, top, right, bottom)

        #  Calculate the layout for the document.
        layoutOptions = QwtPlotLayout.IgnoreScrollbars

        if self.__data.layoutFlags & self.FrameWithScales or\
           self.__data.discardFlags & self.DiscardCanvasFrame:
            layoutOptions |= QwtPlotLayout.IgnoreFrames

        if self.__data.discardFlags & self.DiscardLegend:
            layoutOptions |= QwtPlotLayout.IgnoreLegend
        if self.__data.discardFlags & self.DiscardTitle:
            layoutOptions |= QwtPlotLayout.IgnoreTitle
        if self.__data.discardFlags & self.DiscardFooter:
            layoutOptions |= QwtPlotLayout.IgnoreFooter

        layout.activate(plot, layoutRect, layoutOptions)

        maps = self.buildCanvasMaps(plot, layout.canvasRect())
        if self.updateCanvasMargins(plot, layout.canvasRect(), maps):
            #  recalculate maps and layout, when the margins
            #  have been changed
            layout.activate(plot, layoutRect, layoutOptions)
            maps = self.buildCanvasMaps(plot, layout.canvasRect())

        painter.save()
        painter.setWorldTransform(transform, True)

        self.renderCanvas(plot, painter, layout.canvasRect(), maps)

        if (not self.__data.discardFlags & self.DiscardTitle) and\
           plot.titleLabel().text():
            self.renderTitle(plot, painter, layout.titleRect())

        if (not self.__data.discardFlags & self.DiscardFooter) and\
           plot.titleLabel().text():
            self.renderFooter(plot, painter, layout.footerRect())

        if (not self.__data.discardFlags & self.DiscardLegend) and\
           plot.titleLabel().text():
            self.renderLegend(plot, painter, layout.legendRect())

        for axisId in range(QwtPlot.axisCnt):
            scaleWidget = plot.axisWidget(axisId)
            if scaleWidget:
                baseDist = scaleWidget.margin()
                startDist, endDist = scaleWidget.getBorderDistHint()
                self.renderScale(plot, painter, axisId, startDist, endDist,
                                 baseDist, layout.scaleRect(axisId))

        painter.restore()

        for axisId in range(QwtPlot.axisCnt):
            if self.__data.layoutFlags & self.FrameWithScales:
                scaleWidget = plot.axisWidget(axisId)
                if scaleWidget:
                    scaleWidget.setMargin(baseLineDists[axisId])
            layout.setCanvasMargin(canvasMargins[axisId])

        layout.invalidate()
コード例 #11
0
ファイル: plot_renderer.py プロジェクト: robinkqin/PythonQwt
    def renderCanvas(self, plot, painter, canvasRect, maps):
        """
        Render the canvas into a given rectangle.

        :param qwt.plot.QwtPlot plot: Plot widget
        :param QPainter painter: Painter
        :param qwt.scale_map.QwtScaleMap maps: mapping between plot and paint device coordinates
        :param QRectF rect: Bounding rectangle
        """
        canvas = plot.canvas()
        r = canvasRect.adjusted(0., 0., -1., 1.)
        if self.__data.layoutFlags & self.FrameWithScales:
            painter.save()
            r.adjust(-1., -1., 1., 1.)
            painter.setPen(QPen(Qt.black))
            if not (self.__data.discardFlags & self.DiscardCanvasBackground):
                bgBrush = canvas.palette().brush(plot.backgroundRole())
                painter.setBrush(bgBrush)
            painter.drawRect(r)
            painter.restore()
            painter.save()
            painter.setClipRect(canvasRect)
            plot.drawItems(painter, canvasRect, maps)
            painter.restore()
        elif canvas.testAttribute(Qt.WA_StyledBackground):
            clipPath = QPainterPath()
            painter.save()
            if not self.__data.discardFlags & self.DiscardCanvasBackground:
                QwtPainter.drawBackground(painter, r, canvas)
                clipPath = qwtCanvasClip(canvas, canvasRect)
            painter.restore()
            painter.save()
            if clipPath.isEmpty():
                painter.setClipRect(canvasRect)
            else:
                painter.setClipPath(clipPath)
            plot.drawItems(painter, canvasRect, maps)
            painter.restore()
        else:
            clipPath = QPainterPath()
            frameWidth = 0
            if not self.__data.discardFlags & self.DiscardCanvasFrame:
                frameWidth = canvas.frameWidth()
                clipPath = qwtCanvasClip(canvas, canvasRect)
            innerRect = canvasRect.adjusted(frameWidth, frameWidth,
                                            -frameWidth, -frameWidth)
            painter.save()
            if clipPath.isEmpty():
                painter.setClipRect(innerRect)
            else:
                painter.setClipPath(clipPath)
            if not self.__data.discardFlags & self.DiscardCanvasBackground:
                QwtPainter.drawBackground(painter, innerRect, canvas)
            plot.drawItems(painter, innerRect, maps)
            painter.restore()
            if frameWidth > 0:
                painter.save()
                frameStyle = canvas.frameShadow() | canvas.frameShape()
                frameWidth = canvas.frameWidth()
                borderRadius = canvas.borderRadius()
                if borderRadius > 0.:
                    QwtPainter.drawRoundedFrame(painter, canvasRect, r, r,
                                                canvas.palette(), frameWidth,
                                                frameStyle)
                else:
                    midLineWidth = canvas.midLineWidth()
                    QwtPainter.drawFrame(painter, canvasRect, canvas.palette(),
                                         canvas.foregroundRole(), frameWidth,
                                         midLineWidth, frameStyle)
                painter.restore()
コード例 #12
0
ファイル: plot_renderer.py プロジェクト: robinkqin/PythonQwt
    def render(self, plot, painter, plotRect):
        """
        Paint the contents of a QwtPlot instance into a given rectangle.

        :param qwt.plot.QwtPlot plot: Plot to be rendered
        :param QPainter painter: Painter
        :param str format: Format for the document
        :param QRectF plotRect: Bounding rectangle
        
        .. seealso::
        
            :py:meth:`renderDocument()`, :py:meth:`renderTo()`, 
            :py:meth:`qwt.painter.QwtPainter.setRoundingAlignment()`
        """
        if painter == 0 or not painter.isActive() or not plotRect.isValid()\
           or plot.size().isNull():
            return
        if not self.__data.discardFlags & self.DiscardBackground:
            QwtPainter.drawBackground(painter, plotRect, plot)

        #  The layout engine uses the same methods as they are used
        #  by the Qt layout system. Therefore we need to calculate the
        #  layout in screen coordinates and paint with a scaled painter.
        transform = QTransform()
        transform.scale(float(painter.device().logicalDpiX())/plot.logicalDpiX(),
                        float(painter.device().logicalDpiY())/plot.logicalDpiY())
        
        invtrans, _ok = transform.inverted()
        layoutRect = invtrans.mapRect(plotRect)
        if not (self.__data.discardFlags & self.DiscardBackground):
            left, top, right, bottom = plot.getContentsMargins()
            layoutRect.adjust(left, top, -right, -bottom)

        layout = plot.plotLayout()
        baseLineDists = [None]*QwtPlot.axisCnt
        canvasMargins = [None]*QwtPlot.axisCnt

        for axisId in QwtPlot.validAxes:
            canvasMargins[axisId] = layout.canvasMargin(axisId)
            if self.__data.layoutFlags & self.FrameWithScales:
                scaleWidget = plot.axisWidget(axisId)
                if scaleWidget:
                    baseLineDists[axisId] = scaleWidget.margin()
                    scaleWidget.setMargin(0)
                if not plot.axisEnabled(axisId):
                    left, right, top, bottom = 0, 0, 0, 0
                    #  When we have a scale the frame is painted on
                    #  the position of the backbone - otherwise we
                    #  need to introduce a margin around the canvas
                    if axisId == QwtPlot.yLeft:
                        layoutRect.adjust(1, 0, 0, 0)
                    elif axisId == QwtPlot.yRight:
                        layoutRect.adjust(0, 0, -1, 0)
                    elif axisId == QwtPlot.xTop:
                        layoutRect.adjust(0, 1, 0, 0)
                    elif axisId == QwtPlot.xBottom:
                        layoutRect.adjust(0, 0, 0, -1)
                    layoutRect.adjust(left, top, right, bottom)
        
        #  Calculate the layout for the document.
        layoutOptions = QwtPlotLayout.IgnoreScrollbars
        
        if self.__data.layoutFlags & self.FrameWithScales or\
           self.__data.discardFlags & self.DiscardCanvasFrame:
            layoutOptions |= QwtPlotLayout.IgnoreFrames
        
        if self.__data.discardFlags & self.DiscardLegend:
            layoutOptions |= QwtPlotLayout.IgnoreLegend
        if self.__data.discardFlags & self.DiscardTitle:
            layoutOptions |= QwtPlotLayout.IgnoreTitle
        if self.__data.discardFlags & self.DiscardFooter:
            layoutOptions |= QwtPlotLayout.IgnoreFooter
        
        layout.activate(plot, layoutRect, layoutOptions)

        maps = self.buildCanvasMaps(plot, layout.canvasRect())
        if self.updateCanvasMargins(plot, layout.canvasRect(), maps):
            #  recalculate maps and layout, when the margins
            #  have been changed
            layout.activate(plot, layoutRect, layoutOptions)
            maps = self.buildCanvasMaps(plot, layout.canvasRect())
        
        painter.save()
        painter.setWorldTransform(transform, True)
        
        self.renderCanvas(plot, painter, layout.canvasRect(), maps)
        
        if (not self.__data.discardFlags & self.DiscardTitle) and\
           plot.titleLabel().text():
            self.renderTitle(plot, painter, layout.titleRect())
        
        if (not self.__data.discardFlags & self.DiscardFooter) and\
           plot.titleLabel().text():
            self.renderFooter(plot, painter, layout.footerRect())
            
        if (not self.__data.discardFlags & self.DiscardLegend) and\
           plot.titleLabel().text():
            self.renderLegend(plot, painter, layout.legendRect())
            
        for axisId in QwtPlot.validAxes:
            scaleWidget = plot.axisWidget(axisId)
            if scaleWidget:
                baseDist = scaleWidget.margin()
                startDist, endDist = scaleWidget.getBorderDistHint()
                self.renderScale(plot, painter, axisId, startDist, endDist,
                                 baseDist, layout.scaleRect(axisId))
        
        painter.restore()
        
        for axisId in QwtPlot.validAxes:
            if self.__data.layoutFlags & self.FrameWithScales:
                scaleWidget = plot.axisWidget(axisId)
                if scaleWidget:
                    scaleWidget.setMargin(baseLineDists[axisId])
            layout.setCanvasMargin(canvasMargins[axisId])

        layout.invalidate()
コード例 #13
0
 def renderCanvas(self, plot, painter, canvasRect, maps):
     canvas = plot.canvas()
     r = canvasRect.adjusted(0., 0., -1., 1.)
     if self.__data.layoutFlags & self.FrameWithScales:
         painter.save()
         r.adjust(-1., -1., 1., 1.)
         painter.setPen(QPen(Qt.black))
         if not (self.__data.discardFlags & self.DiscardCanvasBackground):
             bgBrush = canvas.palette().brush(plot.backgroundRole())
             painter.setBrush(bgBrush)
         QwtPainter.drawRect(painter, r)
         painter.restore()
         painter.save()
         painter.setClipRect(canvasRect)
         plot.drawItems(painter, canvasRect, maps)
         painter.restore()
     elif canvas.testAttribute(Qt.WA_StyledBackground):
         clipPath = QPainterPath()
         painter.save()
         if not self.__data.discardFlags & self.DiscardCanvasBackground:
             QwtPainter.drawBackground(painter, r, canvas)
             clipPath = qwtCanvasClip(canvas, canvasRect)
         painter.restore()
         painter.save()
         if clipPath.isEmpty():
             painter.setClipRect(canvasRect)
         else:
             painter.setClipPath(clipPath)
         plot.drawItems(painter, canvasRect, maps)
         painter.restore()
     else:
         clipPath = QPainterPath()
         frameWidth = 0
         if not self.__data.discardFlags & self.DiscardCanvasFrame:
             frameWidth = canvas.frameWidth()
             clipPath = qwtCanvasClip(canvas, canvasRect)
         innerRect = canvasRect.adjusted(frameWidth, frameWidth,
                                         -frameWidth, -frameWidth)
         painter.save()
         if clipPath.isEmpty():
             painter.setClipRect(innerRect)
         else:
             painter.setClipPath(clipPath)
         if not self.__data.discardFlags & self.DiscardCanvasBackground:
             QwtPainter.drawBackground(painter, innerRect, canvas)
         plot.drawItems(painter, innerRect, maps)
         painter.restore()
         if frameWidth > 0:
             painter.save()
             frameStyle = canvas.frameShadow() | canvas.frameShape()
             frameWidth = canvas.frameWidth()
             borderRadius = canvas.borderRadius()
             if borderRadius > 0.:
                 QwtPainter.drawRoundedFrame(painter, canvasRect, r, r,
                                             canvas.palette(), frameWidth,
                                             frameStyle)
             else:
                 midLineWidth = canvas.midLineWidth()
                 QwtPainter.drawFrame(painter, canvasRect, canvas.palette(),
                                      canvas.foregroundRole(), frameWidth,
                                      midLineWidth, frameStyle)
             painter.restore()
コード例 #14
0
    def render(self, plot, painter, plotRect):
        if painter == 0 or not painter.isActive() or not plotRect.isValid()\
           or plot.size().isNull():
            return
        if not self.__data.discardFlags & self.DiscardBackground:
            QwtPainter.drawBackground(painter, plotRect, plot)

        transform = QTransform()
        transform.scale(
            float(painter.device().logicalDpiX()) / plot.logicalDpiX(),
            float(painter.device().logicalDpiY()) / plot.logicalDpiY())

        invtrans, _ok = transform.inverted()
        layoutRect = invtrans.mapRect(plotRect)
        if not (self.__data.discardFlags & self.DiscardBackground):
            left, top, right, bottom = plot.getContentsMargins()
            layoutRect.adjust(left, top, -right, -bottom)

        layout = plot.plotLayout()
        baseLineDists = [None] * QwtPlot.axisCnt
        canvasMargins = [None] * QwtPlot.axisCnt

        for axisId in range(QwtPlot.axisCnt):
            canvasMargins[axisId] = layout.canvasMargin(axisId)
            if self.__data.layoutFlags & self.FrameWithScales:
                scaleWidget = plot.axisWidget(axisId)
                if scaleWidget:
                    baseLineDists[axisId] = scaleWidget.margin()
                    scaleWidget.setMargin(0)
                if not plot.axisEnabled(axisId):
                    left, right, top, bottom = 0, 0, 0, 0
                    if axisId == QwtPlot.yLeft:
                        layoutRect.adjust(1, 0, 0, 0)
                    elif axisId == QwtPlot.yRight:
                        layoutRect.adjust(0, 0, -1, 0)
                    elif axisId == QwtPlot.xTop:
                        layoutRect.adjust(0, 1, 0, 0)
                    elif axisId == QwtPlot.xBottom:
                        layoutRect.adjust(0, 0, 0, -1)
                    layoutRect.adjust(left, top, right, bottom)

        layoutOptions = QwtPlotLayout.IgnoreScrollbars

        if self.__data.layoutFlags & self.FrameWithScales or\
           self.__data.discardFlags & self.DiscardCanvasFrame:
            layoutOptions |= QwtPlotLayout.IgnoreFrames

        if self.__data.discardFlags & self.DiscardLegend:
            layoutOptions |= QwtPlotLayout.IgnoreLegend
        if self.__data.discardFlags & self.DiscardTitle:
            layoutOptions |= QwtPlotLayout.IgnoreTitle
        if self.__data.discardFlags & self.DiscardFooter:
            layoutOptions |= QwtPlotLayout.IgnoreFooter

        layout.activate(plot, layoutRect, layoutOptions)

        maps = self.buildCanvasMaps(plot, layout.canvasRect())
        if self.updateCanvasMargins(plot, layout.canvasRect(), maps):
            layout.activate(plot, layoutRect, layoutOptions)
            maps = self.buildCanvasMaps(plot, layout.canvasRect())

        painter.save()
        painter.setWorldTransform(transform, True)

        self.renderCanvas(plot, painter, layout.canvasRect(), maps)

        if (not self.__data.discardFlags & self.DiscardTitle) and\
           plot.titleLabel().text():
            self.renderTitle(plot, painter, layout.titleRect())

        if (not self.__data.discardFlags & self.DiscardFooter) and\
           plot.titleLabel().text():
            self.renderFooter(plot, painter, layout.footerRect())

        if (not self.__data.discardFlags & self.DiscardLegend) and\
           plot.titleLabel().text():
            self.renderLegend(plot, painter, layout.legendRect())

        for axisId in range(QwtPlot.axisCnt):
            scaleWidget = plot.axisWidget(axisId)
            if scaleWidget:
                baseDist = scaleWidget.margin()
                startDist, endDist = scaleWidget.getBorderDistHint()
                self.renderScale(plot, painter, axisId, startDist, endDist,
                                 baseDist, layout.scaleRect(axisId))

        painter.restore()

        for axisId in range(QwtPlot.axisCnt):
            if self.__data.layoutFlags & self.FrameWithScales:
                scaleWidget = plot.axisWidget(axisId)
                if scaleWidget:
                    scaleWidget.setMargin(baseLineDists[axisId])
            layout.setCanvasMargin(canvasMargins[axisId])

        layout.invalidate()