IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
-const char ATTR_SUFFIX[] = " attr_value";
-
HYDROData_StricklerTable::HYDROData_StricklerTable()
{
}
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;
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 = 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 = toQString( aMap->GetString( aTypeExt ) );
- aStream << " " << aColor << " " << anAttrValue << "\n";
+ aStream << " " << aColor;
+ if( !anAttrValue.isEmpty() )
+ aStream << " " << anAttrValue;
+ aStream << "\n";
}
aFile.close();
for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
aSeq.append( toQString( it.Key() ) );
}
+ aSeq.sort();
return aSeq;
}
QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
{
Handle( TDataStd_NamedData ) aMap = Map();
- TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
+ TCollection_ExtendedString aType = toExtString( theType );
if( aMap->HasString( aType ) )
return toQString( aMap->GetString( aType ) );
else
void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
{
Handle( TDataStd_NamedData ) aMap = Map();
- TCollection_ExtendedString aType = toExtString( theType ) + ATTR_SUFFIX;
+ TCollection_ExtendedString aType = toExtString( theType );
aMap->SetString( aType, toExtString( theAttrValue ) );
}
{
Handle( TDataStd_NamedData ) aMap = Map();
TCollection_ExtendedString anAttrValue = toExtString( theAttrValue );
- int l = strlen( ATTR_SUFFIX );
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 );
return aType;
}
}
HYDRODATA_EXPORT virtual const ObjectKind GetKind() const;
- HYDRODATA_EXPORT bool Import( const TCollection_AsciiString& theFileName );
- HYDRODATA_EXPORT bool Export( const TCollection_AsciiString& theFileName );
+ HYDRODATA_EXPORT bool Import( const QString& theFileName );
+ HYDRODATA_EXPORT bool Export( const QString& theFileName );
HYDRODATA_EXPORT double Get( const QString& theType, double theDefault ) const;
HYDRODATA_EXPORT void Set( const QString& theType, double theCoefficient );
{
SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
QString defTablePath = resMgr->path( "resources", module()->name(), tr( "DEFAULT_STRICKLER_TABLE_FILE" ) );
- aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( defTablePath ) );
+ aStricklerTableObj->Import( defTablePath );
// Set name
QString aStricklerTableName;
if ( aStricklerTableObj->GetName().isEmpty() )
bool res = false;
QString aFilePath = aPanel->getFileName().simplified();
if ( !aFilePath.isEmpty() )
- res = aStricklerTableObj->Export( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
+ res = aStricklerTableObj->Export( aFilePath );
return res;
}
else
{
// Import data from Strickler table file into data model object
- aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( aFilePath ) );
+ aStricklerTableObj->Import( aFilePath );
}
aStricklerTableObj->Update();
CODE_06
-"Zones de champs, prairies, sans cultures" 20.0 FF0000 511
-"Zones de champs cultivé à végétation basse" 17.5 FFFF00 512
-"Zones de champs cultivé à végétation haute" 12.5 00FF00
-"Zones d'arbustes, de sous-bois" 10.0 00FFFF
-"Zones à faible urbanization (bourg)" 9.0 0000FF
-"Zones à forte urbanization (agglomération)" 9.0 123456
-"Canaux naturels" 35.0 FF00FF
-"Canaux artificiels en béton" 65.0 888888
+"Canaux artificiels en béton" 65 888888
+"Canaux naturels" 35 FF00FF
+"Zones d'arbustes, de sous-bois" 10 00FFFF
+"Zones de champs cultivé à végétation basse" 17.5 FFFF00 512
+"Zones de champs cultivé à végétation haute" 12.5 00FF00
+"Zones de champs, prairies, sans cultures" 20 FF0000 511
+"Zones à faible urbanization (bourg)" 9 0000FF
+"Zones à forte urbanization (agglomération)" 9 123456
public:
- bool Import( const TCollection_AsciiString& theFileName );
- bool Export( const TCollection_AsciiString& theFileName );
+ bool Import( const QString& theFileName );
+ bool Export( const QString& theFileName );
double Get( const QString& theType, double theDefault ) const;
void Set( const QString& theType, double theCoefficient );
#include(../../CMake/Common.cmake)
set(PROJECT_HEADERS
+ operators.h
test_HYDROData_Bathymetry.h
test_HYDROData_Document.h
test_HYDROData_Entity.h
--- /dev/null
+
+#pragma once
+
+std::ostream& operator<<( std::ostream& theStream, const QString& theText );
+std::ostream& operator<<( std::ostream& theStream, const QColor& theText );
\ No newline at end of file
#include <cppunit/TextTestProgressListener.h>
#include <stdexcept>
#include <QApplication>
+#include <QColor>
+
+std::ostream& operator<<( std::ostream& theStream, const QString& theText )
+{
+ theStream << theText.toStdString();
+ return theStream;
+}
+
+std::ostream& operator<<( std::ostream& theStream, const QColor& theColor )
+{
+ theStream << "[" << theColor.red() << ", " << theColor.green() << ", " << theColor.blue() << "]";
+ return theStream;
+}
int main( int argc, char* argv[] )
{
#include <test_HYDROData_StricklerTable.h>
#include <HYDROData_Document.h>
+#include <HYDROData_StricklerTable.h>
+#include <QStringList>
+#include <QColor>
+#include <QDir>
+#include <operators.h>
-void test_HYDROData_StricklerTable::testImport()
+const QString DEF_STR_PATH = qgetenv( "HYDRO_ROOT_DIR" ) + "/share/salome/resources/hydro/def_strickler_table.txt";
+
+void test_HYDROData_StricklerTable::test_import()
{
Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
- Handle(HYDROData_Entity) anObj = aDoc->CreateObject( KIND_STRICKLER_TABLE );
- TCollection_AsciiString aFileName = "test_name";
-
+ Handle(HYDROData_StricklerTable) aTable =
+ Handle(HYDROData_StricklerTable)::DownCast( aDoc->CreateObject( KIND_STRICKLER_TABLE ) );
+
+ CPPUNIT_ASSERT_EQUAL( true, aTable->Import( DEF_STR_PATH ) );
+
+ QStringList aTypes = aTable->GetTypes();
+ CPPUNIT_ASSERT_EQUAL( 8, aTypes.size() );
+ CPPUNIT_ASSERT_EQUAL( QString( "CODE_06" ), aTable->GetAttrName() );
+
+ QString aType = "Zones de champs cultivé à végétation basse";
+ CPPUNIT_ASSERT_EQUAL( aType, aTypes[3] );
+ CPPUNIT_ASSERT_EQUAL( QColor( 255, 255, 0 ), aTable->GetColor( aType ) );
+ CPPUNIT_ASSERT_EQUAL( QString( "512" ), aTable->GetAttrValue( aType ) );
+
+ aType = "Zones à forte urbanization (agglomération)";
+ CPPUNIT_ASSERT_EQUAL( aType, aTypes[7] );
+ CPPUNIT_ASSERT_EQUAL( QColor( 18, 52, 86 ), aTable->GetColor( aType ) );
+ CPPUNIT_ASSERT_EQUAL( QString( "" ), aTable->GetAttrValue( aType ) );
+
aDoc->Close();
}
+
+void test_HYDROData_StricklerTable::test_import_export_equivalence()
+{
+ Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
+
+ Handle(HYDROData_StricklerTable) aTable =
+ Handle(HYDROData_StricklerTable)::DownCast( aDoc->CreateObject( KIND_STRICKLER_TABLE ) );
+
+ CPPUNIT_ASSERT_EQUAL( true, aTable->Import( DEF_STR_PATH ) );
+ QString aTmpPath = QDir::tempPath() + "/stricker.txt";
+ CPPUNIT_ASSERT_EQUAL( true, aTable->Export( aTmpPath ) );
+
+ QFile aRefFile( DEF_STR_PATH ), aTmpFile( aTmpPath );
+ CPPUNIT_ASSERT_EQUAL( true, aRefFile.open( QFile::ReadOnly ) );
+ CPPUNIT_ASSERT_EQUAL( true, aTmpFile.open( QFile::ReadOnly ) );
+
+ QByteArray aRefContents = aRefFile.readAll();
+ QByteArray aTmpContents = aTmpFile.readAll();
+
+ bool isEqual = aRefContents.size()==aTmpContents.size();
+ CPPUNIT_ASSERT_EQUAL( true, isEqual );
+ for( int i=0, n=aRefContents.size(); isEqual && i<n; i++ )
+ if( aRefContents[i]!=aTmpContents[i] )
+ isEqual = false;
+
+ CPPUNIT_ASSERT_EQUAL( true, isEqual );
+
+ aRefFile.close();
+ aTmpFile.close();
+ aDoc->Close();
+}
+
class test_HYDROData_StricklerTable : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( test_HYDROData_StricklerTable );
- CPPUNIT_TEST( testImport );
+ CPPUNIT_TEST( test_import );
+ CPPUNIT_TEST( test_import_export_equivalence );
CPPUNIT_TEST_SUITE_END();
public:
- void testImport();
+ void test_import();
+ void test_import_export_equivalence();
};
CPPUNIT_TEST_SUITE_REGISTRATION( test_HYDROData_StricklerTable );