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 dict_command.has_key( commandID ):
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 filename = unicode(filename)
217 if len(filename) == 0:
220 if os.access(str(filename),os.R_OK):
221 __data_model__.loadFile(filename)
223 QMessageBox.warning(sgPyQt.getDesktop(),
225 "Can not read file:\n%s"%(filename))
227 sg.updateObjBrowser()
232 global __data_model__
233 aFilter = "Text files (*.txt)"
234 filename = QFileDialog.getSaveFileName(sgPyQt.getDesktop(),"Save text file", "", aFilter, "Choose a text file to save")
236 if isinstance(filename,tuple) and len(filename) >=2:
237 filename = filename[0]
239 filename = unicode(filename)
241 if filename.endswith(".txt") == 0:
246 # Get directory name and check access
247 if os.access(str(fn[:fn.rindex(os.path.sep)]), os.W_OK):
248 __data_model__.saveFile(filename)
250 QMessageBox.warning(sgPyQt.getDesktop(),
252 "Can not save file:\n%s"%(filename))
258 Insert new line in the selected paragraph.
260 global __data_model__
262 res = QInputDialog.getText(sgPyQt.getDesktop(),
266 if not res[1]: ### user click cancel button
270 # Nb selected objects
271 selcount = sg.SelectedCount()
272 # Nb object in the Data Model
273 paragrCount = len(__data_model__.getParagraphs())
275 # Create first paragraph
277 __data_model__.createObject()
278 # If line not empty create first line
280 __data_model__.createObject(text,__data_model__.getParagraphs()[0])
281 sg.updateObjBrowser()
285 __data_model__.createObject()
286 sg.updateObjBrowser()
290 QMessageBox.warning(sgPyQt.getDesktop(),
292 'Please, select paragraph!')
295 entry = sg.getSelected( 0 )
296 obj = __data_model__.getObject(entry)
299 if(obj.getText() == "\n"):
300 __data_model__.createObject(text,entry)
301 sg.updateObjBrowser();
304 QMessageBox.warning(sgPyQt.getDesktop(),
306 'Please, select paragraph!')
308 QMessageBox.warning(sgPyQt.getDesktop(),
310 'Please, select only one paragraph!')
316 global __data_model__
317 if sg.SelectedCount() == 1:
318 entry = sg.getSelected( 0 )
319 obj = __data_model__.getObject(entry)
320 if(obj.getText() != "\n"):
322 res = QInputDialog.getText(sgPyQt.getDesktop(),
326 PYLIGHT_DataModel.processText(obj.getText()))
327 if not res[1]: ### user click cancel button
333 QMessageBox.information(sgPyQt.getDesktop(),
335 'Please, select line!')
337 QMessageBox.information(sgPyQt.getDesktop(),
339 'Please, select one line!')
340 sg.updateObjBrowser();
343 # Remove selected lines
345 global __data_model__
346 selcount = sg.SelectedCount()
350 entry = sg.getSelected( selcount - 1)
351 #Check what only lines selected
352 if __data_model__.getObject(entry).getText() == "\n":
356 selcount = selcount-1
361 renderer=libSalomePy.getRenderer()
364 if actor is not None:
365 renderer.RemoveActor(actor)
368 __data_model__.removeObjects(lines)
369 sg.updateObjBrowser()
373 # Remove all lines from all paragraphs
375 global __data_model__
376 paragraphs = __data_model__.getParagraphs()
377 for paragr in paragraphs:
378 lines = sgPyQt.getChildren(paragr)
379 __data_model__.removeObjects(lines)
380 renderer=libSalomePy.getRenderer()
383 if actor is not None:
384 renderer.RemoveActor(actor)
387 sg.updateObjBrowser()
390 # Display the selected line
392 global __data_model__
393 if sg.SelectedCount() != 1:
395 entry = sg.getSelected(0)
396 text = __data_model__.getObject(entry).getText()
399 renderer=libSalomePy.getRenderer()
400 actor = getActor(entry)
402 actor = vtk.vtkTextActor()
403 dict_actors[entry] = actor
405 center = renderer.GetCenter()
406 actor.SetInput(str(text))
407 actor.SetPosition(center[0],center[1])
408 txtPr = vtk.vtkTextProperty()
409 txtPr.SetFontSize(30)
410 actor.SetTextProperty(txtPr)
411 for act in dict_actors.values():
412 renderer.RemoveActor(act)
413 renderer.AddActor(actor)
416 # Clear remove all lines under selected paragraph
417 def clearParagraph():
418 global __data_model__
419 lines = sgPyQt.getChildren(sg.getSelected(0))
420 __data_model__.removeObjects(lines)
421 sg.updateObjBrowser()
424 # Erase the selected line
426 global __data_model__
427 if sg.SelectedCount() != 1:
429 entry = sg.getSelected(0)
430 text = __data_model__.getObject(entry).getText()
433 renderer=libSalomePy.getRenderer()
434 actor = getActor(entry)
435 if actor is not None:
436 renderer.RemoveActor(actor)
440 # Return vtkActor by entry
443 if dict_actors.has_key(entry):
444 return dict_actors[entry]
457 973 : clearParagraph,
470 "clearParagraph": 973,