]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_StricklerTable.cxx
Salome HOME
7a16590720ce024879237d099ba57a24ffd9b433
[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 const char ATTR_SUFFIX[] = " attr_value";
43
44 HYDROData_StricklerTable::HYDROData_StricklerTable()
45 {
46 }
47
48 HYDROData_StricklerTable::~HYDROData_StricklerTable()
49 {
50 }
51
52 const ObjectKind HYDROData_StricklerTable::GetKind() const
53 {
54   return KIND_STRICKLER_TABLE;
55 }
56
57 bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName )
58 {
59   Handle(TDataStd_NamedData) aMap = Map();
60   if( aMap.IsNull() )
61     return false;
62
63   QFile aFile( theFileName.ToCString() );
64   if( !aFile.open( QFile::ReadOnly | QFile::Text ) )
65     return false;
66
67   aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
68   aMap->ChangeStrings( TDataStd_DataMapOfStringString() );
69   aMap->ChangeIntegers( TColStd_DataMapOfStringInteger() );
70
71   QTextStream aStream( &aFile );
72   int i=0;
73   while( !aStream.atEnd() )
74   {
75     QString aLine = aStream.readLine();
76     if( i==0 )
77     {
78       SetAttrName( aLine );
79     }
80     else
81     {
82       int p1 = aLine.indexOf( '"' );
83       int p2 = aLine.indexOf( '"', p1+1 );
84       QString aType = aLine.mid( p1+1, p2-p1-1 );
85       QStringList aParts = aLine.mid( p2+1 ).split( ' ', QString::SkipEmptyParts );
86       double aCoeff = aParts[0].toDouble();
87       int aColorInt = aParts[1].toInt( 0, 16 );
88       QString anAttrValue = aParts.size()>2 ? aParts[2] : QString();
89       
90       TCollection_ExtendedString aTypeExt = toExtString( aType );
91       aMap->SetReal( aTypeExt, aCoeff );
92       aMap->SetInteger( aTypeExt, aColorInt );
93       aMap->SetString( aTypeExt, toExtString( anAttrValue ) );
94     }
95     i++;
96   }
97   aFile.close();
98
99   return true;
100 }
101
102 bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName )
103 {
104   Handle(TDataStd_NamedData) aMap = Map();
105   if( aMap.IsNull() )
106     return false;
107
108   std::ofstream aStream( theFileName.ToCString(), std::ios::out | std::ios::binary );
109   if( !aStream )
110     return false;
111
112   aStream << GetAttrName().toStdString() << "\r\n";
113
114   bool aRes = true;
115   for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() )
116   {
117     TCollection_ExtendedString aType = it.Key();
118     TCollection_ExtendedString aLine = TCollection_ExtendedString( '\"' ) + aType + TCollection_ExtendedString( '\"' ) +
119     TCollection_ExtendedString( ' ' ) + TCollection_ExtendedString( it.Value() );
120     Standard_PCharacter aBuf = (Standard_PCharacter)malloc( aLine.LengthOfCString() + 1 );
121     aStream.write( aBuf, aLine.ToUTF8CString( aBuf ) );
122
123     QString aColor = QString::number( aMap->GetInteger( aType ), 16 );
124     QString anAttrValue = toQString( aMap->GetString( aType ) );
125
126     aStream << " " << aColor.toStdString() << " " << anAttrValue.toStdString();
127     aStream.write( "\r\n", 2 );
128     free( aBuf );
129   }
130
131   aStream.close();
132   return aRes;
133 }
134
135 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
136 {
137   Handle( TDataStd_NamedData ) aMap = Map();
138   TCollection_ExtendedString aType = toExtString( theType );
139   if( aMap->HasReal( aType ) )
140     return aMap->GetReal( aType );
141   else
142     return theDefault;
143 }
144
145 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
146 {
147   Handle(TDataStd_NamedData) aMap = Map();
148   aMap->SetReal( toExtString( theType ), theCoefficient );
149 }
150
151 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
152 {
153   theMin = 0;
154   theMax = 0;
155   
156   Handle(TDataStd_NamedData) aMap = Map();
157   Standard_Boolean isFirst = Standard_True;
158   for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
159   {
160     Standard_Real aValue = it.Value();
161     if ( theMin == 0 || aValue < theMin )
162     {
163       theMin = aValue;
164     }
165     if ( theMax == 0 || aValue > theMax )
166     {
167       theMax = aValue;
168     }
169   }
170 }
171
172 QStringList HYDROData_StricklerTable::GetTypes() const
173 {
174   QStringList aSeq;
175   Handle(TDataStd_NamedData) aMap = Map();
176   if( !aMap.IsNull() )
177   {
178     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
179        aSeq.append( toQString( it.Key() ) );
180   }
181   return aSeq;
182 }
183
184 bool HYDROData_StricklerTable::HasType( const QString& theType ) const
185 {
186   Handle(TDataStd_NamedData) aMap = Map();
187   
188   TCollection_ExtendedString aType = toExtString( theType );
189   return !aMap.IsNull() && aMap->HasReal( aType );
190 }
191
192 void HYDROData_StricklerTable::Clear()
193 {
194   Handle(TDataStd_NamedData) aMap = Map();
195   if( !aMap.IsNull() )
196     aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
197 }
198
199 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
200 {
201   QStringList aResList = dumpObjectCreation( theTreatedObjects );
202   QString aPyName = GetObjPyName();
203
204   aResList << QString( "" );
205   Handle(TDataStd_NamedData) aMap = Map();
206   if( !aMap.IsNull() )
207   {
208     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
209     {
210       TCollection_ExtendedString aType = it.Key();
211       Standard_Real aValue = it.Value();
212       aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
213     }
214   }
215   aResList << QString( "" );
216   aResList << QString( "%1.Update();" ).arg( aPyName );
217
218   return aResList;
219 }
220
221 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
222 {
223   TDF_Label aLabel = myLab.FindChild( DataTag_Table );
224   Handle( TDataStd_NamedData ) aMap;
225   if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
226     aMap = TDataStd_NamedData::Set( aLabel );
227   return aMap;
228 }
229
230 TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
231 {
232   TCollection_ExtendedString aRes;
233   if( !theStr.isEmpty() )
234   {
235           Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
236           memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
237           ((short*)extStr)[theStr.length()] = '\0';
238     aRes = TCollection_ExtendedString( extStr );
239           delete [] extStr;
240   }
241   return aRes;
242 }
243
244 QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
245 {
246   return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
247 }
248
249 QString HYDROData_StricklerTable::GetAttrName() const
250 {
251   Handle(TDataStd_AsciiString) aName;
252   if( myLab.FindChild( DataTag_AttrName ).FindAttribute(TDataStd_AsciiString::GetID(), aName)) {
253     TCollection_AsciiString aStr(aName->Get());
254     return QString(aStr.ToCString());
255   }
256   return QString();}
257
258 void HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
259 {
260   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), toExtString( theAttrName ) );
261 }
262
263 QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
264 {
265   Handle( TDataStd_NamedData ) aMap = Map();
266   TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
267   if( aMap->HasString( aType ) )
268     return toQString( aMap->GetString( aType ) );
269   else
270     return "";
271 }
272
273 void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
274 {
275   Handle( TDataStd_NamedData ) aMap = Map();
276   TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
277   aMap->SetString( aType, toExtString( theAttrValue ) );
278 }
279
280 QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
281 {
282   Handle( TDataStd_NamedData ) aMap = Map();
283   TCollection_ExtendedString anAttrValue = toExtString( theAttrValue );
284   int l = strlen( ATTR_SUFFIX );
285   for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() )
286   {
287     if( it.Value() == anAttrValue )
288     {
289       QString aType = toQString( it.Key() );
290       aType.remove( aType.length() - l, l );
291       return aType;
292     }
293   }
294   return "";
295 }
296
297 QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const
298 {
299   Handle( TDataStd_NamedData ) aMap = Map();
300   TCollection_ExtendedString aType = toExtString( theType );
301   if( aMap->HasInteger( aType ) )
302   {
303     int aColorInt = aMap->GetInteger( aType );
304     int b = aColorInt % 256;
305     int g = (aColorInt>>8) % 256;
306     int r = (aColorInt>>16) % 256;
307     return QColor( r, g, b );
308   }
309   else
310     return QColor();
311 }
312
313 void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const
314 {
315   Handle( TDataStd_NamedData ) aMap = Map();
316   TCollection_ExtendedString aType = toExtString( theType );
317   int r = theColor.red();
318   int g = theColor.green();
319   int b = theColor.blue();
320   int aColorInt = ( r<<16 ) + ( g<<8 ) + b;
321   aMap->SetInteger( aType, aColorInt );
322 }