From ee51b473cfb6cacfe36dfbf268d29c4b5c8875b2 Mon Sep 17 00:00:00 2001 From: Manuel Date: Fri, 15 Sep 2023 12:39:47 +0200 Subject: [PATCH] Simplify giving bg entity coordinates in generate_graph --- spinetoolbox/spine_db_editor/generate_graph.py | 7 +++---- spinetoolbox/spine_db_editor/graphics_items.py | 6 +++--- .../widgets/custom_qgraphicsviews.py | 17 ++++++++++++++--- .../spine_db_editor/widgets/graph_view_mixin.py | 1 + 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/spinetoolbox/spine_db_editor/generate_graph.py b/spinetoolbox/spine_db_editor/generate_graph.py index 0eecd9511..bbba55a50 100644 --- a/spinetoolbox/spine_db_editor/generate_graph.py +++ b/spinetoolbox/spine_db_editor/generate_graph.py @@ -29,7 +29,7 @@ def generate_graph( color_parameter="", arc_width_parameter="", bg_img_path=None, - bg_img_coordinate_map=None, + bg_img_entity_coordinates=None, ): """Launches Spine Db Editor and generates a graph.""" if not pyside6_version_check(): @@ -53,9 +53,8 @@ def generate_graph( graph_view.arc_width_parameter = arc_width_parameter if bg_img_path: graph_view.set_bg_image(bg_img_path) - if bg_img_coordinate_map: - (bg1, graph1), (bg2, graph2), *_ignored = bg_img_coordinate_map.items() - graph_view.fit_bg(bg1, bg2, graph1, graph2) + if bg_img_entity_coordinates: + graph_view.set_bg_entity_coordinates(bg_img_entity_coordinates) tree_view = editor.ui.treeView_entity model = tree_view.model() selection_model = tree_view.selectionModel() diff --git a/spinetoolbox/spine_db_editor/graphics_items.py b/spinetoolbox/spine_db_editor/graphics_items.py index 64f95defd..f92fdc0ad 100644 --- a/spinetoolbox/spine_db_editor/graphics_items.py +++ b/spinetoolbox/spine_db_editor/graphics_items.py @@ -947,10 +947,10 @@ def _do_resize(self, rect, strong): def fit_rect(self, rect): self._do_resize(rect, True) - def fit_coordinates(self, bg1, bg2, scen1, scen2): + def fit_coordinates(self, p1, p2, scen1, scen2): size = self._renderer.defaultSize() - x1, y1 = bg1 - x2, y2 = bg2 + x1, y1 = p1 + x2, y2 = p2 scene_x1, scene_y1 = scen1 scene_x2, scene_y2 = scen2 y1 += size.height() diff --git a/spinetoolbox/spine_db_editor/widgets/custom_qgraphicsviews.py b/spinetoolbox/spine_db_editor/widgets/custom_qgraphicsviews.py index 1e9e8e001..26461f026 100644 --- a/spinetoolbox/spine_db_editor/widgets/custom_qgraphicsviews.py +++ b/spinetoolbox/spine_db_editor/widgets/custom_qgraphicsviews.py @@ -47,6 +47,7 @@ def __init__(self, parent): self.color_parameter = "" self.arc_width_parameter = "" self._bg_item = None + self._bg_entity_coordinates = None self.selected_items = [] self.removed_items = set() self.hidden_items = {} @@ -516,9 +517,19 @@ def set_bg_image(self, file_path, transform=None): self._bg_item = BgItem(file_path) self.scene().addItem(self._bg_item) - def fit_bg(self, bg1, bg2, scen1, scen2): - bg1, bg2, scen1, scen2 = [self._spine_db_editor.convert_position(*p) for p in (bg1, bg2, scen1, scen2)] - self._bg_item.fit_coordinates(bg1, bg2, scen1, scen2) + def set_bg_entity_coordinates(self, entity_coordinates): + self._bg_entity_coordinates = { + ent: self._spine_db_editor.convert_position(*p) for ent, p in entity_coordinates.items() + } + self.fit_bg_coordinates() + + def fit_bg_coordinates(self): + (ent1, p1), (ent2, p2), *_ignored = self._bg_entity_coordinates.items() + pos_by_name = {item.display_data: item.pos() for item in self.entity_items} + scen1, scen2 = pos_by_name.get(ent1), pos_by_name.get(ent2) + if None in (scen1, scen2): + return + self._bg_item.fit_coordinates(p1, p2, (scen1.x(), scen1.y()), (scen2.x(), scen2.y())) def clear_scene(self): for item in self.scene().items(): diff --git a/spinetoolbox/spine_db_editor/widgets/graph_view_mixin.py b/spinetoolbox/spine_db_editor/widgets/graph_view_mixin.py index 019b42a42..8384f892b 100644 --- a/spinetoolbox/spine_db_editor/widgets/graph_view_mixin.py +++ b/spinetoolbox/spine_db_editor/widgets/graph_view_mixin.py @@ -388,6 +388,7 @@ def _complete_graph(self, layout_gen_id, x, y): self.ui.graphicsView.reset_zoom() else: self.ui.graphicsView.apply_zoom() + self.ui.graphicsView.fit_bg_coordinates() def _get_selected_db_map_entity_ids(self): """Returns a set of ids corresponding to selected entities in the trees.