From a65368f11c2243fa0937649f7a34c32fccdcf0ba Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson <oscar.gustafsson@gmail.com> Date: Thu, 23 Feb 2023 10:26:49 +0100 Subject: [PATCH] Fix moving operations in y-direction --- b_asic/schedule.py | 35 +++++++++++++++++++++++++ b_asic/scheduler_gui/main_window.py | 4 +++ b_asic/scheduler_gui/scheduler_event.py | 21 +++++++++------ b_asic/scheduler_gui/scheduler_item.py | 6 +++++ 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/b_asic/schedule.py b/b_asic/schedule.py index 2b725089..1ada55a4 100644 --- a/b_asic/schedule.py +++ b/b_asic/schedule.py @@ -360,6 +360,41 @@ class Schedule: self._schedule_time = self._schedule_time // factor return self + def move_y_location(self, graph_id: GraphID, new_y: int, insert: bool = False): + """ + Move operation in y-direction and remove any empty rows. + + Parameters + ---------- + graph_id : GraphID + The GraphID of the operation to move. + new_y : int + The new y-position of the operation. + insert : bool, optional + If True, all operations on that y-position will be moved one position. + The default is False. + + """ + if insert: + for gid, y_location in self._y_locations.items(): + if y_location >= new_y: + self._y_locations[gid] += 1 + self._y_locations[graph_id] = new_y + used_locations = {*self._y_locations.values()} + possible_locations = set(range(max(used_locations) + 1)) + if not possible_locations - used_locations: + return + remapping = dict() + offset = 0 + for loc in possible_locations: + if loc in used_locations: + remapping[loc] = loc - offset + else: + offset += 1 + + for gid, y_location in self._y_locations.items(): + self._y_locations[gid] = remapping[self._y_locations[gid]] + def move_operation(self, graph_id: GraphID, time: int) -> "Schedule": """ Move an operation in the schedule. diff --git a/b_asic/scheduler_gui/main_window.py b/b_asic/scheduler_gui/main_window.py index 8d56996e..28c6e550 100644 --- a/b_asic/scheduler_gui/main_window.py +++ b/b_asic/scheduler_gui/main_window.py @@ -482,9 +482,13 @@ class MainWindow(QMainWindow, Ui_MainWindow): self._graph._signals.schedule_time_changed.connect( self.info_table_update_schedule ) + self._graph._signals.redraw_all.connect(self._redraw_all) self.info_table_fill_schedule(self._schedule) self.update_statusbar(self.tr("Schedule loaded successfully")) + def _redraw_all(self) -> None: + self._graph._redraw_all() + def update_statusbar(self, msg: str) -> None: """ Write *msg* to the statusbar with temporarily policy. diff --git a/b_asic/scheduler_gui/scheduler_event.py b/b_asic/scheduler_gui/scheduler_event.py index 37add599..22b17511 100644 --- a/b_asic/scheduler_gui/scheduler_event.py +++ b/b_asic/scheduler_gui/scheduler_event.py @@ -44,6 +44,7 @@ class SchedulerEvent: # PyQt5 component_selected = Signal(str) schedule_time_changed = Signal() component_moved = Signal(str) + redraw_all = Signal() _axes: Optional[AxesItem] _current_pos: QPointF @@ -243,18 +244,22 @@ class SchedulerEvent: # PyQt5 if pos_x > self._schedule.schedule_time: pos_x = pos_x % self._schedule.schedule_time redraw = True - if self._schedule._y_locations[item.operation.graph_id] % 1: - # TODO: move other operations - self._schedule._y_locations[item.operation.graph_id] = math.ceil( - self._schedule._y_locations[item.operation.graph_id] + # Check move in y-direction + if ( + self._schedule._y_locations[item.operation.graph_id] + != self._old_op_position + ): + self._schedule.move_y_location( + item.operation.graph_id, + math.ceil(self._schedule._y_locations[item.operation.graph_id]), + (self._schedule._y_locations[item.operation.graph_id] % 1) != 0, ) - pos_y = item.y() + (OPERATION_GAP + OPERATION_HEIGHT) / 2 - item.setY(pos_y) - redraw = True + self._signals.redraw_all.emit() + # Operation has been moved in x-direction if redraw: item.setX(pos_x) self._redraw_lines(item) - self._signals.component_moved.emit(item.graph_id) + self._signals.component_moved.emit(item.graph_id) def operation_mouseDoubleClickEvent(self, event: QGraphicsSceneMouseEvent) -> None: ... diff --git a/b_asic/scheduler_gui/scheduler_item.py b/b_asic/scheduler_gui/scheduler_item.py index 0e8a0443..6e231426 100644 --- a/b_asic/scheduler_gui/scheduler_item.py +++ b/b_asic/scheduler_gui/scheduler_item.py @@ -233,6 +233,12 @@ class SchedulerItem(SchedulerEvent, QGraphicsItemGroup): # PySide2 / PyQt5 self._set_position(graph_id) self._redraw_all_lines() + def _redraw_all(self) -> None: + for graph_id in self._operation_items: + self._set_position(graph_id) + self._redraw_all_lines() + self._update_axes() + def _update_axes(self, build=False) -> None: # build axes schedule_time = self.schedule.schedule_time -- GitLab