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