From ec16586755ba2e74d3bd0857864e7ceacd2975a9 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Fri, 4 Aug 2023 15:00:23 +0200
Subject: [PATCH] Geometry, ListedSubWindow: Use ListedSubWindow in Geometry
 windows.

---
 src/View/ASubWindow.py              | 10 ++++++----
 src/View/Geometry/Profile/Window.py | 12 ++++++++----
 src/View/Geometry/Window.py         | 13 +++++++++----
 src/View/ListedSubWindow.py         | 25 ++++++++++++++++++++++++-
 4 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/src/View/ASubWindow.py b/src/View/ASubWindow.py
index c07465f1..d9e121f4 100644
--- a/src/View/ASubWindow.py
+++ b/src/View/ASubWindow.py
@@ -462,10 +462,12 @@ class ASubWindowFeatures(object):
 class ASubMainWindow(QMainWindow, ASubWindowFeatures, WindowToolKit):
     def __init__(self, name="", ui="dummy", parent=None):
         super(ASubMainWindow, self).__init__(parent=parent)
-        self.ui = loadUi(
-            os.path.join(os.path.dirname(__file__), "ui", f"{ui}.ui"),
-            self
-        )
+        if ui is not None:
+            self.ui = loadUi(
+                os.path.join(os.path.dirname(__file__), "ui", f"{ui}.ui"),
+                self
+            )
+
         self.name = name
         self.parent = parent
         if self.parent is not None:
diff --git a/src/View/Geometry/Profile/Window.py b/src/View/Geometry/Profile/Window.py
index cfcf6925..9f7df090 100644
--- a/src/View/Geometry/Profile/Window.py
+++ b/src/View/Geometry/Profile/Window.py
@@ -37,7 +37,7 @@ from PyQt5.QtWidgets import (
 from Model.Geometry.Reach import Reach
 from Model.Geometry.ProfileXYZ import ProfileXYZ
 
-from View.ASubWindow import WindowToolKit
+from View.ASubWindow import ASubMainWindow
 from View.Geometry.Profile.mainwindow_ui_profile import Ui_MainWindow
 from View.Geometry.Profile.Plot import Plot
 from View.Geometry.Profile.Table import *
@@ -45,10 +45,14 @@ from View.Geometry.Profile.Table import *
 _translate = QCoreApplication.translate
 
 
-class ProfileWindow(QMainWindow, WindowToolKit):
-    def __init__(self, profile=None, parent=None):
+class ProfileWindow(ASubMainWindow):
+    def __init__(self, profile=None, title="Profile", parent=None):
+        self._title = title
         self.parent = parent
-        super(ProfileWindow, self).__init__(self.parent)
+        super(ProfileWindow, self).__init__(
+            name=self._title,
+            parent=self.parent
+        )
 
         self.ui = Ui_MainWindow()
         self.ui.setupUi(self)
diff --git a/src/View/Geometry/Window.py b/src/View/Geometry/Window.py
index 9fd0f6f2..46e339f6 100644
--- a/src/View/Geometry/Window.py
+++ b/src/View/Geometry/Window.py
@@ -41,7 +41,8 @@ from View.Geometry.PlotXY import PlotXY
 from View.Geometry.PlotKPC import PlotKPC
 from View.Geometry.PlotAC import PlotAC
 
-from View.ASubWindow import WindowToolKit
+from View.ASubWindow import ASubMainWindow, WindowToolKit
+from View.ListedSubWindow import ListedSubWindow
 from View.Geometry.mainwindow_ui_reach import Ui_MainWindow
 from View.Geometry.Table import *
 from View.Geometry.Profile.Window import ProfileWindow
@@ -49,10 +50,14 @@ from View.Geometry.Profile.Window import ProfileWindow
 _translate = QCoreApplication.translate
 
 
-class GeometryWindow(QMainWindow, WindowToolKit):
-    def __init__(self, model=None, parent=None):
+class GeometryWindow(ASubMainWindow, ListedSubWindow):
+    def __init__(self, model=None, title="Geometry", parent=None):
+        self._title = title
         self.parent = parent
-        super(GeometryWindow, self).__init__(parent=parent)
+        super(GeometryWindow, self).__init__(
+            name=self._title,
+            parent=parent
+        )
 
         self._model = model
         self._reach = model.river.current_reach().reach
diff --git a/src/View/ListedSubWindow.py b/src/View/ListedSubWindow.py
index 37d64234..eaef39ef 100644
--- a/src/View/ListedSubWindow.py
+++ b/src/View/ListedSubWindow.py
@@ -48,9 +48,32 @@ class ListedSubWindow(object):
         self.sub_win_cnt = len(self.sub_win_list)
         logger.info(f"Close window: {name} ({self.sub_win_cnt})")
 
-    def sub_win_exists(self, name):
+    def _sub_win_exists(self, name):
         return reduce(
             lambda acc, n: (acc or (n[0] == name)),
             self.sub_win_list,
             False
         )
+
+    def _sub_win_exists_with_contain(self, name, contain):
+        return reduce(
+            lambda acc, n: (
+                acc or
+                (
+                    (n[0] == name) and
+                    reduce(
+                        lambda acc, a: acc and (a in n[1]._title),
+                        contain,
+                        True
+                    )
+                )
+            ),
+            self.sub_win_list,
+            False
+        )
+
+    def sub_win_exists(self, name, contain = []):
+        if contain == []:
+            self._sub_win_exists(name)
+        else:
+            self._sub_win_exists_with_contain(name, contain)
-- 
GitLab