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_Tool2.h"
25 #include "HYDROGUI_Zone.h"
26 #include "HYDROGUI_Region.h"
28 #include <HYDROData_Bathymetry.h>
29 #include <HYDROData_CalculationCase.h>
30 #include <HYDROGUI_DataModelSync.h>
31 #include <HYDROData_Document.h>
32 #include <HYDROData_DummyObject3D.h>
33 #include <HYDROData_Image.h>
34 #include <HYDROData_ImmersibleZone.h>
35 #include <HYDROData_Iterator.h>
36 #include <HYDROData_Polyline3D.h>
37 #include <HYDROData_PolylineXY.h>
38 #include <HYDROData_Profile.h>
39 #include <HYDROData_VisualState.h>
40 #include <HYDROData_Region.h>
41 #include <HYDROData_Zone.h>
42 #include <HYDROData_Obstacle.h>
43 #include <HYDROData_Channel.h>
44 #include <HYDROData_Digue.h>
45 #include <HYDROData_River.h>
46 #include <HYDROData_Stream.h>
47 #include <HYDROData_StricklerTable.h>
48 #include <HYDROData_LandCoverMap.h>
50 #include <CAM_Module.h>
51 #include <CAM_Study.h>
53 #include <LightApp_Application.h>
54 #include <LightApp_DataObject.h>
55 #include <LightApp_Study.h>
57 #include <SUIT_DataBrowser.h>
58 #include <SUIT_ResourceMgr.h>
59 #include <SUIT_Study.h>
60 #include <SUIT_Tools.h>
61 #include <SUIT_TreeSync.h>
63 #include <HYDROData_Document.h>
65 #include <TDF_Delta.hxx>
66 #include <TDF_ListIteratorOfDeltaList.hxx>
68 #include <QApplication>
71 // #define DEB_GROUPS 1
73 #include <HYDROData_ShapesGroup.h>
76 static HYDROData_SequenceOfObjects myCopyingObjects;
78 const int ENTRY_COLUMN = 2;
81 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
82 : LightApp_DataModel( theModule )
84 update( module()->application()->activeStudy()->id() );
87 HYDROGUI_DataModel::~HYDROGUI_DataModel()
91 bool HYDROGUI_DataModel::create( CAM_Study* theStudy )
93 bool status = LightApp_DataModel::create( theStudy );
99 bool HYDROGUI_DataModel::open( const QString& theURL,
101 QStringList theFileList )
103 LightApp_DataModel::open( theURL, theStudy, theFileList );
104 const int aStudyId = theStudy->id();
106 Data_DocError res = DocError_UnknownProblem;
107 if( theFileList.count() >= 2 )
109 QString aTmpDir = theFileList[0];
110 QString aDataFileName = theFileList[1];
111 QString aStatesFileName = theFileList.count() == 3 ? theFileList[2] : "";
114 QString aDataFullPath = SUIT_Tools::addSlash( aTmpDir ) + aDataFileName;
115 QString aStatesFullPath = aStatesFileName.isEmpty() ? "" : SUIT_Tools::addSlash( aTmpDir ) + aStatesFileName;
119 res = HYDROData_Document::Load( (char*)aDataFullPath.toLatin1().constData(), aStudyId );
123 res = DocError_UnknownProblem;
126 if ( res != DocError_OK )
128 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
132 if ( !aStatesFullPath.isEmpty() )
134 QFile aFile( aStatesFullPath );
135 if( aFile.open( QFile::ReadOnly ) )
137 myStates = aFile.readAll();
145 // if the document open was successful, the data model update happens
146 // in the set mode of the module
147 if ( res == DocError_OK )
153 bool HYDROGUI_DataModel::save( QStringList& theFileList )
155 if( !module()->application()->activeStudy() )
158 LightApp_DataModel::save( theFileList );
161 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
162 bool isMultiFile = false;
164 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
166 // save module data to temporary files
167 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
168 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
170 // save OCAF data to a temporary file
171 QString aDataFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
172 QString aDataFullPath = aTmpDir + aDataFileName;
173 Data_DocError res = getDocument()->Save( (char*)aDataFullPath.toLatin1().constData() );
174 if( res != DocError_OK )
176 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
180 // save tree state data to a temporary file
181 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
182 QByteArray aStatesData = anApp->objectBrowser()->getOpenStates( ENTRY_COLUMN );
183 QString aStatesFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO_tree_states.txt";
184 QString aStatesFullPath = aTmpDir + aStatesFileName;
185 QFile aFile( aStatesFullPath );
186 if( aFile.open( QFile::WriteOnly ) )
188 aFile.write( aStatesData );
192 // add temporary files to the list
193 theFileList.append( aTmpDir );
194 theFileList.append( aDataFileName );
195 theFileList.append( aStatesFileName );
200 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
202 QStringList& theFileList )
205 return save( theFileList );
208 bool HYDROGUI_DataModel::close()
210 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
212 aModule->clearCache();
216 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
219 QStringList& theListOfFiles )
221 LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
223 int aStudyId = theStudy->id();
225 LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
226 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
227 if ( aDocument.IsNull() || !aStudy )
230 QString aDir = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
231 QString aFileToExport = aDir + QString( QDir::separator() ) + "HYDRO.py";
233 bool aRes = aDocument->DumpToPython( aFileToExport, isMultiFile );
236 theListOfFiles.append( aDir );
237 theListOfFiles.append( aFileToExport );
243 bool HYDROGUI_DataModel::isModified() const
245 return getDocument()->IsModified();
248 bool HYDROGUI_DataModel::isSaved() const
253 void HYDROGUI_DataModel::update( const int theStudyId )
255 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
259 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
263 // create a new root object
264 CAM_DataObject* aNewRootObj = new CAM_DataObject();
266 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
267 if( aDocument.IsNull() )
270 // Create root objects:
273 LightApp_DataObject* anImageRootObj = createObject( aNewRootObj, tr( partitionName( KIND_IMAGE ).toLatin1() ) );
276 LightApp_DataObject* aBathymetryRootObj = createObject( aNewRootObj, tr( partitionName( KIND_BATHYMETRY ).toLatin1() ) );
279 LightApp_DataObject* aPolylineRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINEXY ).toLatin1() ) );
282 LightApp_DataObject* aProfileRootObj = createObject( aNewRootObj, tr( partitionName( KIND_PROFILE ).toLatin1() ) );
285 LightApp_DataObject* aPolyline3DRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINE ).toLatin1() ) );
288 LightApp_DataObject* aNaturalObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_NATURAL_OBJECT ).toLatin1() ) );
290 // ARTIFICIAL OBJECTS
291 LightApp_DataObject* anArtificialObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toLatin1() ) );
294 LightApp_DataObject* anObstaclesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_OBSTACLE ).toLatin1() ) );
297 LightApp_DataObject* aStricklerTablesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_STRICKLER_TABLE ).toLatin1() ) );
300 LightApp_DataObject* aLandCoversRootObj = createObject( aNewRootObj, tr( partitionName( KIND_LAND_COVER_MAP ).toLatin1() ) );
303 LightApp_DataObject* aCalculRootObj = createObject( aNewRootObj, tr( partitionName( KIND_CALCULATION ).toLatin1() ) );
306 LightApp_DataObject* aVisualStateRootObj = createObject( aNewRootObj, tr( partitionName( KIND_VISUAL_STATE ).toLatin1() ) );
308 int aNoStricklerTableObj = 0;
309 HYDROData_Iterator anIterator( aDocument, KIND_UNKNOWN );
310 for( ; anIterator.More(); anIterator.Next() ) {
311 LightApp_DataObject* obj = 0;
312 Handle(HYDROData_Entity) anObj = anIterator.Current();
314 if ( !anObj.IsNull() )
316 switch ( anObj->GetKind() ) {
319 Handle(HYDROData_Image) anImageObj =
320 Handle(HYDROData_Image)::DownCast( anObj );
321 if( !anImageObj.IsNull() ) {
322 obj = createObject( anImageRootObj, anImageObj );
327 case KIND_BATHYMETRY:
329 Handle(HYDROData_Bathymetry) aBathymetryObj =
330 Handle(HYDROData_Bathymetry)::DownCast( anObj );
331 if( !aBathymetryObj.IsNull() ) {
332 obj = createObject( aBathymetryRootObj, aBathymetryObj );
339 Handle(HYDROData_Channel) aChannelObj =
340 Handle(HYDROData_Channel)::DownCast( anObj );
341 if( !aChannelObj.IsNull() ) {
342 obj = createObject( anArtificialObjectsRootObj, aChannelObj );
349 Handle(HYDROData_Digue) aDigueObj =
350 Handle(HYDROData_Digue)::DownCast( anObj );
351 if( !aDigueObj.IsNull() ) {
352 obj = createObject( anArtificialObjectsRootObj, aDigueObj );
357 case KIND_IMMERSIBLE_ZONE:
359 Handle(HYDROData_ImmersibleZone) anImmersibleZoneObj =
360 Handle(HYDROData_ImmersibleZone)::DownCast( anObj );
361 if( !anImmersibleZoneObj.IsNull() ) {
362 obj = createObject( aNaturalObjectsRootObj, anImmersibleZoneObj );
369 Handle(HYDROData_River) aRiverObj =
370 Handle(HYDROData_River)::DownCast( anObj );
371 if( !aRiverObj.IsNull() ) {
372 obj = createObject( aNaturalObjectsRootObj, aRiverObj );
379 Handle(HYDROData_Stream) aStreamObj =
380 Handle(HYDROData_Stream)::DownCast( anObj );
381 if( !aStreamObj.IsNull() ) {
382 obj = createObject( aNaturalObjectsRootObj, aStreamObj );
389 Handle(HYDROData_Obstacle) anObstacleObj =
390 Handle(HYDROData_Obstacle)::DownCast( anObj );
391 if( !anObstacleObj.IsNull() ) {
392 obj = createObject( anObstaclesRootObj, anObstacleObj );
397 case KIND_STRICKLER_TABLE:
399 Handle(HYDROData_StricklerTable) aStricklerTableObj =
400 Handle(HYDROData_StricklerTable)::DownCast( anObj );
401 if( !aStricklerTableObj.IsNull() ) {
402 obj = createObject( aStricklerTablesRootObj, aStricklerTableObj );
404 aNoStricklerTableObj++;
408 case KIND_LAND_COVER_MAP:
410 Handle(HYDROData_LandCoverMap) aLandCoverMapObj =
411 Handle(HYDROData_LandCoverMap)::DownCast( anObj );
412 if( !aLandCoverMapObj.IsNull() ) {
413 obj = createObject( aLandCoversRootObj, aLandCoverMapObj );
418 case KIND_CALCULATION:
420 Handle(HYDROData_CalculationCase) aCalculObj =
421 Handle(HYDROData_CalculationCase)::DownCast( anObj );
422 if( !aCalculObj.IsNull() ) {
423 obj = createObject( aCalculRootObj, aCalculObj );
428 case KIND_POLYLINEXY:
430 Handle(HYDROData_PolylineXY) aPolylineObj =
431 Handle(HYDROData_PolylineXY)::DownCast( anObj );
432 if( !aPolylineObj.IsNull() ) {
433 obj = createObject( aPolylineRootObj, aPolylineObj );
440 Handle(HYDROData_Polyline3D) aPolylineObj =
441 Handle(HYDROData_Polyline3D)::DownCast( anObj );
442 if( !aPolylineObj.IsNull() ) {
443 obj = createObject( aPolyline3DRootObj, aPolylineObj );
450 Handle(HYDROData_Profile) aProfileObj =
451 Handle(HYDROData_Profile)::DownCast( anObj );
452 if( !aProfileObj.IsNull() ) {
453 obj = createObject( aProfileRootObj, aProfileObj );
458 case KIND_VISUAL_STATE:
460 Handle(HYDROData_VisualState) aVisualStateObj =
461 Handle(HYDROData_VisualState)::DownCast( anObj );
462 if( !aVisualStateObj.IsNull() ) {
463 obj = createObject( aVisualStateRootObj, aVisualStateObj );
472 // Create default Strickler table object
473 if ( aNoStricklerTableObj == 0 )
474 createDefaultStricklerTable( aDocument, aStricklerTablesRootObj );
476 //if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
478 // anObjectBrowser->setAutoOpenLevel( 3 );
479 // anObjectBrowser->openLevels();
482 HYDROGUI_DataModelSync aSync( aNewRootObj );
483 SUIT_DataObject* aRoot = root();
484 bool isNewDoc = aRoot==0;
486 aRoot = createRootModuleObject( aStudyRoot );
487 ::synchronize < suitPtr, suitPtr, HYDROGUI_DataModelSync >
488 ( aNewRootObj, aRoot, aSync );
490 SUIT_DataBrowser* ob = anApp->objectBrowser();
492 if ( !myStates.isEmpty() )
495 ob->setAutoOpenLevel( 1 );
496 //ob->setOpenStates( myStates, ENTRY_COLUMN );
501 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject )
503 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>(
504 findObject( HYDROGUI_DataObject::dataObjectEntry( theModelObject ) ) );
508 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
510 return NULL; // to do if necessary
513 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
515 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
516 return anApp ? anApp->findObject( theEntry ) : 0;
519 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
520 LightApp_Study* theStudy )
523 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
525 update( theStudy->id() );
528 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
530 CAM_ModuleObject* aRootObj = createModuleObject( theParent );
531 aRootObj->setDataModel( this );
536 void HYDROGUI_DataModel::updateModel()
538 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
540 update( aModule->getStudyId() );
543 Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
544 const ObjectKind theObjectKind )
546 QString anEntry = theEntry;
547 if( anEntry.indexOf( "_" ) != -1 ) // reference object
548 anEntry = anEntry.section( "_", -1 );
550 Handle(HYDROData_Document) aDocument = getDocument();
551 if( !aDocument.IsNull() )
553 HYDROData_Iterator anIterator( aDocument, theObjectKind );
554 for( ; anIterator.More(); anIterator.Next() )
556 Handle(HYDROData_Entity) anObject = anIterator.Current();
557 if( !anObject.IsNull() )
559 QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
560 if( anEntryRef == anEntry )
568 bool HYDROGUI_DataModel::canUndo() const
570 return getDocument()->CanUndo();
573 bool HYDROGUI_DataModel::canRedo() const
575 return getDocument()->CanRedo();
578 QStringList HYDROGUI_DataModel::undoNames() const
581 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
582 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
586 QStringList HYDROGUI_DataModel::redoNames() const
589 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
590 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
594 void HYDROGUI_DataModel::clearUndos()
596 getDocument()->ClearUndos();
599 void HYDROGUI_DataModel::clearRedos()
601 getDocument()->ClearRedos();
604 bool HYDROGUI_DataModel::undo()
608 getDocument()->Undo();
610 catch ( Standard_Failure )
617 bool HYDROGUI_DataModel::redo()
621 getDocument()->Redo();
623 catch ( Standard_Failure )
630 bool HYDROGUI_DataModel::canCopy()
632 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
633 bool isCanCopy = !aSeq.IsEmpty();
635 for ( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
636 Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
637 if( !anObject.IsNull() ) {
638 ObjectKind aKind = anObject->GetKind();
639 bool isUnrecognized = aKind <= KIND_UNKNOWN || aKind > KIND_LAST;
640 bool isChildObject = aKind == KIND_DUMMY_3D ||
641 aKind == KIND_ZONE ||
642 aKind == KIND_SHAPES_GROUP ||
643 aKind == KIND_SPLIT_GROUP;
644 if ( isUnrecognized || isChildObject ) {
654 bool HYDROGUI_DataModel::canPaste()
656 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
658 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
659 if( !anObject.IsNull() && !anObject->IsRemoved() )
665 bool HYDROGUI_DataModel::copy()
667 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
668 changeCopyingObjects( aSeq );
672 bool HYDROGUI_DataModel::paste()
674 bool anIsChanged = false;
675 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
677 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
678 if( !anObject.IsNull() && !anObject->IsRemoved() )
680 ObjectKind aKind = anObject->GetKind();
681 Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
682 if( !aClone.IsNull() )
684 anObject->CopyTo( aClone, true );
688 aClone->RemoveZLevel();
695 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
697 myCopyingObjects.Assign( theSeq );
700 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
702 switch( theObjectKind )
704 case KIND_IMAGE: return "IMAGES";
705 case KIND_POLYLINE: return "POLYLINES_3D";
706 case KIND_POLYLINEXY: return "POLYLINES";
707 case KIND_PROFILE: return "PROFILES";
708 case KIND_VISUAL_STATE: return "VISUAL_STATES";
709 case KIND_BATHYMETRY: return "BATHYMETRIES";
710 case KIND_CALCULATION: return "CALCULATION_CASES";
711 case KIND_OBSTACLE: return "OBSTACLES";
712 case KIND_ARTIFICIAL_OBJECT: return "ARTIFICIAL_OBJECTS";
713 case KIND_NATURAL_OBJECT: return "NATURAL_OBJECTS";
714 case KIND_STRICKLER_TABLE: return "STRICKLER_TABLES";
715 case KIND_LAND_COVER_MAP: return "LAND_COVER_MAPS";
716 case KIND_REGION: return "REGIONS";
722 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
724 int aStudyId = module()->application()->activeStudy()->id();
725 return HYDROData_Document::Document( aStudyId );
728 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
729 Handle(HYDROData_Entity) theModelObject,
730 const QString& theParentEntry,
731 const bool theIsBuildTree )
733 HYDROGUI_DataObject* aResObj = new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
735 const ObjectKind aKind = theModelObject->GetKind();
736 bool visibility = aKind == KIND_IMAGE || aKind == KIND_POLYLINEXY || aKind == KIND_POLYLINE ||
737 aKind == KIND_SHAPES_GROUP || aKind == KIND_SPLIT_GROUP || aKind == KIND_ZONE ||
738 aKind == KIND_IMMERSIBLE_ZONE || aKind == KIND_REGION || aKind == KIND_BATHYMETRY ||
739 aKind == KIND_OBSTACLE || aKind == KIND_STREAM || aKind == KIND_CHANNEL ||
740 aKind == KIND_DIGUE || aKind == KIND_DUMMY_3D || aKind == KIND_LAND_COVER_MAP;
743 Handle(HYDROData_Profile) aProfObj = Handle(HYDROData_Profile)::DownCast( theModelObject );
744 visibility = !aProfObj.IsNull() && aProfObj->IsValid();
747 if ( aKind == KIND_REGION )
749 QString an = aResObj->name();
755 setObjectVisibilityState( theModelObject, aResObj );
758 if ( theIsBuildTree )
760 buildObjectTree( theParent, aResObj, theParentEntry );
766 LightApp_DataObject* HYDROGUI_DataModel::buildObject( SUIT_DataObject* theParent,
767 HYDROGUI_DataObject* theObject,
768 const QString& theParentEntry,
769 const bool theIsBuildTree,
770 const bool theIsInOperation )
772 if ( theIsBuildTree )
774 buildObjectTree( theParent, theObject, theParentEntry, theIsInOperation );
779 LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject* theParent,
780 Handle(HYDROData_Zone) theModelObject,
781 const QString& theParentEntry,
782 const bool theIsBuildTree,
783 const bool theIsInOperation )
785 HYDROGUI_Zone* aZone = new HYDROGUI_Zone( theParent, theModelObject, theParentEntry, theIsInOperation );
786 LightApp_DataObject* aDataObj = buildObject( theParent, aZone, theParentEntry, theIsBuildTree, theIsInOperation );
788 setObjectVisibilityState( theModelObject, aZone );
793 LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject* theParent,
794 Handle(HYDROData_Region) theModelObject,
795 const QString& theParentEntry,
796 const bool theIsBuildTree,
797 const bool theIsInOperation )
799 return buildObject( theParent, new HYDROGUI_Region( theParent, theModelObject, theParentEntry, theIsInOperation ),
800 theParentEntry, theIsBuildTree, theIsInOperation );
803 void HYDROGUI_DataModel::createDefaultStricklerTable( const Handle(HYDROData_Document)& theDocument,
804 LightApp_DataObject* theParent )
806 // Create default Strickler table object
807 Handle(HYDROData_StricklerTable) aStricklerTableObj =
808 Handle(HYDROData_StricklerTable)::DownCast( theDocument->CreateObject(KIND_STRICKLER_TABLE) );
809 if ( !aStricklerTableObj.IsNull() )
811 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
812 QString defTablePath = resMgr->path( "resources", module()->name(), tr( "DEFAULT_STRICKLER_TABLE_FILE" ) );
813 aStricklerTableObj->Import( defTablePath );
815 QString aStricklerTableName;
816 if ( aStricklerTableObj->GetName().isEmpty() )
818 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
820 aStricklerTableName = HYDROGUI_Tool::GenerateObjectName( aModule, tr( "DEFAULT_STRICKLER_TABLE_NAME" ) );
822 if ( aStricklerTableObj->GetName() != aStricklerTableName )
823 aStricklerTableObj->SetName( aStricklerTableName );
825 aStricklerTableObj->Update();
827 LightApp_DataObject* obj = createObject( theParent, aStricklerTableObj );
831 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
832 const QString& theName,
833 const QString& theParentEntry )
835 return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
838 void HYDROGUI_DataModel::buildObjectPartition( SUIT_DataObject* theObject,
839 const HYDROData_SequenceOfObjects& theObjects,
840 const QString& thePartName,
841 const bool theIsCreateEmpty )
843 if ( theObjects.IsEmpty() && !theIsCreateEmpty )
846 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
850 LightApp_DataObject* aPartSect =
851 createObject( aGuiObj, thePartName, aGuiObj->entry() );
853 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
854 for ( ; anIter.More(); anIter.Next() )
856 Handle(HYDROData_Entity) anObj = anIter.Value();
857 if( !anObj.IsNull() && !anObj->IsRemoved() )
858 createObject( aPartSect, anObj, aGuiObj->entry(), false );
862 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
863 SUIT_DataObject* theObject,
864 const QString& theParentEntry,
865 const bool theIsInOperation )
867 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
871 Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
872 if ( aDataObj.IsNull() )
875 if ( aDataObj->IsKind( STANDARD_TYPE(HYDROData_Object) ) )
877 Handle(HYDROData_Object) aGeomObj =
878 Handle(HYDROData_Object)::DownCast( aDataObj );
880 Handle(HYDROData_DummyObject3D) anObject3D = aGeomObj->GetObject3D();
881 if ( !anObject3D.IsNull() )
882 createObject( aGuiObj, anObject3D, "", false );
885 HYDROData_SequenceOfObjects anObjGroups = aGeomObj->GetGroups();
886 buildObjectPartition( aGuiObj, anObjGroups, tr( "OBJECT_GROUPS" ), false );
890 ObjectKind anObjectKind = aDataObj->GetKind();
892 if ( anObjectKind == KIND_IMAGE )
894 Handle(HYDROData_Image) anImageObj =
895 Handle(HYDROData_Image)::DownCast( aDataObj );
896 for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
898 Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
899 if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
900 createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
903 else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
905 Handle(HYDROData_ImmersibleZone) aZoneObj =
906 Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
908 LightApp_DataObject* aPolylineSect =
909 createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
911 Handle(HYDROData_PolylineXY) aPolyline = aZoneObj->GetPolyline();
912 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
913 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
915 LightApp_DataObject* aBathSect =
916 createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
918 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aZoneObj->GetAltitudeObject();
919 if ( !anAltitudeObj.IsNull() && !anAltitudeObj->IsRemoved() )
920 createObject( aBathSect, anAltitudeObj, aGuiObj->entry(), false );
922 else if ( anObjectKind == KIND_POLYLINE )
924 Handle(HYDROData_Polyline3D) aPolyline3D =
925 Handle(HYDROData_Polyline3D)::DownCast( aDataObj );
927 LightApp_DataObject* aPolylineSect =
928 createObject( aGuiObj, tr( "POLYLINE3D_POLYLINE" ), aGuiObj->entry() );
930 Handle(HYDROData_PolylineXY) aPolylineXY = aPolyline3D->GetPolylineXY();
931 if ( !aPolylineXY.IsNull() && !aPolylineXY->IsRemoved() )
932 createObject( aPolylineSect, aPolylineXY, aGuiObj->entry(), false );
934 LightApp_DataObject* aProfileSect =
935 createObject( aGuiObj, tr( "POLYLINE3D_PROFILE" ), aGuiObj->entry() );
937 Handle(HYDROData_ProfileUZ) aProfileUZ = aPolyline3D->GetProfileUZ();
938 if ( aProfileUZ.IsNull() || aProfileUZ->IsRemoved() )
939 aProfileUZ = aPolyline3D->GetChildProfileUZ( false );
941 if ( !aProfileUZ.IsNull() && !aProfileUZ->IsRemoved() )
943 Handle(HYDROData_Profile) aProfile =
944 Handle(HYDROData_Profile)::DownCast( aProfileUZ->GetFatherObject() );
945 if ( !aProfile.IsNull() && !aProfile->IsRemoved() )
946 createObject( aProfileSect, aProfile, aGuiObj->entry(), false );
949 LightApp_DataObject* aBathSect =
950 createObject( aGuiObj, tr( "POLYLINE3D_BATHYMETRY" ), aGuiObj->entry() );
952 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aPolyline3D->GetAltitudeObject();
953 if ( !anAltitudeObj.IsNull() && !anAltitudeObj->IsRemoved() )
954 createObject( aBathSect, anAltitudeObj, aGuiObj->entry(), false );
956 else if ( anObjectKind == KIND_CALCULATION )
958 Handle(HYDROData_CalculationCase) aCaseObj =
959 Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
961 LightApp_DataObject* aPolylineSect =
962 createObject( aGuiObj, tr( "CASE_BOUNDARY" ), aGuiObj->entry() );
964 Handle(HYDROData_PolylineXY) aPolyline = aCaseObj->GetBoundaryPolyline();
965 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
966 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
968 LightApp_DataObject* aCaseAOSect =
969 createObject( aGuiObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toLatin1() ),
971 LightApp_DataObject* aCaseNOSect =
972 createObject( aGuiObj, tr( partitionName( KIND_NATURAL_OBJECT ).toLatin1() ),
975 HYDROData_SequenceOfObjects aSeq = aCaseObj->GetGeometryObjects();
976 HYDROData_SequenceOfObjects::Iterator aGOIter( aSeq );
977 Handle(HYDROData_Entity) anEntity;
978 Handle(HYDROData_ArtificialObject) anAObject;
979 Handle(HYDROData_NaturalObject) aNObject;
980 for ( ; aGOIter.More(); aGOIter.Next() )
982 anEntity = aGOIter.Value();
983 if ( anEntity.IsNull() )
985 anAObject = Handle(HYDROData_ArtificialObject)::DownCast( anEntity );
986 if ( !anAObject.IsNull() )
987 createObject( aCaseAOSect, anAObject, aGuiObj->entry(), false );
990 aNObject = Handle(HYDROData_NaturalObject)::DownCast( anEntity );
991 if ( !aNObject.IsNull() )
992 createObject( aCaseNOSect, aNObject, aGuiObj->entry(), false );
996 LightApp_DataObject* aLandCoverMapSect =
997 createObject( aGuiObj, tr( "CASE_LAND_COVER_MAP" ), aGuiObj->entry() );
999 Handle(HYDROData_LandCoverMap) aLandCoverMap = aCaseObj->GetLandCoverMap();
1000 if ( !aLandCoverMap.IsNull() && !aLandCoverMap->IsRemoved() )
1001 createObject( aLandCoverMapSect, aLandCoverMap, aGuiObj->entry(), false );
1003 LightApp_DataObject* aCaseRegionsSect =
1004 createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
1006 HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions();
1007 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
1008 for ( ; anIter.More(); anIter.Next() )
1010 Handle(HYDROData_Region) aCaseRegion =
1011 Handle(HYDROData_Region)::DownCast( anIter.Value() );
1012 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
1013 createRegion( aCaseRegionsSect, aCaseRegion, "", true, theIsInOperation );
1017 HYDROData_SequenceOfObjects aCalcGroups = aCaseObj->GetGeometryGroups();
1018 buildObjectPartition( aGuiObj, aCalcGroups, tr( "OBJECT_GROUPS" ), false );
1020 HYDROData_SequenceOfObjects aCalcSplitGroups = aCaseObj->GetSplitGroups();
1021 buildObjectPartition( aGuiObj, aCalcSplitGroups, tr( "CASE_SPLIT_GROUPS" ), false );
1025 else if ( anObjectKind == KIND_REGION )
1027 Handle(HYDROData_Region) aRegionObj =
1028 Handle(HYDROData_Region)::DownCast( aDataObj );
1030 HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
1031 HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
1032 for ( ; anIter.More(); anIter.Next() )
1034 Handle(HYDROData_Zone) aRegionZone =
1035 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1036 if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
1037 createZone( aGuiObj, aRegionZone, "", true, theIsInOperation );
1040 else if ( anObjectKind == KIND_PROFILE )
1042 Handle(HYDROData_Profile) aProfileObj =
1043 Handle(HYDROData_Profile)::DownCast( aDataObj );
1045 aGuiObj->setIsValid( aProfileObj->IsValid() );
1047 else if ( anObjectKind == KIND_CHANNEL || anObjectKind == KIND_DIGUE )
1049 Handle(HYDROData_Channel) aChannelObj =
1050 Handle(HYDROData_Channel)::DownCast( aDataObj );
1052 LightApp_DataObject* aGuideLineSect =
1053 createObject( aGuiObj, tr( "CHANNEL_GUIDE_LINE" ), aGuiObj->entry() );
1054 Handle(HYDROData_Polyline3D) aGuideLine = aChannelObj->GetGuideLine();
1055 if ( !aGuideLine.IsNull() && !aGuideLine->IsRemoved() ) {
1056 createObject( aGuideLineSect, aGuideLine, aGuiObj->entry(), false );
1059 LightApp_DataObject* aProfileSect =
1060 createObject( aGuiObj, tr( "CHANNEL_PROFILE" ), aGuiObj->entry() );
1061 Handle(HYDROData_Profile) aProfile = aChannelObj->GetProfile();
1062 if ( !aProfile.IsNull() && !aProfile->IsRemoved() ) {
1063 createObject( aProfileSect, aProfile, aGuiObj->entry(), false );
1066 else if ( anObjectKind == KIND_STREAM )
1068 Handle(HYDROData_Stream) aStreamObj =
1069 Handle(HYDROData_Stream)::DownCast( aDataObj );
1071 LightApp_DataObject* aHydraulicAxisSect =
1072 createObject( aGuiObj, tr( "STREAM_HYDRAULIC_AXIS" ), aGuiObj->entry() );
1073 Handle(HYDROData_PolylineXY) aHydraulicAxis = aStreamObj->GetHydraulicAxis();
1074 if ( !aHydraulicAxis.IsNull() && !aHydraulicAxis->IsRemoved() ) {
1075 createObject( aHydraulicAxisSect, aHydraulicAxis, aGuiObj->entry(), false );
1078 HYDROData_SequenceOfObjects aProfiles = aStreamObj->GetProfiles();
1079 buildObjectPartition( aGuiObj, aProfiles, tr( "STREAM_PROFILES" ), true );
1081 Handle(HYDROData_Polyline3D) aBottomPolyline = aStreamObj->GetBottomPolyline();
1082 if ( !aBottomPolyline.IsNull() && !aBottomPolyline->IsRemoved() ) {
1083 createObject( aGuiObj, aBottomPolyline, aGuiObj->entry(), false );
1086 else if ( anObjectKind == KIND_LAND_COVER_MAP )
1088 Handle(HYDROData_LandCoverMap) aLandCoverMapObj =
1089 Handle(HYDROData_LandCoverMap)::DownCast( aDataObj );
1091 /*TODO: reference objects of the land cover map
1092 HYDROData_SequenceOfObjects aPolylines = aLandCoverMapObj->GetPolylines();
1093 buildObjectPartition( aGuiObj, aPolylines, tr( "LAND_COVER_POLYLINES" ), true );*/
1096 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
1098 aModule->enableLCMActions();
1102 void HYDROGUI_DataModel::buildCaseTree( SUIT_DataObject* theParent, Handle(HYDROData_CalculationCase) theCase )
1104 if ( !theCase.IsNull() )
1108 // Remove previous objects tree
1109 DataObjectList aList;
1110 theParent->children( aList );
1111 QListIterator<SUIT_DataObject*> anIter( aList );
1112 while( anIter.hasNext() )
1113 removeChild( theParent, anIter.next() );
1116 new HYDROGUI_DropTargetObject( theParent, tr( "NEW_REGION" ), "", true );
1118 HYDROData_SequenceOfObjects aCaseRegions = theCase->GetRegions();
1119 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
1120 for ( ; anIter.More(); anIter.Next() )
1122 Handle(HYDROData_Region) aCaseRegion =
1123 Handle(HYDROData_Region)::DownCast( anIter.Value() );
1124 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
1125 createRegion( theParent, aCaseRegion, "", true, true );
1130 void HYDROGUI_DataModel::updateObjectTree( Handle(HYDROData_Entity)& theObj )
1132 if ( !theObj.IsNull() )
1134 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>(
1135 findObject( HYDROGUI_DataObject::dataObjectEntry( theObj ) ) );
1138 // Remove previous objects tree
1139 DataObjectList aList;
1140 aGuiObj->children( aList );
1141 QListIterator<SUIT_DataObject*> anIter( aList );
1142 while( anIter.hasNext() )
1143 removeChild( aGuiObj, anIter.next() );
1145 // Rebuild the subtree
1146 QString aParentEntry;
1147 HYDROGUI_DataObject* aParent = dynamic_cast<HYDROGUI_DataObject*>( aGuiObj->parent() );
1150 aParentEntry = aParent->entry();
1152 buildObjectTree( aParent, aGuiObj, aParentEntry, aGuiObj->isInOperation() );
1156 // workaround for the bug in SalomeApp_Study::findObjectByEntry - it can't find LightApp_DataObjects
1157 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
1160 aModule->getApp()->updateObjectBrowser();
1166 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
1167 SUIT_DataObject* theChild )
1169 SUIT_DataObject* aSubChild = theChild->firstChild();
1170 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
1171 removeChild( theChild, aSubChild );
1172 theParent->removeChild( theChild );
1175 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
1176 const QString& theName )
1178 SUIT_DataObject* aChild = theFather->firstChild();
1181 if( aChild->name() == theName )
1182 return aChild; // found
1183 aChild = aChild->nextBrother();
1185 return NULL; // not found
1188 bool HYDROGUI_DataModel::createNewRegion( Handle(HYDROData_CalculationCase) theCase,
1189 const QList<HYDROGUI_Zone*>& theZonesList )
1191 bool isOk = !theCase.IsNull();
1194 Handle(HYDROData_Region) aRegion;
1195 Handle(HYDROData_Zone) aZone;
1196 for (int i = 0; i < theZonesList.length(); i++ )
1198 aZone = Handle(HYDROData_Zone)::DownCast( theZonesList.at(i)->modelObject() );
1199 if ( !aZone.IsNull() )
1201 if ( aRegion.IsNull() )
1203 aRegion = theCase->AddNewRegion( aZone );
1204 isOk = !aRegion.IsNull();
1208 if ( !( aRegion->AddZone( aZone ) ) )
1219 bool HYDROGUI_DataModel::rename( Handle(HYDROData_Entity) theEntity, const QString& theName )
1221 if ( theName.isEmpty() )
1226 getDocument()->StartOperation();
1227 theEntity->SetName( theName );
1228 getDocument()->CommitOperation( HYDROGUI_Tool::ToExtString( tr("RENAME_TO").arg( theName ) ) );
1229 module()->application()->activeStudy()->Modified();
1231 catch ( Standard_Failure )
1233 getDocument()->AbortOperation();
1239 void HYDROGUI_DataModel::updateDocument()
1241 // Sets the default strickler coefficient from preferences to document.
1242 Handle(HYDROData_Document) aDoc = getDocument();
1243 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
1244 if ( resMgr && !aDoc.IsNull() )
1245 aDoc->SetDefaultStricklerCoefficient( resMgr->doubleValue( "preferences", "default_strickler_coefficient", 0 ) );
1248 void HYDROGUI_DataModel::setObjectVisibilityState( Handle(HYDROData_Entity) theModelObject,
1249 HYDROGUI_DataObject* theObject )
1251 SUIT_AbstractModel* treeModel = 0;
1252 LightApp_Application* app = dynamic_cast<LightApp_Application*>( module()->application() );
1254 treeModel = dynamic_cast<SUIT_AbstractModel*>( app->objectBrowser()->model() );
1258 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
1259 bool isVisible = aModule->isObjectVisible( -1, theModelObject );
1260 Qtx::VisibilityState aVisState = isVisible ? Qtx::ShownState : Qtx::HiddenState;
1261 treeModel->setVisibilityState( theObject->text( theObject->customData( Qtx::IdType ).toInt() ), aVisState, false );