From 5c07b4685d21e7c15366c37cd8ad20bf67c5f9a1 Mon Sep 17 00:00:00 2001
From: Andreas Bolin <andbo467@student.liu.se>
Date: Tue, 19 Jul 2022 17:25:04 +0200
Subject: [PATCH] workspace dump

---
 b_asic/scheduler-gui/component_item.py | 69 ++++++++++++++++++++-----
 b_asic/scheduler-gui/graphics_scene.py | 13 ++---
 b_asic/scheduler-gui/main_window.py    | 71 ++++++++++++++++++++------
 3 files changed, 119 insertions(+), 34 deletions(-)

diff --git a/b_asic/scheduler-gui/component_item.py b/b_asic/scheduler-gui/component_item.py
index a2f55768..2159c12f 100644
--- a/b_asic/scheduler-gui/component_item.py
+++ b/b_asic/scheduler-gui/component_item.py
@@ -1,6 +1,6 @@
 import os
 import sys
-from typing             import Any
+from typing             import Any, Optional
 from pprint             import pprint
 from typing import Any, AnyStr, Generic, Protocol, TypeVar, Union, Optional, overload, Final, final
 # from typing_extensions import Self, Final, Literal, LiteralString, TypeAlias, final
@@ -20,7 +20,8 @@ from qtpy.QtGui     import (
 from qtpy.QtWidgets import (
     QGraphicsView, QGraphicsScene, QGraphicsWidget,
     QGraphicsLayout, QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayoutItem, QGraphicsAnchorLayout,
-    QGraphicsItem, QGraphicsItemGroup, QGraphicsPathItem)
+    QGraphicsItem, QGraphicsItemGroup, QGraphicsPathItem,
+    QStyleOptionGraphicsItem, QWidget)
 from qtpy.QtCore    import (
     QPoint, QPointF)
 
@@ -29,20 +30,36 @@ import logger
 from b_asic.schedule    import Schedule
 
 
-class ComponentItem(QGraphicsItemGroup, QGraphicsLayoutItem):
+# class ComponentItem(QGraphicsItemGroup, QGraphicsLayoutItem):
+class ComponentItem(QGraphicsLayoutItem, QGraphicsItem):
+# class ComponentItem(QGraphicsLayoutItem, QGraphicsItem):
     
     _scale: float
     _component_item: QGraphicsPathItem
     _execution_time_item: QGraphicsPathItem
+    _item_group: QGraphicsItemGroup
     
     
     # def __init__(self, scale: float, *args, **kwargs):
-    def __init__(self, scale: float, parent = None):
-        super().__init__(parent=parent)
+    def __init__(self, scale: float, *args, **kwargs):
+    # def __init__(self, scale: float):
+        super().__init__(*args, **kwargs)
+        # QGraphicsLayoutItem.__init__()
+        # QGraphicsItem.__init__(parent = parent)
         self._scale = scale
+        self.setGraphicsItem(self)
+        print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
+        self._item_group = QGraphicsItemGroup()
+        # self._item_group.setParentItem(self)
+        # print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
+        self._populate()
         
+        # print(self.boundingRect().size())
+
         
-        brush = QBrush(Qt.lightGray, bs=Qt.SolidPattern)
+    def _populate(self):
+        # brush = QBrush(Qt.lightGray, bs=Qt.SolidPattern)
+        brush = QBrush(Qt.lightGray)
         pen = QPen(Qt.SolidLine)
         pen.setWidthF(2/self._scale)
         pen.setBrush(Qt.darkGray)
@@ -75,16 +92,34 @@ class ComponentItem(QGraphicsItemGroup, QGraphicsLayoutItem):
         
         # item group, consist of time_item and component_item
         # item_group = QGraphicsItemGroup()
+        
+        # graphics_item = self.graphicsItem()
+        # print(graphics_item)
+        # self._item_group = graphics_item.childItems()[0]
+        # print(self._item_group)
         # item_group.setScale(self._scale)
-        self.addToGroup(self._component_item)
-        self.addToGroup(self._execution_time_item)
+        self._item_group.addToGroup(self._component_item)
+        self._item_group.addToGroup(self._execution_time_item)
+
+        # self.setGraphicsItem(self)
+        # QGraphicsItemGroup
+        # self.setGroup(item_group)
+        print('Populated!')
 
         
     
     
-    # # QGraphicsLayoutItem virtual functions
-    # def setGeometry(rect): ...
+    # reimplement QGraphicsLayoutItem virtual functions
+    def setGeometry(rect): 
+        print(f'setGeometry({rect})')
+    
     def sizeHint(self, which: Qt.SizeHint, constraint: QSizeF = QSizeF()) -> QSizeF:
+        print(f'sizeHint(which={which}, constraint={constraint}')
+        # return QSizeF(1000, 100)
+    #     if self.isEmpty():
+    #         pass
+
+        # item = self.graphicsItem()
         switch = {
             Qt.MinimumSize:     self.boundingRect().size(),
             Qt.PreferredSize:   self.boundingRect().size(),
@@ -93,6 +128,14 @@ class ComponentItem(QGraphicsItemGroup, QGraphicsLayoutItem):
         return switch.get(which, constraint)
 
     
-    # # QGraphicsItem virtual functions
-    # def boundingRect(): ...
-    # def paint(): ...
\ No newline at end of file
+    # reimplement QGraphicsItem virtual functions
+    def boundingRect(self) -> QRectF:
+        print('boundingRect()')
+        return self._item_group.boundingRect()
+    
+    def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: Optional[QWidget]= None) -> None:
+        print(f'paint(painter={painter}, option={option}, widget={widget})')
+        return self._item_group.paint(painter, option, widget)
+
+# print("MRO:")
+# pprint(ComponentItem.__mro__)
\ No newline at end of file
diff --git a/b_asic/scheduler-gui/graphics_scene.py b/b_asic/scheduler-gui/graphics_scene.py
index 53a9bd51..42ed7002 100644
--- a/b_asic/scheduler-gui/graphics_scene.py
+++ b/b_asic/scheduler-gui/graphics_scene.py
@@ -11,6 +11,7 @@ from typing             import Any
 from pprint             import pprint
 from typing import Any, AnyStr, Generic, Protocol, TypeVar, Union, Optional, overload, Final, final
 # from typing_extensions import Self, Final, Literal, LiteralString, TypeAlias, final
+from copy               import deepcopy
 import numpy as np
 
 import qtpy
@@ -54,14 +55,14 @@ class GraphicsScene(QGraphicsScene):
         
         self._id = id
         if isinstance(schedule, Schedule):
-            self._schedule = schedule
+            self._schedule = deepcopy(schedule)
             self._scale = 10.0
             
-            # print('From GraphicsScene/schedule:\t\t', end='')
-            # pprint(schedule)
-            # print('From GraphicsScene/self._schedule:\t', end='')
-            # pprint(self._schedule)
-            # print('')
+            print('From GraphicsScene/schedule:\t\t', end='')
+            pprint(schedule)
+            print('From GraphicsScene/self._schedule:\t', end='')
+            pprint(self._schedule)
+            print('')
             
             
             # print('########### _start_times:')
diff --git a/b_asic/scheduler-gui/main_window.py b/b_asic/scheduler-gui/main_window.py
index 99279bec..52c897e2 100644
--- a/b_asic/scheduler-gui/main_window.py
+++ b/b_asic/scheduler-gui/main_window.py
@@ -9,6 +9,7 @@ Start main-window with start_gui().
 
 
 
+
 import os
 import sys
 from pathlib        import Path
@@ -20,6 +21,7 @@ from pprint         import pprint
 from importlib.machinery import SourceFileLoader
 import inspect
 
+
 # Qt/qtpy
 import qtpy
 from qtpy           import uic, QtCore, QtGui, QtWidgets
@@ -44,6 +46,7 @@ from qtpy.QtWidgets import (
 import logger
 from b_asic.schedule    import Schedule
 from graphics_scene     import GraphicsScene
+from component_item     import ComponentItem
 
 
 log = logger.getLogger()
@@ -107,6 +110,9 @@ class MainWindow(QMainWindow, Ui_MainWindow):
     """Schedule of an SFG with scheduled Operations."""
     # _schedules: dict
     _scenes: dict[str, GraphicsScene]
+    _scene: QGraphicsScene
+    _diagrams: dict[str, QGraphicsWidget]
+    _diagram_count: int
     _scene_count: int
     _open_file_dialog_opened: bool
     _scale: float
@@ -115,7 +121,9 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         """Initialize Schedule-gui."""
         super().__init__()
         self._scenes = {}
+        self._diagrams = {}
         self._scene_count = 0
+        self._diagram_count = 0
         self._open_file_dialog_opened = False
         self._scale = 10.0
         
@@ -133,7 +141,6 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         self.setupUi(self)
         
         # Connect signals to slots
-        # self.pushbutton_add     .clicked        .connect(self.callback_pushButton)
         self.menu_load_from_file.triggered      .connect(self._load_schedule_from_pyfile)
         self.menu_save          .triggered      .connect(self.save)
         self.menu_save_as       .triggered      .connect(self.save_as)
@@ -149,8 +156,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         # Setup info table
         self.info_table.setHorizontalHeaderLabels(['Property','Value'])
         # test = '#b085b2'
-        # self.info_table.setStyleSheet('alternate-background-color: lightGray;background-color: white;');
-        self.info_table.setStyleSheet('alternate-background-color: #fadefb;background-color: #ebebeb;');
+        # self.info_table.setStyleSheet('alternate-background-color: lightGray;background-color: white;')
+        self.info_table.setStyleSheet('alternate-background-color: #fadefb;background-color: #ebebeb;')
         for i in range(10):
             self.info_table.insertRow(i)
             item = QTableWidgetItem('this is a very very very very long string that says abolutly nothing')
@@ -171,6 +178,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         # self.graphic_view.setGeometry(20, 20, self.width(), self.height())
         # self.graphic_view.setDragMode(QGraphicsView.RubberBandDrag)
         self.graphics_view.scale(self._scale, self._scale)
+        self._scene = QGraphicsScene()
+        self.graphics_view.setScene(self._scene)
         
         
 
@@ -222,6 +231,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
                                 .format(os.path.basename(abs_path_filename)))
             log.info('Could not find any Schedule object in file \'{}\'.'
                      .format(os.path.basename(abs_path_filename)))
+            del module
             return
         
         ret_tuple = QInputDialog.getItem(self,
@@ -232,9 +242,11 @@ class MainWindow(QMainWindow, Ui_MainWindow):
 
         if not ret_tuple[1]:                  # User canceled the operation
             log.debug('Load schedule operation: user canceled')
+            del module
             return
         
         self.open(schedule_obj_list[ret_tuple[0]])
+        del module
         
     
     #@Slot()
@@ -245,19 +257,48 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         #TODO: Unique hash keys
         #TODO: self.open(schedule_obj_list[ret_tuple[0])
         
-        scene = GraphicsScene(self._scene_count, schedule, self.graphics_view)
-        #scene = QGraphicsScene()
-        self._scenes[self._scene_count] = scene
-        self.graphics_view.setScene(scene)
+        # scene = GraphicsScene(self._scene_count, schedule, self.graphics_view)
+        # #scene = QGraphicsScene()
+        # self._scenes[self._scene_count] = scene
+        # self.graphics_view.setScene(scene)
         
-        self.graphics_view.setRenderHint(QPainter.Antialiasing)
-        # self.graphics_view.setGeometry(20, 20, self.width(), self.height())
-        self.graphics_view.setDragMode(QGraphicsView.RubberBandDrag)
-        # self.graphics_view.scale(10.0, 10.0)
-
-
-        self._scene_count += 1
-
+        # self.graphics_view.setRenderHint(QPainter.Antialiasing)
+        # # self.graphics_view.setGeometry(20, 20, self.width(), self.height())
+        # self.graphics_view.setDragMode(QGraphicsView.RubberBandDrag)
+        # # self.graphics_view.scale(10.0, 10.0)
+
+        # self._scene_count += 1
+
+
+        windowLayout = QGraphicsLinearLayout(Qt.Vertical)
+        # vertical = QGraphicsLinearLayout(Qt.Vertical)
+        linear1 = QGraphicsLinearLayout(windowLayout)
+        # linear1 = QGraphicsLinearLayout(Qt.Vertical)
+        # linear2 = QGraphicsLinearLayout(Qt.Horizontal)
+        widget = QGraphicsWidget()
+        item1 = ComponentItem(10.0)
+        # item2 = ComponentItem(10.0)
+        # item3 = ComponentItem(10.0)
+        linear1.addItem(item1)
+        linear1.setStretchFactor(item1, 1)
+        # linear1.addItem(item2)
+        # linear2.addItem(item3)
+        print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 1')
+        # vertical.addItem(linear1)
+        # vertical.addItem(linear2)
+        print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2')
+
+
+        widget.setLayout(windowLayout)
+        # widget.setLayout(linear1)
+        widget.setWindowTitle(self.tr("Basic Graphics Layouts Example"))
+        print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 3')
+
+        self._scene.addItem(widget)
+        
+        print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 4')
+        self._diagrams[self._diagram_count] = widget
+        self._diagram_count += 1
         self.update_statusbar(self.tr('Schedule loaded successfully'))
     
     @Slot()
-- 
GitLab