1 #include <HYDROData_Document.h>
2 #include <HYDROData_Application.h>
3 #include <HYDROData_Iterator.h>
5 #include <TDataStd_Integer.hxx>
7 #include <TDF_Delta.hxx>
10 #include <QStringList>
11 #include <QTextStream>
13 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
14 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
16 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
18 static const int TAG_PROPS = 1; // general properties tag
19 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
20 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
21 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
25 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
27 Handle(HYDROData_Document) aResult =
28 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
29 if (aResult.IsNull()) {
30 aResult = new HYDROData_Document();
31 HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
36 bool HYDROData_Document::HasDocument(const int theStudyID)
38 Handle(HYDROData_Document) aResult =
39 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
40 return !aResult.IsNull();
43 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
45 Handle(TDocStd_Document) aResult;
46 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
47 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
50 aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
52 catch (Standard_Failure)
54 if (!aResult.IsNull()) {
55 aResult->SetUndoLimit(UNDO_LIMIT);
56 HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
59 Data_DocError anError;
62 anError = DocError_OK;
64 case PCDM_RS_NoDriver:
65 case PCDM_RS_UnknownFileDriver:
66 case PCDM_RS_NoSchema:
67 case PCDM_RS_DriverFailure:
68 case PCDM_RS_WrongResource:
69 anError = DocError_ResourcesProblem;
71 case PCDM_RS_OpenError:
72 case PCDM_RS_NoDocument:
73 case PCDM_RS_WrongStreamMode:
74 case PCDM_RS_PermissionDenied:
75 anError = DocError_CanNotOpen;
77 case PCDM_RS_NoVersion:
78 anError = DocError_InvalidVersion;
80 case PCDM_RS_ExtensionFailure:
81 case PCDM_RS_FormatFailure:
82 case PCDM_RS_TypeFailure:
83 case PCDM_RS_TypeNotFoundInSchema:
84 case PCDM_RS_UnrecognizedFileFormat:
85 anError = DocError_InvalidFormat;
87 case PCDM_RS_MakeFailure:
89 anError = DocError_UnknownProblem;
95 Data_DocError HYDROData_Document::Save(const char* theFileName)
97 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
98 PCDM_StoreStatus aStatus;
100 aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
102 catch (Standard_Failure) {}
103 myTransactionsAfterSave = 0;
104 Standard::Purge(); // Release free memory
107 Data_DocError anError;
110 anError = DocError_OK;
112 case PCDM_SS_DriverFailure:
113 anError = DocError_ResourcesProblem;
115 case PCDM_SS_WriteFailure:
116 case PCDM_SS_DiskWritingFailure:
117 case PCDM_SS_UserRightsFailure:
118 anError = DocError_CanNotOpen;
121 anError = DocError_UnknownProblem;
127 void HYDROData_Document::Close()
130 HYDROData_Application::GetApplication()->RemoveDocument(this);
133 bool HYDROData_Document::DumpToPython( const QString& theFileName ) const
135 // Try to open the file
136 QFile aFile( theFileName );
137 if ( !aFile.exists() || !aFile.open( QIODevice::WriteOnly ) )
140 QMap<QString,Handle(HYDROData_Object)> aDumpedObjects;
142 bool aRes = dumpPartitionToPython( aFile, aDumpedObjects, KIND_IMAGE );
143 aRes = aRes && dumpPartitionToPython( aFile, aDumpedObjects, KIND_POLYLINE );
144 aRes = aRes && dumpPartitionToPython( aFile, aDumpedObjects, KIND_BATHYMETRY );
149 bool HYDROData_Document::dumpPartitionToPython(
151 QMap<QString,Handle(HYDROData_Object)>& theDumpedObjects,
152 const ObjectKind& theObjectKind ) const
154 if ( !theFile.isOpen() )
157 QTextStream anOutStream( &theFile );
161 HYDROData_Iterator anIterator( this, theObjectKind );
162 for( ; anIterator.More(); anIterator.Next() )
164 Handle(HYDROData_Object) anObject = anIterator.Current();
165 if ( anObject.IsNull() )
168 QString anObjName = anObject->GetName();
169 if ( theDumpedObjects.contains( anObjName ) )
172 theDumpedObjects.insert( anObjName, anObject );
174 QStringList anObjDump = anObject->DumpToPython();
175 if ( anObjDump.isEmpty() )
178 QString anObjDumpStr = anObjDump.join( "\n" );
179 if ( anObjDumpStr.isEmpty() )
182 anOutStream << anObjDumpStr << "\n";
188 void HYDROData_Document::StartOperation()
193 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
195 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
198 NewID(); // workaround: do something just to modify the document
199 myDoc->CommitCommand();
201 myTransactionsAfterSave++;
203 if( theName.Length() != 0 )
205 const TDF_DeltaList& aList = GetUndos();
206 if( !aList.IsEmpty() )
208 Handle(TDF_Delta) aDelta = aList.Last();
209 if( !aDelta.IsNull() )
210 aDelta->SetName( theName );
215 void HYDROData_Document::AbortOperation()
217 myDoc->AbortCommand();
220 bool HYDROData_Document::IsOperation()
222 return myDoc->HasOpenCommand() != 0;
225 bool HYDROData_Document::IsModified()
227 return myTransactionsAfterSave != 0;
230 bool HYDROData_Document::CanUndo()
232 return myDoc->GetAvailableUndos() > 0;
235 const TDF_DeltaList& HYDROData_Document::GetUndos()
237 return myDoc->GetUndos();
240 void HYDROData_Document::ClearUndos()
242 return myDoc->ClearUndos();
245 void HYDROData_Document::Undo()
248 myTransactionsAfterSave--;
251 bool HYDROData_Document::CanRedo()
253 return myDoc->GetAvailableRedos() > 0;
256 const TDF_DeltaList& HYDROData_Document::GetRedos()
258 return myDoc->GetRedos();
261 void HYDROData_Document::ClearRedos()
263 return myDoc->ClearRedos();
266 void HYDROData_Document::Redo()
269 myTransactionsAfterSave++;
272 Handle_HYDROData_Object HYDROData_Document::CreateObject(const ObjectKind theKind)
274 return HYDROData_Iterator::CreateObject(this, theKind);
277 HYDROData_Document::HYDROData_Document()
279 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
280 myDoc->SetUndoLimit(UNDO_LIMIT);
281 NewID(); // needed to have at least one attribute in initial document to avoid errors
282 myTransactionsAfterSave = 0;
285 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
288 myTransactionsAfterSave = 0;
291 HYDROData_Document::~HYDROData_Document()
295 int HYDROData_Document::NewID()
297 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
298 FindChild(TAG_PROPS_NEW_ID);
299 Handle(TDataStd_Integer) anInt;
300 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
301 anInt = TDataStd_Integer::Set(anIDLab, 0);
303 // just increment value and return
304 anInt->Set(anInt->Get() + 1);
308 TDF_Label HYDROData_Document::LabelOfObjects()
310 return myDoc->Main().FindChild(TAG_OBJECTS);