def gradient_darker(grad, factor): # type: (QGradient, float) -> QGradient """Return a copy of the QGradient darkened by factor. .. note:: Only QLinearGradeint and QRadialGradient are supported. """ if type(grad) is QGradient: if grad.type() == QGradient.LinearGradient: grad = sip.cast(grad, QLinearGradient) elif grad.type() == QGradient.RadialGradient: grad = sip.cast(grad, QRadialGradient) if isinstance(grad, QLinearGradient): new_grad = QLinearGradient(grad.start(), grad.finalStop()) elif isinstance(grad, QRadialGradient): new_grad = QRadialGradient(grad.center(), grad.radius(), grad.focalPoint()) else: raise TypeError new_grad.setCoordinateMode(grad.coordinateMode()) for pos, color in grad.stops(): new_grad.setColorAt(pos, color.darker(factor)) return new_grad
def shadowTemplatePixmap(color, length): """ Returns 1 pixel wide, `length` pixels long linear-gradient. Args: color (QColor): shadow color length (int): length of cast shadow """ key = "InnerShadowTemplate " + \ color.name() + " " + \ str(length) # get cached template shadowPixmap = QPixmapCache.find(key) if shadowPixmap: return shadowPixmap shadowPixmap = QPixmap(1, length) shadowPixmap.fill(Qt.transparent) grad = QLinearGradient(0, 0, 0, length) grad.setColorAt(0, color) grad.setColorAt(1, Qt.transparent) painter = QPainter() painter.begin(shadowPixmap) painter.fillRect(shadowPixmap.rect(), grad) painter.end() # cache template QPixmapCache.insert(key, shadowPixmap) return shadowPixmap
def gradient_darker(grad, factor): """Return a copy of the QGradient darkened by factor. .. note:: Only QLinearGradeint and QRadialGradient are supported. """ if type(grad) is QGradient: if grad.type() == QGradient.LinearGradient: grad = sip.cast(grad, QLinearGradient) elif grad.type() == QGradient.RadialGradient: grad = sip.cast(grad, QRadialGradient) if isinstance(grad, QLinearGradient): new_grad = QLinearGradient(grad.start(), grad.finalStop()) elif isinstance(grad, QRadialGradient): new_grad = QRadialGradient(grad.center(), grad.radius(), grad.focalPoint()) else: raise TypeError new_grad.setCoordinateMode(grad.coordinateMode()) for pos, color in grad.stops(): new_grad.setColorAt(pos, color.darker(factor)) return new_grad
def _add_bar(self): item = QGraphicsRectItem(0, 0, self.BAR_WIDTH, self.__bar_height) gradient = QLinearGradient(0, 0, 0, self.__bar_height) gradient.setColorAt(0, QColor(*RGB_HIGH)) gradient.setColorAt(1, QColor(*RGB_LOW)) item.setPen(QPen(Qt.NoPen)) item.setBrush(gradient) self.__group.addToGroup(item)
class LegendGradient(QGraphicsWidget): """Gradient widget. A gradient square bar that can be used to display continuous values. Parameters ---------- palette : iterable[QColor] parent : QGraphicsWidget orientation : Qt.Orientation Notes ----- .. note:: While the gradient does support any number of colors, any more than 3 is not very readable. This should not be a problem, since Orange only implements 2 or 3 colors. """ # Default sizes (assume gradient is vertical by default) GRADIENT_WIDTH = 20 GRADIENT_HEIGHT = 150 _size_hint = QSizeF(GRADIENT_WIDTH, GRADIENT_HEIGHT) def __init__(self, palette, parent, orientation): super().__init__(parent) self.__gradient = QLinearGradient() num_colors = len(palette) for idx, stop in enumerate(palette): self.__gradient.setColorAt(idx * (1. / (num_colors - 1)), stop) # We need to tell the gradient where it's start and stop points are self.__gradient.setStart(QPointF(0, 0)) if orientation == Qt.Vertical: final_stop = QPointF(0, self.GRADIENT_HEIGHT) else: final_stop = QPointF(self.GRADIENT_HEIGHT, 0) self.__gradient.setFinalStop(final_stop) # Get the appropriate rectangle dimensions based on orientation if orientation == Qt.Vertical: width, height = self.GRADIENT_WIDTH, self.GRADIENT_HEIGHT elif orientation == Qt.Horizontal: width, height = self.GRADIENT_HEIGHT, self.GRADIENT_WIDTH self.__rect_item = QGraphicsRectItem(0, 0, width, height, self) self.__rect_item.setPen(QPen(QColor(0, 0, 0, 0))) self.__rect_item.setBrush(QBrush(self.__gradient)) self._size_hint = QSizeF(self.__rect_item.boundingRect().size()) def sizeHint(self, size_hint, size_constraint=None, *args, **kwargs): return self._size_hint
class LegendGradient(QGraphicsWidget): """Gradient widget. A gradient square bar that can be used to display continuous values. Parameters ---------- palette : iterable[QColor] parent : QGraphicsWidget orientation : Qt.Orientation Notes ----- .. note:: While the gradient does support any number of colors, any more than 3 is not very readable. This should not be a problem, since Orange only implements 2 or 3 colors. """ # Default sizes (assume gradient is vertical by default) GRADIENT_WIDTH = 20 GRADIENT_HEIGHT = 150 _size_hint = QSizeF(GRADIENT_WIDTH, GRADIENT_HEIGHT) def __init__(self, palette, parent, orientation): super().__init__(parent) self.__gradient = QLinearGradient() num_colors = len(palette) for idx, stop in enumerate(palette): self.__gradient.setColorAt(idx * (1. / (num_colors - 1)), stop) # We need to tell the gradient where it's start and stop points are self.__gradient.setStart(QPointF(0, 0)) if orientation == Qt.Vertical: final_stop = QPointF(0, self.GRADIENT_HEIGHT) else: final_stop = QPointF(self.GRADIENT_HEIGHT, 0) self.__gradient.setFinalStop(final_stop) # Get the appropriate rectangle dimensions based on orientation if orientation == Qt.Vertical: width, height = self.GRADIENT_WIDTH, self.GRADIENT_HEIGHT elif orientation == Qt.Horizontal: width, height = self.GRADIENT_HEIGHT, self.GRADIENT_WIDTH self.__rect_item = QGraphicsRectItem(0, 0, width, height, self) self.__rect_item.setPen(QPen(QColor(0, 0, 0, 0))) self.__rect_item.setBrush(QBrush(self.__gradient)) self._size_hint = QSizeF(self.__rect_item.boundingRect().size()) def sizeHint(self, size_hint, size_constraint=None, *args, **kwargs): return self._size_hint
def createContPalettePixmap(width, height, color1, color2, passThroughBlack): p = QPainter() img = QPixmap(width, height) p.begin(img) #p.eraseRect(0, 0, w, h) p.setPen(QPen(Qt.NoPen)) g = QLinearGradient(0, 0, width, height) g.setColorAt(0, color1) g.setColorAt(1, color2) if passThroughBlack: g.setColorAt(0.5, Qt.black) p.fillRect(img.rect(), QBrush(g)) return img
def createExContPalettePixmap(width, height, color1, color2, passThroughColors): p = QPainter() img = QPixmap(width, height) p.begin(img) #p.eraseRect(0, 0, w, h) p.setPen(QPen(Qt.NoPen)) g = QLinearGradient(0, 0, width, height) g.setColorAt(0, color1) g.setColorAt(1, color2) for i, color in enumerate(passThroughColors): g.setColorAt(float(i + 1) / (len(passThroughColors) + 1), color) p.fillRect(img.rect(), QBrush(g)) return img
def createContPalettePixmap(width, height, color1, color2, passThroughBlack): p = QPainter() img = QPixmap(width, height) p.begin(img) #p.eraseRect(0, 0, w, h) p.setPen(QPen(Qt.NoPen)) g = QLinearGradient(0, 0, width, height) g.setColorAt(0, color1) g.setColorAt(1, color2) if passThroughBlack: g.setColorAt(0.5, Qt.black) p.fillRect(img.rect(), QBrush(g)) return img
def createExContPalettePixmap(width, height, color1, color2, passThroughColors): p = QPainter() img = QPixmap(width, height) p.begin(img) #p.eraseRect(0, 0, w, h) p.setPen(QPen(Qt.NoPen)) g = QLinearGradient(0, 0, width, height) g.setColorAt(0, color1) g.setColorAt(1, color2) for i, color in enumerate(passThroughColors): g.setColorAt(float(i + 1) / (len(passThroughColors) + 1), color) p.fillRect(img.rect(), QBrush(g)) return img
def paintEvent(self, event): opt = QStyleOption() opt.initFrom(self) if not self.__popupicon.isVisible(): return if not (opt.state & QStyle.State_MouseOver or opt.state & QStyle.State_HasFocus): return palette = opt.palette # type: QPalette if opt.state & QStyle.State_HasFocus: pen = QPen(palette.color(QPalette.Highlight)) else: pen = QPen(palette.color(QPalette.Dark)) if (self.__fulltext and opt.state & QStyle.State_MouseOver and opt.state & QStyle.State_Active): g = QLinearGradient() g.setCoordinateMode(QLinearGradient.ObjectBoundingMode) base = palette.color(QPalette.Window) base.setAlpha(90) g.setColorAt(0, base.lighter(200)) g.setColorAt(0.6, base) g.setColorAt(1.0, base.lighter(200)) brush = QBrush(g) else: brush = QBrush(Qt.NoBrush) p = QPainter(self) p.setBrush(brush) p.setPen(pen) p.drawRect(opt.rect.adjusted(0, 0, -1, -1))
def paintEvent(self, event): opt = QStyleOption() opt.initFrom(self) if not self.__popupicon.isVisible(): return if not (opt.state & QStyle.State_MouseOver or opt.state & QStyle.State_HasFocus): return palette = opt.palette # type: QPalette if opt.state & QStyle.State_HasFocus: pen = QPen(palette.color(QPalette.Highlight)) else: pen = QPen(palette.color(QPalette.Dark)) if self.__fulltext and \ opt.state & QStyle.State_MouseOver and \ opt.state & QStyle.State_Active: g = QLinearGradient() g.setCoordinateMode(QLinearGradient.ObjectBoundingMode) base = palette.color(QPalette.Window) base.setAlpha(90) g.setColorAt(0, base.lighter(200)) g.setColorAt(0.6, base) g.setColorAt(1.0, base.lighter(200)) brush = QBrush(g) else: brush = QBrush(Qt.NoBrush) p = QPainter(self) p.setBrush(brush) p.setPen(pen) p.drawRect(opt.rect.adjusted(0, 0, -1, -1))
def _set_bar(self, height: int): self._bar_item.setRect(0, 0, self.BAR_WIDTH, height) gradient = QLinearGradient(0, 0, 0, height) gradient.setColorAt(0, QColor(*RGB_HIGH)) gradient.setColorAt(1, QColor(*RGB_LOW)) self._bar_item.setBrush(gradient)