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