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

Merge branch 'refactorcolors' into 'master'

Refactor color handling

See merge request !80
parents f0e26d6d f335a7be
No related branches found
No related tags found
1 merge request!80Refactor color handling
Pipeline #74706 passed
from qtpy.QtCore import Qt
from qtpy.QtGui import QColor
SIGNAL_INACTIVE = QColor(Qt.black)
SIGNAL_ACTIVE = QColor(Qt.red)
SIGNAL_WIDTH = 0.03
OPERATION_LATENCY_INACTIVE = QColor(Qt.lightGray)
OPERATION_LATENCY_ACTIVE = QColor(Qt.red)
OPERATION_EXECUTION_TIME_INACTIVE = QColor(Qt.magenta)
OPERATION_EXECUTION_TIME_ACTIVE = QColor(Qt.magenta)
......@@ -16,6 +16,9 @@ from qtpy.QtWidgets import (
# B-ASIC
from b_asic.graph_component import GraphComponent
from b_asic.scheduler_gui._preferences import (
OPERATION_LATENCY_ACTIVE, OPERATION_LATENCY_INACTIVE,
OPERATION_EXECUTION_TIME_INACTIVE)
class GraphicsComponentItem(QGraphicsItemGroup):
......@@ -99,9 +102,20 @@ class GraphicsComponentItem(QGraphicsItemGroup):
def get_port_location(self, key) -> QPointF:
return self.mapToParent(self._ports[key]['pos'])
def set_active(self):
self._set_background(OPERATION_LATENCY_ACTIVE)
self.setCursor(QCursor(Qt.ClosedHandCursor))
def set_inactive(self):
self._set_background(OPERATION_LATENCY_INACTIVE)
self.setCursor(QCursor(Qt.OpenHandCursor))
def _set_background(self, color: QColor):
brush = QBrush(color)
self._component_item.setBrush(brush)
def _make_component(self) -> None:
"""Makes a new component out of the stored attributes."""
brush1 = QBrush(Qt.lightGray) # used by component filling
pen1 = QPen(Qt.black) # used by component outline
pen1.setWidthF(2/self._scale)
# pen1.setCapStyle(Qt.RoundCap) # Qt.FlatCap, Qt.SquareCap (default), Qt.RoundCap
......@@ -115,22 +129,21 @@ class GraphicsComponentItem(QGraphicsItemGroup):
gray = QColor(Qt.gray)
gray.setAlpha(100) # 0-255
green = QColor(Qt.magenta)
green.setAlpha(200) # 0-255
execution_time = QColor(OPERATION_EXECUTION_TIME_INACTIVE)
execution_time.setAlpha(200) # 0-255
pen3 = QPen() # used by execution time outline
pen3.setColor(green)
pen3.setColor(execution_time)
pen3.setWidthF(3/self._scale)
## component path
def draw_component_path(keys: List[str], revered: bool) -> None:
def draw_component_path(keys: List[str], reversed: bool) -> None:
"""Draws component path and also register port positions in self._ports dictionary."""
nonlocal x
nonlocal y
nonlocal old_x
nonlocal old_y
neg = 1
if revered: neg = -1
neg = -1 if reversed else 1
for key in keys:
# draw 1 or 2 lines
x = self._ports[key]['latency']
......@@ -153,25 +166,26 @@ class GraphicsComponentItem(QGraphicsItemGroup):
output_keys = sorted(output_keys, reverse=True)
# Set the starting position
x = old_x = self._ports[input_keys[0]]['latency'] if input_keys else 0
y = old_y = 0
component_path = QPainterPath(QPointF(x, y)) # starting point
# draw the path
if input_keys:
x = self._ports[input_keys[0]]['latency']
draw_component_path(input_keys, False) # draw input side
else:
x = 0
y = 0
old_x = x
old_y = y
component_path = QPainterPath(QPointF(x, y)) # starting point
y = old_y = self._height
# draw the path
draw_component_path(input_keys, False) # draw input side
draw_component_path(output_keys, True) # draw ouput side
draw_component_path(output_keys, True) # draw output side
component_path.closeSubpath()
## component item
self._component_item = QGraphicsPathItem(component_path)
self._component_item.setPen(pen1)
self._component_item.setBrush(brush1)
self._set_background(Qt.lightGray) # used by component filling
## ports item
for port_dict in self._ports.values():
......
......@@ -19,7 +19,6 @@ from b_asic.scheduler_gui.graphics_axes_item import GraphicsAxesItem
from b_asic.scheduler_gui.graphics_timeline_item import GraphicsTimelineItem
# sys.settrace
# class GraphicsGraphEvent(QGraphicsItemGroup, QObject): # PySide2
class GraphicsGraphEvent: # PyQt5
......@@ -152,23 +151,21 @@ class GraphicsGraphEvent: # PyQt5
horizontally in x-axis scale steps."""
# Qt.DragMoveCursor
# button = event.button()
item: GraphicsComponentItem = self.scene().mouseGrabberItem()
dx = (item.mapToParent(event.pos()) - self._current_pos).x()
if dx > 0.505:
pos = item.x() + 1.0
if self.is_component_valid_pos(item, pos):
# self.prepareGeometryChange()
item.setX(pos)
self._current_pos.setX(self._current_pos.x() + 1.0)
self._redraw_lines(item)
elif dx < -0.505:
pos = item.x() - 1.0
def update_pos(item, delta_x):
pos = item.x() + delta_x
if self.is_component_valid_pos(item, pos):
# self.prepareGeometryChange()
item.setX(pos)
self._current_pos.setX(self._current_pos.x() - 1.0)
self._current_pos.setX(self._current_pos.x() + delta_x)
self._redraw_lines(item)
item: GraphicsComponentItem = 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 comp_mousePressEvent(self, event: QGraphicsSceneMouseEvent) -> None:
"""Changes the cursor to ClosedHandCursor when grabbing an object and
stores the current position in item's parent coordinates. 'event' will
......@@ -178,15 +175,16 @@ class GraphicsGraphEvent: # PyQt5
self._signals.component_selected.emit(item.op_id)
# self.component_selected.emit(item.op_id)
self._current_pos = item.mapToParent(event.pos())
item.setCursor(QCursor(Qt.ClosedHandCursor))
self.set_item_active(item)
event.accept()
def comp_mouseReleaseEvent(self, event: QGraphicsSceneMouseEvent) -> None:
"""Changes the cursor to OpenHandCursor when releasing an object."""
item: GraphicsComponentItem = self.scene().mouseGrabberItem()
item.setCursor(QCursor(Qt.OpenHandCursor))
self.set_item_inactive(item)
self.set_new_starttime(item)
def comp_mouseDoubleClickEvent(self, event: QGraphicsSceneMouseEvent) -> None: ...
def comp_wheelEvent(self, event: QGraphicsSceneWheelEvent) -> None: ...
......
......@@ -7,20 +7,18 @@ maintain a component in a graph.
"""
from collections import defaultdict
from math import floor
from pprint import pprint
from typing import Optional, List, Dict, Set
from pprint import pprint
from typing import Optional, List, Dict, Set
# QGraphics and QPainter imports
from qtpy.QtWidgets import QGraphicsItem, QGraphicsItemGroup
from qtpy.QtGui import QPen
from qtpy.QtCore import Qt
# B-ASIC
from b_asic.schedule import Schedule
from b_asic.scheduler_gui.graphics_component_item import GraphicsComponentItem
from b_asic.scheduler_gui.graphics_axes_item import GraphicsAxesItem
from b_asic.scheduler_gui.graphics_graph_event import GraphicsGraphEvent
from b_asic.scheduler_gui.graphics_signal import GraphicsSignal
from b_asic.schedule import Schedule
from b_asic.scheduler_gui.graphics_component_item import GraphicsComponentItem
from b_asic.scheduler_gui.graphics_axes_item import GraphicsAxesItem
from b_asic.scheduler_gui.graphics_graph_event import GraphicsGraphEvent
from b_asic.scheduler_gui.graphics_signal import GraphicsSignal
class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 / PyQt5
......@@ -91,6 +89,16 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 /
for signal in self._signal_dict[item]:
signal.update_path()
def set_item_active(self, item: GraphicsComponentItem):
item.set_active()
for signal in self._signal_dict[item]:
signal.set_active()
def set_item_inactive(self, item: GraphicsComponentItem):
item.set_inactive()
for signal in self._signal_dict[item]:
signal.set_inactive()
def set_new_starttime(self, item: GraphicsComponentItem) -> None:
"""Set new starttime for *item*."""
pos = item.x()
......@@ -166,16 +174,12 @@ class GraphicsGraphItem(GraphicsGraphEvent, QGraphicsItemGroup): # PySide2 /
self.addToGroup(component)
# self.addToGroup(self._components)
pen1 = QPen(Qt.black) # used by component outline
pen1.setWidthF(0.03)
# add signals
for component in self._components:
for output_port in component.operation.outputs:
for signal in output_port.signals:
dest_component = _components_dict[signal.destination.operation]
gui_signal = GraphicsSignal(component, dest_component, signal, pen=pen1)
gui_signal = GraphicsSignal(component, dest_component, signal)
self.addToGroup(gui_signal)
self._signal_dict[component].add(gui_signal)
self._signal_dict[dest_component].add(gui_signal)
......
from typing import Optional
from typing import Optional
from qtpy.QtWidgets import QGraphicsItem, QGraphicsPathItem
from qtpy.QtGui import QPainterPath, QPen
from qtpy.QtCore import Qt, QPointF
from qtpy.QtCore import QPointF
# B-ASIC
from b_asic.signal import Signal
from b_asic.scheduler_gui.graphics_component_item import GraphicsComponentItem
from b_asic.scheduler_gui.graphics_component_item import GraphicsComponentItem
from b_asic.scheduler_gui._preferences import SIGNAL_ACTIVE, SIGNAL_INACTIVE, SIGNAL_WIDTH
class GraphicsSignal(QGraphicsPathItem):
_path: Optional[QPainterPath] = None
_src_operation: GraphicsComponentItem
_dest_operation: GraphicsComponentItem
_signal: Signal
_active_pen: QPen
_inactive_pen: QPen
def __init__(self,
src_operation: GraphicsComponentItem,
dest_operation: GraphicsComponentItem,
signal: Signal, pen: Optional[QPen] = None,
signal: Signal,
parent: Optional[QGraphicsItem] = None):
super().__init__(parent=parent)
self._src_operation = src_operation
self._dest_operation = dest_operation
self._signal = signal
if pen is None:
pen = QPen(Qt.black)
pen.setWidthF(0.03)
self.setPen(pen)
self.refresh_pens()
self.set_inactive()
self.update_path()
def update_path(self):
......@@ -52,3 +54,17 @@ class GraphicsSignal(QGraphicsPathItem):
path.cubicTo(ctrl_point1, ctrl_point2, dest_point)
self.setPath(path)
def refresh_pens(self):
pen = QPen(SIGNAL_ACTIVE)
pen.setWidthF(SIGNAL_WIDTH)
self._active_pen = pen
pen = QPen(SIGNAL_INACTIVE)
pen.setWidthF(SIGNAL_WIDTH)
self._inactive_pen = pen
def set_active(self):
self.setPen(self._active_pen)
def set_inactive(self):
self.setPen(self._inactive_pen)
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