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")
61 sgPyQt.createAction(dict_actions["clearParagraph"], "Clear Paragraph", "Clear selected paragraph")
62 # Clear all paragraphs
63 sgPyQt.createAction(dict_actions["clearAll"], "Clear All", "Clear all paragraphs")
65 sgPyQt.createAction(dict_actions["displayLine"], "Display Line", "Display selected line")
67 sgPyQt.createAction(dict_actions["eraseLine"], "Erase Line", "Erase selected line")
69 separator = sgPyQt.createSeparator()
72 menuFile = sgPyQt.createMenu( "File", -1, -1 )
73 # Add actions in the menu 'File'
74 sgPyQt.createMenu( separator, menuFile, -1, 10)
75 sgPyQt.createMenu( dict_actions["loadfile"], menuFile, 10 );
76 sgPyQt.createMenu( dict_actions["savefile"], menuFile, 10 );
77 sgPyQt.createMenu( separator, menuFile, -1, 10)
78 # Create 'PyLight' menu
79 menuPyLight = sgPyQt.createMenu( "PyLight", -1, -1, 50)
80 # Add actions in the menu 'PyLight'
81 sgPyQt.createMenu( dict_actions["insertLine"], menuPyLight, 10 );
82 sgPyQt.createMenu( dict_actions["editLine"], menuPyLight, 10 );
83 sgPyQt.createMenu( dict_actions["removeLine"], menuPyLight, 10 );
84 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
85 sgPyQt.createMenu( dict_actions["clearAll"], menuPyLight, 10 );
86 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
87 sgPyQt.createMenu( dict_actions["displayLine"], menuPyLight, 10 );
88 sgPyQt.createMenu( dict_actions["eraseLine"], menuPyLight, 10 );
91 self.DM = PYLIGHT_DataModel.PYLIGHT_DataModel()
93 pass # def __init__( self )
95 pass # class GUIcontext
97 ################################################
98 # Global variables and functions
99 ################################################
102 # get active study ID
105 return sgPyQt.getStudyId()
115 if __verbose__ is None:
117 __verbose__ = int( os.getenv( 'SALOME_VERBOSE', 0 ) )
124 # study-to-context map
125 __study2context__ = {}
127 __current_context__ = None
130 # get current GUI context
133 global __current_context__
134 return __current_context__
137 # set and return current GUI context
138 # study ID is passed as parameter
140 def _setContext( studyID ):
141 global __study2context__, __current_context__
142 if not __study2context__.has_key(studyID):
143 __study2context__[studyID] = GUIcontext()
145 __current_context__ = __study2context__[studyID]
146 return __current_context__
148 ################################################
150 # Create actions and menus
152 if verbose(): print "PYLIGHTGUI::initialize()"
155 # called when module is activated
156 # returns True if activating is successfull and False otherwise
158 if verbose() : print "PYLIGHTGUI.activate() : study : %d" % _getStudyId()
159 ctx = _setContext( _getStudyId() )
162 # called when module is deactivated
164 if verbose() : print "PYLIGHTGUI.deactivate() : study : %d" % _getStudyId()
167 # called when active study is changed
168 # active study ID is passed as parameter
169 def activeStudyChanged( studyID ):
170 if verbose() : print "PYLIGHTGUI.activeStudyChanged(): study : %d" % studyID
171 ctx = _setContext( _getStudyId() )
175 def OnGUIEvent(commandID):
176 if verbose() : print "PYLIGHTGUI::OnGUIEvent : commandID = %d" % commandID
177 if dict_command.has_key( commandID ):
179 dict_command[commandID]()
181 traceback.print_exc()
183 if verbose() : print "The command is not implemented: %d" % commandID
186 # Customize popup menu
187 def createPopupMenu(popup, context):
188 if verbose() : print "PYLIGHTGUI.createPopupMenu(): context = %s" % context
190 if context != 'ObjectBrowser':
193 studyId = _getStudyId()
194 ctx = _setContext( studyId )
195 selcount = sg.SelectedCount()
197 entry = sg.getSelected( 0 )
198 obj = ctx.DM.getObject(entry)
200 if obj.getText() != "\n":
202 popup.addAction(sgPyQt.action(dict_actions["editLine"]))
203 popup.addAction(sgPyQt.action(dict_actions["removeLine"]))
205 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
206 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
207 popup.addAction(sgPyQt.action(dict_actions["eraseLine"]))
210 # Paragraph is selected
211 popup.addAction(sgPyQt.action(dict_actions["insertLine"]))
212 popup.addAction(sgPyQt.action(dict_actions["clearParagraph"]))
218 pass # if selcount == 1
221 # For saving data in the study
222 def saveFiles(prefix):
223 if verbose(): print "PYLIGHTGUI::saveFile()"
224 ctx = _setContext( _getStudyId() )
225 postfix = "PYLIGHT.txt"
226 filename = prefix+postfix
227 ctx.DM.saveFile(QString(filename))
230 # For restore data from the study
231 def openFiles(filelist):
232 if verbose(): print "PYLIGHTGUI::openFile()"
233 ctx = _setContext( _getStudyId() )
234 filename = filelist[0]
235 filename.append(filelist[1])
236 ctx.DM.loadFile(filename)
239 # Loading a text file
241 ctx = _setContext( _getStudyId() )
242 aFilter = "Text files (*.txt)"
243 filename = QFileDialog.getOpenFileName(sgPyQt.getDesktop(), "Open text file", "", aFilter, "Choose a text file to open")
244 if filename.isEmpty():
247 if os.access(str(filename),os.R_OK):
248 ctx.DM.loadFile(filename)
250 QMessageBox.warning(sgPyQt.getDesktop(),
252 QString("Can not read file:\n%1").arg(filename))
254 sg.updateObjBrowser(True)
259 ctx = _setContext( _getStudyId() )
260 aFilter = "Text files (*.txt)"
261 filename = QFileDialog.getSaveFileName(sgPyQt.getDesktop(),"Save text file", "", aFilter, "Choose a text file to save")
262 if filename.contains(".txt") == 0:
263 filename.append(".txt")
266 # Get directory name and check access
267 if os.access(str(fn.left(fn.lastIndexOf("/"))), os.W_OK):
268 ctx.DM.saveFile(filename)
270 QMessageBox.warning(sgPyQt.getDesktop(),
272 QString("Can not save file:\n%1").arg(filename))
278 Insert new line in the selected paragraph.
280 ctx = _setContext( _getStudyId() )
282 res = QInputDialog.getText(sgPyQt.getDesktop(),
286 if not res[1]: ### user click cancel button
290 # Nb selected objects
291 selcount = sg.SelectedCount()
292 # Nb object in the Data Model
293 paragrCount = len(ctx.DM.getParagraphs())
295 # Create first paragraph
297 ctx.DM.createObject()
298 # If line not empty create first line
300 ctx.DM.createObject(text,ctx.DM.getParagraphs()[0])
301 sg.updateObjBrowser(True)
305 ctx.DM.createObject()
306 sg.updateObjBrowser(True)
310 QMessageBox.warning(sgPyQt.getDesktop(),
312 'Please, select paragraph!')
315 entry = sg.getSelected( 0 )
316 obj = ctx.DM.getObject(entry)
319 if(obj.getText() == "\n"):
320 ctx.DM.createObject(text,entry)
321 sg.updateObjBrowser(True);
324 QMessageBox.warning(sgPyQt.getDesktop(),
326 'Please, select paragraph!')
328 QMessageBox.warning(sgPyQt.getDesktop(),
330 'Please, select only one paragraph!')
336 ctx = _setContext( _getStudyId() )
337 if sg.SelectedCount() == 1:
338 entry = sg.getSelected( 0 )
339 obj = ctx.DM.getObject(entry)
340 if(obj.getText() != "\n"):
342 res = QInputDialog.getText(sgPyQt.getDesktop(),
346 PYLIGHT_DataModel.processText(obj.getText()))
347 if not res[1]: ### user click cancel button
353 QMessageBox.information(sgPyQt.getDesktop(),
355 'Please, select line!')
357 QMessageBox.information(sgPyQt.getDesktop(),
359 'Please, select one line!')
360 sg.updateObjBrowser(True);
363 # Remove selected lines
365 ctx = _setContext( _getStudyId() )
366 selcount = sg.SelectedCount()
370 entry = sg.getSelected( selcount - 1)
371 #Check what only lines selected
372 if ctx.DM.getObject(entry).getText() == "\n":
376 selcount = selcount-1
381 renderer=libSalomePy.getRenderer()
384 if actor is not None:
385 renderer.RemoveActor(actor)
388 ctx.DM.removeObjects(lines)
389 sg.updateObjBrowser(True)
393 # Remove all lines from all paragraphs
395 ctx = _setContext( _getStudyId() )
396 paragraphs = ctx.DM.getParagraphs()
397 for paragr in paragraphs:
398 lines = sgPyQt.getChildren(paragr)
399 ctx.DM.removeObjects(lines)
400 renderer=libSalomePy.getRenderer()
403 if actor is not None:
404 renderer.RemoveActor(actor)
407 sg.updateObjBrowser(True)
410 # Display the selected line
412 ctx = _setContext( _getStudyId() )
413 if sg.SelectedCount() != 1:
415 entry = sg.getSelected(0)
416 text = ctx.DM.getObject(entry).getText()
419 renderer=libSalomePy.getRenderer()
420 actor = getActor(entry)
422 actor = vtk.vtkTextActor()
423 dict_actors[entry] = actor
425 center = renderer.GetCenter()
426 actor.SetInput(str(text))
427 actor.SetPosition(center[0],center[1])
428 txtPr = vtk.vtkTextProperty()
429 txtPr.SetFontSize(30)
430 actor.SetTextProperty(txtPr)
431 for act in dict_actors.values():
432 renderer.RemoveActor(act)
433 renderer.AddActor(actor)
436 # Clear remove all lines under selected paragraph
437 def clearParagraph():
438 ctx = _setContext( _getStudyId() )
439 lines = sgPyQt.getChildren(sg.getSelected(0))
440 ctx.DM.removeObjects(lines)
441 sg.updateObjBrowser(True)
444 # Erase the selected line
446 ctx = _setContext( _getStudyId() )
447 if sg.SelectedCount() != 1:
449 entry = sg.getSelected(0)
450 text = ctx.DM.getObject(entry).getText()
453 renderer=libSalomePy.getRenderer()
454 actor = getActor(entry)
455 if actor is not None:
456 renderer.RemoveActor(actor)
460 # Return vtkActor by entry
462 if isinstance(entry,QString):
463 entry = entry.toLatin1().data()
464 if dict_actors.has_key(entry):
465 return dict_actors[entry]
478 973 : clearParagraph,
491 "clearParagraph": 973,