Salome HOME
first part of the porting on OCCT 7.0
[modules/hydro.git] / src / HYDROData / HYDROData_StricklerTable.cxx
index 1cd277049952ee19fc29d6b8077c2f465eea8a85..1c51cf1e49f3849312becdd6ba28145b8894bb8b 100644 (file)
@@ -17,6 +17,8 @@
 //
 
 #include <HYDROData_StricklerTable.h>
+#include <HYDROData_Tool.h>
+#include <HYDROData_Iterator.h>
 
 #include <TDataStd_NamedData.hxx>
 
 #include <QFile>
 #include <QTextStream>
 
-IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
 
-const char ATTR_SUFFIX[] = " attr_value";
-
 HYDROData_StricklerTable::HYDROData_StricklerTable()
+: HYDROData_Entity( Geom_No )
 {
 }
 
@@ -54,13 +54,13 @@ const ObjectKind HYDROData_StricklerTable::GetKind() const
   return KIND_STRICKLER_TABLE;
 }
 
-bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName )
+bool HYDROData_StricklerTable::Import( const QString& theFileName )
 {
   Handle(TDataStd_NamedData) aMap = Map();
   if( aMap.IsNull() )
     return false;
 
-  QFile aFile( theFileName.ToCString() );
+  QFile aFile( theFileName );
   if( !aFile.open( QFile::ReadOnly | QFile::Text ) )
     return false;
 
@@ -87,10 +87,10 @@ bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileNam
       int aColorInt = aParts[1].toInt( 0, 16 );
       QString anAttrValue = aParts.size()>2 ? aParts[2] : QString();
       
-      TCollection_ExtendedString aTypeExt = toExtString( aType );
+      TCollection_ExtendedString aTypeExt = HYDROData_Tool::toExtString( aType );
       aMap->SetReal( aTypeExt, aCoeff );
       aMap->SetInteger( aTypeExt, aColorInt );
-      aMap->SetString( aTypeExt, toExtString( anAttrValue ) );
+      aMap->SetString( aTypeExt, HYDROData_Tool::toExtString( anAttrValue ) );
     }
     i++;
   }
@@ -99,31 +99,38 @@ bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileNam
   return true;
 }
 
-bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName )
+bool HYDROData_StricklerTable::Export( const QString& theFileName )
 {
   Handle(TDataStd_NamedData) aMap = Map();
   if( aMap.IsNull() )
     return false;
 
-  QFile aFile( theFileName.ToCString() );
+  QFile aFile( theFileName );
   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
     return false;
 
   QTextStream aStream( &aFile );
   aStream.setCodec( "UTF-8" );
+  aStream.setGenerateByteOrderMark( true );
 
   aStream << GetAttrName() << "\n";
 
   bool aRes = true;
-  for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() )
+  QStringList aTypes = GetTypes();
+  foreach( QString aType, aTypes )
   {
-    QString aType = toQString( it.Key() );
-    aStream << "\"" << aType << "\"  " << it.Value();
+    TCollection_ExtendedString aTypeExt = HYDROData_Tool::toExtString( aType );
+
+    aStream << "\"" << aType << "\" " << Get( aType, 0.0 );
 
-    QString aColor = QString::number( aMap->GetInteger( it.Key() ), 16 ).toUpper();
-    QString anAttrValue = toQString( aMap->GetString( it.Key() ) );
+    QString aColor = QString::number( aMap->GetInteger( aTypeExt ), 16 ).toUpper();
+    aColor = QString( 6-aColor.length(), '0' ) + aColor;
+    QString anAttrValue = HYDROData_Tool::toQString( aMap->GetString( aTypeExt ) );
 
-    aStream << " " << aColor << " " << anAttrValue << "\n";
+    aStream << " " << aColor;
+    if( !anAttrValue.isEmpty() )
+      aStream << " " << anAttrValue;
+    aStream << "\n";
   }
 
   aFile.close();
@@ -133,7 +140,7 @@ bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileNam
 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
 {
   Handle( TDataStd_NamedData ) aMap = Map();
-  TCollection_ExtendedString aType = toExtString( theType );
+  TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
   if( aMap->HasReal( aType ) )
     return aMap->GetReal( aType );
   else
@@ -143,7 +150,7 @@ double HYDROData_StricklerTable::Get( const QString& theType, double theDefault
 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
 {
   Handle(TDataStd_NamedData) aMap = Map();
-  aMap->SetReal( toExtString( theType ), theCoefficient );
+  aMap->SetReal( HYDROData_Tool::toExtString( theType ), theCoefficient );
 }
 
 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
@@ -174,8 +181,9 @@ QStringList HYDROData_StricklerTable::GetTypes() const
   if( !aMap.IsNull() )
   {
     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
-       aSeq.append( toQString( it.Key() ) );
+       aSeq.append( HYDROData_Tool::toQString( it.Key() ) );
   }
+  aSeq.sort();
   return aSeq;
 }
 
@@ -183,7 +191,7 @@ bool HYDROData_StricklerTable::HasType( const QString& theType ) const
 {
   Handle(TDataStd_NamedData) aMap = Map();
   
-  TCollection_ExtendedString aType = toExtString( theType );
+  TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
   return !aMap.IsNull() && aMap->HasReal( aType );
 }
 
@@ -194,24 +202,36 @@ void HYDROData_StricklerTable::Clear()
     aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
 }
 
-QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
+QStringList HYDROData_StricklerTable::DumpToPython( const QString& thePyScriptPath,
+                                                    MapOfTreatedObjects& theTreatedObjects ) const
 {
   QStringList aResList = dumpObjectCreation( theTreatedObjects );
   QString aPyName = GetObjPyName();
 
+  QString anAttrName = GetAttrName();
+  aResList << QString( "%1.SetAttrName( \"%2\" )" ).arg( aPyName ).arg( anAttrName );
+
   aResList << QString( "" );
   Handle(TDataStd_NamedData) aMap = Map();
   if( !aMap.IsNull() )
   {
     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
     {
-      TCollection_ExtendedString aType = it.Key();
+      QString aType = HYDROData_Tool::toQString( it.Key() );
       Standard_Real aValue = it.Value();
-      aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
+      aResList << QString( "%1.Set( u\"%2\", %3 )" ).arg( aPyName ).arg( aType ).arg( aValue );
+
+      QString anAttrValue = GetAttrValue( aType );
+      aResList << QString( "%1.SetAttrValue( u\"%2\", \"%3\" )" ).arg( aPyName ).arg( aType ).arg( anAttrValue );
+      
+      QColor aColor = GetColor( aType );
+      aResList << QString( "%1.SetColor( u\"%2\", QColor( %3, %4, %5 ) )" ).
+        arg( aPyName ).arg( aType ).arg( aColor.red() ).arg( aColor.green() ).arg( aColor.blue() );
+      aResList << QString();
     }
   }
   aResList << QString( "" );
-  aResList << QString( "%1.Update();" ).arg( aPyName );
+  aResList << QString( "%1.Update()" ).arg( aPyName );
 
   return aResList;
 }
@@ -225,25 +245,6 @@ Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
   return aMap;
 }
 
-TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
-{
-  TCollection_ExtendedString aRes;
-  if( !theStr.isEmpty() )
-  {
-         Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
-         memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
-         ((short*)extStr)[theStr.length()] = '\0';
-    aRes = TCollection_ExtendedString( extStr );
-         delete [] extStr;
-  }
-  return aRes;
-}
-
-QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
-{
-  return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
-}
-
 QString HYDROData_StricklerTable::GetAttrName() const
 {
   Handle(TDataStd_AsciiString) aName;
@@ -253,17 +254,30 @@ QString HYDROData_StricklerTable::GetAttrName() const
   }
   return QString();}
 
-void HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
+bool HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
 {
-  TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), toExtString( theAttrName ) );
+  HYDROData_Iterator anIt( HYDROData_Document::Document( myLab ), KIND_STRICKLER_TABLE );
+  for( ; anIt.More(); anIt.Next() )
+  {
+    Handle( HYDROData_StricklerTable ) aTable = 
+      Handle( HYDROData_StricklerTable )::DownCast( anIt.Current() );
+    if( aTable->Label()==myLab )
+      continue;
+
+    if( theAttrName==aTable->GetAttrName() )
+      return false;
+  }
+
+  TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), HYDROData_Tool::toExtString( theAttrName ) );
+  return true;
 }
 
 QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
 {
   Handle( TDataStd_NamedData ) aMap = Map();
-  TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
+  TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
   if( aMap->HasString( aType ) )
-    return toQString( aMap->GetString( aType ) );
+    return HYDROData_Tool::toQString( aMap->GetString( aType ) );
   else
     return "";
 }
@@ -271,21 +285,22 @@ QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
 void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
 {
   Handle( TDataStd_NamedData ) aMap = Map();
-  TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
-  aMap->SetString( aType, toExtString( theAttrValue ) );
+  TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
+  aMap->SetString( aType, HYDROData_Tool::toExtString( theAttrValue ) );
 }
 
 QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
 {
+  if( theAttrValue.isEmpty() )
+    return "";
+
   Handle( TDataStd_NamedData ) aMap = Map();
-  TCollection_ExtendedString anAttrValue = toExtString( theAttrValue );
-  int l = strlen( ATTR_SUFFIX );
+  TCollection_ExtendedString anAttrValue = HYDROData_Tool::toExtString( theAttrValue );
   for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() )
   {
     if( it.Value() == anAttrValue )
     {
-      QString aType = toQString( it.Key() );
-      aType.remove( aType.length() - l, l );
+      QString aType = HYDROData_Tool::toQString( it.Key() );
       return aType;
     }
   }
@@ -295,7 +310,7 @@ QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
 QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const
 {
   Handle( TDataStd_NamedData ) aMap = Map();
-  TCollection_ExtendedString aType = toExtString( theType );
+  TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
   if( aMap->HasInteger( aType ) )
   {
     int aColorInt = aMap->GetInteger( aType );
@@ -311,10 +326,23 @@ QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const
 void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const
 {
   Handle( TDataStd_NamedData ) aMap = Map();
-  TCollection_ExtendedString aType = toExtString( theType );
+  TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
   int r = theColor.red();
   int g = theColor.green();
   int b = theColor.blue();
   int aColorInt = ( r<<16 ) + ( g<<8 ) + b;
   aMap->SetInteger( aType, aColorInt );
+
+  // synchronize the color for the same type in other maps
+  HYDROData_Iterator anIt( HYDROData_Document::Document( myLab ), KIND_STRICKLER_TABLE );
+  for( ; anIt.More(); anIt.Next() )
+  {
+    Handle( HYDROData_StricklerTable ) aTable = 
+      Handle( HYDROData_StricklerTable )::DownCast( anIt.Current() );
+    if( aTable->Label()==myLab )
+      continue;
+
+    if( aTable->HasType( theType ) )
+      aTable->Map()->SetInteger( aType, aColorInt );
+  }
 }