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"
29 #include "HYDROGUI_Region.h"
31 #include <HYDROData_Bathymetry.h>
32 #include <HYDROData_CalculationCase.h>
33 #include <HYDROData_Document.h>
34 #include <HYDROData_Image.h>
35 #include <HYDROData_ImmersibleZone.h>
36 #include <HYDROData_Iterator.h>
37 #include <HYDROData_Polyline.h>
38 #include <HYDROData_VisualState.h>
39 #include <HYDROData_Region.h>
40 #include <HYDROData_Zone.h>
42 #include <CAM_Application.h>
43 #include <CAM_DataObject.h>
44 #include <CAM_Module.h>
45 #include <CAM_Study.h>
47 #include <LightApp_Application.h>
48 #include <LightApp_DataObject.h>
49 #include <LightApp_Study.h>
51 #include <SUIT_DataObject.h>
52 #include <SUIT_DataBrowser.h>
53 #include <SUIT_ResourceMgr.h>
54 #include <SUIT_Study.h>
55 #include <SUIT_Tools.h>
57 #include <HYDROData_Document.h>
59 #include <TDF_Delta.hxx>
60 #include <TDF_ListIteratorOfDeltaList.hxx>
62 #include <QApplication>
65 static HYDROData_SequenceOfObjects myCopyingObjects;
67 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
68 : LightApp_DataModel( theModule )
70 update( module()->application()->activeStudy()->id() );
73 HYDROGUI_DataModel::~HYDROGUI_DataModel()
77 bool HYDROGUI_DataModel::open( const QString& theURL,
79 QStringList theFileList )
81 LightApp_DataModel::open( theURL, theStudy, theFileList );
82 const int aStudyId = theStudy->id();
84 Data_DocError res = DocError_UnknownProblem;
85 if( theFileList.count() == 2 )
87 QString aTmpDir = theFileList[0];
88 QString aFileName = theFileList[1];
91 QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
95 res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
99 res = DocError_UnknownProblem;
101 if( res != DocError_OK )
103 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
108 // if the document open was successful, the data model update happens
109 // in the set mode of the module
110 if( res == DocError_OK )
116 bool HYDROGUI_DataModel::save( QStringList& theFileList )
118 if( !module()->application()->activeStudy() )
121 LightApp_DataModel::save( theFileList );
125 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
126 bool isMultiFile = false;
128 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
130 // save data to temporary files
131 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
132 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
133 aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
135 QString aFullPath = aTmpDir + aFileName;
136 Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
137 if( res != DocError_OK )
139 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
143 theFileList.append( aTmpDir );
144 theFileList.append( aFileName );
149 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
151 QStringList& theFileList )
154 return save( theFileList );
157 bool HYDROGUI_DataModel::close()
162 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
165 QStringList& theListOfFiles )
167 LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
169 int aStudyId = theStudy->id();
171 LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
172 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
173 if ( aDocument.IsNull() || !aStudy )
176 QString aFileToExport = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
177 aFileToExport += QString( QDir::separator() ) + "HYDRO.py";
179 bool aRes = aDocument->DumpToPython( aFileToExport );
183 theListOfFiles.append( aFileToExport );
189 bool HYDROGUI_DataModel::isModified() const
191 return getDocument()->IsModified();
194 bool HYDROGUI_DataModel::isSaved() const
199 void HYDROGUI_DataModel::update( const int theStudyId )
201 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
205 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
209 // create root object if not exist
210 CAM_DataObject* aRootObj = root();
212 aRootObj = createRootModuleObject( aStudyRoot );
217 DataObjectList aList;
218 aRootObj->children( aList );
219 QListIterator<SUIT_DataObject*> anIter( aList );
220 while( anIter.hasNext() )
221 removeChild( aRootObj, anIter.next() );
223 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
224 if( aDocument.IsNull() )
227 LightApp_DataObject* anImageRootObj = createObject( aRootObj, tr( partitionName( KIND_IMAGE ).toAscii() ) );
229 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
230 for( ; anIterator.More(); anIterator.Next() )
232 Handle(HYDROData_Image) anImageObj =
233 Handle(HYDROData_Image)::DownCast( anIterator.Current() );
234 if( !anImageObj.IsNull() )
235 createObject( anImageRootObj, anImageObj );
238 LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, tr( partitionName( KIND_BATHYMETRY ).toAscii() ) );
240 anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY );
241 for( ; anIterator.More(); anIterator.Next() )
243 Handle(HYDROData_Bathymetry) aBathymetryObj =
244 Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() );
245 if( !aBathymetryObj.IsNull() )
246 createObject( aBathymetryRootObj, aBathymetryObj );
249 LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, tr( partitionName( KIND_POLYLINE ).toAscii() ) );
251 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
252 for( ; anIterator.More(); anIterator.Next() )
254 Handle(HYDROData_Polyline) aPolylineObj =
255 Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
256 if( !aPolylineObj.IsNull() )
257 createObject( aPolylineRootObj, aPolylineObj );
260 LightApp_DataObject* aZonesRootObj = createObject( aRootObj, tr( partitionName( KIND_IMMERSIBLE_ZONE ).toAscii() ) );
262 anIterator = HYDROData_Iterator( aDocument, KIND_IMMERSIBLE_ZONE );
263 for( ; anIterator.More(); anIterator.Next() )
265 Handle(HYDROData_ImmersibleZone) aZoneObj =
266 Handle(HYDROData_ImmersibleZone)::DownCast( anIterator.Current() );
267 if( !aZoneObj.IsNull() )
268 createObject( aZonesRootObj, aZoneObj );
271 LightApp_DataObject* aCalculRootObj = createObject( aRootObj, tr( partitionName( KIND_CALCULATION ).toAscii() ) );
273 anIterator = HYDROData_Iterator( aDocument, KIND_CALCULATION );
274 for( ; anIterator.More(); anIterator.Next() )
276 Handle(HYDROData_CalculationCase) aCalculObj =
277 Handle(HYDROData_CalculationCase)::DownCast( anIterator.Current() );
278 if( !aCalculObj.IsNull() )
279 createObject( aCalculRootObj, aCalculObj );
282 LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, tr( partitionName( KIND_VISUAL_STATE ).toAscii() ) );
284 anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
285 for( ; anIterator.More(); anIterator.Next() )
287 Handle(HYDROData_VisualState) aVisualStateObj =
288 Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
289 if( !aVisualStateObj.IsNull() )
290 createObject( aVisualStateRootObj, aVisualStateObj );
293 if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
295 anObjectBrowser->setAutoOpenLevel( 3 );
296 anObjectBrowser->openLevels();
300 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject )
302 return NULL; // to do if necessary
305 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
307 return NULL; // to do if necessary
310 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
312 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
313 return anApp ? anApp->findObject( theEntry ) : 0;
316 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
317 LightApp_Study* theStudy )
320 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
322 update( theStudy->id() );
325 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
327 CAM_DataObject* aRootObj = createModuleObject( theParent );
332 void HYDROGUI_DataModel::updateModel()
334 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
336 update( aModule->getStudyId() );
339 Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
340 const ObjectKind theObjectKind )
342 QString anEntry = theEntry;
343 if( anEntry.indexOf( "_" ) != -1 ) // reference object
344 anEntry = anEntry.section( "_", -1 );
346 Handle(HYDROData_Document) aDocument = getDocument();
347 if( !aDocument.IsNull() )
349 HYDROData_Iterator anIterator( aDocument, theObjectKind );
350 for( ; anIterator.More(); anIterator.Next() )
352 Handle(HYDROData_Entity) anObject = anIterator.Current();
353 if( !anObject.IsNull() )
355 QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
356 if( anEntryRef == anEntry )
364 bool HYDROGUI_DataModel::canUndo() const
366 return getDocument()->CanUndo();
369 bool HYDROGUI_DataModel::canRedo() const
371 return getDocument()->CanRedo();
374 QStringList HYDROGUI_DataModel::undoNames() const
377 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
378 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
382 QStringList HYDROGUI_DataModel::redoNames() const
385 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
386 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
390 void HYDROGUI_DataModel::clearUndos()
392 getDocument()->ClearUndos();
395 void HYDROGUI_DataModel::clearRedos()
397 getDocument()->ClearRedos();
400 bool HYDROGUI_DataModel::undo()
404 getDocument()->Undo();
406 catch ( Standard_Failure )
413 bool HYDROGUI_DataModel::redo()
417 getDocument()->Redo();
419 catch ( Standard_Failure )
426 bool HYDROGUI_DataModel::canCopy()
428 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
429 if( aSeq.Length() != 1 )
432 Handle(HYDROData_Entity) anObject = aSeq.First();
433 if( anObject.IsNull() )
436 ObjectKind aKind = anObject->GetKind();
437 if( aKind == KIND_IMAGE ||
438 aKind == KIND_POLYLINE ||
439 aKind == KIND_CALCULATION )
445 bool HYDROGUI_DataModel::canPaste()
447 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
449 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
450 if( !anObject.IsNull() && !anObject->IsRemoved() )
456 bool HYDROGUI_DataModel::copy()
458 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
459 changeCopyingObjects( aSeq );
463 bool HYDROGUI_DataModel::paste()
465 bool anIsChanged = false;
466 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
468 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
469 if( !anObject.IsNull() && !anObject->IsRemoved() )
471 ObjectKind aKind = anObject->GetKind();
472 Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
473 if( !aClone.IsNull() )
475 anObject->CopyTo( aClone );
478 // generate a new unique name for the clone object:
479 // case 1: Image_1 -> Image_2
480 // case 2: ImageObj -> ImageObj_1
481 QString aName = aClone->GetName();
482 QString aPrefix = aName;
483 if( aName.contains( '_' ) ) // case 1
485 QString aSuffix = aName.section( '_', -1 );
486 bool anIsInteger = false;
487 aSuffix.toInt( &anIsInteger );
489 aPrefix = aName.section( '_', 0, -2 );
493 aName = HYDROGUI_Tool::GenerateObjectName( (HYDROGUI_Module*)module(), aPrefix );
494 aClone->SetName( aName );
501 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
503 myCopyingObjects.Assign( theSeq );
506 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
508 switch( theObjectKind )
510 case KIND_IMAGE: return "IMAGES";
511 case KIND_POLYLINE: return "POLYLINES";
512 case KIND_VISUAL_STATE: return "VISUAL_STATES";
513 case KIND_BATHYMETRY: return "BATHYMETRIES";
514 case KIND_CALCULATION: return "CALCULATION_CASES";
515 case KIND_IMMERSIBLE_ZONE: return "IMMERSIBLE_ZONES";
521 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
523 int aStudyId = module()->application()->activeStudy()->id();
524 return HYDROData_Document::Document( aStudyId );
527 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
528 Handle(HYDROData_Entity) theModelObject,
529 const QString& theParentEntry,
530 const bool theIsBuildTree )
532 HYDROGUI_DataObject* aResObj = new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
534 if ( theIsBuildTree )
536 buildObjectTree( theParent, aResObj, theParentEntry );
542 LightApp_DataObject* HYDROGUI_DataModel::buildObject( SUIT_DataObject* theParent,
543 HYDROGUI_DataObject* theObject,
544 const QString& theParentEntry,
545 const bool theIsBuildTree )
547 if ( theIsBuildTree )
549 buildObjectTree( theParent, theObject, theParentEntry );
554 LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject* theParent,
555 Handle(HYDROData_Zone) theModelObject,
556 const QString& theParentEntry,
557 const bool theIsBuildTree )
559 return buildObject( theParent, new HYDROGUI_Zone( theParent, theModelObject, theParentEntry ), theParentEntry, theIsBuildTree );
562 LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject* theParent,
563 Handle(HYDROData_Region) theModelObject,
564 const QString& theParentEntry,
565 const bool theIsBuildTree )
567 return buildObject( theParent, new HYDROGUI_Region( theParent, theModelObject, theParentEntry ), theParentEntry, theIsBuildTree );
570 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
571 const QString& theName,
572 const QString& theParentEntry )
574 return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
577 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
578 SUIT_DataObject* theObject,
579 const QString& theParentEntry )
581 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
585 Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
586 if ( aDataObj.IsNull() )
589 ObjectKind anObjectKind = aDataObj->GetKind();
591 if ( anObjectKind == KIND_IMAGE )
593 Handle(HYDROData_Image) anImageObj =
594 Handle(HYDROData_Image)::DownCast( aDataObj );
595 for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
597 Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
598 if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
599 createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
602 else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
604 Handle(HYDROData_ImmersibleZone) aZoneObj =
605 Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
607 LightApp_DataObject* aPolylineSect =
608 createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
610 Handle(HYDROData_Polyline) aPolyline = aZoneObj->GetPolyline();
611 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
612 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
614 LightApp_DataObject* aBathSect =
615 createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
617 Handle(HYDROData_Bathymetry) aBathymetry = aZoneObj->GetBathymetry();
618 if ( !aBathymetry.IsNull() && !aBathymetry->IsRemoved() )
619 createObject( aBathSect, aBathymetry, aGuiObj->entry(), false );
621 else if ( anObjectKind == KIND_CALCULATION )
623 Handle(HYDROData_CalculationCase) aCaseObj =
624 Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
626 LightApp_DataObject* aCaseRegionsSect =
627 createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
629 HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions();
630 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
631 for ( ; anIter.More(); anIter.Next() )
633 Handle(HYDROData_Region) aCaseRegion =
634 Handle(HYDROData_Region)::DownCast( anIter.Value() );
635 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
636 createRegion( aCaseRegionsSect, aCaseRegion, "", true );
639 else if ( anObjectKind == KIND_REGION )
641 Handle(HYDROData_Region) aRegionObj =
642 Handle(HYDROData_Region)::DownCast( aDataObj );
644 HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
645 HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
646 for ( ; anIter.More(); anIter.Next() )
648 Handle(HYDROData_Zone) aRegionZone =
649 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
650 if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
651 createZone( aGuiObj, aRegionZone, "", true );
656 void HYDROGUI_DataModel::buildCaseTree( SUIT_DataObject* theParent, Handle(HYDROData_CalculationCase) theCase )
658 if ( !theCase.IsNull() )
660 new HYDROGUI_DropTargetObject( theParent, tr( "NEW_REGION" ), "" );
662 HYDROData_SequenceOfObjects aCaseRegions = theCase->GetRegions();
663 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
664 for ( ; anIter.More(); anIter.Next() )
666 Handle(HYDROData_Region) aCaseRegion =
667 Handle(HYDROData_Region)::DownCast( anIter.Value() );
668 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
669 createRegion( theParent, aCaseRegion, "", true );
674 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
675 SUIT_DataObject* theChild )
677 SUIT_DataObject* aSubChild = theChild->firstChild();
678 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
679 removeChild( theChild, aSubChild );
680 theParent->removeChild( theChild );
683 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
684 const QString& theName )
686 SUIT_DataObject* aChild = theFather->firstChild();
689 if( aChild->name() == theName )
690 return aChild; // found
691 aChild = aChild->nextBrother();
693 return NULL; // not found
696 bool HYDROGUI_DataModel::createNewRegion( Handle(HYDROData_CalculationCase) theCase,
697 const QList<HYDROGUI_Zone*>& theZonesList )
699 bool isOk = !theCase.IsNull();
702 Handle(HYDROData_Region) aRegion;
703 Handle(HYDROData_Zone) aZone;
704 for (int i = 0; i < theZonesList.length(); i++ )
706 aZone = Handle(HYDROData_Zone)::DownCast( theZonesList.at(i)->modelObject() );
707 if ( !aZone.IsNull() )
709 if ( aRegion.IsNull() )
711 aRegion = theCase->AddNewRegion( aZone );
712 isOk = !aRegion.IsNull();
716 if ( !( aRegion->AddZone( aZone ) ) )