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.
14 #include <IFSelect_EditForm.ixx>
15 #include <Interface_TypedValue.hxx>
16 #include <Interface_MSG.hxx>
17 #include <Message_Messenger.hxx>
18 #include <Message.hxx>
19 #include <TCollection_HAsciiString.hxx>
20 #include <TColStd_HSequenceOfHAsciiString.hxx>
22 IFSelect_EditForm::IFSelect_EditForm
23 (const Handle(IFSelect_Editor)& editor,
24 const Standard_Boolean readonly, const Standard_Boolean undoable,
25 const Standard_CString label)
26 : thecomplete (Standard_True) ,
27 theloaded (Standard_False) ,
28 thekeepst (Standard_False) ,
31 theorigs (0, (undoable ? editor->NbValues() : 0) ) ,
32 themodifs (0, (readonly ? 0 : editor->NbValues()) ) ,
33 thestatus (0, (readonly ? 0 : editor->NbValues()) ) ,
37 IFSelect_EditForm::IFSelect_EditForm
38 (const Handle(IFSelect_Editor)& editor,
39 const TColStd_SequenceOfInteger& nums,
40 const Standard_Boolean readonly, const Standard_Boolean undoable,
41 const Standard_CString label)
42 : thecomplete (Standard_False) ,
43 theloaded (Standard_False) ,
44 thekeepst (Standard_False) ,
46 thenums (0,nums.Length()) ,
47 theorigs (0, (undoable ? nums.Length() : 0) ) ,
48 themodifs (0, (readonly ? 0 : nums.Length()) ) ,
49 thestatus (0, (readonly ? 0 : nums.Length()) ) ,
53 Standard_Integer i,nb = nums.Length();
54 for (i = 1; i <= nb; i ++) thenums.SetValue (i,nums.Value(i));
57 Standard_Boolean& IFSelect_EditForm::EditKeepStatus ()
60 Standard_CString IFSelect_EditForm::Label () const
61 { return thelabel.ToCString(); }
63 Standard_Boolean IFSelect_EditForm::IsLoaded () const
66 void IFSelect_EditForm::ClearData ()
67 { theent.Nullify(); themodel.Nullify(); theloaded = Standard_False; }
69 void IFSelect_EditForm::SetData
70 (const Handle(Standard_Transient)& ent,
71 const Handle(Interface_InterfaceModel)& model)
72 { theent = ent; themodel = model; }
74 void IFSelect_EditForm::SetEntity
75 (const Handle(Standard_Transient)& ent)
78 void IFSelect_EditForm::SetModel
79 (const Handle(Interface_InterfaceModel)& model)
82 Handle(Standard_Transient) IFSelect_EditForm::Entity () const
85 Handle(Interface_InterfaceModel) IFSelect_EditForm::Model () const
88 Handle(IFSelect_Editor) IFSelect_EditForm::Editor () const
91 Standard_Boolean IFSelect_EditForm::IsComplete () const
92 { return thecomplete; }
94 Standard_Integer IFSelect_EditForm::NbValues
95 (const Standard_Boolean editable) const
97 if (!editable || thecomplete) return theeditor->NbValues();
98 return thenums.Upper();
101 Standard_Integer IFSelect_EditForm::NumberFromRank
102 (const Standard_Integer rank) const
104 if (thecomplete) return rank;
105 if (rank < 1 || rank > thenums.Upper()) return 0;
106 return thenums.Value(rank);
109 Standard_Integer IFSelect_EditForm::RankFromNumber
110 (const Standard_Integer num) const
112 if (thecomplete) return num;
113 Standard_Integer i, n = thenums.Upper();
114 for (i = 1; i <= n; i ++) {
115 if (thenums.Value(i) == num) return i;
120 Standard_Integer IFSelect_EditForm::NameNumber
121 (const Standard_CString name) const
123 Standard_Integer res = theeditor->NameNumber(name);
124 if (thecomplete || res == 0) return res;
125 // Sinon, chercher res dans thenums
126 Standard_Integer i, nb = thenums.Length();
127 for (i = 1; i <= nb; i ++) {
128 if (res == thenums.Value(i)) return res;
133 Standard_Integer IFSelect_EditForm::NameRank
134 (const Standard_CString name) const
136 Standard_Integer res = theeditor->NameNumber(name);
137 if (thecomplete || res == 0) return res;
138 // Sinon, chercher res dans thenums
139 Standard_Integer i, nb = thenums.Length();
140 for (i = 1; i <= nb; i ++) {
141 if (res == thenums.Value(i)) return i;
147 void IFSelect_EditForm::LoadDefault ()
149 theloaded = Standard_True;
151 Standard_Integer i,nb = theorigs.Upper();
153 for (i = 1; i <= nb; i ++) {
154 Standard_Integer num = NumberFromRank(i);
155 if (num == 0) continue;
156 Handle(TCollection_HAsciiString) str = theeditor->StringValue (this,num);
157 theorigs.SetValue (i,str);
162 Standard_Boolean IFSelect_EditForm::LoadData
163 (const Handle(Standard_Transient)& ent,
164 const Handle(Interface_InterfaceModel)& model)
167 if (!theeditor->Load (this,ent,model)) return Standard_False;
169 theloaded = Standard_True;
170 return Standard_True;
173 Standard_Boolean IFSelect_EditForm::LoadEntity
174 (const Handle(Standard_Transient)& ent)
177 Handle(Interface_InterfaceModel) model;
178 if (!theeditor->Load (this,ent,model)) return Standard_False;
180 theloaded = Standard_True;
181 return Standard_True;
184 Standard_Boolean IFSelect_EditForm::LoadModel
185 (const Handle(Interface_InterfaceModel)& model)
188 Handle(Standard_Transient) ent;
189 if (!theeditor->Load (this,ent,model)) return Standard_False;
191 theloaded = Standard_True;
192 return Standard_True;
195 Standard_Boolean IFSelect_EditForm::LoadData ()
198 Handle(Interface_InterfaceModel) model;
199 Handle(Standard_Transient) ent;
200 if (!theeditor->Load (this,ent,model)) return Standard_False;
201 theloaded = Standard_True;
202 return Standard_True;
206 // ######## VALUES ########
208 Handle(IFSelect_ListEditor) IFSelect_EditForm::ListEditor
209 (const Standard_Integer num) const
211 Standard_Integer n = RankFromNumber(num);
212 Handle(IFSelect_ListEditor) led;
213 if (n <= 0 || n > theorigs.Upper()) return led;
214 if (!theeditor->IsList(n)) return led;
215 led = theeditor->ListEditor (num);
216 Handle(TColStd_HSequenceOfHAsciiString) lis = theeditor->ListValue(this,num);
217 led->LoadModel (themodel);
218 led->LoadValues (lis);
222 void IFSelect_EditForm::LoadValue
223 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& val)
225 Standard_Integer n = RankFromNumber(num);
226 if (n <= 0 || n > theorigs.Upper()) return;
227 theorigs.SetValue (n,val);
230 void IFSelect_EditForm::LoadList
231 (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& list)
233 Standard_Integer n = RankFromNumber(num);
234 if (n <= 0 || n > theorigs.Upper()) return;
235 theorigs.SetValue (n,list);
239 Handle(TCollection_HAsciiString) IFSelect_EditForm::OriginalValue
240 (const Standard_Integer num) const
242 Standard_Integer n = RankFromNumber(num);
243 Handle(TCollection_HAsciiString) val;
244 if (theorigs.Upper() == 0) return theeditor->StringValue (this,num);
245 else return Handle(TCollection_HAsciiString)::DownCast(theorigs.Value(n));
248 Handle(TColStd_HSequenceOfHAsciiString) IFSelect_EditForm::OriginalList
249 (const Standard_Integer num) const
251 Standard_Integer n = RankFromNumber(num);
252 Handle(TColStd_HSequenceOfHAsciiString) list;
253 if (theorigs.Upper() == 0) return theeditor->ListValue (this,num);
254 else return Handle(TColStd_HSequenceOfHAsciiString)::DownCast(theorigs.Value(n));
257 Handle(TCollection_HAsciiString) IFSelect_EditForm::EditedValue
258 (const Standard_Integer num) const
260 if (themodifs.Upper() == 0) return OriginalValue(num);
261 if (!IsModified(num)) return OriginalValue(num);
262 Standard_Integer n = RankFromNumber(num);
263 return Handle(TCollection_HAsciiString)::DownCast(themodifs.Value(n));
266 Handle(TColStd_HSequenceOfHAsciiString) IFSelect_EditForm::EditedList
267 (const Standard_Integer num) const
269 if (themodifs.Upper() == 0) return OriginalList(num);
270 if (!IsModified(num)) return OriginalList(num);
271 Standard_Integer n = RankFromNumber(num);
272 return Handle(TColStd_HSequenceOfHAsciiString)::DownCast(themodifs.Value(n));
276 Standard_Boolean IFSelect_EditForm::IsModified
277 (const Standard_Integer num) const
279 if (thestatus.Upper() == 0) return Standard_False;
280 Standard_Integer n = RankFromNumber(num);
281 return (thestatus.Value(n) != 0);
284 Standard_Boolean IFSelect_EditForm::IsTouched
285 (const Standard_Integer num) const
287 if (thestatus.Upper() == 0) return Standard_False;
288 Standard_Integer n = RankFromNumber(num);
289 return (thestatus.Value(n) == 2);
292 Standard_Boolean IFSelect_EditForm::Modify
293 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& newval,
294 const Standard_Boolean enforce)
298 if (themodifs.Upper() == 0) return Standard_False;
299 Standard_Integer tnum = RankFromNumber(num);
300 if (tnum == 0) return Standard_False;
301 IFSelect_EditValue acc = theeditor->EditMode (num);
302 if (newval.IsNull() && acc != IFSelect_Optional) return Standard_False;
303 if (!enforce && (acc == IFSelect_EditProtected || acc == IFSelect_EditComputed)) return Standard_False;
306 Handle(Interface_TypedValue) typval = theeditor->TypedValue(num);
307 if (!typval->Satisfies(newval)) return Standard_False;
308 Interface_ParamType pty = typval->Type();
309 if (pty == Interface_ParamIdent && !newval.IsNull()) {
310 if (themodel.IsNull()) return Standard_False;
311 if (themodel->NextNumberForLabel(newval->ToCString(),0,Standard_False) <= 0)
312 return Standard_False;
316 if (!theeditor->Update(this,num,newval,enforce)) return Standard_False;
318 thestatus.SetValue (tnum,1);
319 themodifs.SetValue (tnum,newval);
320 return Standard_True;
323 Standard_Boolean IFSelect_EditForm::ModifyList
324 (const Standard_Integer num, const Handle(IFSelect_ListEditor)& edited,
325 const Standard_Boolean enforce)
328 if (edited.IsNull()) return Standard_False;
329 if (!edited->IsTouched()) return Standard_False;
330 Handle(TColStd_HSequenceOfHAsciiString) newlist = edited->EditedValues();
334 if (themodifs.Upper() == 0) return Standard_False;
335 Standard_Integer tnum = RankFromNumber(num);
336 if (tnum == 0) return Standard_False;
337 IFSelect_EditValue acc = theeditor->EditMode (num);
338 if (acc == IFSelect_EditRead || acc == IFSelect_EditDynamic) return Standard_False;
339 if (newlist.IsNull() && acc != IFSelect_Optional) return Standard_False;
340 if (!enforce && (acc == IFSelect_EditProtected || acc == IFSelect_EditComputed)) return Standard_False;
343 if (!theeditor->UpdateList(this,num,newlist,enforce)) return Standard_False;
345 thestatus.SetValue (tnum,1);
346 themodifs.SetValue (tnum,newlist);
347 return Standard_True;
350 Standard_Boolean IFSelect_EditForm::ModifyListValue
351 (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& list,
352 const Standard_Boolean enforce)
354 Handle(IFSelect_ListEditor) led = ListEditor (num);
355 if (led.IsNull()) return Standard_False;
356 if (!led->LoadEdited(list)) return Standard_False;
357 return ModifyList (num,led,enforce);
361 Standard_Boolean IFSelect_EditForm::Touch
362 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& newval)
364 if (themodifs.Upper() == 0) return Standard_False;
365 Standard_Integer tnum = RankFromNumber(num);
366 if (tnum == 0) return Standard_False;
368 thestatus.SetValue (tnum,2);
369 themodifs.SetValue (tnum,newval);
371 return Standard_True;
374 Standard_Boolean IFSelect_EditForm::TouchList
375 (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& newlist)
377 if (themodifs.Upper() == 0) return Standard_False;
378 Standard_Integer tnum = RankFromNumber(num);
379 if (tnum == 0) return Standard_False;
381 thestatus.SetValue (tnum,2);
382 themodifs.SetValue (tnum,newlist);
384 return Standard_True;
388 void IFSelect_EditForm::ClearEdit (const Standard_Integer num)
390 Standard_Integer i, nb = thestatus.Upper();
392 for (i = 1; i <= nb; i ++) thestatus.SetValue (i,0);
394 Standard_Integer tnum = RankFromNumber(num);
395 if (tnum > 0 && num <= nb) thestatus.SetValue (tnum,0);
400 void IFSelect_EditForm::PrintDefs (const Handle(Message_Messenger)& S) const
402 Standard_Integer iv, nbv = NbValues(Standard_True);
403 S<<"***** EditForm, Label : "<<Label()<<endl;
404 if (IsComplete()) S<<"Complete, "<<nbv<<" Values"<<endl;
406 S<<"Extraction on "<<nbv<<" Values : (extracted<-editor)"<<endl;
407 for (iv = 1; iv <= nbv; iv ++) S<<" "<<iv<<"<-"<<NumberFromRank(iv);
414 static void PrintList
415 (const Handle(TColStd_HSequenceOfHAsciiString)& list,
416 const Handle(Message_Messenger)& S, const Standard_Boolean alsolist)
418 if (list.IsNull()) { S<<"(NULL LIST)"<<endl; return; }
420 Standard_Integer i,nb = list->Length();
421 S<<"(List : "<<nb<<" Items)"<<endl;
422 if (!alsolist) return;
424 for (i = 1; i <= nb; i ++) {
425 Handle(TCollection_HAsciiString) str = list->Value(i);
426 S<<" ["<<i<<"] "<< (str.IsNull() ? "(NULL)" : str->ToCString())<<endl;
430 void IFSelect_EditForm::PrintValues
431 (const Handle(Message_Messenger)& S, const Standard_Integer what,
432 const Standard_Boolean names, const Standard_Boolean alsolist) const
434 Standard_Integer iv, nbv = NbValues(Standard_True);
435 S<< "****************************************************"<<endl;
436 S<<"***** "<<Label()<<Interface_MSG::Blanks(Label(),40)<<"*****"<<endl;
437 S<<"***** *****"<<endl;
439 S<<"***** Values are NOT loaded *****"<<endl;
442 // Donnees sur lesquelles on a travaille
443 if (themodel.IsNull()) {
444 if (theent.IsNull()) S<<"***** No loaded data";
445 else S<<"***** No loaded Model. Loaded object : type "<<theent->DynamicType()->Name();
447 if (theent.IsNull()) S<<"***** No loaded entity";
448 else { S<<"***** Loaded entity : "; themodel->PrintLabel (theent,S); }
451 S<<endl<<"****************************************************"<<endl<<endl;
453 // Affichage des valeurs
454 Standard_Boolean nams = names;
455 Standard_Integer maxnam = theeditor->MaxNameLength (names ? 0 : -1);
456 if (maxnam == 0) { maxnam = theeditor->MaxNameLength (0); nams = Standard_True; }
457 Standard_Integer nbmod = 0;
458 if (what != 0) S<<"Mod N0 Name Value"<<endl;
459 else S<<" N0 Name Value"<<endl;
461 for (iv = 1; iv <= nbv; iv ++) {
462 Standard_Integer jv = NumberFromRank(iv);
463 Standard_CString name = theeditor->Name(jv,!nams);
467 Handle(TCollection_HAsciiString) str;
468 if (IsModified(jv)) S<<"* ";
470 S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
471 <<name<<Interface_MSG::Blanks(name,maxnam)<<" ";
473 if (theeditor->IsList(jv)) {
474 Handle(TColStd_HSequenceOfHAsciiString) list;
475 if (what < 0) list = OriginalList (jv);
476 if (what > 0) list = EditedList (jv);
477 PrintList (list,S,alsolist);
481 if (what < 0) str = OriginalValue (jv);
482 if (what > 0) str = EditedValue (jv);
484 S << (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
488 if (!IsModified(jv)) continue;
490 if (theeditor->IsList(jv)) {
491 Handle(TColStd_HSequenceOfHAsciiString) list= OriginalList (jv);
492 S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
493 <<name<<Interface_MSG::Blanks(name,maxnam)<<" ORIG:";
494 PrintList (list,S,alsolist);
496 list = EditedList (jv);
497 S<<Interface_MSG::Blanks("",maxnam+5)<<"MOD :";
498 PrintList (list,S,alsolist);
503 Handle(TCollection_HAsciiString) str = OriginalValue (jv);
504 S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
505 <<name<<Interface_MSG::Blanks(name,maxnam)<<" ORIG:"
506 << (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
507 str = EditedValue (jv);
508 S<<Interface_MSG::Blanks("",maxnam+4)<<" MOD :"<< (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
511 if (what == 0) S<<"On "<<nbv<<" Values, "<<nbmod<<" Modified"<<endl;
515 Standard_Boolean IFSelect_EditForm::Apply ()
517 Standard_Boolean stat = ApplyData(theent,themodel);
518 if (stat && !thekeepst) ClearEdit();
523 Standard_Boolean IFSelect_EditForm::Recognize () const
524 { return theeditor->Recognize(this); }
526 Standard_Boolean IFSelect_EditForm::ApplyData
527 (const Handle(Standard_Transient)& ent,
528 const Handle(Interface_InterfaceModel)& model)
529 { return theeditor->Apply (this,ent,model); }
532 Standard_Boolean IFSelect_EditForm::Undo ()
534 if (thestatus.Upper() == 0 || theorigs.Upper() == 0) return Standard_False;
535 Standard_Integer i, nb = thestatus.Upper();
536 for (i = 1; i <= nb; i ++) {
537 if (thestatus.Value (i) != 0) themodifs.SetValue (i,theorigs.Value(i));