Esempio n. 1
0
 def copyOptions(self):
     opt = QStyleOptionSlider()
     opt.initFrom(self._slider)
     opt.maximum = self._slider.maximum()
     opt.minimum = self._slider.minimum()
     opt.orientation = self._slider.orientation()
     opt.pageStep = self._slider.pageStep()
     opt.singleStep = self._slider.singleStep()
     return opt
Esempio n. 2
0
    def paintEvent(self, event: QPaintEvent) -> None:
        """
        Render the scrollbars using Qt's draw control, and render the frame elements
        dependent on whether the partner horizontal / vertical scrollbar is also visible
        """

        painter = QStylePainter(self)
        if self.orientation() == Qt.Vertical:
            painter.translate(0.0, self.frame_width)
        else:
            painter.translate(self.frame_width, 0.0)

        option = QStyleOptionSlider()
        option.initFrom(self)
        option.maximum = self.maximum()
        option.minimum = self.minimum()
        option.pageStep = self.pageStep()
        option.singleStep = self.singleStep()
        option.sliderPosition = self.sliderPosition()
        option.orientation = self.orientation()
        if self.orientation() == Qt.Horizontal:
            option.state |= QStyle.State_Horizontal

        rect = self.renderRect()

        option.rect = rect
        option.palette = self.palette()
        option.subControls = (
            QStyle.SC_ScrollBarAddLine
            | QStyle.SC_ScrollBarSubLine
            | QStyle.SC_ScrollBarAddPage
            | QStyle.SC_ScrollBarSubPage
            | QStyle.SC_ScrollBarFirst
            | QStyle.SC_ScrollBarLast
        )

        painter.fillRect(
            option.rect, QApplication.palette().window().color().darker(102)
        )
        self.style().drawComplexControl(QStyle.CC_ScrollBar, option, painter)

        # Highlight the handle (slider) on mouse over, otherwise render it as normal
        option.subControls = QStyle.SC_ScrollBarSlider
        if option.state & QStyle.State_MouseOver == QStyle.State_MouseOver:
            palette = self.palette()
            if sys.platform == "win32":
                color = self.palette().base().color()
            else:
                color = self.palette().button().color().lighter(102)
            palette.setColor(QPalette.Button, color)
            option.palette = palette
        self.style().drawComplexControl(QStyle.CC_ScrollBar, option, painter)

        # Render the borders
        painter.resetTransform()
        painter.setPen(self.midPen)
        self.renderEdges(painter)
Esempio n. 3
0
 def copyOptions(self) :
     opt = QStyleOptionSlider()
     opt.initFrom(self._slider)
     opt.maximum = self._slider.maximum();
     opt.minimum = self._slider.minimum();
     opt.orientation = self._slider.orientation()
     opt.pageStep = self._slider.pageStep()
     opt.singleStep = self._slider.singleStep()
     return opt
Esempio n. 4
0
    def paintEvent(self, e):

        super(LabeledSlider, self).paintEvent(e)

        style = self.sl.style()
        painter = QPainter(self)
        st_slider = QStyleOptionSlider()
        st_slider.initFrom(self.sl)
        st_slider.orientation = self.sl.orientation()

        length = style.pixelMetric(QStyle.PM_SliderLength, st_slider, self.sl)
        available = style.pixelMetric(QStyle.PM_SliderSpaceAvailable,
                                      st_slider, self.sl)

        for v, v_str in self.levels:

            # get the size of the label
            rect = painter.drawText(QRect(), Qt.TextDontPrint, v_str)

            if self.sl.orientation() == Qt.Horizontal:
                # I assume the offset is half the length of slider, therefore
                # + length//2
                x_loc = QStyle.sliderPositionFromValue(self.sl.minimum(),
                                                       self.sl.maximum(), v,
                                                       available) + length // 2

                # left bound of the text = center - half of text width + L_margin
                left = x_loc - rect.width() // 2 + self.left_margin
                bottom = self.rect().bottom()

                # enlarge margins if clipping
                if v == self.sl.minimum():
                    if left <= 0:
                        self.left_margin = rect.width() // 2 - x_loc
                    if self.bottom_margin <= rect.height():
                        self.bottom_margin = rect.height()

                    self.layout.setContentsMargins(self.left_margin,
                                                   self.top_margin,
                                                   self.right_margin,
                                                   self.bottom_margin)

                if v == self.sl.maximum(
                ) and rect.width() // 2 >= self.right_margin:
                    self.right_margin = rect.width() // 2
                    self.layout.setContentsMargins(self.left_margin,
                                                   self.top_margin,
                                                   self.right_margin,
                                                   self.bottom_margin)

            else:
                y_loc = QStyle.sliderPositionFromValue(self.sl.minimum(),
                                                       self.sl.maximum(),
                                                       v,
                                                       available,
                                                       upsideDown=True)

                bottom = y_loc + length // 2 + rect.height(
                ) // 2 + self.top_margin - 3
                # there is a 3 px offset that I can't attribute to any metric

                left = self.left_margin - rect.width()
                if left <= 0:
                    self.left_margin = rect.width() + 2
                    self.layout.setContentsMargins(self.left_margin,
                                                   self.top_margin,
                                                   self.right_margin,
                                                   self.bottom_margin)

            pos = QPoint(left, bottom)
            painter.drawText(pos, v_str)

        return