Salome HOME
Merge branch 'BR_MULTI_BATHS' into HEAD
[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 #include <HYDROData_Tool.h>
21 #include <HYDROData_Iterator.h>
22
23 #include <TDataStd_NamedData.hxx>
24
25 #include <TDataStd_DataMapOfStringReal.hxx>
26 #include <TColStd_DataMapOfStringInteger.hxx>
27 #include <TDataStd_DataMapOfStringString.hxx>
28 #include <TDataStd_DataMapIteratorOfDataMapOfStringReal.hxx>
29 #include <TDataStd_DataMapIteratorOfDataMapOfStringString.hxx>
30 #include <TDataStd_AsciiString.hxx>
31
32 #include <TCollection_AsciiString.hxx>
33 #include <TCollection_ExtendedString.hxx>
34
35 #include <QString>
36 #include <QColor>
37 #include <QStringList>
38 #include <QFile>
39 #include <QTextStream>
40
41 IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
42 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
43
44 HYDROData_StricklerTable::HYDROData_StricklerTable()
45 : HYDROData_Entity( Geom_No )
46 {
47 }
48
49 HYDROData_StricklerTable::~HYDROData_StricklerTable()
50 {
51 }
52
53 const ObjectKind HYDROData_StricklerTable::GetKind() const
54 {
55   return KIND_STRICKLER_TABLE;
56 }
57
58 bool HYDROData_StricklerTable::Import( const QString& theFileName )
59 {
60   Handle(TDataStd_NamedData) aMap = Map();
61   if( aMap.IsNull() )
62     return false;
63
64   QFile aFile( theFileName );
65   if( !aFile.open( QFile::ReadOnly | QFile::Text ) )
66     return false;
67
68   aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
69   aMap->ChangeStrings( TDataStd_DataMapOfStringString() );
70   aMap->ChangeIntegers( TColStd_DataMapOfStringInteger() );
71
72   QTextStream aStream( &aFile );
73   int i=0;
74   while( !aStream.atEnd() )
75   {
76     QString aLine = aStream.readLine();
77     if( i==0 )
78     {
79       SetAttrName( aLine );
80     }
81     else
82     {
83       int p1 = aLine.indexOf( '"' );
84       int p2 = aLine.indexOf( '"', p1+1 );
85       QString aType = aLine.mid( p1+1, p2-p1-1 );
86       QStringList aParts = aLine.mid( p2+1 ).split( ' ', QString::SkipEmptyParts );
87       double aCoeff = aParts[0].toDouble();
88       int aColorInt = aParts[1].toInt( 0, 16 );
89       QString anAttrValue = aParts.size()>2 ? aParts[2] : QString();
90       
91       TCollection_ExtendedString aTypeExt = HYDROData_Tool::toExtString( aType );
92       aMap->SetReal( aTypeExt, aCoeff );
93       aMap->SetInteger( aTypeExt, aColorInt );
94       aMap->SetString( aTypeExt, HYDROData_Tool::toExtString( anAttrValue ) );
95     }
96     i++;
97   }
98   aFile.close();
99
100   return true;
101 }
102
103 bool HYDROData_StricklerTable::Export( const QString& theFileName )
104 {
105   Handle(TDataStd_NamedData) aMap = Map();
106   if( aMap.IsNull() )
107     return false;
108
109   QFile aFile( theFileName );
110   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
111     return false;
112
113   QTextStream aStream( &aFile );
114   aStream.setCodec( "UTF-8" );
115   aStream.setGenerateByteOrderMark( true );
116
117   aStream << GetAttrName() << "\n";
118
119   bool aRes = true;
120   QStringList aTypes = GetTypes();
121   foreach( QString aType, aTypes )
122   {
123     TCollection_ExtendedString aTypeExt = HYDROData_Tool::toExtString( aType );
124
125     aStream << "\"" << aType << "\" " << Get( aType, 0.0 );
126
127     QString aColor = QString::number( aMap->GetInteger( aTypeExt ), 16 ).toUpper();
128     aColor = QString( 6-aColor.length(), '0' ) + aColor;
129     QString anAttrValue = HYDROData_Tool::toQString( aMap->GetString( aTypeExt ) );
130
131     aStream << " " << aColor;
132     if( !anAttrValue.isEmpty() )
133       aStream << " " << anAttrValue;
134     aStream << "\n";
135   }
136
137   aFile.close();
138   return aRes;
139 }
140
141 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
142 {
143   Handle( TDataStd_NamedData ) aMap = Map();
144   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
145   if( aMap->HasReal( aType ) )
146     return aMap->GetReal( aType );
147   else
148     return theDefault;
149 }
150
151 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
152 {
153   Handle(TDataStd_NamedData) aMap = Map();
154   aMap->SetReal( HYDROData_Tool::toExtString( theType ), theCoefficient );
155 }
156
157 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
158 {
159   theMin = 0;
160   theMax = 0;
161   
162   Handle(TDataStd_NamedData) aMap = Map();
163   Standard_Boolean isFirst = Standard_True;
164   for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
165   {
166     Standard_Real aValue = it.Value();
167     if ( theMin == 0 || aValue < theMin )
168     {
169       theMin = aValue;
170     }
171     if ( theMax == 0 || aValue > theMax )
172     {
173       theMax = aValue;
174     }
175   }
176 }
177
178 QStringList HYDROData_StricklerTable::GetTypes() const
179 {
180   QStringList aSeq;
181   Handle(TDataStd_NamedData) aMap = Map();
182   if( !aMap.IsNull() )
183   {
184     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
185        aSeq.append( HYDROData_Tool::toQString( it.Key() ) );
186   }
187   aSeq.sort();
188   return aSeq;
189 }
190
191 bool HYDROData_StricklerTable::HasType( const QString& theType ) const
192 {
193   Handle(TDataStd_NamedData) aMap = Map();
194   
195   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
196   return !aMap.IsNull() && aMap->HasReal( aType );
197 }
198
199 void HYDROData_StricklerTable::Clear()
200 {
201   Handle(TDataStd_NamedData) aMap = Map();
202   if( !aMap.IsNull() )
203     aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
204 }
205
206 QStringList HYDROData_StricklerTable::DumpToPython( const QString& thePyScriptPath,
207                                                     MapOfTreatedObjects& theTreatedObjects ) const
208 {
209   QStringList aResList = dumpObjectCreation( theTreatedObjects );
210   QString aPyName = GetObjPyName();
211
212   QString anAttrName = GetAttrName();
213   aResList << QString( "%1.SetAttrName( \"%2\" )" ).arg( aPyName ).arg( anAttrName );
214
215   aResList << QString( "" );
216   Handle(TDataStd_NamedData) aMap = Map();
217   if( !aMap.IsNull() )
218   {
219     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
220     {
221       QString aType = HYDROData_Tool::toQString( it.Key() );
222       Standard_Real aValue = it.Value();
223       aResList << QString( "%1.Set( u\"%2\", %3 )" ).arg( aPyName ).arg( aType ).arg( aValue );
224
225       QString anAttrValue = GetAttrValue( aType );
226       aResList << QString( "%1.SetAttrValue( u\"%2\", \"%3\" )" ).arg( aPyName ).arg( aType ).arg( anAttrValue );
227       
228       QColor aColor = GetColor( aType );
229       aResList << QString( "%1.SetColor( u\"%2\", QColor( %3, %4, %5 ) )" ).
230         arg( aPyName ).arg( aType ).arg( aColor.red() ).arg( aColor.green() ).arg( aColor.blue() );
231       aResList << QString();
232     }
233   }
234   aResList << QString( "" );
235   aResList << QString( "%1.Update()" ).arg( aPyName );
236
237   return aResList;
238 }
239
240 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
241 {
242   TDF_Label aLabel = myLab.FindChild( DataTag_Table );
243   Handle( TDataStd_NamedData ) aMap;
244   if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
245     aMap = TDataStd_NamedData::Set( aLabel );
246   return aMap;
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 bool HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
259 {
260   HYDROData_Iterator anIt( HYDROData_Document::Document( myLab ), KIND_STRICKLER_TABLE );
261   for( ; anIt.More(); anIt.Next() )
262   {
263     Handle( HYDROData_StricklerTable ) aTable = 
264       Handle( HYDROData_StricklerTable )::DownCast( anIt.Current() );
265     if( aTable->Label()==myLab )
266       continue;
267
268     if( theAttrName==aTable->GetAttrName() )
269       return false;
270   }
271
272   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), HYDROData_Tool::toExtString( theAttrName ) );
273   return true;
274 }
275
276 QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
277 {
278   Handle( TDataStd_NamedData ) aMap = Map();
279   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
280   if( aMap->HasString( aType ) )
281     return HYDROData_Tool::toQString( aMap->GetString( aType ) );
282   else
283     return "";
284 }
285
286 void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
287 {
288   Handle( TDataStd_NamedData ) aMap = Map();
289   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
290   aMap->SetString( aType, HYDROData_Tool::toExtString( theAttrValue ) );
291 }
292
293 QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
294 {
295   if( theAttrValue.isEmpty() )
296     return "";
297
298   Handle( TDataStd_NamedData ) aMap = Map();
299   TCollection_ExtendedString anAttrValue = HYDROData_Tool::toExtString( theAttrValue );
300   for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() )
301   {
302     if( it.Value() == anAttrValue )
303     {
304       QString aType = HYDROData_Tool::toQString( it.Key() );
305       return aType;
306     }
307   }
308   return "";
309 }
310
311 QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const
312 {
313   Handle( TDataStd_NamedData ) aMap = Map();
314   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
315   if( aMap->HasInteger( aType ) )
316   {
317     int aColorInt = aMap->GetInteger( aType );
318     int b = aColorInt % 256;
319     int g = (aColorInt>>8) % 256;
320     int r = (aColorInt>>16) % 256;
321     return QColor( r, g, b );
322   }
323   else
324     return QColor();
325 }
326
327 void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const
328 {
329   Handle( TDataStd_NamedData ) aMap = Map();
330   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
331   int r = theColor.red();
332   int g = theColor.green();
333   int b = theColor.blue();
334   int aColorInt = ( r<<16 ) + ( g<<8 ) + b;
335   aMap->SetInteger( aType, aColorInt );
336
337   // synchronize the color for the same type in other maps
338   HYDROData_Iterator anIt( HYDROData_Document::Document( myLab ), KIND_STRICKLER_TABLE );
339   for( ; anIt.More(); anIt.Next() )
340   {
341     Handle( HYDROData_StricklerTable ) aTable = 
342       Handle( HYDROData_StricklerTable )::DownCast( anIt.Current() );
343     if( aTable->Label()==myLab )
344       continue;
345
346     if( aTable->HasType( theType ) )
347       aTable->Map()->SetInteger( aType, aColorInt );
348   }
349 }