def _on_point_list_contents_changed(self): if not self._points: return if len(self._points) == 1: # Delete only point's line segment try: edge_item = self.point_items[self._points[0]].edge_item except AttributeError: return if edge_item.scene(): edge_item.scene().removeItem(edge_item) del self.point_items[self._points[0]].edge_item return # Make and update edges (line segments) as needed pis = [(point, self.point_items[point]) for point in self._points] for (point1, point_item1), (point2, point_item2) in zip(pis, pis[1:]): line = Qt.QLineF(point1.x, point1.y, point2.x, point2.y) if not hasattr(point_item1, 'edge_item'): point_item1.edge_item = Qt.QGraphicsLineItem( line, self.parentItem()) point_item1.edge_item.setPen(self.line_pen) point_item1.edge_item.installSceneEventFilter(self) point_item1.edge_item.setZValue(-1) elif point_item1.edge_item.line() != line: point_item1.edge_item.setLine(line) # Delete last point's line segment try: edge_item = self.point_items[self._points[-1]].edge_item except AttributeError: return if edge_item.scene(): edge_item.scene().removeItem(edge_item) del self.point_items[self._points[-1]].edge_item
def __init__(self, data: Union[Frame, Time], color: Qt.QColor = cast(Qt.QColor, Qt.Qt.white), label: str = '', line: Qt.QLineF = Qt.QLineF()) -> None: self.data = data self.color = color self.label = label self.line = line
def _on_point_item_list_content_reset(self): for line_item in self.line_items: self.view.scene().removeItem(line_item) self.line_items = [] if len(self.point_items) > 1: for point_item1, point_item2 in zip(self.point_items, self.point_items[1:]): line_item = Qt.QGraphicsLineItem( Qt.QLineF(point_item1.pos(), point_item2.pos()), self.parentItem()) line_item.setPen(self.line_pen) line_item.installSceneEventFilter(self) line_item.setZValue(-1) self.line_items.append(line_item)
def make_and_store_point_item(self, pos): self._ignore_point_item_position_changed = True try: super().make_and_store_point_item(pos) finally: self._ignore_point_item_position_changed = False if len(self.point_items) > 1: p1 = self.point_items[-2].pos() line_item = Qt.QGraphicsLineItem(Qt.QLineF(p1, pos), self.parentItem()) line_item.setPen(self.line_pen) line_item.installSceneEventFilter(self) line_item.setZValue(-1) self.line_items.append(line_item)
def drawWidget(self, painter: Qt.QPainter) -> None: from copy import deepcopy from vspreview.utils import strfdelta # calculations if self.need_full_repaint: labels_notches = Notches() label_notch_bottom = (self.rect_f.top() + self.font_height + self.notch_label_interval + self.notch_height + 5) label_notch_top = label_notch_bottom - self.notch_height label_notch_x = self.rect_f.left() if self.mode == self.Mode.TIME: notch_interval_t = self.calculate_notch_interval_t( self.notch_interval_target_x) label_format = self.generate_label_format( notch_interval_t, self.end_t) label_notch_t = Time() while (label_notch_x < self.rect_f.right() and label_notch_t <= self.end_t): line = Qt.QLineF(label_notch_x, label_notch_bottom, label_notch_x, label_notch_top) labels_notches.add( Notch(deepcopy(label_notch_t), line=line)) label_notch_t += notch_interval_t label_notch_x = self.t_to_x(label_notch_t) elif self.mode == self.Mode.FRAME: notch_interval_f = self.calculate_notch_interval_f( self.notch_interval_target_x) label_notch_f = Frame(0) while (label_notch_x < self.rect_f.right() and label_notch_f <= self.end_f): line = Qt.QLineF(label_notch_x, label_notch_bottom, label_notch_x, label_notch_top) labels_notches.add( Notch(deepcopy(label_notch_f), line=line)) label_notch_f += notch_interval_f label_notch_x = self.f_to_x(label_notch_f) self.scroll_rect = Qt.QRectF( self.rect_f.left(), label_notch_bottom + self.notch_scroll_interval, self.rect_f.width(), self.scroll_height) for toolbar, notches in self.toolbars_notches.items(): if not toolbar.is_notches_visible(): continue for notch in notches: if isinstance(notch.data, Frame): x = self.f_to_x(notch.data) elif isinstance(notch.data, Time): x = self.t_to_x(notch.data) y = self.scroll_rect.top() notch.line = Qt.QLineF(x, y, x, y + self.scroll_rect.height() - 1) cursor_line = Qt.QLineF( self.cursor_x, self.scroll_rect.top(), self.cursor_x, self.scroll_rect.top() + self.scroll_rect.height() - 1) # drawing if self.need_full_repaint: painter.fillRect(self.rect_f, self.palette().color(Qt.QPalette.Window)) painter.setPen( Qt.QPen(self.palette().color(Qt.QPalette.WindowText))) painter.setRenderHint(Qt.QPainter.Antialiasing, False) painter.drawLines([notch.line for notch in labels_notches]) painter.setRenderHint(Qt.QPainter.Antialiasing) for i, notch in enumerate(labels_notches): line = notch.line anchor_rect = Qt.QRectF(line.x2(), line.y2() - self.notch_label_interval, 0, 0) if self.mode == self.Mode.TIME: time = cast(Time, notch.data) label = strfdelta(time, label_format) if self.mode == self.Mode.FRAME: label = str(notch.data) if i == 0: rect = painter.boundingRect( anchor_rect, Qt.Qt.AlignBottom + Qt.Qt.AlignLeft, label) if self.mode == self.Mode.TIME: rect.moveLeft(-2.5) elif i == (len(labels_notches) - 1): rect = painter.boundingRect( anchor_rect, Qt.Qt.AlignBottom + Qt.Qt.AlignHCenter, label) if rect.right() > self.rect_f.right(): rect = painter.boundingRect( anchor_rect, Qt.Qt.AlignBottom + Qt.Qt.AlignRight, label) else: rect = painter.boundingRect( anchor_rect, Qt.Qt.AlignBottom + Qt.Qt.AlignHCenter, label) painter.drawText(rect, label) painter.setRenderHint(Qt.QPainter.Antialiasing, False) painter.fillRect(self.scroll_rect, Qt.Qt.gray) for toolbar, notches in self.toolbars_notches.items(): if not toolbar.is_notches_visible(): continue for notch in notches: painter.setPen(notch.color) painter.drawLine(notch.line) painter.setPen(Qt.Qt.black) painter.drawLine(cursor_line) self.need_full_repaint = False
def _gridline_vert(self, n): return Qt.QLineF(PREV_PIXEL * n + PREV_GRID // 2, 0, PREV_PIXEL * n + PREV_GRID // 2, PREV_PIXEL * HEIGHT + PREV_GRID)
def _gridline_horz(self, n, w): return Qt.QLineF(0, PREV_PIXEL * n + PREV_GRID // 2, w, PREV_PIXEL * n + PREV_GRID // 2)