From 2e2f054a4fac2b4501a43238b466b94b9cad911b Mon Sep 17 00:00:00 2001
From: Theophile Terraz <theophile.terraz@inrae.fr>
Date: Thu, 16 Jan 2025 14:16:24 +0100
Subject: [PATCH] debug Adis-TS

---
 src/Model/Pollutants/Pollutants.py            | 14 +++++++++++
 src/Model/Results/ResultsAdisTS.py            |  1 +
 src/View/BoundaryConditionsAdisTS/Table.py    | 19 +++++++++-----
 .../BoundaryConditionsAdisTS/UndoCommand.py   |  3 +--
 src/View/BoundaryConditionsAdisTS/Window.py   |  7 +-----
 src/View/Pollutants/Edit/Table.py             |  5 +---
 src/View/Pollutants/Edit/UndoCommand.py       | 25 +++++++++----------
 src/View/Pollutants/Edit/Window.py            |  2 +-
 src/View/Pollutants/Table.py                  |  2 +-
 src/View/Pollutants/Window.py                 | 23 ++++++++---------
 10 files changed, 56 insertions(+), 45 deletions(-)

diff --git a/src/Model/Pollutants/Pollutants.py b/src/Model/Pollutants/Pollutants.py
index ab41c6fc..7a5523ad 100644
--- a/src/Model/Pollutants/Pollutants.py
+++ b/src/Model/Pollutants/Pollutants.py
@@ -177,5 +177,19 @@ class Pollutants(SQLSubModel):
     def is_define(self):
         return len(self._data) != 0
 
+    def new_from_data(self, data):
+
+        print("from_data before : ", data)
+        try:
+            new = [int(data[0])]
+            new += [float(d) for d in data[1:-1]]
+        except Exception as e:
+            logger.error(e)
+            new = None
+
+        print("from_data after : ", new)
+
+        return new
+
     def __len__(self):
         return len(self._data)
diff --git a/src/Model/Results/ResultsAdisTS.py b/src/Model/Results/ResultsAdisTS.py
index fccfc27e..b77dfdf9 100644
--- a/src/Model/Results/ResultsAdisTS.py
+++ b/src/Model/Results/ResultsAdisTS.py
@@ -46,6 +46,7 @@ class Results(object):
             el.split("/")[-1][0:-4]
             for el in glob.glob(repertory_results + "/*.bin")
             ]
+        mylist.insert(0, mylist.pop(mylist.index("total_sediment")))
 
         self._phys_var_list = ["C", "G", "M", "D", "L", "N", "R"]
 
diff --git a/src/View/BoundaryConditionsAdisTS/Table.py b/src/View/BoundaryConditionsAdisTS/Table.py
index 25cc538b..65752fad 100644
--- a/src/View/BoundaryConditionsAdisTS/Table.py
+++ b/src/View/BoundaryConditionsAdisTS/Table.py
@@ -181,13 +181,20 @@ class TableModel(PamhyrTableModel):
                     )
                 )
             elif self._headers[column] == "pol":
-                pol = next(filter(lambda x: x.name == value,
-                                  self._data._Pollutants.Pollutants_List))
-                self._undo.push(
-                    SetPolCommand(
-                        self._lst, row, pol.id
+                if value == self._trad["not_associated"]:
+                    self._undo.push(
+                        SetPolCommand(
+                            self._lst, row, None
+                        )
+                    )
+                else:
+                    pol = next(filter(lambda x: x.name == value,
+                                    self._data._Pollutants.Pollutants_List))
+                    self._undo.push(
+                        SetPolCommand(
+                            self._lst, row, pol.id
+                        )
                     )
-                )
         except Exception as e:
             logger.info(e)
             logger.debug(traceback.format_exc())
diff --git a/src/View/BoundaryConditionsAdisTS/UndoCommand.py b/src/View/BoundaryConditionsAdisTS/UndoCommand.py
index 56bf0425..ee0a9f26 100644
--- a/src/View/BoundaryConditionsAdisTS/UndoCommand.py
+++ b/src/View/BoundaryConditionsAdisTS/UndoCommand.py
@@ -68,9 +68,8 @@ class SetPolCommand(QUndoCommand):
 
         self._bcs = bcs
         self._index = index
-        self._pollutant = pollutant
         self._old = self._bcs[self._index].pollutant
-        self._new = self._pollutant
+        self._new = pollutant
 
     def undo(self):
         self._bcs[self._index].pollutant = self._old
diff --git a/src/View/BoundaryConditionsAdisTS/Window.py b/src/View/BoundaryConditionsAdisTS/Window.py
index ae501235..3bc0a587 100644
--- a/src/View/BoundaryConditionsAdisTS/Window.py
+++ b/src/View/BoundaryConditionsAdisTS/Window.py
@@ -74,7 +74,6 @@ class BoundaryConditionAdisTSWindow(PamhyrWindow):
         )
 
         self._pollutants_lst = self._study._river._Pollutants
-
         self._bcs = self._study.river.boundary_conditions_adists
 
         self.setup_graph()
@@ -157,11 +156,7 @@ class BoundaryConditionAdisTSWindow(PamhyrWindow):
         )
 
     def add(self):
-        rows = self.index_selected_rows()
-        if len(self._bcs) == 0 or len(rows) == 0:
-            self._table.add(0)
-        else:
-            self._table.add(rows[0])
+        self._table.add(len(self._bcs))
 
     def delete(self):
         rows = self.index_selected_rows()
diff --git a/src/View/Pollutants/Edit/Table.py b/src/View/Pollutants/Edit/Table.py
index aa3c919b..837fe623 100644
--- a/src/View/Pollutants/Edit/Table.py
+++ b/src/View/Pollutants/Edit/Table.py
@@ -90,9 +90,7 @@ class TableModel(PamhyrTableModel):
 
         self.layoutAboutToBeChanged.emit()
 
-        self._undo.push(
-            PasteCommand(
-                self._data, row,
+        self._undo.push(PasteCommand(self._data, row,
                 list(
                     map(
                         lambda d: self._data.new_from_data(d),
@@ -102,5 +100,4 @@ class TableModel(PamhyrTableModel):
             )
         )
 
-        self.layoutAboutToBeChanged.emit()
         self.layoutChanged.emit()
diff --git a/src/View/Pollutants/Edit/UndoCommand.py b/src/View/Pollutants/Edit/UndoCommand.py
index 5405347d..2c4867c7 100644
--- a/src/View/Pollutants/Edit/UndoCommand.py
+++ b/src/View/Pollutants/Edit/UndoCommand.py
@@ -31,36 +31,35 @@ logger = logging.getLogger()
 
 
 class SetDataCommand(QUndoCommand):
-    def __init__(self, data, index, column, new_value):
+    def __init__(self, data, row, column, new_value):
         QUndoCommand.__init__(self)
 
         self._data = data
-        self._index = index
+        self._row = row
         self._column = column
-        self._old = self._data.data[self._index][self._column]
+        self._old = self._data.data[self._row][self._column]
         self._new = new_value
 
     def undo(self):
-        self._data.data[self._index][self._column] = self._old
+        self._data.data[self._row][self._column] = self._old
 
     def redo(self):
-        self._data.data[self._index][self._column] = self._new
+        self._data.data[self._row][self._column] = self._new
 
 
 class PasteCommand(QUndoCommand):
-    def __init__(self, data, row, hs):
+    def __init__(self, data, row, new_data):
         QUndoCommand.__init__(self)
 
         self._data = data
         self._row = row
-        self._h = hs
-        self._h.reverse()
+        self._new = [n for n in new_data[row]]
+        self._old = [o for o in self._data.data[row]]
 
     def undo(self):
-        self._data.delete_i(
-            range(self._row, self._row + len(self._h))
-        )
+        for i in range(9):
+            self._data.data[self._row][i] = self._old[i]
 
     def redo(self):
-        for h in self._h:
-            self._data.insert(self._row, h)
+        for i in range(9):
+            self._data.data[self._row][i] = self._new[i]
diff --git a/src/View/Pollutants/Edit/Window.py b/src/View/Pollutants/Edit/Window.py
index 1f535191..8adb7f9f 100644
--- a/src/View/Pollutants/Edit/Window.py
+++ b/src/View/Pollutants/Edit/Window.py
@@ -129,7 +129,7 @@ class EditPolluantWindow(PamhyrWindow):
     def _paste(self):
         header, data = self.parseClipboardTable()
 
-        logger.debug(f"paste: h:{header}, d:{data}")
+        logger.debug(f"paste: header:{header}, data:{data}")
 
         if len(data) == 0:
             return
diff --git a/src/View/Pollutants/Table.py b/src/View/Pollutants/Table.py
index 44dd9edb..5103ec1a 100644
--- a/src/View/Pollutants/Table.py
+++ b/src/View/Pollutants/Table.py
@@ -103,7 +103,7 @@ class TableModel(PamhyrTableModel):
 
         self._undo.push(
             DelCommand(
-                self._lst, rows, self._data.initial_conditions_adists
+                self._lst, rows, self._data.ic_adists
             )
         )
 
diff --git a/src/View/Pollutants/Window.py b/src/View/Pollutants/Window.py
index 09f8b2fe..37cb36c3 100644
--- a/src/View/Pollutants/Window.py
+++ b/src/View/Pollutants/Window.py
@@ -105,6 +105,7 @@ class PollutantsWindow(PamhyrWindow):
 
     def setup_connections(self):
         self.find(QAction, "action_add").triggered.connect(self.add)
+        self.find(QAction, "action_delete").setEnabled(False)
         self.find(QAction, "action_delete").triggered.connect(self.delete)
         self.find(QAction, "action_edit").triggered.connect(self.edit)
         self.find(QAction, "action_initial_conditions"
@@ -154,11 +155,7 @@ class PollutantsWindow(PamhyrWindow):
         )
 
     def add(self):
-        rows = self.index_selected_rows()
-        if len(self._pollutants_lst) == 0 or len(rows) == 0:
-            self._table.add(0)
-        else:
-            self._table.add(rows[-1])
+        self._table.add(len(self._pollutants_lst))
 
     def delete(self):
         rows = self.index_selected_rows()
@@ -181,6 +178,9 @@ class PollutantsWindow(PamhyrWindow):
 
     def edit(self):
         rows = self.index_selected_rows()
+        if len(rows) == 0:
+            return
+
         for row in rows:
             data = self._pollutants_lst.get(row)
 
@@ -199,6 +199,8 @@ class PollutantsWindow(PamhyrWindow):
 
     def initial_conditions(self):
         rows = self.index_selected_rows()
+        if len(rows) == 0:
+            return
 
         for row in rows:
             pollutant_id = self._pollutants_lst.get(row).id
@@ -219,11 +221,7 @@ class PollutantsWindow(PamhyrWindow):
             )
             initial.show()
 
-    def boundary_conditions(self, tab=0):
-        rows = self.index_selected_rows()
-
-        for row in rows:
-            pollutant_id = self._pollutants_lst.get(row).id
+    def boundary_conditions(self):
 
         if self.sub_window_exists(
             BoundaryConditionAdisTSWindow,
@@ -242,9 +240,10 @@ class PollutantsWindow(PamhyrWindow):
 
     def lateral_contrib(self):
         rows = self.index_selected_rows()
+        if len(rows) == 0:
+            return
 
-        for row in rows:
-            pollutant_id = self._pollutants_lst.get(row).id
+        pollutant_id = self._pollutants_lst.get(rows[0]).id
 
         if self.sub_window_exists(
             LateralContributionAdisTSWindow,
-- 
GitLab