From b927aa81b30c5af92f85f0929686dfab23384282 Mon Sep 17 00:00:00 2001
From: Youcef AOUAD <youcef.aouad@inrae.fr>
Date: Mon, 17 Jun 2024 16:34:41 +0200
Subject: [PATCH] BC AdisTS View Table

---
 src/View/BoundaryConditionsAdisTS/Table.py    | 113 +++++-------------
 .../BoundaryConditionsAdisTS/UndoCommand.py   |  69 ++++-------
 src/View/BoundaryConditionsAdisTS/Window.py   |  50 ++++----
 .../BoundaryConditionsAdisTS/translate.py     |  10 --
 src/View/Pollutants/Window.py                 |  17 ++-
 src/View/ui/BoundaryConditionsAdisTS.ui       |  62 ++++------
 6 files changed, 110 insertions(+), 211 deletions(-)

diff --git a/src/View/BoundaryConditionsAdisTS/Table.py b/src/View/BoundaryConditionsAdisTS/Table.py
index eec695cb..d2b08caf 100644
--- a/src/View/BoundaryConditionsAdisTS/Table.py
+++ b/src/View/BoundaryConditionsAdisTS/Table.py
@@ -41,8 +41,8 @@ from Model.BoundaryCondition.BoundaryConditionTypes import (
 
 from View.Tools.PamhyrTable import PamhyrTableModel
 
-from View.BoundaryCondition.UndoCommand import (
-    SetNameCommand, SetNodeCommand, SetTypeCommand,
+from View.BoundaryConditionsAdisTS.UndoCommand import (
+    SetNodeCommand, SetTypeCommand,
     AddCommand, DelCommand, SortCommand,
     MoveCommand, PasteCommand,
 )
@@ -71,15 +71,7 @@ class ComboBoxDelegate(QItemDelegate):
         self.editor = QComboBox(parent)
 
         if self._mode == "type":
-            lst = list(
-                map(
-                    lambda k: self._long_types[k],
-                    filter(
-                        lambda k: self._tab in BC_types[k].compatibility(),
-                        BC_types.keys()
-                    )
-                )
-            )
+            lst = [self._trad["not_associated"], "Concentration", "Rate"]
             self.editor.addItems(
                 lst
             )
@@ -115,38 +107,45 @@ class ComboBoxDelegate(QItemDelegate):
 
 
 class TableModel(PamhyrTableModel):
-    def __init__(self, trad=None, **kwargs):
+    def __init__(self, pollutant=None, bc_list =None, trad=None, **kwargs):
         self._trad = trad
-        self._long_types = {}
-        if self._trad is not None:
-            self._long_types = self._trad.get_dict("long_types")
+        self._bc_list = bc_list
+        self._pollutant = pollutant
+        print("pollutant : ", self._pollutant)
 
         super(TableModel, self).__init__(trad=trad, **kwargs)
 
     def _setup_lst(self):
-        self._lst = self._data.boundary_condition
-        self._tab = self._opt_data
+        self._lst = self._bc_list.lst
 
     def rowCount(self, parent):
-        return self._lst.len(self._tab)
+        return len(self._lst)
 
     def data(self, index, role):
+        if len(self._lst) != 0:
+            data = list(filter(lambda x: x.pollutant == self._pollutant,
+                                     self._lst))
+        else:
+            data = []
+
         if role != Qt.ItemDataRole.DisplayRole:
             return QVariant()
 
         row = index.row()
         column = index.column()
 
-        if self._headers[column] == "name":
-            return self._lst.get(self._tab, row).name
-        elif self._headers[column] == "type":
-            t = self._lst.get(self._tab, row).bctype
-            return self._long_types[t]
+        if self._headers[column] == "type":
+            #n = self._lst[row].type
+            n = data[row].type
+            if n is None or n == "":
+                return self._trad["not_associated"]
+            return n
         elif self._headers[column] == "node":
-            n = self._lst.get(self._tab, row).node
+            #n = self._lst[row].node
+            n = data[row].node
             if n is None:
                 return self._trad["not_associated"]
-            return n.name
+            return n
 
         return QVariant()
 
@@ -158,24 +157,16 @@ class TableModel(PamhyrTableModel):
         column = index.column()
 
         try:
-            if self._headers[column] == "name":
-                self._undo.push(
-                    SetNameCommand(
-                        self._lst, self._tab, row, value
-                    )
-                )
-            elif self._headers[column] == "type":
-                key = next(k for k, v in self._long_types.items()
-                           if v == value)
+            if self._headers[column] == "type":
                 self._undo.push(
                     SetTypeCommand(
-                        self._lst, self._tab, row, BC_types[key]
+                        self._lst, row, value
                     )
                 )
             elif self._headers[column] == "node":
                 self._undo.push(
                     SetNodeCommand(
-                        self._lst, self._tab, row, self._data.node(value)
+                        self._lst, row, self._data.node(value)
                     )
                 )
         except Exception as e:
@@ -190,7 +181,7 @@ class TableModel(PamhyrTableModel):
 
         self._undo.push(
             AddCommand(
-                self._lst, self._tab, row
+                self._pollutant, self._bc_list, self._lst, row
             )
         )
 
@@ -202,59 +193,13 @@ class TableModel(PamhyrTableModel):
 
         self._undo.push(
             DelCommand(
-                self._lst, self._tab, rows
+                self._lst, rows
             )
         )
 
         self.endRemoveRows()
         self.layoutChanged.emit()
 
-    def sort(self, _reverse, parent=QModelIndex()):
-        self.layoutAboutToBeChanged.emit()
-
-        self._undo.push(
-            SortCommand(
-                self._lst, self._tab, False
-            )
-        )
-
-        self.layoutAboutToBeChanged.emit()
-        self.layoutChanged.emit()
-
-    def move_up(self, row, parent=QModelIndex()):
-        if row <= 0:
-            return
-
-        target = row + 2
-
-        self.beginMoveRows(parent, row - 1, row - 1, parent, target)
-
-        self._undo_stack.push(
-            MoveCommand(
-                self._lst, self._tab, "up", row
-            )
-        )
-
-        self.endMoveRows()
-        self.layoutChanged.emit()
-
-    def move_down(self, index, parent=QModelIndex()):
-        if row > len(self._lst):
-            return
-
-        target = row
-
-        self.beginMoveRows(parent, row + 1, row + 1, parent, target)
-
-        self._undo_stack.push(
-            MoveCommand(
-                self._lst, self._tab, "down", row
-            )
-        )
-
-        self.endMoveRows()
-        self.layoutChanged.emit()
-
     def undo(self):
         self._undo.undo()
         self.layoutChanged.emit()
diff --git a/src/View/BoundaryConditionsAdisTS/UndoCommand.py b/src/View/BoundaryConditionsAdisTS/UndoCommand.py
index ee3b45df..98fff994 100644
--- a/src/View/BoundaryConditionsAdisTS/UndoCommand.py
+++ b/src/View/BoundaryConditionsAdisTS/UndoCommand.py
@@ -23,87 +23,62 @@ from PyQt5.QtWidgets import (
     QMessageBox, QUndoCommand, QUndoStack,
 )
 
-from Model.BoundaryCondition.BoundaryCondition import BoundaryCondition
-from Model.BoundaryCondition.BoundaryConditionList import BoundaryConditionList
-
-
-class SetNameCommand(QUndoCommand):
-    def __init__(self, bcs, tab, index, new_value):
-        QUndoCommand.__init__(self)
-
-        self._bcs = bcs
-        self._tab = tab
-        self._index = index
-        self._old = self._bcs.get(self._tab, self._index).name
-        self._new = str(new_value)
-
-    def undo(self):
-        self._bcs.get(self._tab, self._index).name = self._old
-
-    def redo(self):
-        self._bcs.get(self._tab, self._index).name = self._new
-
+from Model.BoundaryConditionsAdisTS.BoundaryConditionAdisTS import BoundaryConditionAdisTS
+from Model.BoundaryConditionsAdisTS.BoundaryConditionsAdisTSList import BoundaryConditionsAdisTSList
 
 class SetNodeCommand(QUndoCommand):
-    def __init__(self, bcs, tab, index, node):
+    def __init__(self, bcs, index, node):
         QUndoCommand.__init__(self)
 
         self._bcs = bcs
-        self._tab = tab
         self._index = index
-        self._old = self._bcs.get(self._tab, self._index).node
-        self._new = node
-        self._prev_assoc_to_node = self._bcs.get_assoc_to_node(tab, node)
-
-    def _previous_assoc_node(self, node):
-        if self._prev_assoc_to_node is not None:
-            self._prev_assoc_to_node.node = node
+        self._old = self._bcs[self._index].node
+        self._new = node.id
 
     def undo(self):
-        self._bcs.get(self._tab, self._index).node = self._old
-        self._previous_assoc_node(self._new)
+        self._bcs[self._index].node = self._old
 
     def redo(self):
-        self._bcs.get(self._tab, self._index).node = self._new
-        self._previous_assoc_node(None)
-
+        self._bcs[self._index].node = self._new
 
 class SetTypeCommand(QUndoCommand):
-    def __init__(self, bcs, tab, index, _type):
+    def __init__(self, bcs, index, _type):
         QUndoCommand.__init__(self)
 
         self._bcs = bcs
-        self._tab = tab
         self._index = index
         self._type = _type
-        self._old = self._bcs.get(self._tab, self._index)
-        self._new = self._bcs.get(self._tab, self._index)\
-                             .convert(self._type)
+        self._old = self._bcs[self._index].type
+        self._new = self._type
 
     def undo(self):
-        self._bcs.set(self._tab, self._index, self._old)
+        self._bcs[self._index].type = self._old
 
     def redo(self):
-        self._bcs.set(self._tab, self._index, self._new)
-
+        self._bcs[self._index].type = self._new
+        print("type : ", self._old, self._new, self._bcs[self._index].type)
 
 class AddCommand(QUndoCommand):
-    def __init__(self, bcs, tab, index):
+    def __init__(self, pollutant, bcs_list, bcs, index):
         QUndoCommand.__init__(self)
 
         self._bcs = bcs
-        self._tab = tab
+        self._bc_list = bcs_list
+        self._pollutant = pollutant
         self._index = index
         self._new = None
 
     def undo(self):
-        self._bcs.delete_i(self._tab, [self._index])
+        del self._bcs[self._index]
 
     def redo(self):
         if self._new is None:
-            self._new = self._bcs.new(self._tab, self._index)
+            self._new = self._bc_list.new(self._index, self._pollutant)
+            print(self._new.pollutant)
+            print(len(self._bc_list))
+            print(len(self._bcs))
         else:
-            self._bcs.insert(self._tab, self._index, self._new)
+            self._bcs.insert(self._index, self._new)
 
 
 class DelCommand(QUndoCommand):
diff --git a/src/View/BoundaryConditionsAdisTS/Window.py b/src/View/BoundaryConditionsAdisTS/Window.py
index 5aae8ffb..8b825080 100644
--- a/src/View/BoundaryConditionsAdisTS/Window.py
+++ b/src/View/BoundaryConditionsAdisTS/Window.py
@@ -64,18 +64,20 @@ _translate = QCoreApplication.translate
 logger = logging.getLogger()
 
 
-class BoundaryConditionWindow(PamhyrWindow):
+class BoundaryConditionAdisTSWindow(PamhyrWindow):
     _pamhyr_ui = "BoundaryConditionsAdisTS"
     _pamhyr_name = "Boundary conditions AdisTS"
 
-    def __init__(self, study=None, config=None, parent=None):
+    def __init__(self, pollutant=None, study=None, config=None, parent=None):
         trad = BCAdisTSTranslate()
         name = (
             trad[self._pamhyr_name] +
             " - " + study.name
         )
 
-        super(BoundaryConditionWindow, self).__init__(
+        self._pollutant = pollutant
+
+        super(BoundaryConditionAdisTSWindow, self).__init__(
             title=name,
             study=study,
             config=config,
@@ -83,7 +85,7 @@ class BoundaryConditionWindow(PamhyrWindow):
             parent=parent
         )
 
-        self._bcs = self._study.river.boundary_condition
+        self._bcs = self._study.river.boundary_conditions_adists
 
         self.setup_table()
         self.setup_graph()
@@ -92,40 +94,34 @@ class BoundaryConditionWindow(PamhyrWindow):
         self.ui.setWindowTitle(self._title)
 
     def setup_table(self):
-        self._table = {}
-
-        t = "liquid"
-
         self._delegate_type = ComboBoxDelegate(
             trad=self._trad,
             data=self._study.river,
             mode="type",
-            tab=t,
             parent=self
         )
         self._delegate_node = ComboBoxDelegate(
             trad=self._trad,
             data=self._study.river,
             mode="node",
-            tab=t,
             parent=self
         )
 
-        table = self.find(QTableView, f"tableView_{t}")
-        self._table[t] = TableModel(
+        table = self.find(QTableView, f"tableView")
+        self._table = TableModel(
             table_view=table,
             table_headers=self._trad.get_dict("table_headers"),
-            editable_headers=["name", "type", "node"],
+            editable_headers=["type", "node"],
             delegates={
                 "type": self._delegate_type,
                 "node": self._delegate_node,
             },
             trad=self._trad,
-            data=self._study.river,
+            bc_list = self._study.river.boundary_conditions_adists,
             undo=self._undo_stack,
-            opt_data=t,
+            pollutant=self._pollutant
         )
-        table.setModel(self._table[t])
+        table.setModel(self._table)
         table.setSelectionBehavior(QAbstractItemView.SelectRows)
         table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
         table.setAlternatingRowColors(True)
@@ -154,8 +150,7 @@ class BoundaryConditionWindow(PamhyrWindow):
                     .row()
 
     def index_selected_rows(self):
-        tab = "liquid"
-        table = self.find(QTableView, f"tableView_{tab}")
+        table = self.find(QTableView, f"tableView")
         return list(
             # Delete duplicate
             set(
@@ -167,12 +162,11 @@ class BoundaryConditionWindow(PamhyrWindow):
         )
 
     def add(self):
-        tab = "liquid"
         rows = self.index_selected_rows()
-        if self._bcs.len(tab) == 0 or len(rows) == 0:
-            self._table[tab].add(0)
+        if len(self._bcs) == 0 or len(rows) == 0:
+            self._table.add(0)
         else:
-            self._table[tab].add(rows[0])
+            self._table.add(rows[0])
 
     def delete(self):
         tab = "liquid"
@@ -180,21 +174,21 @@ class BoundaryConditionWindow(PamhyrWindow):
         if len(rows) == 0:
             return
 
-        self._table[tab].delete(rows)
+        self._table.delete(rows)
 
     def sort(self):
         tab = "liquid"
-        self._table[tab].sort(False)
+        self._table.sort(False)
 
     def move_up(self):
         tab = "liquid"
         row = self.index_selected_row()
-        self._table[tab].move_up(row)
+        self._table.move_up(row)
 
     def move_down(self):
         tab = "liquid"
         row = self.index_selected_row()
-        self._table[tab].move_down(row)
+        self._table.move_down(row)
 
     def _copy(self):
         logger.info("TODO: copy")
@@ -204,11 +198,11 @@ class BoundaryConditionWindow(PamhyrWindow):
 
     def _undo(self):
         tab = "liquid"
-        self._table[tab].undo()
+        self._table.undo()
 
     def _redo(self):
         tab = "liquid"
-        self._table[tab].redo()
+        self._table.redo()
 
     def edit(self):
         tab = "liquid"
diff --git a/src/View/BoundaryConditionsAdisTS/translate.py b/src/View/BoundaryConditionsAdisTS/translate.py
index 967bea50..a0974318 100644
--- a/src/View/BoundaryConditionsAdisTS/translate.py
+++ b/src/View/BoundaryConditionsAdisTS/translate.py
@@ -30,17 +30,7 @@ class BCAdisTSTranslate(MainTranslate):
             "BoundaryConditionsAdisTS", "Boundary conditions AdisTS"
         )
 
-        self._sub_dict["long_types"] = {
-            "ND": self._dict["not_defined"],
-            "PC": _translate("BoundaryCondition", "Ponctual contribution"),
-            "TZ": _translate("BoundaryCondition", "Z(t)"),
-            "TD": _translate("BoundaryCondition", "Q(t)"),
-            "ZD": _translate("BoundaryCondition", "Q(Z)"),
-            "SL": _translate("BoundaryCondition", "Solid"),
-        }
-
         self._sub_dict["table_headers"] = {
-            "name": self._dict["name"],
             "type": self._dict["type"],
             "node": _translate("BoundaryCondition", "Node")
         }
diff --git a/src/View/Pollutants/Window.py b/src/View/Pollutants/Window.py
index 0914d045..d9ee260e 100644
--- a/src/View/Pollutants/Window.py
+++ b/src/View/Pollutants/Window.py
@@ -44,7 +44,7 @@ from View.Pollutants.Translate import PollutantsTranslate
 from View.Pollutants.Edit.Window import EditPolluantWindow
 
 from View.InitialConditionsAdisTS.Window import InitialConditionsAdisTSWindow
-from View.BoundaryConditionsAdisTS.Window import BoundaryConditionWindow
+from View.BoundaryConditionsAdisTS.Window import BoundaryConditionAdisTSWindow
 
 logger = logging.getLogger()
 
@@ -214,17 +214,22 @@ 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
+
         if self.sub_window_exists(
-            BoundaryConditionWindow,
-            data=[self._study, None]
+            BoundaryConditionAdisTSWindow,
+            data=[self._study, None, pollutant_id]
         ):
             bound = self.get_sub_window(
-                BoundaryConditionWindow,
-                data=[self._study, None]
+                BoundaryConditionAdisTSWindow,
+                data=[self._study, None, ollutant_id]
             )
             return
 
-        bound = BoundaryConditionWindow(study=self._study, parent=self)
+        bound = BoundaryConditionAdisTSWindow(study=self._study, pollutant=pollutant_id, parent=self)
         bound.show()
 
     def _set_checkbox_state(self):
diff --git a/src/View/ui/BoundaryConditionsAdisTS.ui b/src/View/ui/BoundaryConditionsAdisTS.ui
index b54d77f6..e9ceb0f3 100644
--- a/src/View/ui/BoundaryConditionsAdisTS.ui
+++ b/src/View/ui/BoundaryConditionsAdisTS.ui
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>800</width>
-    <height>450</height>
+    <width>861</width>
+    <height>498</height>
    </rect>
   </property>
   <property name="sizePolicy">
@@ -23,46 +23,36 @@
    <locale language="English" country="Europe"/>
   </property>
   <widget class="QWidget" name="centralwidget">
-   <layout class="QGridLayout" name="gridLayout">
-    <item row="0" column="0">
-     <widget class="QSplitter" name="splitter">
-      <property name="orientation">
-       <enum>Qt::Horizontal</enum>
-      </property>
-      <widget class="QTabWidget" name="tabWidget">
-       <property name="minimumSize">
-        <size>
-         <width>300</width>
-         <height>0</height>
-        </size>
-       </property>
-       <property name="currentIndex">
-        <number>0</number>
-       </property>
-       <widget class="QWidget" name="tab_liquid">
-        <attribute name="title">
-         <string>Liquid</string>
-        </attribute>
-        <layout class="QGridLayout" name="gridLayout_2">
-         <item row="0" column="0">
-          <widget class="QTableView" name="tableView_liquid"/>
-         </item>
-        </layout>
-       </widget>
-      </widget>
-      <widget class="QWidget" name="verticalLayoutWidget">
-       <layout class="QVBoxLayout" name="verticalLayout"/>
-      </widget>
-     </widget>
-    </item>
-   </layout>
+   <widget class="QSplitter" name="splitter">
+    <property name="geometry">
+     <rect>
+      <x>20</x>
+      <y>10</y>
+      <width>811</width>
+      <height>421</height>
+     </rect>
+    </property>
+    <property name="orientation">
+     <enum>Qt::Horizontal</enum>
+    </property>
+    <widget class="QWidget" name="verticalLayoutWidget_2">
+     <layout class="QVBoxLayout" name="verticalLayout_2">
+      <item>
+       <widget class="QTableView" name="tableView"/>
+      </item>
+     </layout>
+    </widget>
+    <widget class="QWidget" name="verticalLayoutWidget">
+     <layout class="QVBoxLayout" name="verticalLayout"/>
+    </widget>
+   </widget>
   </widget>
   <widget class="QMenuBar" name="menubar">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>800</width>
+     <width>861</width>
      <height>22</height>
     </rect>
    </property>
-- 
GitLab