1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2008-2015 EDF R&D
4 # This file is part of SALOME ADAO module
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 This file centralizes the definitions and implementations of ui components used
25 in the GUI part of the module.
28 __author__ = "aribes/gboulant"
31 from PyQt4.QtCore import QObject
32 from PyQt4.QtCore import * # Import from PyQT
33 from PyQt4 import QtGui,QtCore
35 sgPyQt = SalomePyQt.SalomePyQt()
37 from daUtils.enumerate import Enumerate
38 from daGuiImpl.adaoCase import AdaoCase
39 from daEficasWrapper.adaoEficasWrapper import AdaoEficasWrapper
41 from daUtils.adaoEficasEvent import *
43 import adaoStudyEditor
44 from daUtils import adaoLogger
49 # ==============================================================================
50 # Classes to manage the building of UI components
51 # ==============================================================================
53 UI_ELT_IDS = Enumerate([
58 'SAVE_AS_ADAOCASE_ID',
64 UI_ELT_IDS.NEW_ADAOCASE_ID:"newAdaoCase",
65 UI_ELT_IDS.OPEN_ADAOCASE_ID:"openAdaoCase",
66 UI_ELT_IDS.SAVE_ADAOCASE_ID:"saveAdaoCase",
67 UI_ELT_IDS.SAVE_AS_ADAOCASE_ID:"saveasAdaoCase",
68 UI_ELT_IDS.CLOSE_ADAOCASE_ID:"closeAdaoCase",
69 UI_ELT_IDS.YACS_EXPORT_ID:"exportCaseToYACS",
73 class AdaoCaseManager(EficasObserver):
75 Cette classe gére les cas ADAO et coordonne les GUI de SALOME (l'étude)
76 et le GUI de l'objet Eficas (héritage du module Eficas)
81 # Création d'un dictionnaire de cas
82 # Key == ref objet editor eficas (on est sur qu'elle est unique, cas duplication)
83 # Value == objet AdaoCase()
86 # Création des deux managers
87 self.salome_manager = AdaoGuiUiComponentBuilder()
88 self.eficas_manager = AdaoEficasWrapper(parent=SalomePyQt.SalomePyQt().getDesktop())
90 # On s'enregistre comme observer pour les évènements venant d'Eficas
91 # Les évènements du salome_manager viennent par le biais de la méthode
93 self.eficas_manager.addObserver(self)
95 # Création du GUI Eficas
96 self.eficas_manager.init_gui()
98 # Création du viewer QT
99 # Scroll Widget (pour les petites résolutions)
100 area = QtGui.QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
101 area.setWidget(self.eficas_manager)
102 area.setWidgetResizable(1)
104 self.eficas_viewId = sgPyQt.createView(wmType, area)
106 # On interdit que la vue soit fermée
107 # Cela simplifier grandement le code
108 sgPyQt.setViewClosable(self.eficas_viewId, False)
110 # On s'abonne au gestionnaire de selection
111 self.selection_manager = sgPyQt.getSelection()
112 QtCore.QObject.connect(self.selection_manager, QtCore.SIGNAL('currentSelectionChanged()'), self.currentSelectionChanged)
116 # Gestion de l'activation/désactivation du module
121 self.eficas_manager.setEnabled(True)
122 sgPyQt.activateView(self.eficas_viewId)
123 self.harmonizeSelectionFromEficas()
125 def deactivate(self):
126 self.eficas_manager.setEnabled(False)
130 # Gestion de la sélection entre le GUI d'Eficas
131 # et l'arbre d'étude de SALOME
135 # Depuis l'étude SALOME
136 def currentSelectionChanged(self):
138 Cette méthode permet de changer le tab vu dans eficas
139 selon la sélection de l'utilisateur dans l'étude SALOME
141 adaoLogger.debug("currentSelectionChanged")
142 salomeStudyItem = adaoGuiHelper.getSelectedItem()
143 if salomeStudyItem is not None:
144 for case_editor, adao_case in self.cases.iteritems():
145 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
146 self.eficas_manager.selectCase(adao_case.eficas_editor)
150 def _processEficasTabChanged(self, eficasWrapper, eficasEvent):
152 Gestion de la synchonisation entre le tab courant d'Eficas
153 et la selection dans l'étude SALOME
155 editor = eficasEvent.callbackId
156 for case_editor, adao_case in self.cases.iteritems():
157 if case_editor is editor:
158 adaoGuiHelper.selectItem(adao_case.salome_study_item.GetID())
161 # On remet la sélection dans SALOME grâce au tab dans Eficas
162 def harmonizeSelectionFromEficas(self):
164 Cette méthode permet d'harmoniser la sélection dans l'étude
165 grâce au tab courant d'Eficas
168 # 1: Get current tab index in Eficas
169 editor = self.eficas_manager.getCurrentEditor()
170 # 2: sync with SALOME GUI is a tab is opened
172 for case_editor, adao_case in self.cases.iteritems():
173 if case_editor is editor:
174 adaoGuiHelper.selectItem(adao_case.salome_study_item.GetID())
179 # Gestion de la création d'un nouveau cas
180 # 1: la fonction newAdaoCase est appelée par le GUI SALOME
181 # 2: la fonction _processEficasNewEvent est appelée par le manager EFICAS
185 def newAdaoCase(self):
186 adaoLogger.debug("Création d'un nouveau cas adao")
187 self.eficas_manager.adaofileNew(AdaoCase())
189 def _processEficasNewEvent(self, eficasWrapper, eficasEvent):
190 adao_case = eficasEvent.callbackId
192 salomeStudyId = adaoGuiHelper.getActiveStudyId()
193 salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, adao_case)
194 # Affichage correct dans l'étude
195 adaoGuiHelper.refreshObjectBrowser()
196 adaoGuiHelper.selectItem(salomeStudyItem.GetID())
197 # Finalisation des données du cas
198 adao_case.salome_study_id = salomeStudyId
199 adao_case.salome_study_item = salomeStudyItem
201 self.cases[adao_case.eficas_editor] = adao_case
205 # Gestion de l'ouverture d'un cas
206 # 1: la fonction openAdaoCase est appelée par le GUI SALOME
207 # 2: la fonction _processEficasOpenEvent est appelée par le manager EFICAS
211 # Rq: l'ouverture d'un cas adao est un cas particulier de la création d'un cas adao
213 def openAdaoCase(self):
214 adaoLogger.debug("Ouverture d'un cas adao")
215 self.eficas_manager.adaoFileOpen(AdaoCase())
217 def _processEficasOpenEvent(self, eficasWrapper, eficasEvent):
218 self._processEficasNewEvent(eficasWrapper, eficasEvent)
222 # Gestion de la sauvegarde d'un cas
223 # 1: la fonction saveAdaoCase est appelée par le GUI SALOME
224 # 1 bis: la fonction saveasAdaoCase est appelée par le GUI SALOME
225 # 2: la fonction _processEficasSaveEvent est appelée par le manager EFICAS
229 def saveAdaoCase(self):
230 adaoLogger.debug("Sauvegarde du cas s'il y a modification")
231 # A priori, l'utilisateur s'attend à sauvegarder le cas qui est ouvert
232 # dans le GUI d'Eficas
233 self.harmonizeSelectionFromEficas()
234 salomeStudyItem = adaoGuiHelper.getSelectedItem()
235 for case_name, adao_case in self.cases.iteritems():
236 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
237 self.eficas_manager.adaoFileSave(adao_case)
240 def saveasAdaoCase(self):
241 adaoLogger.debug("Sauvegarde du cas s'il y a modification (version save as)")
242 # A priori, l'utilisateur s'attend à sauvegarder le cas qui est ouvert
243 # dans le GUI d'Eficas
244 self.harmonizeSelectionFromEficas()
245 salomeStudyItem = adaoGuiHelper.getSelectedItem()
246 for case_name, adao_case in self.cases.iteritems():
247 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
248 self.eficas_manager.adaoFileSaveAs(adao_case)
251 def _processEficasSaveEvent(self, eficasWrapper, eficasEvent):
252 adao_case = eficasEvent.callbackId
253 # On met à jour l'étude
254 adaoStudyEditor.updateItem(adao_case.salome_study_id, adao_case.salome_study_item, adao_case)
255 # Affichage correct dans l'étude
256 adaoGuiHelper.refreshObjectBrowser()
257 adaoGuiHelper.selectItem(adao_case.salome_study_item.GetID())
259 self.cases[adao_case.name] = adao_case
263 # Gestion de la fermeture d'un cas
264 # 1: la fonction closeAdaoCase est appelée par le GUI SALOME
265 # 2: la fonction _processEficasCloseEvent est appelée par le manager EFICAS
269 def closeAdaoCase(self):
270 adaoLogger.debug("Fermeture d'un cas")
271 # A priori, l'utilisateur s'attend à sauvegarder le cas qui est ouvert
272 # dans le GUI d'Eficas
273 self.harmonizeSelectionFromEficas()
274 salomeStudyItem = adaoGuiHelper.getSelectedItem()
275 for case_name, adao_case in self.cases.iteritems():
276 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
277 self.eficas_manager.adaoFileClose(adao_case)
280 def _processEficasCloseEvent(self, eficasWrapper, eficasEvent):
281 from Extensions.param2 import originalMath
282 originalMath.toOriginal()
283 adaoLogger.debug("Destruction d'un cas")
284 editor = eficasEvent.callbackId
285 # Recuperation du cas
286 adao_case = self.cases[editor]
287 # Suppression de l'objet dans l'étude
288 adaoStudyEditor.removeItem(adao_case.salome_study_id, adao_case.salome_study_item)
290 self.cases.pop(editor)
291 # Refresh GUI -> appelle currentSelectionChanged()
292 adaoGuiHelper.refreshObjectBrowser()
296 # Gestion de la connexion avec YACS
297 # 1: la fonction exportCasToYACS exporte l'étude vers YACS
300 def exportCaseToYACS(self):
301 adaoLogger.debug("Export du cas vers YACS")
303 # A priori, l'utilisateur s'attend à exporter le cas qui est ouvert
304 # dans le GUI d'Eficas
305 self.harmonizeSelectionFromEficas()
306 salomeStudyItem = adaoGuiHelper.getSelectedItem()
307 for case_name, adao_case in self.cases.iteritems():
308 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
310 msg = adao_case.exportCaseToYACS()
311 # If msg is not empty -> error found
313 adaoGuiHelper.gui_warning(SalomePyQt.SalomePyQt().getDesktop(), msg)
315 adaoGuiHelper.gui_warning(SalomePyQt.SalomePyQt().getDesktop(), "Cannot export case, case is not valid")
320 # Méthodes secondaires permettant de rediriger les évènements
321 # de SALOME et d'Eficas vers les bonnes méthodes de la classe
325 # Gestion des évènements venant du manager Eficas
327 EficasEvent.EVENT_TYPES.CLOSE : "_processEficasCloseEvent",
328 EficasEvent.EVENT_TYPES.SAVE : "_processEficasSaveEvent",
329 EficasEvent.EVENT_TYPES.NEW : "_processEficasNewEvent",
330 EficasEvent.EVENT_TYPES.CLOSE : "_processEficasCloseEvent",
331 EficasEvent.EVENT_TYPES.OPEN : "_processEficasOpenEvent",
332 EficasEvent.EVENT_TYPES.TABCHANGED : "_processEficasTabChanged",
333 EficasEvent.EVENT_TYPES.REOPEN : "_processEficasReOpenEvent"
336 def processEficasEvent(self, eficasWrapper, eficasEvent):
338 Implementation of the interface EficasObserver. The implementation is a
339 switch on the possible types of events defined in EficasEvent.EVENT_TYPES.
342 functionName = self.__processOptions.get(eficasEvent.eventType, lambda : "_processEficasUnknownEvent")
343 return getattr(self,functionName)(eficasWrapper, eficasEvent)
345 def _processEficasUnknownEvent(self, eficasWrapper, eficasEvent):
346 adaoLogger.error("Unknown Eficas Event")
348 # Gestion des évènements venant du GUI de SALOME
349 def processGUIEvent(self, actionId):
351 Main switch function for ui actions processing
353 if ACTIONS_MAP.has_key(actionId):
355 functionName = ACTIONS_MAP[actionId]
356 getattr(self,functionName)()
358 traceback.print_exc()
360 adaoLogger.warning("The requested action is not implemented: " + str(actionId))
362 class AdaoGuiUiComponentBuilder:
364 The initialisation of this class creates the graphic components involved
365 in the GUI (menu, menu item, toolbar). A ui component builder should be
366 created for each opened study and associated to its context.
369 self.initUiComponents()
371 def initUiComponents(self):
375 # create top-level menu
376 mid = sgPyQt.createMenu( "ADAO", -1, UI_ELT_IDS.ADAO_MENU_ID, sgPyQt.defaultMenuGroup() )
378 tid = sgPyQt.createTool( "ADAO" )
380 a = sgPyQt.createAction( UI_ELT_IDS.NEW_ADAOCASE_ID, "New case", "New case", "Create a new ADAO case", "eficas_new.png" )
381 sgPyQt.createMenu(a, mid)
382 sgPyQt.createTool(a, tid)
383 a = sgPyQt.createAction( UI_ELT_IDS.OPEN_ADAOCASE_ID, "Open case", "Open case", "Open an ADAO case", "eficas_open.png" )
384 sgPyQt.createMenu(a, mid)
385 sgPyQt.createTool(a, tid)
386 a = sgPyQt.createAction( UI_ELT_IDS.SAVE_ADAOCASE_ID, "Save case", "Save case", "Save an ADAO case", "eficas_save.png" )
387 sgPyQt.createMenu(a, mid)
388 sgPyQt.createTool(a, tid)
389 a = sgPyQt.createAction( UI_ELT_IDS.SAVE_AS_ADAOCASE_ID, "Save as case", "Save as case", "Save an ADAO case as", "eficas_saveas.png" )
390 sgPyQt.createMenu(a, mid)
391 sgPyQt.createTool(a, tid)
392 a = sgPyQt.createAction( UI_ELT_IDS.CLOSE_ADAOCASE_ID, "Close case", "Close case", "Close an ADAO case", "eficas_close.png" )
393 sgPyQt.createMenu(a, mid)
394 sgPyQt.createTool(a, tid)
395 a = sgPyQt.createAction( UI_ELT_IDS.YACS_EXPORT_ID, "Export to YACS", "Export to YACS", "Generate a YACS graph executing this case", "eficas_yacs.png" )
396 sgPyQt.createMenu(a, mid)
397 sgPyQt.createTool(a, tid)
399 # the following action are used in context popup
400 a = sgPyQt.createAction( UI_ELT_IDS.CLOSE_ADAOCASE_ID, "Close case", "Close case", "Close the selected case", "" )
401 a = sgPyQt.createAction( UI_ELT_IDS.YACS_EXPORT_ID, "Export to YACS", "Export to YACS", "Generate a YACS graph executing this case", "" )
403 def createPopupMenuOnItem(self,popup,salomeSudyId, item):
404 if adaoStudyEditor.isValidAdaoCaseItem(salomeSudyId, item):
405 popup.addAction( sgPyQt.action( UI_ELT_IDS.CLOSE_ADAOCASE_ID ) )
406 popup.addAction( sgPyQt.action( UI_ELT_IDS.YACS_EXPORT_ID ) )