1 // Copyright (C) 2007-2014 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 #include <TCollection_HAsciiString.hxx>
33 #include <TCollection_HExtendedString.hxx>
35 QList<QDS_Datum*> QDS::_datumList;
39 \brief A set of usefull static functions.
43 \brief Convert the OpenCascade ASCII string to Qt string.
44 \param src OCC ASCII string
47 QString QDS::toQString( const TCollection_AsciiString& src )
49 QTextCodec* codec = QTextCodec::codecForLocale();
52 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
53 QString( (char*)src.ToCString() );
58 \brief Convert the OpenCascade Unicode string to Qt string.
59 \param src OCC Unicode string
62 QString QDS::toQString( const TCollection_ExtendedString& src )
65 return toQString( TCollection_AsciiString( src ) );
67 return QString( (QChar*)src.ToExtString(), src.Length() );
71 \brief Convert the OpenCascade ASCII string to Qt string.
72 \param src handle to OCC ASCII string
75 QString QDS::toQString( const Handle(TCollection_HAsciiString)& src )
80 return toQString( src->String() );
84 \brief Convert the OpenCascade Unicode string to Qt string.
85 \param src handle to OCC Unicode string
88 QString QDS::toQString( const Handle(TCollection_HExtendedString)& src )
93 return toQString( src->String() );
97 \brief Convert the Qt string to OpenCascade ASCII string.
99 \return OCC ASCII string
101 TCollection_AsciiString QDS::toAsciiString( const QString& src )
103 TCollection_AsciiString res;
104 if ( src.toLatin1().constData() )
106 QTextCodec* codec = QTextCodec::codecForLocale();
109 QByteArray str = codec->fromUnicode( src );
110 res = TCollection_AsciiString( (Standard_CString)(const char*)str, str.size() );
113 res = TCollection_AsciiString( (char*)src.toLatin1().constData() );
119 \brief Convert the OpenCascade Unicode string to OpenCascade ASCII string.
120 \param src OCC Unicode string
121 \return OCC ASCII string
123 TCollection_AsciiString QDS::toAsciiString( const TCollection_ExtendedString& src )
125 return TCollection_AsciiString( src );
129 \brief Convert the OpenCascade Unicode string to OpenCascade ASCII string.
130 \param src handle to OCC Unicode string
131 \return OCC ASCII string
133 TCollection_AsciiString QDS::toAsciiString( const Handle(TCollection_HExtendedString)& src )
135 TCollection_AsciiString res;
137 res = toAsciiString( src->String() );
142 \brief Convert the Qt string to OpenCascade Unicode string.
144 \return OCC Unicode string
146 TCollection_ExtendedString QDS::toExtString( const QString& src )
149 return TCollection_ExtendedString();
151 Standard_Integer len = src.length();
152 Standard_ExtCharacter* extStr = new Standard_ExtCharacter[( len + 1 ) * 2];
153 memcpy( extStr, src.unicode(), len * 2 );
156 TCollection_ExtendedString trg( extStr );
164 \brief Convert the OpenCascade ASCII string to OpenCascade Unicode string.
165 \param src OCC ASCII string
166 \return OCC Unicode string
168 TCollection_ExtendedString QDS::toExtString( const TCollection_AsciiString& src )
170 return TCollection_ExtendedString( src );
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.
177 bool QDS::load( const QString& dictPath )
179 if ( dictPath.isEmpty() )
182 return DDS_Dictionary::Load( toAsciiString( dictPath ) );
186 \brief Get the label of unit system \a sys.
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.
192 \param sys unit system
193 \param comp component
194 \return unit system lavel or empty string if unit system is not found
196 QString QDS::unitSystemLabel( const QString& sys, const QString& comp )
199 TCollection_AsciiString system = toAsciiString( sys );
200 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
202 lab = toQString( comp.isEmpty() ? dic->GetUnitSystemLabel( system ) :
203 dic->GetUnitSystemLabel( system, toAsciiString( comp ) ) );
208 \brief Get the name of active unit system from the specified component \a comp.
210 If component is not specified or empty string, then the first found
211 component will be used.
213 If component exists, then active unit system name is returned. Otherwise,
214 empty string is returned.
216 \param comp component
217 \return name of the active unit system
219 QString QDS::activeUnitSystem( const QString& comp )
222 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
224 sys = toQString( comp.isEmpty() ? dic->GetActiveUnitSystem() :
225 dic->GetActiveUnitSystem( toAsciiString( comp ) ) );
230 \brief Set the active unit system.
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
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()
240 \param unit system to be set active
241 \param comp component
243 void QDS::setActiveUnitSystem( const QString& sys, const QString& comp )
245 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
249 TCollection_AsciiString system = toAsciiString( sys );
250 comp.isEmpty() ? dic->SetActiveUnitSystem( system ) :
251 dic->SetActiveUnitSystem( system, toAsciiString( comp ) );
253 QString unitSys = activeUnitSystem( comp );
254 if ( sys == unitSys )
257 TCollection_AsciiString aComp = toAsciiString( comp );
258 for ( QList<QDS_Datum*>::iterator it = _datumList.begin(); it != _datumList.end(); ++it )
260 QDS_Datum* datum = *it;
264 bool ok = aComp.IsEmpty();
267 Handle(DDS_DicItem) item = datum->dicItem();
268 ok = !item.IsNull() && aComp == item->GetComponent();
272 datum->unitSystemChanged( unitSys );
277 \brief Register given datum \a datum in the global list.
279 This function is invoked automatically by QDS_Datum constructor.
281 \param datum datum being registered
283 void QDS::insertDatum( QDS_Datum* datum )
288 _datumList.append( datum );
292 \brief Remove given datum \a datum from the global list.
294 This function is invoked automatically by QDS_Datum destructor.
296 \param datum datum being unregistered
298 void QDS::removeDatum( QDS_Datum* datum )
303 _datumList.removeAt( _datumList.indexOf(datum) );