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