1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_DataModel.h"
21 #include "HYDROGUI_DataObject.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool.h"
24 #include "HYDROGUI_Zone.h"
25 #include "HYDROGUI_Region.h"
27 #include <HYDROData_Bathymetry.h>
28 #include <HYDROData_CalculationCase.h>
29 #include <HYDROGUI_DataModelSync.h>
30 #include <HYDROData_Document.h>
31 #include <HYDROData_DummyObject3D.h>
32 #include <HYDROData_Image.h>
33 #include <HYDROData_ImmersibleZone.h>
34 #include <HYDROData_Iterator.h>
35 #include <HYDROData_Polyline3D.h>
36 #include <HYDROData_PolylineXY.h>
37 #include <HYDROData_Profile.h>
38 #include <HYDROData_VisualState.h>
39 #include <HYDROData_Region.h>
40 #include <HYDROData_Zone.h>
41 #include <HYDROData_Obstacle.h>
42 #include <HYDROData_Channel.h>
43 #include <HYDROData_Digue.h>
44 #include <HYDROData_River.h>
45 #include <HYDROData_Stream.h>
46 #include <HYDROData_StricklerTable.h>
47 #include <HYDROData_LandCover.h>
49 #include <CAM_Module.h>
50 #include <CAM_Study.h>
52 #include <LightApp_Application.h>
53 #include <LightApp_DataObject.h>
54 #include <LightApp_Study.h>
56 #include <SUIT_DataBrowser.h>
57 #include <SUIT_ResourceMgr.h>
58 #include <SUIT_Study.h>
59 #include <SUIT_Tools.h>
60 #include <SUIT_TreeSync.h>
62 #include <HYDROData_Document.h>
64 #include <TDF_Delta.hxx>
65 #include <TDF_ListIteratorOfDeltaList.hxx>
67 #include <QApplication>
70 // #define DEB_GROUPS 1
72 #include <HYDROData_ShapesGroup.h>
75 static HYDROData_SequenceOfObjects myCopyingObjects;
77 const int ENTRY_COLUMN = 2;
80 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
81 : LightApp_DataModel( theModule )
83 update( module()->application()->activeStudy()->id() );
86 HYDROGUI_DataModel::~HYDROGUI_DataModel()
90 bool HYDROGUI_DataModel::create( CAM_Study* theStudy )
92 bool status = LightApp_DataModel::create( theStudy );
98 bool HYDROGUI_DataModel::open( const QString& theURL,
100 QStringList theFileList )
102 LightApp_DataModel::open( theURL, theStudy, theFileList );
103 const int aStudyId = theStudy->id();
105 Data_DocError res = DocError_UnknownProblem;
106 if( theFileList.count() >= 2 )
108 QString aTmpDir = theFileList[0];
109 QString aDataFileName = theFileList[1];
110 QString aStatesFileName = theFileList.count() == 3 ? theFileList[2] : "";
113 QString aDataFullPath = SUIT_Tools::addSlash( aTmpDir ) + aDataFileName;
114 QString aStatesFullPath = aStatesFileName.isEmpty() ? "" : SUIT_Tools::addSlash( aTmpDir ) + aStatesFileName;
118 res = HYDROData_Document::Load( (char*)aDataFullPath.toLatin1().constData(), aStudyId );
122 res = DocError_UnknownProblem;
125 if ( res != DocError_OK )
127 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
131 if ( !aStatesFullPath.isEmpty() )
133 QFile aFile( aStatesFullPath );
134 if( aFile.open( QFile::ReadOnly ) )
136 myStates = aFile.readAll();
144 // if the document open was successful, the data model update happens
145 // in the set mode of the module
146 if ( res == DocError_OK )
152 bool HYDROGUI_DataModel::save( QStringList& theFileList )
154 if( !module()->application()->activeStudy() )
157 LightApp_DataModel::save( theFileList );
160 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
161 bool isMultiFile = false;
163 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
165 // save module data to temporary files
166 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
167 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
169 // save OCAF data to a temporary file
170 QString aDataFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
171 QString aDataFullPath = aTmpDir + aDataFileName;
172 Data_DocError res = getDocument()->Save( (char*)aDataFullPath.toLatin1().constData() );
173 if( res != DocError_OK )
175 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
179 // save tree state data to a temporary file
180 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
181 QByteArray aStatesData = anApp->objectBrowser()->getOpenStates( ENTRY_COLUMN );
182 QString aStatesFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO_tree_states.txt";
183 QString aStatesFullPath = aTmpDir + aStatesFileName;
184 QFile aFile( aStatesFullPath );
185 if( aFile.open( QFile::WriteOnly ) )
187 aFile.write( aStatesData );
191 // add temporary files to the list
192 theFileList.append( aTmpDir );
193 theFileList.append( aDataFileName );
194 theFileList.append( aStatesFileName );
199 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
201 QStringList& theFileList )
204 return save( theFileList );
207 bool HYDROGUI_DataModel::close()
209 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
211 aModule->clearCache();
215 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
218 QStringList& theListOfFiles )
220 LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
222 int aStudyId = theStudy->id();
224 LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
225 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
226 if ( aDocument.IsNull() || !aStudy )
229 QString aDir = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
230 QString aFileToExport = aDir + QString( QDir::separator() ) + "HYDRO.py";
232 bool aRes = aDocument->DumpToPython( aFileToExport, isMultiFile );
235 theListOfFiles.append( aDir );
236 theListOfFiles.append( aFileToExport );
242 bool HYDROGUI_DataModel::isModified() const
244 return getDocument()->IsModified();
247 bool HYDROGUI_DataModel::isSaved() const
252 void HYDROGUI_DataModel::update( const int theStudyId )
254 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
258 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
262 // create a new root object
263 CAM_DataObject* aNewRootObj = new CAM_DataObject();
265 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
266 if( aDocument.IsNull() )
269 // Create root objects:
272 LightApp_DataObject* anImageRootObj = createObject( aNewRootObj, tr( partitionName( KIND_IMAGE ).toAscii() ) );
275 LightApp_DataObject* aBathymetryRootObj = createObject( aNewRootObj, tr( partitionName( KIND_BATHYMETRY ).toAscii() ) );
277 // ARTIFICIAL OBJECTS
278 LightApp_DataObject* anArtificialObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toAscii() ) );
281 LightApp_DataObject* aNaturalObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_NATURAL_OBJECT ).toAscii() ) );
284 LightApp_DataObject* anObstaclesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_OBSTACLE ).toAscii() ) );
287 LightApp_DataObject* aStricklerTablesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_STRICKLER_TABLE ).toAscii() ) );
290 LightApp_DataObject* aLandCoversRootObj = createObject( aNewRootObj, tr( partitionName( KIND_LAND_COVER ).toAscii() ) );
293 LightApp_DataObject* aCalculRootObj = createObject( aNewRootObj, tr( partitionName( KIND_CALCULATION ).toAscii() ) );
296 LightApp_DataObject* aPolylineRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINEXY ).toAscii() ) );
299 LightApp_DataObject* aPolyline3DRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINE ).toAscii() ) );
302 LightApp_DataObject* aProfileRootObj = createObject( aNewRootObj, tr( partitionName( KIND_PROFILE ).toAscii() ) );
305 LightApp_DataObject* aVisualStateRootObj = createObject( aNewRootObj, tr( partitionName( KIND_VISUAL_STATE ).toAscii() ) );
307 int aNoStricklerTableObj = 0;
308 HYDROData_Iterator anIterator( aDocument, KIND_UNKNOWN );
309 for( ; anIterator.More(); anIterator.Next() ) {
310 LightApp_DataObject* obj = 0;
311 Handle(HYDROData_Entity) anObj = anIterator.Current();
313 if ( !anObj.IsNull() )
315 switch ( anObj->GetKind() ) {
318 Handle(HYDROData_Image) anImageObj =
319 Handle(HYDROData_Image)::DownCast( anObj );
320 if( !anImageObj.IsNull() ) {
321 obj = createObject( anImageRootObj, anImageObj );
326 case KIND_BATHYMETRY:
328 Handle(HYDROData_Bathymetry) aBathymetryObj =
329 Handle(HYDROData_Bathymetry)::DownCast( anObj );
330 if( !aBathymetryObj.IsNull() ) {
331 obj = createObject( aBathymetryRootObj, aBathymetryObj );
338 Handle(HYDROData_Channel) aChannelObj =
339 Handle(HYDROData_Channel)::DownCast( anObj );
340 if( !aChannelObj.IsNull() ) {
341 obj = createObject( anArtificialObjectsRootObj, aChannelObj );
348 Handle(HYDROData_Digue) aDigueObj =
349 Handle(HYDROData_Digue)::DownCast( anObj );
350 if( !aDigueObj.IsNull() ) {
351 obj = createObject( anArtificialObjectsRootObj, aDigueObj );
356 case KIND_IMMERSIBLE_ZONE:
358 Handle(HYDROData_ImmersibleZone) anImmersibleZoneObj =
359 Handle(HYDROData_ImmersibleZone)::DownCast( anObj );
360 if( !anImmersibleZoneObj.IsNull() ) {
361 obj = createObject( aNaturalObjectsRootObj, anImmersibleZoneObj );
368 Handle(HYDROData_River) aRiverObj =
369 Handle(HYDROData_River)::DownCast( anObj );
370 if( !aRiverObj.IsNull() ) {
371 obj = createObject( aNaturalObjectsRootObj, aRiverObj );
378 Handle(HYDROData_Stream) aStreamObj =
379 Handle(HYDROData_Stream)::DownCast( anObj );
380 if( !aStreamObj.IsNull() ) {
381 obj = createObject( aNaturalObjectsRootObj, aStreamObj );
388 Handle(HYDROData_Obstacle) anObstacleObj =
389 Handle(HYDROData_Obstacle)::DownCast( anObj );
390 if( !anObstacleObj.IsNull() ) {
391 obj = createObject( anObstaclesRootObj, anObstacleObj );
396 case KIND_STRICKLER_TABLE:
398 Handle(HYDROData_StricklerTable) aStricklerTableObj =
399 Handle(HYDROData_StricklerTable)::DownCast( anObj );
400 if( !aStricklerTableObj.IsNull() ) {
401 obj = createObject( aStricklerTablesRootObj, aStricklerTableObj );
403 aNoStricklerTableObj++;
407 case KIND_LAND_COVER:
409 Handle(HYDROData_LandCover) aLandCoverObj =
410 Handle(HYDROData_LandCover)::DownCast( anObj );
411 if( !aLandCoverObj.IsNull() ) {
412 obj = createObject( aLandCoversRootObj, aLandCoverObj );
417 case KIND_CALCULATION:
419 Handle(HYDROData_CalculationCase) aCalculObj =
420 Handle(HYDROData_CalculationCase)::DownCast( anObj );
421 if( !aCalculObj.IsNull() ) {
422 obj = createObject( aCalculRootObj, aCalculObj );
427 case KIND_POLYLINEXY:
429 Handle(HYDROData_PolylineXY) aPolylineObj =
430 Handle(HYDROData_PolylineXY)::DownCast( anObj );
431 if( !aPolylineObj.IsNull() ) {
432 obj = createObject( aPolylineRootObj, aPolylineObj );
439 Handle(HYDROData_Polyline3D) aPolylineObj =
440 Handle(HYDROData_Polyline3D)::DownCast( anObj );
441 if( !aPolylineObj.IsNull() ) {
442 obj = createObject( aPolyline3DRootObj, aPolylineObj );
449 Handle(HYDROData_Profile) aProfileObj =
450 Handle(HYDROData_Profile)::DownCast( anObj );
451 if( !aProfileObj.IsNull() ) {
452 obj = createObject( aProfileRootObj, aProfileObj );
457 case KIND_VISUAL_STATE:
459 Handle(HYDROData_VisualState) aVisualStateObj =
460 Handle(HYDROData_VisualState)::DownCast( anObj );
461 if( !aVisualStateObj.IsNull() ) {
462 obj = createObject( aVisualStateRootObj, aVisualStateObj );
471 // Create default Strickler table object
472 if ( aNoStricklerTableObj == 0 )
473 createDefaultStricklerTable( aDocument, aStricklerTablesRootObj );
475 //if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
477 // anObjectBrowser->setAutoOpenLevel( 3 );
478 // anObjectBrowser->openLevels();
481 HYDROGUI_DataModelSync aSync( aNewRootObj );
482 SUIT_DataObject* aRoot = root();
483 bool isNewDoc = aRoot==0;
485 aRoot = createRootModuleObject( aStudyRoot );
486 ::synchronize < suitPtr, suitPtr, HYDROGUI_DataModelSync >
487 ( aNewRootObj, aRoot, aSync );
489 SUIT_DataBrowser* ob = anApp->objectBrowser();
491 if ( !myStates.isEmpty() )
494 ob->setOpenStates( myStates, ENTRY_COLUMN );
499 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject )
501 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>(
502 findObject( HYDROGUI_DataObject::dataObjectEntry( theModelObject ) ) );
506 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
508 return NULL; // to do if necessary
511 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
513 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
514 return anApp ? anApp->findObject( theEntry ) : 0;
517 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
518 LightApp_Study* theStudy )
521 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
523 update( theStudy->id() );
526 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
528 CAM_ModuleObject* aRootObj = createModuleObject( theParent );
529 aRootObj->setDataModel( this );
534 void HYDROGUI_DataModel::updateModel()
536 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
538 update( aModule->getStudyId() );
541 Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
542 const ObjectKind theObjectKind )
544 QString anEntry = theEntry;
545 if( anEntry.indexOf( "_" ) != -1 ) // reference object
546 anEntry = anEntry.section( "_", -1 );
548 Handle(HYDROData_Document) aDocument = getDocument();
549 if( !aDocument.IsNull() )
551 HYDROData_Iterator anIterator( aDocument, theObjectKind );
552 for( ; anIterator.More(); anIterator.Next() )
554 Handle(HYDROData_Entity) anObject = anIterator.Current();
555 if( !anObject.IsNull() )
557 QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
558 if( anEntryRef == anEntry )
566 bool HYDROGUI_DataModel::canUndo() const
568 return getDocument()->CanUndo();
571 bool HYDROGUI_DataModel::canRedo() const
573 return getDocument()->CanRedo();
576 QStringList HYDROGUI_DataModel::undoNames() const
579 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
580 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
584 QStringList HYDROGUI_DataModel::redoNames() const
587 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
588 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
592 void HYDROGUI_DataModel::clearUndos()
594 getDocument()->ClearUndos();
597 void HYDROGUI_DataModel::clearRedos()
599 getDocument()->ClearRedos();
602 bool HYDROGUI_DataModel::undo()
606 getDocument()->Undo();
608 catch ( Standard_Failure )
615 bool HYDROGUI_DataModel::redo()
619 getDocument()->Redo();
621 catch ( Standard_Failure )
628 bool HYDROGUI_DataModel::canCopy()
630 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
631 bool isCanCopy = !aSeq.IsEmpty();
633 for ( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
634 Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
635 if( !anObject.IsNull() ) {
636 ObjectKind aKind = anObject->GetKind();
637 bool isUnrecognized = aKind <= KIND_UNKNOWN || aKind > KIND_LAST;
638 bool isChildObject = aKind == KIND_DUMMY_3D ||
639 aKind == KIND_ZONE ||
640 aKind == KIND_SHAPES_GROUP ||
641 aKind == KIND_SPLITTED_GROUP;
642 if ( isUnrecognized || isChildObject ) {
652 bool HYDROGUI_DataModel::canPaste()
654 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
656 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
657 if( !anObject.IsNull() && !anObject->IsRemoved() )
663 bool HYDROGUI_DataModel::copy()
665 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
666 changeCopyingObjects( aSeq );
670 bool HYDROGUI_DataModel::paste()
672 bool anIsChanged = false;
673 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
675 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
676 if( !anObject.IsNull() && !anObject->IsRemoved() )
678 ObjectKind aKind = anObject->GetKind();
679 Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
680 if( !aClone.IsNull() )
682 anObject->CopyTo( aClone );
686 aClone->RemoveZLevel();
693 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
695 myCopyingObjects.Assign( theSeq );
698 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
700 switch( theObjectKind )
702 case KIND_IMAGE: return "IMAGES";
703 case KIND_POLYLINE: return "POLYLINES_3D";
704 case KIND_POLYLINEXY: return "POLYLINES";
705 case KIND_PROFILE: return "PROFILES";
706 case KIND_VISUAL_STATE: return "VISUAL_STATES";
707 case KIND_BATHYMETRY: return "BATHYMETRIES";
708 case KIND_CALCULATION: return "CALCULATION_CASES";
709 case KIND_OBSTACLE: return "OBSTACLES";
710 case KIND_ARTIFICIAL_OBJECT: return "ARTIFICIAL_OBJECTS";
711 case KIND_NATURAL_OBJECT: return "NATURAL_OBJECTS";
712 case KIND_STRICKLER_TABLE: return "STRICKLER_TABLES";
713 case KIND_LAND_COVER: return "LAND_COVERS";
719 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
721 int aStudyId = module()->application()->activeStudy()->id();
722 return HYDROData_Document::Document( aStudyId );
725 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
726 Handle(HYDROData_Entity) theModelObject,
727 const QString& theParentEntry,
728 const bool theIsBuildTree )
730 HYDROGUI_DataObject* aResObj = new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
732 const ObjectKind aKind = theModelObject->GetKind();
733 bool visibility = aKind == KIND_IMAGE || aKind == KIND_POLYLINEXY || aKind == KIND_POLYLINE ||
734 aKind == KIND_SHAPES_GROUP || aKind == KIND_SPLITTED_GROUP || aKind == KIND_ZONE ||
735 aKind == KIND_IMMERSIBLE_ZONE || aKind == KIND_REGION || aKind == KIND_BATHYMETRY ||
736 aKind == KIND_OBSTACLE || aKind == KIND_STREAM || aKind == KIND_CHANNEL ||
737 aKind == KIND_DIGUE || aKind == KIND_DUMMY_3D || aKind == KIND_LAND_COVER;
740 Handle(HYDROData_Profile) aProfObj = Handle(HYDROData_Profile)::DownCast( theModelObject );
741 visibility = !aProfObj.IsNull() && aProfObj->IsValid();
744 if ( aKind == KIND_REGION )
746 QString an = aResObj->name();
752 SUIT_AbstractModel* treeModel = 0;
753 LightApp_Application* app = dynamic_cast<LightApp_Application*>( module()->application() );
755 treeModel = dynamic_cast<SUIT_AbstractModel*>( app->objectBrowser()->model() );
759 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
760 bool isVisible = aModule->isObjectVisible( -1, theModelObject );
761 Qtx::VisibilityState aVisState = isVisible ? Qtx::ShownState : Qtx::HiddenState;
762 treeModel->setVisibilityState( aResObj->text( aResObj->customData( Qtx::IdType ).toInt() ), aVisState, false );
766 if ( theIsBuildTree )
768 buildObjectTree( theParent, aResObj, theParentEntry );
774 LightApp_DataObject* HYDROGUI_DataModel::buildObject( SUIT_DataObject* theParent,
775 HYDROGUI_DataObject* theObject,
776 const QString& theParentEntry,
777 const bool theIsBuildTree,
778 const bool theIsInOperation )
780 if ( theIsBuildTree )
782 buildObjectTree( theParent, theObject, theParentEntry, theIsInOperation );
787 LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject* theParent,
788 Handle(HYDROData_Zone) theModelObject,
789 const QString& theParentEntry,
790 const bool theIsBuildTree,
791 const bool theIsInOperation )
793 return buildObject( theParent, new HYDROGUI_Zone( theParent, theModelObject, theParentEntry, theIsInOperation ),
794 theParentEntry, theIsBuildTree, theIsInOperation );
797 LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject* theParent,
798 Handle(HYDROData_Region) theModelObject,
799 const QString& theParentEntry,
800 const bool theIsBuildTree,
801 const bool theIsInOperation )
803 return buildObject( theParent, new HYDROGUI_Region( theParent, theModelObject, theParentEntry, theIsInOperation ),
804 theParentEntry, theIsBuildTree, theIsInOperation );
807 void HYDROGUI_DataModel::createDefaultStricklerTable( const Handle(HYDROData_Document)& theDocument,
808 LightApp_DataObject* theParent )
810 // Create default Strickler table object
811 Handle(HYDROData_StricklerTable) aStricklerTableObj =
812 Handle(HYDROData_StricklerTable)::DownCast( theDocument->CreateObject(KIND_STRICKLER_TABLE) );
813 if ( !aStricklerTableObj.IsNull() )
815 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
816 QString defTablePath = resMgr->path( "resources", module()->name(), tr( "DEFAULT_STRICKLER_TABLE_FILE" ) );
817 aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( defTablePath ) );
819 QString aStricklerTableName;
820 if ( aStricklerTableObj->GetName().isEmpty() )
822 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
824 aStricklerTableName = HYDROGUI_Tool::GenerateObjectName( aModule, tr( "DEFAULT_STRICKLER_TABLE_NAME" ) );
826 if ( aStricklerTableObj->GetName() != aStricklerTableName )
827 aStricklerTableObj->SetName( aStricklerTableName );
829 aStricklerTableObj->Update();
831 LightApp_DataObject* obj = createObject( theParent, aStricklerTableObj );
835 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
836 const QString& theName,
837 const QString& theParentEntry )
839 return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
842 void HYDROGUI_DataModel::buildObjectPartition( SUIT_DataObject* theObject,
843 const HYDROData_SequenceOfObjects& theObjects,
844 const QString& thePartName,
845 const bool theIsCreateEmpty )
847 if ( theObjects.IsEmpty() && !theIsCreateEmpty )
850 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
854 LightApp_DataObject* aPartSect =
855 createObject( aGuiObj, thePartName, aGuiObj->entry() );
857 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
858 for ( ; anIter.More(); anIter.Next() )
860 Handle(HYDROData_Entity) anObj = anIter.Value();
861 if( !anObj.IsNull() && !anObj->IsRemoved() )
862 createObject( aPartSect, anObj, aGuiObj->entry(), false );
866 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
867 SUIT_DataObject* theObject,
868 const QString& theParentEntry,
869 const bool theIsInOperation )
871 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
875 Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
876 if ( aDataObj.IsNull() )
879 if ( aDataObj->IsKind( STANDARD_TYPE(HYDROData_Object) ) )
881 Handle(HYDROData_Object) aGeomObj =
882 Handle(HYDROData_Object)::DownCast( aDataObj );
884 Handle(HYDROData_DummyObject3D) anObject3D = aGeomObj->GetObject3D();
885 if ( !anObject3D.IsNull() )
886 createObject( aGuiObj, anObject3D, "", false );
889 HYDROData_SequenceOfObjects anObjGroups = aGeomObj->GetGroups();
890 buildObjectPartition( aGuiObj, anObjGroups, tr( "OBJECT_GROUPS" ), false );
894 ObjectKind anObjectKind = aDataObj->GetKind();
896 if ( anObjectKind == KIND_IMAGE )
898 Handle(HYDROData_Image) anImageObj =
899 Handle(HYDROData_Image)::DownCast( aDataObj );
900 for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
902 Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
903 if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
904 createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
907 else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
909 Handle(HYDROData_ImmersibleZone) aZoneObj =
910 Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
912 LightApp_DataObject* aPolylineSect =
913 createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
915 Handle(HYDROData_PolylineXY) aPolyline = aZoneObj->GetPolyline();
916 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
917 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
919 LightApp_DataObject* aBathSect =
920 createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
922 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aZoneObj->GetAltitudeObject();
923 if ( !anAltitudeObj.IsNull() && !anAltitudeObj->IsRemoved() )
924 createObject( aBathSect, anAltitudeObj, aGuiObj->entry(), false );
926 else if ( anObjectKind == KIND_POLYLINE )
928 Handle(HYDROData_Polyline3D) aPolyline3D =
929 Handle(HYDROData_Polyline3D)::DownCast( aDataObj );
931 LightApp_DataObject* aPolylineSect =
932 createObject( aGuiObj, tr( "POLYLINE3D_POLYLINE" ), aGuiObj->entry() );
934 Handle(HYDROData_PolylineXY) aPolylineXY = aPolyline3D->GetPolylineXY();
935 if ( !aPolylineXY.IsNull() && !aPolylineXY->IsRemoved() )
936 createObject( aPolylineSect, aPolylineXY, aGuiObj->entry(), false );
938 LightApp_DataObject* aProfileSect =
939 createObject( aGuiObj, tr( "POLYLINE3D_PROFILE" ), aGuiObj->entry() );
941 Handle(HYDROData_ProfileUZ) aProfileUZ = aPolyline3D->GetProfileUZ();
942 if ( aProfileUZ.IsNull() || aProfileUZ->IsRemoved() )
943 aProfileUZ = aPolyline3D->GetChildProfileUZ( false );
945 if ( !aProfileUZ.IsNull() && !aProfileUZ->IsRemoved() )
947 Handle(HYDROData_Profile) aProfile =
948 Handle(HYDROData_Profile)::DownCast( aProfileUZ->GetFatherObject() );
949 if ( !aProfile.IsNull() && !aProfile->IsRemoved() )
950 createObject( aProfileSect, aProfile, aGuiObj->entry(), false );
953 LightApp_DataObject* aBathSect =
954 createObject( aGuiObj, tr( "POLYLINE3D_BATHYMETRY" ), aGuiObj->entry() );
956 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aPolyline3D->GetAltitudeObject();
957 if ( !anAltitudeObj.IsNull() && !anAltitudeObj->IsRemoved() )
958 createObject( aBathSect, anAltitudeObj, aGuiObj->entry(), false );
960 else if ( anObjectKind == KIND_CALCULATION )
962 Handle(HYDROData_CalculationCase) aCaseObj =
963 Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
965 LightApp_DataObject* aPolylineSect =
966 createObject( aGuiObj, tr( "CASE_BOUNDARY" ), aGuiObj->entry() );
968 Handle(HYDROData_PolylineXY) aPolyline = aCaseObj->GetBoundaryPolyline();
969 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
970 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
972 LightApp_DataObject* aCaseAOSect =
973 createObject( aGuiObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toAscii() ),
975 LightApp_DataObject* aCaseNOSect =
976 createObject( aGuiObj, tr( partitionName( KIND_NATURAL_OBJECT ).toAscii() ),
979 HYDROData_SequenceOfObjects aSeq = aCaseObj->GetGeometryObjects();
980 HYDROData_SequenceOfObjects::Iterator aGOIter( aSeq );
981 Handle(HYDROData_Entity) anEntity;
982 Handle(HYDROData_ArtificialObject) anAObject;
983 Handle(HYDROData_NaturalObject) aNObject;
984 for ( ; aGOIter.More(); aGOIter.Next() )
986 anEntity = aGOIter.Value();
987 if ( anEntity.IsNull() )
989 anAObject = Handle(HYDROData_ArtificialObject)::DownCast( anEntity );
990 if ( !anAObject.IsNull() )
991 createObject( aCaseAOSect, anAObject, aGuiObj->entry(), false );
994 aNObject = Handle(HYDROData_NaturalObject)::DownCast( anEntity );
995 if ( !aNObject.IsNull() )
996 createObject( aCaseNOSect, aNObject, aGuiObj->entry(), false );
999 LightApp_DataObject* aCaseRegionsSect =
1000 createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
1002 HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions( false );
1003 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
1004 for ( ; anIter.More(); anIter.Next() )
1006 Handle(HYDROData_Region) aCaseRegion =
1007 Handle(HYDROData_Region)::DownCast( anIter.Value() );
1008 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
1009 createRegion( aCaseRegionsSect, aCaseRegion, "", true, theIsInOperation );
1012 LightApp_DataObject* aCaseLandCoverRegionsSect =
1013 createObject( aGuiObj, tr( "CASE_LAND_COVER_REGIONS" ), aGuiObj->entry() );
1015 HYDROData_SequenceOfObjects aCaseLandCoverRegions = aCaseObj->GetRegions( true );
1016 anIter.Init( aCaseLandCoverRegions );
1017 for ( ; anIter.More(); anIter.Next() )
1019 Handle(HYDROData_Region) aCaseLCRegion =
1020 Handle(HYDROData_Region)::DownCast( anIter.Value() );
1021 if( !aCaseLCRegion.IsNull() && !aCaseLCRegion->IsRemoved() )
1022 createRegion( aCaseLandCoverRegionsSect, aCaseLCRegion, "", true, theIsInOperation );
1026 HYDROData_SequenceOfObjects aCalcGroups = aCaseObj->GetGeometryGroups();
1027 buildObjectPartition( aGuiObj, aCalcGroups, tr( "OBJECT_GROUPS" ), false );
1029 HYDROData_SequenceOfObjects aCalcSplitGroups = aCaseObj->GetSplittedGroups();
1030 buildObjectPartition( aGuiObj, aCalcSplitGroups, tr( "CASE_SPLITTED_GROUPS" ), false );
1034 else if ( anObjectKind == KIND_REGION )
1036 Handle(HYDROData_Region) aRegionObj =
1037 Handle(HYDROData_Region)::DownCast( aDataObj );
1039 HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
1040 HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
1041 for ( ; anIter.More(); anIter.Next() )
1043 Handle(HYDROData_Zone) aRegionZone =
1044 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1045 if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
1046 createZone( aGuiObj, aRegionZone, "", true, theIsInOperation );
1049 else if ( anObjectKind == KIND_PROFILE )
1051 Handle(HYDROData_Profile) aProfileObj =
1052 Handle(HYDROData_Profile)::DownCast( aDataObj );
1054 aGuiObj->setIsValid( aProfileObj->IsValid() );
1056 else if ( anObjectKind == KIND_CHANNEL || anObjectKind == KIND_DIGUE )
1058 Handle(HYDROData_Channel) aChannelObj =
1059 Handle(HYDROData_Channel)::DownCast( aDataObj );
1061 LightApp_DataObject* aGuideLineSect =
1062 createObject( aGuiObj, tr( "CHANNEL_GUIDE_LINE" ), aGuiObj->entry() );
1063 Handle(HYDROData_Polyline3D) aGuideLine = aChannelObj->GetGuideLine();
1064 if ( !aGuideLine.IsNull() && !aGuideLine->IsRemoved() ) {
1065 createObject( aGuideLineSect, aGuideLine, aGuiObj->entry(), false );
1068 LightApp_DataObject* aProfileSect =
1069 createObject( aGuiObj, tr( "CHANNEL_PROFILE" ), aGuiObj->entry() );
1070 Handle(HYDROData_Profile) aProfile = aChannelObj->GetProfile();
1071 if ( !aProfile.IsNull() && !aProfile->IsRemoved() ) {
1072 createObject( aProfileSect, aProfile, aGuiObj->entry(), false );
1075 else if ( anObjectKind == KIND_STREAM )
1077 Handle(HYDROData_Stream) aStreamObj =
1078 Handle(HYDROData_Stream)::DownCast( aDataObj );
1080 LightApp_DataObject* aHydraulicAxisSect =
1081 createObject( aGuiObj, tr( "STREAM_HYDRAULIC_AXIS" ), aGuiObj->entry() );
1082 Handle(HYDROData_PolylineXY) aHydraulicAxis = aStreamObj->GetHydraulicAxis();
1083 if ( !aHydraulicAxis.IsNull() && !aHydraulicAxis->IsRemoved() ) {
1084 createObject( aHydraulicAxisSect, aHydraulicAxis, aGuiObj->entry(), false );
1087 HYDROData_SequenceOfObjects aProfiles = aStreamObj->GetProfiles();
1088 buildObjectPartition( aGuiObj, aProfiles, tr( "STREAM_PROFILES" ), true );
1090 Handle(HYDROData_Polyline3D) aBottomPolyline = aStreamObj->GetBottomPolyline();
1091 if ( !aBottomPolyline.IsNull() && !aBottomPolyline->IsRemoved() ) {
1092 createObject( aGuiObj, aBottomPolyline, aGuiObj->entry(), false );
1097 void HYDROGUI_DataModel::buildCaseTree( SUIT_DataObject* theParent, Handle(HYDROData_CalculationCase) theCase,
1098 const bool theLandCover )
1100 if ( !theCase.IsNull() )
1104 // Remove previous objects tree
1105 DataObjectList aList;
1106 theParent->children( aList );
1107 QListIterator<SUIT_DataObject*> anIter( aList );
1108 while( anIter.hasNext() )
1109 removeChild( theParent, anIter.next() );
1112 new HYDROGUI_DropTargetObject( theParent, tr( "NEW_REGION" ), "", true );
1114 HYDROData_SequenceOfObjects aCaseRegions = theCase->GetRegions( theLandCover );
1115 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
1116 for ( ; anIter.More(); anIter.Next() )
1118 Handle(HYDROData_Region) aCaseRegion =
1119 Handle(HYDROData_Region)::DownCast( anIter.Value() );
1120 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
1121 createRegion( theParent, aCaseRegion, "", true, true );
1126 void HYDROGUI_DataModel::updateObjectTree( Handle(HYDROData_Entity)& theObj )
1128 if ( !theObj.IsNull() )
1130 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>(
1131 findObject( HYDROGUI_DataObject::dataObjectEntry( theObj ) ) );
1134 // Remove previous objects tree
1135 DataObjectList aList;
1136 aGuiObj->children( aList );
1137 QListIterator<SUIT_DataObject*> anIter( aList );
1138 while( anIter.hasNext() )
1139 removeChild( aGuiObj, anIter.next() );
1141 // Rebuild the subtree
1142 QString aParentEntry;
1143 HYDROGUI_DataObject* aParent = dynamic_cast<HYDROGUI_DataObject*>( aGuiObj->parent() );
1146 aParentEntry = aParent->entry();
1148 buildObjectTree( aParent, aGuiObj, aParentEntry, aGuiObj->isInOperation() );
1152 // workaround for the bug in SalomeApp_Study::findObjectByEntry - it can't find LightApp_DataObjects
1153 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
1156 aModule->getApp()->updateObjectBrowser();
1162 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
1163 SUIT_DataObject* theChild )
1165 SUIT_DataObject* aSubChild = theChild->firstChild();
1166 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
1167 removeChild( theChild, aSubChild );
1168 theParent->removeChild( theChild );
1171 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
1172 const QString& theName )
1174 SUIT_DataObject* aChild = theFather->firstChild();
1177 if( aChild->name() == theName )
1178 return aChild; // found
1179 aChild = aChild->nextBrother();
1181 return NULL; // not found
1184 bool HYDROGUI_DataModel::createNewRegion( Handle(HYDROData_CalculationCase) theCase,
1185 const QList<HYDROGUI_Zone*>& theZonesList,
1186 const bool theLandCover )
1188 bool isOk = !theCase.IsNull();
1191 Handle(HYDROData_Region) aRegion;
1192 Handle(HYDROData_Zone) aZone;
1193 for (int i = 0; i < theZonesList.length(); i++ )
1195 aZone = Handle(HYDROData_Zone)::DownCast( theZonesList.at(i)->modelObject() );
1196 if ( !aZone.IsNull() )
1198 if ( aRegion.IsNull() )
1200 aRegion = theCase->AddNewRegion( aZone, theLandCover );
1201 isOk = !aRegion.IsNull();
1205 if ( !( aRegion->AddZone( aZone ) ) )
1216 bool HYDROGUI_DataModel::rename( Handle(HYDROData_Entity) theEntity, const QString& theName )
1218 if ( theName.isEmpty() )
1223 getDocument()->StartOperation();
1224 theEntity->SetName( theName );
1225 getDocument()->CommitOperation( HYDROGUI_Tool::ToExtString( tr("RENAME_TO").arg( theName ) ) );
1226 module()->application()->activeStudy()->Modified();
1228 catch ( Standard_Failure )
1230 getDocument()->AbortOperation();
1236 void HYDROGUI_DataModel::updateDocument()
1238 // Sets the default strickler coefficient from preferences to document.
1239 Handle(HYDROData_Document) aDoc = getDocument();
1240 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
1241 if ( resMgr && !aDoc.IsNull() )
1242 aDoc->SetDefaultStricklerCoefficient( resMgr->doubleValue( "preferences", "default_strickler_coefficient", 0 ) );