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>
33 #include <CAM_Application.h>
34 #include <CAM_DataObject.h>
35 #include <CAM_Module.h>
36 #include <CAM_Study.h>
38 #include <LightApp_Application.h>
39 #include <LightApp_DataObject.h>
40 #include <LightApp_Study.h>
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>
48 #include <HYDROData_Document.h>
50 #include <TDF_Delta.hxx>
51 #include <TDF_ListIteratorOfDeltaList.hxx>
53 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
54 : LightApp_DataModel( theModule )
56 update( module()->application()->activeStudy()->id() );
59 HYDROGUI_DataModel::~HYDROGUI_DataModel()
63 bool HYDROGUI_DataModel::open( const QString& theURL,
65 QStringList theFileList )
67 LightApp_DataModel::open( theURL, theStudy, theFileList );
68 const int aStudyId = theStudy->id();
70 Data_DocError res = DocError_UnknownProblem;
71 if( theFileList.count() == 2 )
73 QString aTmpDir = theFileList[0];
74 QString aFileName = theFileList[1];
77 QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
81 res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
85 res = DocError_UnknownProblem;
87 if( res != DocError_OK )
89 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
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 )
102 bool HYDROGUI_DataModel::save( QStringList& theFileList )
104 if( !module()->application()->activeStudy() )
107 LightApp_DataModel::save( theFileList );
111 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
112 bool isMultiFile = false;
114 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
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";
121 QString aFullPath = aTmpDir + aFileName;
122 Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
123 if( res != DocError_OK )
125 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
129 theFileList.append( aTmpDir );
130 theFileList.append( aFileName );
135 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
137 QStringList& theFileList )
140 return save( theFileList );
143 bool HYDROGUI_DataModel::close()
148 bool HYDROGUI_DataModel::isModified() const
150 int aStudyId = module()->application()->activeStudy()->id();
151 return HYDROData_Document::Document( aStudyId )->IsModified();
154 bool HYDROGUI_DataModel::isSaved() const
159 void HYDROGUI_DataModel::update( const int theStudyId )
161 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
165 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
169 // create root object if not exist
170 CAM_DataObject* aRootObj = root();
172 aRootObj = createRootModuleObject( aStudyRoot );
177 DataObjectList aList;
178 aRootObj->children( aList );
179 QListIterator<SUIT_DataObject*> anIter( aList );
180 while( anIter.hasNext() )
181 removeChild( aRootObj, anIter.next() );
183 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
184 if( aDocument.IsNull() )
187 LightApp_DataObject* anImageRootObj = createObject( aRootObj, "IMAGES" );
189 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
190 for( ; anIterator.More(); anIterator.Next() )
192 Handle(HYDROData_Image) anImageObj =
193 Handle(HYDROData_Image)::DownCast( anIterator.Current() );
194 if( !anImageObj.IsNull() )
195 createObject( anImageRootObj, anImageObj );
198 LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, "POLYLINES" );
200 HYDROData_Iterator aPolyIterator( aDocument, KIND_IMAGE );
201 for( ; aPolyIterator.More(); aPolyIterator.Next() )
203 Handle(HYDROData_Image) aPolylineObj =
204 Handle(HYDROData_Image)::DownCast( anIterator.Current() );
205 if( !aPolylineObj.IsNull() )
206 createObject( aPolylineRootObj, aPolylineObj );
209 if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
211 anObjectBrowser->setAutoOpenLevel( 3 );
212 anObjectBrowser->openLevels();
216 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
218 return NULL; // to do if necessary
221 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
223 return NULL; // to do if necessary
226 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
228 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
229 return anApp ? anApp->findObject( theEntry ) : 0;
232 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
233 LightApp_Study* theStudy )
236 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
238 update( theStudy->id() );
241 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
243 CAM_DataObject* aRootObj = createModuleObject( theParent );
248 void HYDROGUI_DataModel::updateModel()
250 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
252 update( aModule->getStudyId() );
255 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
256 const ObjectKind theObjectKind )
258 const int aStudyId = module()->application()->activeStudy()->id();
259 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
260 if( !aDocument.IsNull() )
262 HYDROData_Iterator anIterator( aDocument, theObjectKind );
263 for( ; anIterator.More(); anIterator.Next() )
265 Handle(HYDROData_Object) anObject = anIterator.Current();
266 if( !anObject.IsNull() )
268 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObject );
269 if( anEntry == theEntry )
277 bool HYDROGUI_DataModel::canUndo() const
279 return getDocument()->CanUndo();
282 bool HYDROGUI_DataModel::canRedo() const
284 return getDocument()->CanRedo();
287 QStringList HYDROGUI_DataModel::undoNames() const
290 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
291 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
295 QStringList HYDROGUI_DataModel::redoNames() const
298 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
299 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
303 void HYDROGUI_DataModel::clearUndos()
305 getDocument()->ClearUndos();
308 void HYDROGUI_DataModel::clearRedos()
310 getDocument()->ClearRedos();
313 bool HYDROGUI_DataModel::undo()
317 getDocument()->Undo();
319 catch ( Standard_Failure )
326 bool HYDROGUI_DataModel::redo()
330 getDocument()->Redo();
332 catch ( Standard_Failure )
339 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
341 int aStudyId = module()->application()->activeStudy()->id();
342 return HYDROData_Document::Document( aStudyId );
345 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
346 Handle(HYDROData_Object) theModelObject )
348 return new HYDROGUI_DataObject( theParent, theModelObject );
351 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
352 const QString& theName )
354 return new HYDROGUI_NamedObject( theParent, theName );
357 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
358 SUIT_DataObject* theChild )
360 SUIT_DataObject* aSubChild = theChild->firstChild();
361 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
362 removeChild( theChild, aSubChild );
363 theParent->removeChild( theChild );
366 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
367 const QString& theName )
369 SUIT_DataObject* aChild = theFather->firstChild();
372 if( aChild->name() == theName )
373 return aChild; // found
374 aChild = aChild->nextBrother();
376 return NULL; // not found