1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
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>
21 IMPLEMENT_STANDARD_RTTIEXT(IFSelect_ListEditor,Standard_Transient)
23 IFSelect_ListEditor::IFSelect_ListEditor ()
24 : themax (0) , thetouc (0) { }
26 IFSelect_ListEditor::IFSelect_ListEditor
27 (const Handle(Interface_TypedValue)& def, const Standard_Integer max)
28 : themax (max) , thedef (def) , thetouc (0) { }
31 void IFSelect_ListEditor::LoadModel (const Handle(Interface_InterfaceModel)& model)
34 void IFSelect_ListEditor::LoadValues (const Handle(TColStd_HSequenceOfHAsciiString)& vals)
41 void IFSelect_ListEditor::SetTouched ()
44 void IFSelect_ListEditor::ClearEdit ()
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));
57 // ######## CHECK ########
59 static Standard_Boolean CheckValue
60 (const Handle(TCollection_HAsciiString)& val,
61 const Handle(Interface_InterfaceModel)& modl,
62 const Handle(Interface_TypedValue)& thedef)
64 if (val.IsNull() || modl.IsNull() || thedef.IsNull()) return Standard_True;
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;
75 // ######## EDITION ########
77 Standard_Boolean IFSelect_ListEditor::LoadEdited
78 (const Handle(TColStd_HSequenceOfHAsciiString)& list)
80 if (list.IsNull()) return Standard_False;
81 Standard_Integer i, nb = list->Length();
82 if (nb > themax) return Standard_False;
85 if (!thedef.IsNull()) {
86 for (i = 1; i <= nb; i ++) {
87 Handle(TCollection_HAsciiString) newval = list->Value(i);
88 if (!CheckValue (newval,themodl,thedef)) return Standard_False;
94 thestat = new TColStd_HSequenceOfInteger();
95 for (i = 1; i <= nb; i ++) thestat->Append (1);
102 Standard_Boolean IFSelect_ListEditor::SetValue
103 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& val)
105 if (theedit.IsNull()) return Standard_False;
106 if (num < 1 || num > theedit->Length()) return Standard_False;
109 if (!CheckValue(val,themodl,thedef)) return Standard_False;
112 theedit->SetValue (num,val);
113 thestat->SetValue (num,1);
115 return Standard_True;
119 Standard_Boolean IFSelect_ListEditor::AddValue
120 (const Handle(TCollection_HAsciiString)& val, const Standard_Integer atnum)
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;
126 theedit->InsertBefore (atnum,val);
127 thestat->InsertBefore (atnum,2);
129 theedit->Append (val);
133 return Standard_True;
137 Standard_Boolean IFSelect_ListEditor::Remove
138 (const Standard_Integer num, const Standard_Integer howmany)
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);
145 if ((num+howmany) > nb) return Standard_False;
146 theedit->Remove(num,howmany);
147 thestat->Remove(num,howmany);
149 return Standard_True;
153 // ######## QUERIES ########
155 Handle(TColStd_HSequenceOfHAsciiString) IFSelect_ListEditor::OriginalValues () const
158 Handle(TColStd_HSequenceOfHAsciiString) IFSelect_ListEditor::EditedValues () const
162 Standard_Integer IFSelect_ListEditor::NbValues (const Standard_Boolean edited) const
164 if (edited) return (theedit.IsNull() ? 0 : theedit->Length());
165 return (theorig.IsNull() ? 0 : theorig->Length());
169 Handle(TCollection_HAsciiString) IFSelect_ListEditor::Value
170 (const Standard_Integer num, const Standard_Boolean edited) const
172 Handle(TCollection_HAsciiString) val;
174 if (theedit.IsNull()) return val;
175 if (num < 1 || num > theedit->Length()) return val;
176 val = theedit->Value(num);
178 if (theorig.IsNull()) return val;
179 if (num < 1 || num > theorig->Length()) return val;
180 val = theorig->Value(num);
185 Standard_Boolean IFSelect_ListEditor::IsChanged (const Standard_Integer num) const
187 if (thestat.IsNull()) return Standard_False;
188 if (num < 1 || num > thestat->Length()) return Standard_False;
189 Standard_Integer stat = thestat->Value(num);
193 Standard_Boolean IFSelect_ListEditor::IsModified (const Standard_Integer num) const
195 if (thestat.IsNull()) return Standard_False;
196 if (num < 1 || num > thestat->Length()) return Standard_False;
197 Standard_Integer stat = thestat->Value(num);
201 Standard_Boolean IFSelect_ListEditor::IsAdded (const Standard_Integer num) const
203 if (thestat.IsNull()) return Standard_False;
204 if (num < 1 || num > thestat->Length()) return Standard_False;
205 Standard_Integer stat = thestat->Value(num);
209 Standard_Boolean IFSelect_ListEditor::IsTouched () const
210 { return (thetouc != 0); }