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 "hydro_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,
179 const bool theIsMultiFile ) const
181 // Try to open the file
182 QFile aFile( theFileName );
183 if ( !aFile.open( QIODevice::WriteOnly ) )
186 MapOfTreatedObjects aTreatedObjects;
188 // Dump header for python script
189 QStringList aHeaderDump = DumpToPython( aTreatedObjects, theIsMultiFile );
190 if ( aHeaderDump.isEmpty() )
193 HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
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 );
210 // Dump code to close python fuction
211 if ( aRes && theIsMultiFile )
213 QStringList aFooterScript;
214 aFooterScript << QString( "" );
215 aFooterScript << QString( " pass" );
216 HYDROData_Tool::WriteStringsToFile( aFile, aFooterScript );
222 QString HYDROData_Document::GetDocPyName() const
224 QString aDocName = PYTHON_DOC_NAME;
228 if ( DocumentId( this, aDocId ) )
229 aDocName += "_" + QString::number( aDocId );
235 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects,
236 const bool theIsMultiFile ) const
238 QString aDocName = GetDocPyName();
240 // Append document in to the map of treated objects to prevent names overlaping
241 theTreatedObjects.insert( aDocName, this );
244 if ( !DocumentId( this, aDocId ) )
247 QStringList aResScript;
249 aResScript << QString( "from HYDROPy import *" );
250 aResScript << QString( "from PyQt4.QtCore import *" );
251 aResScript << QString( "from PyQt4.QtGui import *" );
253 if ( theIsMultiFile )
255 aResScript << QString( "" );
256 aResScript << QString( "def RebuildData( theStudy ):" );
257 aResScript << QString( " %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
261 aResScript << QString( "" );
262 aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
268 bool HYDROData_Document::dumpPartitionToPython( QFile& theFile,
269 const bool theIsMultiFile,
270 MapOfTreatedObjects& theTreatedObjects,
271 const ObjectKind& theObjectKind ) const
273 if ( !theFile.isOpen() )
276 QTextStream anOutStream( &theFile );
280 HYDROData_Iterator anIterator( this, theObjectKind );
281 for( ; anIterator.More(); anIterator.Next() )
283 Handle(HYDROData_Entity) anObject = anIterator.Current();
284 if ( anObject.IsNull() )
287 QString anObjName = anObject->GetName();
288 if ( theTreatedObjects.contains( anObjName ) )
291 theTreatedObjects.insert( anObjName, anObject );
293 QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
295 if ( theIsMultiFile )
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( " " );
303 HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
309 void HYDROData_Document::StartOperation()
314 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
316 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
319 NewID(); // workaround: do something just to modify the document
320 myDoc->CommitCommand();
322 myTransactionsAfterSave++;
324 if( theName.Length() != 0 )
326 const TDF_DeltaList& aList = GetUndos();
327 if( !aList.IsEmpty() )
329 Handle(TDF_Delta) aDelta = aList.Last();
330 if( !aDelta.IsNull() )
331 aDelta->SetName( theName );
336 void HYDROData_Document::AbortOperation()
338 myDoc->AbortCommand();
341 bool HYDROData_Document::IsOperation()
343 return myDoc->HasOpenCommand() != 0;
346 bool HYDROData_Document::IsModified()
348 return myTransactionsAfterSave != 0;
351 bool HYDROData_Document::CanUndo()
353 return myDoc->GetAvailableUndos() > 0;
356 const TDF_DeltaList& HYDROData_Document::GetUndos()
358 return myDoc->GetUndos();
361 void HYDROData_Document::ClearUndos()
363 return myDoc->ClearUndos();
366 void HYDROData_Document::Undo()
369 myTransactionsAfterSave--;
372 bool HYDROData_Document::CanRedo()
374 return myDoc->GetAvailableRedos() > 0;
377 const TDF_DeltaList& HYDROData_Document::GetRedos()
379 return myDoc->GetRedos();
382 void HYDROData_Document::ClearRedos()
384 return myDoc->ClearRedos();
387 void HYDROData_Document::Redo()
390 myTransactionsAfterSave++;
393 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
395 return HYDROData_Iterator::CreateObject( this, theKind );
398 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName(
399 const QString& theName,
400 const ObjectKind theObjectKind ) const
402 Handle(HYDROData_Entity) anObject;
403 if ( theName.isEmpty() )
406 QStringList aNamesList;
407 aNamesList << theName;
409 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
410 if( aSeqOfObjs.IsEmpty() )
413 anObject = aSeqOfObjs.First();
417 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
418 const QStringList& theNames,
419 const ObjectKind theObjectKind ) const
421 HYDROData_SequenceOfObjects aResSeq;
423 QStringList aNamesList = theNames;
425 HYDROData_Iterator anIter( this, theObjectKind );
426 for( ; anIter.More(); anIter.Next() )
428 Handle(HYDROData_Entity) anObject = anIter.Current();
429 if( anObject.IsNull() )
432 QString anObjName = anObject->GetName();
433 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
436 aResSeq.Append( anObject );
438 aNamesList.removeAll( anObjName );
439 if ( aNamesList.isEmpty() )
446 HYDROData_Document::HYDROData_Document()
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;
454 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
457 myTransactionsAfterSave = 0;
460 HYDROData_Document::~HYDROData_Document()
464 int HYDROData_Document::NewID()
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);
472 // just increment value and return
473 anInt->Set(anInt->Get() + 1);
477 TDF_Label HYDROData_Document::LabelOfObjects()
479 return myDoc->Main().FindChild(TAG_OBJECTS);