def paint(self, painter, option, index): txt = index.model().data(index, qt.Qt.DisplayRole) if txt == '': return if txt.startswith('no'): super(SymbolDelegate, self).paint(painter, option, index) return lineSymbol = lineSymbols[lineSymbolsText[txt]] painter.save() painter.setRenderHint(qt.QPainter.Antialiasing, True) rect = option.rect rect.adjust(+5, 0, -5, 0) symbolFC = qt.QColor(self.parent().color) symbolEC = qt.QColor(self.parent().color) # symbolSize = self.parent().sizeSpinBox.value() * 2 symbolSize = (self.parent().sizeSpinBox.value() + 1) * 1.75 symbolPath = qt.QPainterPath(lineSymbol) scale = symbolSize painter.scale(scale, scale) symbolOffset = qt.QPointF( (rect.left() + rect.right() - symbolSize) * 0.5 / scale, (rect.top() + rect.bottom() - symbolSize) * 0.5 / scale) symbolPath.translate(symbolOffset) symbolBrush = qt.QBrush(symbolFC, qt.Qt.SolidPattern) symbolPen = qt.QPen(symbolEC, 1. / scale, qt.Qt.SolidLine) painter.setPen(symbolPen) painter.setBrush(symbolBrush) painter.drawPath(symbolPath) painter.restore()
def paintEvent(self, e): txt = self.currentText() if txt.startswith('no'): super(LineStyleComboBox, self).paintEvent(e) return lineStyle = lineStyles[lineStylesText[txt]] p = qt.QStylePainter(self) p.setPen(self.palette().color(qt.QPalette.Text)) opt = qt.QStyleOptionComboBox() self.initStyleOption(opt) p.drawComplexControl(qt.QStyle.CC_ComboBox, opt) painter = qt.QPainter(self) painter.save() painter.setRenderHint(qt.QPainter.Antialiasing, False) rect = p.style().subElementRect(qt.QStyle.SE_ComboBoxFocusRect, opt, self) rect.adjust(+5, 0, -5, 0) pen = qt.QPen() pen.setColor(qt.QColor(self.parent().color)) pen.setWidth(self.parent().widthSpinBox.value() * 1.5) pen.setStyle(lineStyle) painter.setPen(pen) middle = (rect.bottom() + rect.top()) / 2 painter.drawLine(rect.left(), middle, rect.right(), middle) painter.restore()
def setProfile(self, x, y, colormap): """ :param profile: a 1D numpy array :param colormap: an XsocsPlot2DColormap instance :param nColors: number of colors :return: """ assert x.ndim == 1 assert y.ndim == 1 self.__colormap = colormap self.__pixmap = pixmap = Qt.QPixmap(Qt.QSize(x.size, self._pimapHeight)) pixmap.fill() xMin = x.min() xMax = x.max() colors = _applyColormap(colormap, x) profileValues = (y * (1.0 * self._pimapHeight / y.max())) points = [Qt.QPointF(0, 0)] points.extend( [Qt.QPointF(idx, val) for idx, val in enumerate(profileValues)]) points.extend([Qt.QPointF(colormap.nColors - 1, 0)]) poly = Qt.QPolygonF(points) if colormap.minVal is not None: lineMin = ((colormap.minVal - xMin) * (pixmap.width() - 1) / (xMax - xMin)) else: lineMin = None if colormap.maxVal is not None: lineMax = ((colormap.maxVal - xMin) * (pixmap.width() - 1) / (xMax - xMin)) else: lineMax = None self.__lineMin = lineMin self.__lineMax = lineMax gradient = Qt.QLinearGradient(Qt.QPoint(0, 0), Qt.QPoint(colormap.nColors - 1, 0)) for idx, color in enumerate(colors): qColor = Qt.QColor.fromRgbF(*color) gradient.setColorAt(idx / (1.0 * (colormap.nColors - 1)), qColor) painter = Qt.QPainter(pixmap) painter.save() painter.scale(1, -1.) painter.translate(Qt.QPointF(0., -1.0 * self._pimapHeight)) brush = Qt.QBrush(gradient) painter.setBrush(brush) painter.setPen(Qt.QPen(Qt.Qt.NoPen)) painter.drawPolygon(poly) painter.restore() painter.end() self.update()
def __init__(self, parent=None): """Constructor""" super(_HashDropZones, self).__init__(parent) pen = qt.QPen() pen.setColor(qt.QColor("#D0D0D0")) pen.setStyle(qt.Qt.DotLine) pen.setWidth(2) self.__dropPen = pen
def hoverLeaveEvent(self, event): self.setCursor(qt.QCursor(qt.Qt.ArrowCursor)) pen = qt.QPen() color = qt.QColor(qt.Qt.white) color.setAlpha(0) pen.setColor(color) pen.setStyle(qt.Qt.NoPen) self.setPen(pen) self.setBrush(color) return qt.QGraphicsRectItem.hoverLeaveEvent(self, event)
def paint(self, painter, option, index): """ Write multiline text without using any wrap or any alignment according to the cell size. :param qt.QPainter painter: Painter context used to displayed the cell :param qt.QStyleOptionViewItem option: Control how the editor is shown :param qt.QIndex index: Index of the data to display """ painter.save() # set colors painter.setPen(qt.QPen(qt.Qt.NoPen)) if option.state & qt.QStyle.State_Selected: brush = option.palette.highlight() painter.setBrush(brush) else: brush = index.data(qt.Qt.BackgroundRole) if brush is None: # default background color for a cell brush = qt.Qt.white painter.setBrush(brush) painter.drawRect(option.rect) if index.isValid(): if option.state & qt.QStyle.State_Selected: brush = option.palette.highlightedText() else: brush = index.data(qt.Qt.ForegroundRole) if brush is None: brush = option.palette.text() painter.setPen(qt.QPen(brush.color())) text = index.data(qt.Qt.DisplayRole) painter.drawText(qt.QRectF(option.rect), text, self.__textOptions) painter.restore()
def paintEvent(self, e): super(QColorLoop, self).paintEvent(e) rect = e.rect() painter = qt.QPainter(self) painter.setRenderHint(qt.QPainter.Antialiasing, False) painter.save() pen = qt.QPen() pen.setWidth(self.LINE_WIDTH) pen.setStyle(qt.Qt.SolidLine) for ic, color in enumerate(self.colorCycle): pen.setColor(qt.QColor(color)) painter.setPen(pen) pos = (ic + 1.5) * self.LINE_WIDTH painter.drawLine(rect.left() + 2 * self.LINE_WIDTH, pos, rect.right() - 2 * self.LINE_WIDTH, pos) painter.restore()
def paintCheckBox(self, painter, rect): for coords in [self.coords1, self.coords2]: pointerPath = qt.QPainterPath() pointerPath.moveTo(*coords[0]) for xy in coords[1:]: if isinstance(xy, tuple): pointerPath.lineTo(*xy) if isinstance(xy, type('')): end = xy.split(',') if end[0] == 'close': pointerPath.closeSubpath() symbolPen = qt.QPen(qt.Qt.black, float(end[1].strip())) symbolBrush = qt.QBrush(qt.Qt.white) painter.setPen(symbolPen) painter.setBrush(symbolBrush) pointerPath.translate(rect.x() + 12, rect.y() + 16) painter.drawPath(pointerPath)
def paintEvent(self, pEvent): # get button geometry widgGeom = self.rect() paintGeom = qt.QRect(widgGeom.left() + 1, widgGeom.top() + 1, widgGeom.width() - 2, widgGeom.height() - 2) # paint background color painter = qt.QPainter(self) if self.brush is not None: painter.fillRect(paintGeom, self.brush) # paint frame pen = qt.QPen(qt.Qt.black) pen.setWidth(1 if not self.isCurrent() else 5) painter.setPen(pen) painter.drawRect(paintGeom) painter.end() qt.QPushButton.paintEvent(self, pEvent)
def paint(self, painter, option, index): txt = index.model().data(index, qt.Qt.DisplayRole) if txt.startswith('no'): super(LineStyleDelegate, self).paint(painter, option, index) return lineStyle = lineStyles[lineStylesText[txt]] painter.save() painter.setRenderHint(qt.QPainter.Antialiasing, False) rect = option.rect rect.adjust(+5, 0, -5, 0) pen = qt.QPen() pen.setColor(qt.QColor(self.parent().color)) # pen.setWidth(self.parent().widthSpinBox.value() * 1.5) pen.setWidth(self.parent().widthSpinBox.value() + 0.5) pen.setStyle(lineStyle) painter.setPen(pen) middle = (rect.bottom() + rect.top()) / 2 painter.drawLine(rect.left(), middle, rect.right(), middle) painter.restore()
def paintEye(self, painter, rect, pupilR=1.5): color = self.EYE_IRIS painter.setBrush(color) painter.setPen(color) radius0 = 5 painter.drawEllipse(rect.center(), radius0, radius0) color = qt.QColor('black') painter.setBrush(color) painter.setPen(color) radius1 = pupilR painter.drawEllipse(rect.center(), radius1, radius1) painter.setPen(qt.QPen(self.EYE_BROW, 1.5)) c0 = rect.center() x0, y0 = c0.x(), c0.y() ww, hh = min(2.5 * radius0, rect.width() // 2), radius0 painter.drawArc(x0 - ww + 0.5, y0 - radius0 - 1, ww * 2, hh * 5 + 1, 35 * 16, 110 * 16) painter.drawArc(x0 - ww + 0.5, y0 + radius0, ww * 2, -hh * 5 + 3, -35 * 16, -110 * 16)
def __init__(self, parent=None, scene=None, keepratio=True): if qt.qVersion() < '5.0': qt.QGraphicsRectItem.__init__(self, parent, scene) else: qt.QGraphicsRectItem.__init__(self, parent) rect = parent.boundingRect() x = rect.x() y = rect.y() w = rect.width() h = rect.height() self._newRect = None self.keepRatio = keepratio self.setRect(qt.QRectF(x + w - 40, y + h - 40, 40, 40)) self.setAcceptHoverEvents(True) pen = qt.QPen() color = qt.QColor(qt.Qt.white) color.setAlpha(0) pen.setColor(color) pen.setStyle(qt.Qt.NoPen) self.setPen(pen) self.setBrush(color) self.setFlag(self.ItemIsMovable, True) self.show()
def paintEvent(self, e): txt = self.currentText() if txt == '': return if txt.startswith('no'): super(SymbolComboBox, self).paintEvent(e) return lineSymbol = lineSymbols[lineSymbolsText[txt]] p = qt.QStylePainter(self) p.setPen(self.palette().color(qt.QPalette.Text)) opt = qt.QStyleOptionComboBox() self.initStyleOption(opt) p.drawComplexControl(qt.QStyle.CC_ComboBox, opt) painter = qt.QPainter(self) painter.save() painter.setRenderHint(qt.QPainter.Antialiasing, True) rect = p.style().subElementRect(qt.QStyle.SE_ComboBoxFocusRect, opt, self) rect.adjust(+5, 0, -5, 0) symbolFC = qt.QColor(self.parent().color) symbolEC = qt.QColor(self.parent().color) symbolSize = self.parent().sizeSpinBox.value() * 2 symbolPath = qt.QPainterPath(lineSymbol) scale = symbolSize painter.scale(scale, scale) symbolOffset = qt.QPointF( (rect.left() + rect.right() - symbolSize) * 0.5 / scale, (rect.top() + rect.bottom() - symbolSize) * 0.5 / scale) symbolPath.translate(symbolOffset) symbolBrush = qt.QBrush(symbolFC, qt.Qt.SolidPattern) symbolPen = qt.QPen(symbolEC, 1. / scale, qt.Qt.SolidLine) painter.setPen(symbolPen) painter.setBrush(symbolBrush) painter.drawPath(symbolPath) painter.restore()
def drawRangeSliderBackground(cls, painter, option, widget): """Draw the background of the RangeSlider widget into the painter. :param qt.QPainter painter: A painter :param StyleOptionRangeSlider option: Options to draw the widget :param qt.QWidget: The widget which have to be drawn """ painter.save() painter.translate(0.5, 0.5) backgroundRect = qt.QRect(option.rect) if backgroundRect.height() > 8: center = backgroundRect.center() backgroundRect.setHeight(8) backgroundRect.moveCenter(center) selectedRangeRect = qt.QRect(backgroundRect) selectedRangeRect.setLeft(option.handlerRect1.center().x()) selectedRangeRect.setRight(option.handlerRect2.center().x()) highlight = option.palette.color(qt.QPalette.Highlight) activeHighlight = highlight selectedOutline = option.palette.color(qt.QPalette.Highlight) buttonColor = option.palette.button().color() val = qt.qGray(buttonColor.rgb()) buttonColor = buttonColor.lighter(100 + max(1, (180 - val) // 6)) buttonColor.setHsv(buttonColor.hue(), buttonColor.saturation() * 0.75, buttonColor.value()) grooveColor = qt.QColor() grooveColor.setHsv(buttonColor.hue(), min(255, (int)(buttonColor.saturation())), min(255, (int)(buttonColor.value() * 0.9))) selectedInnerContrastLine = qt.QColor(255, 255, 255, 30) outline = option.palette.color(qt.QPalette.Background).darker(140) if (option.state & qt.QStyle.State_HasFocus and option.state & qt.QStyle.State_KeyboardFocusChange): outline = highlight.darker(125) if outline.value() > 160: outline.setHsl(highlight.hue(), highlight.saturation(), 160) # Draw background groove painter.setRenderHint(qt.QPainter.Antialiasing, True) gradient = qt.QLinearGradient() gradient.setStart(backgroundRect.center().x(), backgroundRect.top()) gradient.setFinalStop(backgroundRect.center().x(), backgroundRect.bottom()) painter.setPen(qt.QPen(outline)) gradient.setColorAt(0, grooveColor.darker(110)) gradient.setColorAt(1, grooveColor.lighter(110)) painter.setBrush(gradient) painter.drawRoundedRect(backgroundRect.adjusted(1, 1, -2, -2), 1, 1) # Draw slider background for the value gradient = qt.QLinearGradient() gradient.setStart(selectedRangeRect.center().x(), selectedRangeRect.top()) gradient.setFinalStop(selectedRangeRect.center().x(), selectedRangeRect.bottom()) painter.setRenderHint(qt.QPainter.Antialiasing, True) painter.setPen(qt.QPen(selectedOutline)) gradient.setColorAt(0, activeHighlight) gradient.setColorAt(1, activeHighlight.lighter(130)) painter.setBrush(gradient) painter.drawRoundedRect(selectedRangeRect.adjusted(1, 1, -2, -2), 1, 1) painter.setPen(selectedInnerContrastLine) painter.setBrush(qt.Qt.NoBrush) painter.drawRoundedRect(selectedRangeRect.adjusted(2, 2, -3, -3), 1, 1) painter.restore()
def paint(self, painter, option, index): path = index.data(LOAD_ITEM_PATH_ROLE) if path: # lastPath = configDirs.get( # 'Load', self.parent().transformNode.name, fallback='') if configDirs.has_option( 'Load', self.parent().transformNode.name): lastPath = configDirs.get( 'Load', self.parent().transformNode.name) else: lastPath = '' if lastPath: if os.path.normpath(path).lower() == \ os.path.normpath(lastPath).lower(): option.font.setWeight(qt.QFont.Bold) # lastPathSilx = configDirs.get( # 'Load', self.parent().transformNode.name+'_silx', fallback='') if configDirs.has_option( 'Load', self.parent().transformNode.name+'_silx'): lastPathSilx = configDirs.get( 'Load', self.parent().transformNode.name+'_silx') else: lastPathSilx = '' if lastPathSilx: if os.path.normpath(path).lower() == \ os.path.normpath(lastPathSilx).lower(): option.font.setWeight(qt.QFont.Bold) if (option.state & qt.QStyle.State_MouseOver or option.state & qt.QStyle.State_Selected): loadState = index.data(LOAD_DATASET_ROLE) if option.state & qt.QStyle.State_MouseOver: color = self.parent().palette().highlight().color() if loadState is not None: color = qt.QColor(gco.COLOR_LOAD_CAN) else: color = qt.QColor(gco.COLOR_LOAD_CANNOT) color.setAlphaF(0.2) painter.fillRect(option.rect, color) painter.save() color = None active = (option.state & qt.QStyle.State_Selected or option.state & qt.QStyle.State_MouseOver) if active: if loadState is not None: color = qt.QColor(gco.COLOR_LOAD_CAN) else: color = qt.QColor(gco.COLOR_LOAD_CANNOT) if color is not None: color.setAlphaF(0.2) option.palette.setColor(qt.QPalette.Highlight, color) super(SelectionDelegate, self).paint(painter, option, index) path = index.data(USE_HDF5_ARRAY_ROLE) if path is not None and active: pen = qt.QPen(qt.QColor(gco.COLOR_LOAD_CAN)) pen.setWidth(self.pen2Width) painter.setPen(pen) painter.drawRect(option.rect.x() + self.pen2Width//2, option.rect.y() + self.pen2Width//2, option.rect.width() - self.pen2Width, option.rect.height() - self.pen2Width) painter.restore()
def paint(self, painter, option, index): data = index.data(qt.Qt.DisplayRole) if data is None: return bknd = index.data(qt.Qt.BackgroundRole) rect = option.rect painter.save() painter.setRenderHint(qt.QPainter.Antialiasing, False) painter.setPen(qt.Qt.NoPen) if ((option.state & qt.QStyle.State_Selected or option.state & qt.QStyle.State_MouseOver)) and bknd not in [ BAD_BKGND, UNDEFINED_BKGND, NOTFOUND_BKGND, MATHERROR_BKGND ]: color = self.parent().palette().highlight().color() color.setAlphaF(0.1) else: color = bknd if color is not None: painter.setBrush(color) painter.drawRect(rect) if (type(data) is tuple and bknd not in [ BAD_BKGND, UNDEFINED_BKGND, NOTFOUND_BKGND, MATHERROR_BKGND ]): # plot props lineColor = qt.QColor(data[0]) lineProps = data[1] lineWidth = (lineProps.get('linewidth', 1) + 0.5) lineStyle = lineStyles[lineProps.get('linestyle', '-')] if lineStyle == qt.Qt.NoPen: painter.setPen(qt.QPen(qt.Qt.lightGray)) painter.drawText(option.rect, qt.Qt.AlignCenter, "hidden") else: axisY = lineProps.get('yaxis', -1) if isinstance(axisY, type("")): axisY = -1 if axisY.startswith("l") else 1 # line linePen = qt.QPen(qt.QBrush(lineColor), lineWidth, lineStyle, qt.Qt.FlatCap) painter.setPen(linePen) linePath = qt.QPainterPath() if axisY == -1: # forbid left arrow, comment out to allow it axisY = 0 dl = lineWidth if axisY == -1 else 0 dr = lineWidth if axisY == 1 else 0 linePath.moveTo(rect.left() + 3 + dl, (rect.top() + rect.bottom()) * 0.5) linePath.lineTo(rect.right() - 3 - dr, (rect.top() + rect.bottom()) * 0.5) painter.drawPath(linePath) # > or < symbol font = painter.font() font.setFamily("Arial") font.setPointSize(4 + lineWidth) painter.setFont(font) dh = 2 rect.setBottom(rect.bottom() - dh) if axisY == -1: painter.drawText(rect, qt.Qt.AlignLeft | qt.Qt.AlignVCenter, LEFT_SYMBOL) elif axisY == 1: painter.drawText(rect, qt.Qt.AlignRight | qt.Qt.AlignVCenter, RIGHT_SYMBOL) rect.setBottom(rect.bottom() + dh) symbol = lineProps.get('symbol', None) if symbol in noSymbols: symbol = None if symbol: painter.setRenderHint(qt.QPainter.Antialiasing, True) # symbolFC = lineProps.get( # 'fc', lineProps.get('facecolor', qt.Qt.black)) # symbolEC = lineProps.get( # 'ec', lineProps.get('edgecolor', qt.Qt.black)) symbolFC = lineColor symbolEC = lineColor symbolSize = (lineProps.get('symbolsize', 2) + 1) * 1.75 symbolPath = qt.QPainterPath(lineSymbols[symbol]) scale = symbolSize painter.scale(scale, scale) symbolOffset = qt.QPointF( (rect.left() + rect.right() - symbolSize) * 0.5 / scale, (rect.top() + rect.bottom() - symbolSize) * 0.5 / scale) symbolPath.translate(symbolOffset) symbolBrush = qt.QBrush(symbolFC, qt.Qt.SolidPattern) # symbolPen = qt.QPen(symbolEC, 1./scale, qt.Qt.SolidLine) symbolPen = qt.QPen(symbolEC, 0, qt.Qt.SolidLine) painter.setPen(symbolPen) painter.setBrush(symbolBrush) painter.drawPath(symbolPath) elif type(data) is not tuple: if isinstance(data, int): painter.setPen(qt.QPen(qt.Qt.lightGray)) else: painter.setPen(qt.QPen(qt.Qt.black)) font = painter.font() # font.setFamily("Arial") # font.setPointSize(10) painter.setFont(font) painter.drawText(option.rect, qt.Qt.AlignCenter, "{0}".format(data)) elif bknd == UNDEFINED_BKGND: painter.setPen(qt.QPen(qt.Qt.darkGray)) font = painter.font() painter.setFont(font) painter.drawText(option.rect, qt.Qt.AlignCenter, "out") elif bknd == MATHERROR_BKGND: painter.setPen(qt.QPen(qt.Qt.red)) font = painter.font() painter.setFont(font) painter.drawText(option.rect, qt.Qt.AlignCenter, "error") elif bknd == NOTFOUND_BKGND: painter.setPen(qt.QPen(qt.Qt.red)) font = painter.font() painter.setFont(font) painter.drawText(option.rect, qt.Qt.AlignCenter, "not found") painter.restore()