1 # Copyright (C) 2009-2012 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 # Author : Roman NIKOLAEV, Open CASCADE S.A.S. (roman.nikolaev@opencascade.com)
24 from SalomePyQt import *
25 import PYLIGHT_DataModel
26 from PyQt4.QtGui import *
27 from PyQt4.QtCore import *
34 # Get SALOME PyQt interface
36 # Get SALOME Swig interface
37 sg = libSALOME_Swig.SALOMEGUI_Swig()
39 ################################################
41 # Used to store actions, menus, toolbars, etc...
42 ################################################
49 sgPyQt.createAction(dict_actions["loadfile"], "Load text File", "Load text file")
51 sgPyQt.createAction(dict_actions["savefile"], "Save text File", "Save text file")
53 sgPyQt.createAction(dict_actions["insertLine"], "Insert Line", "Insert new text line")
54 # Insert new line action
55 sgPyQt.createAction(dict_actions["insertLine"], "Insert Line", "Insert new line")
56 # Edit selected line action
57 sgPyQt.createAction(dict_actions["editLine"], "Edit Line", "Edit selected line")
58 # Remove selected line action
59 sgPyQt.createAction(dict_actions["removeLine"], "Remove Lines", "Remove selected lines")
60 # Clear all paragraphs
61 sgPyQt.createAction(dict_actions["clearAll"], "Clear All", "Clear all paragraphs")
63 sgPyQt.createAction(dict_actions["displayLine"], "Display Line", "Display selected line")
65 sgPyQt.createAction(dict_actions["eraseLine"], "Erase Line", "Erase selected line")
67 separator = sgPyQt.createSeparator()
70 menuFile = sgPyQt.createMenu( "File", -1, -1 )
71 # Add actions in the menu 'File'
72 sgPyQt.createMenu( separator, menuFile, -1, 10)
73 sgPyQt.createMenu( dict_actions["loadfile"], menuFile, 10 );
74 sgPyQt.createMenu( dict_actions["savefile"], menuFile, 10 );
75 sgPyQt.createMenu( separator, menuFile, -1, 10)
76 # Create 'PyLight' menu
77 menuPyLight = sgPyQt.createMenu( "PyLight", -1, -1, 50)
78 # Add actions in the menu 'PyLight'
79 sgPyQt.createMenu( dict_actions["insertLine"], menuPyLight, 10 );
80 sgPyQt.createMenu( dict_actions["editLine"], menuPyLight, 10 );
81 sgPyQt.createMenu( dict_actions["removeLine"], menuPyLight, 10 );
82 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
83 sgPyQt.createMenu( dict_actions["clearAll"], menuPyLight, 10 );
84 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
85 sgPyQt.createMenu( dict_actions["displayLine"], menuPyLight, 10 );
86 sgPyQt.createMenu( dict_actions["eraseLine"], menuPyLight, 10 );
89 self.DM = PYLIGHT_DataModel.PYLIGHT_DataModel()
91 pass # def __init__( self )
93 pass # class GUIcontext
95 ################################################
96 # Global variables and functions
97 ################################################
100 # get active study ID
103 return sgPyQt.getStudyId()
113 if __verbose__ is None:
115 __verbose__ = int( os.getenv( 'SALOME_VERBOSE', 0 ) )
122 # study-to-context map
123 __study2context__ = {}
125 __current_context__ = None
128 # get current GUI context
131 global __current_context__
132 return __current_context__
135 # set and return current GUI context
136 # study ID is passed as parameter
138 def _setContext( studyID ):
139 global __study2context__, __current_context__
140 if not __study2context__.has_key(studyID):
141 __study2context__[studyID] = GUIcontext()
143 __current_context__ = __study2context__[studyID]
144 return __current_context__
146 ################################################
148 # Create actions and menus
150 if verbose(): print "PYLIGHTGUI::initialize()"
153 # called when module is activated
154 # returns True if activating is successfull and False otherwise
156 if verbose() : print "PYLIGHTGUI.activate() : study : %d" % _getStudyId()
157 ctx = _setContext( _getStudyId() )
160 # called when module is deactivated
162 if verbose() : print "PYLIGHTGUI.deactivate() : study : %d" % _getStudyId()
165 # called when active study is changed
166 # active study ID is passed as parameter
167 def activeStudyChanged( studyID ):
168 if verbose() : print "PYLIGHTGUI.activeStudyChanged(): study : %d" % studyID
169 ctx = _setContext( _getStudyId() )
173 def OnGUIEvent(commandID):
174 if verbose() : print "PYLIGHTGUI::OnGUIEvent : commandID = %d" % commandID
175 if dict_command.has_key( commandID ):
177 dict_command[commandID]()
179 traceback.print_exc()
181 if verbose() : print "The command is not implemented: %d" % commandID
184 # Customize popup menu
185 def createPopupMenu(popup, context):
186 if verbose() : print "PYLIGHTGUI.createPopupMenu(): context = %s" % context
188 if context != 'ObjectBrowser':
191 studyId = _getStudyId()
192 ctx = _setContext( studyId )
193 selcount = sg.SelectedCount()
195 entry = sg.getSelected( 0 )
196 obj = ctx.DM.getObject(entry)
198 if obj.getText() != "\n":
200 popup.addAction(sgPyQt.action(dict_actions["editLine"]))
201 popup.addAction(sgPyQt.action(dict_actions["removeLine"]))
203 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
204 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
205 popup.addAction(sgPyQt.action(dict_actions["eraseLine"]))
208 # Paragraph is selected
209 popup.addAction(sgPyQt.action(dict_actions["insertLine"]))
210 popup.addAction(sgPyQt.action(dict_actions["clearParagraph"]))
216 pass # if selcount == 1
219 # For saving data in the study
220 def saveFiles(prefix):
221 if verbose(): print "PYLIGHTGUI::saveFile()"
222 ctx = _setContext( _getStudyId() )
223 postfix = "PYLIGHT.txt"
224 filename = prefix+postfix
225 ctx.DM.saveFile(QString(filename))
228 # For restore data from the study
229 def openFiles(filelist):
230 if verbose(): print "PYLIGHTGUI::openFile()"
231 ctx = _setContext( _getStudyId() )
232 filename = filelist[0]
233 filename.append(filelist[1])
234 ctx.DM.loadFile(filename)
237 # Loading a text file
239 ctx = _setContext( _getStudyId() )
240 aFilter = "Text files (*.txt)"
241 filename = QFileDialog.getOpenFileName(sgPyQt.getDesktop(), "Open text file", "", aFilter, "Choose a text file to open")
242 if filename.isEmpty():
245 if os.access(str(filename),os.R_OK):
246 ctx.DM.loadFile(filename)
248 QMessageBox.warning(sgPyQt.getDesktop(),
250 QString("Can not read file:\n%1").arg(filename))
252 sg.updateObjBrowser(True)
257 ctx = _setContext( _getStudyId() )
258 aFilter = "Text files (*.txt)"
259 filename = QFileDialog.getSaveFileName(sgPyQt.getDesktop(),"Save text file", "", aFilter, "Choose a text file to save")
260 if filename.contains(".txt") == 0:
261 filename.append(".txt")
264 # Get directory name and check access
265 if os.access(str(fn.left(fn.lastIndexOf("/"))), os.W_OK):
266 ctx.DM.saveFile(filename)
268 QMessageBox.warning(sgPyQt.getDesktop(),
270 QString("Can not save file:\n%1").arg(filename))
276 Insert new line in the selected paragraph.
278 ctx = _setContext( _getStudyId() )
280 res = QInputDialog.getText(sgPyQt.getDesktop(),
284 if not res[1]: ### user click cancel button
288 # Nb selected objects
289 selcount = sg.SelectedCount()
290 # Nb object in the Data Model
291 paragrCount = len(ctx.DM.getParagraphs())
293 # Create first paragraph
295 ctx.DM.createObject()
296 # If line not empty create first line
298 ctx.DM.createObject(text,ctx.DM.getParagraphs()[0])
299 sg.updateObjBrowser(True)
303 ctx.DM.createObject()
304 sg.updateObjBrowser(True)
308 QMessageBox.warning(sgPyQt.getDesktop(),
310 'Please, select paragraph!')
313 entry = sg.getSelected( 0 )
314 obj = ctx.DM.getObject(entry)
317 if(obj.getText() == "\n"):
318 ctx.DM.createObject(text,entry)
319 sg.updateObjBrowser(True);
322 QMessageBox.warning(sgPyQt.getDesktop(),
324 'Please, select paragraph!')
326 QMessageBox.warning(sgPyQt.getDesktop(),
328 'Please, select only one paragraph!')
334 ctx = _setContext( _getStudyId() )
335 if sg.SelectedCount() == 1:
336 entry = sg.getSelected( 0 )
337 obj = ctx.DM.getObject(entry)
338 if(obj.getText() != "\n"):
340 res = QInputDialog.getText(sgPyQt.getDesktop(),
344 PYLIGHT_DataModel.processText(obj.getText()))
345 if not res[1]: ### user click cancel button
351 QMessageBox.information(sgPyQt.getDesktop(),
353 'Please, select line!')
355 QMessageBox.information(sgPyQt.getDesktop(),
357 'Please, select one line!')
358 sg.updateObjBrowser(True);
361 # Remove selected lines
363 ctx = _setContext( _getStudyId() )
364 selcount = sg.SelectedCount()
368 entry = sg.getSelected( selcount - 1)
369 #Check what only lines selected
370 if ctx.DM.getObject(entry).getText() == "\n":
374 selcount = selcount-1
379 renderer=libSalomePy.getRenderer()
382 if actor is not None:
383 renderer.RemoveActor(actor)
386 ctx.DM.removeObjects(lines)
387 sg.updateObjBrowser(True)
391 # Remove all lines from all paragraphs
393 ctx = _setContext( _getStudyId() )
394 paragraphs = ctx.DM.getParagraphs()
395 for paragr in paragraphs:
396 lines = sgPyQt.getChildren(paragr)
397 ctx.DM.removeObjects(lines)
398 renderer=libSalomePy.getRenderer()
401 if actor is not None:
402 renderer.RemoveActor(actor)
405 sg.updateObjBrowser(True)
408 # Display the selected line
410 ctx = _setContext( _getStudyId() )
411 if sg.SelectedCount() != 1:
413 entry = sg.getSelected(0)
414 text = ctx.DM.getObject(entry).getText()
417 renderer=libSalomePy.getRenderer()
418 actor = getActor(entry)
420 actor = vtk.vtkTextActor()
421 dict_actors[entry] = actor
423 center = renderer.GetCenter()
424 actor.SetInput(str(text))
425 actor.SetPosition(center[0],center[1])
426 txtPr = vtk.vtkTextProperty()
427 txtPr.SetFontSize(30)
428 actor.SetTextProperty(txtPr)
429 for act in dict_actors.values():
430 renderer.RemoveActor(act)
431 renderer.AddActor(actor)
434 # Clear remove all lines under selected paragraph
435 def clearParagraph():
436 ctx = _setContext( _getStudyId() )
437 lines = sgPyQt.getChildren(sg.getSelected(0))
438 ctx.DM.removeObjects(lines)
439 sg.updateObjBrowser(True)
442 # Erase the selected line
444 ctx = _setContext( _getStudyId() )
445 if sg.SelectedCount() != 1:
447 entry = sg.getSelected(0)
448 text = ctx.DM.getObject(entry).getText()
451 renderer=libSalomePy.getRenderer()
452 actor = getActor(entry)
453 if actor is not None:
454 renderer.RemoveActor(actor)
458 # Return vtkActor by entry
460 if isinstance(entry,QString):
461 entry = entry.toLatin1().data()
462 if dict_actors.has_key(entry):
463 return dict_actors[entry]
476 973 : clearParagraph,
489 "clearParagraph": 973,