From 8f87bd73393910b4feee4e8fe403e91dd3fd2ac2 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Thu, 9 Mar 2023 17:35:29 +0100
Subject: [PATCH] MainWindow: Add enable and disable feature mechanism.

---
 src/view/ASubWindow.py  |   7 +-
 src/view/AboutWindow.py |   2 +-
 src/view/MainWindow.py  | 142 ++++++++++++++++++++++++++++++++--------
 3 files changed, 115 insertions(+), 36 deletions(-)

diff --git a/src/view/ASubWindow.py b/src/view/ASubWindow.py
index bc54ec2e..2739a6a2 100644
--- a/src/view/ASubWindow.py
+++ b/src/view/ASubWindow.py
@@ -9,12 +9,6 @@ from PyQt5.QtWidgets import (
 )
 from PyQt5.uic import loadUi
 
-
-# class ASubWindow(QMdiSubWindow):
-#     def __init__(self, ui="error"):
-#         super(ASubWindow, self).__init__()
-#         loadUi(f"ui/{ui}.ui", self)
-
 class ASubWindow(QDialog):
     def __init__(self, name="", ui="dummy", parent=None):
         super(ASubWindow, self).__init__(parent=parent)
@@ -24,6 +18,7 @@ class ASubWindow(QDialog):
         )
         self.name = name
         self.parent = parent
+        self.parent.sub_win_add(name, self)
 
     def closeEvent(self, event):
         if not self.parent is None:
diff --git a/src/view/AboutWindow.py b/src/view/AboutWindow.py
index 8414691b..03f3db62 100644
--- a/src/view/AboutWindow.py
+++ b/src/view/AboutWindow.py
@@ -4,5 +4,5 @@ from view.ASubWindow import ASubWindow
 
 class AboutWindow(ASubWindow):
     def __init__(self, title="About", parent=None):
-        super(AboutWindow, self).__init__(ui="about", parent=parent)
+        super(AboutWindow, self).__init__(name=title, ui="about", parent=parent)
         self.ui.setWindowTitle(title)
diff --git a/src/view/MainWindow.py b/src/view/MainWindow.py
index 857f5ba4..66fbbac0 100644
--- a/src/view/MainWindow.py
+++ b/src/view/MainWindow.py
@@ -7,36 +7,60 @@ from PyQt5.QtWidgets import (
     QMainWindow, QApplication, QAction,
 )
 from PyQt5.uic import loadUi
+
 from view.ListedSubWindow import ListedSubWindow
 from view.DummyWindow import DummyWindow
 from view.AboutWindow import AboutWindow
 
+from model.Study import Study
+
+no_model_action = [
+    "actionOuvrir_une_tude", "actionR_seau", "actionNouvelle_tude_RubarBE",
+    "actionOuvrir_une_tude_2", "actionImporter_un_jeu_de_donn_es_MAGE",
+    "actionImporter_un_jeu_de_donn_es_RubarBE"
+]
+
+model_action = [
+    "actionenregistrer_etude_en_cours", "actionfermer_etude_en_cours",
+    "actionFermer", "actionEnregistrer", "actionEnregistrer_2",
+    "actionEnregistrer_sous", "actionArchiver",
+]
+
+other_model_action = [
+    "actionlancer_solveur",
+]
+
+define_model_action = [
+    "actionReseau", "actionGeometrie",
+    "actionMaillage", "actionlancer_mailleur_externe",
+    "actionCond_Limites", "actionApp_Lat_raux",
+    "actionDeversements", "actionTroncons",
+    "actionFrottements", "actionOuvrages",
+]
+
+action = (
+    no_model_action + model_action + define_model_action
+)
+
 class ApplicationWindow(QMainWindow, ListedSubWindow):
     def __init__(self):
         super(ApplicationWindow, self).__init__()
+
+        # Model
+        self.model = None
+        # UI
         self.ui = loadUi(
             os.path.join(os.path.dirname(__file__), "ui", "MainWindow.ui"),
             self
         )
-        self.showMaximized()
-        self.basic_callback()
 
-    def open_dummy(self, title="Dummy"):
-        """
-        Open a dummy dialog window.
-        """
-        self.dummy = DummyWindow(
-            title=title if type(title) is str else "Dummy",
-            parent=self
-        )
-        self.sub_win_add(title, self.dummy)
-        self.dummy.show()
+        self.init_callback()
+        self.default_style()
 
-    def open_about(self):
-        self.about = AboutWindow(parent=self)
-        self.about.show()
+    def enable_actions(self, action:str, enable:bool):
+        self.ui.findChild(QAction, action).setEnabled(enable)
 
-    def basic_callback(self):
+    def init_callback(self):
         """
         Connect action to callback function.
         """
@@ -44,26 +68,86 @@ class ApplicationWindow(QMainWindow, ListedSubWindow):
             # Menu action
             "actionA_propos": self.open_about,
             # ToolBar action
-            "actionOuvrir_une_tude": self.open_dummy,
+            "actionquitter_application": self.close,
+            "actionOuvrir_une_tude": self.dummy_model,
             "actionenregistrer_etude_en_cours": self.open_dummy,
-            "actionfermer_etude_en_cours": self.open_dummy,
-            "actionquitter_application": self.open_dummy,
+            "actionfermer_etude_en_cours": self.close_model,
             "actionlancer_solveur": self.open_dummy,
             "actioninterrompt_simulation_en_cours": self.open_dummy,
             "actionafficher_listings_simulation": self.open_dummy,
             "actionlancer_solveur": self.open_dummy,
-            "actionReseau": lambda : self.open_dummy("Networks"),
-            "actionGeometrie": lambda : self.open_dummy("Geomerty"),
-            "actionMaillage": lambda : self.open_dummy("Maillage"),
-            "actionlancer_mailleur_externe": lambda : self.open_dummy("Lancement mailleur externe"),
-            "actionCond_Limites": lambda : self.open_dummy("Condition Limites"),
-            "actionApp_Lat_raux": lambda : self.open_dummy("Apport Lateraux"),
-            "actionDeversements": lambda : self.open_dummy("Deversement"),
-            "actionTroncons": lambda : self.open_dummy("Tronçons"),
-            "actionFrottements": lambda : self.open_dummy("Frottements"),
-            "actionOuvrages": lambda : self.open_dummy("Ouvrages"),
+            "actionReseau": lambda: self.open_dummy("Networks"),
+            "actionGeometrie": lambda: self.open_dummy("Geomerty"),
+            "actionMaillage": lambda: self.open_dummy("Maillage"),
+            "actionlancer_mailleur_externe": lambda: self.open_dummy("Lancement mailleur externe"),
+            "actionCond_Limites": lambda: self.open_dummy("Condition Limites"),
+            "actionApp_Lat_raux": lambda: self.open_dummy("Apport Lateraux"),
+            "actionDeversements": lambda: self.open_dummy("Deversement"),
+            "actionTroncons": lambda: self.open_dummy("Tronçons"),
+            "actionFrottements": lambda: self.open_dummy("Frottements"),
+            "actionOuvrages": lambda: self.open_dummy("Ouvrages"),
         }
 
         for action in actions:
             self.ui.findChild(QAction, action)\
                    .triggered.connect(actions[action])
+
+    def default_style(self):
+        self.update_enable_action()
+        # Maximise window
+        self.showMaximized()
+
+    #########
+    # MODEL #
+    #########
+
+    def get_model(self):
+        return self.model
+
+    def set_model(self, model):
+        self.model = model
+        self.update_enable_action()
+
+    def close_model(self):
+        self.model = None
+        self.update_enable_action()
+
+    def update_enable_action(self):
+        no_model = self.model is None
+
+        if no_model:
+            for action in define_model_action + other_model_action:
+                self.enable_actions(action, False)
+
+        for action in no_model_action:
+            self.enable_actions(action, no_model)
+
+        for action in model_action:
+            self.enable_actions(action, not no_model)
+
+    #############
+    # SUBWINDOW #
+    #############
+
+    def open_about(self):
+        self.about = AboutWindow(parent=self)
+        self.about.show()
+
+    # TODO: Delete me !
+    ###############
+    # DUMMY STUFF #
+    ###############
+
+    def open_dummy(self, title="Dummy"):
+        """
+        Open a dummy dialog window.
+        """
+        self.dummy = DummyWindow(
+            title=title if type(title) is str else "Dummy",
+            parent=self
+        )
+        self.dummy.show()
+        self.set_model([1,2,3])
+
+    def dummy_model(self):
+        self.set_model([1,2,3])
-- 
GitLab