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 ################################################
52 sgPyQt.createAction(dict_actions["loadfile"], "Load text File", "Load text file")
54 sgPyQt.createAction(dict_actions["savefile"], "Save text File", "Save text file")
56 sgPyQt.createAction(dict_actions["insertLine"], "Insert Line", "Insert new text line")
57 # Insert new line action
58 sgPyQt.createAction(dict_actions["insertLine"], "Insert Line", "Insert new line")
59 # Edit selected line action
60 sgPyQt.createAction(dict_actions["editLine"], "Edit Line", "Edit selected line")
61 # Remove selected line action
62 sgPyQt.createAction(dict_actions["removeLine"], "Remove Lines", "Remove selected lines")
64 sgPyQt.createAction(dict_actions["clearParagraph"], "Clear Paragraph", "Clear selected paragraph")
65 # Clear all paragraphs
66 sgPyQt.createAction(dict_actions["clearAll"], "Clear All", "Clear all paragraphs")
68 sgPyQt.createAction(dict_actions["displayLine"], "Display Line", "Display selected line")
70 sgPyQt.createAction(dict_actions["eraseLine"], "Erase Line", "Erase selected line")
72 separator = sgPyQt.createSeparator()
75 menuFile = sgPyQt.createMenu( "File", -1, -1 )
76 # Add actions in the menu 'File'
77 sgPyQt.createMenu( separator, menuFile, -1, 10)
78 sgPyQt.createMenu( dict_actions["loadfile"], menuFile, 10 );
79 sgPyQt.createMenu( dict_actions["savefile"], menuFile, 10 );
80 sgPyQt.createMenu( separator, menuFile, -1, 10)
81 # Create 'PyLight' menu
82 menuPyLight = sgPyQt.createMenu( "PyLight", -1, -1, 50)
83 # Add actions in the menu 'PyLight'
84 sgPyQt.createMenu( dict_actions["insertLine"], menuPyLight, 10 );
85 sgPyQt.createMenu( dict_actions["editLine"], menuPyLight, 10 );
86 sgPyQt.createMenu( dict_actions["removeLine"], menuPyLight, 10 );
87 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
88 sgPyQt.createMenu( dict_actions["clearAll"], menuPyLight, 10 );
89 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
90 sgPyQt.createMenu( dict_actions["displayLine"], menuPyLight, 10 );
91 sgPyQt.createMenu( dict_actions["eraseLine"], menuPyLight, 10 );
94 if __data_model__ is None:
95 __data_model__ = PYLIGHT_DataModel.PYLIGHT_DataModel()
98 pass # def __init__( self )
100 pass # class GUIcontext
102 ################################################
103 # Global variables and functions
104 ################################################
114 if __verbose__ is None:
116 __verbose__ = int( os.getenv( 'SALOME_VERBOSE', 0 ) )
123 ################################################
125 # Create actions and menus
127 if verbose(): print("PYLIGHTGUI::initialize()")
130 # called when module is activated
131 # returns True if activating is successfull and False otherwise
133 if verbose() : print("PYLIGHTGUI.activate()")
137 # called when module is deactivated
139 if verbose() : print("PYLIGHTGUI.deactivate()")
143 def OnGUIEvent(commandID):
144 if verbose() : print("PYLIGHTGUI::OnGUIEvent : commandID = %d" % commandID)
145 if commandID in dict_command:
147 dict_command[commandID]()
149 traceback.print_exc()
151 if verbose() : print("The command is not implemented: %d" % commandID)
154 # Customize popup menu
155 def createPopupMenu(popup, context):
156 global __data_model__
157 if verbose() : print("PYLIGHTGUI.createPopupMenu(): context = %s" % context)
159 if context != 'ObjectBrowser':
162 selcount = sg.SelectedCount()
164 entry = sg.getSelected( 0 )
165 obj = __data_model__.getObject(entry)
167 if obj.getText() != "\n":
169 popup.addAction(sgPyQt.action(dict_actions["editLine"]))
170 popup.addAction(sgPyQt.action(dict_actions["removeLine"]))
172 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
173 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
174 popup.addAction(sgPyQt.action(dict_actions["eraseLine"]))
177 # Paragraph is selected
178 popup.addAction(sgPyQt.action(dict_actions["insertLine"]))
179 popup.addAction(sgPyQt.action(dict_actions["clearParagraph"]))
185 pass # if selcount == 1
188 # For saving data in the study
189 def saveFiles(prefix):
190 global __data_model__
191 if verbose(): print("PYLIGHTGUI::saveFile()")
192 postfix = "PYLIGHT.txt"
193 filename = prefix+postfix
194 __data_model__.saveFile(filename)
197 # For restore data from the study
198 def openFiles(filelist):
199 global __data_model__
200 if verbose(): print("PYLIGHTGUI::openFile()")
201 filename = filelist[0]
202 filename.append(filelist[1])
203 __data_model__.loadFile(filename)
206 # Loading a text file
208 global __data_model__
209 aFilter = "Text files (*.txt)"
210 filename = QFileDialog.getOpenFileName(sgPyQt.getDesktop(), "Open text file", "", aFilter, "Choose a text file to open")
212 if isinstance(filename,tuple) and len(filename) >=2:
213 filename = filename[0]
215 if len(filename) == 0:
218 if os.access(str(filename),os.R_OK):
219 __data_model__.loadFile(filename)
221 QMessageBox.warning(sgPyQt.getDesktop(),
223 "Can not read file:\n%s"%(filename))
225 sg.updateObjBrowser()
230 global __data_model__
231 aFilter = "Text files (*.txt)"
232 filename = QFileDialog.getSaveFileName(sgPyQt.getDesktop(),"Save text file", "", aFilter, "Choose a text file to save")
234 if isinstance(filename,tuple) and len(filename) >=2:
235 filename = filename[0]
237 if filename.endswith(".txt") == 0:
242 # Get directory name and check access
243 if os.access(str(fn[:fn.rindex(os.path.sep)]), os.W_OK):
244 __data_model__.saveFile(filename)
246 QMessageBox.warning(sgPyQt.getDesktop(),
248 "Can not save file:\n%s"%(filename))
254 Insert new line in the selected paragraph.
256 global __data_model__
258 res = QInputDialog.getText(sgPyQt.getDesktop(),
262 if not res[1]: ### user click cancel button
266 # Nb selected objects
267 selcount = sg.SelectedCount()
268 # Nb object in the Data Model
269 paragrCount = len(__data_model__.getParagraphs())
271 # Create first paragraph
273 __data_model__.createObject()
274 # If line not empty create first line
276 __data_model__.createObject(text,__data_model__.getParagraphs()[0])
277 sg.updateObjBrowser()
281 __data_model__.createObject()
282 sg.updateObjBrowser()
286 QMessageBox.warning(sgPyQt.getDesktop(),
288 'Please, select paragraph!')
291 entry = sg.getSelected( 0 )
292 obj = __data_model__.getObject(entry)
295 if(obj.getText() == "\n"):
296 __data_model__.createObject(text,entry)
297 sg.updateObjBrowser();
300 QMessageBox.warning(sgPyQt.getDesktop(),
302 'Please, select paragraph!')
304 QMessageBox.warning(sgPyQt.getDesktop(),
306 'Please, select only one paragraph!')
312 global __data_model__
313 if sg.SelectedCount() == 1:
314 entry = sg.getSelected( 0 )
315 obj = __data_model__.getObject(entry)
316 if(obj is not None and obj.getText() != "\n"):
318 res = QInputDialog.getText(sgPyQt.getDesktop(),
322 PYLIGHT_DataModel.processText(obj.getText()))
323 if not res[1]: ### user click cancel button
329 QMessageBox.information(sgPyQt.getDesktop(),
331 'Please, select line!')
333 QMessageBox.information(sgPyQt.getDesktop(),
335 'Please, select one line!')
336 sg.updateObjBrowser();
339 # Remove selected lines
341 global __data_model__
342 selcount = sg.SelectedCount()
346 entry = sg.getSelected( selcount - 1)
347 #Check what only lines selected
348 obj = __data_model__.getObject(entry)
351 if obj.getText() == "\n":
355 selcount = selcount-1
360 renderer=libSalomePy.getRenderer()
363 if actor is not None:
364 renderer.RemoveActor(actor)
367 __data_model__.removeObjects(lines)
368 sg.updateObjBrowser()
372 # Remove all lines from all paragraphs
374 global __data_model__
375 paragraphs = __data_model__.getParagraphs()
376 for paragr in paragraphs:
377 lines = sgPyQt.getChildren(paragr)
378 __data_model__.removeObjects(lines)
379 renderer=libSalomePy.getRenderer()
382 if actor is not None:
383 renderer.RemoveActor(actor)
386 sg.updateObjBrowser()
389 # Display the selected line
391 global __data_model__
392 if sg.SelectedCount() != 1:
394 entry = sg.getSelected(0)
395 obj = ctx.DM.getObject(entry)
397 QMessageBox.information(sgPyQt.getDesktop(),
399 'Please, select line!')
404 renderer=libSalomePy.getRenderer()
405 actor = getActor(entry)
407 actor = vtk.vtkTextActor()
408 dict_actors[entry] = actor
410 center = renderer.GetCenter()
411 actor.SetInput(str(text))
412 actor.SetPosition(center[0],center[1])
413 txtPr = vtk.vtkTextProperty()
414 txtPr.SetFontSize(30)
415 actor.SetTextProperty(txtPr)
416 for act in list(dict_actors.values()):
417 renderer.RemoveActor(act)
418 renderer.AddActor(actor)
421 # Clear remove all lines under selected paragraph
422 def clearParagraph():
423 global __data_model__
424 lines = sgPyQt.getChildren(sg.getSelected(0))
425 __data_model__.removeObjects(lines)
426 sg.updateObjBrowser()
429 # Erase the selected line
431 global __data_model__
432 if sg.SelectedCount() != 1:
434 entry = sg.getSelected(0)
435 obj = __data_model__.getObject(entry)
437 QMessageBox.information(sgPyQt.getDesktop(),
439 'Please, select line!')
444 renderer=libSalomePy.getRenderer()
445 actor = getActor(entry)
446 if actor is not None:
447 renderer.RemoveActor(actor)
451 # Return vtkActor by entry
454 if entry in dict_actors:
455 return dict_actors[entry]
468 973 : clearParagraph,
481 "clearParagraph": 973,