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_CalculationCase.h>
31 #include <HYDROData_Document.h>
32 #include <HYDROData_Image.h>
33 #include <HYDROData_ImmersibleZone.h>
34 #include <HYDROData_Iterator.h>
35 #include <HYDROData_Polyline.h>
36 #include <HYDROData_VisualState.h>
37 #include <HYDROData_Region.h>
38 #include <HYDROData_Zone.h>
40 #include <CAM_Application.h>
41 #include <CAM_DataObject.h>
42 #include <CAM_Module.h>
43 #include <CAM_Study.h>
45 #include <LightApp_Application.h>
46 #include <LightApp_DataObject.h>
47 #include <LightApp_Study.h>
49 #include <SUIT_DataObject.h>
50 #include <SUIT_DataBrowser.h>
51 #include <SUIT_ResourceMgr.h>
52 #include <SUIT_Study.h>
53 #include <SUIT_Tools.h>
55 #include <HYDROData_Document.h>
57 #include <TDF_Delta.hxx>
58 #include <TDF_ListIteratorOfDeltaList.hxx>
60 #include <QApplication>
63 static HYDROData_SequenceOfObjects myCopyingObjects;
65 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
66 : LightApp_DataModel( theModule )
68 update( module()->application()->activeStudy()->id() );
71 HYDROGUI_DataModel::~HYDROGUI_DataModel()
75 bool HYDROGUI_DataModel::open( const QString& theURL,
77 QStringList theFileList )
79 LightApp_DataModel::open( theURL, theStudy, theFileList );
80 const int aStudyId = theStudy->id();
82 Data_DocError res = DocError_UnknownProblem;
83 if( theFileList.count() == 2 )
85 QString aTmpDir = theFileList[0];
86 QString aFileName = theFileList[1];
89 QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
93 res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
97 res = DocError_UnknownProblem;
99 if( res != DocError_OK )
101 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
106 // if the document open was successful, the data model update happens
107 // in the set mode of the module
108 if( res == DocError_OK )
114 bool HYDROGUI_DataModel::save( QStringList& theFileList )
116 if( !module()->application()->activeStudy() )
119 LightApp_DataModel::save( theFileList );
123 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
124 bool isMultiFile = false;
126 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
128 // save data to temporary files
129 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
130 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
131 aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
133 QString aFullPath = aTmpDir + aFileName;
134 Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
135 if( res != DocError_OK )
137 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
141 theFileList.append( aTmpDir );
142 theFileList.append( aFileName );
147 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
149 QStringList& theFileList )
152 return save( theFileList );
155 bool HYDROGUI_DataModel::close()
160 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
163 QStringList& theListOfFiles )
165 LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
167 int aStudyId = theStudy->id();
169 LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
170 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
171 if ( aDocument.IsNull() || !aStudy )
174 QString aFileToExport = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
175 aFileToExport += QString( QDir::separator() ) + "HYDRO.py";
177 bool aRes = aDocument->DumpToPython( aFileToExport );
181 theListOfFiles.append( aFileToExport );
187 bool HYDROGUI_DataModel::isModified() const
189 return getDocument()->IsModified();
192 bool HYDROGUI_DataModel::isSaved() const
197 void HYDROGUI_DataModel::update( const int theStudyId )
199 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
203 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
207 // create root object if not exist
208 CAM_DataObject* aRootObj = root();
210 aRootObj = createRootModuleObject( aStudyRoot );
215 DataObjectList aList;
216 aRootObj->children( aList );
217 QListIterator<SUIT_DataObject*> anIter( aList );
218 while( anIter.hasNext() )
219 removeChild( aRootObj, anIter.next() );
221 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
222 if( aDocument.IsNull() )
225 LightApp_DataObject* anImageRootObj = createObject( aRootObj, partitionName( KIND_IMAGE ) );
227 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
228 for( ; anIterator.More(); anIterator.Next() )
230 Handle(HYDROData_Image) anImageObj =
231 Handle(HYDROData_Image)::DownCast( anIterator.Current() );
232 if( !anImageObj.IsNull() )
233 createObject( anImageRootObj, anImageObj );
236 LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, partitionName( KIND_BATHYMETRY ) );
238 anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY );
239 for( ; anIterator.More(); anIterator.Next() )
241 Handle(HYDROData_Bathymetry) aBathymetryObj =
242 Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() );
243 if( !aBathymetryObj.IsNull() )
244 createObject( aBathymetryRootObj, aBathymetryObj );
247 LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, partitionName( KIND_POLYLINE ) );
249 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
250 for( ; anIterator.More(); anIterator.Next() )
252 Handle(HYDROData_Polyline) aPolylineObj =
253 Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
254 if( !aPolylineObj.IsNull() )
255 createObject( aPolylineRootObj, aPolylineObj );
258 LightApp_DataObject* aZonesRootObj = createObject( aRootObj, partitionName( KIND_IMMERSIBLE_ZONE ) );
260 anIterator = HYDROData_Iterator( aDocument, KIND_IMMERSIBLE_ZONE );
261 for( ; anIterator.More(); anIterator.Next() )
263 Handle(HYDROData_ImmersibleZone) aZoneObj =
264 Handle(HYDROData_ImmersibleZone)::DownCast( anIterator.Current() );
265 if( !aZoneObj.IsNull() )
266 createObject( aZonesRootObj, aZoneObj );
269 LightApp_DataObject* aCalculRootObj = createObject( aRootObj, partitionName( KIND_CALCULATION ) );
271 anIterator = HYDROData_Iterator( aDocument, KIND_CALCULATION );
272 for( ; anIterator.More(); anIterator.Next() )
274 Handle(HYDROData_CalculationCase) aCalculObj =
275 Handle(HYDROData_CalculationCase)::DownCast( anIterator.Current() );
276 if( !aCalculObj.IsNull() )
277 createObject( aCalculRootObj, aCalculObj );
280 LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, partitionName( KIND_VISUAL_STATE ) );
282 anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
283 for( ; anIterator.More(); anIterator.Next() )
285 Handle(HYDROData_VisualState) aVisualStateObj =
286 Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
287 if( !aVisualStateObj.IsNull() )
288 createObject( aVisualStateRootObj, aVisualStateObj );
291 if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
293 anObjectBrowser->setAutoOpenLevel( 3 );
294 anObjectBrowser->openLevels();
298 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject )
300 return NULL; // to do if necessary
303 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
305 return NULL; // to do if necessary
308 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
310 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
311 return anApp ? anApp->findObject( theEntry ) : 0;
314 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
315 LightApp_Study* theStudy )
318 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
320 update( theStudy->id() );
323 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
325 CAM_DataObject* aRootObj = createModuleObject( theParent );
330 void HYDROGUI_DataModel::updateModel()
332 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
334 update( aModule->getStudyId() );
337 Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
338 const ObjectKind theObjectKind )
340 QString anEntry = theEntry;
341 if( anEntry.indexOf( "_" ) != -1 ) // reference object
342 anEntry = anEntry.section( "_", -1 );
344 Handle(HYDROData_Document) aDocument = getDocument();
345 if( !aDocument.IsNull() )
347 HYDROData_Iterator anIterator( aDocument, theObjectKind );
348 for( ; anIterator.More(); anIterator.Next() )
350 Handle(HYDROData_Entity) anObject = anIterator.Current();
351 if( !anObject.IsNull() )
353 QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
354 if( anEntryRef == anEntry )
362 bool HYDROGUI_DataModel::canUndo() const
364 return getDocument()->CanUndo();
367 bool HYDROGUI_DataModel::canRedo() const
369 return getDocument()->CanRedo();
372 QStringList HYDROGUI_DataModel::undoNames() const
375 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
376 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
380 QStringList HYDROGUI_DataModel::redoNames() const
383 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
384 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
388 void HYDROGUI_DataModel::clearUndos()
390 getDocument()->ClearUndos();
393 void HYDROGUI_DataModel::clearRedos()
395 getDocument()->ClearRedos();
398 bool HYDROGUI_DataModel::undo()
402 getDocument()->Undo();
404 catch ( Standard_Failure )
411 bool HYDROGUI_DataModel::redo()
415 getDocument()->Redo();
417 catch ( Standard_Failure )
424 bool HYDROGUI_DataModel::canCopy()
426 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
427 if( aSeq.Length() != 1 )
430 Handle(HYDROData_Entity) anObject = aSeq.First();
431 if( anObject.IsNull() )
434 ObjectKind aKind = anObject->GetKind();
435 if( aKind == KIND_IMAGE ||
436 aKind == KIND_POLYLINE ||
437 aKind == KIND_CALCULATION )
443 bool HYDROGUI_DataModel::canPaste()
445 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
447 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
448 if( !anObject.IsNull() && !anObject->IsRemoved() )
454 bool HYDROGUI_DataModel::copy()
456 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
457 changeCopyingObjects( aSeq );
461 bool HYDROGUI_DataModel::paste()
463 bool anIsChanged = false;
464 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
466 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
467 if( !anObject.IsNull() && !anObject->IsRemoved() )
469 ObjectKind aKind = anObject->GetKind();
470 Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
471 if( !aClone.IsNull() )
473 anObject->CopyTo( aClone );
476 // generate a new unique name for the clone object:
477 // case 1: Image_1 -> Image_2
478 // case 2: ImageObj -> ImageObj_1
479 QString aName = aClone->GetName();
480 QString aPrefix = aName;
481 if( aName.contains( '_' ) ) // case 1
483 QString aSuffix = aName.section( '_', -1 );
484 bool anIsInteger = false;
485 aSuffix.toInt( &anIsInteger );
487 aPrefix = aName.section( '_', 0, -2 );
491 aName = HYDROGUI_Tool::GenerateObjectName( (HYDROGUI_Module*)module(), aPrefix );
492 aClone->SetName( aName );
499 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
501 myCopyingObjects.Assign( theSeq );
504 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
506 switch( theObjectKind )
508 case KIND_IMAGE: return "IMAGES";
509 case KIND_POLYLINE: return "POLYLINES";
510 case KIND_VISUAL_STATE: return "VISUAL_STATES";
511 case KIND_BATHYMETRY: return "BATHYMETRIES";
512 case KIND_CALCULATION: return "CALCULATION_CASES";
513 case KIND_IMMERSIBLE_ZONE: return "IMMERSIBLE_ZONES";
519 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
521 int aStudyId = module()->application()->activeStudy()->id();
522 return HYDROData_Document::Document( aStudyId );
525 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
526 Handle(HYDROData_Entity) theModelObject,
527 const QString& theParentEntry,
528 const bool theIsBuildTree )
530 HYDROGUI_DataObject* aResObj =
531 new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
533 if ( theIsBuildTree )
535 buildObjectTree( theParent, aResObj, theParentEntry );
541 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
542 const QString& theName,
543 const QString& theParentEntry )
545 return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
548 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
549 SUIT_DataObject* theObject,
550 const QString& theParentEntry )
552 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
556 Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
557 if ( aDataObj.IsNull() )
560 ObjectKind anObjectKind = aDataObj->GetKind();
562 if ( anObjectKind == KIND_IMAGE )
564 Handle(HYDROData_Image) anImageObj =
565 Handle(HYDROData_Image)::DownCast( aDataObj );
566 for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
568 Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
569 if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
570 createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
573 else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
575 Handle(HYDROData_ImmersibleZone) aZoneObj =
576 Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
578 LightApp_DataObject* aPolylineSect =
579 createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
581 Handle(HYDROData_Polyline) aPolyline = aZoneObj->GetPolyline();
582 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
583 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
585 LightApp_DataObject* aBathSect =
586 createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
588 Handle(HYDROData_Bathymetry) aBathymetry = aZoneObj->GetBathymetry();
589 if ( !aBathymetry.IsNull() && !aBathymetry->IsRemoved() )
590 createObject( aBathSect, aBathymetry, aGuiObj->entry(), false );
592 else if ( anObjectKind == KIND_CALCULATION )
594 Handle(HYDROData_CalculationCase) aCaseObj =
595 Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
597 LightApp_DataObject* aCaseRegionsSect =
598 createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
600 HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions();
601 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
602 for ( ; anIter.More(); anIter.Next() )
604 Handle(HYDROData_Region) aCaseRegion =
605 Handle(HYDROData_Region)::DownCast( anIter.Value() );
606 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
607 createObject( aCaseRegionsSect, aCaseRegion, "", true );
610 else if ( anObjectKind == KIND_REGION )
612 Handle(HYDROData_Region) aRegionObj =
613 Handle(HYDROData_Region)::DownCast( aDataObj );
615 HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
616 HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
617 for ( ; anIter.More(); anIter.Next() )
619 Handle(HYDROData_Zone) aRegionZone =
620 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
621 if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
622 createObject( aGuiObj, aRegionZone, "", true );
627 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
628 SUIT_DataObject* theChild )
630 SUIT_DataObject* aSubChild = theChild->firstChild();
631 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
632 removeChild( theChild, aSubChild );
633 theParent->removeChild( theChild );
636 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
637 const QString& theName )
639 SUIT_DataObject* aChild = theFather->firstChild();
642 if( aChild->name() == theName )
643 return aChild; // found
644 aChild = aChild->nextBrother();
646 return NULL; // not found