def paintEvent(self, event): super().paintEvent(event) painter = gui.Painter(self) metrics = painter.get_font_metrics() did_elide = False line_spacing = metrics.lineSpacing() y = 0 layout = gui.TextLayout(self.content, painter.font()) with layout.process_layout(): while True: line = layout.createLine() if not line.isValid(): break line.setLineWidth(self.width()) next_line_y = y + line_spacing if self.height() >= next_line_y + line_spacing: line.draw(painter, core.Point(0, y)) y = next_line_y else: last_line = self.content[line.textStart():] elided_line = metrics.elided_text(last_line, "right", self.width()) painter.drawText(0, y + metrics.ascent(), elided_line) line = layout.createLine() did_elide = line.isValid() break if did_elide != self.elided: self.elided = did_elide self.elision_changed.emit(did_elide)
def paintEvent(self, event): painter = gui.Painter(self) painter.fill_rect(self.rect(), "transparent") painter.use_antialiasing() if self._current_counter >= self._line_num: self._current_counter = 0 painter.set_pen("none") for i in range(0, self._line_num): painter.save() painter.translate(self._inner_radius + self._line_length, self._inner_radius + self._line_length) rotate_angle = float(360 * i) / float(self._line_num) painter.rotate(rotate_angle) painter.translate(self._inner_radius, 0) distance = self.linecount_distance_from_primary( i, self._current_counter, self._line_num) color = self.current_line_color(distance, self._line_num, self._trail_fade_percentage, self._minimum_trail_opacity, self._color) painter.setBrush(color) rect = core.Rect(0, -self._line_width / 2, self._line_length, self._line_width) painter.drawRoundedRect(rect, self._roundness, self._roundness, QtCore.Qt.RelativeSize) painter.restore()
def test_painter(): painter = gui.Painter() painter.use_antialiasing() painter.set_pen("none") painter.fill_rect(core.Rect(), "transparent") painter.set_color("black") painter.set_composition_mode("source_atop")
def create_char( cls, char: str, size: int, background: types.ColorType = "black", color: types.ColorType = "white", ): pixmap = cls(size, size) pixmap.fill(QtCore.Qt.transparent) with gui.Painter(pixmap) as painter: painter.setRenderHints(painter.Antialiasing | painter.TextAntialiasing | painter.SmoothPixmapTransform) bg_color = colors.get_color(background) painter.setPen(bg_color) painter.setBrush(bg_color) margin = 1 + size // 16 text_margin = size // 20 rect = QtCore.QRectF(margin, margin, size - 2 * margin, size - 2 * margin) painter.drawRoundedRect(rect, 30.0, 30.0, QtCore.Qt.RelativeSize) painter.setPen(colors.get_color(color)) font = painter.font() # type: QtGui.QFont font.setPixelSize(size - 2 * margin - 2 * text_margin) painter.setFont(font) painter.drawText(rect, QtCore.Qt.AlignCenter, char) return pixmap
def pixmap(self, size, mode, state) -> QtGui.QPixmap: pm = QtGui.QPixmap(size) pm.fill(QtCore.Qt.GlobalColor.transparent) # type: ignore rect = core.Rect(ZERO_COORD, size) painter = gui.Painter(pm) self.paint(painter, rect, mode, state) return pm
def paintEvent(self, event): painter = gui.Painter(self) painter.use_antialiasing() painter.set_pen("none") painter.set_transparent_background(False) painter.set_transparent_background(True) painter.set_brush(gui.Brush()) with painter.paint_on(widgets.Widget()): pass painter.set_brush(gui.Color("red")) painter.fill_rect((0, 1, 3, 5), "transparent") painter.fill_rect(core.Rect(), "transparent") with pytest.raises(InvalidParamError): painter.fill_rect(core.Rect(), "transparent", "test") with pytest.raises(ValueError): painter.fill_rect(core.Rect(), "testus") painter.set_color("black") painter.set_composition_mode("source_atop") painter.get_composition_mode() == "source_atop" with pytest.raises(InvalidParamError): painter.set_composition_mode("test") painter.set_clip_path(gui.PainterPath(), "replace") with pytest.raises(InvalidParamError): painter.set_clip_path(gui.PainterPath(), "test") with pytest.raises(InvalidParamError): painter.set_pen(style="test") with painter.backup_state(): pass assert painter.get_text_rect("test") is not None
def paintEvent(self, pe): """Redefinition of paintEvent. Makes class NotificationArea available in style sheets. Internal QT function (do not call directly). """ o = widgets.StyleOption.based_on(self) p = gui.Painter(self) self.style().drawPrimitive(widgets.Style.PE_Widget, o, p, self)
def paintEvent(self, pe): """Redefinition of paintEvent, do not call directly. Makes class Notification available in style sheets. Interal Qt function. Should not be called directly. """ o = widgets.StyleOption.based_on(self) p = gui.Painter(self) self.style().drawPrimitive(widgets.Style.PE_Widget, o, p, self)
def get_image(self): image = self.grab() gl_widget = self.findChild(QtWidgets.QOpenGLWidget) if gl_widget: d = gl_widget.mapToGlobal(core.Point()) - self.mapToGlobal(core.Point()) with gui.Painter(image) as painter: painter.set_composition_mode("source_atop") painter.drawImage(d, gl_widget.grabFramebuffer()) return image
def paintEvent(self, e): super().paintEvent(e) style = self.sl.style() st_slider = widgets.StyleOptionSlider() st_slider.initFrom(self.sl) st_slider.orientation = self.sl.orientation() length = style.pixelMetric( widgets.Style.PixelMetric.PM_SliderLength, st_slider, self.sl ) available = style.pixelMetric( widgets.Style.PixelMetric.PM_SliderSpaceAvailable, st_slider, self.sl ) painter = gui.Painter(self) for v, v_str in self.levels: # get the size of the label rect = painter.get_text_rect(v_str) if self.sl.is_horizontal(): x_loc = widgets.Style.sliderPositionFromValue( self.sl.minimum(), self.sl.maximum(), v, available ) # I assume the offset is half the length of slider, therefore # + length//2 x_loc += length // 2 # left bound of the text = center - half of text width + L_margin left = x_loc - rect.width() // 2 + self.left_margin bottom = self.rect().bottom() # enlarge margins if clipping if v == self.sl.minimum(): if left <= 0: self.left_margin = rect.width() // 2 - x_loc self.bottom_margin = max(self.bottom_margin, rect.height()) self.adjust_margins() if v == self.sl.maximum() and rect.width() // 2 >= self.right_margin: self.right_margin = rect.width() // 2 self.adjust_margins() else: y_loc = widgets.Style.sliderPositionFromValue( self.sl.minimum(), self.sl.maximum(), v, available, upsideDown=True ) bottom = y_loc + length // 2 + rect.height() // 2 + self.top_margin - 3 # there is a 3 px offset that I can't attribute to any metric left = self.left_margin - rect.width() if left <= 0: self.left_margin = rect.width() + 2 self.adjust_margins() painter.drawText(left, bottom, v_str)
def set_marker(self, item: str | int | widgets.Widget, color: types.ColorType = "red"): widget = self._get_widget(item) if widget == self._get_current_widget(): return template = self.icon_map[widget] px = template.pixmap(100, 100) with gui.Painter(px) as painter: dot = gui.Pixmap.create_dot(color) painter.drawPixmap(0, 0, dot) icon = gui.Icon(px) self.button_map[widget].set_icon(icon)
def testus(self): self.setup(self.getTargetRect().size()) painter = gui.Painter(self) try: dct = dict(antialias=True, background=self.background, painter=painter) self.setExportMode(True, dct) painter.use_antialiasing() self.getScene().render(painter, core.RectF(self.getTargetRect()), core.RectF(self.getSourceRect())) finally: self.setExportMode(False) painter.end()
def create_checkerboard_pattern(cls, n: int, color_1: types.ColorType, color_2: types.ColorType): """Construct tileable checkerboard pattern for paint events.""" # Brush will be an n×n checkerboard pattern pat = gui.Pixmap(2 * n, 2 * n) bg0 = colors.get_color(color_1) bg1 = colors.get_color(color_2) with gui.Painter(pat) as p: p.setPen(QtCore.Qt.PenStyle.NoPen) # Paint a checkerboard pattern for the color to be overlaid on p.fillRect(pat.rect(), bg0) p.fillRect(0, 0, n, n, bg1) p.fillRect(n, n, 2 * n, 2 * n, bg1) return pat
def create_dot(cls, color: types.ColorType = "black", size: int = 16) -> Pixmap: col = colors.get_color(color) px = cls(size, size) px.fill(QtCore.Qt.GlobalColor.transparent) # type: ignore px_size = px.rect().adjusted(1, 1, -1, -1) with gui.Painter(px) as painter: painter.use_antialiasing() painter.setBrush(col) pen_color = gui.Color(15, 15, 15) painter.set_pen(color=pen_color, width=1) painter.drawEllipse(px_size) return px
def test_painter(): painter = gui.Painter(gui.Image()) painter.use_antialiasing() painter.set_pen("none") painter.fill_rect((0, 1, 3, 5), "transparent") painter.fill_rect(core.Rect(), "transparent") with pytest.raises(ValueError): painter.fill_rect(core.Rect(), "testus") painter.set_color("black") painter.set_composition_mode("source_atop") with pytest.raises(ValueError): painter.set_composition_mode("test") with pytest.raises(ValueError): painter.set_pen("test")
def paintEvent(self, event: gui.PaintEvent): outer_radius = min(self.width(), self.height()) rect = core.RectF(1, 1, outer_radius - 2, outer_radius - 2) with gui.Painter(self) as painter: painter.use_antialiasing() if self.bar_style != "line": self._rebuild_data_brush_if_needed() painter.fillRect(0, 0, outer_radius, outer_radius, self.palette().window()) self._draw_base(painter, rect) self._draw_value(painter, rect, self.current_value) inner_rect, inner_radius = self._calculate_inner_rect(outer_radius) self._draw_inner_background(painter, inner_rect) self._draw_text(painter, inner_rect, inner_radius, self.current_value)
def line_area_paintevent(self, event): painter = gui.Painter(self.line_area) painter.fill_rect(event.rect(), "lightgray") block = self.firstVisibleBlock() block_number = block.blockNumber() top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top() bottom = top + self.blockBoundingRect(block).height() width = self.line_area.width() height = self.fontMetrics().height() painter.set_color("black") while block.isValid() and (top <= event.rect().bottom()): if block.isVisible() and (bottom >= event.rect().top()): number = str(block_number + 1) painter.drawText(0, top, width, height, QtCore.Qt.AlignRight, number) block = block.next() top = bottom bottom = top + self.blockBoundingRect(block).height() block_number += 1
def test_spanslider(qtbot): slider = custom_widgets.SpanSlider() qtbot.addWidget(slider) slider.set_lower_value(10) slider.set_upper_value(20) slider.set_lower_pos(15) slider.set_upper_pos(25) assert slider.lower_value == 15 assert slider.upper_value == 25 slider.set_lower_value(12) slider.set_upper_pos(20) color = gui.Color("blue") slider.set_left_color(color) slider.set_right_color(color) slider.swap_controls() slider.trigger_action(slider.SliderNoAction, True) slider.trigger_action(slider.SliderSingleStepAdd, True) slider.paintEvent(None) slider.pixel_pos_to_value(100) slider.draw_span(gui.Painter(), core.Rect()) slider.move_pressed_handle() qtbot.mouseClick(slider, QtCore.Qt.LeftButton) qtbot.mouseMove(slider, core.Point(20, 20)) assert slider.movement_mode == "free"
def paintEvent(self, event): """Paint the editor, offloading the work to the StarRating class.""" painter = gui.Painter(self) self.star_rating.paint(painter, self.rect(), self.palette(), is_editable=True)
def paintEvent(self, event): # Draw time scale = self.get_scale() with gui.Painter(self) as qp: qp.set_color(self.text_color) qp.setFont(self.text_font) qp.use_antialiasing() w = 0 while (w := w + 100) <= self.width(): time_string = helpers.format_seconds(w * scale) rect = core.Rect(w - 50, 0, 100, 100) qp.drawText(rect, constants.ALIGN_H_CENTER, time_string) # Draw down line qp.set_pen(color=PEN_COLOR, width=5) qp.drawLine(0, 40, self.width(), 40) # Draw dash lines point = 0 qp.set_pen(color=self.text_color) qp.drawLine(0, 40, self.width(), 40) while point <= self.width(): y2 = 30 if point % 30 != 0 else 20 qp.drawLine(3 * point, 40, 3 * point, y2) point += 10 if self._position is not None and self._is_in: qp.drawLine(self._position.x(), 0, self._position.x(), 40) poly = gui.Polygon() if self._position is not None: val = self.pointer_time_pos / self.get_scale() line = core.Line(val, 40, val, self.height()) poly.add_points((val - 10, 20), (val + 10, 20), (val, 40)) else: line = core.Line(0, 0, 0, self.height()) poly.add_points((-10, 20), (10, 20), (0, 40)) # Draw samples t = 0.0 for sample in self.video_samples: scaled_dur = sample.duration / scale scaled_t = t / scale t += sample.duration # Clear clip path with qp.clip_path() as path: rect = core.RectF(scaled_t, 50, scaled_dur, 200) path.addRoundedRect(rect, 10, 10) # Draw sample path = gui.PainterPath() qp.set_pen(color=sample.color) rect = core.RectF(scaled_t, 50, scaled_dur, 50) path.addRoundedRect(rect, 10, 10) sample.start_pos = scaled_t sample.end_pos = scaled_t + scaled_dur qp.fillPath(path, sample.color) qp.drawPath(path) # Draw preview pictures if sample.picture is None: continue pic_width = sample.picture.size().width() if pic_width < scaled_dur: width = float(pic_width) pic = sample.picture else: width = scaled_dur pic = sample.picture.copy(0, 0, int(scaled_dur), 45) with qp.clip_path() as path: rect = core.RectF(scaled_t, 52.5, width, 45) path.addRoundedRect(rect, 10, 10) qp.drawPixmap(int(scaled_t), int(52.5), int(width), 45, pic) # Clear clip path with qp.clip_path() as path: path.add_rect(self.rect()) # Draw pointer qp.set_color(PEN_COLOR) qp.set_brush(PEN_COLOR) qp.drawPolygon(poly) qp.drawLine(line)