Back to index

salome-gui  6.5.0
QDS.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "QDS.h"
00024 
00025 #include "QDS_Datum.h"
00026 
00027 #include <QTextCodec>
00028 
00029 #include <DDS_DicItem.h>
00030 #include <DDS_Dictionary.h>
00031 
00032 #include <TCollection_HAsciiString.hxx>
00033 #include <TCollection_HExtendedString.hxx>
00034 
00035 QList<QDS_Datum*> QDS::_datumList;
00036 
00047 QString QDS::toQString( const TCollection_AsciiString& src )
00048 {
00049   QTextCodec* codec = QTextCodec::codecForLocale();
00050   QString res;
00051   if ( !src.IsEmpty() )
00052     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
00053                   QString( (char*)src.ToCString() );
00054   return res;
00055 }
00056 
00062 QString QDS::toQString( const TCollection_ExtendedString& src )
00063 {
00064   if ( src.IsAscii() )
00065     return toQString( TCollection_AsciiString( src ) );
00066   else
00067     return QString( (QChar*)src.ToExtString(), src.Length() );
00068 }
00069 
00075 QString QDS::toQString( const Handle(TCollection_HAsciiString)& src )
00076 {
00077   if ( src.IsNull() )
00078     return QString();
00079   else
00080     return toQString( src->String() );
00081 }
00082 
00088 QString QDS::toQString( const Handle(TCollection_HExtendedString)& src )
00089 {
00090   if ( src.IsNull() )
00091     return QString();
00092   else
00093     return toQString( src->String() );
00094 }
00095 
00101 TCollection_AsciiString QDS::toAsciiString( const QString& src )
00102 {
00103   TCollection_AsciiString res;
00104   if ( src.toLatin1().constData() )
00105   {
00106     QTextCodec* codec = QTextCodec::codecForLocale();
00107     if ( codec )
00108     {
00109       QByteArray str = codec->fromUnicode( src );
00110       res = TCollection_AsciiString( (Standard_CString)(const char*)str, str.size() );
00111     }
00112     else
00113       res = TCollection_AsciiString( (char*)src.toLatin1().constData() );
00114   }
00115   return res;
00116 }
00117 
00123 TCollection_AsciiString QDS::toAsciiString( const TCollection_ExtendedString& src )
00124 {
00125   return TCollection_AsciiString( src );
00126 }
00127 
00133 TCollection_AsciiString QDS::toAsciiString( const Handle(TCollection_HExtendedString)& src )
00134 {
00135   TCollection_AsciiString res;
00136   if ( !src.IsNull() )
00137     res = toAsciiString( src->String() );
00138   return res;
00139 }
00140 
00146 TCollection_ExtendedString QDS::toExtString( const QString& src )
00147 {
00148   if ( src.isEmpty() )
00149     return TCollection_ExtendedString();
00150 
00151   Standard_Integer len = src.length();
00152   Standard_ExtCharacter* extStr = new Standard_ExtCharacter[( len + 1 ) * 2];
00153   memcpy( extStr, src.unicode(), len * 2 );
00154   extStr[len] = 0;
00155 
00156   TCollection_ExtendedString trg( extStr );
00157 
00158   delete [] extStr;
00159 
00160   return trg;
00161 }
00162 
00168 TCollection_ExtendedString QDS::toExtString( const TCollection_AsciiString& src )
00169 {
00170   return TCollection_ExtendedString( src );
00171 }
00172 
00177 bool QDS::load( const QString& dictPath )
00178 {
00179   if ( dictPath.isEmpty() )
00180     return false;
00181 
00182   return DDS_Dictionary::Load( toAsciiString( dictPath ) );
00183 }
00184 
00196 QString QDS::unitSystemLabel( const QString& sys, const QString& comp )
00197 {
00198   QString lab;
00199   TCollection_AsciiString system = toAsciiString( sys );
00200   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
00201   if ( !dic.IsNull() )
00202     lab = toQString( comp.isEmpty() ? dic->GetUnitSystemLabel( system ) :
00203                                       dic->GetUnitSystemLabel( system, toAsciiString( comp ) ) );
00204   return lab;
00205 }
00206 
00219 QString QDS::activeUnitSystem( const QString& comp )
00220 {
00221   QString sys;
00222   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
00223   if ( !dic.IsNull() )
00224     sys = toQString( comp.isEmpty() ? dic->GetActiveUnitSystem() :
00225                                       dic->GetActiveUnitSystem( toAsciiString( comp ) ) );
00226   return sys;
00227 }
00228 
00243 void QDS::setActiveUnitSystem( const QString& sys, const QString& comp )
00244 {
00245   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
00246   if ( dic.IsNull() )
00247     return;
00248 
00249   TCollection_AsciiString system = toAsciiString( sys );
00250   comp.isEmpty() ? dic->SetActiveUnitSystem( system ) :
00251                    dic->SetActiveUnitSystem( system, toAsciiString( comp ) );
00252 
00253   QString unitSys = activeUnitSystem( comp );
00254   if ( sys == unitSys )
00255     return;
00256 
00257   TCollection_AsciiString aComp = toAsciiString( comp );
00258   for ( QList<QDS_Datum*>::iterator it = _datumList.begin(); it != _datumList.end(); ++it )
00259   {
00260     QDS_Datum* datum = *it;
00261     if ( !datum )
00262       continue;
00263 
00264     bool ok = aComp.IsEmpty();
00265     if ( !ok )
00266     {
00267       Handle(DDS_DicItem) item = datum->dicItem();
00268       ok = !item.IsNull() && aComp == item->GetComponent();
00269     }
00270 
00271     if ( ok )
00272       datum->unitSystemChanged( unitSys );
00273   }
00274 }
00275 
00283 void QDS::insertDatum( QDS_Datum* datum )
00284 {
00285   if ( !datum )
00286     return;
00287 
00288   _datumList.append( datum );
00289 }
00290 
00298 void QDS::removeDatum( QDS_Datum* datum )
00299 {
00300   if ( !datum )
00301     return;
00302 
00303   _datumList.removeAt( _datumList.indexOf(datum) );
00304 }