2 #include <HYDROData_Document.h>
3 #include <HYDROData_Application.h>
4 #include <HYDROData_Iterator.h>
5 #include <HYDROData_Tool.h>
7 #include <TDataStd_Integer.hxx>
9 #include <TDF_Delta.hxx>
12 #include <QStringList>
13 #include <QTextStream>
15 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
16 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
18 #define PYTHON_DOC_NAME "doc"
20 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
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)
29 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
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);
40 Handle(HYDROData_Document) HYDROData_Document::Document(
41 const TDF_Label& theObjectLabel )
43 Handle(HYDROData_Document) aResDoc;
44 if ( theObjectLabel.IsNull() )
47 Handle(TDocStd_Document) anObjDoc;
50 anObjDoc = TDocStd_Document::Get( theObjectLabel );
56 if ( anObjDoc.IsNull() )
59 HYDROData_Application* anApp = HYDROData_Application::GetApplication();
61 DataMapOfStudyIDDocument::Iterator aMapIt( anApp->myDocuments );
62 for ( ; aMapIt.More(); aMapIt.Next() )
64 Handle(HYDROData_Document) anAppDoc = aMapIt.Value();
65 if ( anAppDoc.IsNull() || anAppDoc->myDoc != anObjDoc )
75 bool HYDROData_Document::HasDocument(const int theStudyID)
77 Handle(HYDROData_Document) aResult =
78 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
79 return !aResult.IsNull();
82 bool HYDROData_Document::DocumentId(const Handle(HYDROData_Document)& theDocument,
85 return HYDROData_Application::GetApplication()->GetDocumentId(theDocument, theDocId);
88 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
90 Handle(TDocStd_Document) aResult;
91 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
92 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
95 aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
97 catch (Standard_Failure)
99 if (!aResult.IsNull()) {
100 aResult->SetUndoLimit(UNDO_LIMIT);
101 HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
104 Data_DocError anError;
107 anError = DocError_OK;
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;
116 case PCDM_RS_OpenError:
117 case PCDM_RS_NoDocument:
118 case PCDM_RS_WrongStreamMode:
119 case PCDM_RS_PermissionDenied:
120 anError = DocError_CanNotOpen;
122 case PCDM_RS_NoVersion:
123 anError = DocError_InvalidVersion;
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;
132 case PCDM_RS_MakeFailure:
134 anError = DocError_UnknownProblem;
140 Data_DocError HYDROData_Document::Save(const char* theFileName)
142 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
143 PCDM_StoreStatus aStatus;
145 aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
147 catch (Standard_Failure) {}
148 myTransactionsAfterSave = 0;
149 Standard::Purge(); // Release free memory
152 Data_DocError anError;
155 anError = DocError_OK;
157 case PCDM_SS_DriverFailure:
158 anError = DocError_ResourcesProblem;
160 case PCDM_SS_WriteFailure:
161 //case PCDM_SS_DiskWritingFailure:
162 //case PCDM_SS_UserRightsFailure:
163 anError = DocError_CanNotOpen;
166 anError = DocError_UnknownProblem;
172 void HYDROData_Document::Close()
175 HYDROData_Application::GetApplication()->RemoveDocument(this);
178 bool HYDROData_Document::DumpToPython( const QString& theFileName ) const
180 // Try to open the file
181 QFile aFile( theFileName );
182 if ( !aFile.open( QIODevice::WriteOnly ) )
185 MapOfTreatedObjects aTreatedObjects;
187 // Dump header for python script
188 QStringList aHeaderDump = DumpToPython( aTreatedObjects );
189 if ( aHeaderDump.isEmpty() )
192 HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
196 // Dump all model objects to Python script
197 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_IMAGE );
198 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_POLYLINEXY );
199 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_BATHYMETRY );
200 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_PROFILE );
201 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_POLYLINE );
202 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_IMMERSIBLE_ZONE );
203 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_STREAM );
204 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_CHANNEL );
205 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_DIGUE );
206 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_OBSTACLE );
207 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_CALCULATION );
211 QString HYDROData_Document::GetDocPyName() const
213 QString aDocName = PYTHON_DOC_NAME;
216 if ( DocumentId( this, aDocId ) )
217 aDocName += "_" + QString::number( aDocId );
222 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
224 QString aDocName = GetDocPyName();
226 // Append document in to the map of treated objects to prevent names overlaping
227 theTreatedObjects.insert( aDocName, this );
230 if ( !DocumentId( this, aDocId ) )
233 QStringList aResScript;
235 aResScript << QString( "from HYDROpy import *" );
236 aResScript << QString( "from PyQt4.QtCore import *" );
237 aResScript << QString( "from PyQt4.QtGui import *" );
238 aResScript << QString( "" );
239 aResScript << QString( "%1 = HYDROData_Document.Document( %2 );" ).arg( aDocName ).arg( aDocId );
244 bool HYDROData_Document::dumpPartitionToPython( QFile& theFile,
245 MapOfTreatedObjects& theTreatedObjects,
246 const ObjectKind& theObjectKind ) const
248 if ( !theFile.isOpen() )
251 QTextStream anOutStream( &theFile );
255 HYDROData_Iterator anIterator( this, theObjectKind );
256 for( ; anIterator.More(); anIterator.Next() )
258 Handle(HYDROData_Entity) anObject = anIterator.Current();
259 if ( anObject.IsNull() )
262 QString anObjName = anObject->GetName();
263 if ( theTreatedObjects.contains( anObjName ) )
266 theTreatedObjects.insert( anObjName, anObject );
268 QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
270 HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
276 void HYDROData_Document::StartOperation()
281 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
283 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
286 NewID(); // workaround: do something just to modify the document
287 myDoc->CommitCommand();
289 myTransactionsAfterSave++;
291 if( theName.Length() != 0 )
293 const TDF_DeltaList& aList = GetUndos();
294 if( !aList.IsEmpty() )
296 Handle(TDF_Delta) aDelta = aList.Last();
297 if( !aDelta.IsNull() )
298 aDelta->SetName( theName );
303 void HYDROData_Document::AbortOperation()
305 myDoc->AbortCommand();
308 bool HYDROData_Document::IsOperation()
310 return myDoc->HasOpenCommand() != 0;
313 bool HYDROData_Document::IsModified()
315 return myTransactionsAfterSave != 0;
318 bool HYDROData_Document::CanUndo()
320 return myDoc->GetAvailableUndos() > 0;
323 const TDF_DeltaList& HYDROData_Document::GetUndos()
325 return myDoc->GetUndos();
328 void HYDROData_Document::ClearUndos()
330 return myDoc->ClearUndos();
333 void HYDROData_Document::Undo()
336 myTransactionsAfterSave--;
339 bool HYDROData_Document::CanRedo()
341 return myDoc->GetAvailableRedos() > 0;
344 const TDF_DeltaList& HYDROData_Document::GetRedos()
346 return myDoc->GetRedos();
349 void HYDROData_Document::ClearRedos()
351 return myDoc->ClearRedos();
354 void HYDROData_Document::Redo()
357 myTransactionsAfterSave++;
360 Handle(HYDROData_Entity) HYDROData_Document::CreateObject(const ObjectKind theKind)
362 return HYDROData_Iterator::CreateObject(this, theKind);
365 HYDROData_Document::HYDROData_Document()
367 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
368 myDoc->SetUndoLimit(UNDO_LIMIT);
369 NewID(); // needed to have at least one attribute in initial document to avoid errors
370 myTransactionsAfterSave = 0;
373 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
376 myTransactionsAfterSave = 0;
379 HYDROData_Document::~HYDROData_Document()
383 int HYDROData_Document::NewID()
385 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
386 FindChild(TAG_PROPS_NEW_ID);
387 Handle(TDataStd_Integer) anInt;
388 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
389 anInt = TDataStd_Integer::Set(anIDLab, 0);
391 // just increment value and return
392 anInt->Set(anInt->Get() + 1);
396 TDF_Label HYDROData_Document::LabelOfObjects()
398 return myDoc->Main().FindChild(TAG_OBJECTS);