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
45 # ==============================================================================
46 # Classes to manage the building of UI components
47 # ==============================================================================
49 UI_ELT_IDS = Enumerate([
53 'EDIT_ADAOCASE_POP_ID',
54 'REMOVE_ADAOCASE_POP_ID',
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",
66 class AdaoGuiUiComponentBuilder:
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).
73 self.initUiComponents()
75 def initUiComponents(self):
79 # create top-level menu
80 mid = sgPyQt.createMenu( "ADAO", -1, UI_ELT_IDS.ADAO_MENU_ID, sgPyQt.defaultMenuGroup() )
82 tid = sgPyQt.createTool( "ADAO" )
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)
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", "" )
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 ) )
104 class AdaoGuiActionImpl(EficasObserver):
106 This class implements the ui actions concerning the management of oma study
112 # This dialog is created once so that it can be recycled for each call
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
120 # ==========================================================================
121 # Processing of ui actions
123 def processAction(self,actionId):
125 Main switch function for ui actions processing
127 if ACTIONS_MAP.has_key(actionId):
129 functionName = ACTIONS_MAP[actionId]
130 getattr(self,functionName)()
132 traceback.print_exc()
134 msg = "The requested action is not implemented: " + str(actionId)
137 def showEficas(self):
138 if self.__Eficas_viewId == -1:
139 self.__dlgEficasWrapper.init_gui()
143 area = QtGui.QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
144 area.setWidget( self.__dlgEficasWrapper)
145 area.setWidgetResizable(1)
148 self.__Eficas_viewId = sgPyQt.createView(wmType, area)
149 sgPyQt.setViewClosable(self.__Eficas_viewId, False)
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()
157 area = QtGui.QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
158 area.setWidget( self.__dlgEficasWrapper)
159 area.setWidgetResizable(1)
162 self.__Eficas_viewId = sgPyQt.createView(wmType, area)
163 sgPyQt.setViewClosable(self.__Eficas_viewId, False)
168 # Actions from SALOME GUI
170 def newAdaoCase(self):
172 self.__dlgEficasWrapper.fileNew()
174 def openAdaoCase(self):
177 fichier = QtGui.QFileDialog.getOpenFileName(SalomePyQt.SalomePyQt().getDesktop(),
178 self.__dlgEficasWrapper.trUtf8('Ouvrir Fichier'),
179 self.__dlgEficasWrapper.CONFIGURATION.savedir,
180 self.__dlgEficasWrapper.trUtf8('JDC Files (*.comm);;''All Files (*)'))
181 if fichier.isNull(): return
182 new_case = AdaoCase()
183 new_case.set_filename(str(fichier))
184 new_case.set_name(str(fichier.split('/')[-1]))
185 salomeStudyId = adaoGuiHelper.getActiveStudyId()
186 salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, new_case)
187 case_key = (salomeStudyId, salomeStudyItem.GetID())
188 __cases__[case_key] = new_case
190 # Open file in Eficas
191 self.__dlgEficasWrapper.Openfile(new_case.get_filename())
192 callbackId = [salomeStudyId, salomeStudyItem]
193 self.__dlgEficasWrapper.setCallbackId(callbackId)
195 adaoGuiHelper.refreshObjectBrowser()
197 def editAdaoCase(self):
198 # First we show eficas - all cases are reloaded
202 salomeStudyId = adaoGuiHelper.getActiveStudyId()
203 salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
204 case_key = (salomeStudyId, salomeStudyItem.GetID())
206 # ShowEficas, If case is an empty case - case is destroyed by reopen
209 case = __cases__[case_key]
210 # Search if case is in Eficas !
211 callbackId = [salomeStudyId, salomeStudyItem]
212 case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
214 # If case is not in eficas Open It !
215 if case_open_in_eficas == False:
216 if case.get_filename() != "":
217 self.__dlgEficasWrapper.Openfile(case.get_filename())
218 callbackId = [salomeStudyId, salomeStudyItem]
219 self.__dlgEficasWrapper.setCallbackId(callbackId)
221 # Case has been destroyed - create a new one
222 self.__dlgEficasWrapper.fileNew()
224 def removeAdaoCase(self):
227 # First step: selectCase
228 salomeStudyId = adaoGuiHelper.getActiveStudyId()
229 salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
230 callbackId = [salomeStudyId, salomeStudyItem]
231 case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
233 # If case is in eficas close it !
234 if case_open_in_eficas:
235 # fileClose: remove the CallbackId
236 # fileClose: sends a destroy event
237 self.__dlgEficasWrapper.fileClose()
239 # Test if case exists
240 case_key = (salomeStudyId, salomeStudyItem.GetID())
241 if __cases__.has_key(case_key):
242 __cases__.pop(case_key)
243 adaoStudyEditor.removeItem(salomeStudyId, salomeStudyItem)
244 adaoGuiHelper.refreshObjectBrowser()
246 def exportCaseToYACS(self):
249 # Get case from study
250 salomeStudyId = adaoGuiHelper.getActiveStudyId()
251 salomeStudyItem = adaoGuiHelper.getSelectedItem(salomeStudyId)
252 case_key = (salomeStudyId, salomeStudyItem.GetID())
253 case = __cases__[case_key]
255 # Generates YACS schema and export it
256 msg = case.exportCaseToYACS()
258 # If msg is not empty -> error found
260 adaoGuiHelper.gui_warning(self.__parent, msg)
262 # ==========================================================================
263 # Processing notifications from adaoEficasWrapper
266 EficasEvent.EVENT_TYPES.CLOSE : "_processEficasCloseEvent",
267 EficasEvent.EVENT_TYPES.SAVE : "_processEficasSaveEvent",
268 EficasEvent.EVENT_TYPES.NEW : "_processEficasNewEvent",
269 EficasEvent.EVENT_TYPES.DESTROY : "_processEficasDestroyEvent",
270 EficasEvent.EVENT_TYPES.OPEN : "_processEficasOpenEvent",
271 EficasEvent.EVENT_TYPES.REOPEN : "_processEficasReOpenEvent"
273 def processEficasEvent(self, eficasWrapper, eficasEvent):
275 Implementation of the interface EficasObserver. The implementation is a
276 switch on the possible types of events defined in EficasEvent.EVENT_TYPES.
279 functionName = self.__processOptions.get(eficasEvent.eventType, lambda : "_processEficasUnknownEvent")
280 return getattr(self,functionName)(eficasWrapper, eficasEvent)
282 def _processEficasCloseEvent(self, eficasWrapper, eficasEvent):
285 def _processEficasNewEvent(self, eficasWrapper, eficasEvent):
287 new_case = AdaoCase()
288 case_name = eficasWrapper.getCaseName()
289 new_case.set_name(case_name)
290 salomeStudyId = adaoGuiHelper.getActiveStudyId()
291 salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, new_case)
292 case_key = (salomeStudyId, salomeStudyItem.GetID())
293 __cases__[case_key] = new_case
294 adaoGuiHelper.refreshObjectBrowser()
295 callbackId = [salomeStudyId, salomeStudyItem]
296 self.__dlgEficasWrapper.setCallbackId(callbackId)
298 def _processEficasReOpenEvent(self, eficasWrapper, eficasEvent):
301 callbackId = eficasEvent.callbackId
302 [salomeStudyId, salomeStudyItem] = callbackId
303 case_key = (salomeStudyId, salomeStudyItem.GetID())
304 case = __cases__[case_key]
305 # Search if case is in Eficas !
306 callbackId = [salomeStudyId, salomeStudyItem]
307 case_open_in_eficas = self.__dlgEficasWrapper.selectCase(callbackId)
308 # If case is not in eficas Open It !
309 if case_open_in_eficas == False:
310 if case.get_filename() != "":
311 self.__dlgEficasWrapper.Openfile(case.get_filename())
312 callbackId = [salomeStudyId, salomeStudyItem]
313 self.__dlgEficasWrapper.setCallbackId(callbackId)
315 # Since I am an empty case I destroy myself before reloading
316 adaoStudyEditor.removeItem(salomeStudyId, salomeStudyItem)
317 adaoGuiHelper.refreshObjectBrowser()
318 __cases__.pop(case_key)
319 callbackId = [salomeStudyId, salomeStudyItem]
320 self.__dlgEficasWrapper.removeCallbackId(callbackId)
322 print "Oups - cannot reopen case !"
323 traceback.print_exc()
325 def _processEficasOpenEvent(self, eficasWrapper, eficasEvent):
328 # Ouverture du fichier
329 self.__dlgEficasWrapper.Openfile(self.__dlgEficasWrapper.getOpenFileName())
331 # Creation d'un nouveau cas
332 new_case = AdaoCase()
333 salomeStudyId = adaoGuiHelper.getActiveStudyId()
334 salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, new_case)
335 case_key = (salomeStudyId, salomeStudyItem.GetID())
336 __cases__[case_key] = new_case
338 # Connexion du nouveau cas
339 callbackId = [salomeStudyId, salomeStudyItem]
340 self.__dlgEficasWrapper.setCallbackId(callbackId)
342 # On sauvegarde le cas
343 self._processEficasSaveEvent(self.__dlgEficasWrapper, None, callbackId)
345 def _processEficasSaveEvent(self, eficasWrapper, eficasEvent, callbackId=None):
347 if callbackId is None:
348 callbackId = eficasEvent.callbackId
349 if callbackId is None:
350 raise DevelException("the callback data should not be None. Can't guess what are the study and case")
351 [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
352 if ( targetSalomeStudyId is None ) or ( targetSalomeStudyItem is None ):
353 raise DevelException("the parameters targetSalomeStudyId and targetSalomeStudyItem should not be None")
355 [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
357 # Get Editor All infos we need !
358 case_name = eficasWrapper.getCaseName()
359 file_case_name = eficasWrapper.getFileCaseName()
362 old_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
363 case =__cases__[old_case_key]
365 # Set new informations
366 case.set_name(case_name)
367 if str(case_name).startswith("Untitled"):
370 case.set_filename(file_case_name)
371 adaoStudyEditor.updateItem(targetSalomeStudyId, targetSalomeStudyItem, case)
374 #new_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
375 # A ne pas inverser !!!
376 #__cases__.pop(old_case_key)
377 #__cases__[new_case_key] = case
379 adaoGuiHelper.refreshObjectBrowser()
381 def _processEficasDestroyEvent(self, eficasWrapper, eficasEvent):
383 callbackId = eficasEvent.callbackId
384 if callbackId is None:
385 raise DevelException("the callback data should not be None. Can't guess what are the study and case")
386 [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
387 if ( targetSalomeStudyId is None ) or ( targetSalomeStudyItem is None ):
388 raise DevelException("the parameters targetSalomeStudyId and targetSalomeStudyItem should not be None")
390 case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetID())
391 __cases__.pop(case_key)
392 adaoStudyEditor.removeItem(targetSalomeStudyId, targetSalomeStudyItem)
393 adaoGuiHelper.refreshObjectBrowser()
395 def _processEficasUnknownEvent(self, eficasWrapper, eficasEvent):
396 print "Unknown Eficas Event"