1 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 from PyQt4.QtGui import *
21 from PyQt4.QtCore import *
23 from omniORB import CORBA
24 from SALOME_NamingServicePy import *
25 from LifeCycleCORBA import *
27 import SALOMEDS_Attributes_idl
30 ################################################
35 __MODULE_NAME__ = "ATOMGEN"
38 __CMD_IMPORT_XML__ = 4000
39 __CMD_EXPORT_XML__ = 4001
40 __CMD_RUN_ALGO__ = 4002
41 __CMD_RUN_ALGO1__ = 4010
44 __study_data_map__ = {}
46 ################################################
49 # get SALOME PyQt interface
51 sgPyQt = SalomePyQt.SalomePyQt()
53 # get SALOME Swig interface
55 sg = libSALOME_Swig.SALOMEGUI_Swig()
57 ################################################
58 # Global intializations
61 orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
63 # create naming service instance
64 naming_service = SALOME_NamingServicePy_i( orb )
66 # create life cycle CORBA instance
67 lcc = LifeCycleCORBA( orb )
70 obj = naming_service.Resolve( '/myStudyManager' )
71 studyManager = obj._narrow( SALOMEDS.StudyManager )
73 ################################################
80 return qApp.translate( "ATOMGENGUI", s )
82 def processException( e ):
86 print "Exception has been caught:", e
89 def warning( message, title = None ):
91 Show Warning message box
93 if not title: title = tr( "WARNING" )
94 QMessageBox.warning( sgPyQt.getDesktop(), title, message )
97 # --- get ATOMGEN engine ---
105 engine = lcc.FindOrLoadComponent( "FactoryServerPy", __MODULE_NAME__ )
108 # --- get active study ---
113 studyId = sgPyQt.getStudyId()
114 study = studyManager.GetStudyByID( studyId )
119 ################################################
120 # Call back GUI methods
125 This method is called when GUI module is being created
127 Creates menus, toolbars and performs other internal
130 print "ATOMGENGUI::initialize"
131 global __study_data_map__
133 studyId = sgPyQt.getStudyId()
134 if not __study_data_map__.has_key( studyId ):
135 __study_data_map__[ studyId ] = {}
136 # get selection object
137 selection = sgPyQt.getSelection()
138 selection.ClearIObjects()
139 __study_data_map__[ studyId ][ "selection" ] = selection
140 print "ATOMGENGUI::initialize done"
145 This method is called when GUI module is being created
147 Should return a map of the SALOME dockable windows id's
148 needed to be opened when module is activated.
150 print "ATOMGENGUI::windows"
152 winMap[ SalomePyQt.WT_ObjectBrowser ] = Qt.LeftDockWidgetArea
153 winMap[ SalomePyQt.WT_PyConsole ] = Qt.BottomDockWidgetArea
158 This method is called when GUI module is being created
160 Should return a list of the SALOME view window types
161 needed to be opened when module is activated.
163 print "ATOMGENGUI::views"
168 This method is called when GUI module is being activated.
170 print "ATOMGENGUI::activate"
173 global __study_data_map__
174 myStudy = _getStudy()
175 _getEngine().setCurrentStudy( myStudy )
176 studyId = myStudy._get_StudyId()
179 __study_data_map__[ studyId ][ "actions" ] = {}
180 a = sgPyQt.createAction( __CMD_IMPORT_XML__,
181 tr( "MEN_IMPORT_XML" ),
182 tr( "TOP_IMPORT_XML" ),
183 tr( "STB_IMPORT_XML" ) )
184 __study_data_map__[ studyId ][ "actions" ][ __CMD_IMPORT_XML__ ] = a
185 a = sgPyQt.createAction( __CMD_EXPORT_XML__,
186 tr( "MEN_EXPORT_XML" ),
187 tr( "TOP_EXPORT_XML" ),
188 tr( "STB_EXPORT_XML" ) )
189 __study_data_map__[ studyId ][ "actions" ][ __CMD_EXPORT_XML__ ] = a
192 fileMnu = sgPyQt.createMenu( QApplication.translate( "ATOMGENGUI", "MEN_FILE" ), -1, -1 )
193 sgPyQt.createMenu( sgPyQt.createSeparator(), fileMnu, -1, 20 )
194 sgPyQt.createMenu( __CMD_IMPORT_XML__, fileMnu, 20 )
195 sgPyQt.createMenu( __CMD_EXPORT_XML__, fileMnu, 20 )
196 sgPyQt.createMenu( sgPyQt.createSeparator(), fileMnu, -1, 20 )
199 selection = __study_data_map__[ studyId ][ "selection" ]
200 selection.ClearIObjects()
201 QObject.connect( selection, SIGNAL( "currentSelectionChanged()" ), selectionChanged )
210 This method is called when GUI module is being deactivated.
212 print "ATOMGENGUI::deactivate"
215 studyId = myStudy._get_StudyId()
216 selection = __study_data_map__[ studyId ][ "selection" ]
217 selection.ClearIObjects()
218 QObject.disconnect( selection, SIGNAL( "currentSelectionChanged()" ), selectionChanged )
226 def activeStudyChanged( studyId ):
228 This method is called when active study is chaghed
229 (user switches between studies desktops).
230 <studyId> is an id of study being activated.
232 print "ATOMGENGUI::activeStudyChanged: study Id =", studyId
234 if myStudy and myStudy._get_StudyId() == studyId:
242 def createPopupMenu( popup, context ):
244 This method is called when popup menu is requested
246 Should analyze the selection and fill in the popup menu
247 with the corresponding actions
249 print "ATOMGENGUI::createPopupMenu: popup =", popup, "; context =", context
250 selected = selectedItems()
252 for entry in selected:
253 sobject = myStudy.FindObjectID( entry )
254 if sobject and sobject.GetObject() and sobject.GetObject()._narrow( ATOMGEN_ORB.Molecule ):
258 a = sgPyQt.action( __CMD_RUN_ALGO__ )
259 if isOk and a and context == "ObjectBrowser":
264 def OnGUIEvent( commandId ):
266 This method is called when user activates some GUI action
267 <commandId> is an ID of the GUI action.
269 print "ATOMGENGUI::OnGUIEvent: commandId =", commandId
270 if dict_command.has_key( commandId ):
272 dict_command[ commandId ]()
274 processException( e )
276 print "ATOMGENGUI::OnGUIEvent: Action is not implemented: ", commandId
279 ################################################
280 # GUI actions implementation
282 from rundlg_ui import Ui_RunDlg
284 class RunDlg(QDialog, Ui_RunDlg):
286 Run Algo simple dialog box
292 QDialog.__init__(self, sgPyQt.getDesktop())
296 self.selectionChanged()
299 def onCheckAll(self):
301 Called when user switches <Process all> check box
303 self.acLab.setEnabled( not self.allCheck.isChecked() )
304 self.acName.setEnabled( not self.allCheck.isChecked() )
306 selection = __study_data_map__[ myStudy._get_StudyId() ][ "selection" ]
307 if not self.allCheck.isChecked():
308 QObject.connect( selection, SIGNAL( "currentSelectionChanged()" ), self.selectionChanged )
310 QObject.disconnect( selection, SIGNAL( "currentSelectionChanged()" ), self.selectionChanged )
313 def selectionChanged(self):
315 Called when selection is changed
318 selected = selectedItems()
319 for entry in selected:
320 sobject = myStudy.FindObjectID(entry)
322 obj = sobject.GetObject()
323 if obj and obj._narrow( ATOMGEN_ORB.Molecule ):
324 self.selected.append( obj._narrow( ATOMGEN_ORB.Molecule ) )
325 if len( self.selected ) == 1:
326 self.acName.setText( self.selected[0].getName() )
327 elif len( self.selected ) > 1:
328 self.acName.setText(" %d objects selected"%len( self.selected ) )
330 self.acName.setText( "" )
337 data = [] # all data to be processed
338 if not self.allCheck.isChecked():
341 component = myStudy.FindComponent( "ATOMGEN" )
342 if not component: return
343 iter = myStudy.NewChildIterator( component )
345 sobject = iter.Value()
346 if sobject and sobject.GetObject() and sobject.GetObject()._narrow ( ATOMGEN_ORB.Molecule ):
347 data.append( sobject.GetObject()._narrow( ATOMGEN_ORB.Molecule ) )
350 if not len( data ): return
351 if not len(_getEngine().processData( data )):
352 warning( "ALGO_ERROR" )
354 sgPyQt.updateObjBrowser()
370 Gets list of entries of selected objects
372 nbSel = sg.SelectedCount()
374 for i in range(nbSel):
375 selected.append(sg.getSelected(i))
378 def selectionChanged():
380 Global selection changed slot
382 selected = selectedItems()
383 print "--> Selected objects: %d"%len(selected)
388 Import XML file action slot
390 print "--> onImportXml() is started"
391 filters = QStringList()
392 filters.append( tr( "XML_FILES" ) )
393 fileName = sgPyQt.getFileName( sgPyQt.getDesktop(),
398 if not fileName.isEmpty():
399 if not _getEngine().importXmlFile( str( fileName ) ):
400 warning( "IMPORT_ERROR" )
402 sgPyQt.updateObjBrowser()
403 print "--> onImportXml() is finished"
408 Export XML file action slot
410 print "--> onExportXml() is started"
411 filters = QStringList()
412 filters.append( tr( "XML_FILES" ) )
413 fileName = sgPyQt.getFileName( sgPyQt.getDesktop(),
418 if not fileName.isEmpty():
419 if not _getEngine().exportXmlFile( str( fileName ) ):
420 warning( "EXPORT_ERROR" )
421 print "--> onExportXml() is finished"
425 print "--> onRunAlgo() is started !!!"
430 myRunDlg.activateWindow()
432 print "--> onRunAlgol() is finished"
435 ################################################
436 # action-to-function map
439 __CMD_IMPORT_XML__ : onImportXml,
440 __CMD_EXPORT_XML__ : onExportXml,
441 __CMD_RUN_ALGO__ : onRunAlgo,
442 __CMD_RUN_ALGO1__ : onRunAlgo,