Salome HOME
Light Python sample module
[samples/pylight.git] / src / PYLIGHTGUI / PYLIGHT_DataModel.py
1 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
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.
10 #
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.
15 #
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
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 #  Author : Roman NIKOLAEV Open CASCADE S.A.S. (roman.nikolaev@opencascade.com)
23 #  Date   : 13/04/2009
24
25 import SalomePyQt
26 from PyQt4.QtCore import *
27
28 # Get SALOME PyQt interface
29 sgPyQt=SalomePyQt.SalomePyQt()
30
31 def processText(text):
32     '''
33     Remove "\n" sumbol from end of line
34     '''
35     processed = text
36     if isinstance(text,QString):
37         processed = text.toLatin1().data()
38     if processed[len(processed)-1:] == "\n":
39         processed = processed[:len(processed)-1]
40         
41     return processed
42
43 class PYLIGHT_DataModel:
44     '''
45     Data model of PYLIGHT module
46     '''
47     def __init__(self):
48         '''
49         Constructor of PYLIGHT_DataModel class.
50         '''
51         self.myObjects = []
52         pass
53     
54     def getParagraphs(self):
55         '''
56         Return the list of all paragraph entries.
57         '''
58         return sgPyQt.getChildren()
59
60     def getObject(self,entry):
61         '''
62         Return PYLIGHT_DataObject by its entry.
63         '''
64         for obj in self.myObjects:
65             if obj.getEntry() == entry:
66                 return obj
67         return None
68               
69     def createObject(self, text="\n", parent=None):
70         '''
71         Create PYLIGHT_DataObject (Paragraph or Line).
72         '''
73         obj = PYLIGHT_DataObject(text,parent)
74         self.myObjects.append(obj)
75         return obj.getEntry()
76         pass
77
78     def removeObjects(self,lines):
79         '''
80         Remove objects by its entries
81         '''
82         for ln in lines:
83             sgPyQt.removeObject(ln)
84             self.myObjects.remove(self.getObject(ln))
85             pass
86         pass
87
88     def loadFile(self,filename):
89         '''
90         Read text file and publish it.
91         '''
92         aFile = open(str(filename),"r")
93         lines = aFile.readlines()
94         if(lines[0] != "\n"):
95             paragr = self.createObject()
96         for line in lines:
97             if line == "\n":
98                 paragr = self.createObject()
99
100             else:
101                 self.createObject(processText(line), paragr)
102                 pass
103             pass
104         aFile.close()
105         pass
106     
107     def saveFile(self, filename):
108         aFile = open(str(filename),"w")
109         paragrs = self.getParagraphs()
110         for paragr in paragrs:
111             aFile.write("\n")
112             lines = sgPyQt.getChildren(paragr)
113             for line in lines:
114                 aFile.write(str(sgPyQt.getName(line))+"\n")
115                 pass
116             pass
117         aFile.close()
118         pass
119     pass
120
121 class PYLIGHT_DataObject:
122     '''
123     Data Object of PYLIGHT module
124     '''
125     def __init__(self,text,parent):
126         '''
127         Constructor of PYLIGHT_DataObject class
128         '''
129         if(parent == None):
130             entry = sgPyQt.createObject("Paragraph",
131                                         "",
132                                         "Paragraph object")
133             sgPyQt.setIcon(entry,"PYLIGHT_PARAGR_ICON")
134         else:
135             entry = sgPyQt.createObject(processText(text),
136                                         "PYLIGHT_LINE_ICON",
137                                         "Line object",
138                                         parent)
139             pass
140         self.entry = entry
141         self.text = text
142         pass
143     
144     def getEntry(self):
145         '''
146         Return entry of object 
147         '''
148         return self.entry
149         pass
150
151     def setText(self,text):
152         '''
153         Set text string
154         '''
155         self.text = text
156         if(text != ""):
157             sgPyQt.setName(self.entry,text)
158         pass
159
160     def getText(self):
161         '''
162         Return text string
163         '''
164         return self.text
165     
166     pass