]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_DataModel.cxx
Salome HOME
9d395a6a8f2ffd0d2072bde13c220b908f5ab3e4
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataModel.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_DataModel.h"
24
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28
29 #include <HYDROData_Document.h>
30 #include <HYDROData_Image.h>
31 #include <HYDROData_Iterator.h>
32
33 #include <CAM_Application.h>
34 #include <CAM_DataObject.h>
35 #include <CAM_Module.h>
36 #include <CAM_Study.h>
37
38 #include <LightApp_Application.h>
39 #include <LightApp_DataObject.h>
40 #include <LightApp_Study.h>
41
42 #include <SUIT_DataObject.h>
43 #include <SUIT_DataBrowser.h>
44 #include <SUIT_ResourceMgr.h>
45 #include <SUIT_Study.h>
46 #include <SUIT_Tools.h>
47
48 #include <HYDROData_Document.h>
49
50 #include <TDF_Delta.hxx>
51 #include <TDF_ListIteratorOfDeltaList.hxx>
52
53 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
54 : LightApp_DataModel( theModule )
55 {
56   update( module()->application()->activeStudy()->id() );
57 }
58
59 HYDROGUI_DataModel::~HYDROGUI_DataModel()
60 {
61 }
62
63 bool HYDROGUI_DataModel::open( const QString& theURL,
64                                CAM_Study* theStudy,
65                                QStringList theFileList )
66 {
67   LightApp_DataModel::open( theURL, theStudy, theFileList );
68   const int aStudyId = theStudy->id();
69
70   Data_DocError res = DocError_UnknownProblem;
71   if( theFileList.count() == 2 )
72   {
73     QString aTmpDir = theFileList[0];
74     QString aFileName = theFileList[1];
75
76     myStudyURL = theURL;
77     QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
78
79     try
80     {
81       res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
82     }
83     catch(...)
84     {
85       res = DocError_UnknownProblem;
86     }
87     if( res != DocError_OK )
88     {
89       module()->application()->putInfo( tr( "LOAD_ERROR" ) );
90       return false;
91     }
92   }
93
94   // if the document open was successful, the data model update happens
95   // in the set mode of the module
96   if( res == DocError_OK )
97     update( aStudyId );
98
99   return true;
100 }
101
102 bool HYDROGUI_DataModel::save( QStringList& theFileList )
103 {
104   if( !module()->application()->activeStudy() )
105     return false;
106   
107   LightApp_DataModel::save( theFileList );
108
109   QString aTmpDir;
110   QString aFileName;
111   SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
112   bool isMultiFile = false;
113   if( resMgr )
114     isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
115
116   // save data to temporary files
117   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
118   aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
119   aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
120
121   QString aFullPath = aTmpDir + aFileName;
122   Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
123   if( res != DocError_OK )
124   {
125     module()->application()->putInfo( tr( "SAVE_ERROR" ) );
126     return false;
127   }
128
129   theFileList.append( aTmpDir );
130   theFileList.append( aFileName );
131
132   return true;
133 }
134
135 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
136                                  CAM_Study*,
137                                  QStringList& theFileList )
138 {
139   myStudyURL = theURL;
140   return save( theFileList );
141 }
142
143 bool HYDROGUI_DataModel::close()
144 {
145   return true;
146 }
147
148 bool HYDROGUI_DataModel::isModified() const
149 {
150   int aStudyId = module()->application()->activeStudy()->id();
151   return HYDROData_Document::Document( aStudyId )->IsModified();
152 }
153
154 bool HYDROGUI_DataModel::isSaved() const
155 {
156   return true;
157 }
158
159 void HYDROGUI_DataModel::update( const int theStudyId )
160 {
161   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
162   if( !anApp )
163     return;
164
165   SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
166   if( !aStudyRoot )
167     return;
168
169   if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
170     anObjectBrowser->setAutoOpenLevel( 3 );
171
172   // create root object if not exist
173   CAM_DataObject* aRootObj = root();
174   if( !aRootObj )
175     aRootObj = createRootModuleObject( aStudyRoot );
176
177   if( !aRootObj )
178     return;
179
180   DataObjectList aList;
181   aRootObj->children( aList );
182   QListIterator<SUIT_DataObject*> anIter( aList );
183   while( anIter.hasNext() )
184     removeChild( aRootObj, anIter.next() );
185
186   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
187   if( aDocument.IsNull() )
188     return;
189
190   LightApp_DataObject* anImageRootObj = createObject( aRootObj, "IMAGES" );
191
192   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
193   for( ; anIterator.More(); anIterator.Next() )
194   {
195     Handle(HYDROData_Image) anImageObj =
196       Handle(HYDROData_Image)::DownCast( anIterator.Current() );
197     if( !anImageObj.IsNull() )
198       createObject( anImageRootObj, anImageObj );
199   }
200 }
201
202 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
203 {
204   return NULL; // to do if necessary
205 }
206
207 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
208 {
209   return NULL; // to do if necessary
210 }
211
212 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
213 {
214   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
215   return anApp ? anApp->findObject( theEntry ) : 0;
216 }
217
218 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
219                                  LightApp_Study* theStudy )
220 {
221   if( !theStudy )
222     theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
223   if( theStudy )
224     update( theStudy->id() );
225 }
226
227 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
228 {
229   CAM_DataObject* aRootObj = createModuleObject( theParent );
230   setRoot( aRootObj );
231   return aRootObj;
232 }
233
234 void HYDROGUI_DataModel::updateModel()
235 {
236   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
237   if( aModule )
238     update( aModule->getStudyId() );
239 }
240
241 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
242                                                             const ObjectKind theObjectKind )
243 {
244   const int aStudyId = module()->application()->activeStudy()->id();
245   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
246   if( !aDocument.IsNull() )
247   {
248     HYDROData_Iterator anIterator( aDocument, theObjectKind );
249     for( ; anIterator.More(); anIterator.Next() )
250     {
251       Handle(HYDROData_Object) anObject = anIterator.Current();
252       if( !anObject.IsNull() )
253       {
254         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObject );
255         if( anEntry == theEntry )
256           return anObject;
257       }
258     }
259   }
260   return NULL;
261 }
262
263 bool HYDROGUI_DataModel::canUndo() const
264 {
265   return getDocument()->CanUndo();
266 }
267
268 bool HYDROGUI_DataModel::canRedo() const
269 {
270   return getDocument()->CanRedo();
271 }
272
273 QStringList HYDROGUI_DataModel::undoNames() const
274 {
275   QStringList aNames;
276   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
277     aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
278   return aNames;
279 }
280
281 QStringList HYDROGUI_DataModel::redoNames() const
282 {
283   QStringList aNames;
284   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
285     aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
286   return aNames;
287 }
288
289 void HYDROGUI_DataModel::clearUndos()
290 {
291   getDocument()->ClearUndos();
292 }
293
294 void HYDROGUI_DataModel::clearRedos()
295 {
296   getDocument()->ClearRedos();
297 }
298
299 bool HYDROGUI_DataModel::undo()
300 {
301   try 
302   {
303     getDocument()->Undo();
304   }
305   catch ( Standard_Failure )
306   {
307     return false;
308   }
309   return true;
310 }
311
312 bool HYDROGUI_DataModel::redo()
313 {
314   try 
315   {
316     getDocument()->Redo();
317   }
318   catch ( Standard_Failure )
319   {
320     return false;
321   }
322   return true;
323 }
324
325 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
326 {
327   int aStudyId = module()->application()->activeStudy()->id();
328   return HYDROData_Document::Document( aStudyId );
329 }
330
331 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
332                                                        Handle(HYDROData_Object) theModelObject )
333 {
334   return new HYDROGUI_DataObject( theParent, theModelObject );
335 }
336
337 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
338                                                        const QString& theName )
339 {
340   return new HYDROGUI_NamedObject( theParent, theName );
341 }
342
343 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
344                                       SUIT_DataObject* theChild )
345 {
346   SUIT_DataObject* aSubChild = theChild->firstChild();
347   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
348     removeChild( theChild, aSubChild );
349   theParent->removeChild( theChild );
350 }
351
352 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
353                                                       const QString& theName )
354 {
355   SUIT_DataObject* aChild = theFather->firstChild();
356   while( aChild )
357   {
358     if( aChild->name() == theName )
359       return aChild; // found
360     aChild = aChild->nextBrother();
361   }
362   return NULL; // not found
363 }