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_Bathymetry.h>
30 #include <HYDROData_Calculation.h>
31 #include <HYDROData_Document.h>
32 #include <HYDROData_Image.h>
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_Polyline.h>
35 #include <HYDROData_VisualState.h>
37 #include <CAM_Application.h>
38 #include <CAM_DataObject.h>
39 #include <CAM_Module.h>
40 #include <CAM_Study.h>
42 #include <LightApp_Application.h>
43 #include <LightApp_DataObject.h>
44 #include <LightApp_Study.h>
46 #include <SUIT_DataObject.h>
47 #include <SUIT_DataBrowser.h>
48 #include <SUIT_ResourceMgr.h>
49 #include <SUIT_Study.h>
50 #include <SUIT_Tools.h>
52 #include <HYDROData_Document.h>
54 #include <TDF_Delta.hxx>
55 #include <TDF_ListIteratorOfDeltaList.hxx>
57 #include <QApplication>
60 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
61 : LightApp_DataModel( theModule )
63 update( module()->application()->activeStudy()->id() );
66 HYDROGUI_DataModel::~HYDROGUI_DataModel()
70 bool HYDROGUI_DataModel::open( const QString& theURL,
72 QStringList theFileList )
74 LightApp_DataModel::open( theURL, theStudy, theFileList );
75 const int aStudyId = theStudy->id();
77 Data_DocError res = DocError_UnknownProblem;
78 if( theFileList.count() == 2 )
80 QString aTmpDir = theFileList[0];
81 QString aFileName = theFileList[1];
84 QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
88 res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
92 res = DocError_UnknownProblem;
94 if( res != DocError_OK )
96 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
101 // if the document open was successful, the data model update happens
102 // in the set mode of the module
103 if( res == DocError_OK )
109 bool HYDROGUI_DataModel::save( QStringList& theFileList )
111 if( !module()->application()->activeStudy() )
114 LightApp_DataModel::save( theFileList );
118 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
119 bool isMultiFile = false;
121 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
123 // save data to temporary files
124 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
125 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
126 aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
128 QString aFullPath = aTmpDir + aFileName;
129 Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
130 if( res != DocError_OK )
132 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
136 theFileList.append( aTmpDir );
137 theFileList.append( aFileName );
142 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
144 QStringList& theFileList )
147 return save( theFileList );
150 bool HYDROGUI_DataModel::close()
155 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
158 QStringList& theListOfFiles )
160 LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
162 int aStudyId = theStudy->id();
164 LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
165 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
166 if ( aDocument.IsNull() || !aStudy )
169 QString aFileToExport = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
170 aFileToExport += QString( QDir::separator() ) + "HYDRO.py";
172 bool aRes = aDocument->DumpToPython( aFileToExport );
176 theListOfFiles.append( aFileToExport );
182 bool HYDROGUI_DataModel::isModified() const
184 return getDocument()->IsModified();
187 bool HYDROGUI_DataModel::isSaved() const
192 void HYDROGUI_DataModel::update( const int theStudyId )
194 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
198 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
202 // create root object if not exist
203 CAM_DataObject* aRootObj = root();
205 aRootObj = createRootModuleObject( aStudyRoot );
210 DataObjectList aList;
211 aRootObj->children( aList );
212 QListIterator<SUIT_DataObject*> anIter( aList );
213 while( anIter.hasNext() )
214 removeChild( aRootObj, anIter.next() );
216 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
217 if( aDocument.IsNull() )
220 LightApp_DataObject* anImageRootObj = createObject( aRootObj, "IMAGES" );
222 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
223 for( ; anIterator.More(); anIterator.Next() )
225 Handle(HYDROData_Image) anImageObj =
226 Handle(HYDROData_Image)::DownCast( anIterator.Current() );
227 if( !anImageObj.IsNull() )
229 if( LightApp_DataObject* anImageDataObj = createObject( anImageRootObj, anImageObj ) )
231 for( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
233 Handle(HYDROData_Object) aRefObj = anImageObj->Reference( anIndex );
234 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
235 createObject( anImageDataObj, aRefObj, anImageDataObj->entry() );
241 LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, "BATHYMETRIES" );
243 anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY );
244 for( ; anIterator.More(); anIterator.Next() )
246 Handle(HYDROData_Bathymetry) aBathymetryObj =
247 Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() );
248 if( !aBathymetryObj.IsNull() )
249 createObject( aBathymetryRootObj, aBathymetryObj );
252 LightApp_DataObject* aCalculRootObj = createObject( aRootObj, "CALCULATION CASES" );
254 anIterator = HYDROData_Iterator( aDocument, KIND_CALCULATION );
255 for( ; anIterator.More(); anIterator.Next() )
257 Handle(HYDROData_Calculation) aCalculObj =
258 Handle(HYDROData_Calculation)::DownCast( anIterator.Current() );
259 if( !aCalculObj.IsNull() )
260 createObject( aCalculRootObj, aCalculObj );
263 LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, "POLYLINES" );
265 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
266 for( ; anIterator.More(); anIterator.Next() )
268 Handle(HYDROData_Polyline) aPolylineObj =
269 Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
270 if( !aPolylineObj.IsNull() )
271 createObject( aPolylineRootObj, aPolylineObj );
274 LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, "VISUAL_STATES" );
276 anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
277 for( ; anIterator.More(); anIterator.Next() )
279 Handle(HYDROData_VisualState) aVisualStateObj =
280 Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
281 if( !aVisualStateObj.IsNull() )
282 createObject( aVisualStateRootObj, aVisualStateObj );
285 if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
287 anObjectBrowser->setAutoOpenLevel( 3 );
288 anObjectBrowser->openLevels();
292 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
294 return NULL; // to do if necessary
297 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
299 return NULL; // to do if necessary
302 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
304 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
305 return anApp ? anApp->findObject( theEntry ) : 0;
308 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
309 LightApp_Study* theStudy )
312 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
314 update( theStudy->id() );
317 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
319 CAM_DataObject* aRootObj = createModuleObject( theParent );
324 void HYDROGUI_DataModel::updateModel()
326 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
328 update( aModule->getStudyId() );
331 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
332 const ObjectKind theObjectKind )
334 QString anEntry = theEntry;
335 if( anEntry.indexOf( "_" ) != -1 ) // reference object
336 anEntry = anEntry.section( "_", -1 );
338 Handle(HYDROData_Document) aDocument = getDocument();
339 if( !aDocument.IsNull() )
341 HYDROData_Iterator anIterator( aDocument, theObjectKind );
342 for( ; anIterator.More(); anIterator.Next() )
344 Handle(HYDROData_Object) anObject = anIterator.Current();
345 if( !anObject.IsNull() )
347 QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
348 if( anEntryRef == anEntry )
356 bool HYDROGUI_DataModel::canUndo() const
358 return getDocument()->CanUndo();
361 bool HYDROGUI_DataModel::canRedo() const
363 return getDocument()->CanRedo();
366 QStringList HYDROGUI_DataModel::undoNames() const
369 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
370 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
374 QStringList HYDROGUI_DataModel::redoNames() const
377 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
378 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
382 void HYDROGUI_DataModel::clearUndos()
384 getDocument()->ClearUndos();
387 void HYDROGUI_DataModel::clearRedos()
389 getDocument()->ClearRedos();
392 bool HYDROGUI_DataModel::undo()
396 getDocument()->Undo();
398 catch ( Standard_Failure )
405 bool HYDROGUI_DataModel::redo()
409 getDocument()->Redo();
411 catch ( Standard_Failure )
418 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
420 int aStudyId = module()->application()->activeStudy()->id();
421 return HYDROData_Document::Document( aStudyId );
424 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
425 Handle(HYDROData_Object) theModelObject,
426 const QString& theParentEntry )
428 return new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
431 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
432 const QString& theName )
434 return new HYDROGUI_NamedObject( theParent, theName );
437 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
438 SUIT_DataObject* theChild )
440 SUIT_DataObject* aSubChild = theChild->firstChild();
441 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
442 removeChild( theChild, aSubChild );
443 theParent->removeChild( theChild );
446 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
447 const QString& theName )
449 SUIT_DataObject* aChild = theFather->firstChild();
452 if( aChild->name() == theName )
453 return aChild; // found
454 aChild = aChild->nextBrother();
456 return NULL; // not found