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