1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010 EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 This file centralizes the definitions and implementations of ui components used
23 in the GUI part of the module.
26 __author__ = "aribes/gboulant"
29 from PyQt4.QtCore import QObject
30 from PyQt4 import QtGui,QtCore
32 sgPyQt = SalomePyQt.SalomePyQt()
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
40 import adaoStudyEditor
46 # ==============================================================================
47 # Classes to manage the building of UI components
48 # ==============================================================================
50 UI_ELT_IDS = Enumerate([
55 'SAVE_AS_ADAOCASE_ID',
58 'EDIT_ADAOCASE_POP_ID',
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",
69 UI_ELT_IDS.EDIT_ADAOCASE_POP_ID:"editAdaoCase",
70 UI_ELT_IDS.YACS_EXPORT_POP_ID:"exportCaseToYACS",
73 class AdaoGuiUiComponentBuilder:
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).
80 self.initUiComponents()
82 def initUiComponents(self):
86 # create top-level menu
87 mid = sgPyQt.createMenu( "ADAO", -1, UI_ELT_IDS.ADAO_MENU_ID, sgPyQt.defaultMenuGroup() )
89 tid = sgPyQt.createTool( "ADAO" )
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)
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", "" )
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", "" )
113 def createPopupMenuOnItem(self,popup,salomeSudyId, item):
114 if adaoStudyEditor.isValidAdaoCaseItem(salomeSudyId, item):
115 popup.addAction( sgPyQt.action( UI_ELT_IDS.CLOSE_ADAOCASE_ID ) )
117 popup.addAction( sgPyQt.action( UI_ELT_IDS.EDIT_ADAOCASE_POP_ID ) )
118 popup.addAction( sgPyQt.action( UI_ELT_IDS.YACS_EXPORT_POP_ID ) )
122 class AdaoGuiActionImpl(EficasObserver):
124 This class implements the ui actions concerning the management of oma study
130 # This dialog is created once so that it can be recycled for each call
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
138 # ==========================================================================
139 # Processing of ui actions
141 def processAction(self,actionId):
143 Main switch function for ui actions processing
145 if ACTIONS_MAP.has_key(actionId):
147 functionName = ACTIONS_MAP[actionId]
148 getattr(self,functionName)()
150 traceback.print_exc()
152 msg = "The requested action is not implemented: " + str(actionId)
155 def showEficas(self):
156 if self.__Eficas_viewId == -1:
157 self.__dlgEficasWrapper.init_gui()
161 area = QtGui.QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
162 area.setWidget( self.__dlgEficasWrapper)
163 area.setWidgetResizable(1)
166 self.__Eficas_viewId = sgPyQt.createView(wmType, area)
167 sgPyQt.setViewClosable(self.__Eficas_viewId, False)
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()
175 area = QtGui.QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
176 area.setWidget( self.__dlgEficasWrapper)
177 area.setWidgetResizable(1)
180 self.__Eficas_viewId = sgPyQt.createView(wmType, area)
181 sgPyQt.setViewClosable(self.__Eficas_viewId, False)
182 self.__dlgEficasWrapper.setEnabled(True)
187 def deactivate(self):
189 if self.__Eficas_viewId != -1:
190 self.__dlgEficasWrapper.setEnabled(False)
192 # Actions from SALOME GUI
194 def newAdaoCase(self):
196 adaoLogger.debug("newAdaoCase")
198 self.__dlgEficasWrapper.fileNew()
200 def openAdaoCase(self):
202 adaoLogger.debug("openAdaoCase")
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
218 # Open file in Eficas
219 self.__dlgEficasWrapper.Openfile(new_case.get_filename())
220 callbackId = [salomeStudyId, salomeStudyItem]
221 self.__dlgEficasWrapper.setCallbackId(callbackId)
223 adaoGuiHelper.refreshObjectBrowser()
225 def editAdaoCase(self):
227 adaoLogger.debug("editAdaoCase")
231 salomeStudyId = adaoGuiHelper.getActiveStudyId()
232 salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
233 case_key = (salomeStudyId, salomeStudyItem.GetID())
235 # ShowEficas, If case is an empty case - case is destroyed by reopen
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)
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)
250 # Case has been destroyed - create a new one
251 self.__dlgEficasWrapper.fileNew()
253 def closeAdaoCase(self):
255 adaoLogger.debug("closeAdaoCase")
258 # First step: get selected case
259 salomeStudyId = adaoGuiHelper.getActiveStudyId()
260 salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
262 # Check if there is a selected case
263 if salomeStudyItem is None:
264 print "[Close case] Please select a case"
267 callbackId = [salomeStudyId, salomeStudyItem]
268 case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
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()
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()
283 def saveAdaoCase(self):
285 adaoLogger.debug("saveAdaoCase")
288 def saveasAdaoCase(self):
290 adaoLogger.debug("saveasAdaoCase")
293 def exportCaseToYACS(self):
295 adaoLogger.debug("exportCaseToYACS")
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]
304 # Generates YACS schema and export it
305 msg = case.exportCaseToYACS()
307 # If msg is not empty -> error found
309 adaoGuiHelper.gui_warning(self.__parent, msg)
311 # ==========================================================================
312 # Processing notifications from adaoEficasWrapper
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"
322 def processEficasEvent(self, eficasWrapper, eficasEvent):
324 Implementation of the interface EficasObserver. The implementation is a
325 switch on the possible types of events defined in EficasEvent.EVENT_TYPES.
328 functionName = self.__processOptions.get(eficasEvent.eventType, lambda : "_processEficasUnknownEvent")
329 return getattr(self,functionName)(eficasWrapper, eficasEvent)
331 def _processEficasCloseEvent(self, eficasWrapper, eficasEvent):
334 def _processEficasNewEvent(self, eficasWrapper, eficasEvent):
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)
348 # We need to select the case
349 adaoGuiHelper.selectItem(salomeStudyItem.GetID())
352 def _processEficasOpenEvent(self, eficasWrapper, eficasEvent):
355 # Ouverture du fichier
356 self.__dlgEficasWrapper.Openfile(self.__dlgEficasWrapper.getOpenFileName())
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
365 # Connexion du nouveau cas
366 callbackId = [salomeStudyId, salomeStudyItem]
367 self.__dlgEficasWrapper.setCallbackId(callbackId)
369 # On sauvegarde le cas
370 self._processEficasSaveEvent(self.__dlgEficasWrapper, None, callbackId)
372 def _processEficasSaveEvent(self, eficasWrapper, eficasEvent, callbackId=None):
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")
382 [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
384 # Get Editor All infos we need !
385 case_name = eficasWrapper.getCaseName()
386 file_case_name = eficasWrapper.getFileCaseName()
389 old_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
390 case =__cases__[old_case_key]
392 # Set new informations
393 case.set_name(case_name)
394 if str(case_name).startswith("Untitled"):
397 case.set_filename(file_case_name)
398 adaoStudyEditor.updateItem(targetSalomeStudyId, targetSalomeStudyItem, case)
401 #new_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
402 # A ne pas inverser !!!
403 #__cases__.pop(old_case_key)
404 #__cases__[new_case_key] = case
406 adaoGuiHelper.refreshObjectBrowser()
408 def _processEficasDestroyEvent(self, eficasWrapper, eficasEvent):
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")
417 case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
418 __cases__.pop(case_key)
419 adaoStudyEditor.removeItem(targetSalomeStudyId, targetSalomeStudyItem)
420 adaoGuiHelper.refreshObjectBrowser()
423 # Normalement on ne ferme plus le GUI donc on ne passe plus par là
424 def _processEficasReOpenEvent(self, eficasWrapper, eficasEvent):
426 adaoLogger.warning("_processEficasReOpenEvent")
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)
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)
451 print "Oups - cannot reopen case !"
452 traceback.print_exc()
454 def _processEficasUnknownEvent(self, eficasWrapper, eficasEvent):
455 print "Unknown Eficas Event"