From 4c5f45f03430e7a54443166aef339aa8d2097850 Mon Sep 17 00:00:00 2001
From: Andreas Bolin <andbo467@student.liu.se>
Date: Tue, 26 Jul 2022 17:42:44 +0200
Subject: [PATCH] workspace dump

---
 b_asic/scheduler-gui/graphics_axis_item.py    |  2 +-
 .../scheduler-gui/graphics_component_item.py  | 15 +++++-
 b_asic/scheduler-gui/graphics_graph_item.py   | 34 ++++++++----
 b_asic/scheduler-gui/main_window.py           | 54 ++++++++++---------
 b_asic/scheduler-gui/main_window.ui           |  4 +-
 5 files changed, 70 insertions(+), 39 deletions(-)

diff --git a/b_asic/scheduler-gui/graphics_axis_item.py b/b_asic/scheduler-gui/graphics_axis_item.py
index bacfe5f8..86c49266 100644
--- a/b_asic/scheduler-gui/graphics_axis_item.py
+++ b/b_asic/scheduler-gui/graphics_axis_item.py
@@ -55,7 +55,7 @@ class GraphicsAxisItem(QGraphicsItemGroup):
         # self.setFlag(QGraphicsItem.ItemIsSelectable)
         # self.setAcceptHoverEvents(True)
 
-        self.update_(width, height, x_indent)
+        self.update_(width*10.0 + 6, height, x_indent)
     
 
     @property
diff --git a/b_asic/scheduler-gui/graphics_component_item.py b/b_asic/scheduler-gui/graphics_component_item.py
index c42cd603..c0a41aeb 100644
--- a/b_asic/scheduler-gui/graphics_component_item.py
+++ b/b_asic/scheduler-gui/graphics_component_item.py
@@ -24,7 +24,7 @@ from qtpy.QtWidgets import (
     QGraphicsView, QGraphicsScene, QGraphicsWidget,
     QGraphicsLayout, QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayoutItem, QGraphicsAnchorLayout,
     QGraphicsItem, QGraphicsItemGroup, QGraphicsPathItem, QGraphicsRectItem,
-    QStyleOptionGraphicsItem, QWidget, QGraphicsObject, QGraphicsSceneMouseEvent)
+    QStyleOptionGraphicsItem, QWidget, QGraphicsObject, QGraphicsSceneMouseEvent, QGraphicsSimpleTextItem)
 from qtpy.QtCore    import (
     QPoint, QPointF)
 
@@ -37,15 +37,18 @@ from graphics_component_event import GraphicsComponentEvent
 class GraphicsComponentItem(QGraphicsItemGroup):
     
     _scale:                 float = 1.0 # static, changed from MainWindow
+    _op_id:                 str
     _height:                float
     _component_item:        QGraphicsPathItem
     _execution_time_item:   QGraphicsPathItem
+    _text_item:             QGraphicsSimpleTextItem
     _item_group:            QGraphicsItemGroup
     
     
-    def __init__(self, height: float = 10.0, parent: QGraphicsItem = None):
+    def __init__(self, op_id: str, height: float = 10.0, parent: QGraphicsItem = None):
         super().__init__(parent)
 
+        self._op_id = op_id
         self._height = height
         self._component_item = QGraphicsPathItem()
         self._item_group = QGraphicsItemGroup()
@@ -93,6 +96,13 @@ class GraphicsComponentItem(QGraphicsItemGroup):
         self._component_item.setPen(pen)
         self._component_item.setBrush(brush)
         self._component_item.setPos(10, 0)      # in parent (i.e. self) coordinates
+
+        # op-id/label
+        label = QGraphicsSimpleTextItem(self._op_id)
+        label.setScale(label.scale() / self._scale)
+        center = self._component_item.boundingRect().center()
+        center -= label.boundingRect().center() / self._scale
+        label.setPos(self._component_item.pos() + center)
         
         # execution time square
         execution_time_path = QPainterPath(QPoint(0,0))
@@ -107,6 +117,7 @@ class GraphicsComponentItem(QGraphicsItemGroup):
         
         # item group, consist of time_item and component_item
         self.addToGroup(self._component_item)
+        self.addToGroup(label)
         self.addToGroup(self._execution_time_item)
 
     # move: https://evileg.com/en/post/86/
diff --git a/b_asic/scheduler-gui/graphics_graph_item.py b/b_asic/scheduler-gui/graphics_graph_item.py
index c9a95ed0..2096e4b3 100644
--- a/b_asic/scheduler-gui/graphics_graph_item.py
+++ b/b_asic/scheduler-gui/graphics_graph_item.py
@@ -34,6 +34,8 @@ from qtpy.QtCore    import (
 # B-ASIC
 import logger
 from b_asic.schedule            import Schedule
+from b_asic.graph_component     import GraphComponent
+from b_asic.special_operations  import Input, Output
 from graphics_component_item    import GraphicsComponentItem
 from graphics_axis_item         import GraphicsAxisItem
 from graphics_graph_event       import GraphicsGraphEvent
@@ -72,21 +74,33 @@ class GraphicsGraphItem(QGraphicsItemGroup, GraphicsGraphEvent):
         self._components_height = 0.0
         self._x_axis_indent = 2.0
 
+
         # build components
         spacing = 2.0
-        for i in range(5):
-            self._components_height += spacing
-            component = GraphicsComponentItem()
-            component.setPos(self._x_axis_indent, self._components_height)
-            self._components.append(component)
-            # self._components.addToGroup(component)
-            self._components_height += component.height
-        # self._components.setPos(self._x_axis_indent, spacing)
+        # print('Start times:')
+        for op_id, op_start_time in self._schedule._start_times.items():
+            op = self._schedule._sfg.find_by_id(op_id)
+            print(f'type: {type(op).__name__:<24}', end='\t')
+            print(op)
+            # slacks = self._schedule.slacks(op_id)
+            # print(f'{op_id:<5} {slacks}')
+            # op.latency_offsets -> [[int]]
+            # op.latency() -> int (max of all ports)
+            latency = op.latency_offsets
+            print(f'latency: {latency}')
+            if not isinstance(op, (Input, Output)):
+                self._components_height += spacing
+                component = GraphicsComponentItem(op_id)
+                component.setPos(self._x_axis_indent + op_start_time*10, self._components_height)
+                self._components.append(component)
+                self._components_height += component.height
         self._components_height += spacing
 
         # build axis
-        self._axis = GraphicsAxisItem(50 + 6, self._components_height, self._x_axis_indent)
-        
+        schedule_time: int = 0
+        schedule_time = self._schedule.schedule_time
+        self._axis = GraphicsAxisItem(schedule_time, self._components_height, self._x_axis_indent)
+
         # add axis and components
         self.addToGroup(self._axis)
         for component in self._components:
diff --git a/b_asic/scheduler-gui/main_window.py b/b_asic/scheduler-gui/main_window.py
index 07b5a1ff..572a9004 100644
--- a/b_asic/scheduler-gui/main_window.py
+++ b/b_asic/scheduler-gui/main_window.py
@@ -117,7 +117,6 @@ class MainWindow(QMainWindow, Ui_MainWindow):
     """Schedule of an SFG with scheduled Operations."""
     _scene: QGraphicsScene
     _graph: GraphicsGraphItem
-    _open_file_dialog_opened: bool
     _scale: float
     _debug_rects: QGraphicsItemGroup
 
@@ -127,7 +126,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         super().__init__()
         self._graph = None
         self._open_file_dialog_opened = False
-        self._scale = 10.0
+        self._scale = 7.5
         self._debug_rects = None
         
         QIcon.setThemeName('breeze')
@@ -151,7 +150,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         self.menu_node_info     .triggered      .connect(self.toggle_component_info)
         self.menu_exit_dialog   .triggered      .connect(self.toggle_exit_dialog)
         self.actionT            .triggered      .connect(self.actionTbtn)
-        self.splitter_center    .splitterMoved  .connect(self._splitter_center_moved)
+        self.splitter           .splitterMoved  .connect(self._splitter_moved)
 
         # Setup event member functions
         self.closeEvent = self._close_event
@@ -168,17 +167,16 @@ class MainWindow(QMainWindow, Ui_MainWindow):
             self.info_table.setItem(i,1,item)
 
         # Init central-widget splitter
-        self.splitter_center.setStretchFactor(0, 1)
-        self.splitter_center.setStretchFactor(1, 0)
-        self.splitter_center.setCollapsible(0, False)
-        self.splitter_center.setCollapsible(1, True)
+        self.splitter.setStretchFactor(0, 1)
+        self.splitter.setStretchFactor(1, 0)
+        self.splitter.setCollapsible(0, False)
+        self.splitter.setCollapsible(1, True)
 
     def _init_graphics(self) -> None:
         """Initialize the QGraphics framework"""
         self._scene = QGraphicsScene()
         self.view.setScene(self._scene)
         self.view.scale(self._scale, self._scale)
-        # self.setMouseTracking(True)
         GraphicsComponentItem._scale = self._scale
         GraphicsAxisItem._scale = self._scale
         self._scene.changed.connect(self.shrink_scene_to_min_size)
@@ -196,13 +194,19 @@ class MainWindow(QMainWindow, Ui_MainWindow):
     
     @Slot()
     def _load_schedule_from_pyfile(self) -> None:
-        open_dir = QStandardPaths.standardLocations(QStandardPaths.HomeLocation)[0] if not self._open_file_dialog_opened else ''
-        abs_path_filename = QFileDialog.getOpenFileName(self,
+        settings = QSettings()
+        # open_dir = QStandardPaths.standardLocations(QStandardPaths.HomeLocation)[0] if not self._open_file_dialog_opened else ''
+        last_file = settings.value('mainwindow/last_opened_file', QStandardPaths.standardLocations(QStandardPaths.HomeLocation)[0], str)
+        if not os.path.exists(last_file):               # if filename does not exist
+            last_file = os.path.dirname(last_file) + '/'
+            if not os.path.exists(last_file):           # if path does not exist
+                last_file = QStandardPaths.standardLocations(QStandardPaths.HomeLocation)[0]
+
+        abs_path_filename, _ = QFileDialog.getOpenFileName(self,
                                                         self.tr("Open python file"),
-                                                        open_dir,
+                                                        last_file,
                                                         self.tr("Python Files (*.py *.py3)"))
-        abs_path_filename = abs_path_filename[0]
-
+        
         if not abs_path_filename:       # return if empty filename (QFileDialog was canceled)
             return
         log.debug('abs_path_filename = {}.'.format(abs_path_filename))
@@ -244,6 +248,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         
         self.open(schedule_obj_list[ret_tuple[0]])
         del module
+        settings.setValue("mainwindow/last_opened_file", abs_path_filename)
+
         
     
     #@Slot()
@@ -298,14 +304,14 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         """This method toggles the right hand side info window."""
         # Note: splitter handler index 0 is a hidden splitter handle far most left, use index 1
         settings = QSettings()
-        range = self.splitter_center.getRange(1)    # tuple(min, max)
+        range = self.splitter.getRange(1)    # tuple(min, max)
         
         if checked:
-            self.splitter_center.restoreState(settings.value("mainwindow/splitter_center/last_state"))
-            # self.splitter_center.restoreState(settings.value("splitterSizes"))
+            self.splitter.restoreState(settings.value("mainwindow/splitter/last_state"))
+            # self.splitter.restoreState(settings.value("splitterSizes"))
         else:
-            settings.setValue("mainwindow/splitter_center/last_state", self.splitter_center.saveState())
-            self.splitter_center.moveSplitter(range[1], 1)
+            settings.setValue("mainwindow/splitter/last_state", self.splitter.saveState())
+            self.splitter.moveSplitter(range[1], 1)
             
     @Slot(bool)
     def toggle_exit_dialog(self, checked: bool) -> None:
@@ -313,18 +319,18 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         s.setValue("mainwindow/hide_exit_dialog", checked)
 
     @Slot(int, int)
-    def _splitter_center_moved(self, pos: int, index: int) -> None:
+    def _splitter_moved(self, pos: int, index: int) -> None:
         """Callback method used to check if the right widget (info window) 
         has collapsed. Update the checkbutton accordingly."""
         # TODO: Custom move handler, save state on click-release?
-        widths: list[int, int] = list(self.splitter_center.sizes())
+        widths: list[int, int] = list(self.splitter.sizes())
         
         if widths[1] == 0:
             self.menu_node_info.setChecked(False)
         else:
             self.menu_node_info.setChecked(True)
         
-    @Slot(list)
+    @Slot('QList<QRectF>')
     def shrink_scene_to_min_size(self, region: list[QRectF]) -> None:
         self._scene.setSceneRect(self._scene.itemsBoundingRect())
     
@@ -387,7 +393,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         s.setValue('mainwindow/size',           self.size())            # window: size
         s.setValue('mainwindow/state',          self.saveState())       # toolbars, dockwidgets: pos, size
         s.setValue('mainwindow/menu/node_info', self.menu_node_info.isChecked())
-        s.setValue('mainwindow/splitter/state', self.splitter_center.saveState())
+        s.setValue('mainwindow/splitter/state', self.splitter.saveState())
 
         if s.isWritable():
             log.debug('Settings written to \'{}\'.'.format(s.fileName()))
@@ -404,8 +410,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
             self.resize(                    s.value('mainwindow/size', self.size()))
         self.restoreState(                  s.value('mainwindow/state', QByteArray()))
         self.menu_node_info.setChecked(     s.value('mainwindow/menu/node_info', True, bool))
-        self.splitter_center.restoreState(  s.value('mainwindow/splitter/state', QByteArray()))
-        self.menu_exit_dialog.setChecked(s.value('mainwindow/hide_exit_dialog', False, bool))
+        self.splitter.restoreState(         s.value('mainwindow/splitter/state', QByteArray()))
+        self.menu_exit_dialog.setChecked(   s.value('mainwindow/hide_exit_dialog', False, bool))
 
         log.debug('Settings read from \'{}\'.'.format(s.fileName()))
 
diff --git a/b_asic/scheduler-gui/main_window.ui b/b_asic/scheduler-gui/main_window.ui
index 7844ba58..650e75c7 100644
--- a/b_asic/scheduler-gui/main_window.ui
+++ b/b_asic/scheduler-gui/main_window.ui
@@ -45,7 +45,7 @@
      <number>0</number>
     </property>
     <item>
-     <widget class="QSplitter" name="splitter_center">
+     <widget class="QSplitter" name="splitter">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
@@ -95,7 +95,7 @@
      <x>0</x>
      <y>0</y>
      <width>800</width>
-     <height>20</height>
+     <height>22</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
-- 
GitLab