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