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>
21 #include <TDataStd_NamedData.hxx>
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>
30 #include <TCollection_AsciiString.hxx>
31 #include <TCollection_ExtendedString.hxx>
35 #include <QStringList>
37 #include <QTextStream>
39 IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
40 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
42 const char ATTR_SUFFIX[] = " attr_value";
44 HYDROData_StricklerTable::HYDROData_StricklerTable()
48 HYDROData_StricklerTable::~HYDROData_StricklerTable()
52 const ObjectKind HYDROData_StricklerTable::GetKind() const
54 return KIND_STRICKLER_TABLE;
57 bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName )
59 Handle(TDataStd_NamedData) aMap = Map();
63 QFile aFile( theFileName.ToCString() );
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 = toExtString( aType );
91 aMap->SetReal( aTypeExt, aCoeff );
92 aMap->SetInteger( aTypeExt, aColorInt );
93 aMap->SetString( aTypeExt, toExtString( anAttrValue ) );
102 bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName )
104 Handle(TDataStd_NamedData) aMap = Map();
108 QFile aFile( theFileName.ToCString() );
109 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
112 QTextStream aStream( &aFile );
113 aStream.setCodec( "UTF-8" );
115 aStream << GetAttrName() << "\n";
118 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() )
120 QString aType = toQString( it.Key() );
121 aStream << "\"" << aType << "\" " << it.Value();
123 QString aColor = QString::number( aMap->GetInteger( it.Key() ), 16 ).toUpper();
124 QString anAttrValue = toQString( aMap->GetString( it.Key() ) );
126 aStream << " " << aColor << " " << anAttrValue << "\n";
133 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
135 Handle( TDataStd_NamedData ) aMap = Map();
136 TCollection_ExtendedString aType = toExtString( theType );
137 if( aMap->HasReal( aType ) )
138 return aMap->GetReal( aType );
143 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
145 Handle(TDataStd_NamedData) aMap = Map();
146 aMap->SetReal( toExtString( theType ), theCoefficient );
149 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
154 Handle(TDataStd_NamedData) aMap = Map();
155 Standard_Boolean isFirst = Standard_True;
156 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
158 Standard_Real aValue = it.Value();
159 if ( theMin == 0 || aValue < theMin )
163 if ( theMax == 0 || aValue > theMax )
170 QStringList HYDROData_StricklerTable::GetTypes() const
173 Handle(TDataStd_NamedData) aMap = Map();
176 for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
177 aSeq.append( toQString( it.Key() ) );
182 bool HYDROData_StricklerTable::HasType( const QString& theType ) const
184 Handle(TDataStd_NamedData) aMap = Map();
186 TCollection_ExtendedString aType = toExtString( theType );
187 return !aMap.IsNull() && aMap->HasReal( aType );
190 void HYDROData_StricklerTable::Clear()
192 Handle(TDataStd_NamedData) aMap = Map();
194 aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
197 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
199 QStringList aResList = dumpObjectCreation( theTreatedObjects );
200 QString aPyName = GetObjPyName();
202 aResList << QString( "" );
203 Handle(TDataStd_NamedData) aMap = Map();
206 for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
208 TCollection_ExtendedString aType = it.Key();
209 Standard_Real aValue = it.Value();
210 aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
213 aResList << QString( "" );
214 aResList << QString( "%1.Update();" ).arg( aPyName );
219 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
221 TDF_Label aLabel = myLab.FindChild( DataTag_Table );
222 Handle( TDataStd_NamedData ) aMap;
223 if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
224 aMap = TDataStd_NamedData::Set( aLabel );
228 TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
230 TCollection_ExtendedString aRes;
231 if( !theStr.isEmpty() )
233 Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
234 memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
235 ((short*)extStr)[theStr.length()] = '\0';
236 aRes = TCollection_ExtendedString( extStr );
242 QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
244 return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
247 QString HYDROData_StricklerTable::GetAttrName() const
249 Handle(TDataStd_AsciiString) aName;
250 if( myLab.FindChild( DataTag_AttrName ).FindAttribute(TDataStd_AsciiString::GetID(), aName)) {
251 TCollection_AsciiString aStr(aName->Get());
252 return QString(aStr.ToCString());
256 void HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
258 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), toExtString( theAttrName ) );
261 QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
263 Handle( TDataStd_NamedData ) aMap = Map();
264 TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
265 if( aMap->HasString( aType ) )
266 return toQString( aMap->GetString( aType ) );
271 void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
273 Handle( TDataStd_NamedData ) aMap = Map();
274 TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
275 aMap->SetString( aType, toExtString( theAttrValue ) );
278 QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
280 Handle( TDataStd_NamedData ) aMap = Map();
281 TCollection_ExtendedString anAttrValue = toExtString( theAttrValue );
282 int l = strlen( ATTR_SUFFIX );
283 for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() )
285 if( it.Value() == anAttrValue )
287 QString aType = toQString( it.Key() );
288 aType.remove( aType.length() - l, l );
295 QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const
297 Handle( TDataStd_NamedData ) aMap = Map();
298 TCollection_ExtendedString aType = toExtString( theType );
299 if( aMap->HasInteger( aType ) )
301 int aColorInt = aMap->GetInteger( aType );
302 int b = aColorInt % 256;
303 int g = (aColorInt>>8) % 256;
304 int r = (aColorInt>>16) % 256;
305 return QColor( r, g, b );
311 void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const
313 Handle( TDataStd_NamedData ) aMap = Map();
314 TCollection_ExtendedString aType = toExtString( theType );
315 int r = theColor.red();
316 int g = theColor.green();
317 int b = theColor.blue();
318 int aColorInt = ( r<<16 ) + ( g<<8 ) + b;
319 aMap->SetInteger( aType, aColorInt );