MainWindow.py 8.01 KB
Newer Older
# -*- coding: utf-8 -*-

import os

from PyQt5 import QtGui
from PyQt5.QtWidgets import (
    QMainWindow, QApplication, QAction,
)
from PyQt5.uic import loadUi
from view.ListedSubWindow import ListedSubWindow
from view.DummyWindow import DummyWindow
from view.ConfigureWindow import ConfigureWindow
from view.NewStudyWindow import NewStudyWindow
from view.AboutWindow import AboutWindow
from model.Study import Study

no_model_action = [
    "action_menu_new", "action_menu_open", "action_menu_import_mage",
    "action_menu_import_rubarbe", "action_toolBar_open",
    "action_menu_close", "action_menu_edit", "action_menu_save",
    "action_menu_save_as", "action_toolBar_close", "action_toolBar_save",
    "action_toolBar_run_solver", "action_toolBar_kill_solver"
    "action_toolBar_network", "action_toolBar_geometry",
    "action_toolBar_maillage", "action_toolBar_run_mailleur",
    "action_toolBar_cond_limites", "action_toolBar_App_Lateraux",
    "action_toolBar_Deversements", "action_toolBar_Troncons",
    "action_toolBar_Frottements", "action_toolBar_Ouvrages",
]

action = (
    no_model_action + model_action + define_model_action
)

class ApplicationWindow(QMainWindow, ListedSubWindow):
    def __init__(self, conf=None):
        super(ApplicationWindow, self).__init__()
        # App Configuration
        self.conf = conf

            os.path.join(os.path.dirname(__file__), "ui", "MainWindow.ui"),
        self.init_callback()
        self.default_style()
    def enable_actions(self, action:str, enable:bool):
        """Enable of disable an action componant

        Args:
            action: Action to enable/disable
            enable: True to Enable, or False to disable

        Returns:
            Nothing
        """
        self.ui.findChild(QAction, action).setEnabled(enable)
        """Connect action to callback function

        Returns:
            Nothing
        """
        actions = {
            # Menu action
            "action_menu_config": self.open_configure,
            "action_menu_new": self.open_new_study,
            "action_menu_edit": self.open_edit_study,
            "action_menu_open": self.open_model,
            "action_menu_save": self.save_study,
            "action_menu_save_as": self.save_as_study,
            ## Help
            "action_menu_about": self.open_about,
            # ToolBar action
            "action_toolBar_quit": self.close,
            "action_toolBar_open": self.open_model,
            "action_toolBar_save": self.save_study,
            "action_toolBar_close": self.close_model,
            "action_toolBar_run_solver": self.open_dummy,
            "action_toolBar_kill_solver": self.open_dummy,
            "action_toolBar_listing": self.open_dummy,
            ## Current actions
            "action_toolBar_network": lambda: self.open_dummy("Network"),
            "action_toolBar_geometry": lambda: self.open_dummy("Geomerty"),
            "action_toolBar_maillage": lambda: self.open_dummy("Maillage"),
            "action_toolBar_run_mailleur": lambda: self.open_dummy("Lancement mailleur externe"),
            "action_toolBar_cond_limites": lambda: self.open_dummy("Condition Limites"),
            "action_toolBar_App_Lateraux": lambda: self.open_dummy("Apport Lateraux"),
            "action_toolBar_Deversements": lambda: self.open_dummy("Deversement"),
            "action_toolBar_Troncons": lambda: self.open_dummy("Tronçons"),
            "action_toolBar_Frottements": lambda: self.open_dummy("Frottements"),
            "action_toolBar_Ouvrages": lambda: self.open_dummy("Ouvrages"),
        }

        for action in actions:
            self.ui.findChild(QAction, action)\
                   .triggered.connect(actions[action])
        """Set default window style

        Returns:
            Nothing
        """
        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):
        """Update status of action componante
        Update status of action componant, enable or disable in
        function of model state

        Returns:
            Nothing
        """
        no_model = self.model is None

        for action in no_model_action:
            self.enable_actions(action, no_model)

        for action in define_model_action + other_model_action:
            self.enable_actions(action, not no_model)

        for action in model_action:
            self.enable_actions(action, not no_model)

    ############
    # FEATURES #
    ############

    def save_study(self):
        """Save current study

        Save current study, if study as no associate file, open a
        file dialog.

        Returns:
            Nothing
        """
        if self.model.filename == "":
            file_name, _ = QFileDialog.getSaveFileName(
                self, "Save File",
                "", "Pamhyr(*.pkl)")
            if file_name[-4:] == ".pkl":
                self.model.filename = file_name
            else:
                self.model.filename = file_name + ".pkl"

        self.model.save()

    def save_as_study(self):
        """Save current study as new file

        Save current study as new file, if study as no associate file,
        open a file dialog.

        Returns:
            Nothing
        """
        file_name, _ = QFileDialog.getSaveFileName(
            self, "Save File",
            "", "Pamhyr(*.pkl)")
        if file_name[-4:] == ".pkl":
            self.model.filename = file_name
        else:
            self.model.filename = file_name + ".pkl"

        self.model.save()

    #############
    # SUBWINDOW #
    #############

    def open_configure(self):
        """Open configure window

        Open PamHyr configure window

        Returns:
            Nothing
        """
        self.config = ConfigureWindow(conf=self.conf, parent=self)
        self.config.show()

        """Open about window

        Open a new window with information about PamHyr

        Returns:
            Nothing
        """
        self.about = AboutWindow(parent=self)
        self.about.show()

    def open_model(self):
        """Open file dialog to select saved model

        Returns:
            Nothing
        """
        if self.model is None:
            dialog = QFileDialog(self)
            dialog.setFileMode(QFileDialog.FileMode.ExistingFile)
            dialog.setDefaultSuffix(".pkl")
            #dialog.setFilter(dialog.filter() | QtCore.QDir.Hidden)
            dialog.setNameFilters(['PamHyr (*.pkl)'])

            if dialog.exec_():
                file_name = dialog.selectedFiles()
                self.set_model(Study.open(file_name[0]))
                print(f"[MainWindow] Open Study : {self.model.name}")

    def open_new_study(self):
        """Open dialog to set new study

        Returns:
            Nothing
        """
        if self.model is None:
            self.new_study = NewStudyWindow(parent=self)
            self.new_study.show()

    def open_edit_study(self):
        """Open dialog to set new study

        Returns:
            Nothing
        """
        if not self.model is None:
            self.new_study = NewStudyWindow(study=self.model, parent=self)
            self.new_study.show()


    # TODO: Delete me !
    ###############
    # DUMMY STUFF #
    ###############

    def open_dummy(self, title="Dummy"):
        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])