Salome HOME
taille fenetre eficas pour coupling mascaret telemac2d
[modules/hydrosolver.git] / src / salome_hydro / coupling1d2d / eficas / appli.py
1 # -*- coding: utf-8 -*-
2 #
3 #  Copyright (C) 2012-2013 EDF
4 #
5 #  This file is part of SALOME HYDRO module.
6 #
7 #  SALOME HYDRO module is free software: you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation, either version 3 of the License, or
10 #  (at your option) any later version.
11 #
12 #  SALOME HYDRO module is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with SALOME HYDRO module.  If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import sys
22 import re
23
24 from PyQt5.QtWidgets import QMessageBox , QScrollArea, QGridLayout
25
26 import salome
27 import SalomePyQt
28 sgPyQt = SalomePyQt.SalomePyQt()
29
30 from salome.kernel.logger import Logger
31 from salome.kernel import termcolor
32 logger = Logger("salome.hydro.coupling1d2d.eficas.appli",
33                 color = termcolor.GREEN_FG)
34
35 import eficasSalome
36
37 from salome.hydro.study import HydroStudyEditor
38
39 class SalomeEntry:
40     
41     enable_salome_selection = True
42     help_message = u"Une entrée de l'arbre d'étude de Salome est attendue"
43     
44     def __init__(self, entryStr):
45         self._entry = entryStr
46     
47     @staticmethod
48     def __convert__(entryStr):
49         return SalomeEntry(entryStr)
50     
51     @staticmethod
52     def get_selected_value(selected_entry, study_editor):
53         sobj = study_editor.study.FindObjectID(selected_entry)
54         name = sobj.GetName()
55         return "%s (%s)" % (name, selected_entry)
56
57 class EficasForCoupling1D2DAppli(eficasSalome.MyEficas):
58     """
59     This class launches Eficas and adds entries for the created files in
60     HYDRO component in the study tree. The messages in this class are in
61     french because they are displayed in Eficas interface.
62
63     :type  fichier: string
64     :param fichier: path of an Eficas file to open
65
66     """
67     def __init__(self, fichier = None, version = None):
68         self.ed = HydroStudyEditor()
69         self.codedir = os.path.dirname(__file__)
70         sys.path[:0] = [self.codedir]
71         eficasSalome.MyEficas.__init__(self, sgPyQt.getDesktop(), "coupling1d2d",
72                                        fichier, version = version)
73         area = QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
74         gridLayout = QGridLayout(area)
75         gridLayout.addWidget(self)
76         area.setWidgetResizable(1)
77
78         sgPyQt.createView("Eficas Coupling 1D/2D", self)
79
80     def selectGroupFromSalome(self, kwType = None, editor=None):
81         """
82         Select an entry from Salome object browser
83         """
84         nbEntries = salome.sg.SelectedCount()
85         if nbEntries < 1:
86             msg = u"Veuillez sélectionner une entrée de l'arbre d'étude de Salome"
87             QMessageBox.information(self, self.tr(u"Sélection depuis Salome"), self.tr(msg))
88             return [], msg
89         elif nbEntries > 1:
90             msg = u"Une seule entrée doit être sélectionnée dans l'arbre d'étude de Salome"
91             QMessageBox.information(self, self.tr(u"Sélection depuis Salome"), self.tr(msg))
92             return [], msg
93         else:
94             try:
95                 value = kwType.get_selected_value(salome.sg.getSelected(0), self.ed.editor)
96                 msg = u"L'entrée de l'arbre d'étude de Salome a été sélectionnée"
97                 return [value], msg
98             except Exception, e:
99                 QMessageBox.information(self, self.tr(u"Sélection depuis Salome"), self.tr(unicode(e)))
100                 return [], unicode(e)
101
102     def addJdcInSalome(self, jdcPath):
103         """
104         Add the newly created file in Salome study
105         """
106         try:
107             self.ed.find_or_create_coupling1d2d_case(jdcPath)
108         except Exception, exc:
109             msgError = "Can't add file to Salome study tree"
110             logger.exception(msgError)
111             QMessageBox.warning(self, self.tr("Warning"),
112                                 self.tr("%s. Reason:\n%s\n\nSee logs for more details." % (msgError, exc)))
113         salome.sg.updateObjBrowser(0)
114
115     def closeEvent(self, event):
116         while self.codedir in sys.path:
117             sys.path.remove(self.codedir)
118         eficasSalome.MyEficas.closeEvent(self, event)