Skip to content
Snippets Groups Projects
timeline_item.py 3.82 KiB
Newer Older
  • Learn to ignore specific revisions
  • Andreas Bolin's avatar
    Andreas Bolin committed
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    """
    
    B-ASIC Scheduler-GUI Timeline Item Module.
    
    Andreas Bolin's avatar
    Andreas Bolin committed
    
    
    Contains the scheduler_gui TimelineItem class for drawing and
    maintain the timeline in a schedule.
    
    Andreas Bolin's avatar
    Andreas Bolin committed
    """
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
    from typing import List, Optional, overload
    
    Andreas Bolin's avatar
    Andreas Bolin committed
    
    # QGraphics and QPainter imports
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
    from qtpy.QtCore import QLineF, Qt
    from qtpy.QtGui import QCursor
    
    Andreas Bolin's avatar
    Andreas Bolin committed
    from qtpy.QtWidgets import QGraphicsItem, QGraphicsLineItem, QGraphicsTextItem
    
    
    
    class TimelineItem(QGraphicsLineItem):
        """A class to represent the timeline in AxesItem."""
    
    Andreas Bolin's avatar
    Andreas Bolin committed
    
        # _scale:             float
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
        _delta_time_label: QGraphicsTextItem
    
    Andreas Bolin's avatar
    Andreas Bolin committed
    
        @overload
    
        def __init__(self, line: QLineF, parent: Optional[QGraphicsItem] = None) -> None:
    
    Andreas Bolin's avatar
    Andreas Bolin committed
            """
    
            Constructs a TimelineItem out of 'line'. 'parent' is passed to
    
    Andreas Bolin's avatar
    Andreas Bolin committed
            QGraphicsLineItem's constructor.
            """
    
        @overload
        def __init__(self, parent: Optional[QGraphicsItem] = None) -> None:
    
            """Constructs a TimelineItem. 'parent' is passed to
    
    Andreas Bolin's avatar
    Andreas Bolin committed
            QGraphicsLineItem's constructor."""
    
        @overload
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
        def __init__(
            self,
            x1: float,
            y1: float,
            x2: float,
            y2: float,
            parent: Optional[QGraphicsItem] = None,
        ) -> None:
    
    Andreas Bolin's avatar
    Andreas Bolin committed
            """
    
            Constructs a TimelineItem from (x1, y1) to (x2, y2). 'parent'
    
    Andreas Bolin's avatar
    Andreas Bolin committed
            is passed to QGraphicsLineItem's constructor.
            """
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            self.setFlag(QGraphicsItem.ItemIsMovable)  # mouse move events
    
    Andreas Bolin's avatar
    Andreas Bolin committed
            # self.setAcceptHoverEvents(True)                 # mouse hover events
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            self.setAcceptedMouseButtons(
    
                Qt.MouseButton.LeftButton
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            )  # accepted buttons for movements
            self.setCursor(
    
                QCursor(Qt.CursorShape.SizeHorCursor)
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            )  # default cursor when hovering over object
    
    Andreas Bolin's avatar
    Andreas Bolin committed
    
            self._delta_time_label = QGraphicsTextItem()
            self._delta_time_label.hide()
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            self._delta_time_label.setScale(1.05 / 75)  # TODO: dont hardcode scale
    
    Andreas Bolin's avatar
    Andreas Bolin committed
            self._delta_time_label.setParentItem(self)
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            x_pos = (
                -self._delta_time_label.mapRectToParent(
                    self._delta_time_label.boundingRect()
                ).width()
                / 2
            )
    
    Andreas Bolin's avatar
    Andreas Bolin committed
            y_pos = 0.5
            self._delta_time_label.setPos(x_pos, y_pos)
            # pen = QPen(Qt.black)
            # self._delta_time_label.setPen(pen)
    
        # @property
        # def label(self) -> None:
        #     return self._delta_time_label
    
        def set_text(self, number: int) -> None:
            """Set the label text to 'number'."""
            # self.prepareGeometryChange()
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            self._delta_time_label.setPlainText(f"( {number:+} )")
            self._delta_time_label.setX(
                -self._delta_time_label.mapRectToParent(
                    self._delta_time_label.boundingRect()
                ).width()
                / 2
            )
    
    Andreas Bolin's avatar
    Andreas Bolin committed
    
        # def set_text_pen(self, pen: QPen) -> None:
        #     """Set the label pen to 'pen'."""
        #     self._delta_time_label.setPen(pen)
    
        # def set_label_visible(self, visible: bool) -> None:
        #     """If visible is True, the item is made visible. Otherwise, the item is
        #     made invisible"""
        #     self._delta_time_label.setVisible(visible)
    
        def show_label(self) -> None:
            """Show the label (label are not visible by default). This convenience
            function is equivalent to calling set_label_visible(True)."""
            self._delta_time_label.show()
    
        def hide_label(self) -> None:
            """Hide the label (label are not visible by default). This convenience
            function is equivalent to calling set_label_visible(False)."""
            self._delta_time_label.hide()
    
        def set_text_scale(self, scale: float) -> None:
            self._delta_time_label.setScale(scale)
    
        @property
        def event_items(self) -> List[QGraphicsItem]:
    
            """Return a list of objects that receives events."""
    
    Andreas Bolin's avatar
    Andreas Bolin committed
            return [self]