Salome HOME
Mise à jour du template PythonComponent:
[tools/sat.git] / data / templates / PythonComponent / src / PYCMPGUI / PYCMPGUI.py
1 #  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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 # File   : :sat:{PYCMP}GUI.py
25 # Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 # ---
27 #
28 import traceback
29 import string
30 import os
31 import sys
32 from qtsalome import *
33
34 #from :sat:{PYCMP}_utils import *
35
36 import salome
37 from Controller import Controller
38 from TreeWidget import TreeWidget
39 from :sat:{PYCMP}Desktop import :sat:{PYCMP}Desktop
40
41 # Get SALOME PyQt interface
42 import SalomePyQt
43 import libSALOME_Swig
44
45 ########################################################
46 # Global variables
47 ########################################################
48
49 sgPyQt = SalomePyQt.SalomePyQt()
50 sg = libSALOME_Swig.SALOMEGUI_Swig()
51 sgDesktop = sgPyQt.getDesktop()
52 widgetDialogBox = None
53
54 objectsManager = Controller( None )
55 moduleDesktop   = {}
56 currentDesktop = None
57
58 CURVE_MENU_ID = 1000
59 ADVANCED_MENU_ID = 1001
60 POLYLINE_ID = 1002
61 CIRCLE_ID = 1003
62 DEL_ALL_ID = 1004
63
64 ########################################################
65 # Internal methods
66 ########################################################
67
68 def getStudyId():
69     """This method returns the active study ID"""
70     return sgPyQt.getStudyId()
71
72 def getStudy():
73     """This method returns the active study"""
74
75     studyId = _getStudyId()
76     study = getStudyManager().GetStudyByID( studyId )
77     return study
78
79 def getDesktop():
80     """This method returns the current :sat:{PYCMP} desktop"""
81
82     global currentDesktop
83     return currentDesktop
84
85 def setDesktop( studyID ):
86     """This method sets and returns :sat:{PYCMP} desktop"""
87
88     global moduleDesktop, currentDesktop, objectsManager
89
90     if not moduleDesktop.has_key( studyID ):
91         moduleDesktop[studyID] = :sat:{PYCMP}Desktop( sgPyQt, sg )
92         objectsManager = Controller( moduleDesktop[studyID] )
93         moduleDesktop[studyID].setController( objectsManager )
94         pass
95     currentDesktop = moduleDesktop[studyID]
96     return currentDesktop
97
98 def incObjToMap( m, id ):
99     """This method incrementes the object counter in the map"""
100
101     if id not in m: m[id] = 0
102     m[id] += 1
103     pass
104
105 def getSelection():
106     """This method analyses selection"""
107
108     selcount = sg.SelectedCount()
109     seltypes = {}
110     for i in range( selcount ):
111         incObjToMap( seltypes, getObjectID( getStudy(), sg.getSelected( i ) ) )
112         pass
113     return selcount, seltypes
114
115 ################################################
116 # Callback functions
117 ################################################
118
119 def initialize():
120     """This method is called when module is initialized. It performs initialization actions"""
121
122     setDesktop( getStudyId() )
123     pass
124
125 def windows():
126     """This method is called when module is initialized. It returns a map of popup windows to be used by the module"""
127
128     wm = {}
129     wm[SalomePyQt.WT_ObjectBrowser] = Qt.LeftDockWidgetArea
130     wm[SalomePyQt.WT_PyConsole]     = Qt.BottomDockWidgetArea
131     return wm
132
133 def views():
134     """This method is called when module is initialized. It returns a list of 2D/3D views to be used by the module"""
135     return []
136
137 def createPreferences():
138     """This method is called when module is initialized. It exports module preferences"""
139     pass
140
141 def activate():
142     """This method is called when module is initialized. It returns True if activating is successfull, False otherwise"""
143
144     global moduleDesktop, sgPyQt, widgetDialogBox
145
146     widgetDialogBox = QDockWidget( sgDesktop )
147     moduleDesktop[getStudyId()].createActions()
148     moduleDesktop[getStudyId()].createMenus()
149     moduleDesktop[getStudyId()].createToolBars()
150     moduleDesktop[getStudyId()].createPopups()
151     moduleDesktop[getStudyId()].getDockGlobalTree().show()
152     moduleDesktop[getStudyId()].getGlobalGraphicsView().show()
153     sgPyQt.activateView( moduleDesktop[getStudyId()].getGlobalGraphicsViewID() )
154     return True
155
156 def viewTryClose( wid ):
157     sgPyQt.setViewClosable(wid, True)
158     pass
159
160 def deactivate():
161     """This method is called when module is deactivated"""
162
163     global moduleDesktop, widgetDialogBox
164
165     widgetDialogBox.close()
166     moduleDesktop[getStudyId()].getDockGlobalTree().hide()
167     moduleDesktop[getStudyId()].updateGlobalGraphicsView( None )
168     moduleDesktop[getStudyId()].getGlobalGraphicsView().hide()
169     pass
170
171 def activeStudyChanged( studyID ):
172     """This method is called when active study is changed"""
173
174     setDesktop( getStudyId() )
175     pass
176
177 def createPopupMenu( popup, context ):
178     """This method is called when popup menu is invocked"""
179     pass
180
181 def OnGUIEvent( commandID ):
182     """This method is called when a GUI action is activated"""
183
184     if dict_command.has_key( commandID ):
185        dict_command[commandID]()
186        pass
187     pass
188
189 def preferenceChanged( section, setting ):
190     """This method is called when module's preferences are changed"""
191     pass
192
193 def activeViewChanged( viewID ):
194     """This method is called when active view is changed"""
195     pass
196
197 def viewCloned( viewID ):
198     """This method is called when active view is cloned"""
199     pass
200
201 def viewClosed( viewID ):
202     """This method is called when active view viewClosed"""
203     pass
204
205 def engineIOR():
206     """This method is called when study is opened. It returns engine IOR"""
207     return getEngineIOR()
208
209
210 ################################################
211 # GUI actions implementation
212 ################################################
213
214 def showCreatePolylineDialog() :
215     from CreatePolylineDialog import CreatePolylineDialog
216
217     global widgetDialogBox
218     widgetDialogBox = QDockWidget( sgDesktop )
219     myDialog = CreatePolylineDialog( "www.cea.fr", objectsManager, widgetDialogBox )
220     widgetDialogBox.setWidget( myDialog )
221     widgetDialogBox.setWindowTitle( "Polyline definition" )
222     sgDesktop.addDockWidget(Qt.LeftDockWidgetArea, widgetDialogBox)
223     pass
224
225 def showCreateCircleDialog() :
226     from CreateCircleDialog import CreateCircleDialog
227
228     global widgetDialogBox
229     widgetDialogBox = QDockWidget( sgDesktop )
230     myDialog = CreateCircleDialog( "www.cea.fr", objectsManager, widgetDialogBox )
231     widgetDialogBox.setWidget( myDialog )
232     widgetDialogBox.setWindowTitle( "Circle definition" )
233     sgDesktop.addDockWidget(Qt.LeftDockWidgetArea, widgetDialogBox)
234     pass
235
236 def deleteAll() :
237     models = moduleDesktop[getStudyId()].getController().getModels()
238     if len( models ) == 0 : return
239     answer = QMessageBox.question( moduleDesktop[getStudyId()], 'Confirmation', 'Do you really want to delete all the existing objects ?' , QMessageBox.Yes | QMessageBox.No )
240     if answer == QMessageBox.Yes :
241        for model in models :
242           moduleDesktop[getStudyId()].getController().removeModel( model )
243           pass
244        pass
245     pass
246
247 ########################################################
248 # Commands dictionary
249 ########################################################
250
251 dict_command = { POLYLINE_ID : showCreatePolylineDialog,
252                  CIRCLE_ID   : showCreateCircleDialog,
253                  DEL_ALL_ID : deleteAll
254                 }
255
256 ########################################################