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 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 _updatePrinter(self): """Resize :attr:`page`, :attr:`scene` and :attr:`view` to :attr:`printer` width and height.""" printer = self.printer assert printer is not None, \ "_updatePrinter should not be called unless a printer is defined" if self.scene is None: self.scene = qt.QGraphicsScene() self.scene.setBackgroundBrush(qt.QColor(qt.Qt.lightGray)) self.scene.setSceneRect( qt.QRectF(0, 0, printer.width(), printer.height())) if self.page is None: self.page = qt.QGraphicsRectItem(0, 0, printer.width(), printer.height()) self.page.setBrush(qt.QColor(qt.Qt.white)) self.scene.addItem(self.page) self.scene.setSceneRect( qt.QRectF(0, 0, printer.width(), printer.height())) self.page.setPos(qt.QPointF(0.0, 0.0)) self.page.setRect(qt.QRectF(0, 0, printer.width(), printer.height())) if self.view is None: self.view = qt.QGraphicsView(self.scene) self.mainLayout.addWidget(self.view) self._buildStatusBar() # self.view.scale(1./self._viewScale, 1./self._viewScale) self.view.fitInView(self.page.rect(), qt.Qt.KeepAspectRatio) self._viewScale = 1.00 self._updateTargetLabel()
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()
'd': [(0.1, 0.5), (0.5, 0.), (0.9, 0.5), (0.5, 1.)], '+': [(0.0, 0.40), (0.40, 0.40), (0.40, 0.), (0.60, 0.), (0.60, 0.40), (1., 0.40), (1., 0.60), (0.60, 0.60), (0.60, 1.), (0.40, 1.), (0.40, 0.60), (0., 0.60)], 'x': [(0.0, 0.40), (0.40, 0.40), (0.40, 0.), (0.60, 0.), (0.60, 0.40), (1., 0.40), (1., 0.60), (0.60, 0.60), (0.60, 1.), (0.40, 1.), (0.40, 0.60), (0., 0.60)] } for s, c in coords.items(): lineSymbols[s].moveTo(*c[0]) for x, y in c[1:]: lineSymbols[s].lineTo(x, y) lineSymbols[s].closeSubpath() tr = qt.QTransform() tr.rotate(45) lineSymbols['x'].translate(qt.QPointF(-0.5, -0.5)) lineSymbols['x'] = tr.map(lineSymbols['x']) lineSymbols['x'].translate(qt.QPointF(0.5, 0.5)) noSymbols = ('None', 'none', '', ' ') lineSymbolsText = OrderedDict([('no symbol', 'None'), ('circle', 'o'), ('point', '.'), ('pixel', ','), ('cross', '+'), ('x-cross', 'x'), ('diamond', 'd'), ('square', 's'), ('', '')]) class LineStyleDelegate(qt.QItemDelegate): def paint(self, painter, option, index): txt = index.model().data(index, qt.Qt.DisplayRole) if txt.startswith('no'):
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()