Salome HOME
Libraries for several kinds of controls (line edit, spinbox, combobox, ...). These...
[modules/gui.git] / src / DDS / DDS_Dictionary.cxx
1 #include "DDS_Dictionary.h"
2
3 #include "DDS_KeyWords.h"
4
5 #include <LDOMString.hxx>
6 #include <LDOMParser.hxx>
7
8 #include <UnitsAPI.hxx>
9
10 #include <TColStd_SequenceOfInteger.hxx>
11 #include <TColStd_SequenceOfAsciiString.hxx>
12 #include <TColStd_SequenceOfExtendedString.hxx>
13
14 #include <NCollection_Map.hxx>
15
16 #include <Standard_Failure.hxx>
17 #include <Standard_ErrorHandler.hxx>
18
19 IMPLEMENT_STANDARD_HANDLE(DDS_Dictionary, MMgt_TShared)
20 IMPLEMENT_STANDARD_RTTIEXT(DDS_Dictionary, MMgt_TShared)
21
22 DDS_Dictionary::DDS_Dictionary()
23 : MMgt_TShared()
24 {
25 }
26
27 DDS_Dictionary::DDS_Dictionary( const DDS_Dictionary& )
28 {
29 }
30
31 void DDS_Dictionary::operator=( const DDS_Dictionary& )
32 {
33 }
34
35 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems ) const
36 {
37   theSystems.Clear();
38
39   NCollection_Map<TCollection_AsciiString> aMap;
40   for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
41   {
42     TColStd_SequenceOfAsciiString theSeq;
43     myGroupMap.FindFromIndex( i )->GetUnitSystems( theSeq );
44     for ( Standard_Integer s = 1; s <= theSeq.Length(); s++ )
45     {
46       if ( aMap.Contains( theSeq.Value( s ) ) )
47         continue;
48
49       theSystems.Append( theSeq.Value( s ) );
50       aMap.Add( theSeq.Value( s ) );
51     }
52   }
53
54 }
55
56 void DDS_Dictionary::GetUnitSystems( TColStd_SequenceOfAsciiString& theSystems,
57                                      const TCollection_AsciiString& theComponent ) const
58 {
59   theSystems.Clear();
60   if ( myGroupMap.Contains( theComponent ) )
61     myGroupMap.FindFromKey( theComponent )->GetUnitSystems( theSystems );
62 }
63
64 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem ) const
65 {
66   TCollection_ExtendedString aLabel;
67   for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && !aLabel.Length(); i++ )
68     aLabel = myGroupMap.FindFromIndex( i )->GetUnitSystemLabel( theSystem );
69   return aLabel;
70 }
71
72 TCollection_ExtendedString DDS_Dictionary::GetUnitSystemLabel( const TCollection_AsciiString& theSystem,
73                                                                const TCollection_AsciiString& theComponent ) const
74 {
75   TCollection_ExtendedString aLabel;
76   if ( myGroupMap.Contains( theComponent ) )
77     aLabel = myGroupMap.FindFromKey( theComponent )->GetUnitSystemLabel( theSystem );
78   return aLabel;
79 }
80
81 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem() const
82 {
83   TCollection_AsciiString aSystem;
84   if ( myGroupMap.Extent() )
85     aSystem = myGroupMap.FindFromIndex( 1 )->GetActiveUnitSystem();
86   return aSystem;
87 }
88
89 TCollection_AsciiString DDS_Dictionary::GetActiveUnitSystem( const TCollection_AsciiString& theComponent ) const
90 {
91   TCollection_AsciiString aSystem;
92   if ( myGroupMap.Contains( theComponent ) )
93     aSystem = myGroupMap.FindFromKey( theComponent )->GetActiveUnitSystem();
94   return aSystem;
95 }
96
97 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem )
98 {
99   for ( Standard_Integer i = 1; i <= myGroupMap.Extent(); i++ )
100     myGroupMap.FindFromIndex( i )->SetActiveUnitSystem( theSystem );
101 }
102
103 void DDS_Dictionary::SetActiveUnitSystem( const TCollection_AsciiString& theSystem,
104                                           const TCollection_AsciiString& theComponent )
105 {
106   if ( myGroupMap.Contains( theComponent ) )
107     myGroupMap.FindFromKey( theComponent )->SetActiveUnitSystem( theSystem );
108 }
109
110 /*!
111   Returns the instance of dictionary. Create instance if it is NULL.
112 */
113 Handle(DDS_Dictionary) DDS_Dictionary::Get()
114 {
115   static Handle(DDS_Dictionary) sDictionary;
116
117   if ( sDictionary.IsNull() )
118     sDictionary = new DDS_Dictionary();
119
120   return sDictionary;
121 }
122
123 Standard_Boolean DDS_Dictionary::Load( const TCollection_AsciiString theFileName )
124 {
125   static NCollection_Map<TCollection_AsciiString> _LoadMap;
126
127   if ( _LoadMap.Contains( theFileName ) )
128     return Standard_True;
129
130   Handle(DDS_Dictionary) aDic = Get();
131   if ( aDic.IsNull() )
132     return Standard_False;
133
134   LDOMParser aParser;
135   if ( aParser.parse( theFileName.ToCString() ) )
136     return Standard_False;
137
138   LDOM_Document aDoc = aParser.getDocument();
139   LDOM_Element aDocElement = aDoc.getDocumentElement();
140   for ( LDOM_Element aComponentElem = aDocElement.GetChildByTagName( KeyWord( "COMPONENT" ) );
141         !aComponentElem.isNull(); aComponentElem = aComponentElem.GetSiblingByTagName() )
142     aDic->FillDataMap( aComponentElem, aDocElement );
143
144   _LoadMap.Add( theFileName );
145
146   return Standard_True;
147 }
148
149 LDOMString DDS_Dictionary::KeyWord( const TCollection_AsciiString& key )
150 {
151   LDOMString keyWord;
152   Handle(DDS_KeyWords) aKeyWords = DDS_KeyWords::Get();
153   if ( !aKeyWords.IsNull() )
154   {
155     TCollection_AsciiString aStr = aKeyWords->GetKeyWord( key );
156     if ( aStr.Length() )
157       keyWord = LDOMString( aStr.ToCString() );
158   }
159   return keyWord;
160 }
161
162 /*!
163   Returns DicItem from specified group with all attached data
164 */
165
166 Handle(DDS_DicItem) DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID,
167                                                 const TCollection_AsciiString& theGroup ) const
168 {
169   Handle(DDS_DicItem) aDicItem;
170   Handle(DDS_DicGroup) aDicGroup;
171   if ( myGroupMap.Contains( theGroup ) )
172     aDicGroup = myGroupMap.FindFromKey( theGroup );
173   if ( !aDicGroup.IsNull() )
174     aDicItem = aDicGroup->GetDicItem( theID );
175   return aDicItem;
176 }
177
178 /*!
179   Returns DicItem with all attached data
180 */
181
182 Handle(DDS_DicItem) DDS_Dictionary::GetDicItem( const TCollection_AsciiString& theID ) const
183 {
184   Handle(DDS_DicItem) aDicItem;
185   for ( Standard_Integer i = 1; i <= myGroupMap.Extent() && aDicItem.IsNull(); i++ )
186     aDicItem = myGroupMap.FindFromIndex( i )->GetDicItem( theID );
187   return aDicItem;
188 }
189
190 void DDS_Dictionary::FillDataMap( const LDOM_Element& theComponentData, const LDOM_Element& theDocElement )
191 {
192   TCollection_AsciiString aCompName = theComponentData.getAttribute( KeyWord( "COMPONENT_NAME" ) );
193   if ( !myGroupMap.Contains( aCompName ) )
194     myGroupMap.Add( aCompName, new DDS_DicGroup( aCompName ) );
195   Handle(DDS_DicGroup) aDicGroup = myGroupMap.FindFromKey( aCompName );
196   aDicGroup->FillDataMap( theComponentData, theDocElement );
197   myGroupMap.Add( aCompName, aDicGroup );
198 }
199
200 Standard_Real DDS_Dictionary::ToSI( const Standard_Real theValue, const Standard_CString theUnits )
201 {
202   Standard_Real aRetValue = theValue;
203   if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
204   {
205     try {
206       aRetValue = UnitsAPI::AnyToSI( theValue, theUnits );
207     }
208     catch( Standard_Failure ) {
209     }
210   }
211   else if ( theUnits && *theUnits )
212     aRetValue = theValue / 100.0;
213
214   return aRetValue;
215 }
216
217 Standard_Real DDS_Dictionary::FromSI( const Standard_Real theValue, const Standard_CString theUnits )
218 {
219   Standard_Real aRetValue = theValue;
220   if ( theUnits && *theUnits && strcmp( theUnits, "%" ) )
221   {
222     try {
223       aRetValue = UnitsAPI::AnyFromSI( theValue, theUnits );
224     }
225     catch( Standard_Failure ) {
226     }
227   }
228   else if ( theUnits && *theUnits )
229     aRetValue = theValue * 100.0;
230
231   return aRetValue;
232 }