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