Skip to content
Snippets Groups Projects
Commit 6504e635 authored by Oscar Gustafsson's avatar Oscar Gustafsson :bicyclist:
Browse files

Minor cleanup and refactoring plus draw bug fix

parent c77555aa
No related branches found
No related tags found
1 merge request!78Add scheduler GUI
Pipeline #74661 failed
This commit is part of merge request !78. Comments created here will be created in the context of that merge request.
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""B-ASIC Scheduler-gui Graphics Axes Item Module. """B-ASIC Scheduler-gui Graphics Axes Item Module.
Contains the scheduler-gui GraphicsAxesItem class for drawing and maintain the axes in a graph. Contains the scheduler-gui GraphicsAxesItem class for drawing and maintain the
axes in a graph.
""" """
from typing import Union, Optional, List from typing import Union, Optional, List
from math import sin, pi from math import sin, pi
...@@ -44,10 +45,15 @@ class GraphicsAxesItem(QGraphicsItemGroup): ...@@ -44,10 +45,15 @@ class GraphicsAxesItem(QGraphicsItemGroup):
def __init__(self, width: int, height: int, def __init__(self, width: int, height: int,
width_indent: Optional[float] = 0.2, height_indent: Optional[float] = 0.2, width_indent: Optional[float] = 0.2,
width_padding: Optional[float] = 0.6, height_padding: Optional[float] = 0.5, height_indent: Optional[float] = 0.2,
width_padding: Optional[float] = 0.6,
height_padding: Optional[float] = 0.5,
parent: Optional[QGraphicsItem] = None): parent: Optional[QGraphicsItem] = None):
"""Constructs a GraphicsAxesItem. 'parent' is passed to QGraphicsItemGroup's constructor.""" """
Constructs a GraphicsAxesItem.
*parent* is passed to QGraphicsItemGroup's constructor.
"""
super().__init__(parent=parent) super().__init__(parent=parent)
assert width >= 0, f"'width' greater or equal to 0 expected, got: {width}." assert width >= 0, f"'width' greater or equal to 0 expected, got: {width}."
assert height >= 0, f"'height' greater or equal to 0 expected, got: {height}." assert height >= 0, f"'height' greater or equal to 0 expected, got: {height}."
...@@ -198,19 +204,23 @@ class GraphicsAxesItem(QGraphicsItemGroup): ...@@ -198,19 +204,23 @@ class GraphicsAxesItem(QGraphicsItemGroup):
self._x_scale_labels.insert(index, QGraphicsSimpleTextItem(str(index))) self._x_scale_labels.insert(index, QGraphicsSimpleTextItem(str(index)))
self._x_scale_labels[index].setScale(1 / self._scale) self._x_scale_labels[index].setScale(1 / self._scale)
x_pos = self._width_indent + index x_pos = self._width_indent + index
x_pos -= self.mapRectFromItem(self._x_scale_labels[index], self._x_scale_labels[index].boundingRect()).width()/2 x_pos -= self.mapRectFromItem(
self._x_scale_labels[index],
self._x_scale_labels[index].boundingRect()).width()/2
pos = self.mapToScene(QPointF(x_pos, self._x_label_offset)) pos = self.mapToScene(QPointF(x_pos, self._x_label_offset))
self._x_scale_labels[index].setPos(pos) self._x_scale_labels[index].setPos(pos)
self.addToGroup(self._x_scale_labels[index]) self.addToGroup(self._x_scale_labels[index])
self._x_ledger.insert(
index, GraphicsTimelineItem(0, 0, 0, -(self._height_indent +
self._height +
self._height_padding)))
# x-axis vertical ledger # x-axis vertical ledger
if is_timeline: # last line is a timeline if is_timeline: # last line is a timeline
self._x_ledger.insert(index, GraphicsTimelineItem(0, 0, 0, -(self._height_indent + self._height + self._height_padding)))
self._x_ledger[index].setPen(self._timeline_pen) self._x_ledger[index].setPen(self._timeline_pen)
self._x_ledger[index].set_text_scale(1.05/self._scale) self._x_ledger[index].set_text_scale(1.05/self._scale)
self._register_event_item(self._x_ledger[index]) self._register_event_item(self._x_ledger[index])
else: else:
self._x_ledger.insert(index, QGraphicsLineItem(0, 0, 0, -(self._height_indent + self._height + self._height_padding)))
self._x_ledger[index].setPen(self._ledger_pen) self._x_ledger[index].setPen(self._ledger_pen)
pos = self.mapToScene(QPointF(self._width_indent + index, 0)) pos = self.mapToScene(QPointF(self._width_indent + index, 0))
self._x_ledger[index].setPos(pos) self._x_ledger[index].setPos(pos)
...@@ -238,10 +248,10 @@ class GraphicsAxesItem(QGraphicsItemGroup): ...@@ -238,10 +248,10 @@ class GraphicsAxesItem(QGraphicsItemGroup):
# x-axis arrow # x-axis arrow
arrow_size = 8/self._scale arrow_size = 8/self._scale
p0 = QPointF(0, sin(pi/6) * arrow_size) point_0 = QPointF(0, sin(pi/6) * arrow_size)
p1 = QPointF(arrow_size, 0) point_1 = QPointF(arrow_size, 0)
p2 = QPointF(0, -sin(pi/6) * arrow_size) point_2 = QPointF(0, -sin(pi/6) * arrow_size)
polygon = QPolygonF([p0, p1, p2]) polygon = QPolygonF([point_0, point_1, point_2])
self._x_arrow.setPolygon(polygon) self._x_arrow.setPolygon(polygon)
self._x_arrow.setPen(self._base_pen) self._x_arrow.setPen(self._base_pen)
self._x_arrow.setBrush(QBrush(Qt.SolidPattern)) self._x_arrow.setBrush(QBrush(Qt.SolidPattern))
...@@ -251,9 +261,11 @@ class GraphicsAxesItem(QGraphicsItemGroup): ...@@ -251,9 +261,11 @@ class GraphicsAxesItem(QGraphicsItemGroup):
# x-axis label # x-axis label
self._x_label.setText('time') self._x_label.setText('time')
self._x_label.setScale(1 / self._scale) self._x_label.setScale(1 / self._scale)
x_pos = self._width_indent + 0 + self._width_padding # end of x-axis x_pos = self._width_indent + 0 + self._width_padding # end of x-axis
x_pos += self.mapRectFromItem(self._x_arrow, self._x_arrow.boundingRect()).width()/2 # + half arrow width x_pos += self.mapRectFromItem(self._x_arrow,
x_pos -= self.mapRectFromItem(self._x_label, self._x_label.boundingRect()).width()/2 # - center of label self._x_arrow.boundingRect()).width()/2 # + half arrow width
x_pos -= self.mapRectFromItem(self._x_label,
self._x_label.boundingRect()).width()/2 # - center of label
self._x_label.setPos(x_pos, self._x_label_offset) self._x_label.setPos(x_pos, self._x_label_offset)
self.addToGroup(self._x_label) self.addToGroup(self._x_label)
...@@ -265,6 +277,8 @@ class GraphicsAxesItem(QGraphicsItemGroup): ...@@ -265,6 +277,8 @@ class GraphicsAxesItem(QGraphicsItemGroup):
self._x_ledger[-1].setPos(pos + QPoint(self._width, 0)) # move timeline self._x_ledger[-1].setPos(pos + QPoint(self._width, 0)) # move timeline
# y-axis # y-axis
self._y_axis.setLine(0, 0, 0, -(self._height_indent + self._height + self._height_padding + 0.05)) self._y_axis.setLine(0, 0, 0,
-(self._height_indent + self._height +
self._height_padding + 0.05))
self._y_axis.setPen(self._base_pen) self._y_axis.setPen(self._base_pen)
self.addToGroup(self._y_axis) self.addToGroup(self._y_axis)
...@@ -185,9 +185,7 @@ class GraphicsGraphEvent: # PyQt5 ...@@ -185,9 +185,7 @@ class GraphicsGraphEvent: # PyQt5
"""Changes the cursor to OpenHandCursor when releasing an object.""" """Changes the cursor to OpenHandCursor when releasing an object."""
item: GraphicsComponentItem = self.scene().mouseGrabberItem() item: GraphicsComponentItem = self.scene().mouseGrabberItem()
item.setCursor(QCursor(Qt.OpenHandCursor)) item.setCursor(QCursor(Qt.OpenHandCursor))
dx = (item.mapToParent(event.pos()) - self._current_pos).x() self.set_new_starttime(item)
pos = item.x()
self.set_new_starttime(item, pos)
def comp_mouseDoubleClickEvent(self, event: QGraphicsSceneMouseEvent) -> None: ... def comp_mouseDoubleClickEvent(self, event: QGraphicsSceneMouseEvent) -> None: ...
def comp_wheelEvent(self, event: QGraphicsSceneWheelEvent) -> None: ... def comp_wheelEvent(self, event: QGraphicsSceneWheelEvent) -> None: ...
...@@ -204,25 +202,22 @@ class GraphicsGraphEvent: # PyQt5 ...@@ -204,25 +202,22 @@ class GraphicsGraphEvent: # PyQt5
horizontally in x-axis scale steps.""" horizontally in x-axis scale steps."""
# Qt.DragMoveCursor # Qt.DragMoveCursor
# button = event.button() # button = event.button()
item: GraphicsTimelineItem = self.scene().mouseGrabberItem() def update_pos(item, delta_x):
dx = (item.mapToParent(event.pos()) - self._current_pos).x() pos = item.x() + delta_x
if dx > 0.505: if self.is_valid_delta_time(self._delta_time + delta_x):
pos = item.x() + 1.0
if self.is_valid_delta_time(self._delta_time + 1):
# self.prepareGeometryChange()
item.setX(pos)
self._current_pos.setX(self._current_pos.x() + 1.0)
self._delta_time += 1
item.set_text(self._delta_time)
elif dx < -0.505:
pos = item.x() - 1.0
if self.is_valid_delta_time(self._delta_time - 1):
# self.prepareGeometryChange() # self.prepareGeometryChange()
item.setX(pos) item.setX(pos)
self._current_pos.setX(self._current_pos.x() - 1.0) self._current_pos.setX(self._current_pos.x() + delta_x)
self._delta_time -= 1 self._delta_time += delta_x
item.set_text(self._delta_time) item.set_text(self._delta_time)
item: GraphicsTimelineItem = self.scene().mouseGrabberItem()
delta_x = (item.mapToParent(event.pos()) - self._current_pos).x()
if delta_x > 0.505:
update_pos(item, 1)
elif delta_x < -0.505:
update_pos(item, -1)
def timeline_mousePressEvent(self, event: QGraphicsSceneMouseEvent) -> None: def timeline_mousePressEvent(self, event: QGraphicsSceneMouseEvent) -> None:
"""Stores the current position in item's parent coordinates. 'event' will """Stores the current position in item's parent coordinates. 'event' will
by default be accepted, and this item is then the mouse grabber. This by default be accepted, and this item is then the mouse grabber. This
......
...@@ -77,20 +77,23 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / ...@@ -77,20 +77,23 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 /
return False return False
if pos < 0: if pos < 0:
return False return False
elif (self.schedule.cyclic if (self.schedule.cyclic
and new_start_time > self.schedule.schedule_time): and new_start_time > self.schedule.schedule_time):
return False return False
elif (not self.schedule.cyclic if (not self.schedule.cyclic
and new_start_time + end_time > self.schedule.schedule_time): and new_start_time + end_time > self.schedule.schedule_time):
return False return False
return True return True
def _redraw_lines(self, item: GraphicsComponentItem): def _redraw_lines(self, item: GraphicsComponentItem):
"""Update lines connected to *item*."""
for signal in self._signal_dict[item]: for signal in self._signal_dict[item]:
signal.update_path() signal.update_path()
def set_new_starttime(self, item: GraphicsComponentItem, pos: float) -> None: def set_new_starttime(self, item: GraphicsComponentItem) -> None:
"""Set new starttime for *item*."""
pos = item.x()
op_start_time = self.schedule.start_time_of_operation(item.op_id) op_start_time = self.schedule.start_time_of_operation(item.op_id)
new_start_time = floor(pos) - floor(self._x_axis_indent) new_start_time = floor(pos) - floor(self._x_axis_indent)
move_time = new_start_time - op_start_time move_time = new_start_time - op_start_time
...@@ -112,9 +115,9 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / ...@@ -112,9 +115,9 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 /
self.schedule.set_schedule_time(self.schedule.schedule_time + delta_time) self.schedule.set_schedule_time(self.schedule.schedule_time + delta_time)
self._axes.set_width(self._axes.width + delta_time) self._axes.set_width(self._axes.width + delta_time)
@property @property
def schedule(self) -> Schedule: def schedule(self) -> Schedule:
"""The schedule."""
return self._schedule return self._schedule
@property @property
...@@ -137,14 +140,14 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / ...@@ -137,14 +140,14 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 /
_components_dict = {} _components_dict = {}
# print('Start times:') # print('Start times:')
for op_id, op_start_time in self.schedule.start_times.items(): for op_id, op_start_time in self.schedule.start_times.items():
op = self.schedule.sfg.find_by_id(op_id) operation = self.schedule.sfg.find_by_id(op_id)
# if not isinstance(op, (Input, Output)): # if not isinstance(op, (Input, Output)):
self._components_height += spacing self._components_height += spacing
component = GraphicsComponentItem(op) component = GraphicsComponentItem(operation)
component.setPos(self._x_axis_indent + op_start_time, self._components_height) component.setPos(self._x_axis_indent + op_start_time, self._components_height)
self._components.append(component) self._components.append(component)
_components_dict[op] = component _components_dict[operation] = component
self._components_height += component.height self._components_height += component.height
self._event_items += component.event_items self._event_items += component.event_items
# self._components_height += spacing # self._components_height += spacing
...@@ -169,13 +172,12 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / ...@@ -169,13 +172,12 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 /
# add signals # add signals
for component in self._components: for component in self._components:
op = component.operation for output_port in component.operation.outputs:
for i, output_port in enumerate(op.outputs):
for signal in output_port.signals: for signal in output_port.signals:
dest_component = _components_dict[signal.destination.operation] dest_component = _components_dict[signal.destination.operation]
gs = GraphicsSignal(component, dest_component, signal, pen=pen1) gui_signal = GraphicsSignal(component, dest_component, signal, pen=pen1)
self.addToGroup(gs) self.addToGroup(gui_signal)
self._signal_dict[component].add(gs) self._signal_dict[component].add(gui_signal)
self._signal_dict[dest_component].add(gs) self._signal_dict[dest_component].add(gui_signal)
pprint(GraphicsGraphItem.__mro__) pprint(GraphicsGraphItem.__mro__)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment