1 # Copyright (C) 2009-2016 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, or (at your option) any later version.
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 qtsalome import *
33 # Get SALOME PyQt interface
35 # Get SALOME Swig interface
36 sg = libSALOME_Swig.SALOMEGUI_Swig()
38 ################################################
40 # Used to store actions, menus, toolbars, etc...
41 ################################################
48 sgPyQt.createAction(dict_actions["loadfile"], "Load text File", "Load text file")
50 sgPyQt.createAction(dict_actions["savefile"], "Save text File", "Save text file")
52 sgPyQt.createAction(dict_actions["insertLine"], "Insert Line", "Insert new text line")
53 # Insert new line action
54 sgPyQt.createAction(dict_actions["insertLine"], "Insert Line", "Insert new line")
55 # Edit selected line action
56 sgPyQt.createAction(dict_actions["editLine"], "Edit Line", "Edit selected line")
57 # Remove selected line action
58 sgPyQt.createAction(dict_actions["removeLine"], "Remove Lines", "Remove selected lines")
60 sgPyQt.createAction(dict_actions["clearParagraph"], "Clear Paragraph", "Clear selected paragraph")
61 # Clear all paragraphs
62 sgPyQt.createAction(dict_actions["clearAll"], "Clear All", "Clear all paragraphs")
64 sgPyQt.createAction(dict_actions["displayLine"], "Display Line", "Display selected line")
66 sgPyQt.createAction(dict_actions["eraseLine"], "Erase Line", "Erase selected line")
68 separator = sgPyQt.createSeparator()
71 menuFile = sgPyQt.createMenu( "File", -1, -1 )
72 # Add actions in the menu 'File'
73 sgPyQt.createMenu( separator, menuFile, -1, 10)
74 sgPyQt.createMenu( dict_actions["loadfile"], menuFile, 10 );
75 sgPyQt.createMenu( dict_actions["savefile"], menuFile, 10 );
76 sgPyQt.createMenu( separator, menuFile, -1, 10)
77 # Create 'PyLight' menu
78 menuPyLight = sgPyQt.createMenu( "PyLight", -1, -1, 50)
79 # Add actions in the menu 'PyLight'
80 sgPyQt.createMenu( dict_actions["insertLine"], menuPyLight, 10 );
81 sgPyQt.createMenu( dict_actions["editLine"], menuPyLight, 10 );
82 sgPyQt.createMenu( dict_actions["removeLine"], menuPyLight, 10 );
83 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
84 sgPyQt.createMenu( dict_actions["clearAll"], menuPyLight, 10 );
85 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
86 sgPyQt.createMenu( dict_actions["displayLine"], menuPyLight, 10 );
87 sgPyQt.createMenu( dict_actions["eraseLine"], menuPyLight, 10 );
90 self.DM = PYLIGHT_DataModel.PYLIGHT_DataModel()
92 pass # def __init__( self )
94 pass # class GUIcontext
96 ################################################
97 # Global variables and functions
98 ################################################
101 # get active study ID
104 return sgPyQt.getStudyId()
114 if __verbose__ is None:
116 __verbose__ = int( os.getenv( 'SALOME_VERBOSE', 0 ) )
123 # study-to-context map
124 __study2context__ = {}
126 __current_context__ = None
129 # get current GUI context
132 global __current_context__
133 return __current_context__
136 # set and return current GUI context
137 # study ID is passed as parameter
139 def _setContext( studyID ):
140 global __study2context__, __current_context__
141 if not __study2context__.has_key(studyID):
142 __study2context__[studyID] = GUIcontext()
144 __current_context__ = __study2context__[studyID]
145 return __current_context__
147 ################################################
149 # Create actions and menus
151 if verbose(): print "PYLIGHTGUI::initialize()"
154 # called when module is activated
155 # returns True if activating is successfull and False otherwise
157 if verbose() : print "PYLIGHTGUI.activate() : study : %d" % _getStudyId()
158 ctx = _setContext( _getStudyId() )
161 # called when module is deactivated
163 if verbose() : print "PYLIGHTGUI.deactivate() : study : %d" % _getStudyId()
166 # called when active study is changed
167 # active study ID is passed as parameter
168 def activeStudyChanged( studyID ):
169 if verbose() : print "PYLIGHTGUI.activeStudyChanged(): study : %d" % studyID
170 ctx = _setContext( _getStudyId() )
174 def OnGUIEvent(commandID):
175 if verbose() : print "PYLIGHTGUI::OnGUIEvent : commandID = %d" % commandID
176 if dict_command.has_key( commandID ):
178 dict_command[commandID]()
180 traceback.print_exc()
182 if verbose() : print "The command is not implemented: %d" % commandID
185 # Customize popup menu
186 def createPopupMenu(popup, context):
187 if verbose() : print "PYLIGHTGUI.createPopupMenu(): context = %s" % context
189 if context != 'ObjectBrowser':
192 studyId = _getStudyId()
193 ctx = _setContext( studyId )
194 selcount = sg.SelectedCount()
196 entry = sg.getSelected( 0 )
197 obj = ctx.DM.getObject(entry)
199 if obj.getText() != "\n":
201 popup.addAction(sgPyQt.action(dict_actions["editLine"]))
202 popup.addAction(sgPyQt.action(dict_actions["removeLine"]))
204 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
205 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
206 popup.addAction(sgPyQt.action(dict_actions["eraseLine"]))
209 # Paragraph is selected
210 popup.addAction(sgPyQt.action(dict_actions["insertLine"]))
211 popup.addAction(sgPyQt.action(dict_actions["clearParagraph"]))
217 pass # if selcount == 1
220 # For saving data in the study
221 def saveFiles(prefix):
222 if verbose(): print "PYLIGHTGUI::saveFile()"
223 ctx = _setContext( _getStudyId() )
224 postfix = "PYLIGHT.txt"
225 filename = prefix+postfix
226 ctx.DM.saveFile(filename)
229 # For restore data from the study
230 def openFiles(filelist):
231 if verbose(): print "PYLIGHTGUI::openFile()"
232 ctx = _setContext( _getStudyId() )
233 filename = filelist[0]
234 filename.append(filelist[1])
235 ctx.DM.loadFile(filename)
238 # Loading a text file
240 ctx = _setContext( _getStudyId() )
241 aFilter = "Text files (*.txt)"
242 filename = QFileDialog.getOpenFileName(sgPyQt.getDesktop(), "Open text file", "", aFilter, "Choose a text file to open")
245 if isinstance(filename,tuple) and len(filename) >=2:
246 filename = filename[0]
248 filename = unicode(filename)
250 if len(filename) == 0:
253 if os.access(str(filename),os.R_OK):
254 ctx.DM.loadFile(filename)
256 QMessageBox.warning(sgPyQt.getDesktop(),
258 "Can not read file:\n%s"%(filename))
260 sg.updateObjBrowser(True)
265 ctx = _setContext( _getStudyId() )
266 aFilter = "Text files (*.txt)"
267 filename = QFileDialog.getSaveFileName(sgPyQt.getDesktop(),"Save text file", "", aFilter, "Choose a text file to save")
270 if isinstance(filename,tuple) and len(filename) >=2:
271 filename = filename[0]
273 filename = unicode(filename)
275 if filename.endswith(".txt") == 0:
280 # Get directory name and check access
281 if os.access(str(fn[:fn.rindex(os.path.sep)]), os.W_OK):
282 ctx.DM.saveFile(filename)
284 QMessageBox.warning(sgPyQt.getDesktop(),
286 "Can not save file:\n%s"%(filename))
292 Insert new line in the selected paragraph.
294 ctx = _setContext( _getStudyId() )
296 res = QInputDialog.getText(sgPyQt.getDesktop(),
300 if not res[1]: ### user click cancel button
305 # Nb selected objects
306 selcount = sg.SelectedCount()
307 # Nb object in the Data Model
308 paragrCount = len(ctx.DM.getParagraphs())
310 # Create first paragraph
312 ctx.DM.createObject()
313 # If line not empty create first line
315 ctx.DM.createObject(text,ctx.DM.getParagraphs()[0])
316 sg.updateObjBrowser(True)
320 ctx.DM.createObject()
321 sg.updateObjBrowser(True)
325 QMessageBox.warning(sgPyQt.getDesktop(),
327 'Please, select paragraph!')
330 entry = sg.getSelected( 0 )
331 obj = ctx.DM.getObject(entry)
334 if(obj.getText() == "\n"):
335 ctx.DM.createObject(text,entry)
336 sg.updateObjBrowser(True);
339 QMessageBox.warning(sgPyQt.getDesktop(),
341 'Please, select paragraph!')
343 QMessageBox.warning(sgPyQt.getDesktop(),
345 'Please, select only one paragraph!')
351 ctx = _setContext( _getStudyId() )
352 if sg.SelectedCount() == 1:
353 entry = sg.getSelected( 0 )
354 obj = ctx.DM.getObject(entry)
355 if(obj.getText() != "\n"):
357 res = QInputDialog.getText(sgPyQt.getDesktop(),
361 PYLIGHT_DataModel.processText(obj.getText()))
362 if not res[1]: ### user click cancel button
368 QMessageBox.information(sgPyQt.getDesktop(),
370 'Please, select line!')
372 QMessageBox.information(sgPyQt.getDesktop(),
374 'Please, select one line!')
375 sg.updateObjBrowser(True);
378 # Remove selected lines
380 ctx = _setContext( _getStudyId() )
381 selcount = sg.SelectedCount()
385 entry = sg.getSelected( selcount - 1)
386 #Check what only lines selected
387 if ctx.DM.getObject(entry).getText() == "\n":
391 selcount = selcount-1
396 renderer=libSalomePy.getRenderer()
399 if actor is not None:
400 renderer.RemoveActor(actor)
403 ctx.DM.removeObjects(lines)
404 sg.updateObjBrowser(True)
408 # Remove all lines from all paragraphs
410 ctx = _setContext( _getStudyId() )
411 paragraphs = ctx.DM.getParagraphs()
412 for paragr in paragraphs:
413 lines = sgPyQt.getChildren(paragr)
414 ctx.DM.removeObjects(lines)
415 renderer=libSalomePy.getRenderer()
418 if actor is not None:
419 renderer.RemoveActor(actor)
422 sg.updateObjBrowser(True)
425 # Display the selected line
427 ctx = _setContext( _getStudyId() )
428 if sg.SelectedCount() != 1:
430 entry = sg.getSelected(0)
431 text = ctx.DM.getObject(entry).getText()
434 renderer=libSalomePy.getRenderer()
435 actor = getActor(entry)
437 actor = vtk.vtkTextActor()
438 dict_actors[entry] = actor
440 center = renderer.GetCenter()
441 actor.SetInput(str(text))
442 actor.SetPosition(center[0],center[1])
443 txtPr = vtk.vtkTextProperty()
444 txtPr.SetFontSize(30)
445 actor.SetTextProperty(txtPr)
446 for act in dict_actors.values():
447 renderer.RemoveActor(act)
448 renderer.AddActor(actor)
451 # Clear remove all lines under selected paragraph
452 def clearParagraph():
453 ctx = _setContext( _getStudyId() )
454 lines = sgPyQt.getChildren(sg.getSelected(0))
455 ctx.DM.removeObjects(lines)
456 sg.updateObjBrowser(True)
459 # Erase the selected line
461 ctx = _setContext( _getStudyId() )
462 if sg.SelectedCount() != 1:
464 entry = sg.getSelected(0)
465 text = ctx.DM.getObject(entry).getText()
468 renderer=libSalomePy.getRenderer()
469 actor = getActor(entry)
470 if actor is not None:
471 renderer.RemoveActor(actor)
475 # Return vtkActor by entry
478 if dict_actors.has_key(entry):
479 return dict_actors[entry]
492 973 : clearParagraph,
505 "clearParagraph": 973,