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 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 palette_gradient(colors, discrete=False): n = len(colors) stops = numpy.linspace(0.0, 1.0, n, endpoint=True) gradstops = [(float(stop), color) for stop, color in zip(stops, colors)] grad = QLinearGradient(QPointF(0, 0), QPointF(1, 0)) grad.setStops(gradstops) return grad
def palette_gradient(colors): n = len(colors) stops = np.linspace(0.0, 1.0, n, endpoint=True) gradstops = [(float(stop), color) for stop, color in zip(stops, colors)] grad = QLinearGradient(QPointF(0, 0), QPointF(1, 0)) grad.setStops(gradstops) return grad
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 css_gradient(gradient: QLinearGradient) -> str: """ Given an instance of a `QLinearGradient` return an equivalent qt css gradient fragment. """ stop, finalStop = gradient.start(), gradient.finalStop() x1, y1, x2, y2 = stop.x(), stop.y(), finalStop.x(), finalStop.y() stops = gradient.stops() stops = "\n".join(" stop: {0:f} {1}".format(stop, color.name()) for stop, color in stops) return ("qlineargradient(\n" " x1: {x1}, y1: {y1}, x2: {x2}, y2: {y2},\n" "{stops})").format(x1=x1, y1=y1, x2=x2, y2=y2, stops=stops)
def __init__(self): GraphicsWidget.__init__(self) self.width_bar = 15 self.colors = None self.gradient = QLinearGradient() self.setMaximumHeight(2**16) self.setMinimumWidth(self.width_bar) self.setMaximumWidth(self.width_bar) self.rect = QGraphicsRectItem(QRectF(0, 0, self.width_bar, 100), self) self.axis = pg.AxisItem('right', parent=self) self.axis.setX(self.width_bar) self.axis.geometryChanged.connect(self._update_width) self.adapt_to_size() self._initialized = True
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 create_gradient(base_color, stop=QPointF(0, 0), finalStop=QPointF(0, 1)): """ Create a default linear gradient using `base_color` . """ grad = QLinearGradient(stop, finalStop) grad.setStops([(0.0, base_color), (0.5, base_color), (0.8, base_color.darker(105)), (1.0, base_color.darker(110)), ]) grad.setCoordinateMode(QLinearGradient.ObjectBoundingMode) return grad
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 __init__(self, palette, values, parent): QGraphicsObject.__init__(self, parent) self.parent = parent self.palette = palette self.values = values self.legend = parent self.label_items = [QGraphicsTextItem(text, self) for text in values] for i in self.label_items: i.setTextWidth(50) self.rect = QRectF() self.gradient_item = QGraphicsRectItem(self) self.gradient = QLinearGradient() self.gradient.setStops([(v * 0.1, self.palette[v * 0.1]) for v in range(11)]) self.orientation = Qt.Horizontal self.set_orientation(Qt.Vertical)
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 ImageColorLegend(GraphicsWidget): def __init__(self): GraphicsWidget.__init__(self) self.width_bar = 15 self.colors = None self.gradient = QLinearGradient() self.setMaximumHeight(2**16) self.setMinimumWidth(self.width_bar) self.setMaximumWidth(self.width_bar) self.rect = QGraphicsRectItem(QRectF(0, 0, self.width_bar, 100), self) self.axis = pg.AxisItem('right', parent=self) self.axis.setX(self.width_bar) self.axis.geometryChanged.connect(self._update_width) self.adapt_to_size() self._initialized = True def _update_width(self): aw = self.axis.minimumWidth() self.setMinimumWidth(self.width_bar + aw) self.setMaximumWidth(self.width_bar + aw) def resizeEvent(self, ev): if hasattr(self, "_initialized"): self.adapt_to_size() def adapt_to_size(self): h = self.height() self.resetTransform() self.rect.setRect(0, 0, self.width_bar, h) self.axis.setHeight(h) self.gradient.setStart(QPointF(0, h)) self.gradient.setFinalStop(QPointF(0, 0)) self.update_rect() def set_colors(self, colors): # a Nx3 array containing colors self.colors = colors if self.colors is not None: positions = np.linspace(0, 1, len(self.colors)) stops = [] for p, c in zip(positions, self.colors): stops.append((p, QColor(*c))) self.gradient.setStops(stops) self.update_rect() def set_range(self, low, high): self.axis.setRange(low, high) def update_rect(self): if self.colors is None: self.rect.setBrush(QBrush(Qt.white)) else: self.rect.setBrush(QBrush(self.gradient))
def __init__(self, palette, values, parent): QGraphicsObject.__init__(self, parent) self.parent = parent self.palette = palette self.values = values self.legend = parent self.label_items = [QGraphicsTextItem(text, self) for text in values] for i in self.label_items: i.setTextWidth(50) self.rect = QRectF() self.gradient_item = QGraphicsRectItem(self) self.gradient = QLinearGradient() self.gradient.setStops([(v*0.1, self.palette[v*0.1]) for v in range(11) ]) self.orientation = Qt.Horizontal self.set_orientation(Qt.Vertical)
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 create_gradient(base_color: QColor, stop=QPointF(0, 0), finalStop=QPointF(0, 1)) -> QLinearGradient: """ Create a default linear gradient using `base_color` . """ grad = QLinearGradient(stop, finalStop) grad.setStops([(0.0, base_color), (0.5, base_color), (0.8, base_color.darker(105)), (1.0, base_color.darker(110)), ]) grad.setCoordinateMode(QLinearGradient.ObjectBoundingMode) return grad
class OWLegendGradient(QGraphicsObject): gradient_width = 20 def __init__(self, palette, values, parent): QGraphicsObject.__init__(self, parent) self.parent = parent self.palette = palette self.values = values self.legend = parent self.label_items = [QGraphicsTextItem(text, self) for text in values] for i in self.label_items: i.setTextWidth(50) self.rect = QRectF() self.gradient_item = QGraphicsRectItem(self) self.gradient = QLinearGradient() self.gradient.setStops([(v*0.1, self.palette[v*0.1]) for v in range(11) ]) self.orientation = Qt.Horizontal self.set_orientation(Qt.Vertical) def set_orientation(self, orientation): if self.orientation == orientation: return self.orientation = orientation if self.orientation == Qt.Vertical: height = max([item.boundingRect().height() for item in self.label_items]) total_height = height * max(5, len(self.label_items)) interval = (total_height - self.label_items[-1].boundingRect().height()) / (len(self.label_items) -1) self.gradient_item.setRect(10, 0, self.gradient_width, total_height) self.gradient.setStart(10, 0) self.gradient.setFinalStop(10, total_height) self.gradient_item.setBrush(QBrush(self.gradient)) self.gradient_item.setPen(QPen(Qt.NoPen)) y = 0 x = 30 for item in self.label_items: move_item_xy(item, x, y, self.parent.graph.animate_plot) y += interval self.rect = QRectF(10, 0, self.gradient_width + max([item.boundingRect().width() for item in self.label_items]), self.label_items[0].boundingRect().height() * max(5, len(self.label_items))) else: width = 50 height = max([item.boundingRect().height() for item in self.label_items]) total_width = width * max(5, len(self.label_items)) interval = (total_width - self.label_items[-1].boundingRect().width()) / (len(self.label_items) -1) self.gradient_item.setRect(0, 0, total_width, self.gradient_width) self.gradient.setStart(0, 0) self.gradient.setFinalStop(total_width, 0) self.gradient_item.setBrush(QBrush(self.gradient)) self.gradient_item.setPen(QPen(Qt.NoPen)) x = 0 y = 30 for item in self.label_items: move_item_xy(item, x, y, self.parent.graph.animate_plot) x += interval self.rect = QRectF(0, 0, total_width, self.gradient_width + height) def boundingRect(self): return getattr(self, 'rect', QRectF()) def paint(self, painter, option, widget): pass
class OWLegendGradient(QGraphicsObject): gradient_width = 20 def __init__(self, palette, values, parent): QGraphicsObject.__init__(self, parent) self.parent = parent self.palette = palette self.values = values self.legend = parent self.label_items = [QGraphicsTextItem(text, self) for text in values] for i in self.label_items: i.setTextWidth(50) self.rect = QRectF() self.gradient_item = QGraphicsRectItem(self) self.gradient = QLinearGradient() self.gradient.setStops([(v * 0.1, self.palette[v * 0.1]) for v in range(11)]) self.orientation = Qt.Horizontal self.set_orientation(Qt.Vertical) def set_orientation(self, orientation): if self.orientation == orientation: return self.orientation = orientation if self.orientation == Qt.Vertical: height = max( [item.boundingRect().height() for item in self.label_items]) total_height = height * max(5, len(self.label_items)) interval = (total_height - self.label_items[-1].boundingRect().height()) / ( len(self.label_items) - 1) self.gradient_item.setRect(10, 0, self.gradient_width, total_height) self.gradient.setStart(10, 0) self.gradient.setFinalStop(10, total_height) self.gradient_item.setBrush(QBrush(self.gradient)) self.gradient_item.setPen(QPen(Qt.NoPen)) y = 0 x = 30 for item in self.label_items: move_item_xy(item, x, y, self.parent.graph.animate_plot) y += interval self.rect = QRectF( 10, 0, self.gradient_width + max([item.boundingRect().width() for item in self.label_items]), self.label_items[0].boundingRect().height() * max(5, len(self.label_items))) else: width = 50 height = max( [item.boundingRect().height() for item in self.label_items]) total_width = width * max(5, len(self.label_items)) interval = (total_width - self.label_items[-1].boundingRect().width()) / ( len(self.label_items) - 1) self.gradient_item.setRect(0, 0, total_width, self.gradient_width) self.gradient.setStart(0, 0) self.gradient.setFinalStop(total_width, 0) self.gradient_item.setBrush(QBrush(self.gradient)) self.gradient_item.setPen(QPen(Qt.NoPen)) x = 0 y = 30 for item in self.label_items: move_item_xy(item, x, y, self.parent.graph.animate_plot) x += interval self.rect = QRectF(0, 0, total_width, self.gradient_width + height) def boundingRect(self): return getattr(self, 'rect', QRectF()) def paint(self, painter, option, widget): pass
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)