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