Salome HOME
Code alignment.
[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   if( !myDoc->CommitCommand() ) // it means that there were no modifications done
137   {
138     myDoc->NewCommand();
139     NewID(); // workaround: do something just to modify the document
140     myDoc->CommitCommand();
141   }
142   myTransactionsAfterSave++;
143
144   if( theName.Length() != 0 )
145   {
146     const TDF_DeltaList& aList = GetUndos();
147     if( !aList.IsEmpty() )
148     {
149       Handle(TDF_Delta) aDelta = aList.Last();
150       if( !aDelta.IsNull() )
151         aDelta->SetName( theName );
152     }
153   }
154 }
155
156 void HYDROData_Document::AbortOperation()
157 {
158   myDoc->AbortCommand();
159 }
160
161 bool HYDROData_Document::IsOperation()
162 {
163   return myDoc->HasOpenCommand() != 0;
164 }
165
166 bool HYDROData_Document::IsModified()
167 {
168   return myTransactionsAfterSave != 0;
169 }
170
171 bool HYDROData_Document::CanUndo()
172 {
173   return myDoc->GetAvailableUndos() > 0;
174 }
175
176 const TDF_DeltaList& HYDROData_Document::GetUndos()
177 {
178   return myDoc->GetUndos();
179 }
180
181 void HYDROData_Document::ClearUndos()
182 {
183   return myDoc->ClearUndos();
184 }
185
186 void HYDROData_Document::Undo()
187 {
188   myDoc->Undo();
189   myTransactionsAfterSave--;
190 }
191
192 bool HYDROData_Document::CanRedo()
193 {
194   return myDoc->GetAvailableRedos() > 0;
195 }
196
197 const TDF_DeltaList& HYDROData_Document::GetRedos()
198 {
199   return myDoc->GetRedos();
200 }
201
202 void HYDROData_Document::ClearRedos()
203 {
204   return myDoc->ClearRedos();
205 }
206
207 void HYDROData_Document::Redo()
208 {
209   myDoc->Redo();
210   myTransactionsAfterSave++;
211 }
212
213 Handle_HYDROData_Object HYDROData_Document::CreateObject(const ObjectKind theKind)
214 {
215   return HYDROData_Iterator::CreateObject(this, theKind);
216 }
217
218 HYDROData_Document::HYDROData_Document()
219 {
220   HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
221   myDoc->SetUndoLimit(UNDO_LIMIT);
222   NewID(); // needed to have at least one attribute in initial document to avoid errors
223   myTransactionsAfterSave = 0;
224 }
225
226 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
227 {
228   myDoc = theDoc;
229   myTransactionsAfterSave = 0;
230 }
231
232 HYDROData_Document::~HYDROData_Document()
233 {
234 }
235
236 int HYDROData_Document::NewID()
237 {
238   TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
239     FindChild(TAG_PROPS_NEW_ID);
240   Handle(TDataStd_Integer) anInt;
241   if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
242     anInt = TDataStd_Integer::Set(anIDLab, 0);
243   }
244   // just increment value and return
245   anInt->Set(anInt->Get() + 1);
246   return anInt->Get();
247 }
248
249 TDF_Label HYDROData_Document::LabelOfObjects()
250 {
251   return myDoc->Main().FindChild(TAG_OBJECTS);
252 }