Skip to content

Commit

Permalink
Fix fetching fixed positions for items in entity graph
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelma committed Aug 31, 2023
1 parent 8a15690 commit 6081c13
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
45 changes: 29 additions & 16 deletions spinetoolbox/spine_db_editor/widgets/graph_view_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def _get_db_map_entities_for_graph(self, db_map_entity_ids):
(db_map, x)
for db_map in self.db_maps
for x in self.db_mngr.get_items(db_map, "entity", only_visible=False)
if cond(((db_map, id_) in db_map_entity_ids for id_ in x["element_id_list"]))
if x["element_id_list"] and cond((db_map, id_) in db_map_entity_ids for id_ in x["element_id_list"])
] + [
(db_map, self.db_mngr.get_item(db_map, "entity", id_, only_visible=False))
for db_map, id_ in db_map_entity_ids
Expand Down Expand Up @@ -382,16 +382,30 @@ def _update_entity_element_inds(self, db_map_element_id_lists):
self.entity_inds.append(ent_ind)
self.element_inds.append(el_ind)

def _get_parameter_positions(self, parameter_name):
if not parameter_name:
yield from []
for db_map in self.db_maps:
for p in self.db_mngr.get_items_by_field(
db_map, "parameter_value", "parameter_name", parameter_name, only_visible=False
):
pos = from_database(p["value"], p["type"])
if isinstance(pos, float):
yield (db_map, p["entity_id"]), pos
def _get_fixed_pos(self, db_map_entity_id):
db_map, entity_id = db_map_entity_id
entity = self.db_mngr.get_item(db_map, "entity", entity_id, only_visible=False)
alternative = next(iter(self.db_mngr.get_items(db_map, "alternative", only_visible=False)), None)
if not entity or not alternative:
return None
table_cache = db_map.cache.table_cache("parameter_value")
pos_x, pos_y = [
table_cache.find_item(
{
"parameter_definition_name": pname,
"entity_class_name": entity["class_name"],
"entity_byname": entity["byname"],
"alternative_name": alternative["name"],
}
)
for pname in (self.ui.graphicsView.pos_x_parameter, self.ui.graphicsView.pos_y_parameter)
]
if not pos_x or not pos_y:
return None
pos_x, pos_y = [from_database(p["value"], p["type"]) for p in (pos_x, pos_y)]
if isinstance(pos_x, float) and isinstance(pos_y, float):
return {"x": pos_x, "y": pos_y}
return None

def _make_layout_generator(self):
"""Returns a layout generator for the current graph.
Expand All @@ -404,15 +418,14 @@ def _make_layout_generator(self):
for item in self.ui.graphicsView.items():
if isinstance(item, EntityItem):
fixed_positions[item.first_db_map, item.first_id] = {"x": item.pos().x(), "y": item.pos().y()}
param_pos_x = dict(self._get_parameter_positions(self.ui.graphicsView.pos_x_parameter))
param_pos_y = dict(self._get_parameter_positions(self.ui.graphicsView.pos_y_parameter))
for db_map_entity_id in param_pos_x.keys() & param_pos_y.keys():
fixed_positions[db_map_entity_id] = {"x": param_pos_x[db_map_entity_id], "y": param_pos_y[db_map_entity_id]}
for db_map_entity_ids in self.db_map_entity_id_sets:
for db_map_entity_id in db_map_entity_ids:
fixed_positions[db_map_entity_id] = self._get_fixed_pos(db_map_entity_id)
heavy_positions = {
ind: fixed_positions[db_map_entity_id]
for ind, db_map_entity_ids in enumerate(self.db_map_entity_id_sets)
for db_map_entity_id in db_map_entity_ids
if db_map_entity_id in fixed_positions
if fixed_positions[db_map_entity_id]
}
return GraphLayoutGeneratorRunnable(
self._layout_gen_id,
Expand Down
6 changes: 2 additions & 4 deletions spinetoolbox/spine_db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,8 @@ def get_items(self, db_map, item_type, only_visible=True):
Returns:
list
"""
items = list(db_map.cache.get(item_type, {}).values())
if only_visible:
return items
db_map.cache.fetch_all(item_type)
if not only_visible:
db_map.cache.fetch_all(item_type)
return list(db_map.cache.get(item_type, {}).values())

def get_items_by_field(self, db_map, item_type, field, value, only_visible=True):
Expand Down

0 comments on commit 6081c13

Please sign in to comment.