Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / QDS / QDS.cxx
1 // Copyright (C) 2005  CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "QDS.h"
20
21 #include "QDS_Datum.h"
22
23 #include <qtextcodec.h>
24
25 #include <DDS_DicItem.h>
26 #include <DDS_Dictionary.h>
27
28 #include <TCollection_HAsciiString.hxx>
29 #include <TCollection_HExtendedString.hxx>
30
31 QValueList<QDS_Datum*> QDS::_datumList;
32
33 QString QDS::toQString( const TCollection_AsciiString& src )
34 {
35   QTextCodec* codec = QTextCodec::codecForLocale();
36   QString res;
37   if ( !src.IsEmpty() )
38     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
39                   QString( (char*)src.ToCString() );
40   return res;
41 }
42
43 QString QDS::toQString( const TCollection_ExtendedString& src )
44 {
45   if ( src.IsAscii() )
46     return toQString( TCollection_AsciiString( src ) );
47   else
48     return QString( (QChar*)src.ToExtString(), src.Length() );
49 }
50
51 QString QDS::toQString( const Handle(TCollection_HAsciiString)& src )
52 {
53   if ( src.IsNull() )
54     return QString::null;
55   else
56     return toQString( src->String() );
57 }
58
59 QString QDS::toQString( const Handle(TCollection_HExtendedString)& src )
60 {
61   if ( src.IsNull() )
62     return QString::null;
63   else
64     return toQString( src->String() );
65 }
66
67 TCollection_AsciiString QDS::toAsciiString( const QString& src )
68 {
69   TCollection_AsciiString res;
70   if ( src.latin1() )
71   {
72     QTextCodec* codec = QTextCodec::codecForLocale();
73     if ( codec )
74     {
75       int len = -1;
76       QCString str = codec->fromUnicode( src, len );
77       res = TCollection_AsciiString( (Standard_CString)(const char*)str, len );
78     }
79     else
80       res = TCollection_AsciiString( (char*)src.latin1() );
81   }
82   return res;
83 }
84
85 TCollection_AsciiString QDS::toAsciiString( const TCollection_ExtendedString& src )
86 {
87   return TCollection_AsciiString( src );
88 }
89
90 TCollection_AsciiString QDS::toAsciiString( const Handle(TCollection_HExtendedString)& src )
91 {
92   TCollection_AsciiString res;
93   if ( !src.IsNull() )
94     res = toAsciiString( src->String() );
95   return res;
96 }
97
98 TCollection_ExtendedString QDS::toExtString( const QString& src )
99 {
100   if ( src.isEmpty() )
101     return TCollection_ExtendedString();
102
103   Standard_Integer len = src.length();
104   Standard_ExtString extStr = new Standard_ExtCharacter[( len + 1 ) * 2];
105   memcpy( extStr, src.unicode(), len * 2 );
106   extStr[len] = 0;
107
108   TCollection_ExtendedString trg( extStr );
109
110   delete [] extStr;
111
112   return trg;
113 }
114
115 TCollection_ExtendedString QDS::toExtString( const TCollection_AsciiString& src )
116 {
117   return TCollection_ExtendedString( src );
118 }
119
120 bool QDS::load( const QString& dictPath )
121 {
122   if ( dictPath.isEmpty() )
123     return false;
124
125   return DDS_Dictionary::Load( toAsciiString( dictPath ) );
126 }
127
128 QString QDS::unitSystemLabel( const QString& sys, const QString& comp )
129 {
130   QString lab;
131   TCollection_AsciiString system = toAsciiString( sys );
132   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
133   if ( !dic.IsNull() )
134     lab = toQString( comp.isEmpty() ? dic->GetUnitSystemLabel( system ) :
135                                       dic->GetUnitSystemLabel( system, toAsciiString( comp ) ) );
136   return lab;
137 }
138
139 QString QDS::activeUnitSystem( const QString& comp )
140 {
141   QString sys;
142   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
143   if ( !dic.IsNull() )
144     sys = toQString( comp.isEmpty() ? dic->GetActiveUnitSystem() :
145                                       dic->GetActiveUnitSystem( toAsciiString( comp ) ) );
146   return sys;
147 }
148
149 void QDS::setActiveUnitSystem( const QString& sys, const QString& comp )
150 {
151   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
152   if ( dic.IsNull() )
153     return;
154
155   TCollection_AsciiString system = toAsciiString( sys );
156   comp.isEmpty() ? dic->SetActiveUnitSystem( system ) :
157                    dic->SetActiveUnitSystem( system, toAsciiString( comp ) );
158
159   QString unitSys = activeUnitSystem( comp );
160   if ( sys == unitSys )
161     return;
162
163   TCollection_AsciiString aComp = toAsciiString( comp );
164   for ( QValueList<QDS_Datum*>::iterator it = _datumList.begin(); it != _datumList.end(); ++it )
165   {
166     QDS_Datum* datum = *it;
167     if ( !datum )
168       continue;
169
170     bool ok = aComp.IsEmpty();
171     if ( !ok )
172     {
173       Handle(DDS_DicItem) item = datum->dicItem();
174       ok = !item.IsNull() && aComp == item->GetComponent();
175     }
176
177     if ( ok )
178       datum->unitSystemChanged( unitSys );
179   }
180 }
181
182 void QDS::insertDatum( QDS_Datum* datum )
183 {
184   if ( !datum )
185     return;
186
187   _datumList.append( datum );
188 }
189
190 void QDS::removeDatum( QDS_Datum* datum )
191 {
192   if ( !datum )
193     return;
194
195   _datumList.remove( datum );
196 }