Ejemplo n.º 1
0
def _ExecuteTestSaveProject(test_case, project_name=None):
    if not project_name:
        project_name = Path(test_case._testMethodName)

    project_dir = project_name.with_suffix(".ksp")

    test_case.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))

    manager = ProjectManager()

    test_case.assertTrue(manager.SaveProject(project_name))

    plugin_data_path = project_dir / "plugin_data.json"

    # check the files were created
    test_case.assertTrue(project_dir.is_dir())
    test_case.assertTrue(plugin_data_path.is_file())
    test_case.assertTrue(project_dir.joinpath("salome_study.hdf").is_file())

    # check content of "plugin_data.json"
    with open(plugin_data_path, 'r') as plugin_data_file:
        plugin_data = json.load(plugin_data_file)

    test_case.assertIn("general", plugin_data)
    test_case.assertIn("groups", plugin_data)
    test_case.assertNotIn(
        "application",
        plugin_data)  # no app was loaded hence this shouldn't exist

    general = plugin_data["general"]
    test_case.assertIn("version_plugin", general)
    test_case.assertIn("version_salome", general)
    test_case.assertIn("creation_time", general)
    test_case.assertIn("operating_system", general)

    return project_name
Ejemplo n.º 2
0
class PluginController:
    def __init__(self):
        logger.debug('Creating PluginController')
        self._main_window = PluginMainWindow()
        active_window.ACTIVE_WINDOW = self._main_window

        self.__InitializeMembers()

        self.__ConnectMainWindow()

    def __InitializeMembers(self) -> None:
        """completely reinitialize members to clean them"""
        self._project_manager = ProjectManager()
        self._project_path_handler = ProjectPathHandler()
        self._previous_save_path = None

    def __ConnectMainWindow(self) -> None:
        ### File menu
        self._main_window.actionNew.triggered.connect(self._New)
        self._main_window.actionOpen.triggered.connect(self._Open)
        self._main_window.actionSave.triggered.connect(self._Save)
        self._main_window.actionSave_As.triggered.connect(self._SaveAs)
        self._main_window.actionSettings.triggered.connect(self._Settings)
        self._main_window.actionClose.triggered.connect(self._Close)

        ### Kratos menu
        self._main_window.actionGroups.triggered.connect(self._Groups)
        self._main_window.actionLoad_Application.triggered.connect(
            self._LoadApplication)
        self._main_window.actionImport_MDPA.triggered.connect(self._ImportMdpa)

        ### Help menu
        self._main_window.actionAbout.triggered.connect(
            lambda: ShowAbout(self._main_window))
        self._main_window.actionWebsite.triggered.connect(
            lambda: webbrowser.open(
                "https://github.com/philbucher/KratosSalomePlugin"))

        ### Startup buttons
        self._main_window.pushButton_Open.clicked.connect(self._Open)
        self._main_window.pushButton_Load_Application.clicked.connect(
            self._LoadApplication)

    ### File menu
    def _New(self) -> None:
        """Create new project. Salome study is untouched
        # TODO check for unsaved changes
        """
        self.__InitializeMembers()

    def _Open(self) -> None:
        """Open a project. User is asked for the path to the project folder
        # TODO check for unsaved changes
        """
        try:
            path = self._project_path_handler.GetOpenPath(self._main_window)
        except UserInputError as e:
            msg = "User input error while opening project: {}".format(e)
            logger.warning(msg)
            self._main_window.StatusBarWarning(msg)
            return

        if path == Path(
                "."
        ):  # this means the dialog was aborted, do nothing in this case
            msg = "Opening was aborted"
            logger.info(msg)
            self._main_window.StatusBarWarning(msg)
            return

        open_successful = self._project_manager.OpenProject(path)

        if open_successful:
            msg = 'Successfully opened project from "{}"'.format(path)
            logger.info(msg)
            self._main_window.StatusBarInfo(msg)
        else:
            logger.critical('Failed to open project from "%s"!', path)

    def _Save(self) -> None:
        """Save the project. If it was saved before the same path is reused.
        Otherwise SaveAs is used and the user is asked to provide a path for saving
        """
        if self._previous_save_path:
            logger.info("Saving project with previous save path ...")
            self.__SaveProject(self._previous_save_path)
        else:  # project was not previously saved
            self._SaveAs()

    def _SaveAs(self) -> None:
        """Save the project. The user is asked to provide a path for saving.
        If the dialog for giving the path is aborted then nothing happens
        """
        logger.info("Saving project as ...")

        path = self._project_path_handler.GetSavePath(self._main_window)

        if path == Path(
                "."
        ):  # this means the dialog was aborted, do nothing in this case
            msg = "Saving was aborted"
            logger.info(msg)
            self._main_window.StatusBarWarning(msg)
            return

        save_successful = self.__SaveProject(path)
        if save_successful:
            self._previous_save_path = path  # saving the path such that it can be reused

    def _Settings(self) -> None:
        raise Exception("Example of showing exception in messagebox")
        ShowNotImplementedMessage()

    def _Close(self) -> None:
        """Close the plugin window
        State is preserved!
        TODO check for unsaved changes(?)
        TODO if nothing else is implemented this can be moved to the PluginMainWindow
        """
        self._main_window.close()

    ### Kratos menu
    def _Groups(self) -> None:
        logger.critical("This is a critical messagbox example")
        ShowNotImplementedMessage()

    def _LoadApplication(self) -> None:
        ShowNotImplementedMessage()

    def _ImportMdpa(self) -> None:
        ShowNotImplementedMessage()

    def __SaveProject(self, path: Path) -> bool:
        """internal function for saving the project
        and issue the appropriate infos for the user
        returns if saving was successful
        """
        save_successful = self._project_manager.SaveProject(path)
        if save_successful:
            msg = 'Saved project under "{}"'.format(path)
            logger.info(msg)
            self._main_window.StatusBarInfo(msg)
        else:
            logger.critical('Failed to save project under "%s"!', path)
        return save_successful