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 paint(self, p, opt, widget): GraphicsWidget.paint(self, p, opt, widget) rect = self.boundingRect() # Boundaries of visible area in scene coord size = self.size if self.size is not None \ else [rect.right()-rect.left(), rect.bottom() - rect.top()] # determine max width of all labels textPadding = 2 # in px labelWidth = 0 labelHeight = 0 for val in self.labels.values(): b = p.boundingRect(QRectF(0, 0, 0, 0), Qt.AlignLeft | Qt.AlignVCenter, str(val)) labelWidth = max(labelWidth, b.width()) labelHeight = max(labelHeight, b.height()) x1_back = (rect.right() - rect.left() - size[0]) / 2 x2_back = (rect.right() - rect.left() + size[0]) / 2 y1_back = (rect.bottom() - rect.top() - size[1]) / 2 y2_back = (rect.bottom() - rect.top() + size[1]) / 2 x1_grad = x1_back + textPadding * 2 x2_grad = x2_back - (labelWidth + textPadding * 2) y1_grad = y1_back + (labelHeight / 2 + textPadding) y2_grad = y2_back - (labelHeight / 2 + textPadding) # Draw background p.setPen(QPen(QColor(0, 0, 0, 0))) p.setBrush(QBrush(QColor(255, 255, 255, 0))) rect = QRectF(QPointF(x1_back, y1_back), QPointF(x2_back, y2_back)) p.drawRect(rect) # Draw color bar self.gradient.setStart(0, y2_grad) self.gradient.setFinalStop(0, y1_grad) p.setPen(QPen(QColor(0, 0, 0))) p.setBrush(self.gradient) rect = QRectF(QPointF(x1_grad, y1_grad), QPointF(x2_grad, y2_grad)) p.drawRect(rect) # draw labels tx = x2_grad + textPadding * 2 lw = labelWidth lh = labelHeight for key, val in self.labels.items(): y = y1_grad + key * (y2_grad - y1_grad) p.drawText(QRectF(tx, y - lh / 2.0, lw, lh), Qt.AlignLeft | Qt.AlignVCenter, str(val)) self.setMinimumWidth(labelWidth + 2 * textPadding + 20)
def setParentItem(self, parent): ret = GraphicsWidget.setParentItem(self, parent) if self.offset is not None: offset = Point(self.offset) anchorx = 1 if offset[0] <= 0 else 0 anchory = 1 if offset[1] <= 0 else 0 anchor = (anchorx, anchory) self.anchor(itemPos=anchor, parentPos=anchor, offset=offset) parent.items.append(self) return ret
def __init__(self, *args, text="", offset=None): GraphicsWidget.__init__(self) GraphicsWidgetAnchor.__init__(self) self.setFlag(self.ItemIgnoresTransformations) self.layout = QtGui.QGraphicsGridLayout() self.setLayout(self.layout) self.item_anchor = (0, 0) self.object_anchor = (0, 0) if offset is None: self.offset = (0, 0) else: self.offset = offset self.label_item = LabelItem() self.label_item.setText(text) self.layout.addItem(self.label_item, 0, 0) self.pen = mkPen(255, 255, 255, 100) self.brush = mkBrush(100, 100, 100, 50) self.updateSize()
def __init__(self, image=None, fillHistogram=True, bounds: tuple = None): GraphicsWidget.__init__(self) self.imageItem = lambda: None # fake a dead weakref self.layout = QGraphicsGridLayout() self.setLayout(self.layout) self.layout.setContentsMargins(1, 1, 1, 1) self.layout.setSpacing(0) self.vb = ViewBox(parent=self) # self.vb.setMaximumHeight(152) # self.vb.setMinimumWidth(45) self.vb.setMouseEnabled(x=True, y=False) self.region = LinearRegionItem([0, 1], 'vertical', swapMode='block', bounds=bounds) self.region.setZValue(1000) self.vb.addItem(self.region) self.region.lines[0].addMarker('<|', 0.5) self.region.lines[1].addMarker('|>', 0.5) self.region.sigRegionChanged.connect(self.regionChanging) self.region.sigRegionChangeFinished.connect(self.regionChanged) self.axis = AxisItem('bottom', linkView=self.vb, maxTickLength=-10, parent=self) self.layout.addItem(self.axis, 1, 0) self.layout.addItem(self.vb, 0, 0) self.range = None self.vb.sigRangeChanged.connect(self.viewRangeChanged) self.plot = PlotCurveItem(pen=(200, 200, 200, 100)) # self.plot.rotate(90) self.vb.addItem(self.plot) self.fillHistogram(fillHistogram) self._showRegions() self.autoHistogramRange() if image is not None: self.setImageItem(image)
def __init__(self, size=None, colors=None, labels=None, minVal=None, maxVal=None): GraphicsWidget.__init__(self) self.setAcceptedMouseButtons(Qt.NoButton) self.size = size # if min and max values are None, assume normalized data self.minVal = 0 if minVal is None else minVal self.maxVal = 1 if maxVal is None else maxVal self.labels = {1: 'min', 0: 'max'} if labels is None else labels self.label_format = '{: .3f}' self.gradient = QLinearGradient() if colors is None: self.colors = [[0, 0, 0], [255, 0, 0]] else: self.colors = colors self.setIntColorScale(colors=self.colors, minVal=self.minVal, maxVal=self.maxVal, labels=self.labels)
def __init__(self, item): GraphicsWidget.__init__(self) self.item = item
def __init__(self, image=None, fillHistogram=True, rgbHistogram=False, levelMode='mono'): GraphicsWidget.__init__(self) self.overlay = False self.lut = None self.imageItem = lambda: None # fake a dead weakref self.levelMode = levelMode self.rgbHistogram = rgbHistogram self.layout = QtGui.QGraphicsGridLayout() self.setLayout(self.layout) self.layout.setContentsMargins(1,1,1,1) self.layout.setSpacing(0) self.vb = ViewBox(parent=self) self.vb.setMaximumWidth(152) self.vb.setMinimumWidth(45) self.vb.setMouseEnabled(x=False, y=True) self.gradient = GradientEditorItem() self.gradient.setOrientation('right') self.gradient.loadPreset('grey') self.regions = [ LinearRegionItem([0, 1], 'horizontal', swapMode='block'), LinearRegionItem([0, 1], 'horizontal', swapMode='block', pen='r', brush=fn.mkBrush((255, 50, 50, 50)), span=(0., 1/3.)), LinearRegionItem([0, 1], 'horizontal', swapMode='block', pen='g', brush=fn.mkBrush((50, 255, 50, 50)), span=(1/3., 2/3.)), LinearRegionItem([0, 1], 'horizontal', swapMode='block', pen='b', brush=fn.mkBrush((50, 50, 255, 80)), span=(2/3., 1.)), LinearRegionItem([0, 1], 'horizontal', swapMode='block', pen='w', brush=fn.mkBrush((255, 255, 255, 50)), span=(2/3., 1.))] for region in self.regions: region.setZValue(1000) self.vb.addItem(region) region.lines[0].addMarker('<|', 0.5) region.lines[1].addMarker('|>', 0.5) region.sigRegionChanged.connect(self.regionChanging) region.sigRegionChangeFinished.connect(self.regionChanged) self.region = self.regions[0] # for backward compatibility. self.axis = AxisItem('left', linkView=self.vb, maxTickLength=-10, parent=self) self.layout.addItem(self.axis, 0, 0) self.layout.addItem(self.vb, 0, 1) self.layout.addItem(self.gradient, 0, 2) self.range = None self.gradient.setFlag(self.gradient.ItemStacksBehindParent) self.vb.setFlag(self.gradient.ItemStacksBehindParent) self.gradient.sigGradientChanged.connect(self.gradientChanged) self.vb.sigRangeChanged.connect(self.viewRangeChanged) add = QtGui.QPainter.CompositionMode_Plus self.plots = [ PlotCurveItem(pen=(200, 200, 200, 100)), # mono PlotCurveItem(pen=(255, 0, 0, 100), compositionMode=add), # r PlotCurveItem(pen=(0, 255, 0, 100), compositionMode=add), # g PlotCurveItem(pen=(0, 0, 255, 100), compositionMode=add), # b PlotCurveItem(pen=(200, 200, 200, 100), compositionMode=add), # a ] self.plot = self.plots[0] # for backward compatibility. for plot in self.plots: plot.rotate(90) self.vb.addItem(plot) self.fillHistogram(fillHistogram) self._showRegions() self.vb.addItem(self.plot) self.autoHistogramRange() if image is not None: self.setImageItem(image)