Salome HOME
Updating copyright date information and version
[modules/adao.git] / src / daSalome / daGUI / daGuiImpl / adaoGuiManager.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2008-2015 EDF R&D
3 #
4 # This file is part of SALOME ADAO module
5 #
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.
10 #
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.
15 #
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
19 #
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22
23 """
24 This file centralizes the definitions and implementations of ui components used
25 in the GUI part of the module.
26 """
27
28 __author__ = "aribes/gboulant"
29
30 import traceback
31 from PyQt4.QtCore import QObject
32 from PyQt4.QtCore import *        # Import from PyQT
33 from PyQt4 import QtGui,QtCore
34 import SalomePyQt
35 sgPyQt = SalomePyQt.SalomePyQt()
36
37 from daUtils.enumerate import Enumerate
38 from daGuiImpl.adaoCase import AdaoCase
39 from daEficasWrapper.adaoEficasWrapper import AdaoEficasWrapper
40
41 from daUtils.adaoEficasEvent import *
42 import adaoGuiHelper
43 import adaoStudyEditor
44 from daUtils import adaoLogger
45
46 __cases__ = {}
47
48 #
49 # ==============================================================================
50 # Classes to manage the building of UI components
51 # ==============================================================================
52 #
53 UI_ELT_IDS = Enumerate([
54         'ADAO_MENU_ID',
55         'NEW_ADAOCASE_ID',
56         'OPEN_ADAOCASE_ID',
57         'SAVE_ADAOCASE_ID',
58         'SAVE_AS_ADAOCASE_ID',
59         'CLOSE_ADAOCASE_ID',
60         'YACS_EXPORT_ID',
61         ],offset=6950)
62
63 ACTIONS_MAP={
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",
70 }
71
72
73 class AdaoCaseManager(EficasObserver):
74   """
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)
77   """
78
79   def __init__(self):
80
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()
84     self.cases = {}
85
86     # Création des deux managers
87     self.salome_manager = AdaoGuiUiComponentBuilder()
88     self.eficas_manager = AdaoEficasWrapper(parent=SalomePyQt.SalomePyQt().getDesktop())
89
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
92     # processGUIEvent
93     self.eficas_manager.addObserver(self)
94
95     # Création du GUI Eficas
96     self.eficas_manager.init_gui()
97
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)
103     wmType = "ADAO View"
104     self.eficas_viewId = sgPyQt.createView(wmType, area)
105
106     # On interdit que la vue soit fermée
107     # Cela simplifier grandement le code
108     sgPyQt.setViewClosable(self.eficas_viewId, False)
109
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)
113
114 ######
115 #
116 # Gestion de l'activation/désactivation du module
117 #
118 ######
119
120   def activate(self):
121     self.eficas_manager.setEnabled(True)
122     sgPyQt.activateView(self.eficas_viewId)
123     self.harmonizeSelectionFromEficas()
124
125   def deactivate(self):
126     self.eficas_manager.setEnabled(False)
127
128 #######
129 #
130 # Gestion de la sélection entre le GUI d'Eficas
131 # et l'arbre d'étude de SALOME
132 #
133 #######
134
135   # Depuis l'étude SALOME
136   def currentSelectionChanged(self):
137     """
138     Cette méthode permet de changer le tab vu dans eficas
139     selon la sélection de l'utilisateur dans l'étude SALOME
140     """
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)
147           break
148
149   # Depuis Eficas
150   def _processEficasTabChanged(self, eficasWrapper, eficasEvent):
151     """
152     Gestion de la synchonisation entre le tab courant d'Eficas
153     et la selection dans l'étude SALOME
154     """
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())
159         break
160
161   # On remet la sélection dans SALOME grâce au tab dans Eficas
162   def harmonizeSelectionFromEficas(self):
163     """
164     Cette méthode permet d'harmoniser la sélection dans l'étude
165     grâce au tab courant d'Eficas
166     """
167     if self.cases:
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
171       if editor:
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())
175             break
176
177 #######
178 #
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
182 #
183 #######
184
185   def newAdaoCase(self):
186     adaoLogger.debug("Création d'un nouveau cas adao")
187     self.eficas_manager.adaofileNew(AdaoCase())
188
189   def _processEficasNewEvent(self, eficasWrapper, eficasEvent):
190     adao_case = eficasEvent.callbackId
191     # Ajout dand l'étude
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
200     # Ajout du cas
201     self.cases[adao_case.eficas_editor] = adao_case
202
203 #######
204 #
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
208 #
209 #######
210
211 # Rq: l'ouverture d'un cas adao est un cas particulier de la création d'un cas adao
212
213   def openAdaoCase(self):
214     adaoLogger.debug("Ouverture d'un cas adao")
215     self.eficas_manager.adaoFileOpen(AdaoCase())
216
217   def _processEficasOpenEvent(self, eficasWrapper, eficasEvent):
218     self._processEficasNewEvent(eficasWrapper, eficasEvent)
219
220 #######
221 #
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
226 #
227 #######
228
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)
238         break
239
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)
249         break
250
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())
258     # Ajout du cas
259     self.cases[adao_case.name] = adao_case
260
261 #######
262 #
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
266 #
267 #######
268
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)
278         break
279
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)
289     # Suppression du cas
290     self.cases.pop(editor)
291     # Refresh GUI -> appelle currentSelectionChanged()
292     adaoGuiHelper.refreshObjectBrowser()
293
294 #######
295 #
296 # Gestion de la connexion avec YACS
297 # 1: la fonction exportCasToYACS exporte l'étude vers YACS
298 #
299 #######
300   def exportCaseToYACS(self):
301     adaoLogger.debug("Export du cas vers YACS")
302
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():
309         if adao_case.isOk():
310           msg = adao_case.exportCaseToYACS()
311           # If msg is not empty -> error found
312           if msg != "":
313             adaoGuiHelper.gui_warning(SalomePyQt.SalomePyQt().getDesktop(), msg)
314         else:
315           adaoGuiHelper.gui_warning(SalomePyQt.SalomePyQt().getDesktop(), "Cannot export case, case is not valid")
316         break
317
318 #######
319 #
320 # Méthodes secondaires permettant de rediriger les évènements
321 # de SALOME et d'Eficas vers les bonnes méthodes de la classe
322 #
323 #######
324
325   # Gestion des évènements venant du manager Eficas
326   __processOptions={
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"
334       }
335
336   def processEficasEvent(self, eficasWrapper, eficasEvent):
337       """
338       Implementation of the interface EficasObserver. The implementation is a
339       switch on the possible types of events defined in EficasEvent.EVENT_TYPES.
340       @overload
341       """
342       functionName = self.__processOptions.get(eficasEvent.eventType, lambda : "_processEficasUnknownEvent")
343       return getattr(self,functionName)(eficasWrapper, eficasEvent)
344
345   def _processEficasUnknownEvent(self, eficasWrapper, eficasEvent):
346     adaoLogger.error("Unknown Eficas Event")
347
348   # Gestion des évènements venant du GUI de SALOME
349   def processGUIEvent(self, actionId):
350     """
351     Main switch function for ui actions processing
352     """
353     if ACTIONS_MAP.has_key(actionId):
354       try:
355           functionName = ACTIONS_MAP[actionId]
356           getattr(self,functionName)()
357       except:
358           traceback.print_exc()
359     else:
360       adaoLogger.warning("The requested action is not implemented: " + str(actionId))
361
362 class AdaoGuiUiComponentBuilder:
363     """
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.
367     """
368     def __init__(self):
369         self.initUiComponents()
370
371     def initUiComponents(self):
372
373         objectTR = QObject()
374
375         # create top-level menu
376         mid = sgPyQt.createMenu( "ADAO", -1, UI_ELT_IDS.ADAO_MENU_ID, sgPyQt.defaultMenuGroup() )
377         # create toolbar
378         tid = sgPyQt.createTool( "ADAO" )
379
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)
398
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", "" )
402
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 ) )
407         return popup