Пример #1
0
    def fillPixmap(self, widget, pixmap, offset=None):
        """
        Fill a pixmap with the content of a widget

        In Qt >= 5.0 `QPixmap.fill()` is a nop, in Qt 4.x it is buggy
        for backgrounds with gradients. Thus `fillPixmap()` offers 
        an alternative implementation.
        
        :param QWidget widget: Widget
        :param QPixmap pixmap: Pixmap to be filled
        :param QPoint offset: Offset
        
        .. seealso::
        
            :py:meth:`QPixmap.fill()`
        """
        if offset is None:
            offset = QPoint()
        rect = QRect(offset, pixmap.size())
        painter = QPainter(pixmap)
        painter.translate(-offset)
        autoFillBrush = widget.palette().brush(widget.backgroundRole())
        if not (widget.autoFillBackground() and autoFillBrush.isOpaque()):
            bg = widget.palette().brush(QPalette.Window)
            qwtFillRect(widget, painter, rect, bg)
        if widget.autoFillBackground():
            qwtFillRect(widget, painter, rect, autoFillBrush)
        if widget.testAttribute(Qt.WA_StyledBackground):
            painter.setClipRegion(rect)
            opt = QStyleOption()
            opt.initFrom(widget)
            widget.style().drawPrimitive(QStyle.PE_Widget, opt,
                                         painter, widget)
Пример #2
0
 def drawColorBar(self, painter, colorMap, interval, scaleMap,
                  orientation, rect):
     colorTable = []
     if colorMap.format() == QwtColorMap.Indexed:
         colorTable = colorMap.colorTable(interval)
     c = QColor()
     devRect = rect.toAlignedRect()
     pixmap = QPixmap(devRect.size())
     pixmap.fill(Qt.transparent)
     pmPainter = QPainter(pixmap)
     pmPainter.translate(-devRect.x(), -devRect.y())
     if orientation == Qt.Horizontal:
         sMap = scaleMap
         sMap.setPaintInterval(rect.left(), rect.right())
         for x in range(devRect.left(), devRect.right()+1):
             value = sMap.invTransform(x)
             if colorMap.format() == QwtColorMap.RGB:
                 c.setRgba(colorMap.rgb(interval, value))
             else:
                 c = colorTable[colorMap.colorIndex(interval, value)]
             pmPainter.setPen(c)
             pmPainter.drawLine(x, devRect.top(), devRect.bottom())
     else:
         sMap = scaleMap
         sMap.setPaintInterval(rect.bottom(), rect.top())
         for y in range(devRect.top(), devRect.bottom()+1):
             value = sMap.invTransform(y)
             if colorMap.format() == QwtColorMap.RGB:
                 c.setRgba(colorMap.rgb(interval, value))
             else:
                 c = colorTable[colorMap.colorIndex(interval, value)]
             pmPainter.setPen(c)
             pmPainter.drawLine(devRect.left(), y, devRect.right(), y)
     pmPainter.end()
     self.drawPixmap(painter, rect, pixmap)
Пример #3
0
 def drawColorBar(self, painter, colorMap, interval, scaleMap, orientation,
                  rect):
     colorTable = []
     if colorMap.format() == QwtColorMap.Indexed:
         colorTable = colorMap.colorTable(interval)
     c = QColor()
     devRect = rect.toAlignedRect()
     pixmap = QPixmap(devRect.size())
     pixmap.fill(Qt.transparent)
     pmPainter = QPainter(pixmap)
     pmPainter.translate(-devRect.x(), -devRect.y())
     if orientation == Qt.Horizontal:
         sMap = scaleMap
         sMap.setPaintInterval(rect.left(), rect.right())
         for x in range(devRect.left(), devRect.right() + 1):
             value = sMap.invTransform(x)
             if colorMap.format() == QwtColorMap.RGB:
                 c.setRgba(colorMap.rgb(interval, value))
             else:
                 c = colorTable[colorMap.colorIndex(interval, value)]
             pmPainter.setPen(c)
             pmPainter.drawLine(x, devRect.top(), devRect.bottom())
     else:
         sMap = scaleMap
         sMap.setPaintInterval(rect.bottom(), rect.top())
         for y in range(devRect.top(), devRect.bottom() + 1):
             value = sMap.invTransform(y)
             if colorMap.format() == QwtColorMap.RGB:
                 c.setRgba(colorMap.rgb(interval, value))
             else:
                 c = colorTable[colorMap.colorIndex(interval, value)]
             pmPainter.setPen(c)
             pmPainter.drawLine(devRect.left(), y, devRect.right(), y)
     pmPainter.end()
     self.drawPixmap(painter, rect, pixmap)
Пример #4
0
    def fillPixmap(self, widget, pixmap, offset=None):
        """
        Fill a pixmap with the content of a widget

        In Qt >= 5.0 `QPixmap.fill()` is a nop, in Qt 4.x it is buggy
        for backgrounds with gradients. Thus `fillPixmap()` offers 
        an alternative implementation.
        
        :param QWidget widget: Widget
        :param QPixmap pixmap: Pixmap to be filled
        :param QPoint offset: Offset
        
        .. seealso::
        
            :py:meth:`QPixmap.fill()`
        """
        if offset is None:
            offset = QPoint()
        rect = QRect(offset, pixmap.size())
        painter = QPainter(pixmap)
        painter.translate(-offset)
        autoFillBrush = widget.palette().brush(widget.backgroundRole())
        if not (widget.autoFillBackground() and autoFillBrush.isOpaque()):
            bg = widget.palette().brush(QPalette.Window)
            qwtFillRect(widget, painter, rect, bg)
        if widget.autoFillBackground():
            qwtFillRect(widget, painter, rect, autoFillBrush)
        if widget.testAttribute(Qt.WA_StyledBackground):
            painter.setClipRegion(rect)
            opt = QStyleOption()
            opt.initFrom(widget)
            widget.style().drawPrimitive(QStyle.PE_Widget, opt, painter,
                                         widget)
Пример #5
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()
Пример #6
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()
Пример #7
0
def qwtBackgroundWidget(w):
    if w.parentWidget() is None:
        return w
    if w.autoFillBackground():
        brush = w.palette().brush(w.backgroundRole())
        if brush.color().alpha() > 0:
            return w
    if w.testAttribute(Qt.WA_StyledBackground):
        image = QImage(1, 1, QImage.Format_ARGB32)
        image.fill(Qt.transparent)
        painter = QPainter(image)
        painter.translate(-w.rect().center())
        qwtDrawStyledBackground(w, painter)
        painter.end()
        if qAlpha(image.pixel(0, 0)) != 0:
            return w
    return qwtBackgroundWidget(w.parentWidget())
Пример #8
0
def qwtBackgroundWidget(w):
    if w.parentWidget() is None:
        return w
    if w.autoFillBackground():
        brush = w.palette().brush(w.backgroundRole())
        if brush.color().alpha() > 0:
            return w
    if w.testAttribute(Qt.WA_StyledBackground):
        image = QImage(1, 1, QImage.Format_ARGB32)
        image.fill(Qt.transparent)
        painter = QPainter(image)
        painter.translate(-w.rect().center())
        qwtDrawStyledBackground(w, painter)
        painter.end()
        if qAlpha(image.pixel(0, 0)) != 0:
            return w
    return qwtBackgroundWidget(w.parentWidget())
Пример #9
0
 def drawColorBar(self, painter, colorMap, interval, scaleMap,
                  orientation, rect):
     """
     Draw a color bar into a rectangle
     
     :param QPainter painter: Painter
     :param qwt.color_map.QwtColorMap colorMap: Color map
     :param qwt.interval.QwtInterval interval: Value range
     :param qwt.scalemap.QwtScaleMap scaleMap: Scale map
     :param Qt.Orientation orientation: Orientation
     :param QRectF rect: Target rectangle
     """
     colorTable = []
     if colorMap.format() == QwtColorMap.Indexed:
         colorTable = colorMap.colorTable(interval)
     c = QColor()
     devRect = rect.toAlignedRect()
     pixmap = QPixmap(devRect.size())
     pixmap.fill(Qt.transparent)
     pmPainter = QPainter(pixmap)
     pmPainter.translate(-devRect.x(), -devRect.y())
     if orientation == Qt.Horizontal:
         sMap = QwtScaleMap(scaleMap)
         sMap.setPaintInterval(rect.left(), rect.right())
         for x in range(devRect.left(), devRect.right()+1):
             value = sMap.invTransform(x)
             if colorMap.format() == QwtColorMap.RGB:
                 c.setRgba(colorMap.rgb(interval, value))
             else:
                 c = colorTable[colorMap.colorIndex(interval, value)]
             pmPainter.setPen(c)
             pmPainter.drawLine(x, devRect.top(), x, devRect.bottom())
     else:
         sMap = QwtScaleMap(scaleMap)
         sMap.setPaintInterval(rect.bottom(), rect.top())
         for y in range(devRect.top(), devRect.bottom()+1):
             value = sMap.invTransform(y)
             if colorMap.format() == QwtColorMap.RGB:
                 c.setRgba(colorMap.rgb(interval, value))
             else:
                 c = colorTable[colorMap.colorIndex(interval, value)]
             pmPainter.setPen(c)
             pmPainter.drawLine(devRect.left(), y, devRect.right(), y)
     pmPainter.end()
     self.drawPixmap(painter, rect, pixmap)
Пример #10
0
 def drawColorBar(self, painter, colorMap, interval, scaleMap, orientation,
                  rect):
     """
     Draw a color bar into a rectangle
     
     :param QPainter painter: Painter
     :param qwt.color_map.QwtColorMap colorMap: Color map
     :param qwt.interval.QwtInterval interval: Value range
     :param qwt.scalemap.QwtScaleMap scaleMap: Scale map
     :param Qt.Orientation orientation: Orientation
     :param QRectF rect: Target rectangle
     """
     colorTable = []
     if colorMap.format() == QwtColorMap.Indexed:
         colorTable = colorMap.colorTable(interval)
     c = QColor()
     devRect = rect.toAlignedRect()
     pixmap = QPixmap(devRect.size())
     pixmap.fill(Qt.transparent)
     pmPainter = QPainter(pixmap)
     pmPainter.translate(-devRect.x(), -devRect.y())
     if orientation == Qt.Horizontal:
         sMap = QwtScaleMap(scaleMap)
         sMap.setPaintInterval(rect.left(), rect.right())
         for x in range(devRect.left(), devRect.right() + 1):
             value = sMap.invTransform(x)
             if colorMap.format() == QwtColorMap.RGB:
                 c.setRgba(colorMap.rgb(interval, value))
             else:
                 c = colorTable[colorMap.colorIndex(interval, value)]
             pmPainter.setPen(c)
             pmPainter.drawLine(x, devRect.top(), x, devRect.bottom())
     else:
         sMap = QwtScaleMap(scaleMap)
         sMap.setPaintInterval(rect.bottom(), rect.top())
         for y in range(devRect.top(), devRect.bottom() + 1):
             value = sMap.invTransform(y)
             if colorMap.format() == QwtColorMap.RGB:
                 c.setRgba(colorMap.rgb(interval, value))
             else:
                 c = colorTable[colorMap.colorIndex(interval, value)]
             pmPainter.setPen(c)
             pmPainter.drawLine(devRect.left(), y, devRect.right(), y)
     pmPainter.end()
     self.drawPixmap(painter, rect, pixmap)
Пример #11
0
 def fillPixmap(self, widget, pixmap, offset=None):
     if offset is None:
         offset = QPoint()
     rect = QRect(offset, pixmap.size())
     painter = QPainter(pixmap)
     painter.translate(-offset)
     autoFillBrush = widget.palette().brush(widget.backgroundRole())
     if not (widget.autoFillBackground() and autoFillBrush.isOpaque()):
         bg = widget.palette().brush(QPalette.Window)
         qwtFillRect(widget, painter, rect, bg)
     if widget.autoFillBackground():
         qwtFillRect(widget, painter, rect, autoFillBrush)
     if widget.testAttribute(Qt.WA_StyledBackground):
         painter.setClipRegion(rect)
         opt = QStyleOption()
         opt.initFrom(widget)
         widget.style().drawPrimitive(QStyle.PE_Widget, opt,
                                      painter, widget)
Пример #12
0
 def fillPixmap(self, widget, pixmap, offset=None):
     if offset is None:
         offset = QPoint()
     rect = QRect(offset, pixmap.size())
     painter = QPainter(pixmap)
     painter.translate(-offset)
     autoFillBrush = widget.palette().brush(widget.backgroundRole())
     if not (widget.autoFillBackground() and autoFillBrush.isOpaque()):
         bg = widget.palette().brush(QPalette.Window)
         qwtFillRect(widget, painter, rect, bg)
     if widget.autoFillBackground():
         qwtFillRect(widget, painter, rect, autoFillBrush)
     if widget.testAttribute(Qt.WA_StyledBackground):
         painter.setClipRegion(rect)
         opt = QStyleOption()
         opt.initFrom(widget)
         widget.style().drawPrimitive(QStyle.PE_Widget, opt, painter,
                                      widget)
Пример #13
0
 def drawSymbols(self, painter, points, numPoints=None):
     #TODO: remove argument numPoints (not necessary in Python's qwt)
     if numPoints is not None and numPoints <= 0:
         return
     useCache = False
     if QwtPainter.roundingAlignment(painter) and\
        not painter.transform().isScaling():
         if self.__data.cache.policy == QwtSymbol.Cache:
             useCache = True
         elif self.__data.cache.policy == QwtSymbol.AutoCache:
             if painter.paintEngine().type() == QPaintEngine.Raster:
                 useCache = True
             else:
                 if self.__data.style in (QwtSymbol.XCross, QwtSymbol.HLine,
                                          QwtSymbol.VLine, QwtSymbol.Cross):
                     pass
                 elif self.__data.style == QwtSymbol.Pixmap:
                     if not self.__data.size.isEmpty() and\
                        self.__data.size != self.__data.pixmap.pixmap.size():
                         useCache = True
                 else:
                     useCache = True
     if useCache:
         br = QRect(self.boundingRect())
         rect = QRect(0, 0, br.width(), br.height())
         if self.__data.cache.pixmap.isNull():
             self.__data.cache.pixmap = QwtPainter.backingStore(None, br.size())
             self.__data.cache.pixmap.fill(Qt.transparent)
             p = QPainter(self.__data.cache.pixmap)
             p.setRenderHints(painter.renderHints())
             p.translate(-br.topLeft())
             pos = QPointF()
             self.renderSymbols(p, pos, 1)
         dx = br.left()
         dy = br.top()
         for point in points:
             left = round(point.x())+dx
             top = round(point.y())+dy
             painter.drawPixmap(left, top, self.__data.cache.pixmap)
     else:
         painter.save()
         self.renderSymbols(painter, points, numPoints)
         painter.restore()
Пример #14
0
 def paintEvent(self, e):
     cr = self.contentsRect()
     painter = QPainter(self)
     painter.setClipRegion(e.region())
     if self.__data.isDown:
         qDrawWinButton(painter, 0, 0, self.width(), self.height(),
                        self.palette(), True)
     painter.save()
     if self.__data.isDown:
         shiftSize = buttonShift(self)
         painter.translate(shiftSize.width(), shiftSize.height())
     painter.setClipRect(cr)
     self.drawContents(painter)
     if not self.__data.icon.isNull():
         iconRect = QRect(cr)
         iconRect.setX(iconRect.x() + self.margin())
         if self.__data.itemMode != QwtLegendData.ReadOnly:
             iconRect.setX(iconRect.x() + BUTTONFRAME)
         iconRect.setSize(self.__data.icon.size())
         iconRect.moveCenter(QPoint(iconRect.center().x(), cr.center().y()))
         painter.drawPixmap(iconRect, self.__data.icon)
     painter.restore()
Пример #15
0
 def paintEvent(self, e):
     cr = self.contentsRect()
     painter = QPainter(self)
     painter.setClipRegion(e.region())
     if self.__data.isDown:
         qDrawWinButton(painter, 0, 0, self.width(), self.height(),
                        self.palette(), True)
     painter.save()
     if self.__data.isDown:
         shiftSize = buttonShift(self)
         painter.translate(shiftSize.width(), shiftSize.height())
     painter.setClipRect(cr)
     self.drawContents(painter)
     if not self.__data.icon.isNull():
         iconRect = QRect(cr)
         iconRect.setX(iconRect.x()+self.margin())
         if self.__data.itemMode != QwtLegendData.ReadOnly:
             iconRect.setX(iconRect.x()+BUTTONFRAME)
         iconRect.setSize(self.__data.icon.size())
         iconRect.moveCenter(QPoint(iconRect.center().x(),
                                    cr.center().y()))
         painter.drawPixmap(iconRect, self.__data.icon)
     painter.restore()