From cd68bebf0363237a3516d67a5481302cc437ae75 Mon Sep 17 00:00:00 2001
From: Youcef AOUAD <youcef.aouad@inrae.fr>
Date: Wed, 12 Jun 2024 15:06:28 +0200
Subject: [PATCH] INI SPEC Table

---
 .../InitialConditionsAdisTS.py                | 28 ++++++
 src/View/InitialConditionsAdisTS/Table.py     | 36 +++++---
 .../InitialConditionsAdisTS/UndoCommand.py    | 86 +++++++++++++++++--
 src/View/InitialConditionsAdisTS/Window.py    | 16 ++--
 4 files changed, 145 insertions(+), 21 deletions(-)

diff --git a/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTS.py b/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTS.py
index f21d1591..f8122223 100644
--- a/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTS.py
+++ b/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTS.py
@@ -243,6 +243,34 @@ class InitialConditionsAdisTS(SQLSubModel):
         self._enabled = enabled
         self._status.modified()
 
+    def new(self, index):
+        n = ICAdisTSSpec(status=self._status)
+        self._data.insert(index, n)
+        self._status.modified()
+        return n
+
+    def delete(self, data):
+        self._data = list(
+            filter(
+                lambda x: x not in data,
+                self._data
+            )
+        )
+        self._status.modified()
+
+    def delete_i(self, indexes):
+        data = list(
+            map(
+                lambda x: x[1],
+                filter(
+                    lambda x: x[0] in indexes,
+                    enumerate(self._data)
+                )
+            )
+        )
+        self.delete(data)
+
+
 
 
 
diff --git a/src/View/InitialConditionsAdisTS/Table.py b/src/View/InitialConditionsAdisTS/Table.py
index 658e0eba..24ccc9bb 100644
--- a/src/View/InitialConditionsAdisTS/Table.py
+++ b/src/View/InitialConditionsAdisTS/Table.py
@@ -35,10 +35,8 @@ from PyQt5.QtWidgets import (
 
 from View.Tools.PamhyrTable import PamhyrTableModel
 
-from View.InitialConditions.UndoCommand import (
-    SetCommand, AddCommand, DelCommand,
-    SortCommand, MoveCommand, InsertCommand,
-    DuplicateCommand, GenerateCommand,
+from View.InitialConditionsAdisTS.UndoCommand import (
+    SetCommand, AddCommand, SetCommandSpec,
 )
 
 logger = logging.getLogger()
@@ -62,6 +60,8 @@ class ComboBoxDelegate(QItemDelegate):
         if self._mode == "kp":
             reach_id = self._ic_spec_lst[index.row()].reach
 
+            print(self._data.edges()[0].id, reach_id)
+
             reach = next(filter(lambda edge: edge.id == reach_id, self._data.edges()))
 
             if reach_id is not None:
@@ -108,9 +108,17 @@ class ComboBoxDelegate(QItemDelegate):
 
 
 class InitialConditionTableModel(PamhyrTableModel):
-    def __init__(self, data=None, **kwargs):
-        self._lst = data[0]._data
-        super(InitialConditionTableModel, self).__init__(**kwargs)
+    def __init__(self, river=None, data=None, **kwargs):
+        self._river = river
+
+        super(InitialConditionTableModel, self).__init__(data=data, **kwargs)
+
+        self._data = data
+
+        print("init table model : ", self._river)
+
+    def _setup_lst(self):
+        self._lst = self._data._data
 
     def data(self, index, role):
         if role != Qt.ItemDataRole.DisplayRole:
@@ -126,9 +134,10 @@ class InitialConditionTableModel(PamhyrTableModel):
             return n
         elif self._headers[column] is "reach":
             n = self._lst[row].reach
+            print(n)
             if n is None:
                 return self._trad['not_associated']
-            return next(filter(lambda edge: edge.id == n, self._data.edges())).name
+            return next(filter(lambda edge: edge.id == n, self._river.edges())).name
         elif self._headers[column] is "start_kp":
             n = self._lst[row].start_kp
             if n is None:
@@ -175,12 +184,19 @@ class InitialConditionTableModel(PamhyrTableModel):
         column = index.column()
 
         try:
-            if self._headers[column] is not None:
+            if self._headers[column] != "reach":
                 self._undo.push(
-                    SetCommand(
+                    SetCommandSpec(
                         self._lst, row, self._headers[column], value
                     )
                 )
+            elif self._headers[column] == "reach":
+                print(self._river.edge(value).id)
+                self._undo.push(
+                    SetCommandSpec(
+                        self._lst, row, self._headers[column], self._river.edge(value).id
+                    )
+                )
         except Exception as e:
             logger.info(e)
             logger.debug(traceback.format_exc())
diff --git a/src/View/InitialConditionsAdisTS/UndoCommand.py b/src/View/InitialConditionsAdisTS/UndoCommand.py
index dfb1c268..3d956631 100644
--- a/src/View/InitialConditionsAdisTS/UndoCommand.py
+++ b/src/View/InitialConditionsAdisTS/UndoCommand.py
@@ -38,7 +38,6 @@ class SetCommand(QUndoCommand):
         if self._column == "name":
             self._old = self._data[self._row].name
         elif self._column == "concentration":
-            print(self._column, self._data[self._row].concentration, new_value)
             self._old = self._data[self._row].concentration
         elif self._column == "eg":
             self._old = self._data[self._row].eg
@@ -77,21 +76,96 @@ class SetCommand(QUndoCommand):
         elif self._column == "ed":
             self._data[self._row].ed = self._new
 
+class SetCommandSpec(QUndoCommand):
+    def __init__(self, data, row, column, new_value):
+        QUndoCommand.__init__(self)
+
+        self._data = data
+        self._row = row
+        self._column = column
+
+        if self._column == "name":
+            self._old = self._data[self._row].name
+        elif self._column == "reach":
+            self._old = self._data[self._row].reach
+        elif self._column == "start_kp":
+            self._old = self._data[self._row].start_kp
+        elif self._column == "end_kp":
+            self._old = self._data[self._row].end_kp
+        elif self._column == "concentration":
+            self._old = self._data[self._row].concentration
+        elif self._column == "eg":
+            self._old = self._data[self._row].eg
+        elif self._column == "em":
+            self._old = self._data[self._row].em
+        elif self._column == "ed":
+            self._old = self._data[self._row].ed
+        elif self._column == "rate":
+            self._old = self._data[self._row].rate
+
+        _type = float
+        if column == "name":
+            _type = str
+        elif column == "reach":
+            _type = int
+
+        self._new = _type(new_value)
+
+    def undo(self):
+        if self._column == "name":
+            self._data[self._row].name = self._old
+        elif self._column == "reach":
+            self._data[self._row].reach = self._old
+        elif self._column == "start_kp":
+            self._data[self._row].start_kp = self._old
+        elif self._column == "end_kp":
+            self._data[self._row].end_kp = self._old
+        elif self._column == "concentration":
+            self._data[self._row].concentration = self._old
+        elif self._column == "eg":
+            self._data[self._row].eg = self._old
+        elif self._column == "em":
+            self._data[self._row].em = self._old
+        elif self._column == "ed":
+            self._data[self._row].ed = self._old
+        elif self._column == "rate":
+            self._data[self._row].rate = self._old
+
+    def redo(self):
+        if self._column == "name":
+            self._data[self._row].name = self._new
+        elif self._column == "reach":
+            self._data[self._row].reach = self._new
+        elif self._column == "start_kp":
+            self._data[self._row].start_kp = self._new
+        elif self._column == "end_kp":
+            self._data[self._row].end_kp = self._new
+        elif self._column == "concentration":
+            self._data[self._row].concentration = self._new
+        elif self._column == "eg":
+            self._data[self._row].eg = self._new
+        elif self._column == "em":
+            self._data[self._row].em = self._new
+        elif self._column == "ed":
+            self._data[self._row].ed = self._new
+        elif self._column == "rate":
+            self._data[self._row].rate = self._new
+
 class AddCommand(QUndoCommand):
-    def __init__(self, data, ics, index):
+    def __init__(self, data, ics_spec, index):
         QUndoCommand.__init__(self)
 
         self._data = data
-        self._ics = ics
+        self._ics_spec = ics_spec
         self._index = index
         self._new = None
 
     def undo(self):
-        self._ics.delete_i([self._index])
+        self._data.delete_i([self._index])
 
     def redo(self):
         if self._new is None:
-            self._new = self._ics.new(self._index)
+            self._new = self._data.new(self._index)
         else:
-            self._ics.insert(self._index, self._new)
+            self._ics_spec.insert(self._index, self._new)
 
diff --git a/src/View/InitialConditionsAdisTS/Window.py b/src/View/InitialConditionsAdisTS/Window.py
index ed608457..5c63b20d 100644
--- a/src/View/InitialConditionsAdisTS/Window.py
+++ b/src/View/InitialConditionsAdisTS/Window.py
@@ -71,6 +71,7 @@ class InitialConditionsAdisTSWindow(PamhyrWindow):
     def __init__(self, data=None, study=None, config=None, parent=None):
         self._data = []
         self._data.append(data)
+        print(self._data[0]._data)
         trad = IcAdisTSTranslate()
 
         name = (
@@ -91,7 +92,10 @@ class InitialConditionsAdisTSWindow(PamhyrWindow):
 
         self._ics_adists_lst = study.river.initial_conditions_adists
 
+        print("setup tables")
+
         self.setup_table()
+
         self.ui.setWindowTitle(self._title)
 
     def setup_table(self):
@@ -121,8 +125,10 @@ class InitialConditionsAdisTSWindow(PamhyrWindow):
 
         action_add = QAction(self)
         action_add.setIcon(QIcon(os.path.join(path_icons, f"add.png")))
+        action_add.triggered.connect(self.add)
         action_delete = QAction(self)
         action_delete.setIcon(QIcon(os.path.join(path_icons, f"del.png")))
+        action_delete.triggered.connect(self.delete)
 
         toolBar.addAction(action_add)
         toolBar.addAction(action_delete)
@@ -145,6 +151,8 @@ class InitialConditionsAdisTSWindow(PamhyrWindow):
             mode="kp"
         )
 
+        print("hello table", self._study.river)
+
         self._table_spec = InitialConditionTableModel(
             table_view=table_spec,
             table_headers=self._trad.get_dict("table_headers_spec"),
@@ -156,7 +164,8 @@ class InitialConditionsAdisTSWindow(PamhyrWindow):
             },
             data=self._data[0],
             undo=self._undo_stack,
-            trad=self._trad
+            trad=self._trad,
+            river=self._study.river
         )
 
         table_spec.setModel(self._table_spec)
@@ -164,10 +173,6 @@ class InitialConditionsAdisTSWindow(PamhyrWindow):
         table_spec.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
         table_spec.setAlternatingRowColors(True)
 
-    def setup_connections(self):
-        self.find(QAction, "action_add").triggered.connect(self.add)
-        self.find(QAction, "action_delete").triggered.connect(self.delete)
-
     def index_selected_row(self):
         table = self.find(QTableView, f"tableView")
         rows = table.selectionModel()\
@@ -255,6 +260,7 @@ class InitialConditionsAdisTSWindow(PamhyrWindow):
         self._update()
 
     def add(self):
+        print("Hello")
         rows = self.index_selected_rows()
         if len(self._data[0]._data) == 0 or len(rows) == 0:
             self._table_spec.add(0)
-- 
GitLab