From b2bc34a5b00200d40e48bbf273fa393cfbf1606d Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Wed, 7 Jun 2023 11:11:47 +0200
Subject: [PATCH] Sections: Switch window focus to current reach instead of all
 study.

---
 src/Model/River.py               |  9 ++++-----
 src/View/Sections/Table.py       | 31 ++++++++-----------------------
 src/View/Sections/UndoCommand.py |  4 +++-
 src/View/Sections/Window.py      | 29 ++++++++++-------------------
 src/View/Sections/translate.py   |  2 +-
 5 files changed, 26 insertions(+), 49 deletions(-)

diff --git a/src/Model/River.py b/src/Model/River.py
index cc87c75f..0bb8818a 100644
--- a/src/Model/River.py
+++ b/src/Model/River.py
@@ -46,11 +46,15 @@ class RiverReach(Edge):
         )
 
         self._reach = Reach(status=self._status, parent=self)
+        self._sections = SectionList(status=self._status)
 
     @property
     def reach(self):
         return self._reach
 
+    @property
+    def sections(self):
+        return self._sections
 
 class River(Graph):
     def __init__(self, status=None):
@@ -65,7 +69,6 @@ class River(Graph):
         self._lateral_contribution = LateralContributionList(status=self._status)
         self._initial_conditions = InitialConditionsDict(status=self._status)
         self._stricklers = StricklersList(status=self._status)
-        self._sections = SectionList(status=self._status)
 
     @property
     def boundary_condition(self):
@@ -96,10 +99,6 @@ class River(Graph):
 
         return ret[0]
 
-    @property
-    def sections(self):
-        return self._sections
-
     def has_current_reach(self):
         return self._current_reach is not None
 
diff --git a/src/View/Sections/Table.py b/src/View/Sections/Table.py
index 438db996..87461630 100644
--- a/src/View/Sections/Table.py
+++ b/src/View/Sections/Table.py
@@ -16,8 +16,7 @@ from PyQt5.QtWidgets import (
 )
 
 from View.Sections.UndoCommand import (
-    SetNameCommand, SetEdgeCommand,
-    SetBeginCommand, SetEndCommand,
+    SetNameCommand, SetBeginCommand, SetEndCommand,
     SetBeginStricklerCommand, SetEndStricklerCommand,
     AddCommand, DelCommand, SortCommand,
     MoveCommand, PasteCommand, DuplicateCommand,
@@ -28,10 +27,11 @@ from View.Sections.translate import *
 _translate = QCoreApplication.translate
 
 class ComboBoxDelegate(QItemDelegate):
-    def __init__(self, data=None, mode="stricklers", parent=None):
+    def __init__(self, data=None, study=None, mode="stricklers", parent=None):
         super(ComboBoxDelegate, self).__init__(parent)
 
         self._data = data
+        self._study = study
         self._mode = mode
 
     def createEditor(self, parent, option, index):
@@ -43,15 +43,10 @@ class ComboBoxDelegate(QItemDelegate):
                 list(
                     map(
                         lambda s: str(s),
-                        self._data.stricklers.stricklers
+                        self._study.river.stricklers.stricklers
                     )
                 )
             )
-        elif self._mode == "edge":
-            self.editor.addItems(
-                [_translate("Sections", "Not associate")] +
-                self._data.edges_names()
-            )
 
         self.editor.setCurrentText(index.data(Qt.DisplayRole))
         return self.editor
@@ -78,10 +73,11 @@ class ComboBoxDelegate(QItemDelegate):
 
 
 class TableModel(QAbstractTableModel):
-    def __init__(self, data=None, undo=None):
+    def __init__(self, data=None, study=None, undo=None):
         super(QAbstractTableModel, self).__init__()
         self._headers = list(table_headers.keys())
         self._data = data
+        self._study = study
         self._undo = undo
         self._sections = self._data.sections
 
@@ -106,11 +102,6 @@ class TableModel(QAbstractTableModel):
 
         if self._headers[column] == "name":
             return self._sections.get(row).name
-        elif self._headers[column] == "edge":
-            n = self._sections.get(row).edge
-            if n is None:
-                return _translate("Sections", "Not associate")
-            return n.name
         elif self._headers[column] == "begin_kp":
             return self._sections.get(row).begin_kp
         elif self._headers[column] == "end_kp":
@@ -147,12 +138,6 @@ class TableModel(QAbstractTableModel):
                     self._sections, row, value
                 )
             )
-        elif self._headers[column] == "edge":
-            self._undo.push(
-                SetEdgeCommand(
-                    self._sections, row, self._data.edge(value)
-                )
-            )
         elif self._headers[column] == "begin_kp":
             self._undo.push(
                 SetBeginCommand(
@@ -168,7 +153,7 @@ class TableModel(QAbstractTableModel):
         elif self._headers[column] == "begin_strickler":
             self._undo.push(
                 SetBeginStricklerCommand(
-                    self._sections, row, self._data.strickler(value)
+                    self._sections, row, self._study.river.strickler(value)
                 )
             )
         elif self._headers[column] == "end_strickler":
@@ -186,7 +171,7 @@ class TableModel(QAbstractTableModel):
 
         self._undo.push(
             AddCommand(
-                self._sections, row
+                self._sections, row, self._data
             )
         )
 
diff --git a/src/View/Sections/UndoCommand.py b/src/View/Sections/UndoCommand.py
index b6cb445a..9ba5cff6 100644
--- a/src/View/Sections/UndoCommand.py
+++ b/src/View/Sections/UndoCommand.py
@@ -103,11 +103,12 @@ class SetEdgeCommand(QUndoCommand):
         self._sections.get(self._index).edge = self._new
 
 class AddCommand(QUndoCommand):
-    def __init__(self, sections, index):
+    def __init__(self, sections, index, reach):
         QUndoCommand.__init__(self)
 
         self._sections = sections
         self._index = index
+        self._reach = reach
         self._new = None
 
     def undo(self):
@@ -116,6 +117,7 @@ class AddCommand(QUndoCommand):
     def redo(self):
         if self._new is None:
             self._new = self._sections.new(self._index)
+            self._new.edge = self._reach
         else:
             self._sections.insert(self._index, self._new)
 
diff --git a/src/View/Sections/Window.py b/src/View/Sections/Window.py
index e951d245..47c64880 100644
--- a/src/View/Sections/Window.py
+++ b/src/View/Sections/Window.py
@@ -40,13 +40,14 @@ class SectionsWindow(ASubMainWindow, ListedSubWindow):
     def __init__(self, title="Sections", study=None, parent=None):
         title = title + " - " + study.name
 
+        self._study = study
+        self._current_reach = self._study.river._current_reach
+        self._sections = self._current_reach.sections
+
         super(SectionsWindow, self).__init__(
             name=title, ui="Sections", parent=parent
         )
 
-        self._study = study
-        self._sections = self._study.river.sections
-
         self.setup_sc()
         self.setup_table()
         self.setup_graph()
@@ -67,31 +68,25 @@ class SectionsWindow(ASubMainWindow, ListedSubWindow):
 
         table = self.find(QTableView, f"tableView")
         self._table = TableModel(
-            data = self._study.river,
+            data = self._current_reach,
+            study = self._study,
             undo = self._undo_stack,
         )
         table.setModel(self._table)
 
-        self._delegate_edge = ComboBoxDelegate(
-            data = self._study.river,
-            mode = "edge",
-            parent=self
-        )
         self._delegate_stricklers = ComboBoxDelegate(
-            data = self._study.river,
+            data = self._current_reach,
+            study = self._study,
             mode = "stricklers",
             parent=self
         )
 
         table.setItemDelegateForColumn(
-            1, self._delegate_edge
+            3, self._delegate_stricklers
         )
         table.setItemDelegateForColumn(
             4, self._delegate_stricklers
         )
-        table.setItemDelegateForColumn(
-            5, self._delegate_stricklers
-        )
 
         table.setSelectionBehavior(QAbstractItemView.SelectRows)
         table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
@@ -147,11 +142,7 @@ class SectionsWindow(ASubMainWindow, ListedSubWindow):
         highlight = None
 
         if len(rows) > 0:
-            edge = self._study\
-                       .river\
-                       .sections\
-                       .get(rows[0])\
-                       .edge
+            edge = self._current_reach
             if edge:
                 data = edge.reach
                 sec = self._sections.get(rows[0])
diff --git a/src/View/Sections/translate.py b/src/View/Sections/translate.py
index 318c455b..6868851a 100644
--- a/src/View/Sections/translate.py
+++ b/src/View/Sections/translate.py
@@ -6,7 +6,7 @@ _translate = QCoreApplication.translate
 
 table_headers = {
     "name": _translate("Sections", "Name"),
-    "edge": _translate("Sections", "Reach"),
+    # "edge": _translate("Sections", "Reach"),
     "begin_kp": _translate("Sections", "Begin kp (m)"),
     "end_kp": _translate("Sections", "End kp (m)"),
     "begin_strickler": _translate("Sections", "Begin strickler"),
-- 
GitLab