0024023: Revamp the OCCT Handle -- ambiguity
[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 #include <IFSelect_ListEditor.ixx>
15
16 IFSelect_ListEditor::IFSelect_ListEditor  ()
17 : themax (0) , thetouc (0)  {  }
18
19      IFSelect_ListEditor::IFSelect_ListEditor
20   (const Handle(Interface_TypedValue)& def, const Standard_Integer max)
21 : themax (max) , thedef (def) , thetouc (0)    {  }
22
23
24 void  IFSelect_ListEditor::LoadModel (const Handle(Interface_InterfaceModel)& model)
25 {  themodl = model;  }
26
27 void  IFSelect_ListEditor::LoadValues (const Handle(TColStd_HSequenceOfHAsciiString)& vals)
28 {
29   theorig = vals;
30   ClearEdit();
31 }
32
33
34 void  IFSelect_ListEditor::SetTouched ()
35 {  thetouc = 1;  }
36
37 void  IFSelect_ListEditor::ClearEdit ()
38 {
39   theedit = new TColStd_HSequenceOfHAsciiString();
40   thestat = new TColStd_HSequenceOfInteger();
41   if (theorig.IsNull()) return;
42   Standard_Integer i,nb = theorig->Length();
43   for (i = 1; i <= nb; i ++) {
44     theedit->Append (theorig->Value(i));
45     thestat->Append (0);
46   }
47   thetouc = 0;
48 }
49
50 //  ########    CHECK    ########
51
52 static Standard_Boolean  CheckValue
53   (const Handle(TCollection_HAsciiString)& val,
54    const Handle(Interface_InterfaceModel)& modl,
55    const Handle(Interface_TypedValue)& thedef)
56 {
57   if (val.IsNull() || modl.IsNull() || thedef.IsNull()) return Standard_True;
58
59   Interface_ParamType pty = thedef->Type();
60   if (!thedef->Satisfies(val)) return Standard_False;
61   if (pty == Interface_ParamIdent && !val.IsNull()) {
62     if (modl->NextNumberForLabel(val->ToCString(),0) <= 0)
63       return Standard_False;
64   }
65   return Standard_True;
66 }
67
68 //  ########    EDITION    ########
69
70 Standard_Boolean  IFSelect_ListEditor::LoadEdited
71   (const Handle(TColStd_HSequenceOfHAsciiString)& list)
72 {
73   if (list.IsNull()) return Standard_False;
74   Standard_Integer i, nb = list->Length();
75   if (nb > themax) return Standard_False;
76
77 //   check values
78   if (!thedef.IsNull()) {
79     for (i = 1; i <= nb; i ++) {
80       Handle(TCollection_HAsciiString) newval = list->Value(i);
81       if (!CheckValue (newval,themodl,thedef)) return Standard_False;
82     }
83   }
84
85 //  OK
86   theedit = list;
87   thestat = new TColStd_HSequenceOfInteger();
88   for (i = 1; i <= nb; i ++) thestat->Append (1);
89   thetouc = 1;
90
91   return Standard_True;
92 }
93
94
95 Standard_Boolean  IFSelect_ListEditor::SetValue
96   (const Standard_Integer num, const Handle(TCollection_HAsciiString)& val)
97 {
98   if (theedit.IsNull()) return Standard_False;
99   if (num < 1 || num > theedit->Length()) return Standard_False;
100
101 //   check value
102   if (!CheckValue(val,themodl,thedef)) return Standard_False;
103
104 // OK
105   theedit->SetValue (num,val);
106   thestat->SetValue (num,1);
107   thetouc = 1;
108   return Standard_True;
109 }
110
111
112 Standard_Boolean  IFSelect_ListEditor::AddValue
113   (const Handle(TCollection_HAsciiString)& val, const Standard_Integer atnum)
114 {
115   if (theedit.IsNull()) return Standard_False;
116   if (themax > 0 && theedit->Length() >= themax) return Standard_False;
117   if (!CheckValue (val,themodl,thedef)) return Standard_False;
118   if (atnum > 0) {
119     theedit->InsertBefore (atnum,val);
120     thestat->InsertBefore (atnum,2);
121   } else {
122     theedit->Append (val);
123     thestat->Append (2);
124   }
125   thetouc = 2;
126   return Standard_True;
127 }
128
129
130 Standard_Boolean  IFSelect_ListEditor::Remove
131   (const Standard_Integer num, const Standard_Integer howmany)
132 {
133   if (theedit.IsNull()) return Standard_False;
134   Standard_Integer nb = theedit->Length();
135   if (num < 0) return Standard_False;
136   if (num == 0) return Remove (nb-howmany,howmany);
137
138   if ((num+howmany) > nb) return Standard_False;
139   theedit->Remove(num,howmany);
140   thestat->Remove(num,howmany);
141   thetouc = 3;
142   return Standard_True;
143 }
144
145
146 //  ########    QUERIES    ########
147
148 Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_ListEditor::OriginalValues () const
149 {  return theorig;  }
150
151 Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_ListEditor::EditedValues () const
152 {  return theedit;  }
153
154
155 Standard_Integer  IFSelect_ListEditor::NbValues (const Standard_Boolean edited) const
156 {
157   if (edited) return (theedit.IsNull() ? 0 : theedit->Length());
158   return (theorig.IsNull() ? 0 : theorig->Length());
159 }
160
161
162 Handle(TCollection_HAsciiString)  IFSelect_ListEditor::Value
163   (const Standard_Integer num, const Standard_Boolean edited) const
164 {
165   Handle(TCollection_HAsciiString) val;
166   if (edited) {
167     if (theedit.IsNull()) return val;
168     if (num < 1 || num > theedit->Length()) return val;
169     val = theedit->Value(num);
170   } else {
171     if (theorig.IsNull()) return val;
172     if (num < 1 || num > theorig->Length()) return val;
173     val = theorig->Value(num);
174   }
175   return val;
176 }
177
178 Standard_Boolean  IFSelect_ListEditor::IsChanged (const Standard_Integer num) const
179 {
180   if (thestat.IsNull()) return Standard_False;
181   if (num < 1 || num > thestat->Length()) return Standard_False;
182   Standard_Integer stat = thestat->Value(num);
183   return (stat != 0);
184 }
185
186 Standard_Boolean  IFSelect_ListEditor::IsModified (const Standard_Integer num) const
187 {
188   if (thestat.IsNull()) return Standard_False;
189   if (num < 1 || num > thestat->Length()) return Standard_False;
190   Standard_Integer stat = thestat->Value(num);
191   return (stat == 1);
192 }
193
194 Standard_Boolean  IFSelect_ListEditor::IsAdded (const Standard_Integer num) const
195 {
196   if (thestat.IsNull()) return Standard_False;
197   if (num < 1 || num > thestat->Length()) return Standard_False;
198   Standard_Integer stat = thestat->Value(num);
199   return (stat == 2);
200 }
201
202 Standard_Boolean  IFSelect_ListEditor::IsTouched () const
203       {  return (thetouc != 0);  }