From ec9a061f9ad2502a5241eb1762709d0521cd4df6 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Fri, 4 Aug 2023 15:28:01 +0200
Subject: [PATCH] Geometry: Forbidden duplicate window.

---
 src/View/Geometry/Profile/Window.py |  4 +++-
 src/View/Geometry/Window.py         |  3 ++-
 src/View/ListedSubWindow.py         | 24 +++++++++++++++++++++---
 src/View/MainWindow.py              | 15 +++++++++++----
 4 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/src/View/Geometry/Profile/Window.py b/src/View/Geometry/Profile/Window.py
index 9f7df090..54d1e011 100644
--- a/src/View/Geometry/Profile/Window.py
+++ b/src/View/Geometry/Profile/Window.py
@@ -86,12 +86,14 @@ class ProfileWindow(ASubMainWindow):
         if (name is None) or (name == ""):
             name = _translate("MainWindowProfile", "(no name)")
 
-        self.setWindowTitle(
+        self._title = (
             header + " - " +
             f"{self._profile.reach.name}" + " - " +
             f"{name} ({self._profile.kp})"
         )
 
+        self.setWindowTitle(self._title)
+
     def setup_sc(self):
         self._undo_stack = QUndoStack()
 
diff --git a/src/View/Geometry/Window.py b/src/View/Geometry/Window.py
index 46e339f6..851b73bb 100644
--- a/src/View/Geometry/Window.py
+++ b/src/View/Geometry/Window.py
@@ -80,7 +80,8 @@ class GeometryWindow(ASubMainWindow, ListedSubWindow):
         self.changed_slider_value()
 
     def setup_window(self):
-        self.setWindowTitle(f"{self.ui.mainwindow_title} - {self._reach.name}")
+        self._title = f"{self.ui.mainwindow_title} - {self._reach.name}"
+        self.setWindowTitle(self._title)
 
     def setup_sc(self):
         self._undo_stack = QUndoStack()
diff --git a/src/View/ListedSubWindow.py b/src/View/ListedSubWindow.py
index eaef39ef..b44efd61 100644
--- a/src/View/ListedSubWindow.py
+++ b/src/View/ListedSubWindow.py
@@ -62,7 +62,7 @@ class ListedSubWindow(object):
                 (
                     (n[0] == name) and
                     reduce(
-                        lambda acc, a: acc and (a in n[1]._title),
+                        lambda acc, c: acc and (c in n[1]._title),
                         contain,
                         True
                     )
@@ -74,6 +74,24 @@ class ListedSubWindow(object):
 
     def sub_win_exists(self, name, contain = []):
         if contain == []:
-            self._sub_win_exists(name)
+            return self._sub_win_exists(name)
         else:
-            self._sub_win_exists_with_contain(name, contain)
+            return self._sub_win_exists_with_contain(name, contain)
+
+    def sub_win_filter_first(self, name, contain):
+        try:
+            return next(
+                filter(
+                    lambda n: (
+                        (n[0] == name) and
+                        reduce(
+                            lambda acc, c: acc and (c in n[1]._title),
+                            contain,
+                            True
+                        )
+                    ),
+                    self.sub_win_list,
+                )
+            )[1]
+        except:
+            return None
diff --git a/src/View/MainWindow.py b/src/View/MainWindow.py
index 8a94c469..7b03dfb7 100644
--- a/src/View/MainWindow.py
+++ b/src/View/MainWindow.py
@@ -455,10 +455,17 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         Returns:
             Nothing
         """
-        if (self.model is not None and
-            self.model.river.has_current_reach()):
-            geometry = GeometryWindow(model=self.model, parent=self)
-            geometry.show()
+        if (self.model is not None and self.model.river.has_current_reach()):
+            geometry = self.sub_win_filter_first(
+                "Geometry",
+                contain = [self.model.river.current_reach().name]
+            )
+
+            if geometry == None:
+                geometry = GeometryWindow(model=self.model, parent=self)
+                geometry.show()
+            else:
+                geometry.activateWindow()
         else:
             self.msg_select_reach()
 
-- 
GitLab