Salome HOME
Copyright update 2020
[modules/gui.git] / src / QDS / QDS.cxx
1 // Copyright (C) 2007-2020  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 QList<QDS_Datum*> QDS::_datumList;
33
34 /*!
35   \class QDS
36   \brief A set of usefull static functions.
37 */
38
39 /*!
40   \brief Convert the OpenCascade ASCII string to Qt string.
41   \param src OCC ASCII string
42   \return Qt string
43 */
44 QString QDS::toQString( const TCollection_AsciiString& src )
45 {
46   QTextCodec* codec = QTextCodec::codecForLocale();
47   QString res;
48   if ( !src.IsEmpty() )
49     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
50                   QString( (char*)src.ToCString() );
51   return res;
52 }
53
54 /*!
55   \brief Convert the OpenCascade Unicode string to Qt string.
56   \param src OCC Unicode string
57   \return Qt string
58 */
59 QString QDS::toQString( const TCollection_ExtendedString& src )
60 {
61   if ( src.IsAscii() )
62     return toQString( TCollection_AsciiString( src ) );
63   else
64     return QString( (QChar*)src.ToExtString(), src.Length() );
65 }
66
67 /*!
68   \brief Convert the OpenCascade ASCII string to Qt string.
69   \param src handle to OCC ASCII string
70   \return Qt string
71 */
72 QString QDS::toQString( const Handle(TCollection_HAsciiString)& src )
73 {
74   if ( src.IsNull() )
75     return QString();
76   else
77     return toQString( src->String() );
78 }
79
80 /*!
81   \brief Convert the OpenCascade Unicode string to Qt string.
82   \param src handle to OCC Unicode string
83   \return Qt string
84 */
85 QString QDS::toQString( const Handle(TCollection_HExtendedString)& src )
86 {
87   if ( src.IsNull() )
88     return QString();
89   else
90     return toQString( src->String() );
91 }
92
93 /*!
94   \brief Convert the Qt string to OpenCascade ASCII string.
95   \param src Qt string
96   \return OCC ASCII string
97 */
98 TCollection_AsciiString QDS::toAsciiString( const QString& src )
99 {
100   TCollection_AsciiString res;
101   if ( src.toLatin1().constData() )
102   {
103     QTextCodec* codec = QTextCodec::codecForLocale();
104     if ( codec )
105     {
106       QByteArray str = codec->fromUnicode( src );
107       res = TCollection_AsciiString( (Standard_CString)(const char*)str, str.size() );
108     }
109     else
110       res = TCollection_AsciiString( (char*)src.toLatin1().constData() );
111   }
112   return res;
113 }
114
115 /*!
116   \brief Convert the OpenCascade Unicode string to OpenCascade ASCII string.
117   \param src OCC Unicode string
118   \return OCC ASCII string
119 */
120 TCollection_AsciiString QDS::toAsciiString( const TCollection_ExtendedString& src )
121 {
122   return TCollection_AsciiString( src );
123 }
124
125 /*!
126   \brief Convert the OpenCascade Unicode string to OpenCascade ASCII string.
127   \param src handle to OCC Unicode string
128   \return OCC ASCII string
129 */
130 TCollection_AsciiString QDS::toAsciiString( const Handle(TCollection_HExtendedString)& src )
131 {
132   TCollection_AsciiString res;
133   if ( !src.IsNull() )
134     res = toAsciiString( src->String() );
135   return res;
136 }
137
138 /*!
139   \brief Convert the Qt string to OpenCascade Unicode string.
140   \param src Qt string
141   \return OCC Unicode string
142 */
143 TCollection_ExtendedString QDS::toExtString( const QString& src )
144 {
145   if ( src.isEmpty() )
146     return TCollection_ExtendedString();
147
148   Standard_Integer len = src.length();
149   Standard_ExtCharacter* extStr = new Standard_ExtCharacter[( len + 1 ) * 2];
150   memcpy( extStr, src.unicode(), len * 2 );
151   extStr[len] = 0;
152
153   TCollection_ExtendedString trg( extStr );
154
155   delete [] extStr;
156
157   return trg;
158 }
159
160 /*!
161   \brief Convert the OpenCascade ASCII string to OpenCascade Unicode string.
162   \param src OCC ASCII string
163   \return OCC Unicode string
164 */
165 TCollection_ExtendedString QDS::toExtString( const TCollection_AsciiString& src )
166 {
167   return TCollection_ExtendedString( src );
168 }
169
170 /*!
171   \brief Load datum definitions from XML file \a dictPath to the dictionary.
172   \return \c true if loading is successed or \c false otherwise.
173 */
174 bool QDS::load( const QString& dictPath )
175 {
176   if ( dictPath.isEmpty() )
177     return false;
178
179   return DDS_Dictionary::Load( toAsciiString( dictPath ) );
180 }
181
182 /*!
183   \brief Get the label of unit system \a sys.
184
185   If component \a comp is specified and not empty then the function 
186   searches the given unit system in the specified component, otherwise 
187   all components will be searched.
188
189   \param sys unit system
190   \param comp component
191   \return unit system lavel or empty string if unit system is not found
192 */
193 QString QDS::unitSystemLabel( const QString& sys, const QString& comp )
194 {
195   QString lab;
196   TCollection_AsciiString system = toAsciiString( sys );
197   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
198   if ( !dic.IsNull() )
199     lab = toQString( comp.isEmpty() ? dic->GetUnitSystemLabel( system ) :
200                                       dic->GetUnitSystemLabel( system, toAsciiString( comp ) ) );
201   return lab;
202 }
203
204 /*!
205   \brief Get the name of active unit system from the specified component \a comp.
206
207   If component is not specified or empty string, then the first found 
208   component will be used.
209   
210   If component exists, then active unit system name is returned. Otherwise, 
211   empty string is returned.
212   
213   \param comp component
214   \return name of the active unit system
215 */
216 QString QDS::activeUnitSystem( const QString& comp )
217 {
218   QString sys;
219   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
220   if ( !dic.IsNull() )
221     sys = toQString( comp.isEmpty() ? dic->GetActiveUnitSystem() :
222                                       dic->GetActiveUnitSystem( toAsciiString( comp ) ) );
223   return sys;
224 }
225
226 /*!
227   \brief Set the active unit system.
228
229   If not empty component name \a comp is specified, then the unit system
230   will be activated in the given component, otherwise all components 
231   will be processed.
232
233   After the changing of active unit system function notifies about it all
234   registered datums from processed components using method 
235   QDS_Datum::unitSystemChanged()
236
237   \param unit system to be set active
238   \param comp component
239 */
240 void QDS::setActiveUnitSystem( const QString& sys, const QString& comp )
241 {
242   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
243   if ( dic.IsNull() )
244     return;
245
246   TCollection_AsciiString system = toAsciiString( sys );
247   comp.isEmpty() ? dic->SetActiveUnitSystem( system ) :
248                    dic->SetActiveUnitSystem( system, toAsciiString( comp ) );
249
250   QString unitSys = activeUnitSystem( comp );
251   if ( sys == unitSys )
252     return;
253
254   TCollection_AsciiString aComp = toAsciiString( comp );
255   for ( QList<QDS_Datum*>::iterator it = _datumList.begin(); it != _datumList.end(); ++it )
256   {
257     QDS_Datum* datum = *it;
258     if ( !datum )
259       continue;
260
261     bool ok = aComp.IsEmpty();
262     if ( !ok )
263     {
264       Handle(DDS_DicItem) item = datum->dicItem();
265       ok = !item.IsNull() && aComp == item->GetComponent();
266     }
267
268     if ( ok )
269       datum->unitSystemChanged( unitSys );
270   }
271 }
272
273 /*!
274   \brief Register given datum \a datum in the global list.
275   
276   This function is invoked automatically by QDS_Datum constructor.
277   
278   \param datum datum being registered
279 */
280 void QDS::insertDatum( QDS_Datum* datum )
281 {
282   if ( !datum )
283     return;
284
285   _datumList.append( datum );
286 }
287
288 /*!
289   \brief Remove given datum \a datum from the global list.
290
291   This function is invoked automatically by QDS_Datum destructor.
292
293   \param datum datum being unregistered
294 */
295 void QDS::removeDatum( QDS_Datum* datum )
296 {
297   if ( !datum )
298     return;
299
300   _datumList.removeAt( _datumList.indexOf(datum) );
301 }