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_EditForm.hxx>
16 #include <IFSelect_Editor.hxx>
17 #include <IFSelect_ListEditor.hxx>
18 #include <Interface_InterfaceModel.hxx>
19 #include <Interface_MSG.hxx>
20 #include <Interface_TypedValue.hxx>
21 #include <Message.hxx>
22 #include <Message_Messenger.hxx>
23 #include <Standard_Transient.hxx>
24 #include <Standard_Type.hxx>
25 #include <TCollection_HAsciiString.hxx>
26 #include <TColStd_HSequenceOfHAsciiString.hxx>
28 IFSelect_EditForm::IFSelect_EditForm
29 (const Handle(IFSelect_Editor)& editor,
30 const Standard_Boolean readonly, const Standard_Boolean undoable,
31 const Standard_CString label)
32 : thecomplete (Standard_True) ,
33 theloaded (Standard_False) ,
34 thekeepst (Standard_False) ,
37 theorigs (0, (undoable ? editor->NbValues() : 0) ) ,
38 themodifs (0, (readonly ? 0 : editor->NbValues()) ) ,
39 thestatus (0, (readonly ? 0 : editor->NbValues()) ) ,
43 IFSelect_EditForm::IFSelect_EditForm
44 (const Handle(IFSelect_Editor)& editor,
45 const TColStd_SequenceOfInteger& nums,
46 const Standard_Boolean readonly, const Standard_Boolean undoable,
47 const Standard_CString label)
48 : thecomplete (Standard_False) ,
49 theloaded (Standard_False) ,
50 thekeepst (Standard_False) ,
52 thenums (0,nums.Length()) ,
53 theorigs (0, (undoable ? nums.Length() : 0) ) ,
54 themodifs (0, (readonly ? 0 : nums.Length()) ) ,
55 thestatus (0, (readonly ? 0 : nums.Length()) ) ,
59 Standard_Integer i,nb = nums.Length();
60 for (i = 1; i <= nb; i ++) thenums.SetValue (i,nums.Value(i));
63 Standard_Boolean& IFSelect_EditForm::EditKeepStatus ()
66 Standard_CString IFSelect_EditForm::Label () const
67 { return thelabel.ToCString(); }
69 Standard_Boolean IFSelect_EditForm::IsLoaded () const
72 void IFSelect_EditForm::ClearData ()
73 { theent.Nullify(); themodel.Nullify(); theloaded = Standard_False; }
75 void IFSelect_EditForm::SetData
76 (const Handle(Standard_Transient)& ent,
77 const Handle(Interface_InterfaceModel)& model)
78 { theent = ent; themodel = model; }
80 void IFSelect_EditForm::SetEntity
81 (const Handle(Standard_Transient)& ent)
84 void IFSelect_EditForm::SetModel
85 (const Handle(Interface_InterfaceModel)& model)
88 Handle(Standard_Transient) IFSelect_EditForm::Entity () const
91 Handle(Interface_InterfaceModel) IFSelect_EditForm::Model () const
94 Handle(IFSelect_Editor) IFSelect_EditForm::Editor () const
97 Standard_Boolean IFSelect_EditForm::IsComplete () const
98 { return thecomplete; }
100 Standard_Integer IFSelect_EditForm::NbValues
101 (const Standard_Boolean editable) const
103 if (!editable || thecomplete) return theeditor->NbValues();
104 return thenums.Upper();
107 Standard_Integer IFSelect_EditForm::NumberFromRank
108 (const Standard_Integer rank) const
110 if (thecomplete) return rank;
111 if (rank < 1 || rank > thenums.Upper()) return 0;
112 return thenums.Value(rank);
115 Standard_Integer IFSelect_EditForm::RankFromNumber
116 (const Standard_Integer num) const
118 if (thecomplete) return num;
119 Standard_Integer i, n = thenums.Upper();
120 for (i = 1; i <= n; i ++) {
121 if (thenums.Value(i) == num) return i;
126 Standard_Integer IFSelect_EditForm::NameNumber
127 (const Standard_CString name) const
129 Standard_Integer res = theeditor->NameNumber(name);
130 if (thecomplete || res == 0) return res;
131 // Sinon, chercher res dans thenums
132 Standard_Integer i, nb = thenums.Length();
133 for (i = 1; i <= nb; i ++) {
134 if (res == thenums.Value(i)) return res;
139 Standard_Integer IFSelect_EditForm::NameRank
140 (const Standard_CString name) const
142 Standard_Integer res = theeditor->NameNumber(name);
143 if (thecomplete || res == 0) return res;
144 // Sinon, chercher res dans thenums
145 Standard_Integer i, nb = thenums.Length();
146 for (i = 1; i <= nb; i ++) {
147 if (res == thenums.Value(i)) return i;
153 void IFSelect_EditForm::LoadDefault ()
155 theloaded = Standard_True;
157 Standard_Integer i,nb = theorigs.Upper();
159 for (i = 1; i <= nb; i ++) {
160 Standard_Integer num = NumberFromRank(i);
161 if (num == 0) continue;
162 Handle(TCollection_HAsciiString) str = theeditor->StringValue (this,num);
163 theorigs.SetValue (i,str);
168 Standard_Boolean IFSelect_EditForm::LoadData
169 (const Handle(Standard_Transient)& ent,
170 const Handle(Interface_InterfaceModel)& model)
173 if (!theeditor->Load (this,ent,model)) return Standard_False;
175 theloaded = Standard_True;
176 return Standard_True;
179 Standard_Boolean IFSelect_EditForm::LoadEntity
180 (const Handle(Standard_Transient)& ent)
183 Handle(Interface_InterfaceModel) model;
184 if (!theeditor->Load (this,ent,model)) return Standard_False;
186 theloaded = Standard_True;
187 return Standard_True;
190 Standard_Boolean IFSelect_EditForm::LoadModel
191 (const Handle(Interface_InterfaceModel)& model)
194 Handle(Standard_Transient) ent;
195 if (!theeditor->Load (this,ent,model)) return Standard_False;
197 theloaded = Standard_True;
198 return Standard_True;
201 Standard_Boolean IFSelect_EditForm::LoadData ()
204 Handle(Interface_InterfaceModel) model;
205 Handle(Standard_Transient) ent;
206 if (!theeditor->Load (this,ent,model)) return Standard_False;
207 theloaded = Standard_True;
208 return Standard_True;
212 // ######## VALUES ########
214 Handle(IFSelect_ListEditor) IFSelect_EditForm::ListEditor
215 (const Standard_Integer num) const
217 Standard_Integer n = RankFromNumber(num);
218 Handle(IFSelect_ListEditor) led;
219 if (n <= 0 || n > theorigs.Upper()) return led;
220 if (!theeditor->IsList(n)) return led;
221 led = theeditor->ListEditor (num);
222 Handle(TColStd_HSequenceOfHAsciiString) lis = theeditor->ListValue(this,num);
223 led->LoadModel (themodel);
224 led->LoadValues (lis);
228 void IFSelect_EditForm::LoadValue
229 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& val)
231 Standard_Integer n = RankFromNumber(num);
232 if (n <= 0 || n > theorigs.Upper()) return;
233 theorigs.SetValue (n,val);
236 void IFSelect_EditForm::LoadList
237 (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& list)
239 Standard_Integer n = RankFromNumber(num);
240 if (n <= 0 || n > theorigs.Upper()) return;
241 theorigs.SetValue (n,list);
245 Handle(TCollection_HAsciiString) IFSelect_EditForm::OriginalValue
246 (const Standard_Integer num) const
248 Standard_Integer n = RankFromNumber(num);
249 Handle(TCollection_HAsciiString) val;
250 if (theorigs.Upper() == 0) return theeditor->StringValue (this,num);
251 else return Handle(TCollection_HAsciiString)::DownCast(theorigs.Value(n));
254 Handle(TColStd_HSequenceOfHAsciiString) IFSelect_EditForm::OriginalList
255 (const Standard_Integer num) const
257 Standard_Integer n = RankFromNumber(num);
258 Handle(TColStd_HSequenceOfHAsciiString) list;
259 if (theorigs.Upper() == 0) return theeditor->ListValue (this,num);
260 else return Handle(TColStd_HSequenceOfHAsciiString)::DownCast(theorigs.Value(n));
263 Handle(TCollection_HAsciiString) IFSelect_EditForm::EditedValue
264 (const Standard_Integer num) const
266 if (themodifs.Upper() == 0) return OriginalValue(num);
267 if (!IsModified(num)) return OriginalValue(num);
268 Standard_Integer n = RankFromNumber(num);
269 return Handle(TCollection_HAsciiString)::DownCast(themodifs.Value(n));
272 Handle(TColStd_HSequenceOfHAsciiString) IFSelect_EditForm::EditedList
273 (const Standard_Integer num) const
275 if (themodifs.Upper() == 0) return OriginalList(num);
276 if (!IsModified(num)) return OriginalList(num);
277 Standard_Integer n = RankFromNumber(num);
278 return Handle(TColStd_HSequenceOfHAsciiString)::DownCast(themodifs.Value(n));
282 Standard_Boolean IFSelect_EditForm::IsModified
283 (const Standard_Integer num) const
285 if (thestatus.Upper() == 0) return Standard_False;
286 Standard_Integer n = RankFromNumber(num);
287 return (thestatus.Value(n) != 0);
290 Standard_Boolean IFSelect_EditForm::IsTouched
291 (const Standard_Integer num) const
293 if (thestatus.Upper() == 0) return Standard_False;
294 Standard_Integer n = RankFromNumber(num);
295 return (thestatus.Value(n) == 2);
298 Standard_Boolean IFSelect_EditForm::Modify
299 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& newval,
300 const Standard_Boolean enforce)
304 if (themodifs.Upper() == 0) return Standard_False;
305 Standard_Integer tnum = RankFromNumber(num);
306 if (tnum == 0) return Standard_False;
307 IFSelect_EditValue acc = theeditor->EditMode (num);
308 if (newval.IsNull() && acc != IFSelect_Optional) return Standard_False;
309 if (!enforce && (acc == IFSelect_EditProtected || acc == IFSelect_EditComputed)) return Standard_False;
312 Handle(Interface_TypedValue) typval = theeditor->TypedValue(num);
313 if (!typval->Satisfies(newval)) return Standard_False;
314 Interface_ParamType pty = typval->Type();
315 if (pty == Interface_ParamIdent && !newval.IsNull()) {
316 if (themodel.IsNull()) return Standard_False;
317 if (themodel->NextNumberForLabel(newval->ToCString(),0,Standard_False) <= 0)
318 return Standard_False;
322 if (!theeditor->Update(this,num,newval,enforce)) return Standard_False;
324 thestatus.SetValue (tnum,1);
325 themodifs.SetValue (tnum,newval);
326 return Standard_True;
329 Standard_Boolean IFSelect_EditForm::ModifyList
330 (const Standard_Integer num, const Handle(IFSelect_ListEditor)& edited,
331 const Standard_Boolean enforce)
334 if (edited.IsNull()) return Standard_False;
335 if (!edited->IsTouched()) return Standard_False;
336 Handle(TColStd_HSequenceOfHAsciiString) newlist = edited->EditedValues();
340 if (themodifs.Upper() == 0) return Standard_False;
341 Standard_Integer tnum = RankFromNumber(num);
342 if (tnum == 0) return Standard_False;
343 IFSelect_EditValue acc = theeditor->EditMode (num);
344 if (acc == IFSelect_EditRead || acc == IFSelect_EditDynamic) return Standard_False;
345 if (newlist.IsNull() && acc != IFSelect_Optional) return Standard_False;
346 if (!enforce && (acc == IFSelect_EditProtected || acc == IFSelect_EditComputed)) return Standard_False;
349 if (!theeditor->UpdateList(this,num,newlist,enforce)) return Standard_False;
351 thestatus.SetValue (tnum,1);
352 themodifs.SetValue (tnum,newlist);
353 return Standard_True;
356 Standard_Boolean IFSelect_EditForm::ModifyListValue
357 (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& list,
358 const Standard_Boolean enforce)
360 Handle(IFSelect_ListEditor) led = ListEditor (num);
361 if (led.IsNull()) return Standard_False;
362 if (!led->LoadEdited(list)) return Standard_False;
363 return ModifyList (num,led,enforce);
367 Standard_Boolean IFSelect_EditForm::Touch
368 (const Standard_Integer num, const Handle(TCollection_HAsciiString)& newval)
370 if (themodifs.Upper() == 0) return Standard_False;
371 Standard_Integer tnum = RankFromNumber(num);
372 if (tnum == 0) return Standard_False;
374 thestatus.SetValue (tnum,2);
375 themodifs.SetValue (tnum,newval);
377 return Standard_True;
380 Standard_Boolean IFSelect_EditForm::TouchList
381 (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& newlist)
383 if (themodifs.Upper() == 0) return Standard_False;
384 Standard_Integer tnum = RankFromNumber(num);
385 if (tnum == 0) return Standard_False;
387 thestatus.SetValue (tnum,2);
388 themodifs.SetValue (tnum,newlist);
390 return Standard_True;
394 void IFSelect_EditForm::ClearEdit (const Standard_Integer num)
396 Standard_Integer i, nb = thestatus.Upper();
398 for (i = 1; i <= nb; i ++) thestatus.SetValue (i,0);
400 Standard_Integer tnum = RankFromNumber(num);
401 if (tnum > 0 && num <= nb) thestatus.SetValue (tnum,0);
406 void IFSelect_EditForm::PrintDefs (const Handle(Message_Messenger)& S) const
408 Standard_Integer iv, nbv = NbValues(Standard_True);
409 S<<"***** EditForm, Label : "<<Label()<<endl;
410 if (IsComplete()) S<<"Complete, "<<nbv<<" Values"<<endl;
412 S<<"Extraction on "<<nbv<<" Values : (extracted<-editor)"<<endl;
413 for (iv = 1; iv <= nbv; iv ++) S<<" "<<iv<<"<-"<<NumberFromRank(iv);
420 static void PrintList
421 (const Handle(TColStd_HSequenceOfHAsciiString)& list,
422 const Handle(Message_Messenger)& S, const Standard_Boolean alsolist)
424 if (list.IsNull()) { S<<"(NULL LIST)"<<endl; return; }
426 Standard_Integer i,nb = list->Length();
427 S<<"(List : "<<nb<<" Items)"<<endl;
428 if (!alsolist) return;
430 for (i = 1; i <= nb; i ++) {
431 Handle(TCollection_HAsciiString) str = list->Value(i);
432 S<<" ["<<i<<"] "<< (str.IsNull() ? "(NULL)" : str->ToCString())<<endl;
436 void IFSelect_EditForm::PrintValues
437 (const Handle(Message_Messenger)& S, const Standard_Integer what,
438 const Standard_Boolean names, const Standard_Boolean alsolist) const
440 Standard_Integer iv, nbv = NbValues(Standard_True);
441 S<< "****************************************************"<<endl;
442 S<<"***** "<<Label()<<Interface_MSG::Blanks(Label(),40)<<"*****"<<endl;
443 S<<"***** *****"<<endl;
445 S<<"***** Values are NOT loaded *****"<<endl;
448 // Donnees sur lesquelles on a travaille
449 if (themodel.IsNull()) {
450 if (theent.IsNull()) S<<"***** No loaded data";
451 else S<<"***** No loaded Model. Loaded object : type "<<theent->DynamicType()->Name();
453 if (theent.IsNull()) S<<"***** No loaded entity";
454 else { S<<"***** Loaded entity : "; themodel->PrintLabel (theent,S); }
457 S<<endl<<"****************************************************"<<endl<<endl;
459 // Affichage des valeurs
460 Standard_Boolean nams = names;
461 Standard_Integer maxnam = theeditor->MaxNameLength (names ? 0 : -1);
462 if (maxnam == 0) { maxnam = theeditor->MaxNameLength (0); nams = Standard_True; }
463 Standard_Integer nbmod = 0;
464 if (what != 0) S<<"Mod N0 Name Value"<<endl;
465 else S<<" N0 Name Value"<<endl;
467 for (iv = 1; iv <= nbv; iv ++) {
468 Standard_Integer jv = NumberFromRank(iv);
469 Standard_CString name = theeditor->Name(jv,!nams);
473 Handle(TCollection_HAsciiString) str;
474 if (IsModified(jv)) S<<"* ";
476 S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
477 <<name<<Interface_MSG::Blanks(name,maxnam)<<" ";
479 if (theeditor->IsList(jv)) {
480 Handle(TColStd_HSequenceOfHAsciiString) list;
481 if (what < 0) list = OriginalList (jv);
482 if (what > 0) list = EditedList (jv);
483 PrintList (list,S,alsolist);
487 if (what < 0) str = OriginalValue (jv);
488 if (what > 0) str = EditedValue (jv);
490 S << (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
494 if (!IsModified(jv)) continue;
496 if (theeditor->IsList(jv)) {
497 Handle(TColStd_HSequenceOfHAsciiString) list= OriginalList (jv);
498 S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
499 <<name<<Interface_MSG::Blanks(name,maxnam)<<" ORIG:";
500 PrintList (list,S,alsolist);
502 list = EditedList (jv);
503 S<<Interface_MSG::Blanks("",maxnam+5)<<"MOD :";
504 PrintList (list,S,alsolist);
509 Handle(TCollection_HAsciiString) str = OriginalValue (jv);
510 S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
511 <<name<<Interface_MSG::Blanks(name,maxnam)<<" ORIG:"
512 << (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
513 str = EditedValue (jv);
514 S<<Interface_MSG::Blanks("",maxnam+4)<<" MOD :"<< (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
517 if (what == 0) S<<"On "<<nbv<<" Values, "<<nbmod<<" Modified"<<endl;
521 Standard_Boolean IFSelect_EditForm::Apply ()
523 Standard_Boolean stat = ApplyData(theent,themodel);
524 if (stat && !thekeepst) ClearEdit();
529 Standard_Boolean IFSelect_EditForm::Recognize () const
530 { return theeditor->Recognize(this); }
532 Standard_Boolean IFSelect_EditForm::ApplyData
533 (const Handle(Standard_Transient)& ent,
534 const Handle(Interface_InterfaceModel)& model)
535 { return theeditor->Apply (this,ent,model); }
538 Standard_Boolean IFSelect_EditForm::Undo ()
540 if (thestatus.Upper() == 0 || theorigs.Upper() == 0) return Standard_False;
541 Standard_Integer i, nb = thestatus.Upper();
542 for (i = 1; i <= nb; i ++) {
543 if (thestatus.Value (i) != 0) themodifs.SetValue (i,theorigs.Value(i));