1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_DataModel.h"
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
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 #include <HYDROData_Bathymetry.h>
36 #include <CAM_Application.h>
37 #include <CAM_DataObject.h>
38 #include <CAM_Module.h>
39 #include <CAM_Study.h>
41 #include <LightApp_Application.h>
42 #include <LightApp_DataObject.h>
43 #include <LightApp_Study.h>
45 #include <SUIT_DataObject.h>
46 #include <SUIT_DataBrowser.h>
47 #include <SUIT_ResourceMgr.h>
48 #include <SUIT_Study.h>
49 #include <SUIT_Tools.h>
51 #include <HYDROData_Document.h>
53 #include <TDF_Delta.hxx>
54 #include <TDF_ListIteratorOfDeltaList.hxx>
56 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
57 : LightApp_DataModel( theModule )
59 update( module()->application()->activeStudy()->id() );
62 HYDROGUI_DataModel::~HYDROGUI_DataModel()
66 bool HYDROGUI_DataModel::open( const QString& theURL,
68 QStringList theFileList )
70 LightApp_DataModel::open( theURL, theStudy, theFileList );
71 const int aStudyId = theStudy->id();
73 Data_DocError res = DocError_UnknownProblem;
74 if( theFileList.count() == 2 )
76 QString aTmpDir = theFileList[0];
77 QString aFileName = theFileList[1];
80 QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
84 res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
88 res = DocError_UnknownProblem;
90 if( res != DocError_OK )
92 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
97 // if the document open was successful, the data model update happens
98 // in the set mode of the module
99 if( res == DocError_OK )
105 bool HYDROGUI_DataModel::save( QStringList& theFileList )
107 if( !module()->application()->activeStudy() )
110 LightApp_DataModel::save( theFileList );
114 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
115 bool isMultiFile = false;
117 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
119 // save data to temporary files
120 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
121 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
122 aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
124 QString aFullPath = aTmpDir + aFileName;
125 Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
126 if( res != DocError_OK )
128 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
132 theFileList.append( aTmpDir );
133 theFileList.append( aFileName );
138 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
140 QStringList& theFileList )
143 return save( theFileList );
146 bool HYDROGUI_DataModel::close()
151 bool HYDROGUI_DataModel::isModified() const
153 return getDocument()->IsModified();
156 bool HYDROGUI_DataModel::isSaved() const
161 void HYDROGUI_DataModel::update( const int theStudyId )
163 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
167 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
171 // create root object if not exist
172 CAM_DataObject* aRootObj = root();
174 aRootObj = createRootModuleObject( aStudyRoot );
179 DataObjectList aList;
180 aRootObj->children( aList );
181 QListIterator<SUIT_DataObject*> anIter( aList );
182 while( anIter.hasNext() )
183 removeChild( aRootObj, anIter.next() );
185 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
186 if( aDocument.IsNull() )
189 LightApp_DataObject* anImageRootObj = createObject( aRootObj, "IMAGES" );
191 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
192 for( ; anIterator.More(); anIterator.Next() )
194 Handle(HYDROData_Image) anImageObj =
195 Handle(HYDROData_Image)::DownCast( anIterator.Current() );
196 if( !anImageObj.IsNull() )
197 createObject( anImageRootObj, anImageObj );
200 LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, "BATHYMETRIES" );
202 anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY );
203 for( ; anIterator.More(); anIterator.Next() )
205 Handle(HYDROData_Bathymetry) aBathymetryObj =
206 Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() );
207 if( !aBathymetryObj.IsNull() )
208 createObject( aBathymetryRootObj, aBathymetryObj );
211 LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, "POLYLINES" );
213 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
214 for( ; anIterator.More(); anIterator.Next() )
216 Handle(HYDROData_Polyline) aPolylineObj =
217 Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
218 if( !aPolylineObj.IsNull() )
219 createObject( aPolylineRootObj, aPolylineObj );
222 LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, "VISUAL_STATES" );
224 anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
225 for( ; anIterator.More(); anIterator.Next() )
227 Handle(HYDROData_VisualState) aVisualStateObj =
228 Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
229 if( !aVisualStateObj.IsNull() )
230 createObject( aVisualStateRootObj, aVisualStateObj );
233 if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
235 anObjectBrowser->setAutoOpenLevel( 3 );
236 anObjectBrowser->openLevels();
240 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
242 return NULL; // to do if necessary
245 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
247 return NULL; // to do if necessary
250 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
252 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
253 return anApp ? anApp->findObject( theEntry ) : 0;
256 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
257 LightApp_Study* theStudy )
260 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
262 update( theStudy->id() );
265 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
267 CAM_DataObject* aRootObj = createModuleObject( theParent );
272 void HYDROGUI_DataModel::updateModel()
274 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
276 update( aModule->getStudyId() );
279 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
280 const ObjectKind theObjectKind )
282 Handle(HYDROData_Document) aDocument = getDocument();
283 if( !aDocument.IsNull() )
285 HYDROData_Iterator anIterator( aDocument, theObjectKind );
286 for( ; anIterator.More(); anIterator.Next() )
288 Handle(HYDROData_Object) anObject = anIterator.Current();
289 if( !anObject.IsNull() )
291 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObject );
292 if( anEntry == theEntry )
300 bool HYDROGUI_DataModel::canUndo() const
302 return getDocument()->CanUndo();
305 bool HYDROGUI_DataModel::canRedo() const
307 return getDocument()->CanRedo();
310 QStringList HYDROGUI_DataModel::undoNames() const
313 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
314 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
318 QStringList HYDROGUI_DataModel::redoNames() const
321 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
322 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
326 void HYDROGUI_DataModel::clearUndos()
328 getDocument()->ClearUndos();
331 void HYDROGUI_DataModel::clearRedos()
333 getDocument()->ClearRedos();
336 bool HYDROGUI_DataModel::undo()
340 getDocument()->Undo();
342 catch ( Standard_Failure )
349 bool HYDROGUI_DataModel::redo()
353 getDocument()->Redo();
355 catch ( Standard_Failure )
362 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
364 int aStudyId = module()->application()->activeStudy()->id();
365 return HYDROData_Document::Document( aStudyId );
368 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
369 Handle(HYDROData_Object) theModelObject )
371 return new HYDROGUI_DataObject( theParent, theModelObject );
374 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
375 const QString& theName )
377 return new HYDROGUI_NamedObject( theParent, theName );
380 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
381 SUIT_DataObject* theChild )
383 SUIT_DataObject* aSubChild = theChild->firstChild();
384 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
385 removeChild( theChild, aSubChild );
386 theParent->removeChild( theChild );
389 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
390 const QString& theName )
392 SUIT_DataObject* aChild = theFather->firstChild();
395 if( aChild->name() == theName )
396 return aChild; // found
397 aChild = aChild->nextBrother();
399 return NULL; // not found