def paintEvent(self, QPaintEvent): import math if self.direction == 'x': _angle = self.parent.rotationX _x1 = self.width() / 2 _y1 = self.height() / 2 _x2 = math.cos(math.radians(_angle)) * (self.width() / 2) _y2 = math.sin(math.radians(_angle)) * (self.width() / 2) elif self.direction == 'y': _angle = self.parent.rotationY _x1 = self.width() / 2 _y1 = self.height() / 2 _x2 = math.cos(math.radians(_angle + 90)) * (self.height() / 2) _y2 = math.sin(math.radians(_angle + 90)) * (self.height() / 2) else: _angle = 0 _x1 = 0 _y1 = 0 _x2 = 0 _y2 = 0 _painter = QPainter(self) _pm = QPixmap(self.width(), self.height()) _pm_compass = QPixmap(self.compassImage).transformed(QTransform().rotate(self.compassRotation)) _pm_image = QPixmap(self.image).transformed(QTransform().rotate(_angle)) _painter.drawPixmap(0, 0, _pm_compass) _painter.setPen(QColor(255, 160, 47)) _painter.drawLine(_x1, _y1, _x2, _y2) _x = (self.width() - _pm_image.width()) / 2 _y = (self.height() - _pm_image.height()) / 2 _painter.drawPixmap(_x, _y, _pm_image) _painter.end()
def paintEvent(self, e): """ :param e: :return: """ self._is_painting = True super(EventsWidget, self).paintEvent(e) painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.setFont(QFont('Decorative', 8)) slider_pos = self._scroll.sliderPosition() start = slider_pos * self._scale end = start + self.width() * self._scale for i, track in enumerate(self._tracks): if self._break_draw: break track.draw_events(painter, start, end, track_index=i, left_shift=-self._scroll.sliderPosition(), scale=self._scale) # Draw only from pixel start to end painter.setPen(QtCore.Qt.DashLine) painter.setOpacity(0.3) # print('Draw', start, end, self._scale, self._scroll.sliderPosition(), self.width()) # Draw vertical lines for x in range(start - (start % (100 * self._scale)), end, 100 * self._scale): x2draw = (x - slider_pos * self._scale) // self._scale painter.drawLine(x2draw, 20, x2draw, self.height()) string = str(x) boundtext = painter.boundingRect(QtCore.QRectF(), string) painter.drawText(x2draw - boundtext.width() / 2, 15, string) for index, track in enumerate(self._tracks): top = self.which_top(index) # print(top) painter.drawLine(0, top, self.width(), top) painter.drawText(10, top + 15, track.title) painter.setOpacity(1.0) self._pointer.draw(painter, left_shift=-slider_pos, scale=self._scale) # Draw the time pointer painter.end() self._break_draw = False self._is_painting = False
def paintEvent(self, e): # call the base implementation to paint normal interface super(GaugeWidgetVertical, self).paintEvent(e) draw = QPainter() draw.begin(self) window_with = self.width() - 1 diff = (self._higher - self._lower) * self.scale try: self._step = float(self.height()) / float(diff) except ZeroDivisionError: self._step = 0 y_start = self.height() - (self._minVal - self._lower) * self._step * self.scale y_end = self.height() - (self._maxVal - self._lower) * self._step * self.scale draw.setOpacity(1.0) draw.setBrush(QtCore.Qt.NoBrush) draw.setPen(QColor(200, 200, 255)) b = int(self.height() / 5) e = int(self.height() - (self.height() / 5) + 1) s = int(self.height() / 5) for i in range(b, e, s): draw.drawLine(0, i, window_with, i) draw.setBrush(QColor(33, 133, 208)) draw.setPen(QColor(33, 133, 208)) draw.setOpacity(0.7) draw.drawRect(0, y_start, window_with, y_end - y_start) draw.setFont(QFont('Decorative', 8)) draw.setPen(QColor(30, 30, 30)) draw.drawText( 3, self.height() - 3, str(self._lower) if self._use_float else str( int(round(self._lower)))) draw.drawText( 3, 10, str(self._higher) if self._use_float else str( int(round(self._higher)))) draw.drawText( 3, y_start + 11, str(self._minVal) if self._use_float else str( int(round(self._minVal)))) draw.drawText( 3, y_end - 4, str(self._maxVal) if self._use_float else str( int(round(self._maxVal)))) draw.end()
def paintEvent(self, event): """ Paint event :param event: """ super(TimelineWidget, self).paintEvent(event) painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.setFont(QFont('Decorative', 8)) # find the start and end X coordinate to draw start = self.scrollbar.sliderPosition() end = start + self.parent().width() + 50 # Draw graphs ########################################################## if len(self._graphs) > 0: painter.setPen(QtCore.Qt.black) middle = self.height() // 2 painter.setOpacity(0.1) painter.drawLine(start, middle, end, middle) for chart in self._graphs: chart.draw(painter, start, end, 0, self.height()) # End draw graph ####################################################### for track in self.tracks: track.draw_title(painter, start, end) self.__draw_track_lines(painter, start, end) if self._selected_track is not None: self._selected_track.draw_background(painter, start, end) for track in self.tracks: track.draw_events(painter, start, end) # Draw the selected element if self._selected != None: painter.setBrush(QColor(255, 0, 0)) self._selected.draw(painter, showvalues=True) # Draw the time pointer self._pointer.draw(painter, highlight=self._creating_event) painter.end() if not hasattr(self, '_is_refreshing'): self._is_refreshing = True self.update() else: del self._is_refreshing
def paintEvent(self, e): # call the base implementation to paint normal interface QWidget.paintEvent(self, e); draw = QPainter(); draw.begin(self) h = self.height() - 1 diff = (self._higher - self._lower) * self.scale try: self._step = float(self.width()) / float(diff) except ZeroDivisionError: self._step = 0 x_start = (self._minVal - self._lower) * self._step * self.scale x_end = (self._maxVal - self._lower) * self._step * self.scale draw.setOpacity(1.0) draw.setBrush(QtCore.Qt.NoBrush) draw.setPen(QColor(200, 200, 255)) for i in range( int(self.width()/5), int(self.width()-self.width()/5) + 1, int(self.width()/5) ): draw.drawLine(i, 0, i, h) draw.setBrush(QColor(238, 238, 238)) draw.setPen(QColor(238, 238, 238)) draw.drawRoundedRect(0, 2, self.width(), h - 4, 3, 3) draw.setBrush(QColor(33, 133, 208)) draw.setPen(QColor(33, 133, 208)) draw.drawRoundedRect(int(round(x_start)), 2, int(round(x_end - x_start)), h - 4, 3, 3) # draw.setOpacity(1.0) draw.setFont(QFont('Decorative', 8)) draw.setPen(QColor(80, 80, 80)) str(self._maxVal) if self._use_float else str(int(round(self._maxVal))) boundtext = draw.boundingRect(QtCore.QRectF(), str(self._higher) if self._use_float else str(int(round(self._higher)))) draw.drawText(self.width() - boundtext.width(), 14, str(self._higher) if self._use_float else str(int(round(self._higher)))) draw.drawText(0, 14, str(self._lower) if self._use_float else str(int(round(self._lower)))) draw.setPen(QColor(255, 255, 255)) boundtext = draw.boundingRect(QtCore.QRectF(), str(self._minVal) if self._use_float else str(int(round(self._minVal)))) draw.drawText(x_start + 2, 14, str(self._minVal) if self._use_float else str(int(round(self._minVal)))) boundtext = draw.boundingRect(QtCore.QRectF(), str(self._maxVal) if self._use_float else str(int(round(self._maxVal)))) draw.drawText(x_end - boundtext.width(), 14, str(self._maxVal) if self._use_float else str(int(round(self._maxVal)))) draw.end()
def paint(self, painter: QPainter, *_): painter.save() painter.setPen(pg.mkPen(QColor(Qt.black))) painter.setBrush(self.__violin_brush) painter.drawPath(self.__violin_path) if self.__show_rug_plot: data, density = self.__rug_plot_data painter.setPen(pg.mkPen(QColor(Qt.black), width=1)) for x, y in zip(density, data): if self.__orientation == Qt.Vertical: painter.drawLine(QPointF(-x, y), QPointF(x, y)) else: painter.drawLine(QPointF(y, -x), QPointF(y, x)) painter.restore()
def crosshairs(color, radius=24, circle=False): radius = max(radius, 16) pixmap = QPixmap(radius, radius) pixmap.fill(Qt.transparent) painter = QPainter() painter.begin(pixmap) painter.setRenderHints(QPainter.Antialiasing) pen = QPen(QBrush(color), 1) pen.setWidthF(1.5) painter.setPen(pen) if circle: painter.drawEllipse(2, 2, radius - 2, radius - 2) painter.drawLine(radius / 2, 7, radius / 2, radius / 2 - 7) painter.drawLine(7, radius / 2, radius / 2 - 7, radius / 2) painter.end() return pixmap
def crosshairs(color, radius=24, circle=False): radius = max(radius, 16) pixmap = QPixmap(radius, radius) pixmap.fill(Qt.transparent) painter = QPainter() painter.begin(pixmap) painter.setRenderHints(QPainter.Antialiasing) pen = QPen(QBrush(color), 1) pen.setWidthF(1.5) painter.setPen(pen) if circle: painter.drawEllipse(2, 2, radius - 2, radius - 2) painter.drawLine(radius / 2, 7, radius / 2, radius / 2 - 7) painter.drawLine(7, radius / 2, radius / 2 - 7, radius / 2) painter.end() return pixmap
def paint( self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex ) -> None: opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) widget = option.widget style = QApplication.style() if widget is None else widget.style() self.__style = style text = opt.text opt.text = "" style.drawControl(QStyle.CE_ItemViewItem, opt, painter, widget) textrect = style.subElementRect( QStyle.SE_ItemViewItemText, opt, widget) ratio = self.barFillRatioData(index) if ratio is not None and 0. <= ratio <= 1.: color = self.barColorData(index) if color is None: color = self.color if not color.isValid(): color = opt.palette.color(QPalette.Foreground) rect = option.rect pw = self.penWidth hmargin = 3 + pw / 2 # + half pen width for the round line cap vmargin = 1 textoffset = pw + vmargin * 2 baseline = rect.bottom() - textoffset / 2 width = (rect.width() - 2 * hmargin) * ratio painter.save() painter.setRenderHint(QPainter.Antialiasing) pen = self.__pen pen.setColor(color) pen.setWidth(pw) painter.setPen(pen) line = self.__line left = rect.left() + hmargin line.setLine(left, baseline, left + width, baseline) painter.drawLine(line) painter.restore() textrect.adjust(0, 0, 0, -textoffset) opt.text = text self.drawViewItemText(style, painter, opt, textrect)
def paint(self, painter: QPainter, _, widget: Optional[QWidget]): painter.save() q0, q25, q75, q100 = self.__box_plot_data if self.__orientation == Qt.Vertical: quartile1 = QPointF(0, q0), QPointF(0, q100) quartile2 = QPointF(0, q25), QPointF(0, q75) else: quartile1 = QPointF(q0, 0), QPointF(q100, 0) quartile2 = QPointF(q25, 0), QPointF(q75, 0) factor = 1 if widget is None else widget.devicePixelRatio() painter.setPen(pg.mkPen(QColor(Qt.black), width=2 * factor)) painter.drawLine(*quartile1) painter.setPen(pg.mkPen(QColor(Qt.black), width=6 * factor)) painter.drawLine(*quartile2) painter.restore()
def _makeTickMarks(self): if self._tickList is None: return self._tickImage = pixmap = QPixmap( self.width() - self._handleWidth - self._handleWidth, self._tickHeight) pixmap.fill(self.palette().color(QPalette.Active, QPalette.Background)) painter = QPainter(self._tickImage) painter.setPen(QColor(165, 162, 148)) w = pixmap.width() - 1 v = self.__steps for val in self._tickList: if val < self.__scaledMinimum or val > self.__scaledMaximum: continue step = self.valueToSteps(val) x = step * w / v painter.drawLine(x, 0, x, self._tickHeight - 1)
def setHistogram(self, values=None, bins=None, use_kde=False, histogram=None): """ Set background histogram (or density estimation, violin plot) The histogram of bins is calculated from values, optionally as a Gaussian KDE. If histogram is provided, its values are used directly and other parameters are ignored. """ if (values is None or not len(values)) and histogram is None: self.setPixmap(None) return if histogram is not None: self._histogram = hist = histogram else: if bins is None: bins = min(100, max(10, len(values) // 20)) if use_kde: hist = gaussian_kde( values, None if isinstance(use_kde, bool) else use_kde)( np.linspace(np.min(values), np.max(values), bins)) else: hist = np.histogram(values, bins)[0] self._histogram = hist = hist / hist.max() HEIGHT = self.rect().height() / 2 OFFSET = HEIGHT * .3 pixmap = QPixmap(QSize( len(hist), 2 * (HEIGHT + OFFSET))) # +1 avoids right/bottom frame border shadow pixmap.fill(Qt.transparent) painter = QPainter(pixmap) painter.setPen(QPen(Qt.darkGray)) for x, value in enumerate(hist): painter.drawLine(x, HEIGHT * (1 - value) + OFFSET, x, HEIGHT * (1 + value) + OFFSET) if self.orientation() != Qt.Horizontal: pixmap = pixmap.transformed(QTransform().rotate(-90)) self.setPixmap(pixmap)
def setHistogram(self, values=None, bins=None, use_kde=False, histogram=None): """ Set background histogram (or density estimation, violin plot) The histogram of bins is calculated from values, optionally as a Gaussian KDE. If histogram is provided, its values are used directly and other parameters are ignored. """ if (values is None or not len(values)) and histogram is None: self.setPixmap(None) return if histogram is not None: self._histogram = hist = histogram else: if bins is None: bins = min(100, max(10, len(values) // 20)) if use_kde: hist = gaussian_kde(values, None if isinstance(use_kde, bool) else use_kde)( np.linspace(np.min(values), np.max(values), bins)) else: hist = np.histogram(values, bins)[0] self._histogram = hist = hist / hist.max() HEIGHT = self.rect().height() / 2 OFFSET = HEIGHT * .3 pixmap = QPixmap(QSize(len(hist), 2 * (HEIGHT + OFFSET))) # +1 avoids right/bottom frame border shadow pixmap.fill(Qt.transparent) painter = QPainter(pixmap) painter.setPen(QPen(Qt.darkGray)) for x, value in enumerate(hist): painter.drawLine(x, HEIGHT * (1 - value) + OFFSET, x, HEIGHT * (1 + value) + OFFSET) if self.orientation() != Qt.Horizontal: pixmap = pixmap.transformed(QTransform().rotate(-90)) self.setPixmap(pixmap)
def paintEvent(self, e): super(TimelineWidget, self).paintEvent(e) painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.setFont(QFont('Decorative', 8)) start = self._scroll.horizontalScrollBar().sliderPosition() end = start + self.parent().width() + 50 # Draw graphs ########################################################## if len(self._charts) > 0: painter.setPen(QtCore.Qt.black) middle = self.height() // 2 painter.setOpacity(0.1) painter.drawLine(start, middle, end, middle) for chart in self._charts: chart.draw(painter, start, end, 0, self.height()) # End draw graph ####################################################### self.__drawTrackLines(painter, start, end) for track in self._tracks: track.drawPeriods(painter, start, end) # Draw the selected element if self._selected != None: painter.setBrush(QColor(255, 0, 0)) self._selected.draw(painter, showvalues=True) # Draw the time pointer self._pointer.draw(painter) painter.end()
def __paintEventNoStyle(self): p = QPainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) fm = QFontMetrics(opt.font) palette = opt.palette # highlight brush is used as the background for the icon and background # when the tab is expanded and as mouse hover color (lighter). brush_highlight = palette.highlight() if opt.state & QStyle.State_Sunken: # State 'down' pressed during a mouse press (slightly darker). background_brush = brush_darker(brush_highlight, 110) elif opt.state & QStyle.State_MouseOver: background_brush = brush_darker(brush_highlight, 95) elif opt.state & QStyle.State_On: background_brush = brush_highlight else: # The default button brush. background_brush = palette.button() rect = opt.rect icon_area_rect = QRect(rect) icon_area_rect.setRight(int(icon_area_rect.height() * 1.26)) text_rect = QRect(rect) text_rect.setLeft(icon_area_rect.right() + 10) # Background (TODO: Should the tab button have native # toolbutton shape, drawn using PE_PanelButtonTool or even # QToolBox tab shape) # Default outline pen pen = QPen(palette.color(QPalette.Mid)) p.save() p.setPen(Qt.NoPen) p.setBrush(QBrush(background_brush)) p.drawRect(rect) # Draw the background behind the icon if the background_brush # is different. if not opt.state & QStyle.State_On: p.setBrush(brush_highlight) p.drawRect(icon_area_rect) # Line between the icon and text p.setPen(pen) p.drawLine(icon_area_rect.topRight(), icon_area_rect.bottomRight()) if opt.state & QStyle.State_HasFocus: # Set the focus frame pen and draw the border pen = QPen(QColor(FOCUS_OUTLINE_COLOR)) p.setPen(pen) p.setBrush(Qt.NoBrush) # Adjust for pen rect = rect.adjusted(0, 0, -1, -1) p.drawRect(rect) else: p.setPen(pen) # Draw the top/bottom border if self.position == QStyleOptionToolBox.OnlyOneTab or \ self.position == QStyleOptionToolBox.Beginning or \ self.selected & \ QStyleOptionToolBox.PreviousIsSelected: p.drawLine(rect.topLeft(), rect.topRight()) p.drawLine(rect.bottomLeft(), rect.bottomRight()) p.restore() p.save() text = fm.elidedText(opt.text, Qt.ElideRight, text_rect.width()) p.setPen(QPen(palette.color(QPalette.ButtonText))) p.setFont(opt.font) p.drawText(text_rect, int(Qt.AlignVCenter | Qt.AlignLeft) | \ int(Qt.TextSingleLine), text) if not opt.icon.isNull(): if opt.state & QStyle.State_Enabled: mode = QIcon.Normal else: mode = QIcon.Disabled if opt.state & QStyle.State_On: state = QIcon.On else: state = QIcon.Off icon_area_rect = icon_area_rect icon_rect = QRect(QPoint(0, 0), opt.iconSize) icon_rect.moveCenter(icon_area_rect.center()) opt.icon.paint(p, icon_rect, Qt.AlignCenter, mode, state) p.restore()
def __paintEventNoStyle(self): p = QPainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) fm = QFontMetrics(opt.font) palette = opt.palette # highlight brush is used as the background for the icon and background # when the tab is expanded and as mouse hover color (lighter). brush_highlight = palette.highlight() foregroundrole = QPalette.ButtonText if opt.state & QStyle.State_Sunken: # State 'down' pressed during a mouse press (slightly darker). background_brush = brush_darker(brush_highlight, 110) foregroundrole = QPalette.HighlightedText elif opt.state & QStyle.State_MouseOver: background_brush = brush_darker(brush_highlight, 95) foregroundrole = QPalette.HighlightedText elif opt.state & QStyle.State_On: background_brush = brush_highlight foregroundrole = QPalette.HighlightedText else: # The default button brush. background_brush = palette.button() rect = opt.rect icon_area_rect = QRect(rect) icon_area_rect.setRight(int(icon_area_rect.height() * 1.26)) text_rect = QRect(rect) text_rect.setLeft(icon_area_rect.right() + 10) # Background (TODO: Should the tab button have native # toolbutton shape, drawn using PE_PanelButtonTool or even # QToolBox tab shape) # Default outline pen pen = QPen(palette.color(QPalette.Mid)) p.save() p.setPen(Qt.NoPen) p.setBrush(QBrush(background_brush)) p.drawRect(rect) # Draw the background behind the icon if the background_brush # is different. if not opt.state & QStyle.State_On: p.setBrush(brush_highlight) p.drawRect(icon_area_rect) # Line between the icon and text p.setPen(pen) p.drawLine(icon_area_rect.topRight(), icon_area_rect.bottomRight()) if opt.state & QStyle.State_HasFocus: # Set the focus frame pen and draw the border pen = QPen(QColor(FOCUS_OUTLINE_COLOR)) p.setPen(pen) p.setBrush(Qt.NoBrush) # Adjust for pen rect = rect.adjusted(0, 0, -1, -1) p.drawRect(rect) else: p.setPen(pen) # Draw the top/bottom border if self.position == QStyleOptionToolBox.OnlyOneTab or \ self.position == QStyleOptionToolBox.Beginning or \ self.selected & \ QStyleOptionToolBox.PreviousIsSelected: p.drawLine(rect.topLeft(), rect.topRight()) p.drawLine(rect.bottomLeft(), rect.bottomRight()) p.restore() p.save() text = fm.elidedText(opt.text, Qt.ElideRight, text_rect.width()) p.setPen(QPen(palette.color(foregroundrole))) p.setFont(opt.font) p.drawText(text_rect, int(Qt.AlignVCenter | Qt.AlignLeft) | \ int(Qt.TextSingleLine), text) if not opt.icon.isNull(): if opt.state & QStyle.State_Enabled: mode = QIcon.Normal else: mode = QIcon.Disabled if opt.state & QStyle.State_On: state = QIcon.On else: state = QIcon.Off icon_area_rect = icon_area_rect icon_rect = QRect(QPoint(0, 0), opt.iconSize) icon_rect.moveCenter(icon_area_rect.center()) opt.icon.paint(p, icon_rect, Qt.AlignCenter, mode, state) p.restore()