Salome HOME
Get altitude from region implementation.
[modules/hydro.git] / src / HYDROData / HYDROData_Document.cxx
1
2 #include <HYDROData_Document.h>
3 #include <HYDROData_Application.h>
4 #include <HYDROData_Iterator.h>
5 #include <HYDROData_Tool.h>
6
7 #include <TDataStd_Integer.hxx>
8
9 #include <TDF_Delta.hxx>
10
11 #include <QFile>
12 #include <QStringList>
13 #include <QTextStream>
14
15 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
16 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
17
18 #define PYTHON_DOC_NAME "hydro_doc"
19
20 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
21
22 static const int TAG_PROPS = 1; // general properties tag
23 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
24 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
25 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
26
27 using namespace std;
28
29 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
30 {
31   Handle(HYDROData_Document) aResult = 
32     HYDROData_Application::GetApplication()->GetDocument(theStudyID);
33   if (aResult.IsNull()) {
34     aResult = new HYDROData_Document();
35     HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
36   }
37   return aResult;
38 }
39
40 Handle(HYDROData_Document) HYDROData_Document::Document(
41   const TDF_Label& theObjectLabel )
42 {
43   Handle(HYDROData_Document) aResDoc;
44   if ( theObjectLabel.IsNull() )
45     return aResDoc;
46
47   Handle(TDocStd_Document) anObjDoc;
48   try
49   {
50     anObjDoc = TDocStd_Document::Get( theObjectLabel );
51   }
52   catch( ... )
53   {
54   }
55
56   if ( anObjDoc.IsNull() )
57     return aResDoc;
58
59   HYDROData_Application* anApp = HYDROData_Application::GetApplication();
60
61   DataMapOfStudyIDDocument::Iterator aMapIt( anApp->myDocuments );
62   for ( ; aMapIt.More(); aMapIt.Next() )
63   {
64     Handle(HYDROData_Document) anAppDoc = aMapIt.Value();
65     if ( anAppDoc.IsNull() || anAppDoc->myDoc != anObjDoc )
66       continue;
67
68     aResDoc = anAppDoc;
69     break;
70   }
71
72   return aResDoc;
73 }
74
75 bool HYDROData_Document::HasDocument(const int theStudyID)
76 {
77   Handle(HYDROData_Document) aResult = 
78     HYDROData_Application::GetApplication()->GetDocument(theStudyID);
79   return !aResult.IsNull();
80 }
81
82 bool HYDROData_Document::DocumentId(const Handle(HYDROData_Document)& theDocument,
83                                     int&                              theDocId )
84 {
85   return HYDROData_Application::GetApplication()->GetDocumentId(theDocument, theDocId);
86 }
87
88 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
89 {
90   Handle(TDocStd_Document) aResult;
91   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
92   PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
93   try
94   {
95     aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
96   }
97   catch (Standard_Failure)
98   {}
99   if (!aResult.IsNull()) {
100     aResult->SetUndoLimit(UNDO_LIMIT);
101     HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
102   }
103   // recognize error
104   Data_DocError anError;
105   switch(aStatus) {
106   case PCDM_RS_OK:
107     anError = DocError_OK;
108     break;
109   case PCDM_RS_NoDriver:
110   case PCDM_RS_UnknownFileDriver:
111   case PCDM_RS_NoSchema:
112   case PCDM_RS_DriverFailure:
113   case PCDM_RS_WrongResource:
114     anError = DocError_ResourcesProblem;
115     break;
116   case PCDM_RS_OpenError:
117   case PCDM_RS_NoDocument:
118   case PCDM_RS_WrongStreamMode:
119   case PCDM_RS_PermissionDenied:
120     anError = DocError_CanNotOpen;
121     break;
122   case PCDM_RS_NoVersion:
123     anError = DocError_InvalidVersion;
124     break;
125   case PCDM_RS_ExtensionFailure:
126   case PCDM_RS_FormatFailure:
127   case PCDM_RS_TypeFailure:
128   case PCDM_RS_TypeNotFoundInSchema:
129   case PCDM_RS_UnrecognizedFileFormat:
130     anError = DocError_InvalidFormat;
131     break;
132   case PCDM_RS_MakeFailure:
133   default:
134     anError = DocError_UnknownProblem;
135     break;
136   }
137   return anError;
138 }
139
140 Data_DocError HYDROData_Document::Save(const char* theFileName)
141 {
142   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
143   PCDM_StoreStatus aStatus;
144   try {
145     aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
146   }
147   catch (Standard_Failure) {}
148   myTransactionsAfterSave = 0;
149   Standard::Purge(); // Release free memory
150
151   // recognize error
152   Data_DocError anError;
153   switch(aStatus) {
154   case PCDM_SS_OK:
155     anError = DocError_OK;
156     break;
157   case PCDM_SS_DriverFailure:
158     anError = DocError_ResourcesProblem;
159     break;
160   case PCDM_SS_WriteFailure:
161   //case PCDM_SS_DiskWritingFailure:
162   //case PCDM_SS_UserRightsFailure:
163     anError = DocError_CanNotOpen;
164     break;
165   default:
166     anError = DocError_UnknownProblem;
167     break;
168   }
169   return anError;
170 }
171
172 void HYDROData_Document::Close()
173 {
174   myDoc->Close();
175   HYDROData_Application::GetApplication()->RemoveDocument(this);
176 }
177
178 bool HYDROData_Document::DumpToPython( const QString& theFileName,
179                                        const bool     theIsMultiFile ) const
180 {
181   // Try to open the file
182   QFile aFile( theFileName );
183   if ( !aFile.open( QIODevice::WriteOnly ) )
184     return false;
185
186   MapOfTreatedObjects aTreatedObjects;
187
188   // Dump header for python script
189   QStringList aHeaderDump = DumpToPython( aTreatedObjects, theIsMultiFile );
190   if ( aHeaderDump.isEmpty() )
191     return false;
192
193   HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
194
195   bool aRes = true;
196
197   // Dump all model objects to Python script
198   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMAGE );
199   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINEXY );
200   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_BATHYMETRY );
201   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_PROFILE );
202   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINE );
203   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMMERSIBLE_ZONE );
204   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_STREAM );
205   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CHANNEL );
206   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_DIGUE );
207   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_OBSTACLE );
208   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CALCULATION );
209
210   // Dump code to close python fuction
211   if ( aRes && theIsMultiFile )
212   {
213     QStringList aFooterScript;
214     aFooterScript << QString( "" );
215     aFooterScript << QString( "  pass" );
216     HYDROData_Tool::WriteStringsToFile( aFile, aFooterScript );
217   }
218
219   return aRes;
220 }
221
222 QString HYDROData_Document::GetDocPyName() const
223 {
224   QString aDocName = PYTHON_DOC_NAME;
225   
226   /*
227   int aDocId = 1;
228   if ( DocumentId( this, aDocId ) )
229     aDocName += "_" + QString::number( aDocId );
230   */
231   
232   return aDocName;
233 }
234
235 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects,
236                                               const bool           theIsMultiFile ) const
237 {
238   QString aDocName = GetDocPyName();
239
240   // Append document in to the map of treated objects to prevent names overlaping
241   theTreatedObjects.insert( aDocName, this );
242
243   int aDocId = 1;
244   if ( !DocumentId( this, aDocId ) )
245     aDocId = 1;
246
247   QStringList aResScript;
248
249   aResScript << QString( "from HYDROPy import *" );
250   aResScript << QString( "from PyQt4.QtCore import *" );
251   aResScript << QString( "from PyQt4.QtGui import *" );
252
253   if ( theIsMultiFile )
254   {
255     aResScript << QString( "" );
256     aResScript << QString( "def RebuildData( theStudy ):" );
257     aResScript << QString( "  %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
258   }
259   else
260   {
261     aResScript << QString( "" );
262     aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
263   }
264
265   return aResScript;
266 }
267
268 bool HYDROData_Document::dumpPartitionToPython( QFile&               theFile,
269                                                 const bool           theIsMultiFile,
270                                                 MapOfTreatedObjects& theTreatedObjects,
271                                                 const ObjectKind&    theObjectKind ) const
272 {
273   if ( !theFile.isOpen() )
274     return false;
275
276   QTextStream anOutStream( &theFile );
277
278   bool aRes = true;
279
280   HYDROData_Iterator anIterator( this, theObjectKind );
281   for( ; anIterator.More(); anIterator.Next() )
282   {
283     Handle(HYDROData_Entity) anObject = anIterator.Current();
284     if ( anObject.IsNull() )
285       continue;
286
287     QString anObjName = anObject->GetName();
288     if ( theTreatedObjects.contains( anObjName ) )
289       continue;
290
291     theTreatedObjects.insert( anObjName, anObject );
292
293     QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
294
295     if ( theIsMultiFile )
296     {
297       // For multifile dump we use the function, see the document dump header
298       QStringList::iterator anIt = anObjDump.begin();
299       for ( ; anIt != anObjDump.end(); ++anIt )
300         anIt->prepend( "  " );
301     }
302     
303     HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
304   }
305   
306   return aRes;
307 }
308
309 void HYDROData_Document::StartOperation()
310 {
311   myDoc->NewCommand();
312 }
313
314 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
315 {
316   if( !myDoc->CommitCommand() ) // it means that there were no modifications done
317   {
318     myDoc->NewCommand();
319     NewID(); // workaround: do something just to modify the document
320     myDoc->CommitCommand();
321   }
322   myTransactionsAfterSave++;
323
324   if( theName.Length() != 0 )
325   {
326     const TDF_DeltaList& aList = GetUndos();
327     if( !aList.IsEmpty() )
328     {
329       Handle(TDF_Delta) aDelta = aList.Last();
330       if( !aDelta.IsNull() )
331         aDelta->SetName( theName );
332     }
333   }
334 }
335
336 void HYDROData_Document::AbortOperation()
337 {
338   myDoc->AbortCommand();
339 }
340
341 bool HYDROData_Document::IsOperation()
342 {
343   return myDoc->HasOpenCommand() != 0;
344 }
345
346 bool HYDROData_Document::IsModified()
347 {
348   return myTransactionsAfterSave != 0;
349 }
350
351 bool HYDROData_Document::CanUndo()
352 {
353   return myDoc->GetAvailableUndos() > 0;
354 }
355
356 const TDF_DeltaList& HYDROData_Document::GetUndos()
357 {
358   return myDoc->GetUndos();
359 }
360
361 void HYDROData_Document::ClearUndos()
362 {
363   return myDoc->ClearUndos();
364 }
365
366 void HYDROData_Document::Undo()
367 {
368   myDoc->Undo();
369   myTransactionsAfterSave--;
370 }
371
372 bool HYDROData_Document::CanRedo()
373 {
374   return myDoc->GetAvailableRedos() > 0;
375 }
376
377 const TDF_DeltaList& HYDROData_Document::GetRedos()
378 {
379   return myDoc->GetRedos();
380 }
381
382 void HYDROData_Document::ClearRedos()
383 {
384   return myDoc->ClearRedos();
385 }
386
387 void HYDROData_Document::Redo()
388 {
389   myDoc->Redo();
390   myTransactionsAfterSave++;
391 }
392
393 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
394 {
395   return HYDROData_Iterator::CreateObject( this, theKind );
396 }
397
398 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName( 
399   const QString&   theName,
400   const ObjectKind theObjectKind ) const
401 {
402   Handle(HYDROData_Entity) anObject;
403   if ( theName.isEmpty() )
404     return anObject;
405
406   QStringList aNamesList;
407   aNamesList << theName;
408
409   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
410   if( aSeqOfObjs.IsEmpty() )
411     return anObject;
412   
413   anObject = aSeqOfObjs.First();
414   return anObject;
415 }
416
417 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
418   const QStringList& theNames, 
419   const ObjectKind   theObjectKind ) const
420 {
421   HYDROData_SequenceOfObjects aResSeq;
422
423   QStringList aNamesList = theNames;
424
425   HYDROData_Iterator anIter( this, theObjectKind );
426   for( ; anIter.More(); anIter.Next() )
427   {
428     Handle(HYDROData_Entity) anObject = anIter.Current();
429     if( anObject.IsNull() )
430       continue;
431
432     QString anObjName = anObject->GetName();
433     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
434       continue;
435
436     aResSeq.Append( anObject );
437
438     aNamesList.removeAll( anObjName );
439     if ( aNamesList.isEmpty() )
440       break;
441   }
442
443   return aResSeq;
444 }
445
446 HYDROData_Document::HYDROData_Document()
447 {
448   HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
449   myDoc->SetUndoLimit(UNDO_LIMIT);
450   NewID(); // needed to have at least one attribute in initial document to avoid errors
451   myTransactionsAfterSave = 0;
452 }
453
454 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
455 {
456   myDoc = theDoc;
457   myTransactionsAfterSave = 0;
458 }
459
460 HYDROData_Document::~HYDROData_Document()
461 {
462 }
463
464 int HYDROData_Document::NewID()
465 {
466   TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
467     FindChild(TAG_PROPS_NEW_ID);
468   Handle(TDataStd_Integer) anInt;
469   if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
470     anInt = TDataStd_Integer::Set(anIDLab, 0);
471   }
472   // just increment value and return
473   anInt->Set(anInt->Get() + 1);
474   return anInt->Get();
475 }
476
477 TDF_Label HYDROData_Document::LabelOfObjects()
478 {
479   return myDoc->Main().FindChild(TAG_OBJECTS);
480 }