Example #1
0
    def paintEvent(self, event):
        # based on
        # https://github.com/qt/qtbase/blob/f40dbe0d0b54ce83d2168e82905cf4f75059a841/src/widgets/widgets/qslider.cpp#L315
        # https://github.com/enthought/traitsui/blob/master/traitsui/qt4/extra/range_slider.py
        painter = QStylePainter(self)
        minpos = self._min_position
        maxpos = self._max_position

        # Draw the groove
        opt = QStyleOptionSlider()
        self.initStyleOption(opt)
        # Draw empty grove
        opt.sliderPosition = opt.minimum
        opt.subControls = QStyle.SC_SliderGroove
        if self.tickPosition() != self.NoTicks:
            opt.subControls |= QStyle.SC_SliderTickmarks
        painter.drawComplexControl(QStyle.CC_Slider, opt)
        # Draw the highlighted part on top
        # Qt4.8 and Qt5.3 draw the highlighted groove in a weird way because they
        # transpose opt.rect. Qt5.7 works fine.
        if QT_VERSION_STR >= '5.7.0':
            opt.subControls = QStyle.SC_SliderGroove
            opt.sliderPosition = opt.maximum
            if self.orientation() == Qt.Horizontal:
                _w = opt.rect.width() / opt.maximum
                x = round(_w * minpos)
                w = round(_w * (maxpos - minpos))
                opt.rect = QRect(x, 0, w, opt.rect.height())
            else:
                _h = opt.rect.height() / opt.maximum
                y = round(_h * minpos)
                h = round(_h * (maxpos - minpos))
                opt.rect = QRect(0, y, opt.rect.width(), h)
            painter.drawComplexControl(QStyle.CC_Slider, opt)

        # Draw the handles
        for i, position in enumerate((minpos, maxpos)):
            opt = QStyleOptionSlider()
            self.initStyleOption(opt)
            opt.subControls = QStyle.SC_SliderHandle

            if self.__pressed_control and (self.__active_slider == i or
                                           self.__active_slider < 0):
                opt.activeSubControls = self.__pressed_control
                opt.state |= QStyle.State_Sunken
            else:
                opt.activeSubControls = self.__hovered_control

            opt.sliderPosition = position
            opt.sliderValue = position
            painter.drawComplexControl(QStyle.CC_Slider, opt)
    def paintEvent(self, event):
        # based on
        # https://github.com/qt/qtbase/blob/f40dbe0d0b54ce83d2168e82905cf4f75059a841/src/widgets/widgets/qslider.cpp#L315
        # https://github.com/enthought/traitsui/blob/master/traitsui/qt4/extra/range_slider.py
        painter = QStylePainter(self)
        minpos = self.__min_position
        maxpos = self.__max_position

        # Draw the groove
        opt = QStyleOptionSlider()
        self.initStyleOption(opt)
        # Draw empty grove
        opt.sliderPosition = opt.minimum
        opt.subControls = QStyle.SC_SliderGroove
        if self.tickPosition() != self.NoTicks:
            opt.subControls |= QStyle.SC_SliderTickmarks
        painter.drawComplexControl(QStyle.CC_Slider, opt)
        # Draw the highlighted part on top
        # Qt4.8 and Qt5.3 draw the highlighted groove in a weird way because they
        # transpose opt.rect. Qt5.7 works fine.
        if QT_VERSION_STR >= '5.7.0':
            opt.subControls = QStyle.SC_SliderGroove
            opt.sliderPosition = opt.maximum
            if self.orientation() == Qt.Horizontal:
                _w = opt.rect.width() / opt.maximum
                x = round(_w * minpos)
                w = round(_w * (maxpos - minpos))
                opt.rect = QRect(x, 0, w, opt.rect.height())
            else:
                _h = opt.rect.height() / opt.maximum
                y = round(_h * minpos)
                h = round(_h * (maxpos - minpos))
                opt.rect = QRect(0, y, opt.rect.width(), h)
            painter.drawComplexControl(QStyle.CC_Slider, opt)

        # Draw the handles
        for i, position in enumerate((minpos, maxpos)):
            opt = QStyleOptionSlider()
            self.initStyleOption(opt)
            opt.subControls = QStyle.SC_SliderHandle

            if self.__pressed_control and (self.__active_slider == i or
                                           self.__active_slider < 0):
                opt.activeSubControls = self.__pressed_control
                opt.state |= QStyle.State_Sunken
            else:
                opt.activeSubControls = self.__hovered_control

            opt.sliderPosition = position
            opt.sliderValue = position
            painter.drawComplexControl(QStyle.CC_Slider, opt)
    def mousePressEvent(self, event):
        if not event.button():
            event.ignore()
            return

        event.accept()
        opt = QStyleOptionSlider()
        self.initStyleOption(opt)
        self.__active_slider = -1

        for i, value in enumerate((self.__min_position, self.__max_position)):
            opt.sliderPosition = value
            if self._hitTestHandle(opt, event.pos()):
                self.__active_slider = i
                self.__pressed_control = QStyle.SC_SliderHandle

                self.triggerAction(self.SliderMove)
                self.setRepeatAction(self.SliderNoAction)
                self.setSliderDown(True)
                break
        else:
            # If the user clicks the groove between the handles, the whole
            # interval is moved
            self.__pressed_control = QStyle.SC_SliderGroove
            self.__click_offset = self._pixelPosToRangeValue(self._pick(event.pos()))
            self.triggerAction(self.SliderMove)
            self.setRepeatAction(self.SliderNoAction)
    def mousePressEvent(self, event):
        if not event.button():
            event.ignore()
            return

        event.accept()
        opt = QStyleOptionSlider()
        self.initStyleOption(opt)
        self.__active_slider = -1

        for i, value in enumerate((self.__min_position, self.__max_position)):
            opt.sliderPosition = value
            if self._hitTestHandle(opt, event.pos()):
                self.__active_slider = i
                self.__pressed_control = QStyle.SC_SliderHandle

                self.triggerAction(self.SliderMove)
                self.setRepeatAction(self.SliderNoAction)
                self.setSliderDown(True)
                break
        else:
            # If the user clicks the groove between the handles, the whole
            # interval is moved
            self.__pressed_control = QStyle.SC_SliderGroove
            self.__click_offset = self._pixelPosToRangeValue(
                self._pick(event.pos()))
            self.triggerAction(self.SliderMove)
            self.setRepeatAction(self.SliderNoAction)
Example #5
0
    def _testHandleCollision(self, position):
        """Replaces QStyle.hitTestComplexControl in mousePressEvent().

        Return True if handle was pressed.
        Override this in subclasses that don't use the default groove/handles.

        This is used because PyQt<5.5 doesn't expose QProxyStyle.
        """
        for i, h in enumerate([self._min_position, self._max_position]):
            opt = QStyleOptionSlider()
            self.initStyleOption(opt)
            opt.sliderPosition = position

            if self.style().hitTestComplexControl(
                QStyle.CC_Slider, opt, position, self) == QStyle.SC_SliderHandle:
                return i
        return -1
Example #6
0
    def paintEvent(self, e):
        #print("hover: ",self._hover,",press: ",self._press)
        painter = QStylePainter(self)

        # prepare the drawing options
        # for drawing slider groove
        opt = QStyleOptionSlider()
        opt.initFrom(self)

        opt.subControls = QStyle.SC_SliderGroove

        opt.maximum = self.__steps
        opt.minimum = 0
        opt.orientation = self.orientation()
        opt.sliderPosition = self.__position0
        opt.sliderValue = self.__value0

        if self._tickList is not None:
            opt.tickPosition = QSlider.TicksBelow
        else:
            opt.tickPosition = QSlider.NoTicks

        handleWidth = self._handleWidth
        measureWidth = self.width() - handleWidth - handleWidth

        painter.drawComplexControl(QStyle.CC_Slider, opt)

        # draw tickmarks
        if self._tickList is not None:
            painter.drawPixmap(self._handleWidth,
                               self.height() - self._tickHeight,
                               self._tickImage)

        # handle colors
        colorPRESS = QColor(204, 204, 204)
        colorHOVER = QColor(23, 23, 23)
        colorNORMAL = QColor(0, 122, 217) if self.isEnabled() else colorPRESS

        handleHeight = self.height(
        ) if self._tickList is None else self.height() - self._tickHeight

        # draw handle 0
        if self._press in {0, 2}:
            color = colorPRESS
        elif self._hover == 0:
            color = colorHOVER
        else:
            color = colorNORMAL
        self._rects[0] = r0 = QRect(
            self.__position0 / self.__steps * measureWidth, 0, handleWidth,
            handleHeight)
        painter.fillRect(self._rects[0], color)

        # draw handle 1
        if self._press in {1, 2}:
            color = colorPRESS
        elif self._hover == 1:
            color = colorHOVER
        else:
            color = colorNORMAL

        self._rects[1] = r1 = QRect(
            self.__position1 / self.__steps * measureWidth + handleWidth, 0,
            handleWidth, handleHeight)
        painter.fillRect(self._rects[1], color)

        # draw inner handle
        if self._press == 2 or not self.isEnabled():
            color = QColor(0, 0, 0, 15)
        elif self._hover == 2:
            color = QColor(0, 61, 108, 63)
        else:
            color = QColor(0, 122, 217, 63)

        self._rects[2] = QRect(r0.left(), r0.top(),
                               r1.right() - r0.left() + 1, handleHeight)

        painter.fillRect(self._rects[2], color)