Salome HOME
Revert "Synchronize adm files"
[samples/pylight.git] / src / PYLIGHTGUI / PYLIGHT_DataModel.py
1 # Copyright (C) 2009-2014  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 PyQt4.QtCore 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 = text
34     if isinstance(text,QString):
35         processed = text.toLatin1().data()
36     if processed[len(processed)-1:] == "\n":
37         processed = processed[:len(processed)-1]
38         
39     return processed
40
41 class PYLIGHT_DataModel:
42     '''
43     Data model of PYLIGHT module
44     '''
45     def __init__(self):
46         '''
47         Constructor of PYLIGHT_DataModel class.
48         '''
49         self.myObjects = []
50         pass
51     
52     def getParagraphs(self):
53         '''
54         Return the list of all paragraph entries.
55         '''
56         return sgPyQt.getChildren()
57
58     def getObject(self,entry):
59         '''
60         Return PYLIGHT_DataObject by its entry.
61         '''
62         for obj in self.myObjects:
63             if obj.getEntry() == entry:
64                 return obj
65         return None
66               
67     def createObject(self, text="\n", parent=None):
68         '''
69         Create PYLIGHT_DataObject (Paragraph or Line).
70         '''
71         obj = PYLIGHT_DataObject(text,parent)
72         self.myObjects.append(obj)
73         return obj.getEntry()
74         pass
75
76     def removeObjects(self,lines):
77         '''
78         Remove objects by its entries
79         '''
80         for ln in lines:
81             sgPyQt.removeObject(ln)
82             self.myObjects.remove(self.getObject(ln))
83             pass
84         pass
85
86     def loadFile(self,filename):
87         '''
88         Read text file and publish it.
89         '''
90         aFile = open(str(filename),"r")
91         lines = aFile.readlines()
92         if(lines[0] != "\n"):
93             paragr = self.createObject()
94         for line in lines:
95             if line == "\n":
96                 paragr = self.createObject()
97
98             else:
99                 self.createObject(processText(line), paragr)
100                 pass
101             pass
102         aFile.close()
103         pass
104     
105     def saveFile(self, filename):
106         aFile = open(str(filename),"w")
107         paragrs = self.getParagraphs()
108         for paragr in paragrs:
109             aFile.write("\n")
110             lines = sgPyQt.getChildren(paragr)
111             for line in lines:
112                 aFile.write(str(sgPyQt.getName(line))+"\n")
113                 pass
114             pass
115         aFile.close()
116         pass
117     pass
118
119 class PYLIGHT_DataObject:
120     '''
121     Data Object of PYLIGHT module
122     '''
123     def __init__(self,text,parent):
124         '''
125         Constructor of PYLIGHT_DataObject class
126         '''
127         if(parent == None):
128             entry = sgPyQt.createObject("Paragraph",
129                                         "",
130                                         "Paragraph object")
131             sgPyQt.setIcon(entry,"PYLIGHT_PARAGR_ICON")
132         else:
133             entry = sgPyQt.createObject(processText(text),
134                                         "PYLIGHT_LINE_ICON",
135                                         "Line object",
136                                         parent)
137             pass
138         self.entry = entry
139         self.text = text
140         pass
141     
142     def getEntry(self):
143         '''
144         Return entry of object 
145         '''
146         return self.entry
147         pass
148
149     def setText(self,text):
150         '''
151         Set text string
152         '''
153         self.text = text
154         if(text != ""):
155             sgPyQt.setName(self.entry,text)
156         pass
157
158     def getText(self):
159         '''
160         Return text string
161         '''
162         return self.text
163     
164     pass