def _newCurve(self): w = self.display.widget pen = mkPen(10, len(self.paths) + 2) self.curIndex = -1 p = self.pa.addChild({ 'name': str(len(self.paths) + 1), 'type': 'empty', 'highlight': True, 'removable': True, 'renamable': True, 'autoIncrementName': True }) path = FreehandItem(w.imageItem, pen) pAdd = p.addChild({ 'name': 'Add', 'type': 'action', }) pAdd.sigActivated.connect(self._appendToPath) pAdd.sigActivated.connect(self._menu.hide) pMod = p.addChild({'name': 'Modify', 'type': 'action'}) pMod.sigActivated.connect(self._modifyPath) pMod.sigActivated.connect(self._menu.hide) self._initDraw() self.paths.append(path) color = pen.color() pLineColor = p.addChild({ 'name': 'Line color', 'type': 'color', 'value': color, 'path': path }) pLineColor.sigValueChanged.connect(self._changeLineColor) br = QtGui.QColor(color) br.setAlpha(0) path.setBrush(br) pFillColor = p.addChild({ 'name': 'Fill color', 'type': 'color', 'value': br, 'path': path }) pFillColor.sigValueChanged.connect(self._changeFillColor) self.setChecked(True)
def _add(self, typ, name, state): self.curIndex = -1 p = self.pa.addChild({ 'name': '[%i] %s' % (len(self.paths) + 1, name), 'type': 'empty', 'highlight': True, 'removable': True, 'renamable': True, 'autoIncrementName': True }) if typ == 'Freehand': path = self._addFreehand(p, state) if path.isEmpty(): self._initFreehand() elif typ == 'Grid': path = self._addGrid(p, state) elif typ == 'Isolines': path = self._addIso(p, state) elif typ == 'Rectangle': path = self._addROI(_Rect, p, state) elif typ == 'Ellipse': path = self._addROI(_Ellipse, p, state) self.paths.append(path) color = state['pen'].color() pLineColor = p.addChild({ 'name': 'Line color', 'type': 'color', 'value': color, 'path': path }) pLineColor.sigValueChanged.connect(self._changeLineColor) if 'brush' in state: br = state['brush'].color() else: br = QtGui.QColor(color) br.setAlpha(25) path.setBrush(br) pFillColor = p.addChild({ 'name': 'Fill color', 'type': 'color', 'value': br, 'path': path }) pFillColor.sigValueChanged.connect(self._changeFillColor) self.setChecked(True)
def _setColorViaHistogram(self): #get boundaries from the histogram mn,mx = self.histogram.getLevels() if mn <= self.histogramPos <= mx: #calc x as relative position (0-1) between the boundaries if self.histogramPos == mn: x = 0 else: x = (self.histogramPos-mn) / (mx-mn) self.color = self.histogram.gradient.getColor(x).rgb() self.updateLayer() else: self.qimage.fill(QtGui.QColor(0,0,0,0).rgba()) self.vb.updateViewRange()
def __init__(self, name, widget, pkg, toolClasses): QtGui.QToolBar.__init__(self, name) self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.customContextMenuRequested.connect(self._openContextMenu) #make ToolBar smaller: self.setIconSize(QtCore.QSize(16, 16)) self.layout().setContentsMargins(0, 0, 0, 0) self.setAllowedAreas(QtCore.Qt.TopToolBarArea) #measure toolbar width in order to find best place when added self.width = 9 #9=handle width # whether position of toolbar should be #found depending on available space: # self.needPositioning = False name[0].islower() f = name[0] if f.islower(): name = f.upper() + name[1:] self.name = name self.widget = widget self.toolClasses = toolClasses self._toolsCreated = False # there is a central setup for the visibility of all # toolbars that have the same display widget: show = getattr(pkg, 'show', True) if isinstance(show, dict): #whether to show/hide a toolbar depends on the chosen profile: session = QtGui.QApplication.instance().session profile = session.app_opts.get('profile', None) show = show.get(profile, False) if self.widget.__class__.selectedToolbars.get(name, None) is None: self.widget.__class__.selectedToolbars[name] = [ show, # QtCore.Qt.TopToolBarArea, # TO_QT_POSITION[getattr(pkg, 'position','top')], False ] #->hasBreak # #add logo to toolbar: # icon = getattr(pkg, 'icon', None) # if icon: # #icon can ge given as direct path # if not os.path.exists(icon): # #otherwise it is assumed that # #the icon is to be found in the icon directory: # icon = ICONFOLDER.join(icon) # s = ToolBarIcon(self, icon) tip = getattr(pkg, '__doc__', None) ttname = name if tip: ttname += '\n\t%s' % tip self.setToolTip(ttname) #BACKGROUND COLOR: c = getattr(pkg, 'color', None) if not c is None: #create a semi-transparent colour from #normal background color (grey) #and given color: a = 0.9 b = 1 - a p = self.palette() bg = p.color(self.backgroundRole()) c = QtGui.QColor(c) bg.setRed(a * bg.red() + b * c.red()) bg.setGreen(a * bg.green() + b * c.green()) bg.setBlue(a * bg.blue() + b * c.blue()) self.setBackgroundRole(QtGui.QPalette.Window) p.setColor(self.backgroundRole(), bg) self.setPalette(p) self.setAutoFillBackground(True) self.actionSelect = a = QtGui.QAction(name, self) s = 'contains...' for cls in self.toolClasses: s += '\n %s' % cls.__name__ a.setToolTip(s) a.setCheckable(True) a.setChecked(show) a.triggered.connect(self.setSelected)
def getQColor(self): c = QtGui.QColor(self.color) c.setAlpha(int(self.alpha*255)) return c
import pyqtgraph_karl as pg import numpy as np from pyqtgraph_karl.Qt import QtGui, QtCore RED = QtGui.QColor(QtCore.Qt.red).rgb() class ColorLayerItem(pg.ImageItem): ''' An color overlay for ImageWidget. The transparency is given through the [image] array. ''' def __init__(self, image, imageWidget, alpha=0.5, color=RED, histogram=None, pos=None,**kwargs): self.histogram = None self.alpha = alpha self.color = color self.histogramPos = pos # the position within a histogram to get the colour from self.vb = imageWidget.view.vb self.image_full = image self._lastInd = imageWidget.currentIndex if self.image_full.ndim == 3: #multiple images image = self.image_full[self._lastInd] imageWidget.sigTimeChanged.connect(self.changeLayer) pg.ImageItem.__init__(self, image, **kwargs) self.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver) #create an empty QImage: