]> SALOME platform Git repositories - modules/adao.git/blob - src/daSalome/daGUI/daGuiImpl/adaoGuiManager.py
Salome HOME
- Patch pour les changements sur EficasV1
[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         print "First showEficas"
140         self.__dlgEficasWrapper.init_gui()
141
142         # Scroll Widget
143         area = QtGui.QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
144         area.setWidget( self.__dlgEficasWrapper)
145         area.setWidgetResizable(1)
146         self.__Eficas_viewId = SalomePyQt.SalomePyQt().createViewWithWidget(area)
147       else:
148         print "myViewId =",  self.__Eficas_viewId
149         print "activeView =", SalomePyQt.SalomePyQt().getActiveView()
150         if SalomePyQt.SalomePyQt().getActiveView() != self.__Eficas_viewId :
151           result_activate = SalomePyQt.SalomePyQt().activateView(self.__Eficas_viewId)
152           if result_activate == False:
153             print "View was close - create a new eficas widget"
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             self.__Eficas_viewId = SalomePyQt.SalomePyQt().createViewWithWidget(area)
161
162     def activate(self):
163       self.showEficas()
164
165     def newAdaoCase(self):
166       self.showEficas()
167       self.__dlgEficasWrapper.fileNew()
168
169     def openAdaoCase(self):
170       self.showEficas()
171       global __cases__
172       fichier = QtGui.QFileDialog.getOpenFileName(SalomePyQt.SalomePyQt().getDesktop(),
173                                                   self.__dlgEficasWrapper.trUtf8('Ouvrir Fichier'),
174                                                   self.__dlgEficasWrapper.CONFIGURATION.savedir,
175                                                   self.__dlgEficasWrapper.trUtf8('JDC Files (*.comm);;''All Files (*)'))
176       if fichier.isNull(): return
177       new_case = AdaoCase()
178       new_case.set_filename(str(fichier))
179       new_case.set_name(str(fichier.split('/')[-1]))
180       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
181       salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, new_case)
182       case_key = (salomeStudyId, salomeStudyItem.GetID())
183       __cases__[case_key] = new_case
184
185       # Open file in Eficas
186       self.__dlgEficasWrapper.Openfile(new_case.get_filename())
187       callbackId = [salomeStudyId, salomeStudyItem]
188       self.__dlgEficasWrapper.setCallbackId(callbackId)
189       self.showEficas()
190       adaoGuiHelper.refreshObjectBrowser()
191
192     def editAdaoCase(self):
193       # First we show eficas - all cases are reloaded
194       global __cases__
195
196       # Take study item
197       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
198       salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
199       case_key = (salomeStudyId, salomeStudyItem.GetID())
200
201       # ShowEficas, If case is an empty case - case is destroyed by reopen
202       self.showEficas()
203       try:
204         case = __cases__[case_key]
205         # Search if case is in Eficas !
206         callbackId = [salomeStudyId, salomeStudyItem]
207         case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
208
209         # If case is not in eficas Open It !
210         if case_open_in_eficas == False:
211           if case.get_filename() != "":
212             self.__dlgEficasWrapper.Openfile(case.get_filename())
213             callbackId = [salomeStudyId, salomeStudyItem]
214             self.__dlgEficasWrapper.setCallbackId(callbackId)
215       except:
216         print "Oups - cannot edit case !"
217         traceback.print_exc()
218
219     def removeAdaoCase(self):
220       global __cases__
221
222       # First step: selectCase
223       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
224       salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
225       callbackId = [salomeStudyId, salomeStudyItem]
226       case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
227       # If case is in eficas close it !
228       if case_open_in_eficas:
229         self.__dlgEficasWrapper.fileClose()
230
231       # Test if case exists
232       case_key = (salomeStudyId, salomeStudyItem.GetID())
233       if __cases__.has_key(case_key):
234         __cases__.pop(case_key)
235         adaoStudyEditor.removeItem(salomeStudyId, salomeStudyItem)
236         adaoGuiHelper.refreshObjectBrowser()
237
238     def exportCaseToYACS(self):
239       global __cases__
240       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
241       salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
242       case_key = (salomeStudyId, salomeStudyItem.GetID())
243       case = __cases__[case_key]
244
245       msg = case.exportCaseToYACS()
246       
247       if msg != "":
248         adaoGuiHelper.gui_warning(self.__parent, msg)
249
250     # ==========================================================================
251     # Processing notifications from eficas
252     #
253     __processOptions={
254         EficasEvent.EVENT_TYPES.CLOSE : "_processEficasCloseEvent",
255         EficasEvent.EVENT_TYPES.SAVE  : "_processEficasSaveEvent",
256         EficasEvent.EVENT_TYPES.NEW  : "_processEficasNewEvent",
257         EficasEvent.EVENT_TYPES.DESTROY  : "_processEficasDestroyEvent",
258         EficasEvent.EVENT_TYPES.OPEN  : "_processEficasOpenEvent",
259         EficasEvent.EVENT_TYPES.REOPEN  : "_processEficasReOpenEvent"
260         }
261     def processEficasEvent(self, eficasWrapper, eficasEvent):
262         """
263         Implementation of the interface EficasObserver. The implementation is a
264         switch on the possible types of events defined in EficasEvent.EVENT_TYPES.
265         @overload
266         """
267         functionName = self.__processOptions.get(eficasEvent.eventType, lambda : "_processEficasUnknownEvent")
268         return getattr(self,functionName)(eficasWrapper, eficasEvent)
269
270     def _processEficasCloseEvent(self, eficasWrapper, eficasEvent):
271         pass
272
273     def _processEficasNewEvent(self, eficasWrapper, eficasEvent):
274       global __cases__
275       new_case = AdaoCase()
276       case_name = eficasWrapper.getCaseName()
277       new_case.set_name(case_name)
278       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
279       salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, new_case)
280       case_key = (salomeStudyId, salomeStudyItem.GetID())
281       __cases__[case_key] = new_case
282       adaoGuiHelper.refreshObjectBrowser()
283       callbackId = [salomeStudyId, salomeStudyItem]
284       self.__dlgEficasWrapper.setCallbackId(callbackId)
285
286     def _processEficasReOpenEvent(self, eficasWrapper, eficasEvent):
287       global __cases__
288       try:
289         callbackId = eficasEvent.callbackId
290         [salomeStudyId, salomeStudyItem] = callbackId
291         case_key = (salomeStudyId, salomeStudyItem.GetID())
292         case = __cases__[case_key]
293         # Search if case is in Eficas !
294         callbackId = [salomeStudyId, salomeStudyItem]
295         print "selectCase"
296         case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
297         # If case is not in eficas Open It !
298         if case_open_in_eficas == False:
299           print "reopen selectCase"
300           if case.get_filename() != "":
301             self.__dlgEficasWrapper.Openfile(case.get_filename())
302             callbackId = [salomeStudyId, salomeStudyItem]
303             self.__dlgEficasWrapper.setCallbackId(callbackId)
304           else:
305             # Since I am an empty case I destroy myself before reloading
306             adaoStudyEditor.removeItem(salomeStudyId, salomeStudyItem)
307             adaoGuiHelper.refreshObjectBrowser()
308             __cases__.pop(case_key)
309             self.__dlgEficasWrapper.fileNew()
310       except:
311         print "Oups - cannot reopen case !"
312         traceback.print_exc()
313
314     def _processEficasOpenEvent(self, eficasWrapper, eficasEvent):
315       global __cases__
316
317       # Ouverture du fichier
318       self.__dlgEficasWrapper.Openfile(self.__dlgEficasWrapper.getOpenFileName())
319
320       # Creation d'un nouveau cas
321       new_case = AdaoCase()
322       salomeStudyId   = adaoGuiHelper.getActiveStudyId()
323       salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, new_case)
324       case_key = (salomeStudyId, salomeStudyItem.GetID())
325       __cases__[case_key] = new_case
326
327       # Connexion du nouveau cas
328       callbackId = [salomeStudyId, salomeStudyItem]
329       self.__dlgEficasWrapper.setCallbackId(callbackId)
330
331       # On sauvegarde le cas
332       self._processEficasSaveEvent(self.__dlgEficasWrapper, None, callbackId)
333
334     def _processEficasSaveEvent(self, eficasWrapper, eficasEvent, callbackId=None):
335         global __cases__
336         if callbackId is None:
337           callbackId = eficasEvent.callbackId
338           if callbackId is None:
339             raise DevelException("the callback data should not be None. Can't guess what are the study and case")
340           [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
341           if ( targetSalomeStudyId is None ) or ( targetSalomeStudyItem is None ):
342             raise DevelException("the parameters targetSalomeStudyId and targetSalomeStudyItem should not be None")
343         else:
344           [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
345
346         # Get Editor All infos we need !
347         case_name = eficasWrapper.getCaseName()
348         file_case_name = eficasWrapper.getFileCaseName()
349         if case_name != "" :
350           # Get case
351           old_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
352           case =__cases__[old_case_key]
353
354           # Set new informations
355           case.set_name(case_name)
356           if str(case_name).startswith("Untitled"):
357             pass
358           else:
359             case.set_filename(file_case_name)
360           adaoStudyEditor.updateItem(targetSalomeStudyId, targetSalomeStudyItem, case)
361
362           # Case key changed !
363           #new_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
364           # A ne pas inverser !!!
365           #__cases__.pop(old_case_key)
366           #__cases__[new_case_key] = case
367
368           adaoGuiHelper.refreshObjectBrowser()
369
370     def _processEficasDestroyEvent(self, eficasWrapper, eficasEvent):
371         global __cases__
372         callbackId = eficasEvent.callbackId
373         if callbackId is None:
374             raise DevelException("the callback data should not be None. Can't guess what are the study and case")
375         [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
376         if ( targetSalomeStudyId is None ) or ( targetSalomeStudyItem is None ):
377             raise DevelException("the parameters targetSalomeStudyId and targetSalomeStudyItem should not be None")
378
379         case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
380         __cases__.pop(case_key)
381         adaoStudyEditor.removeItem(targetSalomeStudyId, targetSalomeStudyItem)
382         adaoGuiHelper.refreshObjectBrowser()
383
384     def _processEficasUnknownEvent(self, eficasWrapper, eficasEvent):
385       print "Unknown Eficas Event"