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 bool HYDROData_Document::HasDocument(const int theStudyID)
42 Handle(HYDROData_Document) aResult =
43 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
44 return !aResult.IsNull();
47 bool HYDROData_Document::DocumentId(const Handle(HYDROData_Document)& theDocument,
50 return HYDROData_Application::GetApplication()->GetDocumentId(theDocument, theDocId);
53 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
55 Handle(TDocStd_Document) aResult;
56 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
57 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
60 aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
62 catch (Standard_Failure)
64 if (!aResult.IsNull()) {
65 aResult->SetUndoLimit(UNDO_LIMIT);
66 HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
69 Data_DocError anError;
72 anError = DocError_OK;
74 case PCDM_RS_NoDriver:
75 case PCDM_RS_UnknownFileDriver:
76 case PCDM_RS_NoSchema:
77 case PCDM_RS_DriverFailure:
78 case PCDM_RS_WrongResource:
79 anError = DocError_ResourcesProblem;
81 case PCDM_RS_OpenError:
82 case PCDM_RS_NoDocument:
83 case PCDM_RS_WrongStreamMode:
84 case PCDM_RS_PermissionDenied:
85 anError = DocError_CanNotOpen;
87 case PCDM_RS_NoVersion:
88 anError = DocError_InvalidVersion;
90 case PCDM_RS_ExtensionFailure:
91 case PCDM_RS_FormatFailure:
92 case PCDM_RS_TypeFailure:
93 case PCDM_RS_TypeNotFoundInSchema:
94 case PCDM_RS_UnrecognizedFileFormat:
95 anError = DocError_InvalidFormat;
97 case PCDM_RS_MakeFailure:
99 anError = DocError_UnknownProblem;
105 Data_DocError HYDROData_Document::Save(const char* theFileName)
107 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
108 PCDM_StoreStatus aStatus;
110 aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
112 catch (Standard_Failure) {}
113 myTransactionsAfterSave = 0;
114 Standard::Purge(); // Release free memory
117 Data_DocError anError;
120 anError = DocError_OK;
122 case PCDM_SS_DriverFailure:
123 anError = DocError_ResourcesProblem;
125 case PCDM_SS_WriteFailure:
126 case PCDM_SS_DiskWritingFailure:
127 case PCDM_SS_UserRightsFailure:
128 anError = DocError_CanNotOpen;
131 anError = DocError_UnknownProblem;
137 void HYDROData_Document::Close()
140 HYDROData_Application::GetApplication()->RemoveDocument(this);
143 bool HYDROData_Document::DumpToPython( const QString& theFileName ) const
145 // Try to open the file
146 QFile aFile( theFileName );
147 if ( !aFile.open( QIODevice::WriteOnly ) )
150 MapOfTreatedObjects aTreatedObjects;
152 // Dump header for python script
153 QStringList aHeaderDump = DumpToPython( aTreatedObjects );
154 if ( aHeaderDump.isEmpty() )
157 HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
161 // Dump all model objects to Python script
162 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_IMAGE );
163 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_POLYLINE );
164 aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_BATHYMETRY );
169 QString HYDROData_Document::GetDocPyName() const
171 QString aDocName = PYTHON_DOC_NAME;
174 if ( DocumentId( this, aDocId ) )
175 aDocName += "_" + QString::number( aDocId );
180 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
182 QString aDocName = GetDocPyName();
184 // Append document in to the map of treated objects to prevent names overlaping
185 theTreatedObjects.insert( aDocName, this );
188 if ( !DocumentId( this, aDocId ) )
191 QStringList aResScript;
193 aResScript << QString( "from HYDROData import *" );
194 aResScript << QString( "" );
195 aResScript << QString( "%1 = HYDROData_Document.Document( %2 );" ).arg( aDocName ).arg( aDocId );
200 bool HYDROData_Document::dumpPartitionToPython( QFile& theFile,
201 MapOfTreatedObjects& theTreatedObjects,
202 const ObjectKind& theObjectKind ) const
204 if ( !theFile.isOpen() )
207 QTextStream anOutStream( &theFile );
211 HYDROData_Iterator anIterator( this, theObjectKind );
212 for( ; anIterator.More(); anIterator.Next() )
214 Handle(HYDROData_Object) anObject = anIterator.Current();
215 if ( anObject.IsNull() )
218 QString anObjName = anObject->GetName();
219 if ( theTreatedObjects.contains( anObjName ) )
222 theTreatedObjects.insert( anObjName, anObject );
224 QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
226 HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
232 void HYDROData_Document::StartOperation()
237 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
239 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
242 NewID(); // workaround: do something just to modify the document
243 myDoc->CommitCommand();
245 myTransactionsAfterSave++;
247 if( theName.Length() != 0 )
249 const TDF_DeltaList& aList = GetUndos();
250 if( !aList.IsEmpty() )
252 Handle(TDF_Delta) aDelta = aList.Last();
253 if( !aDelta.IsNull() )
254 aDelta->SetName( theName );
259 void HYDROData_Document::AbortOperation()
261 myDoc->AbortCommand();
264 bool HYDROData_Document::IsOperation()
266 return myDoc->HasOpenCommand() != 0;
269 bool HYDROData_Document::IsModified()
271 return myTransactionsAfterSave != 0;
274 bool HYDROData_Document::CanUndo()
276 return myDoc->GetAvailableUndos() > 0;
279 const TDF_DeltaList& HYDROData_Document::GetUndos()
281 return myDoc->GetUndos();
284 void HYDROData_Document::ClearUndos()
286 return myDoc->ClearUndos();
289 void HYDROData_Document::Undo()
292 myTransactionsAfterSave--;
295 bool HYDROData_Document::CanRedo()
297 return myDoc->GetAvailableRedos() > 0;
300 const TDF_DeltaList& HYDROData_Document::GetRedos()
302 return myDoc->GetRedos();
305 void HYDROData_Document::ClearRedos()
307 return myDoc->ClearRedos();
310 void HYDROData_Document::Redo()
313 myTransactionsAfterSave++;
316 Handle_HYDROData_Object HYDROData_Document::CreateObject(const ObjectKind theKind)
318 return HYDROData_Iterator::CreateObject(this, theKind);
321 HYDROData_Document::HYDROData_Document()
323 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
324 myDoc->SetUndoLimit(UNDO_LIMIT);
325 NewID(); // needed to have at least one attribute in initial document to avoid errors
326 myTransactionsAfterSave = 0;
329 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
332 myTransactionsAfterSave = 0;
335 HYDROData_Document::~HYDROData_Document()
339 int HYDROData_Document::NewID()
341 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
342 FindChild(TAG_PROPS_NEW_ID);
343 Handle(TDataStd_Integer) anInt;
344 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
345 anInt = TDataStd_Integer::Set(anIDLab, 0);
347 // just increment value and return
348 anInt->Set(anInt->Get() + 1);
352 TDF_Label HYDROData_Document::LabelOfObjects()
354 return myDoc->Main().FindChild(TAG_OBJECTS);