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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROData_StricklerTable.h>
20 #include <HYDROData_Tool.h>
21 #include <HYDROData_Iterator.h>
23 #include <TDataStd_NamedData.hxx>
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>
32 #include <TCollection_AsciiString.hxx>
33 #include <TCollection_ExtendedString.hxx>
37 #include <QStringList>
39 #include <QTextStream>
41 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
43 HYDROData_StricklerTable::HYDROData_StricklerTable()
44 : HYDROData_Entity( Geom_No )
48 HYDROData_StricklerTable::~HYDROData_StricklerTable()
52 const ObjectKind HYDROData_StricklerTable::GetKind() const
54 return KIND_STRICKLER_TABLE;
57 bool HYDROData_StricklerTable::Import( const QString& theFileName )
59 Handle(TDataStd_NamedData) aMap = Map();
63 QFile aFile( theFileName );
64 if( !aFile.open( QFile::ReadOnly | QFile::Text ) )
67 aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
68 aMap->ChangeStrings( TDataStd_DataMapOfStringString() );
69 aMap->ChangeIntegers( TColStd_DataMapOfStringInteger() );
71 QTextStream aStream( &aFile );
73 while( !aStream.atEnd() )
75 QString aLine = aStream.readLine();
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();
90 TCollection_ExtendedString aTypeExt = HYDROData_Tool::toExtString( aType );
91 aMap->SetReal( aTypeExt, aCoeff );
92 aMap->SetInteger( aTypeExt, aColorInt );
93 aMap->SetString( aTypeExt, HYDROData_Tool::toExtString( anAttrValue ) );
102 bool HYDROData_StricklerTable::Export( const QString& theFileName )
104 Handle(TDataStd_NamedData) aMap = Map();
108 QFile aFile( theFileName );
109 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
112 QTextStream aStream( &aFile );
113 aStream.setCodec( "UTF-8" );
114 aStream.setGenerateByteOrderMark( true );
116 aStream << GetAttrName() << "\n";
119 QStringList aTypes = GetTypes();
120 foreach( QString aType, aTypes )
122 TCollection_ExtendedString aTypeExt = HYDROData_Tool::toExtString( aType );
124 aStream << "\"" << aType << "\" " << Get( aType, 0.0 );
126 QString aColor = QString::number( aMap->GetInteger( aTypeExt ), 16 ).toUpper();
127 aColor = QString( 6-aColor.length(), '0' ) + aColor;
128 QString anAttrValue = HYDROData_Tool::toQString( aMap->GetString( aTypeExt ) );
130 aStream << " " << aColor;
131 if( !anAttrValue.isEmpty() )
132 aStream << " " << anAttrValue;
140 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
142 Handle( TDataStd_NamedData ) aMap = Map();
143 TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
144 if( aMap->HasReal( aType ) )
145 return aMap->GetReal( aType );
150 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
152 Handle(TDataStd_NamedData) aMap = Map();
153 aMap->SetReal( HYDROData_Tool::toExtString( theType ), theCoefficient );
156 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
161 Handle(TDataStd_NamedData) aMap = Map();
162 Standard_Boolean isFirst = Standard_True;
163 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
165 Standard_Real aValue = it.Value();
166 if ( theMin == 0 || aValue < theMin )
170 if ( theMax == 0 || aValue > theMax )
177 QStringList HYDROData_StricklerTable::GetTypes() const
180 Handle(TDataStd_NamedData) aMap = Map();
183 for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
184 aSeq.append( HYDROData_Tool::toQString( it.Key() ) );
190 bool HYDROData_StricklerTable::HasType( const QString& theType ) const
192 Handle(TDataStd_NamedData) aMap = Map();
194 TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
195 return !aMap.IsNull() && aMap->HasReal( aType );
198 void HYDROData_StricklerTable::Clear()
200 Handle(TDataStd_NamedData) aMap = Map();
202 aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
205 QStringList HYDROData_StricklerTable::DumpToPython( const QString& thePyScriptPath,
206 MapOfTreatedObjects& theTreatedObjects ) const
208 QStringList aResList = dumpObjectCreation( theTreatedObjects );
209 QString aPyName = GetObjPyName();
211 QString anAttrName = GetAttrName();
212 aResList << QString( "%1.SetAttrName( \"%2\" )" ).arg( aPyName ).arg( anAttrName );
214 aResList << QString( "" );
215 Handle(TDataStd_NamedData) aMap = Map();
218 for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
220 QString aType = HYDROData_Tool::toQString( it.Key() );
221 Standard_Real aValue = it.Value();
222 aResList << QString( "%1.Set( u\"%2\", %3 )" ).arg( aPyName ).arg( aType ).arg( aValue );
224 QString anAttrValue = GetAttrValue( aType );
225 aResList << QString( "%1.SetAttrValue( u\"%2\", \"%3\" )" ).arg( aPyName ).arg( aType ).arg( anAttrValue );
227 QColor aColor = GetColor( aType );
228 aResList << QString( "%1.SetColor( u\"%2\", QColor( %3, %4, %5 ) )" ).
229 arg( aPyName ).arg( aType ).arg( aColor.red() ).arg( aColor.green() ).arg( aColor.blue() );
230 aResList << QString();
233 aResList << QString( "" );
234 aResList << QString( "%1.Update()" ).arg( aPyName );
239 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
241 TDF_Label aLabel = myLab.FindChild( DataTag_Table );
242 Handle( TDataStd_NamedData ) aMap;
243 if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
244 aMap = TDataStd_NamedData::Set( aLabel );
248 QString HYDROData_StricklerTable::GetAttrName() const
250 Handle(TDataStd_AsciiString) aName;
251 if( myLab.FindChild( DataTag_AttrName ).FindAttribute(TDataStd_AsciiString::GetID(), aName)) {
252 TCollection_AsciiString aStr(aName->Get());
253 return QString(aStr.ToCString());
257 bool HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
259 HYDROData_Iterator anIt( HYDROData_Document::Document(), KIND_STRICKLER_TABLE );
260 for( ; anIt.More(); anIt.Next() )
262 Handle( HYDROData_StricklerTable ) aTable =
263 Handle( HYDROData_StricklerTable )::DownCast( anIt.Current() );
264 if( aTable->Label()==myLab )
267 if( theAttrName==aTable->GetAttrName() )
271 Handle(TDataStd_AsciiString) anAttr = TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), HYDROData_Tool::toExtString( theAttrName ) );
272 anAttr->SetID(TDataStd_AsciiString::GetID());
276 QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
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 ) );
286 void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
288 Handle( TDataStd_NamedData ) aMap = Map();
289 TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
290 aMap->SetString( aType, HYDROData_Tool::toExtString( theAttrValue ) );
293 QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
295 if( theAttrValue.isEmpty() )
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() )
302 if( it.Value() == anAttrValue )
304 QString aType = HYDROData_Tool::toQString( it.Key() );
311 QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const
313 Handle( TDataStd_NamedData ) aMap = Map();
314 TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
315 if( aMap->HasInteger( aType ) )
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 );
327 void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const
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 );
337 // synchronize the color for the same type in other maps
338 HYDROData_Iterator anIt( HYDROData_Document::Document(), KIND_STRICKLER_TABLE );
339 for( ; anIt.More(); anIt.Next() )
341 Handle( HYDROData_StricklerTable ) aTable =
342 Handle( HYDROData_StricklerTable )::DownCast( anIt.Current() );
343 if( aTable->Label()==myLab )
346 if( aTable->HasType( theType ) )
347 aTable->Map()->SetInteger( aType, aColorInt );