Skip to content

Commit

Permalink
Simplify giving bg entity coordinates in generate_graph
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelma committed Sep 15, 2023
1 parent 283509b commit ee51b47
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
7 changes: 3 additions & 4 deletions spinetoolbox/spine_db_editor/generate_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions spinetoolbox/spine_db_editor/graphics_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 14 additions & 3 deletions spinetoolbox/spine_db_editor/widgets/custom_qgraphicsviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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():
Expand Down
1 change: 1 addition & 0 deletions spinetoolbox/spine_db_editor/widgets/graph_view_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ee51b47

Please sign in to comment.