|
10 | 10 | from PySide2.QtCore import Slot |
11 | 11 |
|
12 | 12 | from activity_browser import signals |
13 | | -from activity_browser.mod.bw2data import Database, get_activity |
| 13 | +from activity_browser.mod.bw2data import Database, get_activity, databases, Edge |
| 14 | +from activity_browser.mod.bw2data.backends import ExchangeDataset, ActivityDataset |
14 | 15 |
|
15 | | -from ...bwutils.commontasks import identify_activity_type |
| 16 | +from ...bwutils.commontasks import identify_activity_type, get_activity_name |
16 | 17 | from .base import BaseGraph, BaseNavigatorWidget |
17 | 18 |
|
18 | 19 | log = getLogger(__name__) |
@@ -60,6 +61,9 @@ class GraphNavigatorWidget(BaseNavigatorWidget): |
60 | 61 |
|
61 | 62 | def __init__(self, parent=None, key=None): |
62 | 63 | super().__init__(parent, css_file="navigator.css") |
| 64 | + self.setObjectName(get_activity_name(get_activity(key), str_length=30)) |
| 65 | + self.key = key |
| 66 | + self.tab = parent |
63 | 67 |
|
64 | 68 | self.graph = Graph() |
65 | 69 |
|
@@ -113,6 +117,17 @@ def connect_signals(self): |
113 | 117 | self.update_graph_settings |
114 | 118 | ) |
115 | 119 | self.checkbox_flip_negative_edges.stateChanged.connect(self.reload_graph) |
| 120 | + databases.metadata_changed.connect(self.sync_graph) |
| 121 | + |
| 122 | + def sync_graph(self): |
| 123 | + """Sync the graph with the current project.""" |
| 124 | + self.graph.update(delete_unstacked=False) |
| 125 | + self.send_json() |
| 126 | + try: |
| 127 | + self.setObjectName(get_activity_name(get_activity(self.key), str_length=30)) |
| 128 | + except ActivityDataset.DoesNotExist: |
| 129 | + log.debug("Graph activity no longer exists. Closing tab.") |
| 130 | + self.tab.close_tab_by_tab_name(self.tab.get_tab_name(self)) |
116 | 131 |
|
117 | 132 | def construct_layout(self) -> None: |
118 | 133 | """Layout of Graph Navigator""" |
@@ -251,9 +266,24 @@ def __init__(self): |
251 | 266 | self.flip_negative_edges = False # show true flow direction of edges (e.g. for ecoinvent treatment activities, or substitutions) |
252 | 267 |
|
253 | 268 | def update(self, delete_unstacked: bool = True) -> None: |
| 269 | + self.update_datasets() |
254 | 270 | super().update(delete_unstacked) |
255 | 271 | self.json_data = self.get_json_data() |
256 | 272 |
|
| 273 | + def update_datasets(self): |
| 274 | + """Update the activities in the graph.""" |
| 275 | + try: |
| 276 | + self.nodes = [get_activity(act.key) for act in self.nodes] |
| 277 | + self.edges = [Edge(document=ExchangeDataset.get_by_id(exc._document.id)) for exc in self.edges] |
| 278 | + except (ActivityDataset.DoesNotExist, ExchangeDataset.DoesNotExist): |
| 279 | + try: |
| 280 | + get_activity(self.central_activity.key) # test whether the activity still exists |
| 281 | + self.new_graph(self.central_activity.key) # if so, create a new graph |
| 282 | + except ActivityDataset.DoesNotExist: |
| 283 | + log.warning("Graph activity no longer exists.") |
| 284 | + self.nodes = [] |
| 285 | + self.edges = [] |
| 286 | + |
257 | 287 | def store_previous(self) -> None: |
258 | 288 | self.stack.append((deepcopy(self.nodes), deepcopy(self.edges))) |
259 | 289 |
|
|
0 commit comments