From d36ffb348dbf45214591fc78a5a994ca83468dc9 Mon Sep 17 00:00:00 2001 From: marc-vdm Date: Mon, 7 Feb 2022 12:47:45 +0100 Subject: [PATCH] Show/Hide comment field for exchanges in the Techno/Biosphere. Fixes requests #706 and #583 --- activity_browser/bwutils/commontasks.py | 1 + activity_browser/layouts/tabs/activity.py | 13 +++++++++++++ activity_browser/ui/tables/activity.py | 16 +++++++++++++++- activity_browser/ui/tables/models/activity.py | 8 +++++--- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/activity_browser/bwutils/commontasks.py b/activity_browser/bwutils/commontasks.py index 2b38b9be3..6bab4ba82 100644 --- a/activity_browser/bwutils/commontasks.py +++ b/activity_browser/bwutils/commontasks.py @@ -171,6 +171,7 @@ def count_database_records(name: str) -> int: "Formula": "formula", "Categories": "categories", "Type": "type", + "Comment": "comment", } bw_keys_to_AB_names = {v: k for k, v in AB_names_to_bw_keys.items()} diff --git a/activity_browser/layouts/tabs/activity.py b/activity_browser/layouts/tabs/activity.py index efe4e57e3..5948ff81a 100644 --- a/activity_browser/layouts/tabs/activity.py +++ b/activity_browser/layouts/tabs/activity.py @@ -110,11 +110,18 @@ def __init__(self, key: tuple, parent=None, read_only=True): self.show_uncertainty.setChecked(False) self.show_uncertainty.toggled.connect(self.show_exchange_uncertainty) + # Reveal/hide exchange comment columns + self.show_comment = QtWidgets.QCheckBox("Show Exchange comments") + self.show_comment.setToolTip("Show or hide the comment column in the Technosphere Inputs and Biosphere Flows tables") + self.show_comment.setChecked(False) + self.show_comment.toggled.connect(self.show_comments) + # Toolbar Layout toolbar = QtWidgets.QToolBar() toolbar.addWidget(self.checkbox_edit_act) toolbar.addWidget(self.checkbox_activity_description) toolbar.addWidget(self.show_uncertainty) + toolbar.addWidget(self.show_comment) self.graph_action = toolbar.addAction( qicons.graph_explorer, "Show graph", self.open_graph ) @@ -187,6 +194,7 @@ def populate(self) -> None: self.downstream.model.sync(self.activity.upstream()) self.show_exchange_uncertainty(self.show_uncertainty.isChecked()) + self.show_comments(self.show_comment.isChecked()) def populate_description_box(self): """Populate the activity description.""" @@ -210,6 +218,11 @@ def show_exchange_uncertainty(self, toggled: bool) -> None: self.technosphere.show_uncertainty(toggled) self.biosphere.show_uncertainty(toggled) + @Slot(bool, name="toggleCommentColumn") + def show_comments(self, toggled: bool) -> None: + self.technosphere.show_comments(toggled) + self.biosphere.show_comments(toggled) + @Slot(bool, name="toggleReadOnly") def act_read_only_changed(self, read_only: bool) -> None: """ When read_only=False specific data fields in the tables below become user-editable diff --git a/activity_browser/ui/tables/activity.py b/activity_browser/ui/tables/activity.py index e9cf6ace6..806118365 100644 --- a/activity_browser/ui/tables/activity.py +++ b/activity_browser/ui/tables/activity.py @@ -148,6 +148,7 @@ def __init__(self, parent=None): self.setItemDelegateForColumn(0, FloatDelegate(self)) self.setItemDelegateForColumn(6, ViewOnlyUncertaintyDelegate(self)) self.setItemDelegateForColumn(13, FormulaDelegate(self)) + self.setItemDelegateForColumn(14, StringDelegate(self)) self.setDragDropMode(QtWidgets.QTableView.DragDrop) self.table_name = "technosphere" @@ -167,6 +168,12 @@ def show_uncertainty(self, show: bool = False) -> None: for c in self.model.UNCERTAINTY: self.setColumnHidden(cols.index(c), not show) + def show_comments(self, show: bool = False) -> None: + """Show or hide the comment column. + """ + cols = self.model.columns + self.setColumnHidden(cols.index("Comment"), not show) + def contextMenuEvent(self, event) -> None: if self.indexAt(event.pos()).row() == -1: return @@ -204,8 +211,9 @@ def __init__(self, parent=None): self.setItemDelegateForColumn(0, FloatDelegate(self)) self.setItemDelegateForColumn(5, ViewOnlyUncertaintyDelegate(self)) self.setItemDelegateForColumn(12, FormulaDelegate(self)) - self.table_name = "biosphere" + self.setItemDelegateForColumn(13, StringDelegate(self)) self.setDragDropMode(QtWidgets.QTableView.DropOnly) + self.table_name = "biosphere" @Slot(name="resizeView") def custom_view_sizing(self) -> None: @@ -221,6 +229,12 @@ def show_uncertainty(self, show: bool = False) -> None: for c in self.model.UNCERTAINTY: self.setColumnHidden(cols.index(c), not show) + def show_comments(self, show: bool = False) -> None: + """Show or hide the comment column. + """ + cols = self.model.columns + self.setColumnHidden(cols.index("Comment"), not show) + def contextMenuEvent(self, event) -> None: if self.indexAt(event.pos()).row() == -1: return diff --git a/activity_browser/ui/tables/models/activity.py b/activity_browser/ui/tables/models/activity.py index e2418ed30..297f2e223 100644 --- a/activity_browser/ui/tables/models/activity.py +++ b/activity_browser/ui/tables/models/activity.py @@ -22,7 +22,7 @@ class BaseExchangeModel(EditablePandasModel): # Fields accepted by brightway to be stored in exchange objects. VALID_FIELDS = { "amount", "formula", "uncertainty type", "loc", "scale", "shape", - "minimum", "maximum" + "minimum", "maximum", "comment" } def __init__(self, key=None, parent=None): @@ -204,7 +204,7 @@ def create_row(self, exchange) -> dict: class TechnosphereExchangeModel(BaseExchangeModel): COLUMNS = [ "Amount", "Unit", "Product", "Activity", "Location", "Database", - "Uncertainty", "Formula" + "Uncertainty", "Formula", "Comment" ] UNCERTAINTY = [ "loc", "scale", "shape", "minimum", "maximum" @@ -228,6 +228,7 @@ def create_row(self, exchange: ExchangeProxyBase) -> dict: "Database": act.get("database"), "Uncertainty": exchange.get("uncertainty type", 0), "Formula": exchange.get("formula"), + "Comment": exchange.get("comment"), }) try: matrix = PedigreeMatrix.from_dict(exchange.get("pedigree", {})) @@ -246,7 +247,7 @@ def create_row(self, exchange: ExchangeProxyBase) -> dict: class BiosphereExchangeModel(BaseExchangeModel): COLUMNS = [ "Amount", "Unit", "Flow Name", "Compartments", "Database", - "Uncertainty", "Formula" + "Uncertainty", "Formula", "Comment" ] UNCERTAINTY = [ "loc", "scale", "shape", "minimum", "maximum" @@ -269,6 +270,7 @@ def create_row(self, exchange) -> dict: "Database": act.get("database"), "Uncertainty": exchange.get("uncertainty type", 0), "Formula": exchange.get("formula"), + "Comment": exchange.get("comment"), }) try: matrix = PedigreeMatrix.from_dict(exchange.get("pedigree", {}))