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"
28 #include "HYDROGUI_Zone.h"
30 #include <HYDROData_Bathymetry.h>
31 #include <HYDROData_CalculationCase.h>
32 #include <HYDROData_Document.h>
33 #include <HYDROData_Image.h>
34 #include <HYDROData_ImmersibleZone.h>
35 #include <HYDROData_Iterator.h>
36 #include <HYDROData_Polyline.h>
37 #include <HYDROData_VisualState.h>
38 #include <HYDROData_Region.h>
39 #include <HYDROData_Zone.h>
41 #include <CAM_Application.h>
42 #include <CAM_DataObject.h>
43 #include <CAM_Module.h>
44 #include <CAM_Study.h>
46 #include <LightApp_Application.h>
47 #include <LightApp_DataObject.h>
48 #include <LightApp_Study.h>
50 #include <SUIT_DataObject.h>
51 #include <SUIT_DataBrowser.h>
52 #include <SUIT_ResourceMgr.h>
53 #include <SUIT_Study.h>
54 #include <SUIT_Tools.h>
56 #include <HYDROData_Document.h>
58 #include <TDF_Delta.hxx>
59 #include <TDF_ListIteratorOfDeltaList.hxx>
61 #include <QApplication>
64 static HYDROData_SequenceOfObjects myCopyingObjects;
66 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
67 : LightApp_DataModel( theModule )
69 update( module()->application()->activeStudy()->id() );
72 HYDROGUI_DataModel::~HYDROGUI_DataModel()
76 bool HYDROGUI_DataModel::open( const QString& theURL,
78 QStringList theFileList )
80 LightApp_DataModel::open( theURL, theStudy, theFileList );
81 const int aStudyId = theStudy->id();
83 Data_DocError res = DocError_UnknownProblem;
84 if( theFileList.count() == 2 )
86 QString aTmpDir = theFileList[0];
87 QString aFileName = theFileList[1];
90 QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
94 res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
98 res = DocError_UnknownProblem;
100 if( res != DocError_OK )
102 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
107 // if the document open was successful, the data model update happens
108 // in the set mode of the module
109 if( res == DocError_OK )
115 bool HYDROGUI_DataModel::save( QStringList& theFileList )
117 if( !module()->application()->activeStudy() )
120 LightApp_DataModel::save( theFileList );
124 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
125 bool isMultiFile = false;
127 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
129 // save data to temporary files
130 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
131 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
132 aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
134 QString aFullPath = aTmpDir + aFileName;
135 Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
136 if( res != DocError_OK )
138 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
142 theFileList.append( aTmpDir );
143 theFileList.append( aFileName );
148 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
150 QStringList& theFileList )
153 return save( theFileList );
156 bool HYDROGUI_DataModel::close()
161 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
164 QStringList& theListOfFiles )
166 LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
168 int aStudyId = theStudy->id();
170 LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
171 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
172 if ( aDocument.IsNull() || !aStudy )
175 QString aFileToExport = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
176 aFileToExport += QString( QDir::separator() ) + "HYDRO.py";
178 bool aRes = aDocument->DumpToPython( aFileToExport );
182 theListOfFiles.append( aFileToExport );
188 bool HYDROGUI_DataModel::isModified() const
190 return getDocument()->IsModified();
193 bool HYDROGUI_DataModel::isSaved() const
198 void HYDROGUI_DataModel::update( const int theStudyId )
200 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
204 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
208 // create root object if not exist
209 CAM_DataObject* aRootObj = root();
211 aRootObj = createRootModuleObject( aStudyRoot );
216 DataObjectList aList;
217 aRootObj->children( aList );
218 QListIterator<SUIT_DataObject*> anIter( aList );
219 while( anIter.hasNext() )
220 removeChild( aRootObj, anIter.next() );
222 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
223 if( aDocument.IsNull() )
226 LightApp_DataObject* anImageRootObj = createObject( aRootObj, partitionName( KIND_IMAGE ) );
228 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
229 for( ; anIterator.More(); anIterator.Next() )
231 Handle(HYDROData_Image) anImageObj =
232 Handle(HYDROData_Image)::DownCast( anIterator.Current() );
233 if( !anImageObj.IsNull() )
234 createObject( anImageRootObj, anImageObj );
237 LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, partitionName( KIND_BATHYMETRY ) );
239 anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY );
240 for( ; anIterator.More(); anIterator.Next() )
242 Handle(HYDROData_Bathymetry) aBathymetryObj =
243 Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() );
244 if( !aBathymetryObj.IsNull() )
245 createObject( aBathymetryRootObj, aBathymetryObj );
248 LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, partitionName( KIND_POLYLINE ) );
250 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
251 for( ; anIterator.More(); anIterator.Next() )
253 Handle(HYDROData_Polyline) aPolylineObj =
254 Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
255 if( !aPolylineObj.IsNull() )
256 createObject( aPolylineRootObj, aPolylineObj );
259 LightApp_DataObject* aZonesRootObj = createObject( aRootObj, partitionName( KIND_IMMERSIBLE_ZONE ) );
261 anIterator = HYDROData_Iterator( aDocument, KIND_IMMERSIBLE_ZONE );
262 for( ; anIterator.More(); anIterator.Next() )
264 Handle(HYDROData_ImmersibleZone) aZoneObj =
265 Handle(HYDROData_ImmersibleZone)::DownCast( anIterator.Current() );
266 if( !aZoneObj.IsNull() )
267 createObject( aZonesRootObj, aZoneObj );
270 LightApp_DataObject* aCalculRootObj = createObject( aRootObj, partitionName( KIND_CALCULATION ) );
272 anIterator = HYDROData_Iterator( aDocument, KIND_CALCULATION );
273 for( ; anIterator.More(); anIterator.Next() )
275 Handle(HYDROData_CalculationCase) aCalculObj =
276 Handle(HYDROData_CalculationCase)::DownCast( anIterator.Current() );
277 if( !aCalculObj.IsNull() )
278 createObject( aCalculRootObj, aCalculObj );
281 LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, partitionName( KIND_VISUAL_STATE ) );
283 anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
284 for( ; anIterator.More(); anIterator.Next() )
286 Handle(HYDROData_VisualState) aVisualStateObj =
287 Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
288 if( !aVisualStateObj.IsNull() )
289 createObject( aVisualStateRootObj, aVisualStateObj );
292 if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
294 anObjectBrowser->setAutoOpenLevel( 3 );
295 anObjectBrowser->openLevels();
299 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject )
301 return NULL; // to do if necessary
304 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
306 return NULL; // to do if necessary
309 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
311 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
312 return anApp ? anApp->findObject( theEntry ) : 0;
315 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
316 LightApp_Study* theStudy )
319 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
321 update( theStudy->id() );
324 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
326 CAM_DataObject* aRootObj = createModuleObject( theParent );
331 void HYDROGUI_DataModel::updateModel()
333 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
335 update( aModule->getStudyId() );
338 Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
339 const ObjectKind theObjectKind )
341 QString anEntry = theEntry;
342 if( anEntry.indexOf( "_" ) != -1 ) // reference object
343 anEntry = anEntry.section( "_", -1 );
345 Handle(HYDROData_Document) aDocument = getDocument();
346 if( !aDocument.IsNull() )
348 HYDROData_Iterator anIterator( aDocument, theObjectKind );
349 for( ; anIterator.More(); anIterator.Next() )
351 Handle(HYDROData_Entity) anObject = anIterator.Current();
352 if( !anObject.IsNull() )
354 QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
355 if( anEntryRef == anEntry )
363 bool HYDROGUI_DataModel::canUndo() const
365 return getDocument()->CanUndo();
368 bool HYDROGUI_DataModel::canRedo() const
370 return getDocument()->CanRedo();
373 QStringList HYDROGUI_DataModel::undoNames() const
376 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
377 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
381 QStringList HYDROGUI_DataModel::redoNames() const
384 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
385 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
389 void HYDROGUI_DataModel::clearUndos()
391 getDocument()->ClearUndos();
394 void HYDROGUI_DataModel::clearRedos()
396 getDocument()->ClearRedos();
399 bool HYDROGUI_DataModel::undo()
403 getDocument()->Undo();
405 catch ( Standard_Failure )
412 bool HYDROGUI_DataModel::redo()
416 getDocument()->Redo();
418 catch ( Standard_Failure )
425 bool HYDROGUI_DataModel::canCopy()
427 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
428 if( aSeq.Length() != 1 )
431 Handle(HYDROData_Entity) anObject = aSeq.First();
432 if( anObject.IsNull() )
435 ObjectKind aKind = anObject->GetKind();
436 if( aKind == KIND_IMAGE ||
437 aKind == KIND_POLYLINE ||
438 aKind == KIND_CALCULATION )
444 bool HYDROGUI_DataModel::canPaste()
446 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
448 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
449 if( !anObject.IsNull() && !anObject->IsRemoved() )
455 bool HYDROGUI_DataModel::copy()
457 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
458 changeCopyingObjects( aSeq );
462 bool HYDROGUI_DataModel::paste()
464 bool anIsChanged = false;
465 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
467 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
468 if( !anObject.IsNull() && !anObject->IsRemoved() )
470 ObjectKind aKind = anObject->GetKind();
471 Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
472 if( !aClone.IsNull() )
474 anObject->CopyTo( aClone );
477 // generate a new unique name for the clone object:
478 // case 1: Image_1 -> Image_2
479 // case 2: ImageObj -> ImageObj_1
480 QString aName = aClone->GetName();
481 QString aPrefix = aName;
482 if( aName.contains( '_' ) ) // case 1
484 QString aSuffix = aName.section( '_', -1 );
485 bool anIsInteger = false;
486 aSuffix.toInt( &anIsInteger );
488 aPrefix = aName.section( '_', 0, -2 );
492 aName = HYDROGUI_Tool::GenerateObjectName( (HYDROGUI_Module*)module(), aPrefix );
493 aClone->SetName( aName );
500 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
502 myCopyingObjects.Assign( theSeq );
505 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
507 switch( theObjectKind )
509 case KIND_IMAGE: return "IMAGES";
510 case KIND_POLYLINE: return "POLYLINES";
511 case KIND_VISUAL_STATE: return "VISUAL_STATES";
512 case KIND_BATHYMETRY: return "BATHYMETRIES";
513 case KIND_CALCULATION: return "CALCULATION_CASES";
514 case KIND_IMMERSIBLE_ZONE: return "IMMERSIBLE_ZONES";
520 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
522 int aStudyId = module()->application()->activeStudy()->id();
523 return HYDROData_Document::Document( aStudyId );
526 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
527 Handle(HYDROData_Entity) theModelObject,
528 const QString& theParentEntry,
529 const bool theIsBuildTree )
531 HYDROGUI_DataObject* aResObj;
532 Handle(HYDROData_Zone) aRegionZone =
533 Handle(HYDROData_Zone)::DownCast( theModelObject );
534 if( !aRegionZone.IsNull() )
536 aResObj = new HYDROGUI_Zone( theParent, aRegionZone, theParentEntry );
540 aResObj = new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
543 if ( theIsBuildTree )
545 buildObjectTree( theParent, aResObj, theParentEntry );
551 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
552 const QString& theName,
553 const QString& theParentEntry )
555 return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
558 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
559 SUIT_DataObject* theObject,
560 const QString& theParentEntry )
562 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
566 Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
567 if ( aDataObj.IsNull() )
570 ObjectKind anObjectKind = aDataObj->GetKind();
572 if ( anObjectKind == KIND_IMAGE )
574 Handle(HYDROData_Image) anImageObj =
575 Handle(HYDROData_Image)::DownCast( aDataObj );
576 for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
578 Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
579 if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
580 createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
583 else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
585 Handle(HYDROData_ImmersibleZone) aZoneObj =
586 Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
588 LightApp_DataObject* aPolylineSect =
589 createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
591 Handle(HYDROData_Polyline) aPolyline = aZoneObj->GetPolyline();
592 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
593 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
595 LightApp_DataObject* aBathSect =
596 createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
598 Handle(HYDROData_Bathymetry) aBathymetry = aZoneObj->GetBathymetry();
599 if ( !aBathymetry.IsNull() && !aBathymetry->IsRemoved() )
600 createObject( aBathSect, aBathymetry, aGuiObj->entry(), false );
602 else if ( anObjectKind == KIND_CALCULATION )
604 Handle(HYDROData_CalculationCase) aCaseObj =
605 Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
607 LightApp_DataObject* aCaseRegionsSect =
608 createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
610 HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions();
611 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
612 for ( ; anIter.More(); anIter.Next() )
614 Handle(HYDROData_Region) aCaseRegion =
615 Handle(HYDROData_Region)::DownCast( anIter.Value() );
616 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
617 createObject( aCaseRegionsSect, aCaseRegion, "", true );
620 else if ( anObjectKind == KIND_REGION )
622 Handle(HYDROData_Region) aRegionObj =
623 Handle(HYDROData_Region)::DownCast( aDataObj );
625 HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
626 HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
627 for ( ; anIter.More(); anIter.Next() )
629 Handle(HYDROData_Zone) aRegionZone =
630 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
631 if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
632 createObject( aGuiObj, aRegionZone, "", true );
637 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
638 SUIT_DataObject* theChild )
640 SUIT_DataObject* aSubChild = theChild->firstChild();
641 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
642 removeChild( theChild, aSubChild );
643 theParent->removeChild( theChild );
646 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
647 const QString& theName )
649 SUIT_DataObject* aChild = theFather->firstChild();
652 if( aChild->name() == theName )
653 return aChild; // found
654 aChild = aChild->nextBrother();
656 return NULL; // not found