1 # Copyright (C) 2009-2020 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 *
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 ################################################
53 sgPyQt.createAction(dict_actions["loadfile"], "Load text File", "Load text file")
55 sgPyQt.createAction(dict_actions["savefile"], "Save text File", "Save text file")
57 sgPyQt.createAction(dict_actions["insertLine"], "Insert Line", "Insert new text line")
58 # Insert new line action
59 sgPyQt.createAction(dict_actions["insertLine"], "Insert Line", "Insert new line")
60 # Edit selected line action
61 sgPyQt.createAction(dict_actions["editLine"], "Edit Line", "Edit selected line")
62 # Remove selected line action
63 sgPyQt.createAction(dict_actions["removeLine"], "Remove Lines", "Remove selected lines")
65 sgPyQt.createAction(dict_actions["clearParagraph"], "Clear Paragraph", "Clear selected paragraph")
66 # Clear all paragraphs
67 sgPyQt.createAction(dict_actions["clearAll"], "Clear All", "Clear all paragraphs")
69 sgPyQt.createAction(dict_actions["displayLine"], "Display Line", "Display selected line")
71 sgPyQt.createAction(dict_actions["eraseLine"], "Erase Line", "Erase selected line")
73 separator = sgPyQt.createSeparator()
76 menuFile = sgPyQt.createMenu( "File", -1, -1 )
77 # Add actions in the menu 'File'
78 sgPyQt.createMenu( separator, menuFile, -1, 10)
79 sgPyQt.createMenu( dict_actions["loadfile"], menuFile, 10 );
80 sgPyQt.createMenu( dict_actions["savefile"], menuFile, 10 );
81 sgPyQt.createMenu( separator, menuFile, -1, 10)
82 # Create 'PyLight' menu
83 menuPyLight = sgPyQt.createMenu( "PyLight", -1, -1, 50)
84 # Add actions in the menu 'PyLight'
85 sgPyQt.createMenu( dict_actions["insertLine"], menuPyLight, 10 );
86 sgPyQt.createMenu( dict_actions["editLine"], menuPyLight, 10 );
87 sgPyQt.createMenu( dict_actions["removeLine"], menuPyLight, 10 );
88 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
89 sgPyQt.createMenu( dict_actions["clearAll"], menuPyLight, 10 );
90 sgPyQt.createMenu( separator, menuPyLight, -1, 10);
91 sgPyQt.createMenu( dict_actions["displayLine"], menuPyLight, 10 );
92 sgPyQt.createMenu( dict_actions["eraseLine"], menuPyLight, 10 );
95 if __data_model__ is None:
96 __data_model__ = PYLIGHT_DataModel.PYLIGHT_DataModel()
99 pass # def __init__( self )
101 pass # class GUIcontext
103 ################################################
104 # Global variables and functions
105 ################################################
115 if __verbose__ is None:
117 __verbose__ = int( os.getenv( 'SALOME_VERBOSE', 0 ) )
124 ################################################
126 # Create actions and menus
129 if verbose(): print("PYLIGHTGUI::initialize()")
132 # called when module is activated
133 # returns True if activating is successfull and False otherwise
135 if verbose() : print("PYLIGHTGUI.activate()")
138 # called when module is deactivated
140 if verbose() : print("PYLIGHTGUI.deactivate()")
144 def OnGUIEvent(commandID):
145 if verbose() : print("PYLIGHTGUI::OnGUIEvent : commandID = %d" % commandID)
146 if commandID in dict_command:
148 dict_command[commandID]()
150 traceback.print_exc()
152 if verbose() : print("The command is not implemented: %d" % commandID)
155 # Customize popup menu
156 def createPopupMenu(popup, context):
157 global __data_model__
158 if verbose() : print("PYLIGHTGUI.createPopupMenu(): context = %s" % context)
160 if context != 'ObjectBrowser':
163 selcount = sg.SelectedCount()
165 entry = sg.getSelected( 0 )
166 obj = __data_model__.getObject(entry)
168 if obj.getText() != "\n":
170 popup.addAction(sgPyQt.action(dict_actions["editLine"]))
171 popup.addAction(sgPyQt.action(dict_actions["removeLine"]))
173 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
174 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
175 popup.addAction(sgPyQt.action(dict_actions["eraseLine"]))
178 # Paragraph is selected
179 popup.addAction(sgPyQt.action(dict_actions["insertLine"]))
180 popup.addAction(sgPyQt.action(dict_actions["clearParagraph"]))
186 pass # if selcount == 1
189 # For saving data in the study
190 def saveFiles(directory, url):
191 global __data_model__
192 if verbose(): print("PYLIGHTGUI::saveFile()")
193 filename = osp.join(directory, osp.splitext(osp.basename(url))[0]) + "_PYLIGHT.txt"
194 __data_model__.saveFile(filename)
195 return osp.basename(filename)
197 # For restore data from the study
198 def openFiles(filelist, url):
199 global __data_model__
200 if verbose(): print("PYLIGHTGUI::openFile()")
201 filename = os.path.join(*filelist)
202 __data_model__.loadFile(filename)
205 # Loading a text file
207 global __data_model__
208 aFilter = "Text files (*.txt)"
209 filename = QFileDialog.getOpenFileName(sgPyQt.getDesktop(), "Open text file", "", aFilter, "Choose a text file to open")
211 if isinstance(filename,tuple) and len(filename) >=2:
212 filename = filename[0]
214 if len(filename) == 0:
217 if os.access(str(filename),os.R_OK):
218 __data_model__.loadFile(filename)
220 QMessageBox.warning(sgPyQt.getDesktop(),
222 "Can not read file:\n%s"%(filename))
224 sg.updateObjBrowser()
229 global __data_model__
230 aFilter = "Text files (*.txt)"
231 filename = QFileDialog.getSaveFileName(sgPyQt.getDesktop(),"Save text file", "", aFilter, "Choose a text file to save")
233 if isinstance(filename,tuple) and len(filename) >=2:
234 filename = filename[0]
236 if filename.endswith(".txt") == 0:
241 # Get directory name and check access
242 if os.access(str(fn[:fn.rindex(osp.sep)]), os.W_OK):
243 __data_model__.saveFile(filename)
245 QMessageBox.warning(sgPyQt.getDesktop(),
247 "Can not save file:\n%s"%(filename))
253 Insert new line in the selected paragraph.
255 global __data_model__
257 res = QInputDialog.getText(sgPyQt.getDesktop(),
261 if not res[1]: ### user click cancel button
265 # Nb selected objects
266 selcount = sg.SelectedCount()
267 # Nb object in the Data Model
268 paragrCount = len(__data_model__.getParagraphs())
270 # Create first paragraph
272 __data_model__.createObject()
273 # If line not empty create first line
275 __data_model__.createObject(text,__data_model__.getParagraphs()[0])
276 sg.updateObjBrowser()
280 __data_model__.createObject()
281 sg.updateObjBrowser()
285 QMessageBox.warning(sgPyQt.getDesktop(),
287 'Please, select paragraph!')
290 entry = sg.getSelected( 0 )
291 obj = __data_model__.getObject(entry)
294 if(obj.getText() == "\n"):
295 __data_model__.createObject(text,entry)
296 sg.updateObjBrowser();
299 QMessageBox.warning(sgPyQt.getDesktop(),
301 'Please, select paragraph!')
303 QMessageBox.warning(sgPyQt.getDesktop(),
305 'Please, select only one paragraph!')
311 global __data_model__
312 if sg.SelectedCount() == 1:
313 entry = sg.getSelected( 0 )
314 obj = __data_model__.getObject(entry)
315 if(obj is not None and obj.getText() != "\n"):
317 res = QInputDialog.getText(sgPyQt.getDesktop(),
321 PYLIGHT_DataModel.processText(obj.getText()))
322 if not res[1]: ### user click cancel button
328 QMessageBox.information(sgPyQt.getDesktop(),
330 'Please, select line!')
332 QMessageBox.information(sgPyQt.getDesktop(),
334 'Please, select one line!')
335 sg.updateObjBrowser();
338 # Remove selected lines
340 global __data_model__
341 selcount = sg.SelectedCount()
345 entry = sg.getSelected( selcount - 1)
346 #Check what only lines selected
347 obj = __data_model__.getObject(entry)
350 if obj.getText() == "\n":
354 selcount = selcount-1
359 renderer=libSalomePy.getRenderer()
362 if actor is not None:
363 renderer.RemoveActor(actor)
366 __data_model__.removeObjects(lines)
367 sg.updateObjBrowser()
371 # Remove all lines from all paragraphs
373 global __data_model__
374 paragraphs = __data_model__.getParagraphs()
375 for paragr in paragraphs:
376 lines = sgPyQt.getChildren(paragr)
377 __data_model__.removeObjects(lines)
378 renderer=libSalomePy.getRenderer()
381 if actor is not None:
382 renderer.RemoveActor(actor)
385 sg.updateObjBrowser()
388 # Display the selected line
390 global __data_model__
391 if sg.SelectedCount() != 1:
393 entry = sg.getSelected(0)
394 obj = ctx.DM.getObject(entry)
396 QMessageBox.information(sgPyQt.getDesktop(),
398 'Please, select line!')
403 renderer=libSalomePy.getRenderer()
404 actor = getActor(entry)
406 actor = vtk.vtkTextActor()
407 dict_actors[entry] = actor
409 center = renderer.GetCenter()
410 actor.SetInput(str(text))
411 actor.SetPosition(center[0],center[1])
412 txtPr = vtk.vtkTextProperty()
413 txtPr.SetFontSize(30)
414 actor.SetTextProperty(txtPr)
415 for act in list(dict_actors.values()):
416 renderer.RemoveActor(act)
417 renderer.AddActor(actor)
420 # Clear remove all lines under selected paragraph
421 def clearParagraph():
422 global __data_model__
423 lines = sgPyQt.getChildren(sg.getSelected(0))
424 __data_model__.removeObjects(lines)
425 sg.updateObjBrowser()
428 # Erase the selected line
430 global __data_model__
431 if sg.SelectedCount() != 1:
433 entry = sg.getSelected(0)
434 obj = __data_model__.getObject(entry)
436 QMessageBox.information(sgPyQt.getDesktop(),
438 'Please, select line!')
443 renderer=libSalomePy.getRenderer()
444 actor = getActor(entry)
445 if actor is not None:
446 renderer.RemoveActor(actor)
450 # Return vtkActor by entry
453 if entry in dict_actors:
454 return dict_actors[entry]
467 973 : clearParagraph,
480 "clearParagraph": 973,