0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / IFSelect / IFSelect_EditForm.cxx
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_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>
27
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)  ,
35       thelabel (label) ,
36       thenums (0,1) ,
37       theorigs  (0, (undoable ? editor->NbValues() : 0) ) ,
38       themodifs (0, (readonly ? 0 : editor->NbValues()) ) ,
39       thestatus (0, (readonly ? 0 : editor->NbValues()) ) ,
40       theeditor (editor) , 
41       thetouched (0)    {  }
42
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)   , 
51       thelabel (label) , 
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()) ) ,
56       theeditor (editor) , 
57       thetouched (0)
58 {
59   Standard_Integer i,nb = nums.Length();
60   for (i = 1; i <= nb; i ++) thenums.SetValue (i,nums.Value(i));
61 }
62
63     Standard_Boolean& IFSelect_EditForm::EditKeepStatus ()
64       {  return thekeepst;  }
65
66     Standard_CString  IFSelect_EditForm::Label () const
67       {  return thelabel.ToCString();  }
68
69     Standard_Boolean  IFSelect_EditForm::IsLoaded () const
70       {  return theloaded;  }
71
72     void  IFSelect_EditForm::ClearData ()
73       {  theent.Nullify();  themodel.Nullify();  theloaded = Standard_False;  }
74
75     void  IFSelect_EditForm::SetData
76   (const Handle(Standard_Transient)& ent,
77    const Handle(Interface_InterfaceModel)& model)
78       {  theent = ent;  themodel = model;  }
79
80     void  IFSelect_EditForm::SetEntity
81   (const Handle(Standard_Transient)& ent)
82       {  theent = ent;  }
83
84     void  IFSelect_EditForm::SetModel
85   (const Handle(Interface_InterfaceModel)& model)
86       {  themodel = model;  }
87
88     Handle(Standard_Transient)  IFSelect_EditForm::Entity () const
89       {  return theent;  }
90
91     Handle(Interface_InterfaceModel)  IFSelect_EditForm::Model () const
92       {  return themodel;  }
93
94     Handle(IFSelect_Editor)  IFSelect_EditForm::Editor () const
95       {  return theeditor;  }
96
97     Standard_Boolean  IFSelect_EditForm::IsComplete () const
98       {  return thecomplete;  }
99
100     Standard_Integer  IFSelect_EditForm::NbValues
101   (const Standard_Boolean editable) const
102 {
103   if (!editable || thecomplete) return theeditor->NbValues();
104   return thenums.Upper();
105 }
106
107     Standard_Integer  IFSelect_EditForm::NumberFromRank
108   (const Standard_Integer rank) const
109 {
110   if (thecomplete) return rank;
111   if (rank < 1 || rank > thenums.Upper()) return 0;
112   return thenums.Value(rank);
113 }
114
115     Standard_Integer  IFSelect_EditForm::RankFromNumber
116   (const Standard_Integer num) const
117 {
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;
122   }
123   return 0;
124 }
125
126     Standard_Integer  IFSelect_EditForm::NameNumber
127   (const Standard_CString name) const
128 {
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;
135   }
136   return -res;
137 }
138
139     Standard_Integer  IFSelect_EditForm::NameRank
140   (const Standard_CString name) const
141 {
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;
148   }
149   return 0;
150 }
151
152
153     void  IFSelect_EditForm::LoadDefault ()
154 {
155   theloaded = Standard_True;
156   thetouched = 0;
157   Standard_Integer i,nb = theorigs.Upper();
158   if (nb == 0) return;
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);
164   }
165 }
166
167
168     Standard_Boolean  IFSelect_EditForm::LoadData
169   (const Handle(Standard_Transient)& ent,
170    const Handle(Interface_InterfaceModel)& model)
171 {
172   thetouched = 0;
173   if (!theeditor->Load (this,ent,model)) return Standard_False;
174   SetData (ent,model);
175   theloaded = Standard_True;
176   return Standard_True;
177 }
178
179     Standard_Boolean  IFSelect_EditForm::LoadEntity
180   (const Handle(Standard_Transient)& ent)
181 {
182   thetouched = 0;
183   Handle(Interface_InterfaceModel) model;
184   if (!theeditor->Load (this,ent,model)) return Standard_False;
185   SetEntity (ent);
186   theloaded = Standard_True;
187   return Standard_True;
188 }
189
190     Standard_Boolean  IFSelect_EditForm::LoadModel
191   (const Handle(Interface_InterfaceModel)& model)
192 {
193   thetouched = 0;
194   Handle(Standard_Transient) ent;
195   if (!theeditor->Load (this,ent,model)) return Standard_False;
196   SetData (ent,model);
197   theloaded = Standard_True;
198   return Standard_True;
199 }
200
201     Standard_Boolean  IFSelect_EditForm::LoadData ()
202 {
203   thetouched = 0;
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;
209 }
210
211
212 //  ########    VALUES    ########
213
214     Handle(IFSelect_ListEditor)  IFSelect_EditForm::ListEditor
215   (const Standard_Integer num) const
216 {
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);
225   return led;
226 }
227
228     void  IFSelect_EditForm::LoadValue
229   (const Standard_Integer num, const Handle(TCollection_HAsciiString)& val)
230 {
231   Standard_Integer n = RankFromNumber(num);
232   if (n <= 0 || n > theorigs.Upper()) return;
233   theorigs.SetValue (n,val);
234 }
235
236     void  IFSelect_EditForm::LoadList
237   (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& list)
238 {
239   Standard_Integer n = RankFromNumber(num);
240   if (n <= 0 || n > theorigs.Upper()) return;
241   theorigs.SetValue (n,list);
242 }
243
244
245     Handle(TCollection_HAsciiString)  IFSelect_EditForm::OriginalValue
246   (const Standard_Integer num) const
247 {
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));
252 }
253
254     Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_EditForm::OriginalList
255   (const Standard_Integer num) const
256 {
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));
261 }
262
263     Handle(TCollection_HAsciiString)  IFSelect_EditForm::EditedValue
264   (const Standard_Integer num) const
265 {
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));
270 }
271
272     Handle(TColStd_HSequenceOfHAsciiString)  IFSelect_EditForm::EditedList
273   (const Standard_Integer num) const
274 {
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));
279 }
280
281
282     Standard_Boolean  IFSelect_EditForm::IsModified
283   (const Standard_Integer num) const
284 {
285   if (thestatus.Upper() == 0) return Standard_False;
286   Standard_Integer n = RankFromNumber(num);
287   return (thestatus.Value(n) != 0);
288 }
289
290     Standard_Boolean  IFSelect_EditForm::IsTouched
291   (const Standard_Integer num) const
292 {
293   if (thestatus.Upper() == 0) return Standard_False;
294   Standard_Integer n = RankFromNumber(num);
295   return (thestatus.Value(n) == 2);
296 }
297
298     Standard_Boolean  IFSelect_EditForm::Modify
299   (const Standard_Integer num, const Handle(TCollection_HAsciiString)& newval,
300    const Standard_Boolean enforce)
301 {
302 //  Peut-on editer
303   thetouched = 0;
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;
310
311 //  Satisfies ?
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;
319   }
320
321 //  Update ?
322   if (!theeditor->Update(this,num,newval,enforce)) return Standard_False;
323
324   thestatus.SetValue (tnum,1);
325   themodifs.SetValue (tnum,newval);
326   return Standard_True;
327 }
328
329     Standard_Boolean  IFSelect_EditForm::ModifyList
330   (const Standard_Integer num, const Handle(IFSelect_ListEditor)& edited,
331    const Standard_Boolean enforce)
332 {
333 //  Faut-il prendre
334   if (edited.IsNull()) return Standard_False;
335   if (!edited->IsTouched()) return Standard_False;
336   Handle(TColStd_HSequenceOfHAsciiString) newlist = edited->EditedValues();
337
338 //  Peut-on editer
339   thetouched = 0;
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;
347
348 //  Update ?
349   if (!theeditor->UpdateList(this,num,newlist,enforce)) return Standard_False;
350
351   thestatus.SetValue (tnum,1);
352   themodifs.SetValue (tnum,newlist);
353   return Standard_True;
354 }
355
356     Standard_Boolean  IFSelect_EditForm::ModifyListValue
357   (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& list,
358    const Standard_Boolean enforce)
359 {
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);
364 }
365
366
367     Standard_Boolean  IFSelect_EditForm::Touch
368   (const Standard_Integer num, const Handle(TCollection_HAsciiString)& newval)
369 {
370   if (themodifs.Upper() == 0) return Standard_False;
371   Standard_Integer tnum = RankFromNumber(num);
372   if (tnum == 0) return Standard_False;
373
374   thestatus.SetValue (tnum,2);
375   themodifs.SetValue (tnum,newval);
376   thetouched ++;
377   return Standard_True;
378 }
379
380     Standard_Boolean  IFSelect_EditForm::TouchList
381   (const Standard_Integer num, const Handle(TColStd_HSequenceOfHAsciiString)& newlist)
382 {
383   if (themodifs.Upper() == 0) return Standard_False;
384   Standard_Integer tnum = RankFromNumber(num);
385   if (tnum == 0) return Standard_False;
386
387   thestatus.SetValue (tnum,2);
388   themodifs.SetValue (tnum,newlist);
389   thetouched ++;
390   return Standard_True;
391 }
392
393
394     void  IFSelect_EditForm::ClearEdit (const Standard_Integer num)
395 {
396   Standard_Integer i, nb = thestatus.Upper();
397   if (num == 0) {
398     for (i = 1; i <= nb; i ++)   thestatus.SetValue (i,0);
399   } else {
400     Standard_Integer tnum = RankFromNumber(num);
401     if (tnum > 0 && num <= nb) thestatus.SetValue (tnum,0);
402   }
403 }
404
405
406     void  IFSelect_EditForm::PrintDefs (const Handle(Message_Messenger)& S) const
407 {
408   Standard_Integer iv, nbv = NbValues(Standard_True);
409   S<<"***** EditForm,  Label : "<<Label()<<endl;
410   if (IsComplete()) S<<"Complete, "<<nbv<<" Values"<<endl;
411   else {
412     S<<"Extraction on "<<nbv<<" Values : (extracted<-editor)"<<endl;
413     for (iv = 1; iv <= nbv; iv ++) S<<"  "<<iv<<"<-"<<NumberFromRank(iv);
414     S<<endl;
415   }
416   S<<"*****"<<endl;
417 }
418
419
420 static void PrintList
421   (const Handle(TColStd_HSequenceOfHAsciiString)& list,
422    const Handle(Message_Messenger)& S, const Standard_Boolean alsolist)
423 {
424   if (list.IsNull())  {  S<<"(NULL LIST)"<<endl;  return;  }
425
426   Standard_Integer i,nb = list->Length();
427   S<<"(List : "<<nb<<" Items)"<<endl;
428   if (!alsolist) return;
429
430   for (i = 1; i <= nb; i ++) {
431     Handle(TCollection_HAsciiString) str = list->Value(i);
432     S<<"  ["<<i<<"]     "<< (str.IsNull() ? "(NULL)" : str->ToCString())<<endl;
433   }
434 }
435
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
439 {
440   Standard_Integer iv, nbv = NbValues(Standard_True);
441   S<<  "****************************************************"<<endl;
442   S<<"*****  "<<Label()<<Interface_MSG::Blanks(Label(),40)<<"*****"<<endl;
443   S<<"*****                                          *****"<<endl;
444   if (!theloaded)
445     S<<"*****         Values are NOT loaded            *****"<<endl;
446
447   else {
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();
452     } else {
453       if (theent.IsNull()) S<<"*****  No loaded entity";
454       else { S<<"*****  Loaded entity : "; themodel->PrintLabel (theent,S); }
455     }
456   }
457   S<<endl<<"****************************************************"<<endl<<endl;
458
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;
466
467   for (iv = 1; iv <= nbv; iv ++) {
468     Standard_Integer jv = NumberFromRank(iv);
469     Standard_CString name = theeditor->Name(jv,!nams);
470
471 //     Original ou Final
472     if (what != 0) {
473       Handle(TCollection_HAsciiString) str;
474       if (IsModified(jv)) S<<"* ";
475       else S<<"  ";
476       S <<Interface_MSG::Blanks(iv,3)<<iv<<" "
477         <<name<<Interface_MSG::Blanks(name,maxnam)<<"  ";
478
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);
484         continue;
485       }
486
487       if (what < 0) str = OriginalValue (jv);
488       if (what > 0) str = EditedValue (jv);
489
490       S << (str.IsNull() ? "(NULL)" : str->ToCString()) <<endl;
491
492 //    Modified only
493     } else {
494       if (!IsModified(jv)) continue;
495       nbmod ++;
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);
501
502         list = EditedList (jv);
503         S<<Interface_MSG::Blanks("",maxnam+5)<<"MOD :";
504         PrintList (list,S,alsolist);
505
506         continue;
507       }
508
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;
515     }
516   }
517   if (what == 0) S<<"On "<<nbv<<" Values, "<<nbmod<<" Modified"<<endl;
518 }
519
520
521     Standard_Boolean  IFSelect_EditForm::Apply ()
522 {
523   Standard_Boolean stat = ApplyData(theent,themodel);
524   if (stat && !thekeepst) ClearEdit();
525   return stat;
526 }
527
528
529     Standard_Boolean  IFSelect_EditForm::Recognize () const
530       {  return theeditor->Recognize(this);  }
531
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);  }
536
537
538     Standard_Boolean  IFSelect_EditForm::Undo ()
539 {
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));
544   }
545   return Apply ();
546 }