1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 #include <IFSelect_EditForm.ixx>
19 #include <Interface_TypedValue.hxx>
20 #include <Interface_MSG.hxx>
21 #include <Message_Messenger.hxx>
22 #include <Message.hxx>
24 IFSelect_EditForm::IFSelect_EditForm
25 (const Handle(IFSelect_Editor)& editor,
26 const Standard_Boolean readonly, const Standard_Boolean undoable,
27 const Standard_CString label)
28 : thecomplete (Standard_True) ,
29 theloaded (Standard_False) ,
30 thekeepst (Standard_False) ,
33 theorigs (0, (undoable ? editor->NbValues() : 0) ) ,
34 themodifs (0, (readonly ? 0 : editor->NbValues()) ) ,
35 thestatus (0, (readonly ? 0 : editor->NbValues()) ) ,
39 IFSelect_EditForm::IFSelect_EditForm
40 (const Handle(IFSelect_Editor)& editor,
41 const TColStd_SequenceOfInteger& nums,
42 const Standard_Boolean readonly, const Standard_Boolean undoable,
43 const Standard_CString label)
44 : thecomplete (Standard_False) ,
45 theloaded (Standard_False) ,
46 thekeepst (Standard_False) ,
48 thenums (0,nums.Length()) ,
49 theorigs (0, (undoable ? nums.Length() : 0) ) ,
50 themodifs (0, (readonly ? 0 : nums.Length()) ) ,
51 thestatus (0, (readonly ? 0 : nums.Length()) ) ,
55 Standard_Integer i,nb = nums.Length();
56 for (i = 1; i <= nb; i ++) thenums.SetValue (i,nums.Value(i));
59 Standard_Boolean& IFSelect_EditForm::EditKeepStatus ()
62 Standard_CString IFSelect_EditForm::Label () const
63 { return thelabel.ToCString(); }
65 Standard_Boolean IFSelect_EditForm::IsLoaded () const
68 void IFSelect_EditForm::ClearData ()
69 { theent.Nullify(); themodel.Nullify(); theloaded = Standard_False; }
71 void IFSelect_EditForm::SetData
72 (const Handle(Standard_Transient)& ent,
73 const Handle(Interface_InterfaceModel)& model)
74 { theent = ent; themodel = model; }
76 void IFSelect_EditForm::SetEntity
77 (const Handle(Standard_Transient)& ent)
80 void IFSelect_EditForm::SetModel
81 (const Handle(Interface_InterfaceModel)& model)
84 Handle(Standard_Transient) IFSelect_EditForm::Entity () const
87 Handle(Interface_InterfaceModel) IFSelect_EditForm::Model () const
90 Handle(IFSelect_Editor) IFSelect_EditForm::Editor () const
93 Standard_Boolean IFSelect_EditForm::IsComplete () const
94 { return thecomplete; }
96 Standard_Integer IFSelect_EditForm::NbValues
97 (const Standard_Boolean editable) const
99 if (!editable || thecomplete) return theeditor->NbValues();
100 return thenums.Upper();
103 Standard_Integer IFSelect_EditForm::NumberFromRank
104 (const Standard_Integer rank) const
106 if (thecomplete) return rank;
107 if (rank < 1 || rank > thenums.Upper()) return 0;
108 return thenums.Value(rank);
111 Standard_Integer IFSelect_EditForm::RankFromNumber
112 (const Standard_Integer num) const
114 if (thecomplete) return num;
115 Standard_Integer i, n = thenums.Upper();
116 for (i = 1; i <= n; i ++) {
117 if (thenums.Value(i) == num) return i;
122 Standard_Integer IFSelect_EditForm::NameNumber
123 (const Standard_CString name) const
125 Standard_Integer res = theeditor->NameNumber(name);
126 if (thecomplete || res == 0) return res;
127 // Sinon, chercher res dans thenums
128 Standard_Integer i, nb = thenums.Length();
129 for (i = 1; i <= nb; i ++) {
130 if (res == thenums.Value(i)) return res;
135 Standard_Integer IFSelect_EditForm::NameRank
136 (const Standard_CString name) const
138 Standard_Integer res = theeditor->NameNumber(name);
139 if (thecomplete || res == 0) return res;
140 // Sinon, chercher res dans thenums
141 Standard_Integer i, nb = thenums.Length();
142 for (i = 1; i <= nb; i ++) {
143 if (res == thenums.Value(i)) return i;
149 void IFSelect_EditForm::LoadDefault ()
151 theloaded = Standard_True;
153 Standard_Integer i,nb = theorigs.Upper();
155 for (i = 1; i <= nb; i ++) {
156 Standard_Integer num = NumberFromRank(i);
157 if (num == 0) continue;
158 Handle(TCollection_HAsciiString) str = theeditor->StringValue (this,num);
159 theorigs.SetValue (i,str);
164 Standard_Boolean IFSelect_EditForm::LoadData
165 (const Handle(Standard_Transient)& ent,
166 const Handle(Interface_InterfaceModel)& model)
169 if (!theeditor->Load (this,ent,model)) return Standard_False;
171 theloaded = Standard_True;
172 return Standard_True;
175 Standard_Boolean IFSelect_EditForm::LoadEntity
176 (const Handle(Standard_Transient)& ent)
179 Handle(Interface_InterfaceModel) model;
180 if (!theeditor->Load (this,ent,model)) return Standard_False;
182 theloaded = Standard_True;
183 return Standard_True;
186 Standard_Boolean IFSelect_EditForm::LoadModel
187 (const Handle(Interface_InterfaceModel)& model)
190 Handle(Standard_Transient) ent;
191 if (!theeditor->Load (this,ent,model)) return Standard_False;
193 theloaded = Standard_True;
194 return Standard_True;
197 Standard_Boolean IFSelect_EditForm::LoadData ()
200 Handle(Interface_InterfaceModel) model;
201 Handle(Standard_Transient) ent;
202 if (!theeditor->Load (this,ent,model)) return Standard_False;
203 theloaded = Standard_True;
204 return Standard_True;
208 // ######## VALUES ########
210 Handle(IFSelect_ListEditor) IFSelect_EditForm::ListEditor
211 (const Standard_Integer num) const
213 Standard_Integer n = RankFromNumber(num);
214 Handle(IFSelect_ListEditor) led;
215 if (n <= 0 || n > theorigs.Upper()) return led;
216 if (!theeditor->IsList(n)) return led;
217 led = theeditor->ListEditor (num);
218 Handle(TColStd_HSequenceOfHAsciiString) lis = theeditor->ListValue(this,num);
219 led->LoadModel (themodel);
220 led->LoadValues (lis);
224 void IFSelect_EditForm::LoadValue
225 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& val)
227 Standard_Integer n = RankFromNumber(num);
228 if (n <= 0 || n > theorigs.Upper()) return;
229 theorigs.SetValue (n,val);
232 void IFSelect_EditForm::LoadList
233 (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& list)
235 Standard_Integer n = RankFromNumber(num);
236 if (n <= 0 || n > theorigs.Upper()) return;
237 theorigs.SetValue (n,list);
241 Handle(TCollection_HAsciiString) IFSelect_EditForm::OriginalValue
242 (const Standard_Integer num) const
244 Standard_Integer n = RankFromNumber(num);
245 Handle(TCollection_HAsciiString) val;
246 if (theorigs.Upper() == 0) return theeditor->StringValue (this,num);
247 else return Handle(TCollection_HAsciiString)::DownCast(theorigs.Value(n));
250 Handle(TColStd_HSequenceOfHAsciiString) IFSelect_EditForm::OriginalList
251 (const Standard_Integer num) const
253 Standard_Integer n = RankFromNumber(num);
254 Handle(TColStd_HSequenceOfHAsciiString) list;
255 if (theorigs.Upper() == 0) return theeditor->ListValue (this,num);
256 else return Handle(TColStd_HSequenceOfHAsciiString)::DownCast(theorigs.Value(n));
259 Handle(TCollection_HAsciiString) IFSelect_EditForm::EditedValue
260 (const Standard_Integer num) const
262 if (themodifs.Upper() == 0) return OriginalValue(num);
263 if (!IsModified(num)) return OriginalValue(num);
264 Standard_Integer n = RankFromNumber(num);
265 return Handle(TCollection_HAsciiString)::DownCast(themodifs.Value(n));
268 Handle(TColStd_HSequenceOfHAsciiString) IFSelect_EditForm::EditedList
269 (const Standard_Integer num) const
271 if (themodifs.Upper() == 0) return OriginalList(num);
272 if (!IsModified(num)) return OriginalList(num);
273 Standard_Integer n = RankFromNumber(num);
274 return Handle(TColStd_HSequenceOfHAsciiString)::DownCast(themodifs.Value(n));
278 Standard_Boolean IFSelect_EditForm::IsModified
279 (const Standard_Integer num) const
281 if (thestatus.Upper() == 0) return Standard_False;
282 Standard_Integer n = RankFromNumber(num);
283 return (thestatus.Value(n) != 0);
286 Standard_Boolean IFSelect_EditForm::IsTouched
287 (const Standard_Integer num) const
289 if (thestatus.Upper() == 0) return Standard_False;
290 Standard_Integer n = RankFromNumber(num);
291 return (thestatus.Value(n) == 2);
294 Standard_Boolean IFSelect_EditForm::Modify
295 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& newval,
296 const Standard_Boolean enforce)
300 if (themodifs.Upper() == 0) return Standard_False;
301 Standard_Integer tnum = RankFromNumber(num);
302 if (tnum == 0) return Standard_False;
303 IFSelect_EditValue acc = theeditor->EditMode (num);
304 if (newval.IsNull() && acc != IFSelect_Optional) return Standard_False;
305 if (!enforce && (acc == IFSelect_EditProtected || acc == IFSelect_EditComputed)) return Standard_False;
308 Handle(Interface_TypedValue) typval = theeditor->TypedValue(num);
309 if (!typval->Satisfies(newval)) return Standard_False;
310 Interface_ParamType pty = typval->Type();
311 if (pty == Interface_ParamIdent && !newval.IsNull()) {
312 if (themodel.IsNull()) return Standard_False;
313 if (themodel->NextNumberForLabel(newval->ToCString(),0,Standard_False) <= 0)
314 return Standard_False;
318 if (!theeditor->Update(this,num,newval,enforce)) return Standard_False;
320 thestatus.SetValue (tnum,1);
321 themodifs.SetValue (tnum,newval);
322 return Standard_True;
325 Standard_Boolean IFSelect_EditForm::ModifyList
326 (const Standard_Integer num, const Handle(IFSelect_ListEditor)& edited,
327 const Standard_Boolean enforce)
330 if (edited.IsNull()) return Standard_False;
331 if (!edited->IsTouched()) return Standard_False;
332 Handle(TColStd_HSequenceOfHAsciiString) newlist = edited->EditedValues();
336 if (themodifs.Upper() == 0) return Standard_False;
337 Standard_Integer tnum = RankFromNumber(num);
338 if (tnum == 0) return Standard_False;
339 IFSelect_EditValue acc = theeditor->EditMode (num);
340 if (acc == IFSelect_EditRead || acc == IFSelect_EditDynamic) return Standard_False;
341 if (newlist.IsNull() && acc != IFSelect_Optional) return Standard_False;
342 if (!enforce && (acc == IFSelect_EditProtected || acc == IFSelect_EditComputed)) return Standard_False;
345 if (!theeditor->UpdateList(this,num,newlist,enforce)) return Standard_False;
347 thestatus.SetValue (tnum,1);
348 themodifs.SetValue (tnum,newlist);
349 return Standard_True;
352 Standard_Boolean IFSelect_EditForm::ModifyListValue
353 (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& list,
354 const Standard_Boolean enforce)
356 Handle(IFSelect_ListEditor) led = ListEditor (num);
357 if (led.IsNull()) return Standard_False;
358 if (!led->LoadEdited(list)) return Standard_False;
359 return ModifyList (num,led,enforce);
363 Standard_Boolean IFSelect_EditForm::Touch
364 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& newval)
366 if (themodifs.Upper() == 0) return Standard_False;
367 Standard_Integer tnum = RankFromNumber(num);
368 if (tnum == 0) return Standard_False;
370 thestatus.SetValue (tnum,2);
371 themodifs.SetValue (tnum,newval);
373 return Standard_True;
376 Standard_Boolean IFSelect_EditForm::TouchList
377 (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& newlist)
379 if (themodifs.Upper() == 0) return Standard_False;
380 Standard_Integer tnum = RankFromNumber(num);
381 if (tnum == 0) return Standard_False;
383 thestatus.SetValue (tnum,2);
384 themodifs.SetValue (tnum,newlist);
386 return Standard_True;
390 void IFSelect_EditForm::ClearEdit (const Standard_Integer num)
392 Standard_Integer i, nb = thestatus.Upper();
394 for (i = 1; i <= nb; i ++) thestatus.SetValue (i,0);
396 Standard_Integer tnum = RankFromNumber(num);
397 if (tnum > 0 && num <= nb) thestatus.SetValue (tnum,0);
402 void IFSelect_EditForm::PrintDefs (const Handle(Message_Messenger)& S) const
404 Standard_Integer iv, nbv = NbValues(Standard_True);
405 S<<"***** EditForm, Label : "<<Label()<<endl;
406 if (IsComplete()) S<<"Complete, "<<nbv<<" Values"<<endl;
408 S<<"Extraction on "<<nbv<<" Values : (extracted<-editor)"<<endl;
409 for (iv = 1; iv <= nbv; iv ++) S<<" "<<iv<<"<-"<<NumberFromRank(iv);
416 static void PrintList
417 (const Handle(TColStd_HSequenceOfHAsciiString)& list,
418 const Handle(Message_Messenger)& S, const Standard_Boolean alsolist)
420 if (list.IsNull()) { S<<"(NULL LIST)"<<endl; return; }
422 Standard_Integer i,nb = list->Length();
423 S<<"(List : "<<nb<<" Items)"<<endl;
424 if (!alsolist) return;
426 for (i = 1; i <= nb; i ++) {
427 Handle(TCollection_HAsciiString) str = list->Value(i);
428 S<<" ["<<i<<"] "<< (str.IsNull() ? "(NULL)" : str->ToCString())<<endl;
432 void IFSelect_EditForm::PrintValues
433 (const Handle(Message_Messenger)& S, const Standard_Integer what,
434 const Standard_Boolean names, const Standard_Boolean alsolist) const
436 Standard_Integer iv, nbv = NbValues(Standard_True);
437 S<< "****************************************************"<<endl;
438 S<<"***** "<<Label()<<Interface_MSG::Blanks(Label(),40)<<"*****"<<endl;
439 S<<"***** *****"<<endl;
441 S<<"***** Values are NOT loaded *****"<<endl;
444 // Donnees sur lesquelles on a travaille
445 if (themodel.IsNull()) {
446 if (theent.IsNull()) S<<"***** No loaded data";
447 else S<<"***** No loaded Model. Loaded object : type "<<theent->DynamicType()->Name();
449 if (theent.IsNull()) S<<"***** No loaded entity";
450 else { S<<"***** Loaded entity : "; themodel->PrintLabel (theent,S); }
453 S<<endl<<"****************************************************"<<endl<<endl;
455 // Affichage des valeurs
456 Standard_Boolean nams = names;
457 Standard_Integer maxnam = theeditor->MaxNameLength (names ? 0 : -1);
458 if (maxnam == 0) { maxnam = theeditor->MaxNameLength (0); nams = Standard_True; }
459 Standard_Integer nbmod = 0;
460 if (what != 0) S<<"Mod N0 Name Value"<<endl;
461 else S<<" N0 Name Value"<<endl;
463 for (iv = 1; iv <= nbv; iv ++) {
464 Standard_Integer jv = NumberFromRank(iv);
465 Standard_CString name = theeditor->Name(jv,!nams);
469 Handle(TCollection_HAsciiString) str;
470 if (IsModified(jv)) S<<"* ";
472 S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
473 <<name<<Interface_MSG::Blanks(name,maxnam)<<" ";
475 if (theeditor->IsList(jv)) {
476 Handle(TColStd_HSequenceOfHAsciiString) list;
477 if (what < 0) list = OriginalList (jv);
478 if (what > 0) list = EditedList (jv);
479 PrintList (list,S,alsolist);
483 if (what < 0) str = OriginalValue (jv);
484 if (what > 0) str = EditedValue (jv);
486 S << (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
490 if (!IsModified(jv)) continue;
492 if (theeditor->IsList(jv)) {
493 Handle(TColStd_HSequenceOfHAsciiString) list= OriginalList (jv);
494 S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
495 <<name<<Interface_MSG::Blanks(name,maxnam)<<" ORIG:";
496 PrintList (list,S,alsolist);
498 list = EditedList (jv);
499 S<<Interface_MSG::Blanks("",maxnam+5)<<"MOD :";
500 PrintList (list,S,alsolist);
505 Handle(TCollection_HAsciiString) str = OriginalValue (jv);
506 S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
507 <<name<<Interface_MSG::Blanks(name,maxnam)<<" ORIG:"
508 << (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
509 str = EditedValue (jv);
510 S<<Interface_MSG::Blanks("",maxnam+4)<<" MOD :"<< (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
513 if (what == 0) S<<"On "<<nbv<<" Values, "<<nbmod<<" Modified"<<endl;
517 Standard_Boolean IFSelect_EditForm::Apply ()
519 Standard_Boolean stat = ApplyData(theent,themodel);
520 if (stat && !thekeepst) ClearEdit();
525 Standard_Boolean IFSelect_EditForm::Recognize () const
526 { return theeditor->Recognize(this); }
528 Standard_Boolean IFSelect_EditForm::ApplyData
529 (const Handle(Standard_Transient)& ent,
530 const Handle(Interface_InterfaceModel)& model)
531 { return theeditor->Apply (this,ent,model); }
534 Standard_Boolean IFSelect_EditForm::Undo ()
536 if (thestatus.Upper() == 0 || theorigs.Upper() == 0) return Standard_False;
537 Standard_Integer i, nb = thestatus.Upper();
538 for (i = 1; i <= nb; i ++) {
539 if (thestatus.Value (i) != 0) themodifs.SetValue (i,theorigs.Value(i));