Salome HOME
6062fd6dd88655935fc3872654650a526501c5c0
[modules/hydro.git] / src / HYDROData / HYDROData_Document.cxx
1 #include <HYDROData_Document.h>
2 #include <HYDROData_Application.h>
3 #include <HYDROData_Iterator.h>
4
5 #include <TDataStd_Integer.hxx>
6
7 #include <TDF_Delta.hxx>
8
9 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
10 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
11
12 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
13
14 static const int TAG_PROPS = 1; // general properties tag
15 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
16 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
17 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
18
19 using namespace std;
20
21 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
22 {
23   Handle(HYDROData_Document) aResult = 
24     HYDROData_Application::GetApplication()->GetDocument(theStudyID);
25   if (aResult.IsNull()) {
26     aResult = new HYDROData_Document();
27     HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
28   }
29   return aResult;
30 }
31
32 bool HYDROData_Document::HasDocument(const int theStudyID)
33 {
34   Handle(HYDROData_Document) aResult = 
35     HYDROData_Application::GetApplication()->GetDocument(theStudyID);
36   return !aResult.IsNull();
37 }
38
39 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
40 {
41   Handle(TDocStd_Document) aResult;
42   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
43   PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
44   try
45   {
46     aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
47   }
48   catch (Standard_Failure)
49   {}
50   if (!aResult.IsNull()) {
51     aResult->SetUndoLimit(UNDO_LIMIT);
52     HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
53   }
54   // recognize error
55   Data_DocError anError;
56   switch(aStatus) {
57   case PCDM_RS_OK:
58     anError = DocError_OK;
59     break;
60   case PCDM_RS_NoDriver:
61   case PCDM_RS_UnknownFileDriver:
62   case PCDM_RS_NoSchema:
63   case PCDM_RS_DriverFailure:
64   case PCDM_RS_WrongResource:
65     anError = DocError_ResourcesProblem;
66     break;
67   case PCDM_RS_OpenError:
68   case PCDM_RS_NoDocument:
69   case PCDM_RS_WrongStreamMode:
70   case PCDM_RS_PermissionDenied:
71     anError = DocError_CanNotOpen;
72     break;
73   case PCDM_RS_NoVersion:
74     anError = DocError_InvalidVersion;
75     break;
76   case PCDM_RS_ExtensionFailure:
77   case PCDM_RS_FormatFailure:
78   case PCDM_RS_TypeFailure:
79   case PCDM_RS_TypeNotFoundInSchema:
80   case PCDM_RS_UnrecognizedFileFormat:
81     anError = DocError_InvalidFormat;
82     break;
83   case PCDM_RS_MakeFailure:
84   default:
85     anError = DocError_UnknownProblem;
86     break;
87   }
88   return anError;
89 }
90
91 Data_DocError HYDROData_Document::Save(const char* theFileName)
92 {
93   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
94   PCDM_StoreStatus aStatus;
95   try {
96     aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
97   }
98   catch (Standard_Failure) {}
99   myTransactionsAfterSave = 0;
100   Standard::Purge(); // Release free memory
101
102   // recognize error
103   Data_DocError anError;
104   switch(aStatus) {
105   case PCDM_SS_OK:
106     anError = DocError_OK;
107     break;
108   case PCDM_SS_DriverFailure:
109     anError = DocError_ResourcesProblem;
110     break;
111   case PCDM_SS_WriteFailure:
112   case PCDM_SS_DiskWritingFailure:
113   case PCDM_SS_UserRightsFailure:
114     anError = DocError_CanNotOpen;
115     break;
116   default:
117     anError = DocError_UnknownProblem;
118     break;
119   }
120   return anError;
121 }
122
123 void HYDROData_Document::Close()
124 {
125   myDoc->Close();
126   HYDROData_Application::GetApplication()->RemoveDocument(this);
127 }
128
129 void HYDROData_Document::StartOperation()
130 {
131   myDoc->NewCommand();
132 }
133
134 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
135 {
136   myDoc->CommitCommand();
137   myTransactionsAfterSave++;
138
139   if( theName.Length() != 0 )
140   {
141     const TDF_DeltaList& aList = GetUndos();
142     if( !aList.IsEmpty() )
143     {
144       Handle(TDF_Delta) aDelta = aList.Last();
145       if( !aDelta.IsNull() )
146         aDelta->SetName( theName );
147     }
148   }
149 }
150
151 void HYDROData_Document::AbortOperation()
152 {
153   myDoc->AbortCommand();
154 }
155
156 bool HYDROData_Document::IsOperation()
157 {
158   return myDoc->HasOpenCommand() != 0;
159 }
160
161 bool HYDROData_Document::IsModified()
162 {
163   return myTransactionsAfterSave != 0;
164 }
165
166 bool HYDROData_Document::CanUndo()
167 {
168   return myDoc->GetAvailableUndos() > 0;
169 }
170
171 const TDF_DeltaList& HYDROData_Document::GetUndos()
172 {
173   return myDoc->GetUndos();
174 }
175
176 void HYDROData_Document::ClearUndos()
177 {
178   return myDoc->ClearUndos();
179 }
180
181 void HYDROData_Document::Undo()
182 {
183   myDoc->Undo();
184   myTransactionsAfterSave--;
185 }
186
187 bool HYDROData_Document::CanRedo()
188 {
189   return myDoc->GetAvailableRedos() > 0;
190 }
191
192 const TDF_DeltaList& HYDROData_Document::GetRedos()
193 {
194   return myDoc->GetRedos();
195 }
196
197 void HYDROData_Document::ClearRedos()
198 {
199   return myDoc->ClearRedos();
200 }
201
202 void HYDROData_Document::Redo()
203 {
204   myDoc->Redo();
205   myTransactionsAfterSave++;
206 }
207
208 Handle_HYDROData_Object HYDROData_Document::CreateObject(const ObjectKind theKind)
209 {
210   return HYDROData_Iterator::CreateObject(this, theKind);
211 }
212
213 HYDROData_Document::HYDROData_Document()
214 {
215   HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
216   myDoc->SetUndoLimit(UNDO_LIMIT);
217   NewID(); // needed to have at least one attribute in initial document to avoid errors
218   myTransactionsAfterSave = 0;
219 }
220
221 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
222 {
223   myDoc = theDoc;
224   myTransactionsAfterSave = 0;
225 }
226
227 HYDROData_Document::~HYDROData_Document()
228 {
229 }
230
231 int HYDROData_Document::NewID()
232 {
233   TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
234     FindChild(TAG_PROPS_NEW_ID);
235   Handle(TDataStd_Integer) anInt;
236   if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
237     anInt = TDataStd_Integer::Set(anIDLab, 0);
238   }
239   // just increment value and return
240   anInt->Set(anInt->Get() + 1);
241   return anInt->Get();
242 }
243
244 TDF_Label HYDROData_Document::LabelOfObjects()
245 {
246   return myDoc->Main().FindChild(TAG_OBJECTS);
247 }