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