Salome HOME
272a81e01151357f904904b7420752ce59cca423
[modules/adao.git] / src / daSalome / daGUI / daGuiImpl / adaoGuiManager.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 """
22 This file centralizes the definitions and implementations of ui components used
23 in the GUI part of the module.
24 """
25
26 __author__ = "aribes/gboulant"
27
28 import traceback
29 from PyQt4.QtCore import QObject
30 from PyQt4 import QtGui,QtCore
31 import SalomePyQt
32 sgPyQt = SalomePyQt.SalomePyQt()
33
34 from daGuiImpl.enumerate import Enumerate
35 from daGuiImpl.adaoCase import AdaoCase
36 from daEficasWrapper.adaoEficasWrapper import AdaoEficasWrapper
37 from daEficasWrapper.eficasWrapper import EficasObserver
38 from daEficasWrapper.eficasWrapper import EficasEvent
39 import adaoGuiHelper
40 import adaoStudyEditor
41
42 __cases__ = {}
43
44 #
45 # ==============================================================================
46 # Classes to manage the building of UI components
47 # ==============================================================================
48 #
49 UI_ELT_IDS = Enumerate([
50         'ADAO_MENU_ID',
51         'NEW_ADAOCASE_ID',
52         'OPEN_ADAOCASE_ID',
53         'EDIT_ADAOCASE_POP_ID',
54         'REMOVE_ADAOCASE_POP_ID',
55         'YACS_EXPORT_POP_ID',
56         ],offset=950)
57
58 ACTIONS_MAP={
59     UI_ELT_IDS.NEW_ADAOCASE_ID:"newAdaoCase",
60     UI_ELT_IDS.OPEN_ADAOCASE_ID:"openAdaoCase",
61     UI_ELT_IDS.EDIT_ADAOCASE_POP_ID:"editAdaoCase",
62     UI_ELT_IDS.REMOVE_ADAOCASE_POP_ID:"removeAdaoCase",
63     UI_ELT_IDS.YACS_EXPORT_POP_ID:"exportCaseToYACS",
64 }
65
66 class AdaoGuiUiComponentBuilder:
67     """
68     The initialisation of this class creates the graphic components involved
69     in the GUI (menu, menu item, toolbar). A ui component builder should be
70     created for each opened study and associated to its context (see usage in OMAGUI.py).
71     """
72     def __init__(self):
73         self.initUiComponents()
74
75     def initUiComponents(self):
76
77         objectTR = QObject()
78
79         # create top-level menu
80         mid = sgPyQt.createMenu( "ADAO", -1, UI_ELT_IDS.ADAO_MENU_ID, sgPyQt.defaultMenuGroup() )
81         # create toolbar
82         tid = sgPyQt.createTool( "ADAO" )
83
84         a = sgPyQt.createAction( UI_ELT_IDS.NEW_ADAOCASE_ID, "New case", "New case", "Create a new adao case", "" )
85         sgPyQt.createMenu(a, mid)
86         sgPyQt.createTool(a, tid)
87         a = sgPyQt.createAction( UI_ELT_IDS.OPEN_ADAOCASE_ID, "Open case", "Open case", "Open a adao case", "" )
88         sgPyQt.createMenu(a, mid)
89         sgPyQt.createTool(a, tid)
90
91         # the following action are used in context popup
92         a = sgPyQt.createAction( UI_ELT_IDS.EDIT_ADAOCASE_POP_ID, "Edit case", "Edit case", "Edit the selected study case", "" )
93         a = sgPyQt.createAction( UI_ELT_IDS.REMOVE_ADAOCASE_POP_ID, "Remove case", "Remove case", "Remove the selected study case", "" )
94         a = sgPyQt.createAction( UI_ELT_IDS.YACS_EXPORT_POP_ID, "Export to YACS", "Export to YACS", "Generate a YACS graph executing this case", "" )
95
96     def createPopupMenuOnItem(self,popup,salomeSudyId, item):
97         if adaoStudyEditor.isValidAdaoCaseItem(salomeSudyId, item):
98           popup.addAction( sgPyQt.action( UI_ELT_IDS.EDIT_ADAOCASE_POP_ID ) )
99           popup.addAction( sgPyQt.action( UI_ELT_IDS.REMOVE_ADAOCASE_POP_ID ) )
100           popup.addAction( sgPyQt.action( UI_ELT_IDS.YACS_EXPORT_POP_ID ) )
101
102         return popup
103
104 class AdaoGuiActionImpl(EficasObserver):
105     """
106     This class implements the ui actions concerning the management of oma study
107     cases.
108     """
109
110     def __init__(self):
111         pass
112         # This dialog is created once so that it can be recycled for each call
113         # to newOmaCase().
114         #self.__dlgNewStudyCase = DlgNewStudyCase()
115         self.__parent = SalomePyQt.SalomePyQt().getDesktop()
116         self.__dlgEficasWrapper = AdaoEficasWrapper(parent=SalomePyQt.SalomePyQt().getDesktop())
117         self.__dlgEficasWrapper.addObserver(self)
118         self.__Eficas_viewId = -1
119
120     # ==========================================================================
121     # Processing of ui actions
122     #
123     def processAction(self,actionId):
124         """
125         Main switch function for ui actions processing
126         """
127         if ACTIONS_MAP.has_key(actionId):
128             try:
129                 functionName = ACTIONS_MAP[actionId]
130                 getattr(self,functionName)()
131             except:
132                 traceback.print_exc()
133         else:
134             msg = "The requested action is not implemented: " + str(actionId)
135             print msg
136
137     def showEficas(self):
138       if self.__Eficas_viewId == -1:
139         self.__dlgEficasWrapper.init_gui()
140
141
142         # Scroll Widget
143         area = QtGui.QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
144         area.setWidget( self.__dlgEficasWrapper)
145         area.setWidgetResizable(1)
146
147         wmType = "ADAO View"
148         self.__Eficas_viewId = sgPyQt.createView(wmType, area)
149         sgPyQt.setViewClosable(self.__Eficas_viewId, False)
150       else:
151         if SalomePyQt.SalomePyQt().getActiveView() != self.__Eficas_viewId :
152           result_activate = SalomePyQt.SalomePyQt().activateView(self.__Eficas_viewId)
153           if result_activate == False:
154             self.__dlgEficasWrapper.init_gui()
155
156             # Scroll Widget
157             area = QtGui.QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
158             area.setWidget( self.__dlgEficasWrapper)
159             area.setWidgetResizable(1)
160
161             wmType = "ADAO View"
162             self.__Eficas_viewId = sgPyQt.createView(wmType, area)
163             sgPyQt.setViewClosable(self.__Eficas_viewId, False)
164
165     def activate(self):
166       self.showEficas()
167
168     def newAdaoCase(self):
169       self.showEficas()
170       self.__dlgEficasWrapper.fileNew()
171
172     def openAdaoCase(self):
173       self.showEficas()
174       global __cases__
175       fichier = QtGui.QFileDialog.getOpenFileName(SalomePyQt.SalomePyQt().getDesktop(),
176                                                   self.__dlgEficasWrapper.trUtf8('Ouvrir Fichier'),
177                                                   self.__dlgEficasWrapper.CONFIGURATION.savedir,
178                                                   self.__dlgEficasWrapper.trUtf8('JDC Files (*.comm);;''All Files (*)'))
179       if fichier.isNull(): return
180       new_case = AdaoCase()
181       new_case.set_filename(str(fichier))
182       new_case.set_name(str(fichier.split('/')[-1]))
183       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
184       salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, new_case)
185       case_key = (salomeStudyId, salomeStudyItem.GetID())
186       __cases__[case_key] = new_case
187
188       # Open file in Eficas
189       self.__dlgEficasWrapper.Openfile(new_case.get_filename())
190       callbackId = [salomeStudyId, salomeStudyItem]
191       self.__dlgEficasWrapper.setCallbackId(callbackId)
192       self.showEficas()
193       adaoGuiHelper.refreshObjectBrowser()
194
195     def editAdaoCase(self):
196       # First we show eficas - all cases are reloaded
197       global __cases__
198
199       # Take study item
200       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
201       salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
202       case_key = (salomeStudyId, salomeStudyItem.GetID())
203
204       # ShowEficas, If case is an empty case - case is destroyed by reopen
205       self.showEficas()
206       try:
207         case = __cases__[case_key]
208         # Search if case is in Eficas !
209         callbackId = [salomeStudyId, salomeStudyItem]
210         case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
211
212         # If case is not in eficas Open It !
213         if case_open_in_eficas == False:
214           if case.get_filename() != "":
215             self.__dlgEficasWrapper.Openfile(case.get_filename())
216             callbackId = [salomeStudyId, salomeStudyItem]
217             self.__dlgEficasWrapper.setCallbackId(callbackId)
218       except:
219         # Case has been destroyed - create a new one
220         self.__dlgEficasWrapper.fileNew()
221
222     def removeAdaoCase(self):
223       global __cases__
224
225       # First step: selectCase
226       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
227       salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
228       callbackId = [salomeStudyId, salomeStudyItem]
229       case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
230       # If case is in eficas close it !
231       if case_open_in_eficas:
232         self.__dlgEficasWrapper.fileClose()
233
234       # Test if case exists
235       case_key = (salomeStudyId, salomeStudyItem.GetID())
236       if __cases__.has_key(case_key):
237         __cases__.pop(case_key)
238         adaoStudyEditor.removeItem(salomeStudyId, salomeStudyItem)
239         adaoGuiHelper.refreshObjectBrowser()
240
241       # Remove Callback in adaoEficasWrapper
242       self.__dlgEficasWrapper.removeCallbackId(callbackId)
243
244     def exportCaseToYACS(self):
245       global __cases__
246       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
247       salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
248       case_key = (salomeStudyId, salomeStudyItem.GetID())
249       case = __cases__[case_key]
250
251       msg = case.exportCaseToYACS()
252       if msg != "":
253         adaoGuiHelper.gui_warning(self.__parent, msg)
254
255     # ==========================================================================
256     # Processing notifications from eficas
257     #
258     __processOptions={
259         EficasEvent.EVENT_TYPES.CLOSE : "_processEficasCloseEvent",
260         EficasEvent.EVENT_TYPES.SAVE  : "_processEficasSaveEvent",
261         EficasEvent.EVENT_TYPES.NEW  : "_processEficasNewEvent",
262         EficasEvent.EVENT_TYPES.DESTROY  : "_processEficasDestroyEvent",
263         EficasEvent.EVENT_TYPES.OPEN  : "_processEficasOpenEvent",
264         EficasEvent.EVENT_TYPES.REOPEN  : "_processEficasReOpenEvent"
265         }
266     def processEficasEvent(self, eficasWrapper, eficasEvent):
267         """
268         Implementation of the interface EficasObserver. The implementation is a
269         switch on the possible types of events defined in EficasEvent.EVENT_TYPES.
270         @overload
271         """
272         functionName = self.__processOptions.get(eficasEvent.eventType, lambda : "_processEficasUnknownEvent")
273         return getattr(self,functionName)(eficasWrapper, eficasEvent)
274
275     def _processEficasCloseEvent(self, eficasWrapper, eficasEvent):
276         pass
277
278     def _processEficasNewEvent(self, eficasWrapper, eficasEvent):
279       global __cases__
280       new_case = AdaoCase()
281       case_name = eficasWrapper.getCaseName()
282       new_case.set_name(case_name)
283       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
284       salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, new_case)
285       case_key = (salomeStudyId, salomeStudyItem.GetID())
286       __cases__[case_key] = new_case
287       adaoGuiHelper.refreshObjectBrowser()
288       callbackId = [salomeStudyId, salomeStudyItem]
289       self.__dlgEficasWrapper.setCallbackId(callbackId)
290
291     def _processEficasReOpenEvent(self, eficasWrapper, eficasEvent):
292       global __cases__
293       try:
294         callbackId = eficasEvent.callbackId
295         [salomeStudyId, salomeStudyItem] = callbackId
296         case_key = (salomeStudyId, salomeStudyItem.GetID())
297         case = __cases__[case_key]
298         # Search if case is in Eficas !
299         callbackId = [salomeStudyId, salomeStudyItem]
300         case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
301         # If case is not in eficas Open It !
302         if case_open_in_eficas == False:
303           if case.get_filename() != "":
304             self.__dlgEficasWrapper.Openfile(case.get_filename())
305             callbackId = [salomeStudyId, salomeStudyItem]
306             self.__dlgEficasWrapper.setCallbackId(callbackId)
307           else:
308             # Since I am an empty case I destroy myself before reloading
309             adaoStudyEditor.removeItem(salomeStudyId, salomeStudyItem)
310             adaoGuiHelper.refreshObjectBrowser()
311             __cases__.pop(case_key)
312             callbackId = [salomeStudyId, salomeStudyItem]
313             self.__dlgEficasWrapper.removeCallbackId(callbackId)
314       except:
315         print "Oups - cannot reopen case !"
316         traceback.print_exc()
317
318     def _processEficasOpenEvent(self, eficasWrapper, eficasEvent):
319       global __cases__
320
321       # Ouverture du fichier
322       self.__dlgEficasWrapper.Openfile(self.__dlgEficasWrapper.getOpenFileName())
323
324       # Creation d'un nouveau cas
325       new_case = AdaoCase()
326       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
327       salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, new_case)
328       case_key = (salomeStudyId, salomeStudyItem.GetID())
329       __cases__[case_key] = new_case
330
331       # Connexion du nouveau cas
332       callbackId = [salomeStudyId, salomeStudyItem]
333       self.__dlgEficasWrapper.setCallbackId(callbackId)
334
335       # On sauvegarde le cas
336       self._processEficasSaveEvent(self.__dlgEficasWrapper, None, callbackId)
337
338     def _processEficasSaveEvent(self, eficasWrapper, eficasEvent, callbackId=None):
339         global __cases__
340         if callbackId is None:
341           callbackId = eficasEvent.callbackId
342           if callbackId is None:
343             raise DevelException("the callback data should not be None. Can't guess what are the study and case")
344           [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
345           if ( targetSalomeStudyId is None ) or ( targetSalomeStudyItem is None ):
346             raise DevelException("the parameters targetSalomeStudyId and targetSalomeStudyItem should not be None")
347         else:
348           [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
349
350         # Get Editor All infos we need !
351         case_name = eficasWrapper.getCaseName()
352         file_case_name = eficasWrapper.getFileCaseName()
353         if case_name != "" :
354           # Get case
355           old_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
356           case =__cases__[old_case_key]
357
358           # Set new informations
359           case.set_name(case_name)
360           if str(case_name).startswith("Untitled"):
361             pass
362           else:
363             case.set_filename(file_case_name)
364           adaoStudyEditor.updateItem(targetSalomeStudyId, targetSalomeStudyItem, case)
365
366           # Case key changed !
367           #new_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
368           # A ne pas inverser !!!
369           #__cases__.pop(old_case_key)
370           #__cases__[new_case_key] = case
371
372           adaoGuiHelper.refreshObjectBrowser()
373
374     def _processEficasDestroyEvent(self, eficasWrapper, eficasEvent):
375         global __cases__
376         callbackId = eficasEvent.callbackId
377         if callbackId is None:
378             raise DevelException("the callback data should not be None. Can't guess what are the study and case")
379         [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
380         if ( targetSalomeStudyId is None ) or ( targetSalomeStudyItem is None ):
381             raise DevelException("the parameters targetSalomeStudyId and targetSalomeStudyItem should not be None")
382
383         case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
384         __cases__.pop(case_key)
385         adaoStudyEditor.removeItem(targetSalomeStudyId, targetSalomeStudyItem)
386         adaoGuiHelper.refreshObjectBrowser()
387
388     def _processEficasUnknownEvent(self, eficasWrapper, eficasEvent):
389       print "Unknown Eficas Event"