From e395f9f575b58fb8e22dee5e7b7adfc6b0f7268c Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Fri, 20 Oct 2023 17:16:37 +0200
Subject: [PATCH] BC, LC: Fix sub window open.

---
 src/View/BoundaryCondition/Edit/Window.py   |  2 +
 src/View/BoundaryCondition/Window.py        | 26 +++++-----
 src/View/Geometry/Window.py                 | 26 +---------
 src/View/LateralContribution/Edit/Window.py |  2 +
 src/View/LateralContribution/Window.py      | 26 +++++-----
 src/View/MainWindow.py                      | 56 ++++++---------------
 src/View/Tools/ListedSubWindow.py           | 28 ++++++++++-
 7 files changed, 73 insertions(+), 93 deletions(-)

diff --git a/src/View/BoundaryCondition/Edit/Window.py b/src/View/BoundaryCondition/Edit/Window.py
index 162459ed..b2d9e61f 100644
--- a/src/View/BoundaryCondition/Edit/Window.py
+++ b/src/View/BoundaryCondition/Edit/Window.py
@@ -124,6 +124,8 @@ class EditBoundaryConditionWindow(PamhyrWindow):
             parent=parent
         )
 
+        self._hash_data.append(data)
+
         self.setup_table()
         self.setup_plot()
         self.setup_data()
diff --git a/src/View/BoundaryCondition/Window.py b/src/View/BoundaryCondition/Window.py
index 9d30532e..4489a509 100644
--- a/src/View/BoundaryCondition/Window.py
+++ b/src/View/BoundaryCondition/Window.py
@@ -214,17 +214,17 @@ class BoundaryConditionWindow(PamhyrWindow):
         tab = self.current_tab()
         rows = self.index_selected_rows()
         for row in rows:
-            win = self.sub_win_filter_first(
-                "Edit boundary condition",
-                contain=[f"({self._bcs.get(tab, row).id})"]
-            )
+            data = self._bcs.get(tab, row)
 
-            if win is None:
-                win = EditBoundaryConditionWindow(
-                    data=self._bcs.get(tab, row),
-                    study=self._study,
-                    parent=self
-                )
-                win.show()
-            else:
-                win.activateWindow()
+            if self.sub_window_exists(
+                EditBoundaryConditionWindow,
+                data=[self._study, None, data]
+            ):
+                continue
+
+            win = EditBoundaryConditionWindow(
+                data=data,
+                study=self._study,
+                parent=self
+            )
+            win.show()
diff --git a/src/View/Geometry/Window.py b/src/View/Geometry/Window.py
index 459bc95d..3bc4ebd1 100644
--- a/src/View/Geometry/Window.py
+++ b/src/View/Geometry/Window.py
@@ -216,30 +216,6 @@ class GeometryWindow(PamhyrWindow):
             self.plot_kpc()
             self.plot_ac()
 
-    def _sub_window_exists(self, cls,
-                           data=None):
-        """Check if window already exists
-
-        Check if window already exists, used to deni window open
-        duplication
-
-        Args:
-            cls: Window class, must inerit to PamhyrWindow or
-                PamhyrDialog
-            data: Data used for hash computation of cls
-
-        Returns:
-            The window if hash already exists on sub window dictionary,
-            otherelse None
-        """
-        hash = cls._hash(data)
-        if self.sub_win_exists(hash):
-            win = self.get_sub_win(hash)
-            win.activateWindow()
-            return True
-        else:
-            return False
-
     def edit_profile(self):
         self.tableView.model().blockSignals(True)
 
@@ -252,7 +228,7 @@ class GeometryWindow(PamhyrWindow):
         for row in rows:
             profile = self._reach.profile(row)
 
-            if self._sub_window_exists(
+            if self.sub_window_exists(
                 ProfileWindow,
                 data=[None, None, profile]
             ):
diff --git a/src/View/LateralContribution/Edit/Window.py b/src/View/LateralContribution/Edit/Window.py
index c813a3a9..72c59b4c 100644
--- a/src/View/LateralContribution/Edit/Window.py
+++ b/src/View/LateralContribution/Edit/Window.py
@@ -77,6 +77,8 @@ class EditLateralContributionWindow(PamhyrWindow):
             parent=parent
         )
 
+        self._hash_data.append(data)
+
         self.setup_table()
         self.setup_plot()
         self.setup_connections()
diff --git a/src/View/LateralContribution/Window.py b/src/View/LateralContribution/Window.py
index 72ca343d..8a9a2e8e 100644
--- a/src/View/LateralContribution/Window.py
+++ b/src/View/LateralContribution/Window.py
@@ -258,17 +258,17 @@ class LateralContributionWindow(PamhyrWindow):
         tab = self.current_tab()
         rows = self.index_selected_rows()
         for row in rows:
-            win = self.sub_win_filter_first(
-                "Edit lateral contribution",
-                contain=[f"({self._lcs.get(tab, row).id})"]
-            )
+            data = self._lcs.get(tab, row)
 
-            if win is None:
-                win = EditLateralContributionWindow(
-                    data=self._lcs.get(tab, row),
-                    study=self._study,
-                    parent=self
-                )
-                win.show()
-            else:
-                win.activateWindow()
+            if self.sub_window_exists(
+                EditLateralContributionWindow,
+                data=[self._study, None, data]
+            ):
+                continue
+
+            win = EditLateralContributionWindow(
+                data=data,
+                study=self._study,
+                parent=self
+            )
+            win.show()
diff --git a/src/View/MainWindow.py b/src/View/MainWindow.py
index 27e09ad1..92a3e8f5 100644
--- a/src/View/MainWindow.py
+++ b/src/View/MainWindow.py
@@ -480,30 +480,6 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
     # SUBWINDOW #
     #############
 
-    def _sub_window_exists(self, cls,
-                           data=None):
-        """Check if window already exists
-
-        Check if window already exists, used to deni window open
-        duplication
-
-        Args:
-            cls: Window class, must inerit to PamhyrWindow or
-                PamhyrDialog
-            data: Data used for hash computation of cls
-
-        Returns:
-            The window if hash already exists on sub window dictionary,
-            otherelse None
-        """
-        hash = cls._hash(data)
-        if self.sub_win_exists(hash):
-            win = self.get_sub_win(hash)
-            win.activateWindow()
-            return True
-        else:
-            return False
-
     def open_configure(self):
         """Open configure window
 
@@ -512,7 +488,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         Returns:
             Nothing
         """
-        if self._sub_window_exists(
+        if self.sub_window_exists(
             ConfigureWindow,
             data=[None, self.conf]
         ):
@@ -529,7 +505,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         Returns:
             Nothing
         """
-        if self._sub_window_exists(
+        if self.sub_window_exists(
             AboutWindow,
             data=[None, None]
         ):
@@ -563,7 +539,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
             Nothing
         """
         if self._study is None:
-            if self._sub_window_exists(
+            if self.sub_window_exists(
                 NewStudyWindow,
                 data=[None, None]
             ):
@@ -579,7 +555,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
             Nothing
         """
         if self._study is not None:
-            if self._sub_window_exists(
+            if self.sub_window_exists(
                 NewStudyWindow,
                 data=[self._study, None]
             ):
@@ -595,7 +571,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
             Nothing
         """
         if self._study is not None:
-            if self._sub_window_exists(
+            if self.sub_window_exists(
                 NetworkWindow,
                 data=[self._study, None]
             ):
@@ -613,7 +589,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         if (self._study is not None and self._study.river.has_current_reach()):
             reach = self._study.river.current_reach().reach
 
-            if self._sub_window_exists(
+            if self.sub_window_exists(
                 GeometryWindow,
                 data=[self._study, self.conf, reach]
             ):
@@ -630,7 +606,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
             self.msg_select_reach()
 
     def open_boundary_cond(self):
-        if self._sub_window_exists(
+        if self.sub_window_exists(
             BoundaryConditionWindow,
             data=[self._study, None]
         ):
@@ -640,7 +616,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         bound.show()
 
     def open_lateral_contrib(self):
-        if self._sub_window_exists(
+        if self.sub_window_exists(
             LateralContributionWindow,
             data=[self._study, None]
         ):
@@ -650,7 +626,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         lateral.show()
 
     def open_stricklers(self):
-        if self._sub_window_exists(
+        if self.sub_window_exists(
             StricklersWindow,
             data=[self._study, self.conf]
         ):
@@ -668,7 +644,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
             if self._study.river.has_current_reach():
                 reach = self._study.river.current_reach()
 
-                if self._sub_window_exists(
+                if self.sub_window_exists(
                     FrictionsWindow,
                     data=[self._study, None, reach]
                 ):
@@ -686,7 +662,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         if self._study.river.has_current_reach():
             reach = self._study.river.current_reach()
 
-            if self._sub_window_exists(
+            if self.sub_window_exists(
                 InitialConditionsWindow,
                 data=[self._study, self.conf, reach]
             ):
@@ -703,7 +679,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
             self.msg_select_reach()
 
     def open_solver_parameters(self):
-        if self._sub_window_exists(
+        if self.sub_window_exists(
             SolverParametersWindow,
             data=[self._study, None]
         ):
@@ -716,7 +692,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         params.show()
 
     def open_sediment_layers(self):
-        if self._sub_window_exists(
+        if self.sub_window_exists(
             SedimentLayersWindow,
             data=[self._study, None]
         ):
@@ -731,7 +707,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
     def open_reach_sediment_layers(self):
         reach = self._study.river.current_reach().reach
 
-        if self._sub_window_exists(
+        if self.sub_window_exists(
             ReachSedimentLayersWindow,
             data=[self._study, None, reach]
         ):
@@ -756,7 +732,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         if run.exec():
             solver = run.solver
 
-            if self._sub_window_exists(
+            if self.sub_window_exists(
                 CheckListWindow,
                 data=[
                     self._study,
@@ -793,7 +769,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
             return
 
         # Windows already opened
-        if self._sub_window_exists(
+        if self.sub_window_exists(
                 ResultsWindow,
                 data=[
                     self._study,
diff --git a/src/View/Tools/ListedSubWindow.py b/src/View/Tools/ListedSubWindow.py
index 5c3bf1dd..7fe21bb7 100644
--- a/src/View/Tools/ListedSubWindow.py
+++ b/src/View/Tools/ListedSubWindow.py
@@ -39,7 +39,7 @@ class ListedSubWindow(object):
         self.sub_win_cnt += 1
         try:
             logger.info(
-                f"Open window: {name}: {self.sub_win_cnt}: {win.hash()}")
+                f"Open window: {name}: {self.sub_win_cnt}")
         except Exception:
             logger.info(f"Open window: {name}: {self.sub_win_cnt}: X")
             logger.warning(f"Sub window without hash method !")
@@ -52,7 +52,7 @@ class ListedSubWindow(object):
             )
         )
         self.sub_win_cnt = len(self.sub_win_list)
-        logger.info(f"Close window: {h}: {self.sub_win_cnt}")
+        logger.info(f"Close window: ({h}) {self.sub_win_cnt}")
 
     def _sub_win_exists(self, h):
         return reduce(
@@ -74,3 +74,27 @@ class ListedSubWindow(object):
             )[1]
         except Exception:
             return None
+
+    def sub_window_exists(self, cls,
+                          data=None):
+        """Check if window already exists
+
+        Check if window already exists, used to deni window open
+        duplication
+
+        Args:
+            cls: Window class, must inerit to PamhyrWindow or
+                PamhyrDialog
+            data: Data used for hash computation of cls
+
+        Returns:
+            The window if hash already exists on sub window dictionary,
+            otherelse None
+        """
+        hash = cls._hash(data)
+        if self.sub_win_exists(hash):
+            win = self.get_sub_win(hash)
+            win.activateWindow()
+            return True
+        else:
+            return False
-- 
GitLab