Salome HOME
fix compilation issue with gcc 6.3
[modules/hydro.git] / src / HYDROData / HYDROData_Document.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_Document.h>
20 #include <HYDROData_Application.h>
21 #include <HYDROData_Iterator.h>
22 #include <HYDROData_Tool.h>
23 #include <HYDROData_InterpolatorsFactory.h>
24 #include <HYDROData_StricklerTable.h>
25 #include <HYDROData_LandCoverMap.h>
26
27 #include <TDataStd_Real.hxx>
28 #include <TDataStd_Integer.hxx>
29 #include <TDataXtd_Position.hxx>
30
31 #include <TDF_Delta.hxx>
32
33 #include <gp_Pnt.hxx>
34
35 #include <QFile>
36 #include <QStringList>
37 #include <QTextStream>
38 #include <QColor>
39
40 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
41
42 #define PYTHON_DOC_NAME "hydro_doc"
43
44 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
45
46 static const int TAG_PROPS = 1; // general properties tag
47 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
48 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
49 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
50 static const int TAG_LOCAL_CS = 4; // tag of local coordinate system information
51 static const int TAG_DEF_STRICKLER_COEFF = 5; // tag of default strickler coefficient
52 static const int TAG_COUNT_QUADTREE = 6; // tag of number of quadtrees created so far
53 static const int TAG_COUNT_DELAUNAY = 7; // tag of number of Delaunay triangulations created so far
54 static const gp_Pnt2d DEFAULT_LOCAL_CS( 0, 0 );
55
56 using namespace std;
57
58 typedef QMap<Standard_Integer, Handle(HYDROData_Entity)> MapOfOrdered;
59 typedef QMap<QString, Handle(HYDROData_Entity)> MapOfUnordered;
60
61 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
62 {
63   Handle(HYDROData_Document) aResult = 
64     HYDROData_Application::GetApplication()->GetDocument(theStudyID);
65   if (aResult.IsNull()) {
66     aResult = new HYDROData_Document();
67     HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
68   }
69   return aResult;
70 }
71
72 Handle(HYDROData_Document) HYDROData_Document::Document(
73   const TDF_Label& theObjectLabel )
74 {
75   Handle(HYDROData_Document) aResDoc;
76   if ( theObjectLabel.IsNull() )
77     return aResDoc;
78
79   Handle(TDocStd_Document) anObjDoc;
80   try
81   {
82     anObjDoc = TDocStd_Document::Get( theObjectLabel );
83   }
84   catch( ... )
85   {
86   }
87
88   if ( anObjDoc.IsNull() )
89     return aResDoc;
90
91   HYDROData_Application* anApp = HYDROData_Application::GetApplication();
92
93   DataMapOfStudyIDDocument::Iterator aMapIt( anApp->myDocuments );
94   for ( ; aMapIt.More(); aMapIt.Next() )
95   {
96     Handle(HYDROData_Document) anAppDoc = aMapIt.Value();
97     if ( anAppDoc.IsNull() || anAppDoc->myDoc != anObjDoc )
98       continue;
99
100     aResDoc = anAppDoc;
101     break;
102   }
103
104   return aResDoc;
105 }
106
107 bool HYDROData_Document::HasDocument(const int theStudyID)
108 {
109   Handle(HYDROData_Document) aResult = 
110     HYDROData_Application::GetApplication()->GetDocument(theStudyID);
111   return !aResult.IsNull();
112 }
113
114 bool HYDROData_Document::DocumentId(const Handle(HYDROData_Document)& theDocument,
115                                     int&                              theDocId )
116 {
117   return HYDROData_Application::GetApplication()->GetDocumentId(theDocument, theDocId);
118 }
119
120 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
121 {
122   Handle(TDocStd_Document) aResult;
123   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
124   PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
125   try
126   {
127     aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
128   }
129   catch (Standard_Failure)
130   {}
131   if (!aResult.IsNull()) {
132     aResult->SetUndoLimit(UNDO_LIMIT);
133     HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
134   }
135   // recognize error
136   Data_DocError anError;
137   switch(aStatus) {
138   case PCDM_RS_OK:
139     anError = DocError_OK;
140     break;
141   case PCDM_RS_NoDriver:
142   case PCDM_RS_UnknownFileDriver:
143   case PCDM_RS_NoSchema:
144   case PCDM_RS_DriverFailure:
145   case PCDM_RS_WrongResource:
146     anError = DocError_ResourcesProblem;
147     break;
148   case PCDM_RS_OpenError:
149   case PCDM_RS_NoDocument:
150   case PCDM_RS_WrongStreamMode:
151   case PCDM_RS_PermissionDenied:
152     anError = DocError_CanNotOpen;
153     break;
154   case PCDM_RS_NoVersion:
155     anError = DocError_InvalidVersion;
156     break;
157   case PCDM_RS_ExtensionFailure:
158   case PCDM_RS_FormatFailure:
159   case PCDM_RS_TypeFailure:
160   case PCDM_RS_TypeNotFoundInSchema:
161   case PCDM_RS_UnrecognizedFileFormat:
162     anError = DocError_InvalidFormat;
163     break;
164   case PCDM_RS_MakeFailure:
165   default:
166     anError = DocError_UnknownProblem;
167     break;
168   }
169   return anError;
170 }
171
172 Data_DocError HYDROData_Document::Save(const char* theFileName)
173 {
174   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
175   PCDM_StoreStatus aStatus;
176   try {
177     aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
178   }
179   catch (Standard_Failure) {}
180   myTransactionsAfterSave = 0;
181   Standard::Purge(); // Release free memory
182
183   // recognize error
184   Data_DocError anError;
185   switch(aStatus) {
186   case PCDM_SS_OK:
187     anError = DocError_OK;
188     break;
189   case PCDM_SS_DriverFailure:
190     anError = DocError_ResourcesProblem;
191     break;
192   case PCDM_SS_WriteFailure:
193   //case PCDM_SS_DiskWritingFailure:
194   //case PCDM_SS_UserRightsFailure:
195     anError = DocError_CanNotOpen;
196     break;
197   default:
198     anError = DocError_UnknownProblem;
199     break;
200   }
201   return anError;
202 }
203
204 void HYDROData_Document::Close()
205 {
206   myDoc->Close();
207   HYDROData_Application::GetApplication()->RemoveDocument(this);
208 }
209
210 double HYDROData_Document::GetDefaultStricklerCoefficient() const
211 {
212     double aRes = 0;
213     TDF_Label aLabel = myDoc->Main().FindChild(TAG_DEF_STRICKLER_COEFF, Standard_False);
214     if ( !aLabel.IsNull() )
215     {
216         Handle(TDataStd_Real) anAttr;
217         if ( aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
218             aRes = anAttr->Get();
219     }
220
221     return aRes;
222 }
223
224 void HYDROData_Document::SetDefaultStricklerCoefficient( double theCoeff ) const
225 {
226     TDF_Label aLabel = myDoc->Main().FindChild(TAG_DEF_STRICKLER_COEFF);
227     if ( !aLabel.IsNull() )
228     {
229         Handle(TDataStd_Real) anAttr;
230         if ( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
231             aLabel.AddAttribute( anAttr = new TDataStd_Real() );
232         anAttr->Set( theCoeff );
233     }
234 }
235
236 int HYDROData_Document::GetCountQuadtree() const
237 {
238   int nbQuad = 0;
239   TDF_Label aLabel = myDoc->Main().FindChild(TAG_COUNT_QUADTREE, Standard_False);
240   if ( !aLabel.IsNull() )
241   {
242       Handle(TDataStd_Integer) anAttr;
243       if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anAttr ) )
244           nbQuad = anAttr->Get();
245   }
246   return nbQuad;
247 }
248
249 void HYDROData_Document::SetCountQuadtree( int nbQuad) const
250 {
251   TDF_Label aLabel = myDoc->Main().FindChild(TAG_COUNT_QUADTREE);
252   if ( !aLabel.IsNull() )
253   {
254       Handle(TDataStd_Integer) anAttr;
255       if ( !aLabel.FindAttribute( TDataStd_Integer::GetID(), anAttr ) )
256           aLabel.AddAttribute( anAttr = new TDataStd_Integer() );
257       anAttr->Set( nbQuad );
258   }
259 }
260
261 int HYDROData_Document::GetCountDelaunay() const
262 {
263   int nbDelaunay = 0;
264   TDF_Label aLabel = myDoc->Main().FindChild(TAG_COUNT_DELAUNAY, Standard_False);
265   if ( !aLabel.IsNull() )
266   {
267       Handle(TDataStd_Integer) anAttr;
268       if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anAttr ) )
269         nbDelaunay = anAttr->Get();
270   }
271   return nbDelaunay;
272 }
273
274 void HYDROData_Document::SetCountDelaunay( int nbDelaunay) const
275 {
276   TDF_Label aLabel = myDoc->Main().FindChild(TAG_COUNT_DELAUNAY);
277   if ( !aLabel.IsNull() )
278   {
279       Handle(TDataStd_Integer) anAttr;
280       if ( !aLabel.FindAttribute( TDataStd_Integer::GetID(), anAttr ) )
281           aLabel.AddAttribute( anAttr = new TDataStd_Integer() );
282       anAttr->Set( nbDelaunay );
283   }
284 }
285
286 bool HYDROData_Document::DumpToPython( const QString& thePyScriptPath,
287                                        const bool     theIsMultiFile ) const
288 {
289   // Try to open the file
290   QFile aFile( thePyScriptPath );
291   if ( !aFile.open( QIODevice::WriteOnly | QFile::Text ) )
292     return false;
293
294   MapOfTreatedObjects aTreatedObjects;
295
296   // Dump header for python script
297   QStringList aHeaderDump = DumpToPython( thePyScriptPath, aTreatedObjects, theIsMultiFile );
298   if ( aHeaderDump.isEmpty() )
299     return false;
300
301   HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
302
303   bool aRes = true;
304
305   // Dump the local CS data to Python 
306   UpdateLCSFields();
307   QString aLCS = QString( "%1.SetLocalCS( %2, %3 )" ).arg( GetDocPyName() ).arg( myLX, 0, 'f', 3 ).arg( myLY, 0, 'f', 3 );
308   if( theIsMultiFile )
309     aLCS.prepend( "  " );
310   HYDROData_Tool::WriteStringsToFile( aFile, QStringList() << aLCS );
311
312   // Dump all model objects to Python script
313   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_IMAGE );
314   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_STRICKLER_TABLE );
315   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_POLYLINEXY );
316   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_BATHYMETRY );
317   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_PROFILE );
318   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_POLYLINE );
319   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_IMMERSIBLE_ZONE );
320   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_STREAM );
321   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_CHANNEL );
322   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_DIGUE );
323   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_OBSTACLE );
324   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_LAND_COVER_MAP );
325   aRes = aRes && dumpPartitionToPython( aFile, thePyScriptPath, theIsMultiFile, aTreatedObjects, KIND_CALCULATION );
326
327   // Dump code to close python fuction
328   if ( aRes && theIsMultiFile )
329   {
330     QStringList aFooterScript;
331     aFooterScript << QString( "" );
332     aFooterScript << QString( "  pass" );
333     HYDROData_Tool::WriteStringsToFile( aFile, aFooterScript );
334   }
335
336   return aRes;
337 }
338
339 QString HYDROData_Document::GetDocPyName() const
340 {
341   QString aDocName = PYTHON_DOC_NAME;
342   
343   /*
344   int aDocId = 1;
345   if ( DocumentId( this, aDocId ) )
346     aDocName += "_" + QString::number( aDocId );
347   */
348   
349   return aDocName;
350 }
351
352 QStringList HYDROData_Document::DumpToPython( const QString& thePyScriptPath,
353                                               MapOfTreatedObjects& theTreatedObjects,
354                                               const bool           theIsMultiFile ) const
355 {
356   QString aDocName = GetDocPyName();
357
358   // Append document in to the map of treated objects to prevent names overlaping
359   theTreatedObjects.insert( aDocName, this );
360
361   int aDocId = 1;
362   if ( !DocumentId( this, aDocId ) )
363     aDocId = 1;
364
365   QStringList aResScript;
366
367   aResScript << QString( "from HYDROPy import *" );
368   aResScript << QString( "from PyQt5.QtCore import *" );
369   aResScript << QString( "from PyQt5.QtGui import *" );
370
371   if ( theIsMultiFile )
372   {
373     aResScript << QString( "import salome" );
374     aResScript << QString( "" );
375     aResScript << QString( "def RebuildData( theStudy ):" );
376     aResScript << QString( "  %1 = HYDROData_Document.Document( theStudy._get_StudyId() )" ).arg( aDocName );
377   }
378   else
379   {
380     aResScript << QString( "" );
381     aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() )" ).arg( aDocName );
382   }
383
384   return aResScript;
385 }
386
387 bool HYDROData_Document::dumpPartitionToPython( QFile&               theFile,
388                                                 const QString&       thePyScriptPath,
389                                                 const bool           theIsMultiFile,
390                                                 MapOfTreatedObjects& theTreatedObjects,
391                                                 const ObjectKind&    theObjectKind ) const
392 {
393   if ( !theFile.isOpen() )
394     return false;
395
396   QTextStream anOutStream( &theFile );
397
398   bool aRes = true;
399
400   HYDROData_Iterator anIterator( this, theObjectKind );
401   for( ; anIterator.More(); anIterator.Next() )
402   {
403     Handle(HYDROData_Entity) anObject = anIterator.Current();
404     if ( anObject.IsNull() )
405       continue;
406
407     QString anObjName = anObject->GetName();
408     if ( theTreatedObjects.contains( anObjName ) )
409       continue;
410
411     theTreatedObjects.insert( anObjName, anObject );
412
413     QStringList anObjDump = anObject->DumpToPython( thePyScriptPath, theTreatedObjects );
414
415     if ( theIsMultiFile )
416     {
417       // For multifile dump we use the function, see the document dump header
418       QStringList::iterator anIt = anObjDump.begin();
419       for ( ; anIt != anObjDump.end(); ++anIt )
420         anIt->prepend( "  " );
421     }
422     
423     HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
424   }
425   
426   return aRes;
427 }
428
429 bool takeLastDigits( QString& theStr, int& aRes )
430 {
431   aRes = -1;
432
433   QString anStrNum;
434   for ( int i = theStr.length() - 1; i >= 0; --i )
435   {
436     const QChar& aChar = theStr.at( i );
437     if ( !aChar.isDigit() )
438       break;
439
440     anStrNum.prepend( aChar );
441   }
442
443   if ( anStrNum.isEmpty() )
444     return false;
445
446   theStr.remove( theStr.length() - anStrNum.length(), anStrNum.length() );
447   aRes = anStrNum.toInt();
448
449   return true;
450 }
451
452 bool isObjectNameLessThan( const QString& theStr1, const QString& theStr2 )
453 {
454   QString aStr1 = theStr1, aStr2 = theStr2;
455
456   int aNum1 = -1, aNum2 = -1;
457   if ( takeLastDigits( aStr1, aNum1 ) && takeLastDigits( aStr2, aNum2 ) )
458   {
459     if ( aStr1 == aStr2 )
460       return aNum1 < aNum2;
461   }
462
463   return theStr1 < theStr2;
464 }
465
466 HYDROData_SequenceOfObjects HYDROData_Document::GetObjectsLayerOrder(
467   const Standard_Boolean theIsAll ) const
468 {
469   HYDROData_SequenceOfObjects anOrder;
470
471   MapOfOrdered   aMapOfOrdered;
472   MapOfUnordered aMapOfUnordered;
473
474   HYDROData_Iterator anIter( this );
475   for ( ; anIter.More(); anIter.Next() )
476   {
477     Handle(HYDROData_Entity) anObject = anIter.Current();
478     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
479       continue;
480
481     Standard_Integer anObjZLevel = -1;
482     if ( anObject->GetZLevel( anObjZLevel ) )
483     {
484       aMapOfOrdered.insert( anObjZLevel, anObject );
485     }
486     else
487     {
488       QString anObjName = anObject->GetName();
489       if ( anObjName.isEmpty() )
490         continue;
491
492       aMapOfUnordered.insert( anObjName, anObject );
493     }
494   }
495
496   MapOfOrdered::const_iterator anOrderedMapIt = aMapOfOrdered.constBegin();
497   for ( ; anOrderedMapIt != aMapOfOrdered.constEnd(); anOrderedMapIt++ )
498   {
499     const Handle(HYDROData_Entity)& anObject = anOrderedMapIt.value();
500     anOrder.Prepend( anObject );
501   }
502
503   if ( theIsAll )
504   {
505     QStringList aSortedNames = aMapOfUnordered.keys();
506     qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
507
508     for ( int i = 0; i < aSortedNames.length(); ++i )
509     {
510       QString anObjName = aSortedNames.value( i );
511
512       const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
513       anOrder.Append( anObject );
514     }
515   }
516
517   return anOrder;
518 }
519
520 void HYDROData_Document::SetObjectsLayerOrder( const HYDROData_SequenceOfObjects& theOrder )
521 {
522   // At first we remove previous model order
523   RemoveObjectsLayerOrder();
524
525   // Make new objects order
526   Standard_Integer aLevel = 0;
527   for ( int i = theOrder.Length(), n = 1; i >= n; --i )
528   {
529     const Handle(HYDROData_Entity)& anObject = theOrder.Value( i );
530     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
531       continue;
532
533     anObject->SetZLevel( aLevel++ );
534   }
535 }
536
537 void HYDROData_Document::Show( const Handle(HYDROData_Entity)& theObject )
538 {
539   HYDROData_SequenceOfObjects anOrder;
540   anOrder.Append( theObject );
541   Show( anOrder );
542 }
543
544 void HYDROData_Document::Show( const HYDROData_SequenceOfObjects& theObjects )
545 {
546   MapOfUnordered aMapOfUnordered;
547
548   for ( int i = 1, n = theObjects.Length(); i <= n; ++i )
549   {
550     const Handle(HYDROData_Entity)& anObject = theObjects.Value( i );
551     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
552       continue;
553
554     Standard_Integer anObjZLevel = -1;
555     if ( anObject->GetZLevel( anObjZLevel ) )
556     {
557       continue; // Skip objects that already have the z-level
558     }
559     else
560     {
561       QString anObjName = anObject->GetName();
562       if ( anObjName.isEmpty() )
563         continue;
564
565       aMapOfUnordered.insert( anObjName, anObject );
566     }
567   }
568
569   if ( aMapOfUnordered.isEmpty() )
570     return; // Nothing to show
571
572   Standard_Integer aTopId = 0;
573
574   HYDROData_SequenceOfObjects aModelOrder = GetObjectsLayerOrder( Standard_False );
575   if ( !aModelOrder.IsEmpty() )
576   {
577     const Handle(HYDROData_Entity)& anObject = aModelOrder.First();
578     anObject->GetZLevel( aTopId );
579     aTopId++;
580   }
581
582   aTopId += aMapOfUnordered.size() - 1;
583
584   QStringList aSortedNames = aMapOfUnordered.keys();
585   qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
586
587   for ( int i = 0; i < aSortedNames.length(); ++i )
588   {
589     QString anObjName = aSortedNames.value( i );
590
591     const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
592     anObject->SetZLevel( aTopId-- );
593   }
594 }
595
596 void HYDROData_Document::RemoveObjectsLayerOrder()
597 {
598   HYDROData_Iterator anIter( this );
599   for ( ; anIter.More(); anIter.Next() )
600   {
601     Handle(HYDROData_Entity) anObject = anIter.Current();
602     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
603       continue;
604
605     anObject->RemoveZLevel();
606   }
607 }
608
609 void HYDROData_Document::StartOperation()
610 {
611   myDoc->NewCommand();
612 }
613
614 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
615 {
616   if( !myDoc->CommitCommand() ) // it means that there were no modifications done
617   {
618     myDoc->NewCommand();
619     NewID(); // workaround: do something just to modify the document
620     myDoc->CommitCommand();
621   }
622   myTransactionsAfterSave++;
623
624   if( theName.Length() != 0 )
625   {
626     const TDF_DeltaList& aList = GetUndos();
627     if( !aList.IsEmpty() )
628     {
629       Handle(TDF_Delta) aDelta = aList.Last();
630       if( !aDelta.IsNull() )
631         aDelta->SetName( theName );
632     }
633   }
634 }
635
636 void HYDROData_Document::AbortOperation()
637 {
638   myDoc->AbortCommand();
639 }
640
641 bool HYDROData_Document::IsOperation()
642 {
643   return myDoc->HasOpenCommand() != 0;
644 }
645
646 bool HYDROData_Document::IsModified()
647 {
648   return myTransactionsAfterSave != 0;
649 }
650
651 bool HYDROData_Document::CanUndo()
652 {
653   return myDoc->GetAvailableUndos() > 0;
654 }
655
656 const TDF_DeltaList& HYDROData_Document::GetUndos()
657 {
658   return myDoc->GetUndos();
659 }
660
661 void HYDROData_Document::ClearUndos()
662 {
663   return myDoc->ClearUndos();
664 }
665
666 void HYDROData_Document::Undo()
667 {
668   myDoc->Undo();
669   myTransactionsAfterSave--;
670 }
671
672 bool HYDROData_Document::CanRedo()
673 {
674   return myDoc->GetAvailableRedos() > 0;
675 }
676
677 const TDF_DeltaList& HYDROData_Document::GetRedos()
678 {
679   return myDoc->GetRedos();
680 }
681
682 void HYDROData_Document::ClearRedos()
683 {
684   return myDoc->ClearRedos();
685 }
686
687 void HYDROData_Document::Redo()
688 {
689   myDoc->Redo();
690   myTransactionsAfterSave++;
691 }
692
693 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
694 {
695   return HYDROData_Iterator::CreateObject( this, theKind );
696 }
697
698 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName( 
699   const QString&   theName,
700   const ObjectKind theObjectKind ) const
701 {
702   Handle(HYDROData_Entity) anObject;
703   if ( theName.isEmpty() )
704     return anObject;
705
706   QStringList aNamesList;
707   aNamesList << theName;
708
709   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
710   if( aSeqOfObjs.IsEmpty() )
711     return anObject;
712   
713   anObject = aSeqOfObjs.First();
714   return anObject;
715 }
716
717 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
718   const QStringList& theNames, 
719   const ObjectKind   theObjectKind ) const
720 {
721   HYDROData_SequenceOfObjects aResSeq;
722
723   QStringList aNamesList = theNames;
724
725   HYDROData_Iterator anIter( this, theObjectKind );
726   for( ; anIter.More(); anIter.Next() )
727   {
728     Handle(HYDROData_Entity) anObject = anIter.Current();
729     if( anObject.IsNull() )
730       continue;
731
732     QString anObjName = anObject->GetName();
733     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
734       continue;
735
736     aResSeq.Append( anObject );
737
738     aNamesList.removeAll( anObjName );
739     if ( aNamesList.isEmpty() )
740       break;
741   }
742
743   return aResSeq;
744 }
745
746 HYDROData_SequenceOfObjects HYDROData_Document::CollectAllObjects( const ObjectKind theObjectKind ) const
747 {
748   HYDROData_SequenceOfObjects aResSeq;
749   HYDROData_Iterator anIter( this, theObjectKind );
750   for( ; anIter.More(); anIter.Next() )
751   {
752     Handle(HYDROData_Entity) anObject = anIter.Current();
753     if( anObject.IsNull() )
754       continue;
755     aResSeq.Append( anObject );
756   }
757   return aResSeq;
758 }
759
760 HYDROData_Document::HYDROData_Document()
761 {
762   HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
763   myDoc->SetUndoLimit(UNDO_LIMIT);
764   NewID(); // needed to have at least one attribute in initial document to avoid errors
765   myTransactionsAfterSave = 0;
766   myLX = -1;
767   myLY = -1;
768
769   myInterpolatorsFactory = 0;
770 }
771
772 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
773 {
774   myDoc = theDoc;
775   myTransactionsAfterSave = 0;
776   myLX = -1;
777   myLY = -1;
778
779   myInterpolatorsFactory = 0;
780 }
781
782 HYDROData_Document::~HYDROData_Document()
783 {
784 }
785
786 int HYDROData_Document::NewID()
787 {
788   TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
789     FindChild(TAG_PROPS_NEW_ID);
790   Handle(TDataStd_Integer) anInt;
791   if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
792     anInt = TDataStd_Integer::Set(anIDLab, 0);
793   }
794   // just increment value and return
795   anInt->Set(anInt->Get() + 1);
796   return anInt->Get();
797 }
798
799 TDF_Label HYDROData_Document::LabelOfObjects()
800 {
801   return myDoc->Main().FindChild(TAG_OBJECTS);
802 }
803
804 TDF_Label HYDROData_Document::LabelOfLocalCS() const
805 {
806   return myDoc->Main().FindChild(TAG_LOCAL_CS);
807 }
808
809 void HYDROData_Document::GetLocalCS( double& theLX, double& theLY ) const
810 {
811   TDF_Label aLocalCSLab = LabelOfLocalCS();
812
813   Handle( TDataXtd_Position ) aLocalCS;
814   if( aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
815   {
816     gp_Pnt aLocalCS3d = aLocalCS->GetPosition();
817     theLX = aLocalCS3d.X();
818     theLY = aLocalCS3d.Y();
819   }
820   else
821   {
822     theLX = DEFAULT_LOCAL_CS.X();
823     theLY = DEFAULT_LOCAL_CS.Y();
824   }
825 }
826
827 void HYDROData_Document::SetLocalCS( double theLX, double theLY )
828 {
829   UpdateLCSFields();
830
831   // update the local CS data in attribute
832   TDF_Label aLocalCSLab = LabelOfLocalCS();
833   Handle( TDataXtd_Position ) aLocalCS;
834   if( !aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
835     aLocalCS = TDataXtd_Position::Set( aLocalCSLab );
836
837   gp_Pnt aLocalCS3d( theLX, theLY, 0 );
838   aLocalCS->SetPosition( aLocalCS3d );
839
840   // calculate delta for coordinates
841   double aDX = myLX - theLX;
842   double aDY = myLY - theLY;
843
844   // update the local CS data in internal fields
845   myLX = theLX;
846   myLY = theLY;
847
848   //update all objects in the document
849   HYDROData_Iterator anIterator( this, KIND_UNKNOWN );
850   for( ; anIterator.More(); anIterator.Next() )
851     anIterator.Current()->UpdateLocalCS( aDX, aDY );
852 }
853
854 void HYDROData_Document::UpdateLCSFields() const
855 {
856   if( myLX >= 0 && myLY >= 0 )
857     return;
858
859   double aLX, aLY;
860   GetLocalCS( aLX, aLY );
861   HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
862   aThat->myLX = aLX;
863   aThat->myLY = aLY;
864 }
865
866 void HYDROData_Document::Transform( double& X, double& Y, bool IsToLocalCS ) const
867 {
868   UpdateLCSFields();
869   if( IsToLocalCS )
870   {
871     X -= myLX;
872     Y -= myLY;
873   }
874   else
875   {
876     X += myLX;
877     Y += myLY;
878   }
879 }
880
881 void HYDROData_Document::Transform( gp_Pnt& thePnt, bool IsToLocalCS ) const
882 {
883   double X = thePnt.X();
884   double Y = thePnt.Y();
885   double Z = thePnt.Z();
886   Transform( X, Y, IsToLocalCS );
887   thePnt = gp_Pnt( X, Y, Z ); 
888 }
889
890 void HYDROData_Document::Transform( gp_XYZ& thePnt, bool IsToLocalCS ) const
891 {
892   double X = thePnt.X();
893   double Y = thePnt.Y();
894   double Z = thePnt.Z();
895   Transform( X, Y, IsToLocalCS );
896   thePnt = gp_XYZ( X, Y, Z ); 
897 }
898
899 void HYDROData_Document::Transform( double& X, double& Y, double& Z, bool IsToLocalCS ) const
900 {
901   Transform( X, Y, IsToLocalCS );
902 }
903
904 void HYDROData_Document::Transform( gp_XY& thePnt, bool IsToLocalCS ) const
905 {
906   double X = thePnt.X();
907   double Y = thePnt.Y();
908   Transform( X, Y, IsToLocalCS );
909   thePnt = gp_XY( X, Y ); 
910 }
911
912 HYDROData_InterpolatorsFactory* HYDROData_Document::GetInterpolatorsFactory()
913 {
914   if ( !myInterpolatorsFactory ) {
915     myInterpolatorsFactory = new HYDROData_InterpolatorsFactory();
916   }
917
918   return myInterpolatorsFactory;
919 }
920
921 HYDROData_IProfilesInterpolator* HYDROData_Document::GetInterpolator( const TCollection_AsciiString& theName ) const
922 {
923   HYDROData_IProfilesInterpolator* anInterpolator = NULL;
924
925   HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
926   HYDROData_InterpolatorsFactory* aFactory = aThat->GetInterpolatorsFactory();
927   if ( aFactory ) {
928     anInterpolator = aFactory->GetInterpolator( theName );
929   }
930
931   return anInterpolator;
932 }
933  
934 NCollection_Sequence<TCollection_AsciiString> HYDROData_Document::GetInterpolatorNames() const
935 {
936   NCollection_Sequence<TCollection_AsciiString> aNames;
937
938   HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
939   HYDROData_InterpolatorsFactory* aFactory = aThat->GetInterpolatorsFactory();
940   if ( aFactory ) {
941     aNames = aFactory->GetInterpolatorNames();
942   }
943
944   return aNames;
945 }
946
947 QColor HYDROData_Document::GetAssociatedColor( const QString& theStricklerType, const Handle(HYDROData_StricklerTable)& theTable ) const
948 {
949   if( !theTable.IsNull() && theTable->HasType( theStricklerType ) )
950     return theTable->GetColor( theStricklerType );
951
952   HYDROData_Iterator anIt( this, KIND_STRICKLER_TABLE );
953   for( ; anIt.More(); anIt.Next() )
954   {
955     Handle(HYDROData_StricklerTable) aTable = Handle(HYDROData_StricklerTable)::DownCast( anIt.Current() );
956     if( aTable->HasType( theStricklerType ) )
957       return aTable->GetColor( theStricklerType );
958   }
959   return QColor();
960 }
961
962 void HYDROData_Document::CollectQGISValues( const QString& theAttrName,
963                                             QStringList& theAttrValues,
964                                             QStringList& theStricklerTypes ) const
965 {
966   HYDROData_Iterator It( this, KIND_STRICKLER_TABLE );
967   for( ; It.More(); It.Next() )
968   {
969     Handle(HYDROData_StricklerTable) aTable = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );
970     if( !aTable.IsNull() && aTable->GetAttrName()==theAttrName )
971     {
972       theAttrValues.clear();
973       theStricklerTypes = aTable->GetTypes();
974       foreach( QString aType, theStricklerTypes )
975       {
976         QString anAttrValue = aTable->GetAttrValue( aType );
977         theAttrValues.append( anAttrValue );
978       }
979     }
980   }
981 }