Skip to content
Snippets Groups Projects

Add scheduler GUI

Merged Oscar Gustafsson requested to merge scheduler-gui into master
8 files
+ 77
117
Compare changes
  • Side-by-side
  • Inline
Files
8
@@ -4,39 +4,18 @@
Contains the scheduler-gui GraphicsComponentItem class for drawing and maintain a component in a graph.
"""
import os
import sys
from typing import Any, Optional
from pprint import pprint
from typing import (
Any, Union, Optional, overload, final, Dict, OrderedDict, List)
# from typing_extensions import Self, Final, Literal, LiteralString, TypeAlias, final
import numpy as np
import qtpy
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets
Union, Optional, Dict, List)
# QGraphics and QPainter imports
from qtpy.QtCore import (
Qt, QObject, QRect, QRectF, QPoint, QSize, QSizeF, QByteArray, Slot, QEvent)
from qtpy.QtGui import (
QPaintEvent, QPainter, QPainterPath, QColor, QBrush, QPen, QFont, QPolygon, QIcon, QPixmap,
QLinearGradient, QTransform, QCursor)
from qtpy.QtCore import Qt, QPointF
from qtpy.QtGui import QPainterPath, QColor, QBrush, QPen, QCursor
from qtpy.QtWidgets import (
QGraphicsView, QGraphicsScene, QGraphicsWidget,
QGraphicsLayout, QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayoutItem, QGraphicsAnchorLayout,
QGraphicsItem, QGraphicsItemGroup, QGraphicsPathItem, QGraphicsRectItem,
QStyleOptionGraphicsItem, QWidget, QGraphicsObject, QGraphicsSceneMouseEvent, QGraphicsSimpleTextItem,
QGraphicsEllipseItem)
from qtpy.QtCore import (
QPoint, QPointF)
QGraphicsSimpleTextItem, QGraphicsEllipseItem)
# B-ASIC
import logger
from b_asic.schedule import Schedule
# from b_asic.graph_component import GraphComponent
from b_asic.graph_component import GraphComponent
class GraphicsComponentItem(QGraphicsItemGroup):
@@ -52,18 +31,15 @@ class GraphicsComponentItem(QGraphicsItemGroup):
_execution_time_item: QGraphicsRectItem
_label_item: QGraphicsSimpleTextItem
_port_items: List[QGraphicsEllipseItem]
def __init__(self, op_id: str, latency_offsets: Dict[str, int], execution_time: Optional[int] = None, height: float = 0.75, parent: Optional[QGraphicsItem] = None):
def __init__(self, operation: GraphComponent, height: float = 0.75, parent: Optional[QGraphicsItem] = None):
"""Constructs a GraphicsComponentItem. 'parent' is passed to QGraphicsItemGroup's constructor."""
super().__init__(parent=parent)
self._op_id = op_id
self._operation = operation
self._height = height
self._ports = {k:{'latency':float(v)} for k,v in latency_offsets.items()}
self._end_time = 0
for latency in latency_offsets.values():
self._end_time = max(self._end_time, latency)
self._execution_time = execution_time
self._ports = {k:{'latency':float(v)} for k,v in operation.latency_offsets.items()}
self._end_time = max(operation.latency_offsets.values())
self._port_items = []
self.setFlag(QGraphicsItem.ItemIsMovable) # mouse move events
@@ -71,46 +47,47 @@ class GraphicsComponentItem(QGraphicsItemGroup):
# self.setAcceptHoverEvents(True) # mouse hover events
self.setAcceptedMouseButtons(Qt.LeftButton) # accepted buttons for movements
self.setCursor(QCursor(Qt.OpenHandCursor)) # default cursor when hovering over object
self._make_component()
# def sceneEvent(self, event: QEvent) -> bool:
# print(f'Component -->\t\t\t\t{event.type()}')
# # event.accept()
# # QApplication.sendEvent(self.scene(), event)
# return True
def clear(self) -> None:
"""Sets all children's parent to 'None' and delete the axis."""
for item in self.childItems():
item.setParentItem(None)
del item
@property
def op_id(self) -> str:
"""Get the op-id."""
return self._op_id
return self._operation.graph_id
@property
def height(self) -> float:
"""Get or set the current component height. Setting the height to a new
value will update the component automatically."""
return self._height
@height.setter
def height(self, height: float) -> None:
if self._height != height:
self.clear()
self._height = height
self._make_component()
@property
def end_time(self) -> int:
"""Get the relative end time."""
return self._end_time
@property
def event_items(self) -> List[QGraphicsItem]:
"""Returnes a list of objects, that receives events."""
@@ -123,22 +100,21 @@ class GraphicsComponentItem(QGraphicsItemGroup):
pen1.setWidthF(2/self._scale)
# pen1.setCapStyle(Qt.RoundCap) # Qt.FlatCap, Qt.SquareCap (default), Qt.RoundCap
pen1.setJoinStyle(Qt.RoundJoin) # Qt.MiterJoin, Qt.BevelJoin (default), Qt.RoundJoin, Qt.SvgMiterJoin
brush2 = QBrush(Qt.black) # used by port filling
pen2 = QPen(Qt.black) # used by port outline
pen2.setWidthF(0)
# pen2.setCosmetic(True)
port_size = 7/self._scale # the diameter of an port
gray = QColor(Qt.gray)
gray.setAlpha(100) # 0-255
brush3 = QBrush(gray) # used by execution time
green = QColor(Qt.magenta)
green.setAlpha(200) # 0-255
pen3 = QPen() # used by execution time outline
pen3.setColor(green)
pen3.setWidthF(3/self._scale)
## component path
def draw_component_path(keys: List[str], revered: bool) -> None:
@@ -169,14 +145,17 @@ class GraphicsComponentItem(QGraphicsItemGroup):
input_keys = sorted(input_keys)
output_keys = [key for key in self._ports.keys() if key.lower().startswith("out")]
output_keys = sorted(output_keys, reverse=True)
# Set the starting position
x = self._ports[input_keys[0]]['latency']
if input_keys:
x = self._ports[input_keys[0]]['latency']
else:
x = 0
y = 0
old_x = x
old_y = y
component_path = QPainterPath(QPointF(x, y)) # starting point
# draw the path
draw_component_path(input_keys, False) # draw input side
draw_component_path(output_keys, True) # draw ouput side
@@ -198,22 +177,22 @@ class GraphicsComponentItem(QGraphicsItemGroup):
self._port_items[-1].setPos(port_pos.x(), port_pos.y())
## op-id/label
self._label_item = QGraphicsSimpleTextItem(self._op_id)
self._label_item = QGraphicsSimpleTextItem(self._operation.graph_id)
self._label_item.setScale(self._label_item.scale() / self._scale)
center = self._component_item.boundingRect().center()
center -= self._label_item.boundingRect().center() / self._scale
self._label_item.setPos(self._component_item.pos() + center)
## execution time
if self._execution_time:
self._execution_time_item = QGraphicsRectItem(0, 0, self._execution_time, self._height)
if self._operation.execution_time:
self._execution_time_item = QGraphicsRectItem(0, 0, self._operation.execution_time, self._height)
self._execution_time_item.setPen(pen3)
# self._execution_time_item.setBrush(brush3)
## item group, consist of component_item, port_items and execution_time_item
self.addToGroup(self._component_item)
for port in self._port_items:
self.addToGroup(port)
self.addToGroup(self._label_item)
if self._execution_time:
if self._operation.execution_time:
self.addToGroup(self._execution_time_item)
Loading