]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Document.cxx
Salome HOME
722a39e84c9dff6b01e4678b99b3a64b8b6e82e3
[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 #include <QFile>
10 #include <QStringList>
11 #include <QTextStream>
12
13 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
14 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
15
16 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
17
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)
22
23 using namespace std;
24
25 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
26 {
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);
32   }
33   return aResult;
34 }
35
36 bool HYDROData_Document::HasDocument(const int theStudyID)
37 {
38   Handle(HYDROData_Document) aResult = 
39     HYDROData_Application::GetApplication()->GetDocument(theStudyID);
40   return !aResult.IsNull();
41 }
42
43 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
44 {
45   Handle(TDocStd_Document) aResult;
46   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
47   PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
48   try
49   {
50     aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
51   }
52   catch (Standard_Failure)
53   {}
54   if (!aResult.IsNull()) {
55     aResult->SetUndoLimit(UNDO_LIMIT);
56     HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
57   }
58   // recognize error
59   Data_DocError anError;
60   switch(aStatus) {
61   case PCDM_RS_OK:
62     anError = DocError_OK;
63     break;
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;
70     break;
71   case PCDM_RS_OpenError:
72   case PCDM_RS_NoDocument:
73   case PCDM_RS_WrongStreamMode:
74   case PCDM_RS_PermissionDenied:
75     anError = DocError_CanNotOpen;
76     break;
77   case PCDM_RS_NoVersion:
78     anError = DocError_InvalidVersion;
79     break;
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;
86     break;
87   case PCDM_RS_MakeFailure:
88   default:
89     anError = DocError_UnknownProblem;
90     break;
91   }
92   return anError;
93 }
94
95 Data_DocError HYDROData_Document::Save(const char* theFileName)
96 {
97   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
98   PCDM_StoreStatus aStatus;
99   try {
100     aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
101   }
102   catch (Standard_Failure) {}
103   myTransactionsAfterSave = 0;
104   Standard::Purge(); // Release free memory
105
106   // recognize error
107   Data_DocError anError;
108   switch(aStatus) {
109   case PCDM_SS_OK:
110     anError = DocError_OK;
111     break;
112   case PCDM_SS_DriverFailure:
113     anError = DocError_ResourcesProblem;
114     break;
115   case PCDM_SS_WriteFailure:
116   case PCDM_SS_DiskWritingFailure:
117   case PCDM_SS_UserRightsFailure:
118     anError = DocError_CanNotOpen;
119     break;
120   default:
121     anError = DocError_UnknownProblem;
122     break;
123   }
124   return anError;
125 }
126
127 void HYDROData_Document::Close()
128 {
129   myDoc->Close();
130   HYDROData_Application::GetApplication()->RemoveDocument(this);
131 }
132
133 bool HYDROData_Document::DumpToPython( const QString& theFileName ) const
134 {
135   // Try to open the file
136   QFile aFile( theFileName );
137   if ( !aFile.exists() || !aFile.open( QIODevice::WriteOnly ) )
138     return false;
139
140   QMap<QString,Handle(HYDROData_Object)> aDumpedObjects;
141
142   bool aRes =    dumpPartitionToPython( aFile, aDumpedObjects, KIND_IMAGE      );
143   aRes = aRes && dumpPartitionToPython( aFile, aDumpedObjects, KIND_POLYLINE   );
144   aRes = aRes && dumpPartitionToPython( aFile, aDumpedObjects, KIND_BATHYMETRY );
145
146   return aRes;
147 }
148
149 bool HYDROData_Document::dumpPartitionToPython(
150   QFile&                                  theFile,
151   QMap<QString,Handle(HYDROData_Object)>& theDumpedObjects,
152   const ObjectKind&                       theObjectKind ) const
153 {
154   if ( !theFile.isOpen() )
155     return false;
156
157   QTextStream anOutStream( &theFile );
158
159   bool aRes = true;
160
161   HYDROData_Iterator anIterator( this, theObjectKind );
162   for( ; anIterator.More(); anIterator.Next() )
163   {
164     Handle(HYDROData_Object) anObject = anIterator.Current();
165     if ( anObject.IsNull() )
166       continue;
167
168     QString anObjName = anObject->GetName();
169     if ( theDumpedObjects.contains( anObjName ) )
170       continue;
171
172     theDumpedObjects.insert( anObjName, anObject );
173
174     QStringList anObjDump = anObject->DumpToPython();
175     if ( anObjDump.isEmpty() )
176       continue;
177     
178     QString anObjDumpStr = anObjDump.join( "\n" );
179     if ( anObjDumpStr.isEmpty() )
180       continue;
181
182     anOutStream << anObjDumpStr << "\n";
183   }
184   
185   return aRes;
186 }
187
188 void HYDROData_Document::StartOperation()
189 {
190   myDoc->NewCommand();
191 }
192
193 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
194 {
195   if( !myDoc->CommitCommand() ) // it means that there were no modifications done
196   {
197     myDoc->NewCommand();
198     NewID(); // workaround: do something just to modify the document
199     myDoc->CommitCommand();
200   }
201   myTransactionsAfterSave++;
202
203   if( theName.Length() != 0 )
204   {
205     const TDF_DeltaList& aList = GetUndos();
206     if( !aList.IsEmpty() )
207     {
208       Handle(TDF_Delta) aDelta = aList.Last();
209       if( !aDelta.IsNull() )
210         aDelta->SetName( theName );
211     }
212   }
213 }
214
215 void HYDROData_Document::AbortOperation()
216 {
217   myDoc->AbortCommand();
218 }
219
220 bool HYDROData_Document::IsOperation()
221 {
222   return myDoc->HasOpenCommand() != 0;
223 }
224
225 bool HYDROData_Document::IsModified()
226 {
227   return myTransactionsAfterSave != 0;
228 }
229
230 bool HYDROData_Document::CanUndo()
231 {
232   return myDoc->GetAvailableUndos() > 0;
233 }
234
235 const TDF_DeltaList& HYDROData_Document::GetUndos()
236 {
237   return myDoc->GetUndos();
238 }
239
240 void HYDROData_Document::ClearUndos()
241 {
242   return myDoc->ClearUndos();
243 }
244
245 void HYDROData_Document::Undo()
246 {
247   myDoc->Undo();
248   myTransactionsAfterSave--;
249 }
250
251 bool HYDROData_Document::CanRedo()
252 {
253   return myDoc->GetAvailableRedos() > 0;
254 }
255
256 const TDF_DeltaList& HYDROData_Document::GetRedos()
257 {
258   return myDoc->GetRedos();
259 }
260
261 void HYDROData_Document::ClearRedos()
262 {
263   return myDoc->ClearRedos();
264 }
265
266 void HYDROData_Document::Redo()
267 {
268   myDoc->Redo();
269   myTransactionsAfterSave++;
270 }
271
272 Handle_HYDROData_Object HYDROData_Document::CreateObject(const ObjectKind theKind)
273 {
274   return HYDROData_Iterator::CreateObject(this, theKind);
275 }
276
277 HYDROData_Document::HYDROData_Document()
278 {
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;
283 }
284
285 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
286 {
287   myDoc = theDoc;
288   myTransactionsAfterSave = 0;
289 }
290
291 HYDROData_Document::~HYDROData_Document()
292 {
293 }
294
295 int HYDROData_Document::NewID()
296 {
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);
302   }
303   // just increment value and return
304   anInt->Set(anInt->Get() + 1);
305   return anInt->Get();
306 }
307
308 TDF_Label HYDROData_Document::LabelOfObjects()
309 {
310   return myDoc->Main().FindChild(TAG_OBJECTS);
311 }