Salome HOME
Correction de la fonction Remove
[modules/adao.git] / src / daSalome / daGUI / daEficasWrapper / adaoEficasWrapper.py
1 # -*- coding: utf-8 -*-
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 import sys
22 import os
23 from PyQt4 import QtGui,QtCore
24
25 # Import from EFICAS_SRC
26 import eficasSalome
27 # Import from Eficas
28 from InterfaceQT4 import qtEficas
29
30 from adaoWrapperUtils import *
31
32 #
33 # ============================================
34 # Specialization of the EficasWrapper for ADAO
35 # ============================================
36 #
37 class AdaoEficasWrapper(eficasSalome.MyEficas):
38
39     def __init__(self, parent):
40         # Configuration de l'installation
41         # Permet à EFICAS de faire ses import correctement
42         my_path = os.path.dirname(os.path.abspath(__file__))
43         ADAO_INSTALL_DIR = my_path + "/../daEficas"
44         sys.path[:0]=[ADAO_INSTALL_DIR]
45
46         self.__myCallbackId = {}
47         self.__close_editor = None
48         self.__file_open_name = ""
49         self.__parent = parent
50
51     def init_gui(self):
52       eficasSalome.MyEficas.__init__(self, self.__parent, code="ADAO", module="ADAO")
53       # On réouvre tous les fichiers comm
54       # On fait une copie pour ne pas tomber dans une boucle infinie
55       save_CallbackId =  self.__myCallbackId.copy()
56       for editor, myCallbackId in save_CallbackId.iteritems():
57         self.notifyObserver(EficasEvent.EVENT_TYPES.REOPEN, callbackId=myCallbackId)
58
59     def addJdcInSalome(  self, jdcPath ):
60       # On gere nous meme l'etude
61       pass
62
63     def fileNew(self):
64         """
65         @overload
66         """
67         qtEficas.Appli.fileNew(self)
68         self.notifyObserver(EficasEvent.EVENT_TYPES.NEW)
69
70     def openEmptyCase(self, callbackId):
71         qtEficas.Appli.fileNew(self)
72         self.removeCallbackId(callbackId)
73         self.setCallbackId(callbackId)
74
75     def fileSave(self):
76         """
77         @overload
78         """
79         qtEficas.Appli.fileSave(self)
80         index = self.viewmanager.myQtab.currentIndex()
81         if index > -1 :
82           self.notifyObserver(EficasEvent.EVENT_TYPES.SAVE)
83
84     def fileSaveAs(self):
85         """
86         @overload
87         """
88         qtEficas.Appli.fileSaveAs(self)
89         self.notifyObserver(EficasEvent.EVENT_TYPES.SAVE)
90
91     def getCaseName(self):
92       if self.__close_editor is None:
93         index = self.viewmanager.myQtab.currentIndex()
94         CaseName = self.viewmanager.myQtab.tabText(index)
95         return CaseName
96       else:
97         CaseName = str(self.__close_editor.fichier.split('/')[-1])
98         return CaseName
99
100     def getFileCaseName(self):
101       if self.__close_editor is None:
102         index = self.viewmanager.myQtab.currentIndex()
103         editor = self.viewmanager.dict_editors[index]
104         return editor.fichier
105       else:
106         return self.__close_editor.fichier
107
108     def Openfile(self, filename):
109       self.viewmanager.handleOpen(fichier=filename)
110
111     def handleOpenRecent(self):
112       """
113       @overload
114       """
115       idx = self.sender()
116       fichier = self.ficRecents[idx]
117       self.__file_open_name = fichier
118       self.notifyObserver(EficasEvent.EVENT_TYPES.OPEN)
119       self.__file_open_name = ""
120
121     def fileOpen(self):
122         """
123         @overload
124         """
125         fichier = QtGui.QFileDialog.getOpenFileName(self,
126                                                     self.trUtf8('Ouvrir Fichier'),
127                                                     self.CONFIGURATION.savedir,
128                                                     self.trUtf8('JDC Files (*.comm);;''All Files (*)'))
129         if fichier.isNull(): return
130         self.__file_open_name = fichier
131         self.notifyObserver(EficasEvent.EVENT_TYPES.OPEN)
132         self.__file_open_name = ""
133
134     def getOpenFileName(self):
135       return str(self.__file_open_name)
136
137     def selectCase(self, callbackId):
138       rtn = False
139       for editor, myCallbackId in self.__myCallbackId.iteritems():
140         if myCallbackId[0] == callbackId[0]:
141           if myCallbackId[1].GetID() == callbackId[1].GetID():
142             try:
143               for indexEditor in self.viewmanager.dict_editors.keys():
144                 if editor is self.viewmanager.dict_editors[indexEditor]:
145                   self.viewmanager.myQtab.setCurrentIndex(indexEditor)
146                   rtn = True
147             except:
148               pass
149       return rtn
150
151     def fileClose(self):
152         """
153         @overload
154         """
155         index = self.viewmanager.myQtab.currentIndex()
156         self.__close_editor = self.viewmanager.dict_editors[index]
157         res = self.viewmanager.handleClose(self)
158         if res != 2: # l utilsateur a annule
159           if self.__close_editor.fichier is None:
160             # We have to destroy the case
161             self.notifyObserver(EficasEvent.EVENT_TYPES.DESTROY)
162             self.__myCallbackId.pop(self.__close_editor)
163           else:
164             # Il faudrait en faire plus -> Voir Edit dans SALOME !
165             self.notifyObserver(EficasEvent.EVENT_TYPES.SAVE)
166             self.__myCallbackId.pop(self.__close_editor)
167         self.__close_editor = None
168         return res
169
170     def fileCloseAll(self):
171       """
172       @overload
173       """
174       while len(self.viewmanager.dict_editors) > 0:
175         self.viewmanager.myQtab.setCurrentIndex(0)
176         if self.viewmanager.myQtab.currentIndex() == 0:
177           res = self.fileClose()
178           if res==2 : return res   # l utilsateur a annule
179         else:
180           return 0
181
182     # ==========================================================================
183     # Function for the notification interface between an EficasWrapper an an
184     # EficasObserver.
185
186     # Association de l'objet editor avec le callbackId
187     def setCallbackId(self, callbackId):
188       index = self.viewmanager.myQtab.currentIndex()
189       self.__myCallbackId[self.viewmanager.dict_editors[index]] = callbackId
190
191     def removeCallbackId(self, callbackId):
192       key_to_remove = None
193       print callbackId
194       for k, v in self.__myCallbackId.iteritems():
195         print k, v
196         if v[0] == callbackId[0] and v[1].GetID() == callbackId[1].GetID():
197           key_to_remove = k
198       if key_to_remove is not None:
199         del self.__myCallbackId[key_to_remove]
200       else:
201         print "Oups - cannot find callbackId"
202
203     def getCallbackId(self):
204       if self.__close_editor is None:
205         index = self.viewmanager.myQtab.currentIndex()
206         return self.__myCallbackId[self.viewmanager.dict_editors[index]]
207       else:
208         return self.__myCallbackId[self.__close_editor]
209
210     def addObserver(self, observer):
211         """
212         In fact, only one observer may be defined for the moment.
213         """
214         try:
215             observer.processEficasEvent
216         except:
217             raise DevelException("the argument should implement the function processEficasEvent")
218         self.__observer = observer
219
220     def notifyObserver(self, eventType, callbackId=None):
221       if eventType != EficasEvent.EVENT_TYPES.NEW and eventType != EficasEvent.EVENT_TYPES.OPEN:
222         if callbackId is None :
223           eficasEvent = EficasEvent(eventType, self.getCallbackId())
224         else:
225           eficasEvent = EficasEvent(eventType, callbackId)
226       else:
227         eficasEvent = EficasEvent(eventType)
228       self.__observer.processEficasEvent(self, eficasEvent)
229