1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 #include "QDS_Datum.h"
29 #include <DDS_DicItem.h>
30 #include <DDS_Dictionary.h>
32 QList<QDS_Datum*> QDS::_datumList;
36 \brief A set of usefull static functions.
40 \brief Convert the OpenCascade ASCII string to Qt string.
41 \param src OCC ASCII string
44 QString QDS::toQString( const TCollection_AsciiString& src )
46 QTextCodec* codec = QTextCodec::codecForLocale();
49 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
50 QString( (char*)src.ToCString() );
55 \brief Convert the OpenCascade Unicode string to Qt string.
56 \param src OCC Unicode string
59 QString QDS::toQString( const TCollection_ExtendedString& src )
62 return toQString( TCollection_AsciiString( src ) );
64 return QString( (QChar*)src.ToExtString(), src.Length() );
68 \brief Convert the OpenCascade ASCII string to Qt string.
69 \param src handle to OCC ASCII string
72 QString QDS::toQString( const Handle(TCollection_HAsciiString)& src )
77 return toQString( src->String() );
81 \brief Convert the OpenCascade Unicode string to Qt string.
82 \param src handle to OCC Unicode string
85 QString QDS::toQString( const Handle(TCollection_HExtendedString)& src )
90 return toQString( src->String() );
94 \brief Convert the Qt string to OpenCascade ASCII string.
96 \return OCC ASCII string
98 TCollection_AsciiString QDS::toAsciiString( const QString& src )
100 TCollection_AsciiString res;
101 if ( src.toLatin1().constData() )
103 QTextCodec* codec = QTextCodec::codecForLocale();
106 QByteArray str = codec->fromUnicode( src );
107 res = TCollection_AsciiString( (Standard_CString)(const char*)str, str.size() );
110 res = TCollection_AsciiString( (char*)src.toLatin1().constData() );
116 \brief Convert the OpenCascade Unicode string to OpenCascade ASCII string.
117 \param src OCC Unicode string
118 \return OCC ASCII string
120 TCollection_AsciiString QDS::toAsciiString( const TCollection_ExtendedString& src )
122 return TCollection_AsciiString( src );
126 \brief Convert the OpenCascade Unicode string to OpenCascade ASCII string.
127 \param src handle to OCC Unicode string
128 \return OCC ASCII string
130 TCollection_AsciiString QDS::toAsciiString( const Handle(TCollection_HExtendedString)& src )
132 TCollection_AsciiString res;
134 res = toAsciiString( src->String() );
139 \brief Convert the Qt string to OpenCascade Unicode string.
141 \return OCC Unicode string
143 TCollection_ExtendedString QDS::toExtString( const QString& src )
146 return TCollection_ExtendedString();
148 Standard_Integer len = src.length();
149 Standard_ExtCharacter* extStr = new Standard_ExtCharacter[( len + 1 ) * 2];
150 memcpy( extStr, src.unicode(), len * 2 );
153 TCollection_ExtendedString trg( extStr );
161 \brief Convert the OpenCascade ASCII string to OpenCascade Unicode string.
162 \param src OCC ASCII string
163 \return OCC Unicode string
165 TCollection_ExtendedString QDS::toExtString( const TCollection_AsciiString& src )
167 return TCollection_ExtendedString( src );
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.
174 bool QDS::load( const QString& dictPath )
176 if ( dictPath.isEmpty() )
179 return DDS_Dictionary::Load( toAsciiString( dictPath ) );
183 \brief Get the label of unit system \a sys.
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.
189 \param sys unit system
190 \param comp component
191 \return unit system lavel or empty string if unit system is not found
193 QString QDS::unitSystemLabel( const QString& sys, const QString& comp )
196 TCollection_AsciiString system = toAsciiString( sys );
197 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
199 lab = toQString( comp.isEmpty() ? dic->GetUnitSystemLabel( system ) :
200 dic->GetUnitSystemLabel( system, toAsciiString( comp ) ) );
205 \brief Get the name of active unit system from the specified component \a comp.
207 If component is not specified or empty string, then the first found
208 component will be used.
210 If component exists, then active unit system name is returned. Otherwise,
211 empty string is returned.
213 \param comp component
214 \return name of the active unit system
216 QString QDS::activeUnitSystem( const QString& comp )
219 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
221 sys = toQString( comp.isEmpty() ? dic->GetActiveUnitSystem() :
222 dic->GetActiveUnitSystem( toAsciiString( comp ) ) );
227 \brief Set the active unit system.
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
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()
237 \param unit system to be set active
238 \param comp component
240 void QDS::setActiveUnitSystem( const QString& sys, const QString& comp )
242 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
246 TCollection_AsciiString system = toAsciiString( sys );
247 comp.isEmpty() ? dic->SetActiveUnitSystem( system ) :
248 dic->SetActiveUnitSystem( system, toAsciiString( comp ) );
250 QString unitSys = activeUnitSystem( comp );
251 if ( sys == unitSys )
254 TCollection_AsciiString aComp = toAsciiString( comp );
255 for ( QList<QDS_Datum*>::iterator it = _datumList.begin(); it != _datumList.end(); ++it )
257 QDS_Datum* datum = *it;
261 bool ok = aComp.IsEmpty();
264 Handle(DDS_DicItem) item = datum->dicItem();
265 ok = !item.IsNull() && aComp == item->GetComponent();
269 datum->unitSystemChanged( unitSys );
274 \brief Register given datum \a datum in the global list.
276 This function is invoked automatically by QDS_Datum constructor.
278 \param datum datum being registered
280 void QDS::insertDatum( QDS_Datum* datum )
285 _datumList.append( datum );
289 \brief Remove given datum \a datum from the global list.
291 This function is invoked automatically by QDS_Datum destructor.
293 \param datum datum being unregistered
295 void QDS::removeDatum( QDS_Datum* datum )
300 _datumList.removeAt( _datumList.indexOf(datum) );