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.QtCore import * # Import from PyQT
31 from PyQt4 import QtGui,QtCore
33 sgPyQt = SalomePyQt.SalomePyQt()
35 from daUtils.enumerate import Enumerate
36 from daGuiImpl.adaoCase import AdaoCase
37 from daEficasWrapper.adaoEficasWrapper import AdaoEficasWrapper
39 from daUtils.adaoEficasEvent import *
41 import adaoStudyEditor
42 from daUtils import adaoLogger
47 # ==============================================================================
48 # Classes to manage the building of UI components
49 # ==============================================================================
51 UI_ELT_IDS = Enumerate([
56 'SAVE_AS_ADAOCASE_ID',
62 UI_ELT_IDS.NEW_ADAOCASE_ID:"newAdaoCase",
63 UI_ELT_IDS.OPEN_ADAOCASE_ID:"openAdaoCase",
64 UI_ELT_IDS.SAVE_ADAOCASE_ID:"saveAdaoCase",
65 UI_ELT_IDS.SAVE_AS_ADAOCASE_ID:"saveasAdaoCase",
66 UI_ELT_IDS.CLOSE_ADAOCASE_ID:"closeAdaoCase",
67 UI_ELT_IDS.YACS_EXPORT_ID:"exportCaseToYACS",
71 class AdaoCaseManager(EficasObserver):
73 Cette classe gére les cas ADAO et coordonne les GUI de SALOME (l'étude)
74 et le GUI de l'objet Eficas (héritage du module Eficas)
79 # Création d'un dictionnaire de cas
80 # Key == ref objet editor eficas (on est sur qu'elle est unique, cas duplication)
81 # Value == objet AdaoCase()
84 # Création des deux managers
85 self.salome_manager = AdaoGuiUiComponentBuilder()
86 self.eficas_manager = AdaoEficasWrapper(parent=SalomePyQt.SalomePyQt().getDesktop())
88 # On s'enregistre comme observer pour les évènements venant d'Eficas
89 # Les évènements du salome_manager viennent par le biais de la méthode
91 self.eficas_manager.addObserver(self)
93 # Création du GUI Eficas
94 self.eficas_manager.init_gui()
96 # Création du viewer QT
97 # Scroll Widget (pour les petites résolutions)
98 area = QtGui.QScrollArea(SalomePyQt.SalomePyQt().getDesktop());
99 area.setWidget(self.eficas_manager)
100 area.setWidgetResizable(1)
102 self.eficas_viewId = sgPyQt.createView(wmType, area)
104 # On interdit que la vue soit fermée
105 # Cela simplifier grandement le code
106 sgPyQt.setViewClosable(self.eficas_viewId, False)
108 # On s'abonne au gestionnaire de selection
109 self.selection_manager = sgPyQt.getSelection()
110 QtCore.QObject.connect(self.selection_manager, QtCore.SIGNAL('currentSelectionChanged()'), self.currentSelectionChanged)
114 # Gestion de l'activation/désactivation du module
119 self.eficas_manager.setEnabled(True)
120 sgPyQt.activateView(self.eficas_viewId)
121 self.harmonizeSelectionFromEficas()
123 def deactivate(self):
124 self.eficas_manager.setEnabled(False)
128 # Gestion de la sélection entre le GUI d'Eficas
129 # et l'arbre d'étude de SALOME
133 # Depuis l'étude SALOME
134 def currentSelectionChanged(self):
136 Cette méthode permet de changer le tab vu dans eficas
137 selon la sélection de l'utilisateur dans l'étude SALOME
139 adaoLogger.debug("currentSelectionChanged")
140 salomeStudyItem = adaoGuiHelper.getSelectedItem()
141 for case_editor, adao_case in self.cases.iteritems():
142 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
143 self.eficas_manager.selectCase(adao_case.eficas_editor)
147 def _processEficasTabChanged(self, eficasWrapper, eficasEvent):
149 Gestion de la synchonisation entre le tab courant d'Eficas
150 et la selection dans l'étude SALOME
152 editor = eficasEvent.callbackId
153 for case_editor, adao_case in self.cases.iteritems():
154 if case_editor is editor:
155 adaoGuiHelper.selectItem(adao_case.salome_study_item.GetID())
158 # On remet la sélection dans SALOME grâce au tab dans Eficas
159 def harmonizeSelectionFromEficas(self):
161 Cette méthode permet d'harmoniser la sélection dans l'étude
162 grâce au tab courant d'Eficas
165 # 1: Get current tab index in Eficas
166 editor = self.eficas_manager.getCurrentEditor()
167 # 2: sync with SALOME GUI is a tab is opened
169 for case_editor, adao_case in self.cases.iteritems():
170 if case_editor is editor:
171 adaoGuiHelper.selectItem(adao_case.salome_study_item.GetID())
176 # Gestion de la création d'un nouveau cas
177 # 1: la fonction newAdaoCase est appelée par le GUI SALOME
178 # 2: la fonction _processEficasNewEvent est appelée par le manager EFICAS
182 def newAdaoCase(self):
183 adaoLogger.debug("Création d'un nouveau cas adao")
184 self.eficas_manager.adaofileNew(AdaoCase())
186 def _processEficasNewEvent(self, eficasWrapper, eficasEvent):
187 adao_case = eficasEvent.callbackId
189 salomeStudyId = adaoGuiHelper.getActiveStudyId()
190 salomeStudyItem = adaoStudyEditor.addInStudy(salomeStudyId, adao_case)
191 # Affichage correct dans l'étude
192 adaoGuiHelper.refreshObjectBrowser()
193 adaoGuiHelper.selectItem(salomeStudyItem.GetID())
194 # Finalisation des données du cas
195 adao_case.salome_study_id = salomeStudyId
196 adao_case.salome_study_item = salomeStudyItem
198 self.cases[adao_case.eficas_editor] = adao_case
202 # Gestion de l'ouverture d'un cas
203 # 1: la fonction openAdaoCase est appelée par le GUI SALOME
204 # 2: la fonction _processEficasOpenEvent est appelée par le manager EFICAS
208 # Rq: l'ouverture d'un cas adao est un cas particulier de la création d'un cas adao
210 def openAdaoCase(self):
211 adaoLogger.debug("Ouverture d'un cas adao")
212 self.eficas_manager.adaoFileOpen(AdaoCase())
214 def _processEficasOpenEvent(self, eficasWrapper, eficasEvent):
215 self._processEficasNewEvent(eficasWrapper, eficasEvent)
219 # Gestion de la sauvegarde d'un cas
220 # 1: la fonction saveAdaoCase est appelée par le GUI SALOME
221 # 1 bis: la fonction saveasAdaoCase est appelée par le GUI SALOME
222 # 2: la fonction _processEficasSaveEvent est appelée par le manager EFICAS
226 def saveAdaoCase(self):
227 adaoLogger.debug("Sauvegarde du cas s'il y a modification")
228 # A priori, l'utilisateur s'attend à sauvegarder le cas qui est ouvert
229 # dans le GUI d'Eficas
230 self.harmonizeSelectionFromEficas()
231 salomeStudyItem = adaoGuiHelper.getSelectedItem()
232 for case_name, adao_case in self.cases.iteritems():
233 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
234 self.eficas_manager.adaoFileSave(adao_case)
237 def saveasAdaoCase(self):
238 adaoLogger.debug("Sauvegarde du cas s'il y a modification (version save as)")
239 # A priori, l'utilisateur s'attend à sauvegarder le cas qui est ouvert
240 # dans le GUI d'Eficas
241 self.harmonizeSelectionFromEficas()
242 salomeStudyItem = adaoGuiHelper.getSelectedItem()
243 for case_name, adao_case in self.cases.iteritems():
244 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
245 self.eficas_manager.adaoFileSaveAs(adao_case)
248 def _processEficasSaveEvent(self, eficasWrapper, eficasEvent):
249 adao_case = eficasEvent.callbackId
250 # On met à jour l'étude
251 adaoStudyEditor.updateItem(adao_case.salome_study_id, adao_case.salome_study_item, adao_case)
252 # Affichage correct dans l'étude
253 adaoGuiHelper.refreshObjectBrowser()
254 adaoGuiHelper.selectItem(adao_case.salome_study_item.GetID())
256 self.cases[adao_case.name] = adao_case
260 # Gestion de la fermeture d'un cas
261 # 1: la fonction closeAdaoCase est appelée par le GUI SALOME
262 # 2: la fonction _processEficasCloseEvent est appelée par le manager EFICAS
266 def closeAdaoCase(self):
267 adaoLogger.debug("Fermeture d'un cas")
268 # A priori, l'utilisateur s'attend à sauvegarder le cas qui est ouvert
269 # dans le GUI d'Eficas
270 self.harmonizeSelectionFromEficas()
271 salomeStudyItem = adaoGuiHelper.getSelectedItem()
272 for case_name, adao_case in self.cases.iteritems():
273 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
274 self.eficas_manager.adaoFileClose(adao_case)
277 def _processEficasCloseEvent(self, eficasWrapper, eficasEvent):
278 adaoLogger.debug("Destruction d'un cas")
279 editor = eficasEvent.callbackId
280 # Recuperation du cas
281 adao_case = self.cases[editor]
282 # Suppression de l'objet dans l'étude
283 adaoStudyEditor.removeItem(adao_case.salome_study_id, adao_case.salome_study_item)
285 self.cases.pop(editor)
286 # Refresh GUI -> appelle currentSelectionChanged()
287 adaoGuiHelper.refreshObjectBrowser()
291 # Gestion de la connexion avec YACS
292 # 1: la fonction exportCasToYACS exporte l'étude vers YACS
295 def exportCaseToYACS(self):
296 adaoLogger.debug("Export du cas vers YACS")
298 # A priori, l'utilisateur s'attend à exporter le cas qui est ouvert
299 # dans le GUI d'Eficas
300 self.harmonizeSelectionFromEficas()
301 salomeStudyItem = adaoGuiHelper.getSelectedItem()
302 for case_name, adao_case in self.cases.iteritems():
303 if adao_case.salome_study_item.GetID() == salomeStudyItem.GetID():
305 msg = adao_case.exportCaseToYACS()
306 # If msg is not empty -> error found
308 adaoGuiHelper.gui_warning(SalomePyQt.SalomePyQt().getDesktop(), msg)
310 adaoGuiHelper.gui_warning(SalomePyQt.SalomePyQt().getDesktop(), "Cannot export case, case is not valid")
315 # Méthodes secondaires permettant de rediriger les évènements
316 # de SALOME et d'Eficas vers les bonnes méthodes de la classe
320 # Gestion des évènements venant du manager Eficas
322 EficasEvent.EVENT_TYPES.CLOSE : "_processEficasCloseEvent",
323 EficasEvent.EVENT_TYPES.SAVE : "_processEficasSaveEvent",
324 EficasEvent.EVENT_TYPES.NEW : "_processEficasNewEvent",
325 EficasEvent.EVENT_TYPES.CLOSE : "_processEficasCloseEvent",
326 EficasEvent.EVENT_TYPES.OPEN : "_processEficasOpenEvent",
327 EficasEvent.EVENT_TYPES.TABCHANGED : "_processEficasTabChanged",
328 EficasEvent.EVENT_TYPES.REOPEN : "_processEficasReOpenEvent"
331 def processEficasEvent(self, eficasWrapper, eficasEvent):
333 Implementation of the interface EficasObserver. The implementation is a
334 switch on the possible types of events defined in EficasEvent.EVENT_TYPES.
337 functionName = self.__processOptions.get(eficasEvent.eventType, lambda : "_processEficasUnknownEvent")
338 return getattr(self,functionName)(eficasWrapper, eficasEvent)
340 def _processEficasUnknownEvent(self, eficasWrapper, eficasEvent):
341 adaoLogger.error("Unknown Eficas Event")
343 # Gestion des évènements venant du GUI de SALOME
344 def processGUIEvent(self, actionId):
346 Main switch function for ui actions processing
348 if ACTIONS_MAP.has_key(actionId):
350 functionName = ACTIONS_MAP[actionId]
351 getattr(self,functionName)()
353 traceback.print_exc()
355 adaoLogger.warning("The requested action is not implemented: " + str(actionId))
357 class AdaoGuiUiComponentBuilder:
359 The initialisation of this class creates the graphic components involved
360 in the GUI (menu, menu item, toolbar). A ui component builder should be
361 created for each opened study and associated to its context.
364 self.initUiComponents()
366 def initUiComponents(self):
370 # create top-level menu
371 mid = sgPyQt.createMenu( "ADAO", -1, UI_ELT_IDS.ADAO_MENU_ID, sgPyQt.defaultMenuGroup() )
373 tid = sgPyQt.createTool( "ADAO" )
375 a = sgPyQt.createAction( UI_ELT_IDS.NEW_ADAOCASE_ID, "New case", "New case", "Create a new adao case", "eficas_new.png" )
376 sgPyQt.createMenu(a, mid)
377 sgPyQt.createTool(a, tid)
378 a = sgPyQt.createAction( UI_ELT_IDS.OPEN_ADAOCASE_ID, "Open case", "Open case", "Open an adao case", "eficas_open.png" )
379 sgPyQt.createMenu(a, mid)
380 sgPyQt.createTool(a, tid)
381 a = sgPyQt.createAction( UI_ELT_IDS.SAVE_ADAOCASE_ID, "Save case", "Save case", "Save an adao case", "eficas_save.png" )
382 sgPyQt.createMenu(a, mid)
383 sgPyQt.createTool(a, tid)
384 a = sgPyQt.createAction( UI_ELT_IDS.SAVE_AS_ADAOCASE_ID, "Save as case", "Save as case", "Save an adao case as", "eficas_saveas.png" )
385 sgPyQt.createMenu(a, mid)
386 sgPyQt.createTool(a, tid)
387 a = sgPyQt.createAction( UI_ELT_IDS.CLOSE_ADAOCASE_ID, "Close case", "Close case", "Close an adao case", "eficas_close.png" )
388 sgPyQt.createMenu(a, mid)
389 sgPyQt.createTool(a, tid)
390 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" )
391 sgPyQt.createMenu(a, mid)
392 sgPyQt.createTool(a, tid)
394 # the following action are used in context popup
395 a = sgPyQt.createAction( UI_ELT_IDS.CLOSE_ADAOCASE_ID, "Close case", "Close case", "Close the selected case", "" )
396 a = sgPyQt.createAction( UI_ELT_IDS.YACS_EXPORT_ID, "Export to YACS", "Export to YACS", "Generate a YACS graph executing this case", "" )
398 def createPopupMenuOnItem(self,popup,salomeSudyId, item):
399 if adaoStudyEditor.isValidAdaoCaseItem(salomeSudyId, item):
400 popup.addAction( sgPyQt.action( UI_ELT_IDS.CLOSE_ADAOCASE_ID ) )
401 popup.addAction( sgPyQt.action( UI_ELT_IDS.YACS_EXPORT_ID ) )