Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor exchange table #271

Merged
merged 8 commits into from
Aug 23, 2019
1 change: 1 addition & 0 deletions activity_browser/app/bwutils/commontasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def is_technosphere_db(db_name):
"Location": "location",
"Database": "database",
"Uncertainty": "uncertainty type",
"Formula": "formula",
}

bw_keys_to_AB_names = {v: k for k, v in AB_names_to_bw_keys.items()}
Expand Down
25 changes: 24 additions & 1 deletion activity_browser/app/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import uuid

import brightway2 as bw
from PyQt5 import QtWidgets
from PyQt5 import QtCore, QtWidgets
from bw2data.backends.peewee import Exchange, sqlite3_lci_db
from bw2data.project import ProjectDataset, SubstitutableDatabase

Expand Down Expand Up @@ -61,6 +61,7 @@ def connect_signals(self):
signals.exchanges_deleted.connect(self.delete_exchanges)
signals.exchanges_add.connect(self.add_exchanges)
signals.exchange_amount_modified.connect(self.modify_exchange_amount)
signals.exchange_modified.connect(self.modify_exchange)
# Calculation Setups
signals.new_calculation_setup.connect(self.new_calculation_setup)
signals.rename_calculation_setup.connect(self.rename_calculation_setup)
Expand Down Expand Up @@ -475,3 +476,25 @@ def modify_exchange_amount(self, exchange, value):
exchange['amount'] = value
exchange.save()
signals.database_changed.emit(exchange['output'][0])

@staticmethod
@QtCore.pyqtSlot(object, str, object)
def modify_exchange(exchange, field, value):
# The formula field needs special handling.
if field == "formula":
if field in exchange and value == "":
# Remove formula entirely.
del exchange[field]
if "original_amount" in exchange:
# Restore the original amount, if possible
exchange["amount"] = exchange["original_amount"]
del exchange["original_amount"]
if value:
# At least set the formula, possibly also store the amount
if field not in exchange:
exchange["original_amount"] = exchange["amount"]
exchange[field] = value
else:
exchange[field] = value
exchange.save()
signals.database_changed.emit(exchange['output'][0])
1 change: 1 addition & 0 deletions activity_browser/app/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Signals(QtCore.QObject):
exchanges_deleted = QtCore.pyqtSignal(list)
exchanges_add = QtCore.pyqtSignal(list, tuple)
exchange_amount_modified = QtCore.pyqtSignal(object, float)
exchange_modified = QtCore.pyqtSignal(object, str, object)

# Calculation Setups
new_calculation_setup = QtCore.pyqtSignal()
Expand Down
3 changes: 2 additions & 1 deletion activity_browser/app/ui/tables/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from .activity import ExchangeTable
from .activity import (BiosphereExchangeTable, DownstreamExchangeTable,
ProductExchangeTable, TechnosphereExchangeTable)
from .history import ActivitiesHistoryTable
from .impact_categories import CFTable, MethodsTable
from .inventory import ActivitiesBiosphereTable, DatabasesTable
Expand Down
Loading