]> SALOME platform Git repositories - modules/gui.git/blob - src/QDS/QDS.cxx
Salome HOME
fb1cdae2448ec31ca1181cfee38d4c587d2517d2
[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/ or email : webmaster.salome@opencascade.com
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 /*!
34   Convert the OpenCascade ascii string to Qt string.
35 */
36 QString QDS::toQString( const TCollection_AsciiString& src )
37 {
38   QTextCodec* codec = QTextCodec::codecForLocale();
39   QString res;
40   if ( !src.IsEmpty() )
41     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
42                   QString( (char*)src.ToCString() );
43   return res;
44 }
45
46 /*!
47   Convert the OpenCascade unicode string to Qt string.
48 */
49 QString QDS::toQString( const TCollection_ExtendedString& src )
50 {
51   if ( src.IsAscii() )
52     return toQString( TCollection_AsciiString( src ) );
53   else
54     return QString( (QChar*)src.ToExtString(), src.Length() );
55 }
56
57 /*!
58   Convert the OpenCascade ascii string to Qt string.
59 */
60 QString QDS::toQString( const Handle(TCollection_HAsciiString)& src )
61 {
62   if ( src.IsNull() )
63     return QString::null;
64   else
65     return toQString( src->String() );
66 }
67
68 /*!
69   Convert the OpenCascade unicode string to Qt string.
70 */
71 QString QDS::toQString( const Handle(TCollection_HExtendedString)& src )
72 {
73   if ( src.IsNull() )
74     return QString::null;
75   else
76     return toQString( src->String() );
77 }
78
79 /*!
80   Convert the Qt string to OpenCascade ascii string.
81 */
82 TCollection_AsciiString QDS::toAsciiString( const QString& src )
83 {
84   TCollection_AsciiString res;
85   if ( src.latin1() )
86   {
87     QTextCodec* codec = QTextCodec::codecForLocale();
88     if ( codec )
89     {
90       int len = -1;
91       QCString str = codec->fromUnicode( src, len );
92       res = TCollection_AsciiString( (Standard_CString)(const char*)str, len );
93     }
94     else
95       res = TCollection_AsciiString( (char*)src.latin1() );
96   }
97   return res;
98 }
99
100 /*!
101   Convert the OpenCascade unicode string to OpenCascade ascii string.
102 */
103 TCollection_AsciiString QDS::toAsciiString( const TCollection_ExtendedString& src )
104 {
105   return TCollection_AsciiString( src );
106 }
107
108 /*!
109   Convert the OpenCascade unicode string to OpenCascade ascii string.
110 */
111 TCollection_AsciiString QDS::toAsciiString( const Handle(TCollection_HExtendedString)& src )
112 {
113   TCollection_AsciiString res;
114   if ( !src.IsNull() )
115     res = toAsciiString( src->String() );
116   return res;
117 }
118
119 /*!
120   Convert the Qt string to OpenCascade unicode string.
121 */
122 TCollection_ExtendedString QDS::toExtString( const QString& src )
123 {
124   if ( src.isEmpty() )
125     return TCollection_ExtendedString();
126
127   Standard_Integer len = src.length();
128   Standard_ExtString extStr = new Standard_ExtCharacter[( len + 1 ) * 2];
129   memcpy( extStr, src.unicode(), len * 2 );
130   extStr[len] = 0;
131
132   TCollection_ExtendedString trg( extStr );
133
134   delete [] extStr;
135
136   return trg;
137 }
138
139 /*!
140   Convert the OpenCascade ascii string to OpenCascade unicode string.
141 */
142 TCollection_ExtendedString QDS::toExtString( const TCollection_AsciiString& src )
143 {
144   return TCollection_ExtendedString( src );
145 }
146
147 /*!
148   Load datum definitions in the dictionary from XML file \adictPath.
149   Returns true if load successed or false otherwise.
150 */
151 bool QDS::load( const QString& dictPath )
152 {
153   if ( dictPath.isEmpty() )
154     return false;
155
156   return DDS_Dictionary::Load( toAsciiString( dictPath ) );
157 }
158
159 /*!
160   Returns the label of unit system \asys. If component \acomp specified and not empty then
161   function find the given unit system in the given component otherwise all components will be searched.
162   If unit system not found then empty string returned.
163 */
164 QString QDS::unitSystemLabel( const QString& sys, const QString& comp )
165 {
166   QString lab;
167   TCollection_AsciiString system = toAsciiString( sys );
168   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
169   if ( !dic.IsNull() )
170     lab = toQString( comp.isEmpty() ? dic->GetUnitSystemLabel( system ) :
171                                       dic->GetUnitSystemLabel( system, toAsciiString( comp ) ) );
172   return lab;
173 }
174
175 /*!
176   Gets the name of active unit system from the specified component \acomp.
177   If component not specified or component is empty string then first got component will be used.
178   If component exist then active unit system name returned or empty string otherwise.
179 */
180 QString QDS::activeUnitSystem( const QString& comp )
181 {
182   QString sys;
183   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
184   if ( !dic.IsNull() )
185     sys = toQString( comp.isEmpty() ? dic->GetActiveUnitSystem() :
186                                       dic->GetActiveUnitSystem( toAsciiString( comp ) ) );
187   return sys;
188 }
189
190 /*!
191   Sets the active unit system named \asys. If not empty component name \acomp specified then
192   unit system will be activated in the given component otherwise all components will be processed.
193
194   After the changing of active unit system function notify about it to all registered datums
195   from processed components using method QDS_Datum::unitSystemChanged();
196 */
197 void QDS::setActiveUnitSystem( const QString& sys, const QString& comp )
198 {
199   Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
200   if ( dic.IsNull() )
201     return;
202
203   TCollection_AsciiString system = toAsciiString( sys );
204   comp.isEmpty() ? dic->SetActiveUnitSystem( system ) :
205                    dic->SetActiveUnitSystem( system, toAsciiString( comp ) );
206
207   QString unitSys = activeUnitSystem( comp );
208   if ( sys == unitSys )
209     return;
210
211   TCollection_AsciiString aComp = toAsciiString( comp );
212   for ( QValueList<QDS_Datum*>::iterator it = _datumList.begin(); it != _datumList.end(); ++it )
213   {
214     QDS_Datum* datum = *it;
215     if ( !datum )
216       continue;
217
218     bool ok = aComp.IsEmpty();
219     if ( !ok )
220     {
221       Handle(DDS_DicItem) item = datum->dicItem();
222       ok = !item.IsNull() && aComp == item->GetComponent();
223     }
224
225     if ( ok )
226       datum->unitSystemChanged( unitSys );
227   }
228 }
229
230 /*!
231   Register given datum \adatum in the static list.
232   This function invoked by QDS_Datum constructor.
233 */
234 void QDS::insertDatum( QDS_Datum* datum )
235 {
236   if ( !datum )
237     return;
238
239   _datumList.append( datum );
240 }
241
242 /*!
243   Remove given datum \adatum from the static list.
244   This function invoked by QDS_Datum destructor.
245 */
246 void QDS::removeDatum( QDS_Datum* datum )
247 {
248   if ( !datum )
249     return;
250
251   _datumList.remove( datum );
252 }