]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_DataModel.cxx
Salome HOME
1) Improved visibility management
[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 #include <HYDROData_Polyline.h>
33 #include <HYDROData_VisualState.h>
34
35 #include <CAM_Application.h>
36 #include <CAM_DataObject.h>
37 #include <CAM_Module.h>
38 #include <CAM_Study.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_DataObject.h>
42 #include <LightApp_Study.h>
43
44 #include <SUIT_DataObject.h>
45 #include <SUIT_DataBrowser.h>
46 #include <SUIT_ResourceMgr.h>
47 #include <SUIT_Study.h>
48 #include <SUIT_Tools.h>
49
50 #include <HYDROData_Document.h>
51
52 #include <TDF_Delta.hxx>
53 #include <TDF_ListIteratorOfDeltaList.hxx>
54
55 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
56 : LightApp_DataModel( theModule )
57 {
58   update( module()->application()->activeStudy()->id() );
59 }
60
61 HYDROGUI_DataModel::~HYDROGUI_DataModel()
62 {
63 }
64
65 bool HYDROGUI_DataModel::open( const QString& theURL,
66                                CAM_Study* theStudy,
67                                QStringList theFileList )
68 {
69   LightApp_DataModel::open( theURL, theStudy, theFileList );
70   const int aStudyId = theStudy->id();
71
72   Data_DocError res = DocError_UnknownProblem;
73   if( theFileList.count() == 2 )
74   {
75     QString aTmpDir = theFileList[0];
76     QString aFileName = theFileList[1];
77
78     myStudyURL = theURL;
79     QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
80
81     try
82     {
83       res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
84     }
85     catch(...)
86     {
87       res = DocError_UnknownProblem;
88     }
89     if( res != DocError_OK )
90     {
91       module()->application()->putInfo( tr( "LOAD_ERROR" ) );
92       return false;
93     }
94   }
95
96   // if the document open was successful, the data model update happens
97   // in the set mode of the module
98   if( res == DocError_OK )
99     update( aStudyId );
100
101   return true;
102 }
103
104 bool HYDROGUI_DataModel::save( QStringList& theFileList )
105 {
106   if( !module()->application()->activeStudy() )
107     return false;
108   
109   LightApp_DataModel::save( theFileList );
110
111   QString aTmpDir;
112   QString aFileName;
113   SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
114   bool isMultiFile = false;
115   if( resMgr )
116     isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
117
118   // save data to temporary files
119   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
120   aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
121   aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
122
123   QString aFullPath = aTmpDir + aFileName;
124   Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
125   if( res != DocError_OK )
126   {
127     module()->application()->putInfo( tr( "SAVE_ERROR" ) );
128     return false;
129   }
130
131   theFileList.append( aTmpDir );
132   theFileList.append( aFileName );
133
134   return true;
135 }
136
137 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
138                                  CAM_Study*,
139                                  QStringList& theFileList )
140 {
141   myStudyURL = theURL;
142   return save( theFileList );
143 }
144
145 bool HYDROGUI_DataModel::close()
146 {
147   return true;
148 }
149
150 bool HYDROGUI_DataModel::isModified() const
151 {
152   return getDocument()->IsModified();
153 }
154
155 bool HYDROGUI_DataModel::isSaved() const
156 {
157   return true;
158 }
159
160 void HYDROGUI_DataModel::update( const int theStudyId )
161 {
162   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
163   if( !anApp )
164     return;
165
166   SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
167   if( !aStudyRoot )
168     return;
169
170   // create root object if not exist
171   CAM_DataObject* aRootObj = root();
172   if( !aRootObj )
173     aRootObj = createRootModuleObject( aStudyRoot );
174
175   if( !aRootObj )
176     return;
177
178   DataObjectList aList;
179   aRootObj->children( aList );
180   QListIterator<SUIT_DataObject*> anIter( aList );
181   while( anIter.hasNext() )
182     removeChild( aRootObj, anIter.next() );
183
184   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
185   if( aDocument.IsNull() )
186     return;
187
188   LightApp_DataObject* anImageRootObj = createObject( aRootObj, "IMAGES" );
189
190   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
191   for( ; anIterator.More(); anIterator.Next() )
192   {
193     Handle(HYDROData_Image) anImageObj =
194       Handle(HYDROData_Image)::DownCast( anIterator.Current() );
195     if( !anImageObj.IsNull() )
196       createObject( anImageRootObj, anImageObj );
197   }
198
199   LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, "POLYLINES" );
200
201   anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
202   for( ; anIterator.More(); anIterator.Next() )
203   {
204     Handle(HYDROData_Polyline) aPolylineObj =
205       Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
206     if( !aPolylineObj.IsNull() )
207       createObject( aPolylineRootObj, aPolylineObj );
208   }
209
210   LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, "VISUAL_STATES" );
211
212   anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
213   for( ; anIterator.More(); anIterator.Next() )
214   {
215     Handle(HYDROData_VisualState) aVisualStateObj =
216       Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
217     if( !aVisualStateObj.IsNull() )
218       createObject( aVisualStateRootObj, aVisualStateObj );
219   }
220
221   if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
222   {
223     anObjectBrowser->setAutoOpenLevel( 3 );
224     anObjectBrowser->openLevels();
225   }
226 }
227
228 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
229 {
230   return NULL; // to do if necessary
231 }
232
233 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
234 {
235   return NULL; // to do if necessary
236 }
237
238 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
239 {
240   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
241   return anApp ? anApp->findObject( theEntry ) : 0;
242 }
243
244 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
245                                  LightApp_Study* theStudy )
246 {
247   if( !theStudy )
248     theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
249   if( theStudy )
250     update( theStudy->id() );
251 }
252
253 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
254 {
255   CAM_DataObject* aRootObj = createModuleObject( theParent );
256   setRoot( aRootObj );
257   return aRootObj;
258 }
259
260 void HYDROGUI_DataModel::updateModel()
261 {
262   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
263   if( aModule )
264     update( aModule->getStudyId() );
265 }
266
267 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
268                                                             const ObjectKind theObjectKind )
269 {
270   Handle(HYDROData_Document) aDocument = getDocument();
271   if( !aDocument.IsNull() )
272   {
273     HYDROData_Iterator anIterator( aDocument, theObjectKind );
274     for( ; anIterator.More(); anIterator.Next() )
275     {
276       Handle(HYDROData_Object) anObject = anIterator.Current();
277       if( !anObject.IsNull() )
278       {
279         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObject );
280         if( anEntry == theEntry )
281           return anObject;
282       }
283     }
284   }
285   return NULL;
286 }
287
288 bool HYDROGUI_DataModel::canUndo() const
289 {
290   return getDocument()->CanUndo();
291 }
292
293 bool HYDROGUI_DataModel::canRedo() const
294 {
295   return getDocument()->CanRedo();
296 }
297
298 QStringList HYDROGUI_DataModel::undoNames() const
299 {
300   QStringList aNames;
301   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
302     aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
303   return aNames;
304 }
305
306 QStringList HYDROGUI_DataModel::redoNames() const
307 {
308   QStringList aNames;
309   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
310     aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
311   return aNames;
312 }
313
314 void HYDROGUI_DataModel::clearUndos()
315 {
316   getDocument()->ClearUndos();
317 }
318
319 void HYDROGUI_DataModel::clearRedos()
320 {
321   getDocument()->ClearRedos();
322 }
323
324 bool HYDROGUI_DataModel::undo()
325 {
326   try 
327   {
328     getDocument()->Undo();
329   }
330   catch ( Standard_Failure )
331   {
332     return false;
333   }
334   return true;
335 }
336
337 bool HYDROGUI_DataModel::redo()
338 {
339   try 
340   {
341     getDocument()->Redo();
342   }
343   catch ( Standard_Failure )
344   {
345     return false;
346   }
347   return true;
348 }
349
350 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
351 {
352   int aStudyId = module()->application()->activeStudy()->id();
353   return HYDROData_Document::Document( aStudyId );
354 }
355
356 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
357                                                        Handle(HYDROData_Object) theModelObject )
358 {
359   return new HYDROGUI_DataObject( theParent, theModelObject );
360 }
361
362 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
363                                                        const QString& theName )
364 {
365   return new HYDROGUI_NamedObject( theParent, theName );
366 }
367
368 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
369                                       SUIT_DataObject* theChild )
370 {
371   SUIT_DataObject* aSubChild = theChild->firstChild();
372   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
373     removeChild( theChild, aSubChild );
374   theParent->removeChild( theChild );
375 }
376
377 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
378                                                       const QString& theName )
379 {
380   SUIT_DataObject* aChild = theFather->firstChild();
381   while( aChild )
382   {
383     if( aChild->name() == theName )
384       return aChild; // found
385     aChild = aChild->nextBrother();
386   }
387   return NULL; // not found
388 }