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
43 __study_data_map__ = {}
45 ################################################
48 # get SALOME PyQt interface
50 sgPyQt = SalomePyQt.SalomePyQt()
52 # get SALOME Swig interface
54 sg = libSALOME_Swig.SALOMEGUI_Swig()
56 ################################################
57 # Global intializations
60 orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
62 # create naming service instance
63 naming_service = SALOME_NamingServicePy_i( orb )
65 # create life cycle CORBA instance
66 lcc = LifeCycleCORBA( orb )
69 obj = naming_service.Resolve( '/myStudyManager' )
70 studyManager = obj._narrow( SALOMEDS.StudyManager )
72 ################################################
79 return qApp.translate( "ATOMGENGUI", s )
81 def processException( e ):
85 print "Exception has been caught:", e
88 def warning( message, title = None ):
90 Show Warning message box
92 if not title: title = tr( "WARNING" )
93 QMessageBox.warning( sgPyQt.getDesktop(), title, message )
96 # --- get ATOMGEN engine ---
104 engine = lcc.FindOrLoadComponent( "FactoryServerPy", __MODULE_NAME__ )
107 # --- get active study ---
112 studyId = sgPyQt.getStudyId()
113 study = studyManager.GetStudyByID( studyId )
118 ################################################
119 # Call back GUI methods
124 This method is called when GUI module is being created
126 Creates menus, toolbars and performs other internal
129 print "ATOMGENGUI::initialize"
130 global __study_data_map__
132 studyId = sgPyQt.getStudyId()
133 if not __study_data_map__.has_key( studyId ):
134 __study_data_map__[ studyId ] = {}
135 # get selection object
136 selection = sgPyQt.getSelection()
137 selection.ClearIObjects()
138 __study_data_map__[ studyId ][ "selection" ] = selection
139 print "ATOMGENGUI::initialize done"
144 This method is called when GUI module is being created
146 Should return a map of the SALOME dockable windows id's
147 needed to be opened when module is activated.
149 print "ATOMGENGUI::windows"
151 winMap[ SalomePyQt.WT_ObjectBrowser ] = Qt.LeftDockWidgetArea
152 winMap[ SalomePyQt.WT_PyConsole ] = Qt.BottomDockWidgetArea
157 This method is called when GUI module is being created
159 Should return a list of the SALOME view window types
160 needed to be opened when module is activated.
162 print "ATOMGENGUI::views"
167 This method is called when GUI module is being activated.
169 print "ATOMGENGUI::activate"
172 global __study_data_map__
173 myStudy = _getStudy()
174 _getEngine().setCurrentStudy( myStudy )
175 studyId = myStudy._get_StudyId()
178 __study_data_map__[ studyId ][ "actions" ] = {}
179 a = sgPyQt.createAction( __CMD_IMPORT_XML__,
180 tr( "MEN_IMPORT_XML" ),
181 tr( "TOP_IMPORT_XML" ),
182 tr( "STB_IMPORT_XML" ) )
183 __study_data_map__[ studyId ][ "actions" ][ __CMD_IMPORT_XML__ ] = a
184 a = sgPyQt.createAction( __CMD_EXPORT_XML__,
185 tr( "MEN_EXPORT_XML" ),
186 tr( "TOP_EXPORT_XML" ),
187 tr( "STB_EXPORT_XML" ) )
188 __study_data_map__[ studyId ][ "actions" ][ __CMD_EXPORT_XML__ ] = a
191 fileMnu = sgPyQt.createMenu( QApplication.translate( "ATOMGENGUI", "MEN_FILE" ), -1, -1 )
192 sgPyQt.createMenu( sgPyQt.createSeparator(), fileMnu, -1, 20 )
193 sgPyQt.createMenu( __CMD_IMPORT_XML__, fileMnu, 20 )
194 sgPyQt.createMenu( __CMD_EXPORT_XML__, fileMnu, 20 )
195 sgPyQt.createMenu( sgPyQt.createSeparator(), fileMnu, -1, 20 )
198 selection = __study_data_map__[ studyId ][ "selection" ]
199 selection.ClearIObjects()
200 QObject.connect( selection, SIGNAL( "currentSelectionChanged()" ), selectionChanged )
209 This method is called when GUI module is being deactivated.
211 print "ATOMGENGUI::deactivate"
214 studyId = myStudy._get_StudyId()
215 selection = __study_data_map__[ studyId ][ "selection" ]
216 selection.ClearIObjects()
217 QObject.disconnect( selection, SIGNAL( "currentSelectionChanged()" ), selectionChanged )
225 def activeStudyChanged( studyId ):
227 This method is called when active study is chaghed
228 (user switches between studies desktops).
229 <studyId> is an id of study being activated.
231 print "ATOMGENGUI::activeStudyChanged: study Id =", studyId
233 if myStudy and myStudy._get_StudyId() == studyId:
241 def createPopupMenu( popup, context ):
243 This method is called when popup menu is requested
245 Should analyze the selection and fill in the popup menu
246 with the corresponding actions
248 print "ATOMGENGUI::createPopupMenu: popup =", popup, "; context =", context
249 selected = selectedItems()
251 for entry in selected:
252 sobject = myStudy.FindObjectID( entry )
253 if sobject and sobject.GetObject() and sobject.GetObject()._narrow( ATOMGEN_ORB.Molecule ):
257 a = sgPyQt.action( __CMD_RUN_ALGO__ )
258 if isOk and a and context == "ObjectBrowser":
262 def OnGUIEvent( commandId ):
264 This method is called when user activates some GUI action
265 <commandId> is an ID of the GUI action.
267 print "ATOMGENGUI::OnGUIEvent: commandId =", commandId
268 if dict_command.has_key( commandId ):
270 dict_command[ commandId ]()
272 processException( e )
274 print "ATOMGENGUI::OnGUIEvent: Action is not implemented: ", commandId
277 ################################################
278 # GUI actions implementation
282 class RunDlg(QDialog, ui_rundlg.Ui_RunDlg):
284 Run Algo simple dialog box
290 QDialog.__init__(self, sgPyQt.getDesktop())
294 self.selectionChanged()
297 def onCheckAll(self):
299 Called when user switches <Process all> check box
301 self.acLab.setEnabled( not self.allCheck.isChecked() )
302 self.acName.setEnabled( not self.allCheck.isChecked() )
304 selection = __study_data_map__[ myStudy._get_StudyId() ][ "selection" ]
305 if not self.allCheck.isChecked():
306 QObject.connect( selection, SIGNAL( "currentSelectionChanged()" ), self.selectionChanged )
308 QObject.disconnect( selection, SIGNAL( "currentSelectionChanged()" ), self.selectionChanged )
311 def selectionChanged(self):
313 Called when selection is changed
316 selected = selectedItems()
317 for entry in selected:
318 sobject = myStudy.FindObjectID(entry)
320 obj = sobject.GetObject()
321 if obj and obj._narrow( ATOMGEN_ORB.Molecule ):
322 self.selected.append( obj._narrow( ATOMGEN_ORB.Molecule ) )
323 if len( self.selected ) == 1:
324 self.acName.setText( self.selected[0].getName() )
325 elif len( self.selected ) > 1:
326 self.acName.setText(" %d objects selected"%len( self.selected ) )
328 self.acName.setText( "" )
335 data = [] # all data to be processed
336 if not self.allCheck.isChecked():
339 component = myStudy.FindComponent( "ATOMGEN" )
340 if not component: return
341 iter = myStudy.NewChildIterator( component )
343 sobject = iter.Value()
344 if sobject and sobject.GetObject() and sobject.GetObject()._narrow ( ATOMGEN_ORB.Molecule ):
345 data.append( sobject.GetObject()._narrow( ATOMGEN_ORB.Molecule ) )
348 if not len( data ): return
349 if not len(_getEngine().processData( data )):
350 warning( "ALGO_ERROR" )
352 sgPyQt.updateObjBrowser()
368 Gets list of entries of selected objects
370 nbSel = sg.SelectedCount()
372 for i in range(nbSel):
373 selected.append(sg.getSelected(i))
376 def selectionChanged():
378 Global selection changed slot
380 selected = selectedItems()
381 print "--> Selected objects: %d"%len(selected)
386 Import XML file action slot
388 print "--> onImportXml() is started"
389 filters = QStringList()
390 filters.append( tr( "XML_FILES" ) )
391 fileName = sgPyQt.getFileName( sgPyQt.getDesktop(),
396 if not fileName.isEmpty():
397 if not _getEngine().importXmlFile( str( fileName ) ):
398 warning( "IMPORT_ERROR" )
400 sgPyQt.updateObjBrowser()
401 print "--> onImportXml() is finished"
406 Export XML file action slot
408 print "--> onExportXml() is started"
409 filters = QStringList()
410 filters.append( tr( "XML_FILES" ) )
411 fileName = sgPyQt.getFileName( sgPyQt.getDesktop(),
416 if not fileName.isEmpty():
417 if not _getEngine().exportXmlFile( str( fileName ) ):
418 warning( "EXPORT_ERROR" )
419 print "--> onExportXml() is finished"
423 print "--> onRunAlgo() is started !!!"
428 myRunDlg.activateWindow()
430 print "--> onRunAlgol() is finished"
433 ################################################
434 # action-to-function map
437 __CMD_IMPORT_XML__ : onImportXml,
438 __CMD_EXPORT_XML__ : onExportXml,
439 __CMD_RUN_ALGO__ : onRunAlgo,