def paintEvent(self, event): painter = QStylePainter(self) rect = self._subControlRect(QStyle.SC_SliderGroove) is_horizontal = self.orientation() == Qt.Horizontal minpos, maxpos = self.minimumPosition(), self.maximumPosition() span = rect.width() if is_horizontal else rect.height() x1 = QStyle.sliderPositionFromValue( self.minimum(), self.maximum(), minpos, span, self.invertedAppearance()) x2 = QStyle.sliderPositionFromValue( self.minimum(), self.maximum(), maxpos, span, self.invertedAppearance()) # Background painter.fillRect(rect, Qt.white) # Highlight painter.setOpacity(.7) if is_horizontal: painter.fillRect(x1, rect.y(), x2 - x1, rect.height(), Qt.yellow) else: painter.fillRect(rect.x(), x1, rect.width(), x2 - x1, Qt.yellow) painter.setOpacity(1) # Histogram if self._pixmap: painter.drawPixmap(rect, self._pixmap, self._pixmap.rect()) # Frame painter.setPen(QPen(QBrush(Qt.darkGray), 2)) painter.drawRect(rect) # Handles painter.setPen(QPen(QBrush(self._HANDLE_COLOR), self._HANDLE_WIDTH)) painter.setOpacity(9) if is_horizontal: painter.drawLine(x1, rect.y(), x1, rect.y() + rect.height()) painter.drawLine(x2, rect.y(), x2, rect.y() + rect.height()) else: painter.drawLine(rect.x(), x1, rect.x() + rect.width(), x1) painter.drawLine(rect.x(), x2, rect.x() + rect.width(), x2) painter.setOpacity(1) if self._show_text: painter.setFont(QFont('sans-serif', 7, QFont.Bold)) strMin, strMax = self.formatValues(minpos, maxpos) widthMin = painter.fontMetrics().width(strMin) widthMax = painter.fontMetrics().width(strMax) height = painter.fontMetrics().height() is_enough_space = x2 - x1 > 2 + (max(widthMax, widthMin) if is_horizontal else (2 * height + self._HANDLE_WIDTH)) if is_enough_space: if is_horizontal: painter.drawText(x1 + 3, rect.y() + height - 2, strMin) painter.drawText(x2 - widthMax - 1, rect.y() + rect.height() - 2, strMax) else: painter.drawText(rect.x() + 1, x1 + height, strMin) painter.drawText(rect.x() + rect.width() - widthMax - 1, x2 - 2, strMax)
def paintEvent(self, event): painter = QStylePainter(self) rect = self._subControlRect(QStyle.SC_SliderGroove) is_horizontal = self.orientation() == Qt.Horizontal minpos, maxpos = self.minimumPosition(), self.maximumPosition() span = rect.width() if is_horizontal else rect.height() x1 = QStyle.sliderPositionFromValue( self.minimum(), self.maximum(), minpos, span, self.invertedAppearance()) x2 = QStyle.sliderPositionFromValue( self.minimum(), self.maximum(), maxpos, span, self.invertedAppearance()) # Background painter.fillRect(rect, Qt.white) # Highlight painter.setOpacity(.7) if is_horizontal: painter.fillRect(x1, rect.y(), x2 - x1, rect.height(), Qt.yellow) else: painter.fillRect(rect.x(), x1, rect.width(), x2 - x1, Qt.yellow) painter.setOpacity(1) # Histogram if self._pixmap: painter.drawPixmap(rect, self._pixmap, self._pixmap.rect()) # Frame painter.setPen(QPen(QBrush(Qt.darkGray), 2)) painter.drawRect(rect) # Handles painter.setPen(QPen(QBrush(self._HANDLE_COLOR), self._HANDLE_WIDTH)) painter.setOpacity(9) if is_horizontal: painter.drawLine(x1, rect.y(), x1, rect.y() + rect.height()) painter.drawLine(x2, rect.y(), x2, rect.y() + rect.height()) else: painter.drawLine(rect.x(), x1, rect.x() + rect.width(), x1) painter.drawLine(rect.x(), x2, rect.x() + rect.width(), x2) painter.setOpacity(1) if self._show_text: painter.setFont(QFont('Monospace', 7, QFont.Bold)) strMin, strMax = self.formatValues(minpos, maxpos) widthMin = painter.fontMetrics().width(strMin) widthMax = painter.fontMetrics().width(strMax) height = painter.fontMetrics().height() is_enough_space = x2 - x1 > 3 + (max(widthMax, widthMin) if is_horizontal else (2 * height + self._HANDLE_WIDTH)) if is_enough_space: if is_horizontal: painter.drawText(x1 + 3, rect.y() + height, strMin) painter.drawText(x2 - widthMax - 2, rect.y() + rect.height() - 3, strMax) else: painter.drawText(rect.x() + 1, x1 + height, strMin) painter.drawText(rect.x() + rect.width() - widthMax - 1, x2 - 2, strMax) # Tooltip if self._showControlTooltip\ and (not self._show_text or not is_enough_space): # (show control-drag tooltip) painter.setFont(QFont('Monospace', 10, QFont.Normal)) text = "Hold {} to move time interval" \ .format("Cmd" if sys.platform == "darwin" else "Ctrl") w = painter.fontMetrics().width(text) h = painter.fontMetrics().height() brush = QColor(224, 224, 224, 212) pen = QPen(Qt.NoPen) rect = QRect(4, 4, w + 8, h + 4) painter.setBrush(brush) painter.setPen(pen) painter.drawRect(rect) painter.setPen(Qt.black) painter.drawText(8, 4 + h, text)
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)
def paintEvent(self, event): painter = QStylePainter(self) rect = self._subControlRect(QStyle.SC_SliderGroove) is_horizontal = self.orientation() == Qt.Horizontal minpos, maxpos = self.minimumPosition(), self.maximumPosition() span = rect.width() if is_horizontal else rect.height() x1 = QStyle.sliderPositionFromValue(self.minimum(), self.maximum(), minpos, span, self.invertedAppearance()) x2 = QStyle.sliderPositionFromValue(self.minimum(), self.maximum(), maxpos, span, self.invertedAppearance()) # Background painter.fillRect(rect, Qt.white) # Highlight painter.setOpacity(.7) if is_horizontal: painter.fillRect(x1, rect.y(), x2 - x1, rect.height(), Qt.yellow) else: painter.fillRect(rect.x(), x1, rect.width(), x2 - x1, Qt.yellow) painter.setOpacity(1) # Histogram if self._pixmap: painter.drawPixmap(rect, self._pixmap, self._pixmap.rect()) # Frame painter.setPen(QPen(QBrush(Qt.darkGray), 2)) painter.drawRect(rect) # Handles painter.setPen(QPen(QBrush(self._HANDLE_COLOR), self._HANDLE_WIDTH)) painter.setOpacity(9) if is_horizontal: painter.drawLine(x1, rect.y(), x1, rect.y() + rect.height()) painter.drawLine(x2, rect.y(), x2, rect.y() + rect.height()) else: painter.drawLine(rect.x(), x1, rect.x() + rect.width(), x1) painter.drawLine(rect.x(), x2, rect.x() + rect.width(), x2) painter.setOpacity(1) if self._show_text: painter.setFont(QFont('sans-serif', 7, QFont.Bold)) strMin, strMax = self.formatValues(minpos, maxpos) widthMin = painter.fontMetrics().width(strMin) widthMax = painter.fontMetrics().width(strMax) height = painter.fontMetrics().height() is_enough_space = x2 - x1 > 2 + (max(widthMax, widthMin) if is_horizontal else (2 * height + self._HANDLE_WIDTH)) if is_enough_space: if is_horizontal: painter.drawText(x1 + 3, rect.y() + height - 2, strMin) painter.drawText(x2 - widthMax - 1, rect.y() + rect.height() - 2, strMax) else: painter.drawText(rect.x() + 1, x1 + height, strMin) painter.drawText(rect.x() + rect.width() - widthMax - 1, x2 - 2, strMax)