]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_StricklerTable.cxx
Salome HOME
13a6e529a47284cc10aa7481f25da4e64aa834a2
[modules/hydro.git] / src / HYDROData / HYDROData_StricklerTable.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROData_StricklerTable.h>
20 #include <HYDROData_Tool.h>
21 #include <HYDROData_Iterator.h>
22
23 #include <TDataStd_NamedData.hxx>
24
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>
31
32 #include <TCollection_AsciiString.hxx>
33 #include <TCollection_ExtendedString.hxx>
34
35 #include <QString>
36 #include <QColor>
37 #include <QStringList>
38 #include <QFile>
39 #include <QTextStream>
40
41 IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
42 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
43
44 HYDROData_StricklerTable::HYDROData_StricklerTable()
45 {
46 }
47
48 HYDROData_StricklerTable::~HYDROData_StricklerTable()
49 {
50 }
51
52 const ObjectKind HYDROData_StricklerTable::GetKind() const
53 {
54   return KIND_STRICKLER_TABLE;
55 }
56
57 bool HYDROData_StricklerTable::Import( const QString& theFileName )
58 {
59   Handle(TDataStd_NamedData) aMap = Map();
60   if( aMap.IsNull() )
61     return false;
62
63   QFile aFile( theFileName );
64   if( !aFile.open( QFile::ReadOnly | QFile::Text ) )
65     return false;
66
67   aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
68   aMap->ChangeStrings( TDataStd_DataMapOfStringString() );
69   aMap->ChangeIntegers( TColStd_DataMapOfStringInteger() );
70
71   QTextStream aStream( &aFile );
72   int i=0;
73   while( !aStream.atEnd() )
74   {
75     QString aLine = aStream.readLine();
76     if( i==0 )
77     {
78       SetAttrName( aLine );
79     }
80     else
81     {
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();
89       
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 ) );
94     }
95     i++;
96   }
97   aFile.close();
98
99   return true;
100 }
101
102 bool HYDROData_StricklerTable::Export( const QString& theFileName )
103 {
104   Handle(TDataStd_NamedData) aMap = Map();
105   if( aMap.IsNull() )
106     return false;
107
108   QFile aFile( theFileName );
109   if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
110     return false;
111
112   QTextStream aStream( &aFile );
113   aStream.setCodec( "UTF-8" );
114   aStream.setGenerateByteOrderMark( true );
115
116   aStream << GetAttrName() << "\n";
117
118   bool aRes = true;
119   QStringList aTypes = GetTypes();
120   foreach( QString aType, aTypes )
121   {
122     TCollection_ExtendedString aTypeExt = HYDROData_Tool::toExtString( aType );
123
124     aStream << "\"" << aType << "\" " << Get( aType, 0.0 );
125
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 ) );
129
130     aStream << " " << aColor;
131     if( !anAttrValue.isEmpty() )
132       aStream << " " << anAttrValue;
133     aStream << "\n";
134   }
135
136   aFile.close();
137   return aRes;
138 }
139
140 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
141 {
142   Handle( TDataStd_NamedData ) aMap = Map();
143   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
144   if( aMap->HasReal( aType ) )
145     return aMap->GetReal( aType );
146   else
147     return theDefault;
148 }
149
150 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
151 {
152   Handle(TDataStd_NamedData) aMap = Map();
153   aMap->SetReal( HYDROData_Tool::toExtString( theType ), theCoefficient );
154 }
155
156 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
157 {
158   theMin = 0;
159   theMax = 0;
160   
161   Handle(TDataStd_NamedData) aMap = Map();
162   Standard_Boolean isFirst = Standard_True;
163   for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
164   {
165     Standard_Real aValue = it.Value();
166     if ( theMin == 0 || aValue < theMin )
167     {
168       theMin = aValue;
169     }
170     if ( theMax == 0 || aValue > theMax )
171     {
172       theMax = aValue;
173     }
174   }
175 }
176
177 QStringList HYDROData_StricklerTable::GetTypes() const
178 {
179   QStringList aSeq;
180   Handle(TDataStd_NamedData) aMap = Map();
181   if( !aMap.IsNull() )
182   {
183     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
184        aSeq.append( HYDROData_Tool::toQString( it.Key() ) );
185   }
186   aSeq.sort();
187   return aSeq;
188 }
189
190 bool HYDROData_StricklerTable::HasType( const QString& theType ) const
191 {
192   Handle(TDataStd_NamedData) aMap = Map();
193   
194   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
195   return !aMap.IsNull() && aMap->HasReal( aType );
196 }
197
198 void HYDROData_StricklerTable::Clear()
199 {
200   Handle(TDataStd_NamedData) aMap = Map();
201   if( !aMap.IsNull() )
202     aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
203 }
204
205 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
206 {
207   QStringList aResList = dumpObjectCreation( theTreatedObjects );
208   QString aPyName = GetObjPyName();
209
210   aResList << QString( "" );
211   Handle(TDataStd_NamedData) aMap = Map();
212   if( !aMap.IsNull() )
213   {
214     for( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
215     {
216       TCollection_ExtendedString aType = it.Key();
217       Standard_Real aValue = it.Value();
218       aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
219     }
220   }
221   aResList << QString( "" );
222   aResList << QString( "%1.Update();" ).arg( aPyName );
223
224   return aResList;
225 }
226
227 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
228 {
229   TDF_Label aLabel = myLab.FindChild( DataTag_Table );
230   Handle( TDataStd_NamedData ) aMap;
231   if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
232     aMap = TDataStd_NamedData::Set( aLabel );
233   return aMap;
234 }
235
236 QString HYDROData_StricklerTable::GetAttrName() const
237 {
238   Handle(TDataStd_AsciiString) aName;
239   if( myLab.FindChild( DataTag_AttrName ).FindAttribute(TDataStd_AsciiString::GetID(), aName)) {
240     TCollection_AsciiString aStr(aName->Get());
241     return QString(aStr.ToCString());
242   }
243   return QString();}
244
245 bool HYDROData_StricklerTable::SetAttrName( const QString& theAttrName ) const
246 {
247   HYDROData_Iterator anIt( HYDROData_Document::Document( myLab ), KIND_STRICKLER_TABLE );
248   for( ; anIt.More(); anIt.Next() )
249   {
250     Handle( HYDROData_StricklerTable ) aTable = 
251       Handle( HYDROData_StricklerTable )::DownCast( anIt.Current() );
252     if( aTable->Label()==myLab )
253       continue;
254
255     if( theAttrName==aTable->GetAttrName() )
256       return false;
257   }
258
259   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_AttrName ), HYDROData_Tool::toExtString( theAttrName ) );
260   return true;
261 }
262
263 QString HYDROData_StricklerTable::GetAttrValue( const QString& theType ) const
264 {
265   Handle( TDataStd_NamedData ) aMap = Map();
266   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
267   if( aMap->HasString( aType ) )
268     return HYDROData_Tool::toQString( aMap->GetString( aType ) );
269   else
270     return "";
271 }
272
273 void HYDROData_StricklerTable::SetAttrValue( const QString& theType, const QString& theAttrValue ) const
274 {
275   Handle( TDataStd_NamedData ) aMap = Map();
276   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
277   aMap->SetString( aType, HYDROData_Tool::toExtString( theAttrValue ) );
278 }
279
280 QString HYDROData_StricklerTable::GetType( const QString& theAttrValue ) const
281 {
282   if( theAttrValue.isEmpty() )
283     return "";
284
285   Handle( TDataStd_NamedData ) aMap = Map();
286   TCollection_ExtendedString anAttrValue = HYDROData_Tool::toExtString( theAttrValue );
287   for( TDataStd_DataMapIteratorOfDataMapOfStringString it( aMap->GetStringsContainer() ); it.More(); it.Next() )
288   {
289     if( it.Value() == anAttrValue )
290     {
291       QString aType = HYDROData_Tool::toQString( it.Key() );
292       return aType;
293     }
294   }
295   return "";
296 }
297
298 QColor HYDROData_StricklerTable::GetColor( const QString& theType ) const
299 {
300   Handle( TDataStd_NamedData ) aMap = Map();
301   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
302   if( aMap->HasInteger( aType ) )
303   {
304     int aColorInt = aMap->GetInteger( aType );
305     int b = aColorInt % 256;
306     int g = (aColorInt>>8) % 256;
307     int r = (aColorInt>>16) % 256;
308     return QColor( r, g, b );
309   }
310   else
311     return QColor();
312 }
313
314 void HYDROData_StricklerTable::SetColor( const QString& theType, const QColor& theColor ) const
315 {
316   Handle( TDataStd_NamedData ) aMap = Map();
317   TCollection_ExtendedString aType = HYDROData_Tool::toExtString( theType );
318   int r = theColor.red();
319   int g = theColor.green();
320   int b = theColor.blue();
321   int aColorInt = ( r<<16 ) + ( g<<8 ) + b;
322   aMap->SetInteger( aType, aColorInt );
323
324   // synchronize the color for the same type in other maps
325   HYDROData_Iterator anIt( HYDROData_Document::Document( myLab ), KIND_STRICKLER_TABLE );
326   for( ; anIt.More(); anIt.Next() )
327   {
328     Handle( HYDROData_StricklerTable ) aTable = 
329       Handle( HYDROData_StricklerTable )::DownCast( anIt.Current() );
330     if( aTable->Label()==myLab )
331       continue;
332
333     if( aTable->HasType( theType ) )
334       aTable->Map()->SetInteger( aType, aColorInt );
335   }
336 }