]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_StricklerTable.cxx
Salome HOME
d206c59cb2d5c8a5dc862a659d19847e89c5237c
[modules/hydro.git] / src / HYDROData / HYDROData_StricklerTable.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROData_StricklerTable.h>
20
21 #include <TDataStd_NamedData.hxx>
22
23 #include <TDataStd_DataMapOfStringReal.hxx>
24 #include <TColStd_DataMapOfStringInteger.hxx>
25 #include <TDataStd_DataMapOfStringString.hxx>
26 #include <TDataStd_DataMapIteratorOfDataMapOfStringReal.hxx>
27 #include <TDataStd_DataMapIteratorOfDataMapOfStringString.hxx>
28 #include <TDataStd_AsciiString.hxx>
29
30 #include <TCollection_AsciiString.hxx>
31 #include <TCollection_ExtendedString.hxx>
32
33 #include <QString>
34 #include <QColor>
35 #include <QStringList>
36 #include <QFile>
37 #include <QTextStream>
38
39 IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
40 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
41
42 HYDROData_StricklerTable::HYDROData_StricklerTable()
43 {
44 }
45
46 HYDROData_StricklerTable::~HYDROData_StricklerTable()
47 {
48 }
49
50 const ObjectKind HYDROData_StricklerTable::GetKind() const
51 {
52   return KIND_STRICKLER_TABLE;
53 }
54
55 bool HYDROData_StricklerTable::Import( const QString& theFileName )
56 {
57   Handle(TDataStd_NamedData) aMap = Map();
58   if( aMap.IsNull() )
59     return false;
60
61   QFile aFile( theFileName );
62   if( !aFile.open( QFile::ReadOnly | QFile::Text ) )
63     return false;
64
65   aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
66   aMap->ChangeStrings( TDataStd_DataMapOfStringString() );
67   aMap->ChangeIntegers( TColStd_DataMapOfStringInteger() );
68
69   QTextStream aStream( &aFile );
70   int i=0;
71   while( !aStream.atEnd() )
72   {
73     QString aLine = aStream.readLine();
74     if( i==0 )
75     {
76       SetAttrName( aLine );
77     }
78     else
79     {
80       int p1 = aLine.indexOf( '"' );
81       int p2 = aLine.indexOf( '"', p1+1 );
82       QString aType = aLine.mid( p1+1, p2-p1-1 );
83       QStringList aParts = aLine.mid( p2+1 ).split( ' ', QString::SkipEmptyParts );
84       double aCoeff = aParts[0].toDouble();
85       int aColorInt = aParts[1].toInt( 0, 16 );
86       QString anAttrValue = aParts.size()>2 ? aParts[2] : QString();
87       
88       TCollection_ExtendedString aTypeExt = toExtString( aType );
89       aMap->SetReal( aTypeExt, aCoeff );
90       aMap->SetInteger( aTypeExt, aColorInt );
91       aMap->SetString( aTypeExt, toExtString( anAttrValue ) );
92     }
93     i++;
94   }
95   aFile.close();
96
97   return true;
98 }
99
100 bool HYDROData_StricklerTable::Export( const QString& theFileName )
101 {
102   Handle(TDataStd_NamedData) aMap = Map();
103   if( aMap.IsNull() )
104     return false;
105
106   QFile aFile( theFileName );
107   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
108     return false;
109
110   QTextStream aStream( &aFile );
111   aStream.setCodec( "UTF-8" );
112   aStream.setGenerateByteOrderMark( true );
113
114   aStream << GetAttrName() << "\n";
115
116   bool aRes = true;
117   QStringList aTypes = GetTypes();
118   foreach( QString aType, aTypes )
119   {
120     TCollection_ExtendedString aTypeExt = toExtString( aType );
121
122     aStream << "\"" << aType << "\" " << Get( aType, 0.0 );
123
124     QString aColor = QString::number( aMap->GetInteger( aTypeExt ), 16 ).toUpper();
125     aColor = QString( 6-aColor.length(), '0' ) + aColor;
126     QString anAttrValue = toQString( aMap->GetString( aTypeExt ) );
127
128     aStream << " " << aColor;
129     if( !anAttrValue.isEmpty() )
130       aStream << " " << anAttrValue;
131     aStream << "\n";
132   }
133
134   aFile.close();
135   return aRes;
136 }
137
138 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
139 {
140   Handle( TDataStd_NamedData ) aMap = Map();
141   TCollection_ExtendedString aType = toExtString( theType );
142   if( aMap->HasReal( aType ) )
143     return aMap->GetReal( aType );
144   else
145     return theDefault;
146 }
147
148 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
149 {
150   Handle(TDataStd_NamedData) aMap = Map();
151   aMap->SetReal( toExtString( theType ), theCoefficient );
152 }
153
154 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
155 {
156   theMin = 0;
157   theMax = 0;
158   
159   Handle(TDataStd_NamedData) aMap = Map();
160   Standard_Boolean isFirst = Standard_True;
161   for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
162   {
163     Standard_Real aValue = it.Value();
164     if ( theMin == 0 || aValue < theMin )
165     {
166       theMin = aValue;
167     }
168     if ( theMax == 0 || aValue > theMax )
169     {
170       theMax = aValue;
171     }
172   }
173 }
174
175 QStringList HYDROData_StricklerTable::GetTypes() const
176 {
177   QStringList aSeq;
178   Handle(TDataStd_NamedData) aMap = Map();
179   if( !aMap.IsNull() )
180   {
181     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
182        aSeq.append( toQString( it.Key() ) );
183   }
184   aSeq.sort();
185   return aSeq;
186 }
187
188 bool HYDROData_StricklerTable::HasType( const QString& theType ) const
189 {
190   Handle(TDataStd_NamedData) aMap = Map();
191   
192   TCollection_ExtendedString aType = toExtString( theType );
193   return !aMap.IsNull() && aMap->HasReal( aType );
194 }
195
196 void HYDROData_StricklerTable::Clear()
197 {
198   Handle(TDataStd_NamedData) aMap = Map();
199   if( !aMap.IsNull() )
200     aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
201 }
202
203 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
204 {
205   QStringList aResList = dumpObjectCreation( theTreatedObjects );
206   QString aPyName = GetObjPyName();
207
208   aResList << QString( "" );
209   Handle(TDataStd_NamedData) aMap = Map();
210   if( !aMap.IsNull() )
211   {
212     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
213     {
214       TCollection_ExtendedString aType = it.Key();
215       Standard_Real aValue = it.Value();
216       aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
217     }
218   }
219   aResList << QString( "" );
220   aResList << QString( "%1.Update();" ).arg( aPyName );
221
222   return aResList;
223 }
224
225 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
226 {
227   TDF_Label aLabel = myLab.FindChild( DataTag_Table );
228   Handle( TDataStd_NamedData ) aMap;
229   if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
230     aMap = TDataStd_NamedData::Set( aLabel );
231   return aMap;
232 }
233
234 TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
235 {
236   TCollection_ExtendedString aRes;
237   if( !theStr.isEmpty() )
238   {
239           Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
240           memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
241           ((short*)extStr)[theStr.length()] = '\0';
242     aRes = TCollection_ExtendedString( extStr );
243           delete [] extStr;
244   }
245   return aRes;
246 }
247
248 QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
249 {
250   return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
251 }
252
253 QString HYDROData_StricklerTable::GetAttrName() const
254 {
255   Handle(TDataStd_AsciiString) aName;
256   if( myLab.FindChild( DataTag_AttrName ).FindAttribute(TDataStd_AsciiString::GetID(), aName)) {
257     TCollection_AsciiString aStr(aName->Get());
258     return QString(aStr.ToCString());
259   }
260   return QString();}
261
262 void HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
263 {
264   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), toExtString( theAttrName ) );
265 }
266
267 QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
268 {
269   Handle( TDataStd_NamedData ) aMap = Map();
270   TCollection_ExtendedString aType = toExtString( theType );
271   if( aMap->HasString( aType ) )
272     return toQString( aMap->GetString( aType ) );
273   else
274     return "";
275 }
276
277 void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
278 {
279   Handle( TDataStd_NamedData ) aMap = Map();
280   TCollection_ExtendedString aType = toExtString( theType );
281   aMap->SetString( aType, toExtString( theAttrValue ) );
282 }
283
284 QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
285 {
286   Handle( TDataStd_NamedData ) aMap = Map();
287   TCollection_ExtendedString anAttrValue = toExtString( theAttrValue );
288   for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() )
289   {
290     if( it.Value() == anAttrValue )
291     {
292       QString aType = toQString( it.Key() );
293       return aType;
294     }
295   }
296   return "";
297 }
298
299 QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const
300 {
301   Handle( TDataStd_NamedData ) aMap = Map();
302   TCollection_ExtendedString aType = toExtString( theType );
303   if( aMap->HasInteger( aType ) )
304   {
305     int aColorInt = aMap->GetInteger( aType );
306     int b = aColorInt % 256;
307     int g = (aColorInt>>8) % 256;
308     int r = (aColorInt>>16) % 256;
309     return QColor( r, g, b );
310   }
311   else
312     return QColor();
313 }
314
315 void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const
316 {
317   Handle( TDataStd_NamedData ) aMap = Map();
318   TCollection_ExtendedString aType = toExtString( theType );
319   int r = theColor.red();
320   int g = theColor.green();
321   int b = theColor.blue();
322   int aColorInt = ( r<<16 ) + ( g<<8 ) + b;
323   aMap->SetInteger( aType, aColorInt );
324 }