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 static HYDROData_SequenceOfObjects myCopyingObjects;
62 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
63 : LightApp_DataModel( theModule )
65 update( module()->application()->activeStudy()->id() );
68 HYDROGUI_DataModel::~HYDROGUI_DataModel()
72 bool HYDROGUI_DataModel::open( const QString& theURL,
74 QStringList theFileList )
76 LightApp_DataModel::open( theURL, theStudy, theFileList );
77 const int aStudyId = theStudy->id();
79 Data_DocError res = DocError_UnknownProblem;
80 if( theFileList.count() == 2 )
82 QString aTmpDir = theFileList[0];
83 QString aFileName = theFileList[1];
86 QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
90 res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
94 res = DocError_UnknownProblem;
96 if( res != DocError_OK )
98 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
103 // if the document open was successful, the data model update happens
104 // in the set mode of the module
105 if( res == DocError_OK )
111 bool HYDROGUI_DataModel::save( QStringList& theFileList )
113 if( !module()->application()->activeStudy() )
116 LightApp_DataModel::save( theFileList );
120 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
121 bool isMultiFile = false;
123 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
125 // save data to temporary files
126 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
127 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
128 aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
130 QString aFullPath = aTmpDir + aFileName;
131 Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
132 if( res != DocError_OK )
134 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
138 theFileList.append( aTmpDir );
139 theFileList.append( aFileName );
144 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
146 QStringList& theFileList )
149 return save( theFileList );
152 bool HYDROGUI_DataModel::close()
157 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
160 QStringList& theListOfFiles )
162 LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
164 int aStudyId = theStudy->id();
166 LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
167 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
168 if ( aDocument.IsNull() || !aStudy )
171 QString aFileToExport = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
172 aFileToExport += QString( QDir::separator() ) + "HYDRO.py";
174 bool aRes = aDocument->DumpToPython( aFileToExport );
178 theListOfFiles.append( aFileToExport );
184 bool HYDROGUI_DataModel::isModified() const
186 return getDocument()->IsModified();
189 bool HYDROGUI_DataModel::isSaved() const
194 void HYDROGUI_DataModel::update( const int theStudyId )
196 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
200 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
204 // create root object if not exist
205 CAM_DataObject* aRootObj = root();
207 aRootObj = createRootModuleObject( aStudyRoot );
212 DataObjectList aList;
213 aRootObj->children( aList );
214 QListIterator<SUIT_DataObject*> anIter( aList );
215 while( anIter.hasNext() )
216 removeChild( aRootObj, anIter.next() );
218 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
219 if( aDocument.IsNull() )
222 LightApp_DataObject* anImageRootObj = createObject( aRootObj, partitionName( KIND_IMAGE ) );
224 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
225 for( ; anIterator.More(); anIterator.Next() )
227 Handle(HYDROData_Image) anImageObj =
228 Handle(HYDROData_Image)::DownCast( anIterator.Current() );
229 if( !anImageObj.IsNull() )
231 if( LightApp_DataObject* anImageDataObj = createObject( anImageRootObj, anImageObj ) )
233 for( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
235 Handle(HYDROData_Object) aRefObj = anImageObj->Reference( anIndex );
236 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
237 createObject( anImageDataObj, aRefObj, anImageDataObj->entry() );
243 LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, partitionName( KIND_BATHYMETRY ) );
245 anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY );
246 for( ; anIterator.More(); anIterator.Next() )
248 Handle(HYDROData_Bathymetry) aBathymetryObj =
249 Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() );
250 if( !aBathymetryObj.IsNull() )
251 createObject( aBathymetryRootObj, aBathymetryObj );
254 LightApp_DataObject* aCalculRootObj = createObject( aRootObj, partitionName( KIND_CALCULATION ) );
256 anIterator = HYDROData_Iterator( aDocument, KIND_CALCULATION );
257 for( ; anIterator.More(); anIterator.Next() )
259 Handle(HYDROData_Calculation) aCalculObj =
260 Handle(HYDROData_Calculation)::DownCast( anIterator.Current() );
261 if( !aCalculObj.IsNull() )
262 createObject( aCalculRootObj, aCalculObj );
265 LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, partitionName( KIND_POLYLINE ) );
267 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
268 for( ; anIterator.More(); anIterator.Next() )
270 Handle(HYDROData_Polyline) aPolylineObj =
271 Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
272 if( !aPolylineObj.IsNull() )
273 createObject( aPolylineRootObj, aPolylineObj );
276 LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, partitionName( KIND_VISUAL_STATE ) );
278 anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
279 for( ; anIterator.More(); anIterator.Next() )
281 Handle(HYDROData_VisualState) aVisualStateObj =
282 Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
283 if( !aVisualStateObj.IsNull() )
284 createObject( aVisualStateRootObj, aVisualStateObj );
287 if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
289 anObjectBrowser->setAutoOpenLevel( 3 );
290 anObjectBrowser->openLevels();
294 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
296 return NULL; // to do if necessary
299 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
301 return NULL; // to do if necessary
304 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
306 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
307 return anApp ? anApp->findObject( theEntry ) : 0;
310 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
311 LightApp_Study* theStudy )
314 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
316 update( theStudy->id() );
319 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
321 CAM_DataObject* aRootObj = createModuleObject( theParent );
326 void HYDROGUI_DataModel::updateModel()
328 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
330 update( aModule->getStudyId() );
333 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
334 const ObjectKind theObjectKind )
336 QString anEntry = theEntry;
337 if( anEntry.indexOf( "_" ) != -1 ) // reference object
338 anEntry = anEntry.section( "_", -1 );
340 Handle(HYDROData_Document) aDocument = getDocument();
341 if( !aDocument.IsNull() )
343 HYDROData_Iterator anIterator( aDocument, theObjectKind );
344 for( ; anIterator.More(); anIterator.Next() )
346 Handle(HYDROData_Object) anObject = anIterator.Current();
347 if( !anObject.IsNull() )
349 QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
350 if( anEntryRef == anEntry )
358 bool HYDROGUI_DataModel::canUndo() const
360 return getDocument()->CanUndo();
363 bool HYDROGUI_DataModel::canRedo() const
365 return getDocument()->CanRedo();
368 QStringList HYDROGUI_DataModel::undoNames() const
371 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
372 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
376 QStringList HYDROGUI_DataModel::redoNames() const
379 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
380 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
384 void HYDROGUI_DataModel::clearUndos()
386 getDocument()->ClearUndos();
389 void HYDROGUI_DataModel::clearRedos()
391 getDocument()->ClearRedos();
394 bool HYDROGUI_DataModel::undo()
398 getDocument()->Undo();
400 catch ( Standard_Failure )
407 bool HYDROGUI_DataModel::redo()
411 getDocument()->Redo();
413 catch ( Standard_Failure )
420 bool HYDROGUI_DataModel::canCopy()
422 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
423 if( aSeq.Length() != 1 )
426 Handle(HYDROData_Object) anObject = aSeq.First();
427 if( anObject.IsNull() )
430 ObjectKind aKind = anObject->GetKind();
431 if( aKind == KIND_IMAGE ||
432 aKind == KIND_POLYLINE ||
433 aKind == KIND_CALCULATION )
439 bool HYDROGUI_DataModel::canPaste()
441 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
443 Handle(HYDROData_Object) anObject = myCopyingObjects.Value( anIndex );
444 if( !anObject.IsNull() && !anObject->IsRemoved() )
450 bool HYDROGUI_DataModel::copy()
452 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
453 changeCopyingObjects( aSeq );
457 bool HYDROGUI_DataModel::paste()
459 bool anIsChanged = false;
460 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
462 Handle(HYDROData_Object) anObject = myCopyingObjects.Value( anIndex );
463 if( !anObject.IsNull() && !anObject->IsRemoved() )
465 ObjectKind aKind = anObject->GetKind();
466 Handle(HYDROData_Object) aClone = getDocument()->CreateObject( aKind );
467 if( !aClone.IsNull() )
469 anObject->CopyTo( aClone );
472 // generate a new unique name for the clone object:
473 // case 1: Image_1 -> Image_2
474 // case 2: ImageObj -> ImageObj_1
475 QString aName = aClone->GetName();
476 QString aPrefix = aName;
477 if( aName.contains( '_' ) ) // case 1
479 QString aSuffix = aName.section( '_', -1 );
480 bool anIsInteger = false;
481 aSuffix.toInt( &anIsInteger );
483 aPrefix = aName.section( '_', 0, -2 );
487 aName = HYDROGUI_Tool::GenerateObjectName( (HYDROGUI_Module*)module(), aPrefix );
488 aClone->SetName( aName );
495 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
497 myCopyingObjects.Assign( theSeq );
500 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
502 switch( theObjectKind )
504 case KIND_IMAGE: return "IMAGES";
505 case KIND_POLYLINE: return "POLYLINES";
506 case KIND_VISUAL_STATE: return "VISUAL_STATES";
507 case KIND_BATHYMETRY: return "BATHYMETRIES";
508 case KIND_CALCULATION: return "CALCULATION_CASES";
514 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
516 int aStudyId = module()->application()->activeStudy()->id();
517 return HYDROData_Document::Document( aStudyId );
520 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
521 Handle(HYDROData_Object) theModelObject,
522 const QString& theParentEntry )
524 return new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
527 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
528 const QString& theName )
530 return new HYDROGUI_NamedObject( theParent, theName );
533 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
534 SUIT_DataObject* theChild )
536 SUIT_DataObject* aSubChild = theChild->firstChild();
537 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
538 removeChild( theChild, aSubChild );
539 theParent->removeChild( theChild );
542 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
543 const QString& theName )
545 SUIT_DataObject* aChild = theFather->firstChild();
548 if( aChild->name() == theName )
549 return aChild; // found
550 aChild = aChild->nextBrother();
552 return NULL; // not found