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