Salome HOME
b60aafd3aeddfcbcd58df4a8d8745c2300fba456
[modules/med.git] / src / MEDCalc / tui / medevents.py
1 # Copyright (C) 2015-2023  CEA, EDF
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 import medcalc
21 import SALOME
22 import salome
23
24 dataManager = medcalc.medcorba.factory.getDataManager()
25
26 # Initializing the notification system (events from components to GUI)
27
28 # The MEDEventListener is created in the GUI (WorkspaceController) and
29 # a reference is transmitted to the python context throw its IOR. The
30 # transmission consists in initializing the variable below from the
31 # GUI (who knows the IOR) when importing medcalc in the python console
32 # (see WorkspaceController)
33 __eventListener = None
34 def connectEventListener():
35   global __eventListener
36   try:
37     eventListenerIOR = dataManager.getEventListenerIOR()
38     __eventListener = salome.orb.string_to_object(eventListenerIOR)
39   except SALOME.SALOME_Exception as e:
40     medcalc.wrn("The event listener is not running yet")
41     msg ="When you'll have loaded the MED GUI, "
42     msg+="call explicitly \"medcalc.medevents.connectEventListener()\" "
43     msg+="to connect the GUI event listener"
44     medcalc.inf(msg)
45     __eventListener = None
46   except Exception as e:
47     medcalc.err("An unknown error occurs. Check if this ior=%s is valid."%eventListenerIOR)
48     print(e)
49 #
50
51 def eventListenerIsRunning():
52   global __eventListener
53   if __eventListener is not None:
54     return True
55
56   # Try to define the event listener
57   connectEventListener()
58   if __eventListener is None:
59     # it definitely does not work
60     medcalc.wrn("the GUI is not loaded yet and will not be notified of the modification")
61     return False
62
63   return True
64 #
65
66 def processMedEvent(event):
67   __eventListener.processMedEvent(event)
68 #
69
70 # One can try to connect to the eventListener, but it will fail if the
71 # MED GUI is not loaded. That does not matter, because the event
72 # listener is used only to notify the GUI of some event. If the GUI is
73 # not loaded, there is no use of notification.
74 connectEventListener()
75
76
77 #
78 # ===================================================================
79 # Functions for events notification
80 # ===================================================================
81 #
82 # Note that these functions are not part of the class FieldProxy so
83 # that they could be used in another context than the FieldProxy instances
84 import MEDCALC
85
86 def __notifyGui(eventType, dataId=-1, filename="", presentationId=-1, msg=""):
87   medEvent = MEDCALC.MedEvent(eventType, dataId, filename, presentationId, msg)
88   if not eventListenerIsRunning(): return
89
90   # Notify the GUI of the update event
91   processMedEvent(medEvent)
92 #
93
94 def notifyGui_updateField(fieldId):
95   """
96   This function must be used to notify the GUI that the field
97   meta-data have changed so it could update the gui
98   presentations of this field.
99   """
100   __notifyGui(MEDCALC.EVENT_UPDATE_FIELD, dataId=fieldId)
101 #
102
103 def notifyGui_putInWorkspace(fieldId):
104   __notifyGui(MEDCALC.EVENT_PUT_IN_WORKSPACE, dataId=fieldId)
105 #
106 def notifyGui_removeFromWorkspace(fieldId):
107   __notifyGui(MEDCALC.EVENT_REMOVE_FROM_WORKSPACE, dataId=fieldId)
108
109 def notifyGui_cleanWorkspace():
110   __notifyGui(MEDCALC.EVENT_CLEAN_WORKSPACE)
111 #
112 def notifyGui_addDatasource(handlerId, filename):
113   __notifyGui(MEDCALC.EVENT_ADD_DATASOURCE, dataId=handlerId, filename=filename)
114 #
115 def notifyGui_addPresentation(fieldId, presId):
116   __notifyGui(MEDCALC.EVENT_ADD_PRESENTATION, dataId=fieldId, presentationId=presId)
117 #
118 def notifyGui_removePresentation(presId):
119   __notifyGui(MEDCALC.EVENT_REMOVE_PRESENTATION, presentationId=presId)
120 #
121 def notifyGui_modifyPresentation(presId):
122   __notifyGui(MEDCALC.EVENT_MODIFY_PRESENTATION, presentationId=presId)
123 #
124 def notifyGui_visibilityChanged(presId):
125   __notifyGui(MEDCALC.EVENT_VISIBILITY_CHANGED, presentationId=presId)
126
127 def notifyGui_playQtTestingScenario(filename):
128   __notifyGui(MEDCALC.EVENT_PLAY_TEST, filename=filename)
129 #
130 def notifyGui_termination():
131   __notifyGui(MEDCALC.EVENT_QUIT_SALOME)
132
133 def notifyGui_error(msg):
134   __notifyGui(MEDCALC.EVENT_ERROR, msg=msg)
135
136 def notifyGui_changeUnderlyingMesh(fieldId):
137   __notifyGui(MEDCALC.EVENT_CHANGE_UNDERLYING_MESH, dataId=fieldId)
138
139 def notifyGui_interpolateField(fieldId):
140   __notifyGui(MEDCALC.EVENT_INTERPOLATE_FIELD, dataId=fieldId)