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 
IMPLEMENT_STANDARD_RTTIEXT(IFSelect_ListEditor,Standard_Transient)21 IMPLEMENT_STANDARD_RTTIEXT(IFSelect_ListEditor,Standard_Transient)
22 
23 IFSelect_ListEditor::IFSelect_ListEditor  ()
24 : themax (0) , thetouc (0)  {  }
25 
IFSelect_ListEditor(const Handle (Interface_TypedValue)& def,const Standard_Integer max)26      IFSelect_ListEditor::IFSelect_ListEditor
27   (const Handle(Interface_TypedValue)& def, const Standard_Integer max)
28 : themax (max) , thedef (def) , thetouc (0)    {  }
29 
30 
LoadModel(const Handle (Interface_InterfaceModel)& model)31 void  IFSelect_ListEditor::LoadModel (const Handle(Interface_InterfaceModel)& model)
32 {  themodl = model;  }
33 
LoadValues(const Handle (TColStd_HSequenceOfHAsciiString)& vals)34 void  IFSelect_ListEditor::LoadValues (const Handle(TColStd_HSequenceOfHAsciiString)& vals)
35 {
36   theorig = vals;
37   ClearEdit();
38 }
39 
40 
SetTouched()41 void  IFSelect_ListEditor::SetTouched ()
42 {  thetouc = 1;  }
43 
ClearEdit()44 void  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 
CheckValue(const Handle (TCollection_HAsciiString)& val,const Handle (Interface_InterfaceModel)& modl,const Handle (Interface_TypedValue)& thedef)59 static 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 
LoadEdited(const Handle (TColStd_HSequenceOfHAsciiString)& list)77 Standard_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;
83 
84 //   check values
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;
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 
SetValue(const Standard_Integer num,const Handle (TCollection_HAsciiString)& val)102 Standard_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 
AddValue(const Handle (TCollection_HAsciiString)& val,const Standard_Integer atnum)119 Standard_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 
Remove(const Standard_Integer num,const Standard_Integer howmany)137 Standard_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 
Handle(TColStd_HSequenceOfHAsciiString)155 Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_ListEditor::OriginalValues () const
156 {  return theorig;  }
157 
Handle(TColStd_HSequenceOfHAsciiString)158 Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_ListEditor::EditedValues () const
159 {  return theedit;  }
160 
161 
NbValues(const Standard_Boolean edited) const162 Standard_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 
Handle(TCollection_HAsciiString)169 Handle(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 
IsChanged(const Standard_Integer num) const185 Standard_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 
IsModified(const Standard_Integer num) const193 Standard_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 
IsAdded(const Standard_Integer num) const201 Standard_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 
IsTouched() const209 Standard_Boolean  IFSelect_ListEditor::IsTouched () const
210       {  return (thetouc != 0);  }
211