0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / IFSelect / IFSelect_ListEditor.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <IFSelect_ListEditor.hxx>
16 #include <Interface_InterfaceModel.hxx>
17 #include <Interface_TypedValue.hxx>
18 #include <Standard_Type.hxx>
19 #include <TCollection_HAsciiString.hxx>
20
21 IFSelect_ListEditor::IFSelect_ListEditor  ()
22 : themax (0) , thetouc (0)  {  }
23
24      IFSelect_ListEditor::IFSelect_ListEditor
25   (const Handle(Interface_TypedValue)& def, const Standard_Integer max)
26 : themax (max) , thedef (def) , thetouc (0)    {  }
27
28
29 void  IFSelect_ListEditor::LoadModel (const Handle(Interface_InterfaceModel)& model)
30 {  themodl = model;  }
31
32 void  IFSelect_ListEditor::LoadValues (const Handle(TColStd_HSequenceOfHAsciiString)& vals)
33 {
34   theorig = vals;
35   ClearEdit();
36 }
37
38
39 void  IFSelect_ListEditor::SetTouched ()
40 {  thetouc = 1;  }
41
42 void  IFSelect_ListEditor::ClearEdit ()
43 {
44   theedit = new TColStd_HSequenceOfHAsciiString();
45   thestat = new TColStd_HSequenceOfInteger();
46   if (theorig.IsNull()) return;
47   Standard_Integer i,nb = theorig->Length();
48   for (i = 1; i <= nb; i ++) {
49     theedit->Append (theorig->Value(i));
50     thestat->Append (0);
51   }
52   thetouc = 0;
53 }
54
55 //  ########    CHECK    ########
56
57 static Standard_Boolean  CheckValue
58   (const Handle(TCollection_HAsciiString)& val,
59    const Handle(Interface_InterfaceModel)& modl,
60    const Handle(Interface_TypedValue)& thedef)
61 {
62   if (val.IsNull() || modl.IsNull() || thedef.IsNull()) return Standard_True;
63
64   Interface_ParamType pty = thedef->Type();
65   if (!thedef->Satisfies(val)) return Standard_False;
66   if (pty == Interface_ParamIdent && !val.IsNull()) {
67     if (modl->NextNumberForLabel(val->ToCString(),0) <= 0)
68       return Standard_False;
69   }
70   return Standard_True;
71 }
72
73 //  ########    EDITION    ########
74
75 Standard_Boolean  IFSelect_ListEditor::LoadEdited
76   (const Handle(TColStd_HSequenceOfHAsciiString)& list)
77 {
78   if (list.IsNull()) return Standard_False;
79   Standard_Integer i, nb = list->Length();
80   if (nb > themax) return Standard_False;
81
82 //   check values
83   if (!thedef.IsNull()) {
84     for (i = 1; i <= nb; i ++) {
85       Handle(TCollection_HAsciiString) newval = list->Value(i);
86       if (!CheckValue (newval,themodl,thedef)) return Standard_False;
87     }
88   }
89
90 //  OK
91   theedit = list;
92   thestat = new TColStd_HSequenceOfInteger();
93   for (i = 1; i <= nb; i ++) thestat->Append (1);
94   thetouc = 1;
95
96   return Standard_True;
97 }
98
99
100 Standard_Boolean  IFSelect_ListEditor::SetValue
101   (const Standard_Integer num, const Handle(TCollection_HAsciiString)& val)
102 {
103   if (theedit.IsNull()) return Standard_False;
104   if (num < 1 || num > theedit->Length()) return Standard_False;
105
106 //   check value
107   if (!CheckValue(val,themodl,thedef)) return Standard_False;
108
109 // OK
110   theedit->SetValue (num,val);
111   thestat->SetValue (num,1);
112   thetouc = 1;
113   return Standard_True;
114 }
115
116
117 Standard_Boolean  IFSelect_ListEditor::AddValue
118   (const Handle(TCollection_HAsciiString)& val, const Standard_Integer atnum)
119 {
120   if (theedit.IsNull()) return Standard_False;
121   if (themax > 0 && theedit->Length() >= themax) return Standard_False;
122   if (!CheckValue (val,themodl,thedef)) return Standard_False;
123   if (atnum > 0) {
124     theedit->InsertBefore (atnum,val);
125     thestat->InsertBefore (atnum,2);
126   } else {
127     theedit->Append (val);
128     thestat->Append (2);
129   }
130   thetouc = 2;
131   return Standard_True;
132 }
133
134
135 Standard_Boolean  IFSelect_ListEditor::Remove
136   (const Standard_Integer num, const Standard_Integer howmany)
137 {
138   if (theedit.IsNull()) return Standard_False;
139   Standard_Integer nb = theedit->Length();
140   if (num < 0) return Standard_False;
141   if (num == 0) return Remove (nb-howmany,howmany);
142
143   if ((num+howmany) > nb) return Standard_False;
144   theedit->Remove(num,howmany);
145   thestat->Remove(num,howmany);
146   thetouc = 3;
147   return Standard_True;
148 }
149
150
151 //  ########    QUERIES    ########
152
153 Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_ListEditor::OriginalValues () const
154 {  return theorig;  }
155
156 Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_ListEditor::EditedValues () const
157 {  return theedit;  }
158
159
160 Standard_Integer  IFSelect_ListEditor::NbValues (const Standard_Boolean edited) const
161 {
162   if (edited) return (theedit.IsNull() ? 0 : theedit->Length());
163   return (theorig.IsNull() ? 0 : theorig->Length());
164 }
165
166
167 Handle(TCollection_HAsciiString)  IFSelect_ListEditor::Value
168   (const Standard_Integer num, const Standard_Boolean edited) const
169 {
170   Handle(TCollection_HAsciiString) val;
171   if (edited) {
172     if (theedit.IsNull()) return val;
173     if (num < 1 || num > theedit->Length()) return val;
174     val = theedit->Value(num);
175   } else {
176     if (theorig.IsNull()) return val;
177     if (num < 1 || num > theorig->Length()) return val;
178     val = theorig->Value(num);
179   }
180   return val;
181 }
182
183 Standard_Boolean  IFSelect_ListEditor::IsChanged (const Standard_Integer num) const
184 {
185   if (thestat.IsNull()) return Standard_False;
186   if (num < 1 || num > thestat->Length()) return Standard_False;
187   Standard_Integer stat = thestat->Value(num);
188   return (stat != 0);
189 }
190
191 Standard_Boolean  IFSelect_ListEditor::IsModified (const Standard_Integer num) const
192 {
193   if (thestat.IsNull()) return Standard_False;
194   if (num < 1 || num > thestat->Length()) return Standard_False;
195   Standard_Integer stat = thestat->Value(num);
196   return (stat == 1);
197 }
198
199 Standard_Boolean  IFSelect_ListEditor::IsAdded (const Standard_Integer num) const
200 {
201   if (thestat.IsNull()) return Standard_False;
202   if (num < 1 || num > thestat->Length()) return Standard_False;
203   Standard_Integer stat = thestat->Value(num);
204   return (stat == 2);
205 }
206
207 Standard_Boolean  IFSelect_ListEditor::IsTouched () const
208       {  return (thetouc != 0);  }