Skip to content

Commit

Permalink
Add option to save all positions without selecting items
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelma committed Sep 19, 2023
1 parent bbee0ff commit c9b6c17
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
76 changes: 38 additions & 38 deletions spinetoolbox/spine_db_editor/widgets/custom_qgraphicsviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ def __init__(self, parent):
self._select_graph_params_action = None
self._save_pos_action = None
self._clear_pos_action = None
self._hide_selected_action = None
self._show_all_hidden_action = None
self._prune_selected_action = None
self._restore_all_pruned_action = None
self._rebuild_action = None
self._export_as_image_action = None
Expand Down Expand Up @@ -190,8 +188,8 @@ def set_many_properties(self, props):
for name, value in props.items():
self.set_property(name, value)

def set_pruned_entity_ids(self, db_map, ids):
self.pruned_db_map_entity_ids = {"pruned from saved view": {(db_map, id_) for id_ in ids}}
def set_pruned_db_map_entity_ids(self, key, pruned_db_map_entity_ids):
self.pruned_db_map_entity_ids = {key: pruned_db_map_entity_ids}

def get_pruned_entity_ids(self, db_map):
return [
Expand Down Expand Up @@ -238,14 +236,12 @@ def populate_context_menu(self):
self._menu.addSeparator()
self._find_action = self._menu.addAction("Search...", self._find)
self._menu.addSeparator()
self._hide_selected_action = self._menu.addAction("Hide selected", self.hide_selected_items)
self._hide_classes_menu = self._menu.addMenu("Hide classes")
self._hide_classes_menu.triggered.connect(self._hide_class)
self._show_hidden_menu = self._menu.addMenu("Show")
self._show_hidden_menu.triggered.connect(self.show_hidden_items)
self._show_all_hidden_action = self._menu.addAction("Show all", self.show_all_hidden_items)
self._menu.addSeparator()
self._prune_selected_action = self._menu.addAction("Prune selected", self.prune_selected_items)
self._prune_classes_menu = self._menu.addMenu("Prune classes")
self._prune_classes_menu.triggered.connect(self._prune_class)
self._restore_pruned_menu = self._menu.addMenu("Restore")
Expand Down Expand Up @@ -281,8 +277,8 @@ def populate_context_menu(self):
)
self._select_bg_image_action = self._menu.addAction("Select background image...", self._select_bg_image)
self._menu.addSeparator()
self._save_pos_action = self._menu.addAction("Save positions", self.save_positions)
self._clear_pos_action = self._menu.addAction("Clear saved positions", self.clear_saved_positions)
self._save_pos_action = self._menu.addAction("Save positions", self._save_all_positions)
self._clear_pos_action = self._menu.addAction("Clear saved positions", self._clear_all_positions)
self._menu.addSeparator()
self._save_state_action = self._menu.addAction("Save state...", self._save_state)
self._load_state_menu = self._menu.addMenu("Load state")
Expand Down Expand Up @@ -347,21 +343,22 @@ def _update_actions_visibility(self):
key = f"{item.entity_class_name}"
self._items_per_class.setdefault(key, list()).append(item)
self._db_map_graph_data_by_name = self._spine_db_editor.get_db_map_graph_data_by_name()
self._save_pos_action.setEnabled(bool(self.selected_items))
self._clear_pos_action.setEnabled(bool(self.selected_items))
self._hide_selected_action.setEnabled(bool(self.selected_items))
self._show_hidden_menu.setEnabled(any(self.hidden_items.values()))
self._show_all_hidden_action.setEnabled(bool(self.hidden_items))
self._prune_selected_action.setEnabled(bool(self.selected_items))
self._restore_pruned_menu.setEnabled(any(self.pruned_db_map_entity_ids.values()))
self._restore_all_pruned_action.setEnabled(any(self.pruned_db_map_entity_ids.values()))
self._prune_selected_action.setText(f"Prune {self._get_selected_entity_names()}")
self._rebuild_action.setEnabled(has_graph)
self._zoom_action.setEnabled(has_graph)
self._rotate_action.setEnabled(has_graph)
self._find_action.setEnabled(has_graph)
self._export_as_image_action.setEnabled(has_graph)
self._export_as_video_action.setEnabled(has_graph and self._spine_db_editor.ui.time_line_widget.isVisible())
self._show_hidden_menu.clear()
self._show_hidden_menu.setEnabled(any(self.hidden_items.values()))
for key in sorted(self.hidden_items):
self._show_hidden_menu.addAction(key)
self._restore_pruned_menu.clear()
self._restore_pruned_menu.setEnabled(any(self.pruned_db_map_entity_ids.values()))
for key in sorted(self.pruned_db_map_entity_ids):
self._restore_pruned_menu.addAction(key)
self._hide_classes_menu.clear()
self._hide_classes_menu.setEnabled(bool(self._items_per_class))
for key in sorted(self._items_per_class.keys() - self.hidden_items.keys()):
Expand All @@ -381,15 +378,14 @@ def _update_actions_visibility(self):

def make_items_menu(self):
menu = QMenu(self)
menu.addAction(self._save_pos_action)
menu.addAction(self._clear_pos_action)
menu.addAction("Save positions", self._save_selected_positions).setEnabled(bool(self.selected_items))
menu.addAction("Clear saved positions", self._clear_selected_positions).setEnabled(bool(self.selected_items))
menu.addSeparator()
menu.addAction(self._hide_selected_action)
menu.addAction(self._prune_selected_action)
menu.addAction("Hide", self.hide_selected_items).setEnabled(bool(self.selected_items))
menu.addAction("Prune", self.prune_selected_items).setEnabled(bool(self.selected_items))
menu.addSeparator()
menu.addAction("Edit", self.edit_selected)
menu.addAction("Remove", self.remove_selected)
menu.aboutToShow.connect(self._update_actions_visibility)
menu.addAction("Edit", self.edit_selected).setEnabled(bool(self.selected_items))
menu.addAction("Remove", self.remove_selected).setEnabled(bool(self.selected_items))
return menu

@Slot(bool)
Expand Down Expand Up @@ -423,7 +419,6 @@ def hide_selected_items(self, checked=False):
"""Hides selected items."""
key = self._get_selected_entity_names()
self.hidden_items[key] = self.selected_items
self._show_hidden_menu.addAction(key)
for item in self.selected_items:
item.setVisible(False)

Expand All @@ -433,7 +428,6 @@ def _hide_class(self, action):
key = action.text()
items = self._items_per_class[key]
self.hidden_items[key] = items
self._show_hidden_menu.addAction(key)
for item in items:
item.setVisible(False)

Expand All @@ -442,7 +436,6 @@ def show_all_hidden_items(self, checked=False):
"""Shows all hidden items."""
if not self.scene():
return
self._show_hidden_menu.clear()
while self.hidden_items:
_, items = self.hidden_items.popitem()
for item in items:
Expand All @@ -454,8 +447,6 @@ def show_hidden_items(self, action):
key = action.text()
items = self.hidden_items.pop(key, None)
if items is not None:
action = next(iter(a for a in self._show_hidden_menu.actions() if a.text() == key))
self._show_hidden_menu.removeAction(action)
for item in items:
item.setVisible(True)

Expand All @@ -464,7 +455,6 @@ def prune_selected_items(self, checked=False):
"""Prunes selected items."""
key = self._get_selected_entity_names()
self.pruned_db_map_entity_ids[key] = {db_map_id for x in self.selected_items for db_map_id in x.db_map_ids}
self._restore_pruned_menu.addAction(key)
self._spine_db_editor.build_graph()

@Slot(QAction)
Expand All @@ -479,23 +469,19 @@ def _prune_class(self, action):
db_map, "entity", "class_id", item.entity_class_id(db_map), only_visible=False
)
}
self._restore_pruned_menu.addAction(key)
self._spine_db_editor.build_graph()

@Slot(bool)
def restore_all_pruned_items(self, checked=False):
"""Reinstates all pruned items."""
self.pruned_db_map_entity_ids.clear()
self._restore_pruned_menu.clear()
self._spine_db_editor.build_graph()

@Slot(QAction)
def restore_pruned_items(self, action):
"""Reinstates some pruned items."""
key = action.text()
if self.pruned_db_map_entity_ids.pop(key, None) is not None:
action = next(iter(a for a in self._restore_pruned_menu.actions() if a.text() == key))
self._restore_pruned_menu.removeAction(action)
self._spine_db_editor.build_graph()

@Slot(bool)
Expand Down Expand Up @@ -523,12 +509,19 @@ def _set_graph_parameters(
self._spine_db_editor.polish_items()

@Slot(bool)
def save_positions(self, checked=False):
def _save_selected_positions(self, checked=False):
self._save_positions(self.selected_items)

@Slot(bool)
def _save_all_positions(self, checked=False):
self._save_positions(self.entity_items)

def _save_positions(self, items):
if not self.pos_x_parameter or not self.pos_y_parameter:
msg = "You haven't selected the position parameters. Please go to Graph -> Select position parameters"
msg = "You haven't selected the position parameters"
self._spine_db_editor.msg.emit(msg)
return
ent_items = [item for item in self.selected_items if isinstance(item, EntityItem)]
ent_items = [item for item in items if isinstance(item, EntityItem)]
db_map_class_ent_items = {}
for item in ent_items:
for db_map in item.db_maps:
Expand All @@ -553,11 +546,18 @@ def save_positions(self, checked=False):
self.db_mngr.import_data(db_map_data)

@Slot(bool)
def clear_saved_positions(self, checked=False):
if not self.selected_items:
def _clear_selected_positions(self, checked=False):
self._clear_positions(self.selected_items)

@Slot(bool)
def _clear_all_positions(self, checked=False):
self._clear_positions(self.entity_items)

def _clear_positions(self, items):
if not items:
return
db_map_ids = {}
for item in self.selected_items:
for item in items:
db_map_ids.setdefault(item.db_map, set()).add(item.entity_id)
db_map_typed_data = {}
for db_map, ids in db_map_ids.items():
Expand Down
5 changes: 4 additions & 1 deletion spinetoolbox/spine_db_editor/widgets/graph_view_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ def load_graph_data(self, db_map_graph_data):
for db_map, gd in db_map_graph_data.items():
for item_type, ids in gd["selected_item_type_ids"].items():
self._selected_item_type_db_map_ids.setdefault(item_type, {})[db_map] = ids
self.ui.graphicsView.set_pruned_entity_ids(db_map, gd["pruned_entity_ids"])
pruned_db_map_entity_ids = {
(db_map, id_) for db_map, gd in db_map_graph_data.items() for id_ in gd["pruned_entity_ids"]
}
self.ui.graphicsView.set_pruned_db_map_entity_ids("Pruned in loaded state", pruned_db_map_entity_ids)
graph_data = db_map_graph_data[self.first_db_map]
self.ui.graphicsView.pos_x_parameter = graph_data["pos_x_parameter"]
self.ui.graphicsView.pos_y_parameter = graph_data["pos_y_parameter"]
Expand Down

0 comments on commit c9b6c17

Please sign in to comment.