예제 #1
0
    def setProfile(self, x, y, colormap):
        """

        :param profile: a 1D numpy array
        :param colormap: an XsocsPlot2DColormap instance
        :param nColors: number of colors
        :return:
        """
        assert x.ndim == 1
        assert y.ndim == 1

        self.__colormap = colormap
        self.__pixmap = pixmap = Qt.QPixmap(Qt.QSize(x.size,
                                                     self._pimapHeight))
        pixmap.fill()

        xMin = x.min()
        xMax = x.max()

        colors = _applyColormap(colormap, x)
        profileValues = (y * (1.0 * self._pimapHeight / y.max()))
        points = [Qt.QPointF(0, 0)]
        points.extend(
            [Qt.QPointF(idx, val) for idx, val in enumerate(profileValues)])
        points.extend([Qt.QPointF(colormap.nColors - 1, 0)])
        poly = Qt.QPolygonF(points)

        if colormap.minVal is not None:
            lineMin = ((colormap.minVal - xMin) * (pixmap.width() - 1) /
                       (xMax - xMin))
        else:
            lineMin = None

        if colormap.maxVal is not None:
            lineMax = ((colormap.maxVal - xMin) * (pixmap.width() - 1) /
                       (xMax - xMin))
        else:
            lineMax = None

        self.__lineMin = lineMin
        self.__lineMax = lineMax

        gradient = Qt.QLinearGradient(Qt.QPoint(0, 0),
                                      Qt.QPoint(colormap.nColors - 1, 0))
        for idx, color in enumerate(colors):
            qColor = Qt.QColor.fromRgbF(*color)
            gradient.setColorAt(idx / (1.0 * (colormap.nColors - 1)), qColor)

        painter = Qt.QPainter(pixmap)
        painter.save()
        painter.scale(1, -1.)
        painter.translate(Qt.QPointF(0., -1.0 * self._pimapHeight))
        brush = Qt.QBrush(gradient)
        painter.setBrush(brush)
        painter.setPen(Qt.QPen(Qt.Qt.NoPen))
        painter.drawPolygon(poly)
        painter.restore()
        painter.end()
        self.update()
예제 #2
0
    def drawRangeSliderBackground(cls, painter, option, widget):
        """Draw the background of the RangeSlider widget into the painter.

        :param qt.QPainter painter: A painter
        :param StyleOptionRangeSlider option: Options to draw the widget
        :param qt.QWidget: The widget which have to be drawn
        """
        painter.save()
        painter.translate(0.5, 0.5)

        backgroundRect = qt.QRect(option.rect)
        if backgroundRect.height() > 8:
            center = backgroundRect.center()
            backgroundRect.setHeight(8)
            backgroundRect.moveCenter(center)

        selectedRangeRect = qt.QRect(backgroundRect)
        selectedRangeRect.setLeft(option.handlerRect1.center().x())
        selectedRangeRect.setRight(option.handlerRect2.center().x())

        highlight = option.palette.color(qt.QPalette.Highlight)
        activeHighlight = highlight
        selectedOutline = option.palette.color(qt.QPalette.Highlight)

        buttonColor = option.palette.button().color()
        val = qt.qGray(buttonColor.rgb())
        buttonColor = buttonColor.lighter(100 + max(1, (180 - val) // 6))
        buttonColor.setHsv(buttonColor.hue(),
                           buttonColor.saturation() * 0.75,
                           buttonColor.value())

        grooveColor = qt.QColor()
        grooveColor.setHsv(buttonColor.hue(),
                           min(255, (int)(buttonColor.saturation())),
                           min(255, (int)(buttonColor.value() * 0.9)))

        selectedInnerContrastLine = qt.QColor(255, 255, 255, 30)

        outline = option.palette.color(qt.QPalette.Background).darker(140)
        if (option.state & qt.QStyle.State_HasFocus
                and option.state & qt.QStyle.State_KeyboardFocusChange):
            outline = highlight.darker(125)
            if outline.value() > 160:
                outline.setHsl(highlight.hue(), highlight.saturation(), 160)

        # Draw background groove
        painter.setRenderHint(qt.QPainter.Antialiasing, True)
        gradient = qt.QLinearGradient()
        gradient.setStart(backgroundRect.center().x(), backgroundRect.top())
        gradient.setFinalStop(backgroundRect.center().x(),
                              backgroundRect.bottom())
        painter.setPen(qt.QPen(outline))
        gradient.setColorAt(0, grooveColor.darker(110))
        gradient.setColorAt(1, grooveColor.lighter(110))
        painter.setBrush(gradient)
        painter.drawRoundedRect(backgroundRect.adjusted(1, 1, -2, -2), 1, 1)

        # Draw slider background for the value
        gradient = qt.QLinearGradient()
        gradient.setStart(selectedRangeRect.center().x(),
                          selectedRangeRect.top())
        gradient.setFinalStop(selectedRangeRect.center().x(),
                              selectedRangeRect.bottom())
        painter.setRenderHint(qt.QPainter.Antialiasing, True)
        painter.setPen(qt.QPen(selectedOutline))
        gradient.setColorAt(0, activeHighlight)
        gradient.setColorAt(1, activeHighlight.lighter(130))
        painter.setBrush(gradient)
        painter.drawRoundedRect(selectedRangeRect.adjusted(1, 1, -2, -2), 1, 1)
        painter.setPen(selectedInnerContrastLine)
        painter.setBrush(qt.Qt.NoBrush)
        painter.drawRoundedRect(selectedRangeRect.adjusted(2, 2, -3, -3), 1, 1)

        painter.restore()
예제 #3
0
    def paintEvent(self, event):
        painter = Qt.QPainter(self)

        style = Qt.QApplication.style()

        area = self.__drawArea()
        pixmapRect = self.__pixMapRect()

        sliders = self.__sliders

        option = Qt.QStyleOptionProgressBar()
        option.initFrom(self)
        option.rect = area
        option.state = ((self.isEnabled() and Qt.QStyle.State_Enabled)
                        or Qt.QStyle.State_None)
        style.drawControl(Qt.QStyle.CE_ProgressBarGroove, option, painter,
                          self)

        # showing interval rect only if show is forced or if there is not
        # background and show is True or None
        showRngBckgrnd = (self.__showRangeBackground
                          or (self.__pixmap and self.__showRangeBackground)
                          or (self.__pixmap is None
                              and self.__showRangeBackground is None)
                          or self.__showRangeBackground)

        alpha = (self.isEnabled() and 255) or 100

        if showRngBckgrnd:
            painter.save()
            rect = Qt.QRect(area)
            rect.setLeft(sliders['left'].center().x())
            rect.setRight(sliders['right'].center().x())
            gradient = Qt.QLinearGradient(area.topLeft(), area.bottomLeft())
            color = Qt.QColor(Qt.Qt.cyan)
            color.setAlpha(alpha)
            gradient.setColorAt(0., color)
            color = Qt.QColor(Qt.Qt.blue)
            color.setAlpha(alpha)
            gradient.setColorAt(1., color)
            brush = Qt.QBrush(gradient)
            painter.setBrush(brush)
            painter.drawRect(rect)
            painter.restore()

        if self.__pixmap and alpha == 255:
            painter.save()
            pen = painter.pen()
            pen.setWidth(2)
            pen.setColor(Qt.Qt.black)
            painter.setPen(pen)
            painter.drawRect(pixmapRect.adjusted(-1, -1, 1, 1))
            painter.restore()

            painter.drawPixmap(
                area.adjusted(self._sliderWidth / 2, self._pixmapVOffset,
                              -self._sliderWidth / 2, -self._pixmapVOffset),
                self.__pixmap, self.__pixmap.rect())

        option = Qt.QStyleOptionButton()
        option.initFrom(self)

        for side, slider in sliders.items():
            option.icon = self.__sliderIcons[side]
            option.iconSize = slider.size() * 0.7
            if self.__hover == side:
                option.state |= Qt.QStyle.State_MouseOver
            elif option.state & Qt.QStyle.State_MouseOver:
                option.state ^= Qt.QStyle.State_MouseOver
            if self.__focus == side:
                option.state |= Qt.QStyle.State_HasFocus
            elif option.state & Qt.QStyle.State_HasFocus:
                option.state ^= Qt.QStyle.State_HasFocus
            option.rect = slider
            style.drawControl(Qt.QStyle.CE_PushButton, option, painter, self)