1# -*- coding: utf-8 -*- 2from ..Qt import QtGui, QtCore, QtWidgets 3from .GraphicsView import GraphicsView 4from ..graphicsItems.GradientEditorItem import GradientEditorItem 5 6__all__ = ['GradientWidget'] 7 8 9class GradientWidget(GraphicsView): 10 """ 11 Widget displaying an editable color gradient. The user may add, move, recolor, 12 or remove colors from the gradient. Additionally, a context menu allows the 13 user to select from pre-defined gradients. 14 """ 15 sigGradientChanged = QtCore.Signal(object) 16 sigGradientChangeFinished = QtCore.Signal(object) 17 18 def __init__(self, parent=None, orientation='bottom', *args, **kargs): 19 """ 20 The *orientation* argument may be 'bottom', 'top', 'left', or 'right' 21 indicating whether the gradient is displayed horizontally (top, bottom) 22 or vertically (left, right) and on what side of the gradient the editable 23 ticks will appear. 24 25 All other arguments are passed to 26 :func:`GradientEditorItem.__init__ <pyqtgraph.GradientEditorItem.__init__>`. 27 28 Note: For convenience, this class wraps methods from 29 :class:`GradientEditorItem <pyqtgraph.GradientEditorItem>`. 30 """ 31 GraphicsView.__init__(self, parent, useOpenGL=False, background=None) 32 self.maxDim = 31 33 kargs['tickPen'] = 'k' 34 self.item = GradientEditorItem(*args, **kargs) 35 self.item.sigGradientChanged.connect(self.sigGradientChanged) 36 self.item.sigGradientChangeFinished.connect(self.sigGradientChangeFinished) 37 self.setCentralItem(self.item) 38 self.setOrientation(orientation) 39 self.setCacheMode(self.CacheModeFlag.CacheNone) 40 self.setRenderHints(QtGui.QPainter.RenderHint.Antialiasing | QtGui.QPainter.RenderHint.TextAntialiasing) 41 frame_style = QtWidgets.QFrame.Shape.NoFrame | QtWidgets.QFrame.Shadow.Plain 42 43 self.setFrameStyle(frame_style) 44 #self.setBackgroundRole(QtGui.QPalette.ColorRole.NoRole) 45 #self.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.BrushStyle.NoBrush)) 46 #self.setAutoFillBackground(False) 47 #self.setAttribute(QtCore.Qt.WindowType.WindowType.WidgetAttribute.WA_PaintOnScreen, False) 48 #self.setAttribute(QtCore.Qt.WindowType.WindowType.WidgetAttribute.WA_OpaquePaintEvent, True) 49 50 def setOrientation(self, ort): 51 """Set the orientation of the widget. May be one of 'bottom', 'top', 52 'left', or 'right'.""" 53 self.item.setOrientation(ort) 54 self.orientation = ort 55 self.setMaxDim() 56 57 def setMaxDim(self, mx=None): 58 if mx is None: 59 mx = self.maxDim 60 else: 61 self.maxDim = mx 62 63 if self.orientation in ['bottom', 'top']: 64 self.setFixedHeight(mx) 65 self.setMaximumWidth(16777215) 66 else: 67 self.setFixedWidth(mx) 68 self.setMaximumHeight(16777215) 69 70 def __getattr__(self, attr): 71 ### wrap methods from GradientEditorItem 72 return getattr(self.item, attr) 73 74 def widgetGroupInterface(self): 75 return (self.sigGradientChanged, self.saveState, self.restoreState) 76