1 # Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 # See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
22 from PyQt4.QtGui import *
23 from PyQt4.QtCore import *
25 from omniORB import CORBA
26 from SALOME_NamingServicePy import *
27 from LifeCycleCORBA import *
29 import SALOMEDS_Attributes_idl
32 ################################################
37 __MODULE_NAME__ = "ATOMGEN"
40 __CMD_IMPORT_XML__ = 4000
41 __CMD_EXPORT_XML__ = 4001
42 __CMD_RUN_ALGO__ = 4002
45 __study_data_map__ = {}
47 ################################################
50 # get SALOME PyQt interface
52 sgPyQt = SalomePyQt.SalomePyQt()
54 # get SALOME Swig interface
56 sg = libSALOME_Swig.SALOMEGUI_Swig()
58 ################################################
59 # Global intializations
62 orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
64 # create naming service instance
65 naming_service = SALOME_NamingServicePy_i( orb )
67 # create life cycle CORBA instance
68 lcc = LifeCycleCORBA( orb )
71 obj = naming_service.Resolve( '/myStudyManager' )
72 studyManager = obj._narrow( SALOMEDS.StudyManager )
74 ################################################
81 return qApp.translate( "ATOMGENGUI", s )
83 def processException( e ):
87 print "Exception has been caught:", e
90 def warning( message, title = None ):
92 Show Warning message box
94 if not title: title = tr( "WARNING" )
95 QMessageBox.warning( sgPyQt.getDesktop(), title, message )
98 # --- get ATOMGEN engine ---
106 engine = lcc.FindOrLoadComponent( "FactoryServerPy", __MODULE_NAME__ )
109 # --- get active study ---
114 studyId = sgPyQt.getStudyId()
115 study = studyManager.GetStudyByID( studyId )
120 ################################################
121 # Call back GUI methods
126 This method is called when GUI module is being created
128 Creates menus, toolbars and performs other internal
131 print "ATOMGENGUI::initialize"
132 global __study_data_map__
134 studyId = sgPyQt.getStudyId()
135 if not __study_data_map__.has_key( studyId ):
136 __study_data_map__[ studyId ] = {}
137 # get selection object
138 selection = sgPyQt.getSelection()
139 selection.ClearIObjects()
140 __study_data_map__[ studyId ][ "selection" ] = selection
141 print "ATOMGENGUI::initialize done"
146 This method is called when GUI module is being created
148 Should return a map of the SALOME dockable windows id's
149 needed to be opened when module is activated.
151 print "ATOMGENGUI::windows"
153 winMap[ SalomePyQt.WT_ObjectBrowser ] = Qt.LeftDockWidgetArea
154 winMap[ SalomePyQt.WT_PyConsole ] = Qt.BottomDockWidgetArea
159 This method is called when GUI module is being created
161 Should return a list of the SALOME view window types
162 needed to be opened when module is activated.
164 print "ATOMGENGUI::views"
169 This method is called when GUI module is being activated.
171 print "ATOMGENGUI::activate"
174 global __study_data_map__
175 myStudy = _getStudy()
176 _getEngine().setCurrentStudy( myStudy )
177 studyId = myStudy._get_StudyId()
180 __study_data_map__[ studyId ][ "actions" ] = {}
181 a = sgPyQt.createAction( __CMD_IMPORT_XML__,
182 tr( "MEN_IMPORT_XML" ),
183 tr( "TOP_IMPORT_XML" ),
184 tr( "STB_IMPORT_XML" ) )
185 __study_data_map__[ studyId ][ "actions" ][ __CMD_IMPORT_XML__ ] = a
186 a = sgPyQt.createAction( __CMD_EXPORT_XML__,
187 tr( "MEN_EXPORT_XML" ),
188 tr( "TOP_EXPORT_XML" ),
189 tr( "STB_EXPORT_XML" ) )
190 __study_data_map__[ studyId ][ "actions" ][ __CMD_EXPORT_XML__ ] = a
193 fileMnu = sgPyQt.createMenu( QApplication.translate( "ATOMGENGUI", "MEN_FILE" ), -1, -1 )
194 sgPyQt.createMenu( sgPyQt.createSeparator(), fileMnu, -1, 20 )
195 sgPyQt.createMenu( __CMD_IMPORT_XML__, fileMnu, 20 )
196 sgPyQt.createMenu( __CMD_EXPORT_XML__, fileMnu, 20 )
197 sgPyQt.createMenu( sgPyQt.createSeparator(), fileMnu, -1, 20 )
200 selection = __study_data_map__[ studyId ][ "selection" ]
201 selection.ClearIObjects()
202 QObject.connect( selection, SIGNAL( "currentSelectionChanged()" ), selectionChanged )
211 This method is called when GUI module is being deactivated.
213 print "ATOMGENGUI::deactivate"
216 studyId = myStudy._get_StudyId()
217 selection = __study_data_map__[ studyId ][ "selection" ]
218 selection.ClearIObjects()
219 QObject.disconnect( selection, SIGNAL( "currentSelectionChanged()" ), selectionChanged )
227 def activeStudyChanged( studyId ):
229 This method is called when active study is chaghed
230 (user switches between studies desktops).
231 <studyId> is an id of study being activated.
233 print "ATOMGENGUI::activeStudyChanged: study Id =", studyId
235 if myStudy and myStudy._get_StudyId() == studyId:
243 def createPopupMenu( popup, context ):
245 This method is called when popup menu is requested
247 Should analyze the selection and fill in the popup menu
248 with the corresponding actions
250 print "ATOMGENGUI::createPopupMenu: popup =", popup, "; context =", context
251 selected = selectedItems()
253 for entry in selected:
254 sobject = myStudy.FindObjectID( entry )
255 if sobject and sobject.GetObject() and sobject.GetObject()._narrow( ATOMGEN_ORB.Molecule ):
259 a = sgPyQt.action( __CMD_RUN_ALGO__ )
260 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
284 class RunDlg(QDialog, ui_rundlg.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 _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,