diff --git a/b_asic/scheduler-gui/graphics_axis_item.py b/b_asic/scheduler-gui/graphics_axis_item.py index e389525ab71cd81afb0d1f9083e84c505fd573be..bacfe5f8b7720591df1a4921a2dd934b0051476d 100644 --- a/b_asic/scheduler-gui/graphics_axis_item.py +++ b/b_asic/scheduler-gui/graphics_axis_item.py @@ -37,7 +37,7 @@ from graphics_graph_event import GraphicsGraphEvent -class GraphicsAxisItem(QGraphicsItemGroup, GraphicsGraphEvent): +class GraphicsAxisItem(QGraphicsItemGroup): _scale: float = 1.0 # static, changed from MainWindow _width: float @@ -55,7 +55,7 @@ class GraphicsAxisItem(QGraphicsItemGroup, GraphicsGraphEvent): # self.setFlag(QGraphicsItem.ItemIsSelectable) # self.setAcceptHoverEvents(True) - self.update(width, height, x_indent) + self.update_(width, height, x_indent) @property @@ -82,7 +82,7 @@ class GraphicsAxisItem(QGraphicsItemGroup, GraphicsGraphEvent): # for child in self._axis.values(): # del child - def update(self, width, height, x_indent) -> None: + def update_(self, width, height, x_indent) -> None: self._width = width self._height = height self._x_indent = x_indent diff --git a/b_asic/scheduler-gui/graphics_component_item.py b/b_asic/scheduler-gui/graphics_component_item.py index c234685446cace8f882c45bfa8f29572294d390f..10a9467450abf1d7e44186951a7dfd9e7f8f960a 100644 --- a/b_asic/scheduler-gui/graphics_component_item.py +++ b/b_asic/scheduler-gui/graphics_component_item.py @@ -16,7 +16,7 @@ from qtpy import QtWidgets # QGraphics and QPainter imports from qtpy.QtCore import ( - Qt, QObject, QRect, QRectF, QPoint, QSize, QSizeF, QByteArray, Slot) + 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) @@ -34,7 +34,7 @@ from b_asic.schedule import Schedule from graphics_component_event import GraphicsComponentEvent -class GraphicsComponentItem(QGraphicsItemGroup, GraphicsComponentEvent): +class GraphicsComponentItem(QGraphicsItemGroup): _scale: float = 1.0 # static, changed from MainWindow _height: float @@ -49,18 +49,24 @@ class GraphicsComponentItem(QGraphicsItemGroup, GraphicsComponentEvent): self._height = height self._component_item = QGraphicsPathItem() self._item_group = QGraphicsItemGroup() - self.setFlag(QGraphicsItem.ItemIsMovable) - # self.setFlag(QGraphicsItem.ItemIsSelectable) + self.setHandlesChildEvents(True) # PySide2 QGraphicsItemGroup default: true. PyQt5 not an option + self.setFlag(QGraphicsItem.ItemIsMovable) # mouse move events + self.setFlag(QGraphicsItem.ItemIsSelectable) # mouse click events # self.setFlag(QGraphicsItem.ItemSendsGeometryChanges) - self.setAcceptHoverEvents(True) + # self.setAcceptHoverEvents(True) # self.setAcceptTouchEvents(True) # self.setAcceptDrops(True) - self.setAcceptedMouseButtons(Qt.AllButtons) + # self.setAcceptedMouseButtons(Qt.AllButtons) # self.setAcceptedMouseButtons(Qt.LeftButton) # self.setAcceptedMouseButtons(Qt.NoButton) self._populate() + def sceneEvent(self, event: QEvent) -> bool: + print(f'Component -->\t\t\t\t{event.type()}') + # event.accept() + # QApplication.sendEvent(self.scene(), event) + return True @property def height(self) -> float: diff --git a/b_asic/scheduler-gui/graphics_graph_event.py b/b_asic/scheduler-gui/graphics_graph_event.py index bf3770b6a7bd454135c633b7e9ad26af28075f24..1b5d18db4ab4cdddc3ff70c37b9e401dca93abe1 100644 --- a/b_asic/scheduler-gui/graphics_graph_event.py +++ b/b_asic/scheduler-gui/graphics_graph_event.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -"""TODO""" +"""B-ASIC Scheduler-gui Graphics Graph Event Module. + +Contains event filters and handlers for GraphicsGraphItem objects.""" import os import sys @@ -19,66 +21,120 @@ from qtpy import QtWidgets # QGraphics and QPainter imports from qtpy.QtCore import ( - Qt, QObject, QRect, QRectF, QPoint, QSize, QSizeF, QByteArray, Slot) + 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) + QLinearGradient, QTransform, QCursor, QFocusEvent) from qtpy.QtWidgets import ( QGraphicsView, QGraphicsScene, QGraphicsWidget, QGraphicsLayout, QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayoutItem, QGraphicsAnchorLayout, QGraphicsItem, QGraphicsItemGroup, QGraphicsPathItem, QGraphicsLineItem, QGraphicsRectItem, QStyleOptionGraphicsItem, QWidget, - QGraphicsObject, QGraphicsSceneMouseEvent, QGraphicsSceneHoverEvent, QGraphicsSceneContextMenuEvent) + QGraphicsObject, QGraphicsSceneMouseEvent, QGraphicsSceneHoverEvent, QGraphicsSceneContextMenuEvent, + QGraphicsSceneDragDropEvent, QGraphicsSceneWheelEvent) from qtpy.QtCore import ( QPoint, QPointF) +from abc import ABC +from graphics_component_item import GraphicsComponentItem + + +# class GraphicsGraphEvent(ABC): class GraphicsGraphEvent(QGraphicsItem): - """Event handler for GraphicsComponentItem""" + """Event filter and handlers for GraphicsGraphItem""" + # _component_group: list[GraphicsComponentItem] - def __init__(self, parent): - super().__init__() + def __init__(self, parent: QGraphicsItem = None): + super().__init__(parent) # self.setAcceptedMouseButtons(Qt.LeftButton) # self.setFlag(QGraphicsItem.ItemIsMovable) # self.setFlag(QGraphicsItem.ItemIsSelectable) # self.setAcceptHoverEvents(True) # self.setFlag(QGraphicsItem.ItemSendsGeometryChanges) + + # self.setFlag(QGraphicsItem.ItemIsMovable) + # self.setFlag(QGraphicsItem.ItemIsSelectable) + self.setAcceptHoverEvents(True) + print(f'GraphicsGraphItem.handlesChildEvents(): {self.handlesChildEvents()}') + self.setHandlesChildEvents(True) # PySide2 QGraphicsItemGroup default: true. PyQt5 not an option + # self.setAcceptedMouseButtons(Qt.NoButton) + - 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('GraphicsGraphEvent.mouseMoveEvent()') - # Qt.DragMoveCursor - # self.setPos(self.mapToScene(event.pos())) - super().mouseMoveEvent(event) - - def mousePressEvent(self, event: QGraphicsSceneMouseEvent) -> None: - """Changes the cursor to ClosedHandCursor when grabbing an object""" - print('GraphicsGraphEvent.mousePressEvent()') - super().mousePressEvent(event) - # self.setCursor(QCursor(Qt.ClosedHandCursor)) - # event.accept() - - def mouseReleaseEvent(self, event: QGraphicsSceneMouseEvent) -> None: - """Changes the cursor to OpenHandCursor when releasing an object""" - print('GraphicsGraphEvent.mouseReleaseEvent()') - super().mouseReleaseEvent(event) - # self.setCursor(QCursor(Qt.OpenHandCursor)) - # event.accept() + ################# + #### Filters #### + ################# + def installSceneEventFilters(self) -> None: + """Installs an event filter for 'item' on 'self', causing + all events for 'item' to first pass through 'self's + sceneEventFilter() function.""" + for item in self._component_group: + item.installSceneEventFilter(self) + self.setFiltersChildEvents(True) # default false + + def removeSceneEventFilters(self) -> None: + """Removes an event filter on 'item' from 'self'.""" + for item in self._component_group: + item.removeSceneEventFilter(self) + self.setFiltersChildEvents(False) + - def hoverEnterEvent(self, event: QGraphicsSceneHoverEvent) -> None: - """Changes the cursor to OpenHandCursor when hovering an object""" - print('GraphicsGraphEvent.hoverEnterEvent()') - super().hoverEnterEvent(event) - # self.setCursor(QCursor(Qt.OpenHandCursor)) + def sceneEventFilter(self, item: 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'Graph -->\t{type(item).__name__}\t{type_}') + print(__name__) + # if event.button(): + # # print(f'Graph -->\t{type_}\t{item}') + # print(f'-------->\t{event.button()}') + + if type(item) == GraphicsComponentItem: + + switch = { + QEvent.FocusIn: self.graph_focusInEvent(item, QFocusEvent(event)), + QEvent.GraphicsSceneContextMenu: self.graph_contextMenuEvent(item, QGraphicsSceneContextMenuEvent(event)), + QEvent.GraphicsSceneDragEnter: self.graph_dragEnterEvent(item, QGraphicsSceneDragDropEvent(event)), + QEvent.GraphicsSceneDragMove: self.graph_dragMoveEvent(item, QGraphicsSceneDragDropEvent(event)), + QEvent.GraphicsSceneDragLeave: self.graph_dragLeaveEvent(item, QGraphicsSceneDragDropEvent(event)), + QEvent.GraphicsSceneDrop: self.graph_dropEvent(item, QGraphicsSceneDragDropEvent(event)), + QEvent.GraphicsSceneHoverEnter: self.graph_hoverEnterEvent(item, QGraphicsSceneHoverEvent(event)), + QEvent.GraphicsSceneHoverMove: self.graph_hoverMoveEvent(item, QGraphicsSceneHoverEvent(event)), + QEvent.GraphicsSceneHoverLeave: self.graph_hoverLeaveEvent(item, QGraphicsSceneHoverEvent(event)), + QEvent.GraphicsSceneMouseMove: self.graph_mouseMoveEvent(item, QGraphicsSceneMouseEvent(event)), + QEvent.GraphicsSceneMousePress: self.graph_mousePressEvent(item, QGraphicsSceneMouseEvent(event)), + QEvent.GraphicsSceneMouseRelease: self.graph_mouseReleaseEvent(item, QGraphicsSceneMouseEvent(event)), + QEvent.GraphicsSceneMouseDoubleClick: self.graph_mouseDoubleClickEvent(item, QGraphicsSceneMouseEvent(event)), + QEvent.GraphicsSceneWheel: self.graph_wheelEvent(item, QGraphicsSceneWheelEvent(event)) + } + # return switch.get(event.type(), self.log(item, event); False) + return switch.get(event.type(), False) + else: + print(f'Graph -->\t{type(item).__name__}\t{type_}') + + return False # returns False if event is ignored and pass through event to its child + + # def sceneEvent(self, event: QEvent) -> bool: + # print(f'sceneEvent() --> {event.type()}') + # # event.accept() + # # QApplication.sendEvent(self.scene(), event) + # return False - # def hoverMoveEvent(event: QGraphicsSceneHoverEvent) -> None: ... - # def contextMenuEvent(event: QGraphicsSceneContextMenuEvent) -> None: ... - def hoverLeaveEvent(self, event: QGraphicsSceneHoverEvent) -> None: - """Changes the cursor to ArrowCursor when leaving an object""" - print('GraphicsGraphEvent.hoverLeaveEvent()') - super().hoverLeaveEvent(event) - # self.setCursor(QCursor(Qt.ArrowCursor)) + ######################## + #### Event Handlers #### + ######################## + def graph_focusInEvent(self, item: QGraphicsItem, event: QFocusEvent) -> bool: ... + def graph_contextMenuEvent(self, item: QGraphicsItem, event: QGraphicsSceneContextMenuEvent) -> bool: ... + def graph_dragEnterEvent(self, item: QGraphicsItem, event: QGraphicsSceneDragDropEvent) -> bool: ... + def graph_dragMoveEvent(self, item: QGraphicsItem, event: QGraphicsSceneDragDropEvent) -> bool: ... + def graph_dragLeaveEvent(self, item: QGraphicsItem, event: QGraphicsSceneDragDropEvent) -> bool: ... + def graph_dropEvent(self, item: QGraphicsItem, event: QGraphicsSceneDragDropEvent) -> bool: ... + def graph_hoverEnterEvent(self, item: QGraphicsItem, event: QGraphicsSceneHoverEvent) -> bool: ... + def graph_hoverMoveEvent(self, item: QGraphicsItem, event: QGraphicsSceneHoverEvent) -> bool: ... + def graph_hoverLeaveEvent(self, item: QGraphicsItem, event: QGraphicsSceneHoverEvent) -> bool: ... + def graph_mouseMoveEvent(self, item: QGraphicsItem, event: QGraphicsSceneMouseEvent) -> bool: ... + def graph_mousePressEvent(self, item: QGraphicsItem, event: QGraphicsSceneMouseEvent) -> bool: ... + def graph_mouseReleaseEvent(self, item: QGraphicsItem, event: QGraphicsSceneMouseEvent) -> bool: ... + def graph_mouseDoubleClickEvent(self, item: QGraphicsItem, event: QGraphicsSceneMouseEvent) -> bool: ... + def graph_wheelEvent(self, item: QGraphicsItem, event: QGraphicsSceneWheelEvent) -> bool: ... \ No newline at end of file diff --git a/b_asic/scheduler-gui/graphics_graph_item.py b/b_asic/scheduler-gui/graphics_graph_item.py index e937b34413905be5a1c677c916d8202e3bd61d77..f425437a5a012805dd2a49481e1908942250cd72 100644 --- a/b_asic/scheduler-gui/graphics_graph_item.py +++ b/b_asic/scheduler-gui/graphics_graph_item.py @@ -26,7 +26,8 @@ from qtpy.QtWidgets import ( QGraphicsView, QGraphicsScene, QGraphicsWidget, QGraphicsLayout, QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayoutItem, QGraphicsAnchorLayout, QGraphicsItem, QGraphicsItemGroup, QGraphicsPathItem, QGraphicsLineItem, QGraphicsRectItem, - QStyleOptionGraphicsItem, QWidget, QGraphicsObject) + QStyleOptionGraphicsItem, QWidget, QGraphicsObject, + QGraphicsSceneContextMenuEvent, QGraphicsSceneHoverEvent) from qtpy.QtCore import ( QPoint, QPointF, QEvent) @@ -50,11 +51,12 @@ class GraphicsGraphItem(QGraphicsItemGroup): def __init__(self, schedule: Schedule, parent: QGraphicsItem = None): super().__init__(parent) - # self.setFlag(QGraphicsItem.ItemIsMovable) - # self.setFlag(QGraphicsItem.ItemIsSelectable) - self.setAcceptHoverEvents(True) - self.setHandlesChildEvents(False) - # self.setAcceptedMouseButtons(Qt.NoButton) + # # self.setFlag(QGraphicsItem.ItemIsMovable) + # # self.setFlag(QGraphicsItem.ItemIsSelectable) + # self.setAcceptHoverEvents(True) + # print(f'GraphicsGraphItem.handlesChildEvents(): {self.handlesChildEvents()}') + # self.setHandlesChildEvents(True) # PySide2 QGraphicsItemGroup default: true. PyQt5 not an option + # # self.setAcceptedMouseButtons(Qt.NoButton) self._schedule = deepcopy(schedule) @@ -92,32 +94,48 @@ class GraphicsGraphItem(QGraphicsItemGroup): item.installSceneEventFilter(self) # for item in self._component_group.childItems(): # item.installSceneEventFilter(self) - self.setFiltersChildEvents(True) + self.setFiltersChildEvents(True) # default false - def sceneEventFilter(self, ojb: QGraphicsItem, event: QEvent) -> bool: - """Returns true if the event was filtered (i.e. stopped), otherwise false""" + def sceneEventFilter(self, item: 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 + if type_ != QEvent.GraphicsSceneHoverMove: print(f'Graph -->\t{type(item).__name__}\t{type_}') + print(__name__) + # if event.button(): + # # print(f'Graph -->\t{type_}\t{item}') + # print(f'-------->\t{event.button()}') + + if type(item) == GraphicsComponentItem: + + switch = { + QEvent.FocusIn: self.graph_focusInEvent(item, Qt.QFocusEvent(event)), + QEvent.GraphicsSceneContextMenu: self.graph_contextMenuEvent(item, Qt.QGraphicsSceneContextMenuEvent(event)), + QEvent.GraphicsSceneDragEnter: self.graph_dragEnterEvent(item, Qt.QGraphicsSceneDragDropEvent(event)), + QEvent.GraphicsSceneDragMove: self.graph_dragMoveEvent(item, Qt.QGraphicsSceneDragDropEvent(event)), + QEvent.GraphicsSceneDragLeave: self.graph_dragLeaveEvent(item, Qt.QGraphicsSceneDragDropEvent(event)), + QEvent.GraphicsSceneDrop: self.graph_dropEvent(item, Qt.QGraphicsSceneDragDropEvent(event)), + QEvent.GraphicsSceneHoverEnter: self.graph_hoverEnterEvent(item, Qt.QGraphicsSceneHoverEvent(event)), + QEvent.GraphicsSceneHoverMove: self.graph_hoverMoveEvent(item, Qt.QGraphicsSceneHoverEvent(event)), + QEvent.GraphicsSceneHoverLeave: self.graph_hoverLeaveEvent(item, Qt.QGraphicsSceneHoverEvent(event)), + QEvent.GraphicsSceneMouseMove: self.graph_mouseMoveEvent(item, Qt.QGraphicsSceneMouseEvent(event)), + QEvent.GraphicsSceneMousePress: self.graph_mousePressEvent(item, Qt.QGraphicsSceneMouseEvent(event)), + QEvent.GraphicsSceneMouseRelease: self.graph_mouseReleaseEvent(item, Qt.QGraphicsSceneMouseEvent(event)), + QEvent.GraphicsSceneMouseDoubleClick: self.graph_mouseDoubleClickEvent(item, Qt.QGraphicsSceneMouseEvent(event)), + QEvent.GraphicsSceneWheel: self.graph_wheelEvent(item, Qt.QGraphicsSceneWheelEvent(event)) + } + return switch.get(event.type(), self.log(item, event); False) + return switch.get(event.type(), False) + else: + print(f'Graph -->\t{type(item).__name__}\t{type_}') - return False + return False # returns False if event is ignored and passed through to its child - # def sceneEvent(self, event: QEvent) -> bool: - # print(f'sceneEvent() --> {event.type()}') - # # event.accept() - # # QApplication.sendEvent(self.scene(), event) - # 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() diff --git a/b_asic/scheduler-gui/logger.py b/b_asic/scheduler-gui/logger.py index 5f470a5fbaf30c93e461163f5545894931871340..8f552c979e9774126b5b452d815490738f2a2046 100644 --- a/b_asic/scheduler-gui/logger.py +++ b/b_asic/scheduler-gui/logger.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -""" B-ASIC Scheduler-gui Logger Module. +"""B-ASIC Scheduler-gui Logger Module. Contains a logger that logs to the console and a file using levels. It is based on the `logging` module and has predefined levels of logging.