Skip to content

Commit 79656b9

Browse files
committed
CS fixes
1 parent 859060e commit 79656b9

1 file changed

Lines changed: 55 additions & 7 deletions

File tree

activity_browser/layouts/pages/calculation_setup/functional_unit_section.py

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from qtpy import QtWidgets
2+
from qtpy.QtCore import Qt
23

34
import bw2data as bd
45
import pandas as pd
56

67
from activity_browser import actions
7-
from activity_browser.ui import widgets, icons
8-
from activity_browser.bwutils import AB_metadata
8+
from activity_browser.ui import widgets, icons, delegates
9+
from activity_browser.bwutils import AB_metadata, is_node_product
910

1011

1112
class FunctionalUnitSection(QtWidgets.QWidget):
@@ -46,6 +47,7 @@ def build_df(self):
4647
act_df = AB_metadata.get_metadata(keys, cols)
4748
act_df["amount"] = amounts
4849
act_df["_activity_key"] = keys
50+
act_df["_cs_name"] = self.calculation_setup_name
4951

5052
act_df["_processor_key"] = act_df["processor"]
5153
act_df["_processor_key"] = act_df["_processor_key"].fillna(act_df["_activity_key"])
@@ -73,12 +75,15 @@ def build_df(self):
7375
act_df.update(act_df["product"].rename("name"))
7476
act_df["product"] = act_df["name"]
7577

76-
cols = ["amount", "unit", "product", "process", "database", "location", "_processor_key", "_activity_key"]
78+
cols = ["amount", "unit", "product", "process", "database", "location", "_processor_key", "_activity_key", "_cs_name"]
7779

7880
return act_df[cols].reset_index(drop=True)
7981

8082

8183
class FunctionalUnitView(widgets.ABTreeView):
84+
defaultColumnDelegates = {
85+
"amount": delegates.AmountDelegate
86+
}
8287

8388
class ContextMenu(widgets.ABMenu):
8489
menuSetup = [
@@ -119,19 +124,24 @@ def mouseDoubleClickEvent(self, event) -> None:
119124
Args:
120125
event: The mouse double click event.
121126
"""
127+
index = self.indexAt(event.pos())
128+
if index.column() == 0:
129+
return super().mouseDoubleClickEvent(event)
130+
122131
if self.selectedIndexes():
123132
activities = [index.internalPointer()["_processor_key"] for index in self.selectedIndexes()]
124133
actions.ActivityOpen.run(list(set(activities)))
125134

135+
return None
136+
126137
def dragMoveEvent(self, event) -> None:
127138
pass
128139

129140
def dragEnterEvent(self, event):
130141
if event.mimeData().hasFormat("application/bw-nodekeylist"):
131142
keys: list = event.mimeData().retrievePickleData("application/bw-nodekeylist")
132143
for key in keys:
133-
act = bd.get_node(key=key)
134-
if act["type"] not in bd.labels.product_node_types + ["processwithreferenceproduct", "process"]:
144+
if not is_node_product(key):
135145
keys.remove(key)
136146

137147
if not keys:
@@ -145,8 +155,7 @@ def dropEvent(self, event) -> None:
145155

146156
keys: list = event.mimeData().retrievePickleData("application/bw-nodekeylist")
147157
for key in keys:
148-
act = bd.get_node(key=key)
149-
if act["type"] not in bd.labels.product_node_types + ["processwithreferenceproduct", "process"]:
158+
if not is_node_product(key):
150159
keys.remove(key)
151160

152161
actions.CSAddFunctionalUnit.run(cs_name, keys)
@@ -158,6 +167,45 @@ def decorationData(self, col: int, key: str):
158167
return icons.qicons.product
159168
if key == "process":
160169
return icons.qicons.process
170+
return super().decorationData(col, key)
171+
172+
def flags(self, col: int, key: str):
173+
"""
174+
Returns the item flags for the given column and key.
175+
176+
Args:
177+
col (int): The column index.
178+
key (str): The key for which to return the flags.
179+
180+
Returns:
181+
QtCore.Qt.ItemFlags: The item flags.
182+
"""
183+
flags = super().flags(col, key)
184+
if key in ["amount"]:
185+
return flags | Qt.ItemFlag.ItemIsEditable
186+
return flags
187+
188+
def setData(self, col: int, key: str, value) -> bool:
189+
"""
190+
Sets the data for the given column and key.
191+
192+
Args:
193+
col (int): The column index.
194+
key (str): The key for which to set the data.
195+
value: The value to set.
196+
197+
Returns:
198+
bool: True if the data was set successfully, False otherwise.
199+
"""
200+
if key not in ["amount"]:
201+
return False
202+
203+
cs_name = self["_cs_name"]
204+
index = self.key()
205+
206+
actions.CSChangeFunctionalUnit.run(cs_name, index, value)
207+
208+
161209

162210

163211
class FunctionalUnitModel(widgets.ABItemModel):

0 commit comments

Comments
 (0)