Skip to content

Commit ba7eb77

Browse files
committed
Negative values in calculation setup
1 parent 0c4d6eb commit ba7eb77

5 files changed

Lines changed: 46 additions & 8 deletions

File tree

activity_browser/actions/calculation_setup/cs_add_functional_unit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run(cs_name: str, activities: list[tuple | int | bd.Node]):
1616
activities = [bwutils.refresh_node(node) for node in activities]
1717
calculation_setup = bd.calculation_setups[cs_name]
1818

19-
fus = [{act.key: 1.0} for act in activities]
19+
fus = [{act.key: -1.0 if act.get("type") == "waste" else 1.0} for act in activities]
2020
calculation_setup['inv'] += fus
2121

2222
bd.calculation_setups[cs_name] = calculation_setup

activity_browser/layouts/pages/activity_details/exchanges_tab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class ExchangesView(widgets.ABTreeView):
289289
hovered_item (ExchangesItem): The item currently being hovered over.
290290
"""
291291
defaultColumnDelegates = {
292-
"amount": delegates.AmountDelegate,
292+
"amount": delegates.AbsoluteAmountDelegate,
293293
"allocation_factor": delegates.FloatDelegate,
294294
"substitution_factor": delegates.FloatDelegate,
295295
"unit": delegates.StringDelegate,

activity_browser/layouts/pages/calculation_setup/functional_unit_section.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def sync(self):
3737

3838
def build_df(self):
3939
keys, amounts = [], []
40-
cols = ["unit", "name", "product", "location", "database", "processor"]
40+
cols = ["unit", "name", "product", "location", "database", "processor", "type"]
4141

4242
for fu in self.calculation_setup.get("inv", []):
4343
for key, amount in fu.items():
@@ -75,7 +75,9 @@ def build_df(self):
7575
act_df.update(act_df["product"].rename("name"))
7676
act_df["product"] = act_df["name"]
7777

78-
cols = ["amount", "unit", "product", "process", "database", "location", "_processor_key", "_activity_key", "_cs_name"]
78+
act_df.rename({"type": "_type"}, axis="columns", inplace=True)
79+
80+
cols = ["amount", "unit", "product", "process", "database", "location", "_processor_key", "_activity_key", "_cs_name", "_type"]
7981

8082
return act_df[cols].reset_index(drop=True)
8183

@@ -163,7 +165,11 @@ def dropEvent(self, event) -> None:
163165

164166
class FunctionalUnitItem(widgets.ABDataItem):
165167
def decorationData(self, col: int, key: str):
166-
if key == "product":
168+
if key == "product" and self["_type"] == "waste":
169+
return icons.qicons.waste
170+
elif key == "product" and self["type"] == "processwithreferenceproduct":
171+
return icons.qicons.processproduct
172+
elif key == "product":
167173
return icons.qicons.product
168174
if key == "process":
169175
return icons.qicons.process

activity_browser/ui/delegates/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
from .new_formula import NewFormulaDelegate
1515
from .date_time import DateTimeDelegate
1616
from .property import PropertyDelegate
17-
from .amount import AmountDelegate
17+
from .amount import AmountDelegate, AbsoluteAmountDelegate
1818

1919
__all__ = [
2020
"AmountDelegate",
21+
"AbsoluteAmountDelegate",
2122
"CheckboxDelegate",
2223
"ComboBoxDelegate",
2324
"DatabaseDelegate",

activity_browser/ui/delegates/amount.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def displayText(self, value, locale):
1010

1111
if math.isnan(value):
1212
return ""
13-
return str(abs(value))
13+
return str(value)
1414

1515
def createEditor(self, parent, option, index):
1616
editor = QtWidgets.QLineEdit(parent)
@@ -23,6 +23,37 @@ def createEditor(self, parent, option, index):
2323
editor.setValidator(validator)
2424
return editor
2525

26+
def setEditorData(self, editor: QtWidgets.QLineEdit, index: QtCore.QModelIndex):
27+
"""Populate the editor with data if editing an existing field."""
28+
import math
29+
30+
data = index.data(QtCore.Qt.DisplayRole)
31+
32+
try:
33+
value = float(data)
34+
except ValueError:
35+
value = math.nan
36+
37+
editor.setText(format(value, '.10f').rstrip('0').rstrip('.'))
38+
39+
def setModelData(
40+
self,
41+
editor: QtWidgets.QLineEdit,
42+
model: QtCore.QAbstractItemModel,
43+
index: QtCore.QModelIndex,
44+
):
45+
"""Take the editor, read the given value and set it in the model"""
46+
try:
47+
value = float(editor.text())
48+
model.setData(index, value, QtCore.Qt.EditRole)
49+
except ValueError:
50+
pass
51+
52+
53+
class AbsoluteAmountDelegate(AmountDelegate):
54+
def displayText(self, value, locale):
55+
return str(abs(float(super().displayText(value, locale))))
56+
2657
def setEditorData(self, editor: QtWidgets.QLineEdit, index: QtCore.QModelIndex):
2758
"""Populate the editor with data if editing an existing field."""
2859
import math
@@ -52,4 +83,4 @@ def setModelData(
5283

5384
model.setData(index, value, QtCore.Qt.EditRole)
5485
except ValueError:
55-
pass
86+
pass

0 commit comments

Comments
 (0)