1 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 # Author : Roman NIKOLAEV Open CASCADE S.A.S. (roman.nikolaev@opencascade.com)
26 from SalomePyQt import *
27 import PYLIGHT_DataModel
28 from PyQt4.QtGui import *
29 from PyQt4.QtCore import *
36 # Get SALOME PyQt interface
38 # Get SALOME Swig interface
39 sg = libSALOME_Swig.SALOMEGUI_Swig()
42 DM = PYLIGHT_DataModel.PYLIGHT_DataModel()
44 # Create actions and menus
47 print "PYLIGHTGUI::initialize()"
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")
55 separator = sgPyQt.createSeparator()
57 menuFile = sgPyQt.createMenu( "File", -1, -1 )
58 # Add actions in the menu 'File'
59 sgPyQt.createMenu( separator ,menuFile, -1, 10)
60 sgPyQt.createMenu( dict_actions["loadfile"], menuFile, 10 );
61 sgPyQt.createMenu( dict_actions["savefile"], menuFile, 10 );
62 sgPyQt.createMenu( separator ,menuFile, -1, 10)
64 # Insert new line action
65 sgPyQt.createAction(dict_actions["insertLine"], "Insert Line", "Insert new line")
66 # Edit selected line action
67 sgPyQt.createAction(dict_actions["editLine"], "Edit Line", "Edit selected line")
68 # Remove selected line action
69 sgPyQt.createAction(dict_actions["removeLine"], "Remove Lines", "Remove selected lines")
70 # Clear all paragraphs
71 sgPyQt.createAction(dict_actions["clearAll"], "Clear All", "Clear all paragraphs")
73 sgPyQt.createAction(dict_actions["displayLine"], "Display Line", "Display selected line")
75 sgPyQt.createAction(dict_actions["eraseLine"], "Erase Line", "Erase selected line")
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 );
93 def OnGUIEvent(commandID):
94 print "PYLIGHTGUI::OnGUIEvent : commandID =",commandID
95 if dict_command.has_key( commandID ):
97 dict_command[commandID]()
101 print "The command is not implemented: ",commandID
104 # Customize popup menu
105 def createPopupMenu(popup, context):
109 if context != 'ObjectBrowser':
112 selcount = sg.SelectedCount()
114 entry = sg.getSelected( 0 )
115 obj = DM.getObject(entry)
117 if obj.getText() != "\n":
119 popup.addAction(sgPyQt.action(dict_actions["editLine"]))
120 popup.addAction(sgPyQt.action(dict_actions["removeLine"]))
122 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
123 popup.addAction(sgPyQt.action(dict_actions["displayLine"]))
124 popup.addAction(sgPyQt.action(dict_actions["eraseLine"]))
128 #Paragraph is selected
129 popup.addAction(sgPyQt.action(dict_actions["insertLine"]))
130 popup.addAction(sgPyQt.action(dict_actions["clearParagraph"]))
135 # For saving data in the study
136 def saveFiles(prefix):
137 print "PYLIGHTGUI::saveFile()"
138 postfix = "PYLIGHT.txt"
139 filename = prefix+postfix
140 DM.saveFile(QString(filename))
143 # For restore data from the study
144 def openFiles(filelist):
145 print "PYLIGHTGUI::openFile()"
146 filename = filelist[0]
147 filename.append(filelist[1])
148 DM.loadFile(filename)
151 # Loading a text file
153 aFilter = "Text files (*.txt)"
154 filename = QFileDialog.getOpenFileName(sgPyQt.getDesktop(), "Open text file", "", aFilter, "Choose a text file to open")
155 if filename.isEmpty():
158 if os.access(str(filename),os.R_OK):
159 DM.loadFile(filename)
161 QMessageBox.warning(sgPyQt.getDesktop(),
163 QString("Can not read file:\n%1").arg(filename))
165 sg.updateObjBrowser(True)
170 aFilter = "Text files (*.txt)"
171 filename = QFileDialog.getSaveFileName(sgPyQt.getDesktop(),"Save text file", "", aFilter, "Choose a text file to save")
172 if filename.contains(".txt") == 0:
173 filename.append(".txt")
177 # Get directory name and check access
178 if os.access(str(fn.left(fn.lastIndexOf("/"))), os.W_OK):
179 DM.saveFile(filename)
181 QMessageBox.warning(sgPyQt.getDesktop(),
183 QString("Can not save file:\n%1").arg(filename))
189 Insert new line in the selected paragraph.
192 res = QInputDialog.getText(sgPyQt.getDesktop(),
196 if not res[1]: ### user click cancel button
200 # Nb selected objects
201 selcount = sg.SelectedCount()
202 # Nb object in the Data Model
203 paragrCount = len(DM.getParagraphs())
205 # Create first paragraph
208 # If line not empty create first line
210 DM.createObject(text,DM.getParagraphs()[0])
211 sg.updateObjBrowser(True)
216 sg.updateObjBrowser(True)
220 QMessageBox.warning(sgPyQt.getDesktop(),
222 'Please, select paragraph!')
225 entry = sg.getSelected( 0 )
226 obj = DM.getObject(entry)
229 if(obj.getText() == "\n"):
230 DM.createObject(text,entry)
231 sg.updateObjBrowser(True);
234 QMessageBox.warning(sgPyQt.getDesktop(),
236 'Please, select paragraph!')
238 QMessageBox.warning(sgPyQt.getDesktop(),
240 'Please, select only one paragraph!')
246 if sg.SelectedCount() == 1:
247 entry = sg.getSelected( 0 )
248 obj = DM.getObject(entry)
249 if(obj.getText() != "\n"):
251 res = QInputDialog.getText(sgPyQt.getDesktop(),
255 PYLIGHT_DataModel.processText(obj.getText()))
256 if not res[1]: ### user click cancel button
262 QMessageBox.information(sgPyQt.getDesktop(),
264 'Please, select line!')
266 QMessageBox.information(sgPyQt.getDesktop(),
268 'Please, select one line!')
269 sg.updateObjBrowser(True);
272 # Remove selected lines
274 selcount = sg.SelectedCount()
278 entry = sg.getSelected( selcount - 1)
279 #Check what only lines selected
280 if DM.getObject(entry).getText() == "\n":
284 selcount = selcount-1
289 renderer=libSalomePy.getRenderer()
292 if actor is not None:
293 renderer.RemoveActor(actor)
296 DM.removeObjects(lines)
297 sg.updateObjBrowser(True)
301 # Remove all lines from all paragraphs
303 paragraphs = DM.getParagraphs()
304 for paragr in paragraphs:
305 lines = sgPyQt.getChildren(paragr)
306 DM.removeObjects(lines)
307 renderer=libSalomePy.getRenderer()
310 if actor is not None:
311 renderer.RemoveActor(actor)
314 sg.updateObjBrowser(True)
317 # Display the selected line
319 if sg.SelectedCount() != 1:
321 entry = sg.getSelected(0)
322 text = DM.getObject(entry).getText()
325 renderer=libSalomePy.getRenderer()
326 actor = getActor(entry)
328 actor = vtk.vtkTextActor()
329 dict_actors[entry] = actor
331 center = renderer.GetCenter()
332 actor.SetInput(str(text))
333 actor.SetPosition(center[0],center[1])
334 txtPr = vtk.vtkTextProperty()
335 txtPr.SetFontSize(30)
336 actor.SetTextProperty(txtPr)
337 for act in dict_actors.values():
338 renderer.RemoveActor(act)
339 renderer.AddActor(actor)
342 # Clear remove all lines under selected paragraph
343 def clearParagraph():
344 lines = sgPyQt.getChildren(sg.getSelected(0))
345 DM.removeObjects(lines)
346 sg.updateObjBrowser(True)
349 # Erase the selected line
351 if sg.SelectedCount() != 1:
353 entry = sg.getSelected(0)
354 text = DM.getObject(entry).getText()
357 renderer=libSalomePy.getRenderer()
358 actor = getActor(entry)
359 if actor is not None:
360 renderer.RemoveActor(actor)
364 # Return vtkActor by entry
366 if isinstance(entry,QString):
367 entry = entry.toLatin1().data()
368 if dict_actors.has_key(entry):
369 return dict_actors[entry]
382 973 : clearParagraph,
395 "clearParagraph": 973,