Salome HOME
Edit popup
[modules/adao.git] / src / daSalome / daGUI / daGuiImpl / datassimGuiManager.py
1 # -*- coding: iso-8859-1 -*-
2 #  Copyright (C) 2010 EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 """
22 This file centralizes the definitions and implementations of ui components used
23 in the GUI part of the module.
24 """
25
26 __author__ = "aribes/gboulant"
27
28 import traceback
29 from PyQt4.QtCore import QObject
30 import SalomePyQt
31 sgPyQt = SalomePyQt.SalomePyQt()
32
33 from daGuiImpl.enumerate import Enumerate
34 from daGuiImpl.datassimCase import DatassimCase
35 from daEficasWrapper.datassimEficasWrapper import DatassimEficasWrapper
36 from daEficasWrapper.eficasWrapper import EficasObserver
37 from daEficasWrapper.eficasWrapper import EficasEvent
38 import datassimGuiHelper
39 import datassimStudyEditor
40
41 __cases__ = {}
42
43 #
44 # ==============================================================================
45 # Classes to manage the building of UI components
46 # ==============================================================================
47 #
48 UI_ELT_IDS = Enumerate([
49         'DATASSIM_MENU_ID',
50         'NEW_DATASSIMCASE_ID',
51         'EDIT_DATASSIMCASE_POP_ID',
52         'DELETE_DATASSIMCASE_POP_ID',
53         'YACS_EXPORT_POP_ID',
54         ],offset=950)
55
56 ACTIONS_MAP={
57     UI_ELT_IDS.NEW_DATASSIMCASE_ID:"newDatassimCase",
58     UI_ELT_IDS.EDIT_DATASSIMCASE_POP_ID:"editDatassimCase",
59 }
60
61 class DatassimGuiUiComponentBuilder:
62     """
63     The initialisation of this class creates the graphic components involved
64     in the GUI (menu, menu item, toolbar). A ui component builder should be
65     created for each opened study and associated to its context (see usage in OMAGUI.py).
66     """
67     def __init__(self):
68         self.initUiComponents()
69
70     def initUiComponents(self):
71         
72         objectTR = QObject()
73
74         # create top-level menu
75         mid = sgPyQt.createMenu( "DATASSIM", -1, UI_ELT_IDS.DATASSIM_MENU_ID, sgPyQt.defaultMenuGroup() )
76         # create toolbar
77         tid = sgPyQt.createTool( "DATASSIM" )
78
79         a = sgPyQt.createAction( UI_ELT_IDS.NEW_DATASSIMCASE_ID, "New case", "New case", "Create a new datassim case", "" )
80         sgPyQt.createMenu(a, mid)
81         sgPyQt.createTool(a, tid)
82
83         # the following action are used in context popup
84         a = sgPyQt.createAction( UI_ELT_IDS.EDIT_DATASSIMCASE_POP_ID, "Edit case", "Edit case", "Edit the selected study case", "" )
85         a = sgPyQt.createAction( UI_ELT_IDS.DELETE_DATASSIMCASE_POP_ID, "Delete case", "Delete case", "Delete the selected study case", "" )
86         a = sgPyQt.createAction( UI_ELT_IDS.YACS_EXPORT_POP_ID, "Export to YACS", "Export to YACS", "Generate a YACS graph executing this case", "" )
87
88     def createPopupMenuOnItem(self,popup,salomeSudyId, item):
89         if datassimStudyEditor.isValidDatassimCaseItem(salomeSudyId, item):
90           popup.addAction( sgPyQt.action( UI_ELT_IDS.EDIT_DATASSIMCASE_POP_ID ) )
91           popup.addAction( sgPyQt.action( UI_ELT_IDS.DELETE_DATASSIMCASE_POP_ID ) )
92           popup.addAction( sgPyQt.action( UI_ELT_IDS.YACS_EXPORT_POP_ID ) )
93
94         return popup
95
96 class DatassimGuiActionImpl(EficasObserver):
97     """
98     This class implements the ui actions concerning the management of oma study
99     cases.
100     """
101     __dlgEficasWrapper = None
102
103     def __init__(self):
104         pass
105         # This dialog is created once so that it can be recycled for each call
106         # to newOmaCase().
107         #self.__dlgNewStudyCase = DlgNewStudyCase()
108         self.__dlgEficasWrapper = DatassimEficasWrapper(parent=SalomePyQt.SalomePyQt().getDesktop())
109         self.__dlgEficasWrapper.addObserver(self)
110
111     # ==========================================================================
112     # Processing of ui actions
113     #
114     def processAction(self,actionId):
115         """
116         Main switch function for ui actions processing
117         """
118         if ACTIONS_MAP.has_key(actionId):
119             try:
120                 functionName = ACTIONS_MAP[actionId]
121                 getattr(self,functionName)()
122             except:
123                 traceback.print_exc()
124         else:
125             msg = "The requested action is not implemented: " + str(actionId)
126             print msg
127
128     def newDatassimCase(self):
129       self.__dlgEficasWrapper.displayNew()
130
131     def editDatassimCase(self):
132       global __cases__
133       salomeStudyId   = datassimGuiHelper.getActiveStudyId()
134       salomeStudyItem = datassimGuiHelper.getSelectedItem(salomeStudyId)
135       case_key = (salomeStudyId, salomeStudyItem.GetName())
136       try:
137         case = __cases__[case_key]
138         self.__dlgEficasWrapper.Openfile(case.get_name())
139         callbackId = [salomeStudyId, salomeStudyItem]
140         self.__dlgEficasWrapper.setCallbackId(callbackId)
141         self.__dlgEficasWrapper.show()
142       except:
143         print "Oups - cannot edit case !"
144         traceback.print_exc()
145
146
147     # ==========================================================================
148     # Processing notifications from eficas
149     #
150     __processOptions={
151         EficasEvent.EVENT_TYPES.CLOSE : "_processEficasCloseEvent",
152         EficasEvent.EVENT_TYPES.SAVE  : "_processEficasSaveEvent",
153         EficasEvent.EVENT_TYPES.NEW  : "_processEficasNewEvent",
154         EficasEvent.EVENT_TYPES.DESTROY  : "_processEficasDestroyEvent"
155         }
156     def processEficasEvent(self, eficasWrapper, eficasEvent):
157         """
158         Implementation of the interface EficasObserver. The implementation is a
159         switch on the possible types of events defined in EficasEvent.EVENT_TYPES.
160         @overload
161         """
162         functionName = self.__processOptions.get(eficasEvent.eventType, lambda : "_processEficasUnknownEvent")
163         return getattr(self,functionName)(eficasWrapper, eficasEvent)
164
165     def _processEficasCloseEvent(self, eficasWrapper, eficasEvent):
166         print "This is the process of EficasCloseEvent"
167         print "Remove datassim case in study if empty..."
168         pass
169
170     def _processEficasNewEvent(self, eficasWrapper, eficasEvent):
171       global __cases__
172       new_case = DatassimCase()
173       salomeStudyId   = datassimGuiHelper.getActiveStudyId()
174       salomeStudyItem = datassimStudyEditor.addInStudy(salomeStudyId, new_case)
175       case_key = (salomeStudyId, salomeStudyItem.GetName())
176       __cases__[case_key] = new_case
177       datassimGuiHelper.refreshObjectBrowser()
178       callbackId = [salomeStudyId, salomeStudyItem]
179       self.__dlgEficasWrapper.setCallbackId(callbackId)
180
181     def _processEficasSaveEvent(self, eficasWrapper, eficasEvent):
182         global __cases__
183         callbackId = eficasEvent.callbackId
184         if callbackId is None:
185             raise DevelException("the callback data should not be None. Can't guess what are the study and case")
186         [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
187         if ( targetSalomeStudyId is None ) or ( targetSalomeStudyItem is None ):
188             raise DevelException("the parameters targetSalomeStudyId and targetSalomeStudyItem should not be None")
189
190         # Get Editor All infos we need !
191         file_name = eficasWrapper.getCaseName()
192         if file_name != "" :
193           # Get case
194           old_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetName())
195           case =__cases__[old_case_key]
196
197           # Set new informations
198           case.set_name(file_name)
199           datassimStudyEditor.updateItem(targetSalomeStudyId, targetSalomeStudyItem, case)
200
201           # Case key changed !
202           new_case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetName())
203           # A ne pas inverser !!!
204           __cases__.pop(old_case_key)
205           __cases__[new_case_key] = case
206
207           datassimGuiHelper.refreshObjectBrowser()
208
209     def _processEficasDestroyEvent(self, eficasWrapper, eficasEvent):
210         global __cases__
211         callbackId = eficasEvent.callbackId
212         if callbackId is None:
213             raise DevelException("the callback data should not be None. Can't guess what are the study and case")
214         [targetSalomeStudyId,targetSalomeStudyItem] = callbackId
215         if ( targetSalomeStudyId is None ) or ( targetSalomeStudyItem is None ):
216             raise DevelException("the parameters targetSalomeStudyId and targetSalomeStudyItem should not be None")
217
218         case_key = (targetSalomeStudyId, targetSalomeStudyItem.GetName())
219         __cases__.pop(case_key)
220         datassimStudyEditor.removeItem(targetSalomeStudyId, targetSalomeStudyItem)
221         datassimGuiHelper.refreshObjectBrowser()
222
223     def _processEficasUnknownEvent(self, eficasWrapper, eficasEvent):
224       print "Unknown Eficas Event"