Salome HOME
Libraries for several kinds of controls (line edit, spinbox, combobox, ...). These...
[modules/gui.git] / src / DDS / DDS_DicItem.cxx
1 #include "DDS_DicItem.h"
2 #include "DDS_Dictionary.h"
3
4 #include <TColStd_SequenceOfInteger.hxx>
5 #include <TColStd_SequenceOfExtendedString.hxx>
6
7 #include <LDOM_Text.hxx>
8 #include <LDOMString.hxx>
9 #include <LDOM_Element.hxx>
10
11 #include <UnitsAPI.hxx>
12 #include <Units_Dimensions.hxx>
13
14 #include <TColStd_MapOfReal.hxx>
15 #include <TColStd_SequenceOfAsciiString.hxx>
16
17 IMPLEMENT_STANDARD_HANDLE(DDS_DicItem, MMgt_TShared)
18 IMPLEMENT_STANDARD_RTTIEXT(DDS_DicItem, MMgt_TShared)
19
20 DDS_DicItem::DDS_DicItem()
21 : myType( 0 ),
22 myDefValue( 0 ),
23 myMax( 0 ),
24 myMin( 0 ),
25 myMinZoom( 0.1 ),
26 myMaxZoom( 10 ),
27 myZoomOrder( 2 )
28 {
29 }
30
31 DDS_DicItem::DDS_DicItem( const DDS_DicItem& )
32 {
33 }
34
35 void DDS_DicItem::operator=( const DDS_DicItem& )
36 {
37 }
38
39 TCollection_AsciiString DDS_DicItem::GetId() const
40 {
41   return myId;
42 }
43
44 DDS_DicItem::Type DDS_DicItem::GetType() const
45 {
46   return (DDS_DicItem::Type)myType;
47 }
48
49 TCollection_ExtendedString DDS_DicItem::GetLabel() const
50 {
51   return myLabel;
52 }
53
54 TCollection_ExtendedString DDS_DicItem::GetFilter() const
55 {
56   return myFilter;
57 }
58
59 TCollection_ExtendedString DDS_DicItem::GetRequired() const
60 {
61   return myRequired;
62 }
63
64 DDS_MsgType DDS_DicItem::GetWarningLevel() const
65 {
66   return (DDS_MsgType)myWarnLevel;
67 }
68
69 TCollection_ExtendedString DDS_DicItem::GetLongDescription() const
70 {
71   return myLongDescr;
72 }
73
74 TCollection_ExtendedString DDS_DicItem::GetShortDescription() const
75 {
76   return myShortDescr;
77 }
78
79 TCollection_AsciiString DDS_DicItem::GetComponent() const
80 {
81   TCollection_AsciiString aCompName;
82   Handle(DDS_DicGroup) aComponent = Handle(DDS_DicGroup)::DownCast(myComponent);
83   if ( !aComponent.IsNull() )
84     aCompName = aComponent->GetName();
85   return aCompName;
86 }
87
88 TCollection_AsciiString DDS_DicItem::GetUnits() const
89 {
90   return GetUnits( GetActiveUnitSystem() );
91 }
92
93 TCollection_AsciiString DDS_DicItem::GetUnits( const UnitSystem& theSystem ) const
94 {
95   TCollection_AsciiString anUnits;
96   UnitData* unitData = GetUnitData( theSystem );
97   if ( unitData )
98     anUnits = unitData->myUnits;
99   return anUnits;
100 }
101
102 Standard_Real DDS_DicItem::GetMinValue() const
103 {
104   return GetMinValue( GetActiveUnitSystem() );
105 }
106
107 Standard_Real DDS_DicItem::GetMinValue( const UnitSystem& theUnitsSystem ) const
108 {
109   return FromSI( myMin, theUnitsSystem );
110 }
111
112 Standard_Real DDS_DicItem::GetMaxValue() const
113 {
114   return GetMaxValue( GetActiveUnitSystem() );
115 }
116
117 Standard_Real DDS_DicItem::GetMaxValue( const UnitSystem& theUnitsSystem ) const
118 {
119   return FromSI( myMax, theUnitsSystem );
120 }
121
122 Standard_Integer DDS_DicItem::GetPrecision() const
123 {
124   return GetPrecision( GetActiveUnitSystem() );
125 }
126
127 Standard_Integer DDS_DicItem::GetPrecision( const UnitSystem& theSystem ) const
128 {
129   Standard_Integer aRes = 0;
130   UnitData* unitData = GetUnitData( theSystem );
131   if ( unitData )
132     aRes = unitData->myPrecision;
133   return aRes;
134 }
135
136 TCollection_ExtendedString DDS_DicItem::GetDefaultValue() const
137 {
138   return GetDefaultValue( GetActiveUnitSystem() );
139 }
140
141 TCollection_ExtendedString DDS_DicItem::GetDefaultValue( const UnitSystem& theSystem ) const
142 {
143   if ( !myDefString.Length() )
144     return myDefString;
145
146   TCollection_ExtendedString aStr;
147
148   switch ( myType )
149   {
150   case Float:
151   case Integer:
152     aStr = FromSI( myDefValue, theSystem );
153     break;
154   case List:
155   case String:
156     aStr = myDefString;
157     break;
158   default:
159     break;
160   }
161   return aStr;
162 }
163
164 TCollection_AsciiString DDS_DicItem::GetFormat( const Standard_Boolean theCanonical ) const
165 {
166   return GetFormat( GetActiveUnitSystem(), theCanonical );
167 }
168
169 TCollection_AsciiString DDS_DicItem::GetFormat( const UnitSystem& theSystem,
170                                                 const Standard_Boolean theCanonical ) const
171 {
172   TCollection_AsciiString aFormat;
173   UnitData* unitData = GetUnitData( theSystem );
174   if ( unitData )
175     aFormat = unitData->myFormat;
176
177   if ( theCanonical && aFormat.Length() > 1 )
178   {
179     static TCollection_AsciiString f;
180     f = aFormat;
181     Standard_Boolean isRemoved = false;
182     while ( !isRemoved )
183     {
184       char ch = f.Value( f.Length() - 1 );
185       if ( ( ch != '%' && ch != '.' && !IsDigit( ch ) ) && f.Length() > 1 )
186         f.Remove( f.Length() - 1 );
187       else
188         isRemoved = true;
189     }
190     aFormat = f;
191   }
192
193   return aFormat;
194 }
195
196 /*!
197   Access valueList:name of the parameter. This string is void if the list is
198   not defined - then use other properties: Type, DefaultValue, MaxValue, MinValue
199 */
200 TCollection_ExtendedString DDS_DicItem::GetNameOfValues() const
201 {
202   return myListName;
203 }
204
205 /*!
206   Access valueList of the parameter. This sequence is empty if the list is
207   not defined - then use other properties: Type, DefaultValue, MaxValue, MinValue
208 */
209 Standard_Boolean DDS_DicItem::GetListOfValues( Handle(TColStd_HArray1OfExtendedString)& theStrings,
210                                                Handle(TColStd_HArray1OfInteger)& theIntegers ) const
211 {
212   theStrings  = myListRef;
213   theIntegers = myListRefID;
214   return !theIntegers.IsNull() && !theStrings.IsNull();
215 }
216
217 /*!
218   Access valueList of the parameter. This sequence is empty if the list is not
219   defined - then use other properties: Type, DefaultValue, MaxValue, MinValue
220 */
221 Standard_Boolean DDS_DicItem::GetListOfValues( Handle(TColStd_HArray1OfExtendedString)& theStrings,
222                                                Handle(TColStd_HArray1OfInteger)& theIntegers,
223                                                Handle(TColStd_HArray1OfExtendedString)& theIcons ) const
224 {
225   theStrings  = myListRef;
226   theIntegers = myListRefID;
227   theIcons    = myListRefIcons;
228   return !theIntegers.IsNull() && !theStrings.IsNull() && !theIcons.IsNull();
229 }
230
231 Standard_Boolean DDS_DicItem::GetSpecialValues( TColStd_MapOfReal& theMap ) const
232 {
233   theMap.Clear();
234   if ( !myListRef.IsNull() )
235   {
236     for ( Standard_Integer i = myListRef->Lower(); i <= myListRef->Upper(); i++ )
237     {
238       if ( myListRef->Value( i ).IsAscii() )
239       {
240         TCollection_AsciiString aStr( myListRef->Value( i ) );
241         if ( aStr.IsRealValue() )
242           theMap.Add( aStr.RealValue() );
243       }
244     }
245   }
246
247   return theMap.Extent() > 0;
248 }
249
250 /*!
251   Returns min value of lateral zooming
252 */
253 Standard_Real DDS_DicItem::GetMinZoom() const
254 {
255   return myMinZoom;
256 }
257
258 /*!
259   Returns Max Value of lateral zooming
260 */
261 Standard_Real DDS_DicItem::GetMaxZoom() const
262 {
263   return myMaxZoom;
264 }
265
266 /*!
267   Get Order of lateral zooming
268 */
269 Standard_Real DDS_DicItem::GetZoomOrder() const
270 {
271   return myZoomOrder;
272 }
273
274 Standard_Real DDS_DicItem::ToSI( const Standard_Real theVal ) const
275 {
276   return ToSI( theVal, GetActiveUnitSystem() );
277 }
278
279 Standard_Real DDS_DicItem::FromSI( const Standard_Real theVal ) const
280 {
281   return FromSI( theVal, GetActiveUnitSystem() );
282 }
283
284 /*!
285   Convert value to default SI units according to current units
286 */
287 Standard_Real DDS_DicItem::ToSI( const Standard_Real theVal, const UnitSystem& theUnitsSystem ) const
288 {
289   Standard_Real aRes = theVal;
290   UnitData* anUnitData = GetUnitData( theUnitsSystem );
291   if ( anUnitData )
292     aRes = anUnitData->myZero + aRes * anUnitData->myScale;
293   return aRes;
294 }
295
296 /*!
297   Convert value from default SI units according to current units
298 */
299 Standard_Real DDS_DicItem::FromSI( const Standard_Real theVal, const UnitSystem& theUnitsSystem ) const
300 {
301   Standard_Real aRes = theVal;
302   UnitData* anUnitData = GetUnitData( theUnitsSystem );
303   if ( anUnitData )
304     aRes = ( aRes - anUnitData->myZero ) / anUnitData->myScale;
305   return aRes;
306 }
307
308 /*!
309   Parse record in XML file and retrieve information relevant for this data dic item
310 */
311 void DDS_DicItem::FillDataMap( TCollection_AsciiString theID, const LDOM_Element& theDatum,
312                                const LDOM_Element& theDocElement, const TColStd_SequenceOfAsciiString& theSystems )
313 {
314   TCollection_AsciiString aLabel    = theDatum.getAttribute( DDS_Dictionary::KeyWord( "DATUM_LABEL" ) );
315   TCollection_AsciiString aFormat   = theDatum.getAttribute( DDS_Dictionary::KeyWord( "DATUM_FORMAT" ) );
316   TCollection_AsciiString aFilter   = theDatum.getAttribute( DDS_Dictionary::KeyWord( "DATUM_FILTER" ) );
317   TCollection_AsciiString aRequired = theDatum.getAttribute( DDS_Dictionary::KeyWord( "DATUM_REQUIRED" ) );
318
319   TCollection_AsciiString aBaseKeyWord = DDS_Dictionary::KeyWord( "DATUM_UNITS" );
320
321   for ( Standard_Integer j = 0; j < theSystems.Length(); j++ )
322   {
323     UnitSystem anUnitSystem = theSystems.Value( j );
324     if ( !anUnitSystem.Length() )
325       continue;
326
327     TCollection_AsciiString aUnitKeyword = anUnitSystem + aBaseKeyWord;
328
329     if ( !myUnitData.IsBound( anUnitSystem ) )
330       myUnitData.Bind( anUnitSystem, UnitData() );
331
332     UnitData& anUnitData = myUnitData.ChangeFind( anUnitSystem );
333     anUnitData.myUnits = theDatum.getAttribute( LDOMString( aUnitKeyword.ToCString() ) );
334   }
335
336   if ( theSystems.Length() && myUnitData.IsBound( theSystems.First() ) &&
337        !myUnitData.Find( theSystems.First() ).myUnits.Length() )
338   {
339     TCollection_AsciiString units = theDatum.getAttribute( LDOMString( aBaseKeyWord.ToCString() ) );
340     if ( units.Length() )
341       myUnitData.ChangeFind( theSystems.First() ).myUnits = units;
342   }
343
344   TCollection_AsciiString units;
345   for ( NCollection_DataMap<UnitSystem, UnitData>::Iterator iter( myUnitData ); iter.More() && units.IsEmpty(); iter.Next() )
346     units = iter.Value().myUnits;
347
348   for ( NCollection_DataMap<UnitSystem, UnitData>::Iterator itr( myUnitData ); itr.More(); itr.Next() )
349   {
350     UnitData& dataUnits = itr.ChangeValue();
351     if ( dataUnits.myUnits.IsEmpty() )
352       dataUnits.myUnits = units;
353   }
354   
355   // 2. Elements ( domain, description )
356   Standard_Real aRealMinV = 0;
357   Standard_Real aRealMaxV = 0;
358   Standard_Real aRealDefV = 0;
359
360   TCollection_AsciiString aType;
361
362   DDS_MsgType aWrongValue = DDS_MT_NONE;
363   DDS_DicItem::Type aEnumType = DDS_DicItem::Unknown;
364
365   TCollection_AsciiString aMinV;
366   TCollection_AsciiString aMaxV;
367   TCollection_AsciiString aDefV;
368   TCollection_AsciiString aListName;
369
370   TCollection_AsciiString aLongD;
371   TCollection_AsciiString aShortD;
372
373   TColStd_SequenceOfInteger aSeqOfValueID;
374   TColStd_SequenceOfExtendedString aSeqOfValue;
375   TColStd_SequenceOfExtendedString aSeqOfValueIconName;
376
377   // Presentation
378   Standard_Real aMinZoom   = 0;
379   Standard_Real aMaxZoom   = 0;
380   Standard_Real aZoomOrder = 0;
381
382   // Datum::Reports tags (if any)
383   LDOM_Element aWLev = theDatum.GetChildByTagName( DDS_Dictionary::KeyWord( "WARNING_LEVEL" ) );
384   if ( !aWLev.isNull() )
385   {
386     TCollection_AsciiString aWrongValWL = aWLev.getAttribute( DDS_Dictionary::KeyWord( "WRONG_VALUE" ) );
387     if ( aWrongValWL.IsEqual( "Info" ) )
388       aWrongValue = DDS_MT_INFO;
389     else if ( aWrongValWL.IsEqual( "Warning" ) )
390       aWrongValue = DDS_MT_WARNING;
391     else if ( aWrongValWL.IsEqual( "Alarm" ) )
392       aWrongValue = DDS_MT_ALARM;
393     else if ( aWrongValWL.IsEqual( "Error" ) )
394       aWrongValue = DDS_MT_ERROR;
395   }
396
397   // Datum::Presentation
398   LDOM_Element aPrs = theDatum.GetChildByTagName( DDS_Dictionary::KeyWord( "PRS" ) );
399   if ( !aPrs.isNull() )
400   {
401     LDOM_Element aLateralZoom = aPrs.GetChildByTagName( DDS_Dictionary::KeyWord( "LATERAL_ZOOM" ) );
402     if ( !aLateralZoom.isNull() )
403     {
404       TCollection_AsciiString aMinZoomStr   = aLateralZoom.getAttribute( DDS_Dictionary::KeyWord( "LZ_MINV" ) );
405       TCollection_AsciiString aMaxZoomStr   = aLateralZoom.getAttribute( DDS_Dictionary::KeyWord( "LZ_MAXV" ) );
406       TCollection_AsciiString aZoomOrderStr = aLateralZoom.getAttribute( DDS_Dictionary::KeyWord( "LZ_ORDER" ) );
407       
408       aMinZoomStr.RemoveAll( ' ' );
409       if ( aMinZoomStr.IsRealValue() )
410         aMinZoom = aMinZoomStr.RealValue();
411
412       aMaxZoomStr.RemoveAll( ' ' );
413       if ( aMaxZoomStr.IsRealValue() )
414         aMaxZoom = aMaxZoomStr.RealValue();
415
416       aZoomOrderStr.RemoveAll( ' ' );
417       if ( aZoomOrderStr.IsRealValue() )
418         aZoomOrder = aZoomOrderStr.RealValue();
419     }
420   }
421
422   // Quantity::Domain record as the only child of that tag name
423   LDOM_Element aDomain = theDatum.GetChildByTagName( DDS_Dictionary::KeyWord( "DY_DOMAIN" ) );
424   if ( !aDomain.isNull() )
425   {
426     LDOM_Element aValueDescr = aDomain.GetChildByTagName( DDS_Dictionary::KeyWord( "VALUE_DESCR" ) );
427     if ( !aValueDescr.isNull() )
428     {
429       // read: valueDescr? (type?,min?,max?,default?)
430       aType = aValueDescr.getAttribute( DDS_Dictionary::KeyWord( "VD_TYPE" ) );
431       if ( aType.IsEqual( "String" ) )
432         aEnumType = String;
433       else if ( aType.IsEqual( "Float" ) )
434         aEnumType = Float;
435       else if ( aType.IsEqual( "Integer" ) )
436         aEnumType = Integer;
437
438       aMinV = aValueDescr.getAttribute( DDS_Dictionary::KeyWord( "VD_MINV" ) );
439       aMinV.RemoveAll( ' ' );
440       if ( aMinV.IsRealValue() )
441         aRealMinV = aMinV.RealValue();
442       aMaxV = aValueDescr.getAttribute( DDS_Dictionary::KeyWord( "VD_MAXV" ) );
443       aMaxV.RemoveAll( ' ' );
444       if ( aMaxV.IsRealValue() )
445         aRealMaxV = aMaxV.RealValue();
446       aDefV = aValueDescr.getAttribute( DDS_Dictionary::KeyWord( "VD_DEFV" ) );
447       aDefV.RemoveAll( ' ' );
448       if ( aDefV.IsRealValue() )
449         aRealDefV = aDefV.RealValue();
450
451       TCollection_AsciiString aSpecVal = aValueDescr.getAttribute( DDS_Dictionary::KeyWord( "VD_SPEC" ) );
452       Split( aSpecVal, myListRef );
453     }
454     else
455     {
456       //  read: listRef? (list?)
457       LDOM_Element aListRef = aDomain.GetChildByTagName( DDS_Dictionary::KeyWord( "VALUE_LIST_REF" ) );
458       if ( !aListRef.isNull() )
459       {
460         aType = "List";
461         aEnumType = List;                       
462         LDOMString aListId = aListRef.getAttribute( DDS_Dictionary::KeyWord( "VLR_LIST" ) );
463         aDefV = aListRef.getAttribute( DDS_Dictionary::KeyWord( "VD_DEFV" ) );
464         aDefV.RemoveAll( ' ' );
465         for ( LDOM_Element aListItem = theDocElement.GetChildByTagName( DDS_Dictionary::KeyWord( "VALUE_LIST" ) );
466               aListItem != NULL; aListItem = aListItem.GetSiblingByTagName() )
467         {
468           if ( aListItem.getAttribute( DDS_Dictionary::KeyWord( "VALUE_LIST_ID" ) ).equals( aListId ) )
469           {
470             //  The appropriate list of values is found: store the list name
471             aListName = aListItem.getAttribute( DDS_Dictionary::KeyWord( "VALUE_LIST_NAME" ) );
472             //  Iteration through the list of values
473             LDOM_Element aListItemValue = aListItem.GetChildByTagName( DDS_Dictionary::KeyWord( "VALUE_LIST_VALUE" ) );
474             while ( aListItemValue != NULL )
475             {
476               // read value ID
477               TCollection_AsciiString aListValueID = aListItemValue.getAttribute( DDS_Dictionary::KeyWord( "VALUE_LIST_VALUEID" ) );
478               if ( aListValueID.IsIntegerValue() )
479               {
480                 //  Read the text in the element "value"
481                 LDOM_Text aListItemTxt = (const LDOM_Text&)aListItemValue.getFirstChild();
482                 if ( !aListItemTxt.isNull() )
483                 {
484                   // adding ID and text value to sequence
485                   aSeqOfValueID.Append( aListValueID.IntegerValue() );
486                   aSeqOfValue.Append( aListItemTxt.getData() );
487                   // adding icon file name (optional) to sequence
488                   TCollection_ExtendedString aListValueIcon = aListItemValue.getAttribute( DDS_Dictionary::KeyWord( "VALUE_LIST_VALUEICON" ) );
489                   aSeqOfValueIconName.Append( aListValueIcon );
490                 }
491               }
492               aListItemValue = aListItemValue.GetSiblingByTagName();
493             }
494           }
495         }
496       }
497     }
498   }
499
500   // Quantity::Description record as the only child of that tag name
501   LDOM_Element aDescr = theDatum.GetChildByTagName( DDS_Dictionary::KeyWord( "DESCR" ) );
502   if ( !aDescr.isNull() )
503   {
504     // short description (#PCDATA)*
505     LDOM_Element aShDescr = aDescr.GetChildByTagName( DDS_Dictionary::KeyWord( "SHORT_D" ) );
506     if ( !aShDescr.isNull() )
507     {
508       // text is always a sub-node of element, containing it
509       LDOM_Text aShDescrTxt = (const LDOM_Text&)aShDescr.getFirstChild();
510       if ( !aShDescrTxt.isNull() )
511         aShortD = aShDescrTxt.getData();
512     }
513
514     // long description (#PCDATA)*
515     LDOM_Element aLDescr = aDescr.GetChildByTagName( DDS_Dictionary::KeyWord( "LONG_D" ) );
516     if ( !aLDescr.isNull() )
517     {
518       // text is always a sub-node of element, containing it
519       LDOM_Text aLDescrTxt = (const LDOM_Text&)aLDescr.getFirstChild();
520       if ( !aLDescrTxt.isNull() )
521         aLongD = aLDescrTxt.getData();
522     }
523   }
524
525   NCollection_DataMap<UnitSystem, Handle(Units_Dimensions)> aDimMap;
526
527   for ( NCollection_DataMap<UnitSystem, UnitData>::Iterator it( myUnitData ); it.More(); it.Next() )
528   {
529     UnitData& anUnitData = it.ChangeValue();
530
531     // check units
532     anUnitData.myZero  = 0.;
533     anUnitData.myScale = 1.;
534     try {
535       Standard_CString aUnitDataStr;
536       aUnitDataStr = (Standard_CString)anUnitData.myUnits.ToCString();
537       if ( anUnitData.myUnits.ToCString()[0] && strcmp( anUnitData.myUnits.ToCString(), "%" ) )
538       {
539         Handle(Units_Dimensions) aDim;
540         anUnitData.myZero  = UnitsAPI::AnyToSI( 0.0, aUnitDataStr, aDim );
541         anUnitData.myScale = UnitsAPI::AnyToSI( 1.0, aUnitDataStr, aDim ) - anUnitData.myZero;
542         UnitsAPI::AnyFromSI( 1.0, aUnitDataStr );
543         if ( !aDimMap.IsBound( it.Key() ) )
544           aDimMap.Bind( it.Key(), aDim );
545       }
546       else if ( anUnitData.myUnits.ToCString()[0] ) // treat '%' as unit with scale 100
547         anUnitData.myScale = 0.01;
548     }
549           catch( Standard_Failure ) {
550       anUnitData.myUnits.Clear();
551     }
552
553     Handle(Units_Dimensions) aPrev;
554     Standard_Boolean aStatus = Standard_True;
555     for ( NCollection_DataMap<UnitSystem, Handle(Units_Dimensions)>::Iterator itr( aDimMap );
556           itr.More() && aStatus; itr.Next() )
557     {
558       if ( itr.Value().IsNull() )
559         continue;
560
561       if ( aPrev.IsNull() )
562         aPrev = itr.Value();
563
564       aStatus = aPrev->IsEqual( itr.Value() );
565     }
566
567     if ( !aStatus )
568       printf( "Error in DataDictionary: Different dimensions for %s item", theID );
569   }
570
571   myId                = theID;
572   myType              = aEnumType;
573   myWarnLevel         = aWrongValue;
574   myLabel             = aLabel.ToCString();
575   myFilter            = aFilter.ToCString();
576   myLongDescr         = aLongD.ToCString();
577   myShortDescr        = aShortD.ToCString();
578   myMin               = aRealMinV;
579   myMax               = aRealMaxV;
580   myDefValue          = aRealDefV;
581   myDefString         = aDefV.ToCString();
582   myRequired          = aRequired.ToCString();
583   myListName          = aListName.ToCString();
584   myMinZoom           = aMinZoom;
585   myMaxZoom           = aMaxZoom;
586   myZoomOrder         = aZoomOrder;
587
588   // prepare formats
589   PrepareFormats( aFormat );
590
591   const Standard_Integer aLength = aSeqOfValue.Length();
592   if ( aLength > 0 )
593   {
594     myListRef      = new TColStd_HArray1OfExtendedString( 1, aLength );
595     myListRefID    = new TColStd_HArray1OfInteger( 1, aLength );
596     myListRefIcons = new TColStd_HArray1OfExtendedString( 1, aLength );
597     for ( Standard_Integer i = aLength; i > 0; i-- )
598     {
599       myListRef->ChangeValue( i ) = aSeqOfValue.Value( i );
600       myListRefID->ChangeValue( i ) = aSeqOfValueID.Value( i );
601       myListRefIcons->ChangeValue( i ) = aSeqOfValueIconName.Value( i );
602     }
603   }
604
605   if ( myType == List && myDefString == "" && !myListRef.IsNull() && myListRef->Length() > 0 )
606     myDefString = myListRef->Value( myListRef->Lower() );
607 }
608
609 /*!
610   Returns default formats for each unit systems
611 */
612 void DDS_DicItem::GetDefaultFormat()
613 {
614   for ( NCollection_DataMap<UnitSystem, UnitData>::Iterator it( myUnitData ); it.More(); it.Next() )
615   {
616     UnitData& anUnitData = it.ChangeValue();
617
618     switch ( myType )
619     {
620     case Integer:
621       anUnitData.myFormat = "%d";
622       break;
623     case Float:
624       anUnitData.myFormat = "%g";
625       break;
626     case String:
627     default:
628       anUnitData.myFormat.Clear();
629       break;;
630     }
631   }
632 }
633
634 /*!
635   Returns format for the string
636 */
637 void DDS_DicItem::GetStringFormat( const TCollection_AsciiString& theFlags,
638                                    const TCollection_AsciiString& theWidth,
639                                    const TCollection_AsciiString& thePrecision,
640                                    const TCollection_AsciiString& theTypePrefix,
641                                    TCollection_AsciiString& theFormat )
642 {
643   theFormat = "%";
644   theFormat += theFlags;
645   theFormat += theWidth;
646
647   if ( !thePrecision.IsEmpty() ) 
648   {
649     theFormat += ".";
650     theFormat += thePrecision;
651   }
652
653   theFormat += theTypePrefix;
654   theFormat += "s";
655 }
656
657 /*!
658   Returns format for the integer
659 */
660 void DDS_DicItem::GetIntegerFormat( const TCollection_AsciiString& theFlags,
661                                     const TCollection_AsciiString& theWidth,
662                                     const TCollection_AsciiString& thePrecision,
663                                     const TCollection_AsciiString& theTypePrefix,
664                                     const Standard_Character theType,
665                                     TCollection_AsciiString& theFormat )
666 {
667   Standard_Integer aPrecision = 0;
668   if ( !thePrecision.IsEmpty() )
669     aPrecision = thePrecision.IntegerValue();
670   Standard_Integer aWidth = 0;
671
672   if ( !theWidth.IsEmpty() )
673     aWidth = theWidth.IntegerValue();
674
675   if ( !thePrecision.IsEmpty() && aPrecision < 0 )
676   {
677     // possible value 0.1 will be 10.0
678     aWidth -= aPrecision;
679     aPrecision = 0;
680   }
681
682   if ( !thePrecision.IsEmpty() && aPrecision > ( aWidth - 2 ) )
683     aWidth = aPrecision + 2;
684
685   theFormat = "%";
686
687   theFormat += theFlags;
688   if ( !theWidth.IsEmpty() )
689     theFormat += aWidth;
690
691   theFormat += theTypePrefix;
692   theFormat += theType;
693 }
694
695 /*!
696   Returns format for the float
697 */
698 void DDS_DicItem::GetFloatFormat( const TCollection_AsciiString& theFlags,
699                                   const TCollection_AsciiString& theWidth,
700                                   const TCollection_AsciiString& thePrecision,
701                                   const TCollection_AsciiString& theTypePrefix,
702                                   const Standard_Character theType,
703                                   TCollection_AsciiString& theFormat )
704 {
705   Standard_Integer aPrecision = 0;
706   if ( !thePrecision.IsEmpty() )
707     aPrecision = thePrecision.IntegerValue();
708   Standard_Integer aWidth = 0;
709
710   if (!theWidth.IsEmpty() )
711     aWidth = theWidth.IntegerValue();
712
713   if (!thePrecision.IsEmpty() && aPrecision < 0 )
714   {
715     // possible value 0.1 will be 10.0
716     aWidth -= aPrecision;
717     aPrecision = 0;
718   }
719
720   if ( !thePrecision.IsEmpty() && aPrecision > ( aWidth - 2 ) )
721   {
722     aWidth = aPrecision + 2;
723   }
724
725   theFormat = "%";
726   theFormat += theFlags;
727
728   if ( !theWidth.IsEmpty() ) 
729     theFormat += aWidth;
730
731   if ( !thePrecision.IsEmpty() ) 
732   {
733     theFormat += ".";
734     theFormat += aPrecision;
735   }
736
737   theFormat += theTypePrefix;
738   theFormat += theType;
739 }
740
741 /*!
742   Prepares three formants for each unit systems
743 */
744 void DDS_DicItem::PrepareFormats( const TCollection_AsciiString&  theFormat )
745 {
746   for ( NCollection_DataMap<UnitSystem, UnitData>::Iterator it( myUnitData ); it.More(); it.Next() )
747   {
748     UnitData& anUnitData = it.ChangeValue();
749
750     anUnitData.myFormat = theFormat;
751     anUnitData.myPrecision = 0;
752   }
753
754   TCollection_AsciiString aPrecisionStr;
755   if ( theFormat.IsEmpty() && myType == List )
756     return;
757
758   // checking % presenting
759   if ( *theFormat.ToCString() != '%' )
760   {
761     GetDefaultFormat();
762     return;
763   }
764
765   TCollection_AsciiString aStr = ( theFormat.ToCString() + 1 );
766   Standard_Character aType = aStr.Value( aStr.Length() );
767
768   if ( ( aType != 's' && myType == String ) ||
769        ( aType != 'd' && myType == Integer ) ||
770        ( aType != 'f' && aType != 'g' && aType != 'e' && aType != 'G' && aType != 'E' && myType == Float ) )
771   {
772     GetDefaultFormat();
773     return;
774   }
775
776   // removing type character
777   aStr.Trunc( aStr.Length() - 1 );
778
779   TCollection_AsciiString aFlags;
780   while ( !aStr.IsEmpty() && aStr.Value( 1 ) != '.' && ( aStr.Value( 1 ) < '0' || aStr.Value( 1 ) > '9' ) )
781   {
782     aFlags = aFlags + aStr.Value( 1 );
783     aStr.Remove( 1 );
784   }
785
786   Standard_Integer aPos = 1;
787   while ( aPos <= aStr.Length() && ( aStr.Value( aPos ) == '.' ||
788           ( aStr.Value( aPos ) >= '0' && aStr.Value( aPos ) <= '9' ) ) )
789     aPos++;
790
791   TCollection_AsciiString aTypePrefix;
792   if ( aPos <= aStr.Length() )
793   {
794     aTypePrefix = aStr.SubString( aPos, aStr.Length() );
795     aStr.Trunc( aPos - 1 );
796   }
797
798   Standard_Integer aBasePrecision = 0;
799
800   // taking width and precision
801   TCollection_AsciiString aPrecision;
802
803   aPos = aStr.Search( "." );
804   if ( aPos >= 0 ) 
805   {
806     // aPrecision is defined
807     aPrecision = aStr.Split( aPos );
808     aStr.Remove( aStr.Length() );
809     if ( !aPrecision.IsEmpty() )
810     {
811       if ( !aPrecision.IsIntegerValue() ) 
812       { 
813         GetDefaultFormat();
814         return;
815       }
816       else
817       {
818         aPrecisionStr  = aPrecision;
819         aBasePrecision = aPrecision.IntegerValue();
820       }
821     }
822   }
823
824   if ( !aStr.IsEmpty() && !aStr.IsIntegerValue() )
825   {
826     GetDefaultFormat();
827     return;
828   }
829
830   NCollection_DataMap<UnitSystem, UnitData>::Iterator itr;
831
832   switch ( myType )
833   {
834   case String:
835     for ( itr.Initialize( myUnitData ); itr.More(); itr.Next() )
836     {
837       if ( aType != 'f' && aType != 'g' && aType != 'e' && aType != 'G' && aType != 'E' )
838         GetStringFormat( aFlags, aStr, aPrecisionStr, aTypePrefix, itr.ChangeValue().myFormat );
839     }
840     break;
841   case Float:
842   case Integer:
843     for ( itr.Initialize( myUnitData ); itr.More(); itr.Next() )
844     {
845       UnitData& anUnitData = itr.ChangeValue();
846       Standard_Integer aAmendment =
847         (Standard_Integer)log10( 10.0 / DDS_Dictionary::FromSI( 10.0, anUnitData.myUnits.ToCString() ) );
848       anUnitData.myPrecision = aBasePrecision + aAmendment;
849       aPrecisionStr = TCollection_AsciiString( anUnitData.myPrecision );
850
851       // create a formats
852       if ( myType == Integer )
853         GetIntegerFormat( aFlags, aStr, aPrecisionStr, aTypePrefix, aType, anUnitData.myFormat );
854       else
855         GetFloatFormat( aFlags, aStr, aPrecisionStr, aTypePrefix, aType, anUnitData.myFormat );
856     }
857     break;
858   default:;
859     GetDefaultFormat();
860     break;
861   }
862 }
863
864 void DDS_DicItem::Split( const TCollection_AsciiString& theStr, Handle(TColStd_HArray1OfExtendedString)& aRes )
865 {
866   aRes.Nullify();
867
868   if ( theStr.Length() > 0 )
869   {
870     TCollection_AsciiString aStr = theStr;
871     TColStd_SequenceOfAsciiString aSeq;
872     Standard_Integer anIndex = aStr.SearchFromEnd( (Standard_CString)" " );
873     while( anIndex > 1 )
874     {
875       TCollection_AsciiString tmpStr = aStr.Split( anIndex - 1 );
876       tmpStr.RemoveAll( ( Standard_Character )' ' );
877       if ( tmpStr.Length() > 0 )
878         aSeq.Append( tmpStr );
879       anIndex = aStr.SearchFromEnd( (Standard_CString)" " );
880     }
881
882     aStr.RemoveAll( ( Standard_Character )' ' );
883     if ( aStr.Length() > 0 )
884       aSeq.Append( aStr );
885
886     if ( aSeq.Length() > 0 )
887     {
888       aRes = new TColStd_HArray1OfExtendedString( 1, aSeq.Length() );
889       for ( int i = 1, n = aSeq.Length(); i <= n; i++ )
890         aRes->ChangeValue( i ) = aSeq( i );
891     }
892   }
893 }
894
895 DDS_DicItem::UnitData* DDS_DicItem::GetUnitData( const UnitSystem& sys ) const
896 {
897   UnitData* unit = 0;
898
899   if ( myUnitData.IsBound( sys ) )
900     unit = (UnitData*)&myUnitData.Find( sys );
901
902   return unit;
903 }
904
905 DDS_DicItem::UnitSystem DDS_DicItem::GetActiveUnitSystem() const
906 {
907   UnitSystem aSystem;
908   Handle(DDS_DicGroup) aComponent = Handle(DDS_DicGroup)::DownCast(myComponent);
909   if ( !aComponent.IsNull() )
910     aSystem = aComponent->GetActiveUnitSystem();
911   return aSystem;
912 }