diff --git a/b_asic/scheduler-gui/graphics_axis_item.py b/b_asic/scheduler-gui/graphics_axis_item.py index a656ab8c66309819985ac95bfb54082fb4d73488..e389525ab71cd81afb0d1f9083e84c505fd573be 100644 --- a/b_asic/scheduler-gui/graphics_axis_item.py +++ b/b_asic/scheduler-gui/graphics_axis_item.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + from operator import contains import os import sys @@ -48,9 +51,9 @@ class GraphicsAxisItem(QGraphicsItemGroup, GraphicsGraphEvent): self._dy_height = 5/self._scale self._axis = {} - self.setFlag(QGraphicsItem.ItemIsMovable) - self.setFlag(QGraphicsItem.ItemIsSelectable) - self.setAcceptHoverEvents(True) + # self.setFlag(QGraphicsItem.ItemIsMovable) + # self.setFlag(QGraphicsItem.ItemIsSelectable) + # self.setAcceptHoverEvents(True) self.update(width, height, x_indent) diff --git a/b_asic/scheduler-gui/graphics_component_event.py b/b_asic/scheduler-gui/graphics_component_event.py index 3fc0844860e8d613094bce8e35261230a7a65f90..da3b4937e6482834674151160784a0051130a945 100644 --- a/b_asic/scheduler-gui/graphics_component_event.py +++ b/b_asic/scheduler-gui/graphics_component_event.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + """TODO""" import os @@ -32,28 +35,41 @@ from qtpy.QtCore import ( -class GraphicsComponentEvent(QGraphicsObject): +class GraphicsComponentEvent(QGraphicsItem): """Event handler for GraphicsComponentItem""" + current_pos: QPointF + # _scale: float + def __init__(self, parent): super().__init__() # self.setAcceptedMouseButtons(Qt.LeftButton) - self.setFlag(QGraphicsItem.ItemIsMovable) - self.setFlag(QGraphicsItem.ItemIsSelectable) - self.setAcceptHoverEvents(True) + # self.setAcceptedMouseButtons(Qt.NoButton) + # self.setFlag(QGraphicsItem.ItemIsMovable) + # self.setFlag(QGraphicsItem.ItemIsSelectable) + # self.setAcceptHoverEvents(True) # self.setFlag(QGraphicsItem.ItemSendsGeometryChanges) - + def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent) -> None: """Set the position of the graphical element in the graphic scene, translate coordinates of the cursor within the graphic element - in the coordinate system of the graphic scenes""" - print('GraphicsComponentEvent.mouseMoveEvent()') + in the coordinate system of the parent object""" # Qt.DragMoveCursor - self.setPos(self.mapToScene(event.pos())) + dx = (self.mapToParent(event.pos()) - self.current_pos).x() + if dx > 5.05: + # TODO: send signal + self.setX(self.x() + 10.0) + self.current_pos.setX(self.current_pos.x() + 10.0) + elif dx < -5.05: + # TODO: send signal + self.setX(self.x() - 10-0) + self.current_pos.setX(self.current_pos.x() - 10.0) + def mousePressEvent(self, event: QGraphicsSceneMouseEvent) -> None: """Changes the cursor to ClosedHandCursor when grabbing an object""" print('GraphicsComponentEvent.mousePressEvent()') + self.current_pos = self.mapToParent(event.pos()) self.setCursor(QCursor(Qt.ClosedHandCursor)) event.accept() diff --git a/b_asic/scheduler-gui/graphics_component_item.py b/b_asic/scheduler-gui/graphics_component_item.py index b99a7de6218616c10166437d29bf692354d38ff8..c234685446cace8f882c45bfa8f29572294d390f 100644 --- a/b_asic/scheduler-gui/graphics_component_item.py +++ b/b_asic/scheduler-gui/graphics_component_item.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + import os import sys from typing import Any, Optional @@ -21,7 +24,7 @@ from qtpy.QtWidgets import ( QGraphicsView, QGraphicsScene, QGraphicsWidget, QGraphicsLayout, QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayoutItem, QGraphicsAnchorLayout, QGraphicsItem, QGraphicsItemGroup, QGraphicsPathItem, QGraphicsRectItem, - QStyleOptionGraphicsItem, QWidget, QGraphicsObject) + QStyleOptionGraphicsItem, QWidget, QGraphicsObject, QGraphicsSceneMouseEvent) from qtpy.QtCore import ( QPoint, QPointF) @@ -31,7 +34,7 @@ from b_asic.schedule import Schedule from graphics_component_event import GraphicsComponentEvent -class GraphicsComponentItem(QGraphicsItemGroup, GraphicsComponentEvent, QGraphicsObject): +class GraphicsComponentItem(QGraphicsItemGroup, GraphicsComponentEvent): _scale: float = 1.0 # static, changed from MainWindow _height: float @@ -46,14 +49,19 @@ class GraphicsComponentItem(QGraphicsItemGroup, GraphicsComponentEvent, QGraphic self._height = height self._component_item = QGraphicsPathItem() self._item_group = QGraphicsItemGroup() - self.setFlag(QGraphicsItem.ItemIsMovable) - self.setFlag(QGraphicsItem.ItemIsSelectable) + # self.setFlag(QGraphicsItem.ItemIsSelectable) + # self.setFlag(QGraphicsItem.ItemSendsGeometryChanges) self.setAcceptHoverEvents(True) + # self.setAcceptTouchEvents(True) + # self.setAcceptDrops(True) + self.setAcceptedMouseButtons(Qt.AllButtons) + # self.setAcceptedMouseButtons(Qt.LeftButton) + # self.setAcceptedMouseButtons(Qt.NoButton) self._populate() - + @property def height(self) -> float: return self._height diff --git a/b_asic/scheduler-gui/graphics_graph_event.py b/b_asic/scheduler-gui/graphics_graph_event.py index dbe0215841a6428bcaf3d5c00cdf86bc7550c716..bf3770b6a7bd454135c633b7e9ad26af28075f24 100644 --- a/b_asic/scheduler-gui/graphics_graph_event.py +++ b/b_asic/scheduler-gui/graphics_graph_event.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- """TODO""" import os @@ -32,15 +34,15 @@ from qtpy.QtCore import ( -class GraphicsGraphEvent(QGraphicsObject): +class GraphicsGraphEvent(QGraphicsItem): """Event handler for GraphicsComponentItem""" def __init__(self, parent): super().__init__() # self.setAcceptedMouseButtons(Qt.LeftButton) - self.setFlag(QGraphicsItem.ItemIsMovable) - self.setFlag(QGraphicsItem.ItemIsSelectable) - self.setAcceptHoverEvents(True) + # self.setFlag(QGraphicsItem.ItemIsMovable) + # self.setFlag(QGraphicsItem.ItemIsSelectable) + # self.setAcceptHoverEvents(True) # self.setFlag(QGraphicsItem.ItemSendsGeometryChanges) def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent) -> None: diff --git a/b_asic/scheduler-gui/graphics_graph_item.py b/b_asic/scheduler-gui/graphics_graph_item.py index d3a9e5bcdc5f26c9f8dbbf1fdaece2a9b66b194b..e937b34413905be5a1c677c916d8202e3bd61d77 100644 --- a/b_asic/scheduler-gui/graphics_graph_item.py +++ b/b_asic/scheduler-gui/graphics_graph_item.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- import os import sys from typing import Any, Optional @@ -20,25 +22,27 @@ from qtpy.QtGui import ( QPaintEvent, QPainter, QPainterPath, QColor, QBrush, QPen, QFont, QPolygon, QIcon, QPixmap, QLinearGradient, QTransform) from qtpy.QtWidgets import ( + QApplication, QGraphicsView, QGraphicsScene, QGraphicsWidget, QGraphicsLayout, QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayoutItem, QGraphicsAnchorLayout, QGraphicsItem, QGraphicsItemGroup, QGraphicsPathItem, QGraphicsLineItem, QGraphicsRectItem, QStyleOptionGraphicsItem, QWidget, QGraphicsObject) from qtpy.QtCore import ( - QPoint, QPointF) + QPoint, QPointF, QEvent) # B-ASIC import logger from b_asic.schedule import Schedule from graphics_component_item import GraphicsComponentItem from graphics_axis_item import GraphicsAxisItem +from graphics_graph_event import GraphicsGraphEvent -class GraphicsGraphItem(QGraphicsItemGroup, QGraphicsObject): +class GraphicsGraphItem(QGraphicsItemGroup): _schedule: Schedule _axis: GraphicsAxisItem - _component_group: QGraphicsItemGroup + _component_group: list[GraphicsComponentItem] _components_height: float _x_axis_indent: float @@ -46,24 +50,32 @@ class GraphicsGraphItem(QGraphicsItemGroup, QGraphicsObject): def __init__(self, schedule: Schedule, parent: QGraphicsItem = None): super().__init__(parent) - self.setFlag(QGraphicsItem.ItemIsMovable) - self.setFlag(QGraphicsItem.ItemIsSelectable) + # self.setFlag(QGraphicsItem.ItemIsMovable) + # self.setFlag(QGraphicsItem.ItemIsSelectable) self.setAcceptHoverEvents(True) + self.setHandlesChildEvents(False) + # self.setAcceptedMouseButtons(Qt.NoButton) + self._schedule = deepcopy(schedule) self._axis = None - self._component_group = QGraphicsItemGroup() + # self._component_group = QGraphicsItemGroup() + self._component_group = [] + # self._component_group.setHandlesChildEvents(False) + # self._component_group.setAcceptedMouseButtons(Qt.NoButton) 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(0, self._components_height) - self._component_group.addToGroup(component) - self._components_height += component.height + spacing - self._component_group.setPos(self._x_axis_indent, spacing) + component.setPos(self._x_axis_indent, self._components_height) + self._component_group.append(component) + # self._component_group.addToGroup(component) + self._components_height += component.height + # self._component_group.setPos(self._x_axis_indent, spacing) self._components_height += spacing # build axis @@ -71,26 +83,52 @@ class GraphicsGraphItem(QGraphicsItemGroup, QGraphicsObject): # add axis and components self.addToGroup(self._axis) - self.addToGroup(self._component_group) + for component in self._component_group: + self.addToGroup(component) + # self.addToGroup(self._component_group) + + def installSceneEventFilters(self) -> None: + for item in self._component_group: + item.installSceneEventFilter(self) + # for item in self._component_group.childItems(): + # item.installSceneEventFilter(self) + self.setFiltersChildEvents(True) + + + def sceneEventFilter(self, ojb: QGraphicsItem, event: QEvent) -> bool: + """Returns true if the event was filtered (i.e. stopped), otherwise false""" + type_ = event.type() + if type_ != QEvent.GraphicsSceneHoverMove: print(f'---> Event: {type_}') + + if type_ == QEvent.GraphicsSceneHoverEnter: ... + # print('\t\tQEvent.GraphicsSceneHoverEnter') + elif type_ == QEvent.GraphicsSceneHoverLeave: ... + # print('\t\tQEvent.GraphicsSceneHoverLeave') + elif type_ == QEvent.GraphicsSceneContextMenu: ... + # elif type_ == QEvent.mousePressEvent: + # print('\t\tQEvent.mousePressEvent') + # elif type_ == QEvent.mouseReleaseEvent: + # print('\t\tQEvent.mouseReleaseEvent') + else: ... #False - def update(self) -> None: + return False + + # def sceneEvent(self, event: QEvent) -> bool: + # print(f'sceneEvent() --> {event.type()}') + # # event.accept() + # # QApplication.sendEvent(self.scene(), event) + # return False + + def update_(self) -> None: # self.prepareGeometryChange() # self.removeFromGroup(self._axis) self._axis.update(40 + 6, self._components_height, self._x_axis_indent) # self.addToGroup(self._axis) - - # @property - # def scale(self) -> float: - # return self._scale - # @scale.setter - # def scale(self, scale:float) -> None: - # self._scale = scale - # for component in self._component_group.childItems(): - # component.scale = scale - - # @staticmethod + @property + def schedule(self) -> Schedule: + return self._schedule @property def axis(self) -> GraphicsAxisItem: diff --git a/b_asic/scheduler-gui/logger.py b/b_asic/scheduler-gui/logger.py index a19c657f1e692fd4960468ba17ecaf6ba95be98f..5f470a5fbaf30c93e461163f5545894931871340 100644 --- a/b_asic/scheduler-gui/logger.py +++ b/b_asic/scheduler-gui/logger.py @@ -1,4 +1,5 @@ -# This Python file uses the following encoding: utf-8 +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- """ B-ASIC Scheduler-gui Logger Module. Contains a logger that logs to the console and a file using levels. It is based diff --git a/b_asic/scheduler-gui/main_window.py b/b_asic/scheduler-gui/main_window.py index 524180cf430dcc12c495928e06722aa393f77fed..4134efbaee6157d55cbe0b389ad8cbbade340f79 100644 --- a/b_asic/scheduler-gui/main_window.py +++ b/b_asic/scheduler-gui/main_window.py @@ -1,4 +1,5 @@ -# This Python file uses the following encoding: utf-8 +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- """B-ASIC Scheduler-gui Module. Contains the scheduler-gui class for scheduling operations in an SFG. @@ -170,14 +171,10 @@ class MainWindow(QMainWindow, Ui_MainWindow): def _init_graphics(self) -> None: """Initialize the QGraphics framework""" - # scene = GraphicsScene(0, parent=self) - # self.graphic_view.setScene(scene) - # self.graphic_view.setRenderHint(QPainter.Antialiasing) - # self.graphic_view.setGeometry(20, 20, self.width(), self.height()) self._scene = QGraphicsScene() - self.graphics_view.setDragMode(QGraphicsView.RubberBandDrag) self.graphics_view.setScene(self._scene) self.graphics_view.scale(self._scale, self._scale) + # self.setMouseTracking(True) GraphicsComponentItem._scale = self._scale GraphicsAxisItem._scale = self._scale @@ -189,23 +186,24 @@ class MainWindow(QMainWindow, Ui_MainWindow): ############### @Slot() def actionTbtn(self) -> None: - sched = self._graph.schedule - sched.plot_schedule() + self._graph.schedule.plot_schedule() + print(f'filtersChildEvents(): {self._graph.filtersChildEvents()}') # self.printButtonPressed('callback_pushButton()') @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, self.tr("Open python file"), - open_dir, - self.tr("Python Files (*.py *.py3)")) + abs_path_filename = QFileDialog.getOpenFileName(self, + self.tr("Open python file"), + open_dir, + 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)) self._open_file_dialog_opened = True - + module_name = inspect.getmodulename(abs_path_filename) if not module_name: # return if empty module name log.error('Could not load module from file \'{}\'.'.format(abs_path_filename)) @@ -250,6 +248,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): self._graph = GraphicsGraphItem(schedule) self._scene.addItem(self._graph) + self._graph.installSceneEventFilters() # graph.prepareGeometryChange() # graph.setPos(200, 20) @@ -362,9 +361,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): #### Helper member functions #### ################################# def printButtonPressed(self, func_name: str) -> None: - #TODO: remove - self.label.setText("hello") - + #TODO: remove alert = QMessageBox(self) alert.setText("Called from " + func_name + '!')