Salome HOME
Update the names of Regions and Zones if case name changed (Bug #110).
[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 "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 ) const
179 {
180   // Try to open the file
181   QFile aFile( theFileName );
182   if ( !aFile.open( QIODevice::WriteOnly ) )
183     return false;
184
185   MapOfTreatedObjects aTreatedObjects;
186
187   // Dump header for python script
188   QStringList aHeaderDump = DumpToPython( aTreatedObjects );
189   if ( aHeaderDump.isEmpty() )
190     return false;
191
192   HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
193
194   bool aRes = true;
195
196   // Dump all model objects to Python script
197   aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_IMAGE      );
198   aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_POLYLINE   );
199   aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_BATHYMETRY );
200   aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_ALTITUDE );
201   aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_IMMERSIBLE_ZONE );
202   aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_CALCULATION );
203
204   return aRes;
205 }
206
207 QString HYDROData_Document::GetDocPyName() const
208 {
209   QString aDocName = PYTHON_DOC_NAME;
210   
211   int aDocId = 1;
212   if ( DocumentId( this, aDocId ) )
213     aDocName += "_" + QString::number( aDocId );
214   
215   return aDocName;
216 }
217
218 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
219 {
220   QString aDocName = GetDocPyName();
221
222   // Append document in to the map of treated objects to prevent names overlaping
223   theTreatedObjects.insert( aDocName, this );
224
225   int aDocId = 1;
226   if ( !DocumentId( this, aDocId ) )
227     aDocId = 1;
228
229   QStringList aResScript;
230
231   aResScript << QString( "from HYDROData import *" );
232   aResScript << QString( "from PyQt4.QtCore import *" );
233   aResScript << QString( "from PyQt4.QtGui import *" );
234   aResScript << QString( "" );
235   aResScript << QString( "%1 = HYDROData_Document.Document( %2 );" ).arg( aDocName ).arg( aDocId );
236
237   return aResScript;
238 }
239
240 bool HYDROData_Document::dumpPartitionToPython( QFile&               theFile,
241                                                 MapOfTreatedObjects& theTreatedObjects,
242                                                 const ObjectKind&    theObjectKind ) const
243 {
244   if ( !theFile.isOpen() )
245     return false;
246
247   QTextStream anOutStream( &theFile );
248
249   bool aRes = true;
250
251   HYDROData_Iterator anIterator( this, theObjectKind );
252   for( ; anIterator.More(); anIterator.Next() )
253   {
254     Handle(HYDROData_Entity) anObject = anIterator.Current();
255     if ( anObject.IsNull() )
256       continue;
257
258     QString anObjName = anObject->GetName();
259     if ( theTreatedObjects.contains( anObjName ) )
260       continue;
261
262     theTreatedObjects.insert( anObjName, anObject );
263
264     QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
265     
266     HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
267   }
268   
269   return aRes;
270 }
271
272 void HYDROData_Document::StartOperation()
273 {
274   myDoc->NewCommand();
275 }
276
277 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
278 {
279   if( !myDoc->CommitCommand() ) // it means that there were no modifications done
280   {
281     myDoc->NewCommand();
282     NewID(); // workaround: do something just to modify the document
283     myDoc->CommitCommand();
284   }
285   myTransactionsAfterSave++;
286
287   if( theName.Length() != 0 )
288   {
289     const TDF_DeltaList& aList = GetUndos();
290     if( !aList.IsEmpty() )
291     {
292       Handle(TDF_Delta) aDelta = aList.Last();
293       if( !aDelta.IsNull() )
294         aDelta->SetName( theName );
295     }
296   }
297 }
298
299 void HYDROData_Document::AbortOperation()
300 {
301   myDoc->AbortCommand();
302 }
303
304 bool HYDROData_Document::IsOperation()
305 {
306   return myDoc->HasOpenCommand() != 0;
307 }
308
309 bool HYDROData_Document::IsModified()
310 {
311   return myTransactionsAfterSave != 0;
312 }
313
314 bool HYDROData_Document::CanUndo()
315 {
316   return myDoc->GetAvailableUndos() > 0;
317 }
318
319 const TDF_DeltaList& HYDROData_Document::GetUndos()
320 {
321   return myDoc->GetUndos();
322 }
323
324 void HYDROData_Document::ClearUndos()
325 {
326   return myDoc->ClearUndos();
327 }
328
329 void HYDROData_Document::Undo()
330 {
331   myDoc->Undo();
332   myTransactionsAfterSave--;
333 }
334
335 bool HYDROData_Document::CanRedo()
336 {
337   return myDoc->GetAvailableRedos() > 0;
338 }
339
340 const TDF_DeltaList& HYDROData_Document::GetRedos()
341 {
342   return myDoc->GetRedos();
343 }
344
345 void HYDROData_Document::ClearRedos()
346 {
347   return myDoc->ClearRedos();
348 }
349
350 void HYDROData_Document::Redo()
351 {
352   myDoc->Redo();
353   myTransactionsAfterSave++;
354 }
355
356 Handle(HYDROData_Entity) HYDROData_Document::CreateObject(const ObjectKind theKind)
357 {
358   return HYDROData_Iterator::CreateObject(this, theKind);
359 }
360
361 HYDROData_Document::HYDROData_Document()
362 {
363   HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
364   myDoc->SetUndoLimit(UNDO_LIMIT);
365   NewID(); // needed to have at least one attribute in initial document to avoid errors
366   myTransactionsAfterSave = 0;
367 }
368
369 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
370 {
371   myDoc = theDoc;
372   myTransactionsAfterSave = 0;
373 }
374
375 HYDROData_Document::~HYDROData_Document()
376 {
377 }
378
379 int HYDROData_Document::NewID()
380 {
381   TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
382     FindChild(TAG_PROPS_NEW_ID);
383   Handle(TDataStd_Integer) anInt;
384   if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
385     anInt = TDataStd_Integer::Set(anIDLab, 0);
386   }
387   // just increment value and return
388   anInt->Set(anInt->Get() + 1);
389   return anInt->Get();
390 }
391
392 TDF_Label HYDROData_Document::LabelOfObjects()
393 {
394   return myDoc->Main().FindChild(TAG_OBJECTS);
395 }