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 decorate_welcome_icon(icon, background_color): # type: (QIcon, Union[QColor, str]) -> QIcon """Return a `QIcon` with a circle shaped background. """ welcome_icon = QIcon() sizes = [32, 48, 64, 80, 128, 256] background_color = NAMED_COLORS.get(background_color, background_color) background_color = QColor(background_color) grad = radial_gradient(background_color) for size in sizes: icon_size = QSize(int(5 * size / 8), int(5 * size / 8)) icon_rect = QRect(QPoint(0, 0), icon_size) pixmap = QPixmap(size, size) pixmap.fill(Qt.transparent) p = QPainter(pixmap) p.setRenderHint(QPainter.Antialiasing, True) p.setBrush(QBrush(grad)) p.setPen(Qt.NoPen) ellipse_rect = QRect(0, 0, size, size) p.drawEllipse(ellipse_rect) icon_rect.moveCenter(ellipse_rect.center()) icon.paint( p, icon_rect, Qt.AlignCenter, ) p.end() welcome_icon.addPixmap(pixmap) return welcome_icon
def drawViewItemText(self, style: QStyle, painter: QPainter, option: QStyleOptionViewItem, rect: QRect) -> None: """ Draw view item text in `rect` using `style` and `painter`. """ margin = style.pixelMetric(QStyle.PM_FocusFrameHMargin, None, option.widget) + 1 rect = rect.adjusted(margin, 0, -margin, 0) font = option.font st = self.__static_text_elided_cache(option.text, font, option.fontMetrics, option.textElideMode, rect.width()) tsize = st.size() textalign = int(option.displayAlignment) text_pos_x = text_pos_y = 0.0 if textalign & _Qt_AlignLeft: text_pos_x = rect.left() elif textalign & _Qt_AlignRight: text_pos_x = rect.x() + rect.width() - tsize.width() elif textalign & _Qt_AlignHCenter: text_pos_x = rect.x() + rect.width() / 2 - tsize.width() / 2 if textalign & _Qt_AlignVCenter: text_pos_y = rect.y() + rect.height() / 2 - tsize.height() / 2 elif textalign & _Qt_AlignTop: text_pos_y = rect.top() elif textalign & _Qt_AlignBottom: text_pos_y = rect.top() + rect.height() - tsize.height() painter.setPen(self.__pen_cache(option.palette, option.state)) painter.setFont(font) painter.drawStaticText(QPointF(text_pos_x, text_pos_y), st)
def decorate_welcome_icon(icon, background_color): """Return a `QIcon` with a circle shaped background. """ welcome_icon = QIcon() sizes = [32, 48, 64, 80, 128, 256] background_color = NAMED_COLORS.get(background_color, background_color) background_color = QColor(background_color) grad = radial_gradient(background_color) for size in sizes: icon_size = QSize(5 * size / 8, 5 * size / 8) icon_rect = QRect(QPoint(0, 0), icon_size) pixmap = QPixmap(size, size) pixmap.fill(Qt.transparent) p = QPainter(pixmap) p.setRenderHint(QPainter.Antialiasing, True) p.setBrush(QBrush(grad)) p.setPen(Qt.NoPen) ellipse_rect = QRect(0, 0, size, size) p.drawEllipse(ellipse_rect) icon_rect.moveCenter(ellipse_rect.center()) icon.paint(p, icon_rect, Qt.AlignCenter, ) p.end() welcome_icon.addPixmap(pixmap) return welcome_icon
def decorate_welcome_icon(icon, background_color): """Return a `QIcon` with a circle shaped background. """ welcome_icon = QIcon() sizes = [32, 48, 64, 80] background_color = NAMED_COLORS.get(background_color, background_color) background_color = QColor(background_color) grad = radial_gradient(background_color) for size in sizes: icon_pixmap = icon.pixmap(5 * size / 8, 5 * size / 8) icon_size = icon_pixmap.size() icon_rect = QRect(QPoint(0, 0), icon_size) pixmap = QPixmap(size, size) pixmap.fill(QColor(0, 0, 0, 0)) p = QPainter(pixmap) p.setRenderHint(QPainter.Antialiasing, True) p.setBrush(QBrush(grad)) p.setPen(Qt.NoPen) ellipse_rect = QRect(0, 0, size, size) p.drawEllipse(ellipse_rect) icon_rect.moveCenter(ellipse_rect.center()) p.drawPixmap(icon_rect.topLeft(), icon_pixmap) p.end() welcome_icon.addPixmap(pixmap) return welcome_icon
def splash_screen(): """ """ pm = QPixmap( pkg_resources.resource_filename( __name__, "icons/orange-splash-screen.png") ) version = QCoreApplication.applicationVersion() size = 21 if len(version) < 5 else 16 font = QFont("Helvetica") font.setPixelSize(size) font.setBold(True) font.setItalic(True) font.setLetterSpacing(QFont.AbsoluteSpacing, 2) metrics = QFontMetrics(font) br = metrics.boundingRect(version).adjusted(-5, 0, 5, 0) br.moveCenter(QPoint(436, 224)) p = QPainter(pm) p.setRenderHint(QPainter.Antialiasing) p.setRenderHint(QPainter.TextAntialiasing) p.setFont(font) p.setPen(QColor("#231F20")) p.drawText(br, Qt.AlignCenter, version) p.end() return pm, QRect(88, 193, 200, 20)
def splash_screen(): splash_n = random.randint(1, 3) path = pkg_resources.resource_filename( __name__, f"icons/orange-splash-screen-{splash_n:02}.png") pm = QPixmap(path) version = Config.ApplicationVersion if version: version_parsed = LooseVersion(version) version_comp = version_parsed.version version = ".".join(map(str, version_comp[:2])) size = 13 font = QFont("Helvetica") font.setPixelSize(size) metrics = QFontMetrics(font) br = metrics.boundingRect(version) br.moveTopLeft(QPoint(171, 438)) p = QPainter(pm) p.setRenderHint(QPainter.Antialiasing) p.setRenderHint(QPainter.TextAntialiasing) p.setFont(font) p.setPen(QColor("#000000")) p.drawText(br, Qt.AlignLeft, version) p.end() return pm, QRect(23, 24, 200, 20)
def paint(self, painter: QPainter, *_): painter.save() painter.setPen(pg.mkPen((255, 255, 100), width=1)) painter.setBrush(pg.mkBrush(255, 255, 0, 100)) if self.__selection_range is not None: painter.drawRect(self.selection_rect) painter.restore()
def paintEvent(self, event): opt = QStyleOption() opt.initFrom(self) if not self.__popupicon.isVisible(): return if not (opt.state & QStyle.State_MouseOver or opt.state & QStyle.State_HasFocus): return palette = opt.palette # type: QPalette if opt.state & QStyle.State_HasFocus: pen = QPen(palette.color(QPalette.Highlight)) else: pen = QPen(palette.color(QPalette.Dark)) if self.__fulltext and \ opt.state & QStyle.State_MouseOver and \ opt.state & QStyle.State_Active: g = QLinearGradient() g.setCoordinateMode(QLinearGradient.ObjectBoundingMode) base = palette.color(QPalette.Window) base.setAlpha(90) g.setColorAt(0, base.lighter(200)) g.setColorAt(0.6, base) g.setColorAt(1.0, base.lighter(200)) brush = QBrush(g) else: brush = QBrush(Qt.NoBrush) p = QPainter(self) p.setBrush(brush) p.setPen(pen) p.drawRect(opt.rect.adjusted(0, 0, -1, -1))
def paintEvent(self, event): opt = QStyleOption() opt.initFrom(self) if not self.__popupicon.isVisible(): return if not (opt.state & QStyle.State_MouseOver or opt.state & QStyle.State_HasFocus): return palette = opt.palette # type: QPalette if opt.state & QStyle.State_HasFocus: pen = QPen(palette.color(QPalette.Highlight)) else: pen = QPen(palette.color(QPalette.Dark)) if (self.__fulltext and opt.state & QStyle.State_MouseOver and opt.state & QStyle.State_Active): g = QLinearGradient() g.setCoordinateMode(QLinearGradient.ObjectBoundingMode) base = palette.color(QPalette.Window) base.setAlpha(90) g.setColorAt(0, base.lighter(200)) g.setColorAt(0.6, base) g.setColorAt(1.0, base.lighter(200)) brush = QBrush(g) else: brush = QBrush(Qt.NoBrush) p = QPainter(self) p.setBrush(brush) p.setPen(pen) p.drawRect(opt.rect.adjusted(0, 0, -1, -1))
def splash_screen(): """ """ pm = QPixmap( pkg_resources.resource_filename(__name__, "icons/orange-splash-screen.png")) version = QCoreApplication.applicationVersion() size = 21 if len(version) < 5 else 16 font = QFont("Helvetica") font.setPixelSize(size) font.setBold(True) font.setItalic(True) font.setLetterSpacing(QFont.AbsoluteSpacing, 2) metrics = QFontMetrics(font) br = metrics.boundingRect(version).adjusted(-5, 0, 5, 0) br.moveCenter(QPoint(436, 224)) p = QPainter(pm) p.setRenderHint(QPainter.Antialiasing) p.setRenderHint(QPainter.TextAntialiasing) p.setFont(font) p.setPen(QColor("#231F20")) p.drawText(br, Qt.AlignCenter, version) p.end() return pm, QRect(88, 193, 200, 20)
def paint(self, painter: QPainter, option, widget): painter.setBrush(self.COLOR) painter.setPen(self.COLOR) painter.drawRect(self.boundingRect().adjusted(-self.PADDING, -self.PADDING, self.PADDING, self.PADDING)) super().paint(painter, option, widget)
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 palette_pixmap(colors, size): img = QPixmap(size) img.fill(Qt.transparent) painter = QPainter(img) grad = palette_gradient(colors) grad.setCoordinateMode(QLinearGradient.ObjectBoundingMode) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(grad)) painter.drawRect(0, 0, size.width(), size.height()) painter.end() return img
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 createDiscPalettePixmap(width, height, palette): p = QPainter() img = QPixmap(width, height) p.begin(img) p.setPen(QPen(Qt.NoPen)) if type(palette) == dict: # if palette is the dict with different palette = palette[max(palette.keys())] if len(palette) == 0: return img rectWidth = width / float(len(palette)) for i, col in enumerate(palette): p.setBrush(QBrush(QColor(*col))) p.drawRect(QRectF(i * rectWidth, 0, (i + 1) * rectWidth, height)) return img
def createExContPalettePixmap(width, height, color1, color2, passThroughColors): p = QPainter() img = QPixmap(width, height) p.begin(img) #p.eraseRect(0, 0, w, h) p.setPen(QPen(Qt.NoPen)) g = QLinearGradient(0, 0, width, height) g.setColorAt(0, color1) g.setColorAt(1, color2) for i, color in enumerate(passThroughColors): g.setColorAt(float(i + 1) / (len(passThroughColors) + 1), color) p.fillRect(img.rect(), QBrush(g)) return img
def createContPalettePixmap(width, height, color1, color2, passThroughBlack): p = QPainter() img = QPixmap(width, height) p.begin(img) #p.eraseRect(0, 0, w, h) p.setPen(QPen(Qt.NoPen)) g = QLinearGradient(0, 0, width, height) g.setColorAt(0, color1) g.setColorAt(1, color2) if passThroughBlack: g.setColorAt(0.5, Qt.black) p.fillRect(img.rect(), QBrush(g)) return img
def paintEvent(self, event): if self.__isTransparencySupported: opt = QStyleOption() opt.initFrom(self) rect = opt.rect p = QPainter(self) p.setRenderHint(QPainter.Antialiasing, True) p.setBrush(opt.palette.brush(QPalette.Window)) p.setPen(Qt.NoPen) p.drawRoundedRect(rect, self.__radius, self.__radius) p.end() else: StyledWidget_paintEvent(self, event)
def grab_svg(scene): # type: (QGraphicsScene) -> str """ Return a SVG rendering of the scene contents. Parameters ---------- scene : :class:`CanvasScene` """ svg_buffer = QBuffer() gen = _QSvgGenerator() gen.setOutputDevice(svg_buffer) items_rect = scene.itemsBoundingRect().adjusted(-10, -10, 10, 10) if items_rect.isNull(): items_rect = QRectF(0, 0, 10, 10) width, height = items_rect.width(), items_rect.height() rect_ratio = float(width) / height # Keep a fixed aspect ratio. aspect_ratio = 1.618 if rect_ratio > aspect_ratio: height = int(height * rect_ratio / aspect_ratio) else: width = int(width * aspect_ratio / rect_ratio) target_rect = QRectF(0, 0, width, height) source_rect = QRectF(0, 0, width, height) source_rect.moveCenter(items_rect.center()) gen.setSize(target_rect.size().toSize()) gen.setViewBox(target_rect) painter = QPainter(gen) # Draw background. painter.setPen(Qt.NoPen) painter.setBrush(scene.palette().base()) painter.drawRect(target_rect) # Render the scene scene.render(painter, target_rect, source_rect) painter.end() buffer_str = bytes(svg_buffer.buffer()) return buffer_str.decode("utf-8")
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 __updateMask(self): opt = QStyleOption() opt.initFrom(self) rect = opt.rect size = rect.size() mask = QBitmap(size) p = QPainter(mask) p.setRenderHint(QPainter.Antialiasing) p.setBrush(Qt.black) p.setPen(Qt.NoPen) p.drawRoundedRect(rect, self.__radius, self.__radius) p.end() self.setMask(mask)
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 paintEvent(self, event): super().paintEvent(event) p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) p.setBrush(self.indicator_color) p.save() p.setPen(Qt.NoPen) fm = QFontMetrics(self.font()) width = self.rect().width() height = fm.height() + 6 rect = QRectF(0, 0, width, height) p.drawRoundedRect(rect, 5, 5) p.restore() textstart = (width - fm.width(self.indicator_text)) / 2 p.drawText(textstart, height / 2 + 5, self.indicator_text)
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 __paint(self): picture = QPicture() painter = QPainter(picture) pen = QPen(QBrush(Qt.white), 0.5) pen.setCosmetic(True) painter.setPen(pen) geom = self.geometry x, y = geom.x(), geom.y() w, h = geom.width(), geom.height() wsingle = w / len(self.dist) for d, c in zip(self.dist, self.colors): painter.setBrush(QBrush(c)) painter.drawRect(QRectF(x, y, wsingle, d * h)) x += wsingle painter.end() self.__picture = picture
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 splash_screen(): # type: () -> Tuple[QPixmap, QRect] """ Return a splash screen pixmap and an text area within it. The text area is used for displaying text messages during application startup. The default implementation returns a bland rectangle splash screen. Returns ------- t : Tuple[QPixmap, QRect] A QPixmap and a rect area within it. """ path = pkg_resources.resource_filename( __name__, "icons/orange-canvas-core-splash.svg") pm = QPixmap(path) version = QCoreApplication.applicationVersion() if version: version_parsed = LooseVersion(version) version_comp = version_parsed.version version = ".".join(map(str, version_comp[:2])) size = 21 if len(version) < 5 else 16 font = QFont() font.setPixelSize(size) font.setBold(True) font.setItalic(True) font.setLetterSpacing(QFont.AbsoluteSpacing, 2) metrics = QFontMetrics(font) br = metrics.boundingRect(version).adjusted(-5, 0, 5, 0) br.moveBottomRight(QPoint(pm.width() - 15, pm.height() - 15)) p = QPainter(pm) p.setRenderHint(QPainter.Antialiasing) p.setRenderHint(QPainter.TextAntialiasing) p.setFont(font) p.setPen(QColor("#231F20")) p.drawText(br, Qt.AlignCenter, version) p.end() textarea = QRect(15, 15, 170, 20) return pm, textarea
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 splash_screen(): path = pkg_resources.resource_filename(__name__, "icons/tods-splash-screen.png") pm = QPixmap(path) version = pkg_resources.get_distribution("tods").version size = 18 if len(version) < 5 else 16 font = QFont("Helvetica") font.setPixelSize(size) font.setBold(True) font.setItalic(True) font.setLetterSpacing(QFont.AbsoluteSpacing, 2) metrics = QFontMetrics(font) br = metrics.boundingRect(version).adjusted(-5, 0, 5, 0) br.moveCenter(QPoint(440, 350)) p = QPainter(pm) p.setRenderHint(QPainter.Antialiasing) p.setRenderHint(QPainter.TextAntialiasing) p.setFont(font) p.setPen(QColor("#231F20")) p.drawText(br, Qt.AlignCenter, version) p.end() return pm, QRect(88, 193, 200, 20)
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() 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()