Salome HOME
Synchronize adm files
[modules/gui.git] / src / QDS / QDS.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "QDS.h"
24
25 #include "QDS_Datum.h"
26
27 #include <QTextCodec>
28
29 #include <DDS_DicItem.h>
30 #include <DDS_Dictionary.h>
31
32 #include <TCollection_HAsciiString.hxx>
33 #include <TCollection_HExtendedString.hxx>
34
35 QList<QDS_Datum*> QDS::_datumList;
36
37 /*!
38   \class QDS
39   \brief A set of usefull static functions.
40 */
41
42 /*!
43   \brief Convert the OpenCascade ASCII string to Qt string.
44   \param src OCC ASCII string
45   \return Qt string
46 */
47 QString QDS::toQString( const TCollection_AsciiString& src )
48 {
49   QTextCodec* codec = QTextCodec::codecForLocale();
50   QString res;
51   if ( !src.IsEmpty() )
52     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
53                   QString( (char*)src.ToCString() );
54   return res;
55 }
56
57 /*!
58   \brief Convert the OpenCascade Unicode string to Qt string.
59   \param src OCC Unicode string
60   \return Qt string
61 */
62 QString QDS::toQString( const TCollection_ExtendedString& src )
63 {
64   if ( src.IsAscii() )
65     return toQString( TCollection_AsciiString( src ) );
66   else
67     return QString( (QChar*)src.ToExtString(), src.Length() );
68 }
69
70 /*!
71   \brief Convert the OpenCascade ASCII string to Qt string.
72   \param src handle to OCC ASCII string
73   \return Qt string
74 */
75 QString QDS::toQString( const Handle(TCollection_HAsciiString)& src )
76 {
77   if ( src.IsNull() )
78     return QString();
79   else
80     return toQString( src->String() );
81 }
82
83 /*!
84   \brief Convert the OpenCascade Unicode string to Qt string.
85   \param src handle to OCC Unicode string
86   \return Qt string
87 */
88 QString QDS::toQString( const Handle(TCollection_HExtendedString)& src )
89 {
90   if ( src.IsNull() )
91     return QString();
92   else
93     return toQString( src->String() );
94 }
95
96 /*!
97   \brief Convert the Qt string to OpenCascade ASCII string.
98   \param src Qt string
99   \return OCC ASCII string
100 */
101 TCollection_AsciiString QDS::toAsciiString( const QString& src )
102 {
103   TCollection_AsciiString res;
104   if ( src.toLatin1().constData() )
105   {
106     QTextCodec* codec = QTextCodec::codecForLocale();
107     if ( codec )
108     {
109       QByteArray str = codec->fromUnicode( src );
110       res = TCollection_AsciiString( (Standard_CString)(const char*)str, str.size() );
111     }
112     else
113       res = TCollection_AsciiString( (char*)src.toLatin1().constData() );
114   }
115   return res;
116 }
117
118 /*!
119   \brief Convert the OpenCascade Unicode string to OpenCascade ASCII string.
120   \param src OCC Unicode string
121   \return OCC ASCII string
122 */
123 TCollection_AsciiString QDS::toAsciiString( const TCollection_ExtendedString& src )
124 {
125   return TCollection_AsciiString( src );
126 }
127
128 /*!
129   \brief Convert the OpenCascade Unicode string to OpenCascade ASCII string.
130   \param src handle to OCC Unicode string
131   \return OCC ASCII string
132 */
133 TCollection_AsciiString QDS::toAsciiString( const Handle(TCollection_HExtendedString)& src )
134 {
135   TCollection_AsciiString res;
136   if ( !src.IsNull() )
137     res = toAsciiString( src->String() );
138   return res;
139 }
140
141 /*!
142   \brief Convert the Qt string to OpenCascade Unicode string.
143   \param src Qt string
144   \return OCC Unicode string
145 */
146 TCollection_ExtendedString QDS::toExtString( const QString& src )
147 {
148   if ( src.isEmpty() )
149     return TCollection_ExtendedString();
150
151   Standard_Integer len = src.length();
152   Standard_ExtCharacter* extStr = new Standard_ExtCharacter[( len + 1 ) * 2];
153   memcpy( extStr, src.unicode(), len * 2 );
154   extStr[len] = 0;
155
156   TCollection_ExtendedString trg( extStr );
157
158   delete [] extStr;
159
160   return trg;
161 }
162
163 /*!
164   \brief Convert the OpenCascade ASCII string to OpenCascade Unicode string.
165   \param src OCC ASCII string
166   \return OCC Unicode string
167 */
168 TCollection_ExtendedString QDS::toExtString( const TCollection_AsciiString& src )
169 {
170   return TCollection_ExtendedString( src );
171 }
172
173 /*!
174   \brief Load datum definitions from XML file \a dictPath to the dictionary.
175   \return \c true if loading is successed or \c false otherwise.
176 */
177 bool QDS::load( const QString& dictPath )
178 {
179   if ( dictPath.isEmpty() )
180     return false;
181
182   return DDS_Dictionary::Load( toAsciiString( dictPath ) );
183 }
184
185 /*!
186   \brief Get the label of unit system \a sys.
187
188   If component \a comp is specified and not empty then the function 
189   searches the given unit system in the specified component, otherwise 
190   all components will be searched.
191
192   \param sys unit system
193   \param comp component
194   \return unit system lavel or empty string if unit system is not found
195 */
196 QString QDS::unitSystemLabel( const QString& sys, const QString& comp )
197 {
198   QString lab;
199   TCollection_AsciiString system = toAsciiString( sys );
200   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
201   if ( !dic.IsNull() )
202     lab = toQString( comp.isEmpty() ? dic->GetUnitSystemLabel( system ) :
203                                       dic->GetUnitSystemLabel( system, toAsciiString( comp ) ) );
204   return lab;
205 }
206
207 /*!
208   \brief Get the name of active unit system from the specified component \a comp.
209
210   If component is not specified or empty string, then the first found 
211   component will be used.
212   
213   If component exists, then active unit system name is returned. Otherwise, 
214   empty string is returned.
215   
216   \param comp component
217   \return name of the active unit system
218 */
219 QString QDS::activeUnitSystem( const QString& comp )
220 {
221   QString sys;
222   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
223   if ( !dic.IsNull() )
224     sys = toQString( comp.isEmpty() ? dic->GetActiveUnitSystem() :
225                                       dic->GetActiveUnitSystem( toAsciiString( comp ) ) );
226   return sys;
227 }
228
229 /*!
230   \brief Set the active unit system.
231
232   If not empty component name \a comp is specified, then the unit system
233   will be activated in the given component, otherwise all components 
234   will be processed.
235
236   After the changing of active unit system function notifies about it all
237   registered datums from processed components using method 
238   QDS_Datum::unitSystemChanged()
239
240   \param unit system to be set active
241   \param comp component
242 */
243 void QDS::setActiveUnitSystem( const QString& sys, const QString& comp )
244 {
245   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
246   if ( dic.IsNull() )
247     return;
248
249   TCollection_AsciiString system = toAsciiString( sys );
250   comp.isEmpty() ? dic->SetActiveUnitSystem( system ) :
251                    dic->SetActiveUnitSystem( system, toAsciiString( comp ) );
252
253   QString unitSys = activeUnitSystem( comp );
254   if ( sys == unitSys )
255     return;
256
257   TCollection_AsciiString aComp = toAsciiString( comp );
258   for ( QList<QDS_Datum*>::iterator it = _datumList.begin(); it != _datumList.end(); ++it )
259   {
260     QDS_Datum* datum = *it;
261     if ( !datum )
262       continue;
263
264     bool ok = aComp.IsEmpty();
265     if ( !ok )
266     {
267       Handle(DDS_DicItem) item = datum->dicItem();
268       ok = !item.IsNull() && aComp == item->GetComponent();
269     }
270
271     if ( ok )
272       datum->unitSystemChanged( unitSys );
273   }
274 }
275
276 /*!
277   \brief Register given datum \a datum in the global list.
278   
279   This function is invoked automatically by QDS_Datum constructor.
280   
281   \param datum datum being registered
282 */
283 void QDS::insertDatum( QDS_Datum* datum )
284 {
285   if ( !datum )
286     return;
287
288   _datumList.append( datum );
289 }
290
291 /*!
292   \brief Remove given datum \a datum from the global list.
293
294   This function is invoked automatically by QDS_Datum destructor.
295
296   \param datum datum being unregistered
297 */
298 void QDS::removeDatum( QDS_Datum* datum )
299 {
300   if ( !datum )
301     return;
302
303   _datumList.removeAt( _datumList.indexOf(datum) );
304 }