From c77555aa7b6207c7854b5eba68f6d8160faef274 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson <oscar.gustafsson@gmail.com> Date: Fri, 2 Sep 2022 16:10:41 +0200 Subject: [PATCH] Add connections in schedule --- b_asic/operation.py | 1 - .../scheduler-gui/graphics_component_item.py | 15 ++++++--- b_asic/scheduler-gui/graphics_graph_event.py | 2 ++ b_asic/scheduler-gui/graphics_graph_item.py | 32 ++++++++++++++++--- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/b_asic/operation.py b/b_asic/operation.py index 92a20a36..588fabd8 100644 --- a/b_asic/operation.py +++ b/b_asic/operation.py @@ -7,7 +7,6 @@ from b_asic.signal import Signal from b_asic.port import SignalSourceProvider, InputPort, OutputPort from b_asic.graph_component import GraphComponent, AbstractGraphComponent, Name import itertools as it -from math import trunc import collections from abc import abstractmethod diff --git a/b_asic/scheduler-gui/graphics_component_item.py b/b_asic/scheduler-gui/graphics_component_item.py index 81bc4a9d..842a970a 100644 --- a/b_asic/scheduler-gui/graphics_component_item.py +++ b/b_asic/scheduler-gui/graphics_component_item.py @@ -68,6 +68,11 @@ class GraphicsComponentItem(QGraphicsItemGroup): """Get the op-id.""" return self._operation.graph_id + @property + def operation(self) -> GraphComponent: + """Get the operation.""" + return self._operation + @property def height(self) -> float: """Get or set the current component height. Setting the height to a new @@ -86,12 +91,14 @@ class GraphicsComponentItem(QGraphicsItemGroup): """Get the relative end time.""" return self._end_time - @property def event_items(self) -> List[QGraphicsItem]: """Returnes a list of objects, that receives events.""" return [self] + def get_port_location(self, key) -> QPointF: + return self.mapToParent(self._ports[key]['pos']) + def _make_component(self) -> None: """Makes a new component out of the stored attributes.""" brush1 = QBrush(Qt.lightGray) # used by component filling @@ -170,10 +177,10 @@ class GraphicsComponentItem(QGraphicsItemGroup): for port_dict in self._ports.values(): port_pos = self.mapToParent(port_dict['pos']) port = QGraphicsEllipseItem(-port_size/2, -port_size/2, port_size, port_size) # center of circle is in origo + port.setPen(pen2) + port.setBrush(brush2) + port.setPos(port_pos.x(), port_pos.y()) self._port_items.append(port) - self._port_items[-1].setPen(pen2) - self._port_items[-1].setBrush(brush2) - self._port_items[-1].setPos(port_pos.x(), port_pos.y()) ## op-id/label self._label_item = QGraphicsSimpleTextItem(self._operation.graph_id) diff --git a/b_asic/scheduler-gui/graphics_graph_event.py b/b_asic/scheduler-gui/graphics_graph_event.py index f277fdc4..8d5832a5 100644 --- a/b_asic/scheduler-gui/graphics_graph_event.py +++ b/b_asic/scheduler-gui/graphics_graph_event.py @@ -160,12 +160,14 @@ class GraphicsGraphEvent: # PyQt5 # self.prepareGeometryChange() item.setX(pos) self._current_pos.setX(self._current_pos.x() + 1.0) + self._redraw_lines(item) elif dx < -0.505: pos = item.x() - 1.0 if self.is_component_valid_pos(item, pos): # self.prepareGeometryChange() item.setX(pos) self._current_pos.setX(self._current_pos.x() - 1.0) + self._redraw_lines(item) def comp_mousePressEvent(self, event: QGraphicsSceneMouseEvent) -> None: """Changes the cursor to ClosedHandCursor when grabbing an object and diff --git a/b_asic/scheduler-gui/graphics_graph_item.py b/b_asic/scheduler-gui/graphics_graph_item.py index 173aa8bf..9124d452 100644 --- a/b_asic/scheduler-gui/graphics_graph_item.py +++ b/b_asic/scheduler-gui/graphics_graph_item.py @@ -5,19 +5,22 @@ Contains the scheduler-gui GraphicsGraphItem class for drawing and maintain a component in a graph. """ +from collections import defaultdict from math import floor from pprint import pprint -from typing import Optional, List +from typing import Optional, List, Dict, Set # QGraphics and QPainter imports from qtpy.QtWidgets import QGraphicsItem, QGraphicsItemGroup +from qtpy.QtGui import QPen +from qtpy.QtCore import Qt # B-ASIC from b_asic.schedule import Schedule -from b_asic.special_operations import Input, Output from graphics_component_item import GraphicsComponentItem from graphics_axes_item import GraphicsAxesItem from graphics_graph_event import GraphicsGraphEvent +from graphics_signal import GraphicsSignal class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / PyQt5 @@ -33,6 +36,7 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / _components_height: float _x_axis_indent: float _event_items: List[QGraphicsItem] + _signal_dict: Dict[GraphicsComponentItem, Set[GraphicsSignal]] def __init__(self, schedule: Schedule, parent: Optional[QGraphicsItem] = None): @@ -50,7 +54,7 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / self._components_height = 0.0 self._x_axis_indent = 0.2 self._event_items = [] - + self._signal_dict = defaultdict(set) self._make_graph() def clear(self) -> None: @@ -82,6 +86,10 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / return True + def _redraw_lines(self, item: GraphicsComponentItem): + for signal in self._signal_dict[item]: + signal.update_path() + def set_new_starttime(self, item: GraphicsComponentItem, pos: float) -> None: op_start_time = self.schedule.start_time_of_operation(item.op_id) new_start_time = floor(pos) - floor(self._x_axis_indent) @@ -89,7 +97,6 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / if move_time: self.schedule.move_operation(item.op_id, move_time) - def is_valid_delta_time(self, delta_time: int) -> bool: """Takes in a delta time and returns true if the new schedule time is valid, false otherwise.""" @@ -127,6 +134,7 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / """Makes a new graph out of the stored attributes.""" # build components spacing = 0.2 + _components_dict = {} # print('Start times:') for op_id, op_start_time in self.schedule.start_times.items(): op = self.schedule.sfg.find_by_id(op_id) @@ -136,6 +144,7 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / component = GraphicsComponentItem(op) component.setPos(self._x_axis_indent + op_start_time, self._components_height) self._components.append(component) + _components_dict[op] = component self._components_height += component.height self._event_items += component.event_items # self._components_height += spacing @@ -154,4 +163,19 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / self.addToGroup(component) # self.addToGroup(self._components) + + pen1 = QPen(Qt.black) # used by component outline + pen1.setWidthF(0.03) + + # add signals + for component in self._components: + op = component.operation + for i, output_port in enumerate(op.outputs): + for signal in output_port.signals: + dest_component = _components_dict[signal.destination.operation] + gs = GraphicsSignal(component, dest_component, signal, pen=pen1) + self.addToGroup(gs) + self._signal_dict[component].add(gs) + self._signal_dict[dest_component].add(gs) + pprint(GraphicsGraphItem.__mro__) -- GitLab