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 HYDROData_StricklerTable::HYDROData_StricklerTable()
46 HYDROData_StricklerTable::~HYDROData_StricklerTable()
50 const ObjectKind HYDROData_StricklerTable::GetKind() const
52 return KIND_STRICKLER_TABLE;
55 bool HYDROData_StricklerTable::Import( const QString& theFileName )
57 Handle(TDataStd_NamedData) aMap = Map();
61 QFile aFile( theFileName );
62 if( !aFile.open( QFile::ReadOnly | QFile::Text ) )
65 aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
66 aMap->ChangeStrings( TDataStd_DataMapOfStringString() );
67 aMap->ChangeIntegers( TColStd_DataMapOfStringInteger() );
69 QTextStream aStream( &aFile );
71 while( !aStream.atEnd() )
73 QString aLine = aStream.readLine();
80 int p1 = aLine.indexOf( '"' );
81 int p2 = aLine.indexOf( '"', p1+1 );
82 QString aType = aLine.mid( p1+1, p2-p1-1 );
83 QStringList aParts = aLine.mid( p2+1 ).split( ' ', QString::SkipEmptyParts );
84 double aCoeff = aParts[0].toDouble();
85 int aColorInt = aParts[1].toInt( 0, 16 );
86 QString anAttrValue = aParts.size()>2 ? aParts[2] : QString();
88 TCollection_ExtendedString aTypeExt = toExtString( aType );
89 aMap->SetReal( aTypeExt, aCoeff );
90 aMap->SetInteger( aTypeExt, aColorInt );
91 aMap->SetString( aTypeExt, toExtString( anAttrValue ) );
100 bool HYDROData_StricklerTable::Export( const QString& theFileName )
102 Handle(TDataStd_NamedData) aMap = Map();
106 QFile aFile( theFileName );
107 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
110 QTextStream aStream( &aFile );
111 aStream.setCodec( "UTF-8" );
112 aStream.setGenerateByteOrderMark( true );
114 aStream << GetAttrName() << "\n";
117 QStringList aTypes = GetTypes();
118 foreach( QString aType, aTypes )
120 TCollection_ExtendedString aTypeExt = toExtString( aType );
122 aStream << "\"" << aType << "\" " << Get( aType, 0.0 );
124 QString aColor = QString::number( aMap->GetInteger( aTypeExt ), 16 ).toUpper();
125 aColor = QString( 6-aColor.length(), '0' ) + aColor;
126 QString anAttrValue = toQString( aMap->GetString( aTypeExt ) );
128 aStream << " " << aColor;
129 if( !anAttrValue.isEmpty() )
130 aStream << " " << anAttrValue;
138 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
140 Handle( TDataStd_NamedData ) aMap = Map();
141 TCollection_ExtendedString aType = toExtString( theType );
142 if( aMap->HasReal( aType ) )
143 return aMap->GetReal( aType );
148 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
150 Handle(TDataStd_NamedData) aMap = Map();
151 aMap->SetReal( toExtString( theType ), theCoefficient );
154 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
159 Handle(TDataStd_NamedData) aMap = Map();
160 Standard_Boolean isFirst = Standard_True;
161 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
163 Standard_Real aValue = it.Value();
164 if ( theMin == 0 || aValue < theMin )
168 if ( theMax == 0 || aValue > theMax )
175 QStringList HYDROData_StricklerTable::GetTypes() const
178 Handle(TDataStd_NamedData) aMap = Map();
181 for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
182 aSeq.append( toQString( it.Key() ) );
188 bool HYDROData_StricklerTable::HasType( const QString& theType ) const
190 Handle(TDataStd_NamedData) aMap = Map();
192 TCollection_ExtendedString aType = toExtString( theType );
193 return !aMap.IsNull() && aMap->HasReal( aType );
196 void HYDROData_StricklerTable::Clear()
198 Handle(TDataStd_NamedData) aMap = Map();
200 aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
203 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
205 QStringList aResList = dumpObjectCreation( theTreatedObjects );
206 QString aPyName = GetObjPyName();
208 aResList << QString( "" );
209 Handle(TDataStd_NamedData) aMap = Map();
212 for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
214 TCollection_ExtendedString aType = it.Key();
215 Standard_Real aValue = it.Value();
216 aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
219 aResList << QString( "" );
220 aResList << QString( "%1.Update();" ).arg( aPyName );
225 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
227 TDF_Label aLabel = myLab.FindChild( DataTag_Table );
228 Handle( TDataStd_NamedData ) aMap;
229 if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
230 aMap = TDataStd_NamedData::Set( aLabel );
234 TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
236 TCollection_ExtendedString aRes;
237 if( !theStr.isEmpty() )
239 Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
240 memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
241 ((short*)extStr)[theStr.length()] = '\0';
242 aRes = TCollection_ExtendedString( extStr );
248 QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
250 return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
253 QString HYDROData_StricklerTable::GetAttrName() const
255 Handle(TDataStd_AsciiString) aName;
256 if( myLab.FindChild( DataTag_AttrName ).FindAttribute(TDataStd_AsciiString::GetID(), aName)) {
257 TCollection_AsciiString aStr(aName->Get());
258 return QString(aStr.ToCString());
262 void HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
264 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), toExtString( theAttrName ) );
267 QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
269 Handle( TDataStd_NamedData ) aMap = Map();
270 TCollection_ExtendedString aType = toExtString( theType );
271 if( aMap->HasString( aType ) )
272 return toQString( aMap->GetString( aType ) );
277 void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
279 Handle( TDataStd_NamedData ) aMap = Map();
280 TCollection_ExtendedString aType = toExtString( theType );
281 aMap->SetString( aType, toExtString( theAttrValue ) );
284 QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
286 Handle( TDataStd_NamedData ) aMap = Map();
287 TCollection_ExtendedString anAttrValue = toExtString( theAttrValue );
288 for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() )
290 if( it.Value() == anAttrValue )
292 QString aType = toQString( it.Key() );
299 QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const
301 Handle( TDataStd_NamedData ) aMap = Map();
302 TCollection_ExtendedString aType = toExtString( theType );
303 if( aMap->HasInteger( aType ) )
305 int aColorInt = aMap->GetInteger( aType );
306 int b = aColorInt % 256;
307 int g = (aColorInt>>8) % 256;
308 int r = (aColorInt>>16) % 256;
309 return QColor( r, g, b );
315 void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const
317 Handle( TDataStd_NamedData ) aMap = Map();
318 TCollection_ExtendedString aType = toExtString( theType );
319 int r = theColor.red();
320 int g = theColor.green();
321 int b = theColor.blue();
322 int aColorInt = ( r<<16 ) + ( g<<8 ) + b;
323 aMap->SetInteger( aType, aColorInt );