def paint(self, painter: QPainter, option, widget): pen = QPen(self.color, self.penWidth) if self.isSelected(): pen.setStyle(Qt.DashLine) pen.setCosmetic(True) painter.setPen(pen) painter.drawRect(self.rect())
def __init__( self, image: ScaledImageItem, data: np.ndarray, pen: QtGui.QPen = None, font: QtGui.QFont = None, parent: QtWidgets.QGraphicsItem = None, ): super().__init__(image, parent) if pen is None: pen = QtGui.QPen(QtCore.Qt.white, 2.0, QtCore.Qt.DotLine) pen.setCosmetic(True) pen.setCapStyle(QtCore.Qt.RoundCap) if font is None: font = QtGui.QFont() self.image_data = data self.sliced: Optional[np.ndarray] = None self.pen = pen self.font = font self.line = QtCore.QLineF() self.poly = QtGui.QPolygonF() self.action_copy_to_clipboard = qAction( "insert-text", "Copy To Clipboard", "Copy slice values to the clipboard.", self.actionCopyToClipboard, )
def addMark(self, new_mark: SliderMark, update: bool = True): """Adds a marked value to the slider. Args: new_mark: value to mark update: Whether to redraw slider with new mark. Returns: None. """ # check if mark is within slider range if new_mark.val > self._val_max: return if new_mark.val < self._val_min: return self._marks.add(new_mark) v_top_pad = self._header_height + 1 v_bottom_pad = 1 v_top_pad += new_mark.top_pad v_bottom_pad += new_mark.bottom_pad width = new_mark.visual_width v_offset = v_top_pad if new_mark.type == "track": v_offset += self._get_track_vertical_pos( *self._get_track_column_row(new_mark.row) ) height = new_mark.get_height( container_height=self.box_rect.height() - self._header_height ) color = new_mark.QColor pen = QPen(color, 0.5) pen.setCosmetic(True) brush = QBrush(color) if new_mark.filled else QBrush() line = self.scene.addRect(-width // 2, v_offset, width, height, pen, brush) self._mark_items[new_mark] = line if new_mark.type == "tick": # Show tick mark behind other slider marks self._mark_items[new_mark].setZValue(0) # Add a text label to show in header area mark_label_text = ( f"{new_mark.val + self.tick_index_offset:g}" # sci notation if large ) self._mark_labels[new_mark] = self.scene.addSimpleText( mark_label_text, self._base_font ) else: # Show in front of tick marks self._mark_items[new_mark].setZValue(1) if update: self._update_visual_positions()
def create_pens(self): pen = QPen(QColor("#33434B"), 3) pen.setCosmetic(True) # makes pen size zoom independent self.link_pen = pen pen = QPen(QColor("#5A667A"), self.room_size) pen.setCosmetic(True) # makes pen size zoom independent self.room_pen = pen pen = QPen(QColor("#FF0266"), self.ant_size) pen.setCosmetic(True) # makes pen size zoom independent self.ant_pen = pen pen = QPen(QColor("#AAAAAA"), 1) self.text_pen = pen pen = QPen(QColor("#20FF20"), 1) self.special_text_pen = pen # create path pens ant_colors = ["#FF008D", "#FF00FF", "#FFE100", "#FF0000"] self.solution_path_pens = [ QPen(QColor(color).darker(100), 3) for color in ant_colors ] for pen in self.solution_path_pens: pen.setCosmetic(True) self.ant_pens = [ QPen(QColor(color), self.ant_size) for color in ant_colors ] for pen in self.ant_pens: pen.setCosmetic(True)
def __init__( self, image: ScaledImageItem, pen: QtGui.QPen = None, font: QtGui.QFont = None, unit: str = "μm", parent: QtWidgets.QGraphicsItem = None, ): super().__init__(image, None, parent) if pen is None: pen = QtGui.QPen(QtCore.Qt.white, 2.0) pen.setCosmetic(True) pen.setStyle(QtCore.Qt.DashLine) pen.setCapStyle(QtCore.Qt.RoundCap) if font is None: font = QtGui.QFont() self.pen = pen self.font = font self.text = "" self.unit = unit self.line = QtCore.QLineF()
def doubleBufferPaint(self, buffer=None): self.blockSignals(True) #self.drawFrame(painter) if buffer is None: buffer = QImage(self.width(), self.height(), QImage.Format_RGB888) buffer.fill(Qt.black) # calculate offset and scale of droplet image pixmap scale_x, scale_y, offset_x, offset_y = self.get_from_image_transform() db_painter = QPainter(buffer) db_painter.setRenderHints(QPainter.Antialiasing | QPainter.NonCosmeticDefaultPen) db_painter.setBackground(QBrush(Qt.black)) db_painter.setPen(QPen(Qt.black, 0)) db_painter.drawPixmap(offset_x, offset_y, self._pixmap) pen = QPen(Qt.magenta, 1) pen_fine = QPen(Qt.blue, 1) pen.setCosmetic(True) db_painter.setPen(pen) # draw droplet outline and tangent only if evaluate_droplet was successful if self._droplet.is_valid: try: # transforming true image coordinates to scaled pixmap coordinates db_painter.translate(offset_x, offset_y) db_painter.scale(scale_x, scale_y) # drawing tangents and baseline db_painter.drawLine(*self._droplet.line_l) db_painter.drawLine(*self._droplet.line_r) db_painter.drawLine(*self._droplet.int_l, *self._droplet.int_r) # move origin to ellipse origin db_painter.translate(*self._droplet.center) # draw diagnostics # db_painter.setPen(pen_fine) # # lines parallel to coordinate axes # db_painter.drawLine(0,0,20*scale_x,0) # db_painter.drawLine(0,0,0,20*scale_y) # # angle arc # db_painter.drawArc(-5*scale_x, -5*scale_y, 10*scale_x, 10*scale_y, 0, -self._droplet.tilt_deg*16) # rotate coordinates to ellipse tilt db_painter.rotate(self._droplet.tilt_deg) # draw ellipse # db_painter.setPen(pen) db_painter.drawEllipse(-self._droplet.maj / 2, -self._droplet.min / 2, self._droplet.maj, self._droplet.min) # # major and minor axis for diagnostics # db_painter.drawLine(0, 0, self._droplet.maj/2, 0) # db_painter.drawLine(0, 0, 0, self._droplet.min/2) except Exception as ex: logging.error(ex) db_painter.end() self.blockSignals(False) return buffer
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: Optional[QWidget]) -> None: if self.is_on_selected_path: pen = QPen(QColor("green"), 5) pen.setCosmetic(True) painter.setPen(pen) painter.drawLine(self.ix, self.iy, self.jx, self.jy) else: pen = QPen(Qt.gray) pen.setWidth(2) pen.setCosmetic(True) painter.setPen(pen) painter.drawLine(self.ix, self.iy, self.jx, self.jy)
def paint(self, painter=QPainter, option=QStyleOptionGraphicsItem, widget=QWidget): corners = 5 body = self.boundingRect() height = body.height() width = body.width() percentage = height / 10 painter.setRenderHint(QPainter.Antialiasing) path = QPainterPath() path.addRoundedRect(body, corners, corners) pen = QPen(QColor(255, 255, 255, 100), 3) if self.hover and not self.isPressed else QPen( Qt.black, 0.1) pen.setJoinStyle(Qt.RoundJoin) pen.setCosmetic(True) painter.setPen(pen) painter.fillPath(path, self._model["bgColor"]) painter.drawPath(path) if not self.isPressed: grad = QLinearGradient(0, height, 0, height - percentage) grad.setColorAt(0, QColor(175, 175, 175, 255)) grad.setColorAt(1, QColor(175, 175, 175, 0)) path = QPainterPath() path.addRoundedRect(body, corners, corners) painter.setCompositionMode(QPainter.CompositionMode_Multiply) painter.setPen(Qt.NoPen) painter.fillPath(path, grad) painter.drawPath(path) grad = QLinearGradient(0, percentage, 0, 0) grad.setColorAt(1, QColor(255, 255, 255, 255)) grad.setColorAt(0, QColor(255, 255, 255, 0)) path = QPainterPath() path.addRoundedRect(body, corners, corners) painter.setCompositionMode(QPainter.CompositionMode_Overlay) painter.setPen(Qt.NoPen) painter.fillPath(path, grad) painter.drawPath(path) painter.setCompositionMode(QPainter.CompositionMode_Source) else: path = QPainterPath() path.addRoundedRect(body, corners, corners) painter.setCompositionMode(QPainter.CompositionMode_Overlay) painter.setPen(Qt.NoPen) painter.fillPath(path, QColor(255, 255, 255)) painter.drawPath(path) painter.setCompositionMode(QPainter.CompositionMode_Source) super(ButtonNode, self).paint(painter, option, widget)
def paintEvent(self, ev): """ Manually implemented paint event :param ev: the QT paint event :return: """ h = self.height() w = self.width() p = QPainter(self) p.setClipRect(ev.region().boundingRect()) pen = QPen(QColor(0, 0, 0)) pen.setWidth(4) ls = QFontMetricsF(p.font()).lineSpacing() for idx, t in enumerate(sorted(list(self._loadData.keys()))): y = 10 + idx * ls pen.setColor(ThreadToColor.singleton.get(t)) p.setPen(pen) p.drawLine(QLineF(15, y, 15 + 15, y)) pen.setColor(QColor(0, 0, 0)) p.setPen(pen) p.drawText(QPointF(35, y), t) if len(self._loadData) > 0: right = max([ polygon[polygon.count() - 1].x() for _, polygon in self._loadData.items() ]) else: right = 0.0 p.translate(w - 10 - right * 20, h - 10) p.scale( 20, -(h - 20) ) # x direction: 20 pixels per second, y direction: spread between 10 and h-10 topleft = p.transform().inverted()[0].map(QPointF(10, 10)) pen.setWidthF(0) pen.setCosmetic(True) left = topleft.x() p.setRenderHint(QPainter.Antialiasing, True) p.setPen(pen) p.drawLine(QLineF(left, 0, right, 0)) p.drawLine(QLineF(left, 0, left, 1)) idx = 0 for t, polygon in self._loadData.items(): pen.setColor(ThreadToColor.singleton.get(t)) p.setPen(pen) p.drawPolyline(polygon) p.end()
def test_cosmetic_pen_with_dashes(self, p, opd): pen = QPen() pen.setDashPattern([2, 1]) pen.setCosmetic(True) pen.setWidth(5) p.setPen(pen) p.scale(10, 10) path = QPainterPath() path.moveTo(0, 0) path.lineTo(3, 0) p.drawPath(path) assert [(colour, lines) for (colour, width, lines) in opd.getOutlines()] == [ ((0.0, 0.0, 0.0, 1.0), [(0.0, 0.0), (1.0, 0.0)]), ((0.0, 0.0, 0.0, 1.0), [(1.5, 0.0), (2.5, 0.0)]), ]
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: Optional[QWidget]) -> None: # Draw a circle for the node pen = QPen(Qt.gray) pen.setWidth(2) pen.setCosmetic(True) # Combine ideas from these code samples to draw items at a fixed size: # https://stackoverflow.com/questions/1222914/qgraphicsview-and-qgraphicsitem-don%C2%B4t-scale-item-when-scaling-the-view-rect # https://www.qtcentre.org/threads/28691-Scale-independent-QGraphicsItem object_rect = self.boundingRect() mapped_rect = painter.transform().mapRect(object_rect) width_ratio = object_rect.width() / mapped_rect.width() scale_factor = max(1, width_ratio) painter.setPen(pen) painter.setBrush(Qt.gray) scaled_diameter = self.diameter * scale_factor painter.drawEllipse((self.x - scaled_diameter / 2), (self.y - scaled_diameter / 2), scaled_diameter, scaled_diameter) # Draw text for the node name label_path = QPainterPath() label_font = QFont("Calibri", 10 * scale_factor) label_path.addText(self.x, -self.y - self.diameter / 2, label_font, self.name) painter.scale(1.0, -1.0) painter.setBrush(Qt.blue) painter.setPen(Qt.NoPen) painter.drawPath(label_path)
def __init__( self, orientation=-1, # for compatibility with QSlider min=0, max=1, val=0, marks=None, *args, **kwargs, ): super(VideoSlider, self).__init__(*args, **kwargs) self.scene = QtWidgets.QGraphicsScene() self.setScene(self.scene) self.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop) self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setMouseTracking(True) self._get_val_tooltip = None self.tick_index_offset = 1 self.zoom_factor = 1 self._track_rows = 0 self._track_height = 5 self._max_tracks_stacked = 120 self._track_stack_skip_count = 10 self._header_label_height = 20 self._header_graph_height = 40 self._header_height = self._header_label_height # room for frame labels self._min_height = 19 + self._header_height self._base_font = QtGui.QFont() self._base_font.setPixelSize(10) self._tick_marks = [] # Add border rect outline_rect = QtCore.QRectF(0, 0, 200, self._min_height - 3) self.box_rect = outline_rect # self.outlineBox = self.scene.addRect(outline_rect) # self.outlineBox.setPen(QPen(QColor("black", alpha=0))) # Add drag handle rect self._handle_width = 6 handle_rect = QtCore.QRect(0, self._handle_top, self._handle_width, self._handle_height) self.setMinimumHeight(self._min_height) self.setMaximumHeight(self._min_height) self.handle = self.scene.addRect(handle_rect) self.handle.setPen(QPen(QColor(80, 80, 80))) self.handle.setBrush(QColor(128, 128, 128, 128)) # Add (hidden) rect to highlight selection self.select_box = self.scene.addRect( QtCore.QRect(0, 1, 0, outline_rect.height() - 2)) self.select_box.setPen(QPen(QColor(80, 80, 255))) self.select_box.setBrush(QColor(80, 80, 255, 128)) self.select_box.hide() self.zoom_box = self.scene.addRect( QtCore.QRect(0, 1, 0, outline_rect.height() - 2)) self.zoom_box.setPen(QPen(QColor(80, 80, 80, 64))) self.zoom_box.setBrush(QColor(80, 80, 80, 64)) self.zoom_box.hide() self.scene.setBackgroundBrush(QBrush(QColor(200, 200, 200))) self.clearSelection() self.setEnabled(True) self.setMinimum(min) self.setMaximum(max) self.setValue(val) self.setMarks(marks) pen = QPen(QColor(80, 80, 255), 0.5) pen.setCosmetic(True) self.poly = self.scene.addPath(QPainterPath(), pen, self.select_box.brush()) self.headerSeries = dict() self._draw_header()
def paintEvent(self, ev): """ Manually implemented paint event of the time / occupancy diagram. :param ev: the qt paint event :return: """ bgcolor = self.palette().color(self.backgroundRole()) h = self.height() w = self.width() p = QPainter(self) p.setClipRect(ev.region().boundingRect()) pen = QPen(QColor(0, 0, 0)) pen.setWidth(0) pen.setCosmetic(True) ls = QFontMetricsF(p.font()).lineSpacing() maxx = 0 minx = None for t in sorted(list(self._spanData.keys())): for port in sorted(list(self._spanData[t].keys())): sd = self._spanData[t][port] maxx = np.maximum(maxx, np.max(sd)) minx = np.minimum( minx, np.min(sd)) if minx is not None else np.min(sd) scalex = 1e-9 * 200 # 200 pixels / second # (maxx-minx)*scalex + offx = w-10 if minx is None: return offx = w - 10 - (maxx - minx) * scalex idx = 0 self.portYCoords = [] for t in sorted(list(self._spanData.keys())): for port in sorted(list(self._spanData[t].keys())): pen.setColor(QColor(0, 0, 0)) p.setPen(pen) y = 10 + idx * ls self.portYCoords.append((t, port, y - ls / 2, y)) idx += 1 sd = self._spanData[t][port] for i in range(sd.shape[0]): x1, x2 = sd[i, :] x1 = (x1 - minx) * scalex + offx x2 = (x2 - minx) * scalex + offx color = ThreadToColor.singleton.get(t) color.setAlpha(125) p.fillRect(QRectF(x1, y - ls / 2, x2 - x1, ls / 2), color) p.drawRect(QRectF(x1, y - ls / 2, x2 - x1, ls / 2)) pen = QPen(QColor(40, 40, 40)) pen.setWidth(0) pen.setCosmetic(True) pen.setStyle(Qt.DashLine) p.setPen(pen) for x in range(w - 10, -1, -20): p.drawLine(x, 10, x, h - 10) idx = 0 pen.setStyle(Qt.SolidLine) p.setPen(pen) for t in sorted(list(self._spanData.keys())): for port in sorted(list(self._spanData[t].keys())): y = 10 + idx * ls idx += 1 br = QFontMetricsF(p.font()).boundingRect(port) br.translate(10, y) p.fillRect(br, bgcolor) p.drawText(10, y, port) p.end()