]> SALOME platform Git repositories - modules/adao.git/blob - src/daSalome/daGUI/daEficasWrapper/eficasWrapper.py.in
Salome HOME
4b646df26d43ed61b9a0da1b1818d14e6c56fcfa
[modules/adao.git] / src / daSalome / daGUI / daEficasWrapper / eficasWrapper.py.in
1 # -*- coding: iso-8859-1 -*-
2 #  Copyright (C) 2010 EDF R&D
3 #
4 #  This library is free software; you can redistribute it and/or
5 #  modify it under the terms of the GNU Lesser General Public
6 #  License as published by the Free Software Foundation; either
7 #  version 2.1 of the License.
8 #
9 #  This library is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 #  Lesser General Public License for more details.
13 #
14 #  You should have received a copy of the GNU Lesser General Public
15 #  License along with this library; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 __author__="aribes/gboulant"
22
23 # HACK - Pour l'instant
24 import logging
25 logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s')
26 logger = logging.getLogger()
27
28 class DevelException(Exception):
29     def __init__(self, message):
30         """Canonical constructor"""
31         Exception.__init__(self,message)
32
33 #
34 # ==============================================================================
35 # Interface of an eficas observer (for implementing the subject/observer pattern)
36 # ==============================================================================
37 #
38 from daGuiImpl.enumerate import Enumerate
39 class EficasObserver:
40     """
41     This class specifies the interface of an eficas observer. See example at the
42     bottom of this file.
43     """
44     def processEficasEvent(self, eficasWrapper, eficasEvent):
45         """
46         This function should be implemented in the concrete Observer.
47         @param eficasWrapper the instance of the source EficasWrapper
48         @param eficasEvent the emitted event (instance of EficasEvent)
49         """
50         raise DevelException("processEficasEvent not implemented yet")
51
52 class EficasEvent:
53     EVENT_TYPES=Enumerate([
54         'CLOSE',
55         'SAVE'
56     ])
57
58     def __init__(self,eventType,callbackId=None):
59         """
60         Creates an eficas event to be used by an EficasWrapper to notify an
61         EficasObserver.
62
63         The eventType depends of the context of creation. It specify the nature
64         of the event inside EficasWrapper that triggers the creation of this instance.
65         
66         The callbackId is an object used by the EficasObserver to map the
67         notification (this received event) with the initial event (callback)
68         This object can be anything and has to be defined through a convention
69         that both the EficasWrapper and the EficasObserver can understand.
70         Typically, the eficas observer set the callback id to the eficas wrapper
71         before running the asynchronous show. Then, when an event is raised by
72         the eficas wrapper toward its observer, it embeds the callback id so that
73         the observer can match the received event to the initial trigger context.
74         
75         @param the eventType to be choosen in the EVENT_TYPES
76         @param callbackId an arbitrary data object
77         """
78         if not self.EVENT_TYPES.isValid(eventType):
79             raise DevelException("The event type "+str(eventType)+" is not defined")
80
81         self.eventType = eventType
82         self.callbackId = callbackId
83
84 #
85 # ==============================================================================
86 # Definition of an Eficas wrapper for integration to the SALOME framework
87 # This wrapper is not specific to the OMA context and is intended not to be.
88 # ==============================================================================
89 #
90 eficasPath = '@EFICAS_DIR@'
91 import os
92 if os.environ.has_key( "EFICAS_ROOT"):
93     eficasPath = os.environ["EFICAS_ROOT"]
94 import sys
95 sys.path[:0]=[eficasPath,
96               os.path.join( eficasPath,'Editeur'),
97               os.path.join( eficasPath,'UiQT4'),
98              ]
99
100 import Editeur
101 from InterfaceQT4 import qtEficas
102
103 class EficasWrapper(qtEficas.Appli):
104
105     def __init__(self, parent, code , fichier=None, module="Eficas", version=None, salome=0):
106
107         self.__codetype = code
108         self.__jdc = None
109
110         # variables for the notification procedure
111         self.__observer  = None
112         self.__callbackId = None
113
114         # The convention is that the python pref associated to the code are in
115         # a folder whose name is the code name with lower letters but the
116         # first that is in capital letter.
117         pathCode=code[0]+code[1:].lower()
118         sys.path[:0]=[os.path.join(eficasPath,pathCode)]
119
120         if Editeur.__dict__.has_key( 'session' ):
121             from Editeur import session
122             eficasArg = []
123             eficasArg += sys.argv
124             if fichier:
125                 eficasArg += [ fichier ]
126             if version:
127                 eficasArg += [ "-c", version ]
128             else :
129                 print "noversion"
130             session.parse( eficasArg )
131
132         qtEficas.Appli.__init__( self,code=code,salome=salome,parent=parent)
133
134     def setCodeType(self,code):
135         self.__codetype = code
136
137     def setJdc(self, value, name=None):
138         self.__jdc = value
139         if self.__jdc is None:
140             editor = self.viewmanager.getEditor()
141             editor.fichier = os.path.abspath(unicode("/tmp/eficasfile.comm"))
142         else:
143             fichier = os.path.abspath(unicode("/tmp/eficasfile.comm"))
144             f = open(fichier,'w')
145             f.write(self.__jdc)
146             f.close()
147             editor=self.viewmanager.getEditor( fichier,units=None)
148
149         # _TODO_ set the name and indicate that it's not used to ask a filename
150         #index=self.viewmanager.myQtab.currentIndex()
151         #self.viewmanager.myQtab.setTabText(index,fileName)
152
153     def getJdc(self):
154         # First update the jdc attribute and return the value
155         self.__jdc = self.__getJdcText()
156         return self.__jdc
157
158     # ==========================================================================
159     # Function for the notification interface between an EficasWrapper an an
160     # EficasObserver.
161     #
162     def addObserver(self, observer):
163         """
164         In fact, only one observer may be defined for the moment.
165         """
166         try:
167             observer.processEficasEvent
168         except:
169             raise DevelException("the argument should implement the function processEficasEvent")
170         self.__observer = observer
171
172     def notifyObserver(self, eventType):
173         eficasEvent = EficasEvent(eventType, self.getCallbackId())
174         self.__observer.processEficasEvent(self, eficasEvent)
175
176     def setCallbackId(self, callbackId):
177         """
178         This function should be used to define the callback identifier that
179         will be used by the observer to map the notifcation event with the source
180         event.
181         @param callbackId Any object to be used in the notification interface
182         """
183         self.__callbackId = callbackId
184
185     def getCallbackId(self):
186         return self.__callbackId
187
188     # ==========================================================================
189     # Functions to handle the Eficas callback functions
190     #
191     def closeEvent(self,event):
192         """
193         This method is a notification function called at the end of the eficas
194         processes quit/exit.
195         This close event has to be overload to handle the management of data
196         saving process, and to prevent the application from bug when reopen
197         eficas after a first close operation inside SALOME.
198         @overload
199         """
200         logger.debug("#### Handle closeEvent")
201
202         # Get the data and notify the observers of the event
203         # _TODO_ get data
204         self.notifyObserver(EficasEvent.EVENT_TYPES.CLOSE)
205         self.fileCloseAll()
206
207         # This is to prevent from bug when reopen eficas after a first close
208         # operation inside SALOME.
209         import InterfaceQT4.readercata
210         if hasattr(InterfaceQT4.readercata,'reader') :
211            del InterfaceQT4.readercata.reader
212         event.accept()
213
214
215     def fileSave(self):
216         """
217         @overload
218         """
219         logger.debug("This is my fileSave")
220         # Notify the observers of the event
221         self.notifyObserver(EficasEvent.EVENT_TYPES.SAVE)
222
223         qtEficas.Appli.fileSave(self)
224
225
226     def fileSaveAs(self):
227         """
228         @overload
229         """
230         logger.debug("This is my fileSaveAs")
231         self.fileSave()
232
233     def fileClose(self):
234         """
235         @overload
236         """
237         logger.debug("This is my fileClose")
238         qtEficas.Appli.fileClose(self)
239
240     def fileCloseAll(self):
241         """
242         @overload
243         """
244         logger.debug("This is my fileCloseAll")
245         qtEficas.Appli.fileCloseAll(self)
246
247     # ==========================================================================
248     # helper functions
249     #
250     def __getJdcText(self):
251         """
252         This function returns the current jdc in text format.
253         """
254         index=self.viewmanager.myQtab.currentIndex()
255         if index < 0 : return
256
257         editor=self.viewmanager.dict_editors[index]
258         format = self.format_fichier
259         strSource = str( editor.get_text_JDC(format) )
260         return strSource
261
262     def displayNew(self):
263         self.fileNew()
264         self.show()
265
266     # ==========================================================================
267     # Test functions ...
268     #
269     def __displayData(self, textjdc):
270         index=self.viewmanager.myQtab.currentIndex()
271         if index < 0 : return
272
273         editor=self.viewmanager.dict_editors[index]
274
275         jdc=editor.readercata.cata[0].JdC(procedure=textjdc,appli=editor,cata=editor.readercata.cata,cata_ord_dico=editor.readercata.cata_ordonne_dico,nom="JDC name",rep_mat=editor.CONFIGURATION.rep_mat)
276         self.viewmanager.getEditor("toto.com",jdc)
277
278