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::open( const QString& theURL,
92 QStringList theFileList )
94 LightApp_DataModel::open( theURL, theStudy, theFileList );
95 const int aStudyId = theStudy->id();
97 Data_DocError res = DocError_UnknownProblem;
98 if( theFileList.count() >= 2 )
100 QString aTmpDir = theFileList[0];
101 QString aDataFileName = theFileList[1];
102 QString aStatesFileName = theFileList.count() == 3 ? theFileList[2] : "";
105 QString aDataFullPath = SUIT_Tools::addSlash( aTmpDir ) + aDataFileName;
106 QString aStatesFullPath = aStatesFileName.isEmpty() ? "" : SUIT_Tools::addSlash( aTmpDir ) + aStatesFileName;
110 res = HYDROData_Document::Load( (char*)aDataFullPath.toLatin1().constData(), aStudyId );
114 res = DocError_UnknownProblem;
116 if( res != DocError_OK )
118 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
122 if( !aStatesFullPath.isEmpty() )
124 QFile aFile( aStatesFullPath );
125 if( aFile.open( QFile::ReadOnly ) )
127 myStates = aFile.readAll();
133 // if the document open was successful, the data model update happens
134 // in the set mode of the module
135 if( res == DocError_OK )
141 bool HYDROGUI_DataModel::save( QStringList& theFileList )
143 if( !module()->application()->activeStudy() )
146 LightApp_DataModel::save( theFileList );
149 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
150 bool isMultiFile = false;
152 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
154 // save module data to temporary files
155 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
156 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
158 // save OCAF data to a temporary file
159 QString aDataFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
160 QString aDataFullPath = aTmpDir + aDataFileName;
161 Data_DocError res = getDocument()->Save( (char*)aDataFullPath.toLatin1().constData() );
162 if( res != DocError_OK )
164 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
168 // save tree state data to a temporary file
169 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
170 QByteArray aStatesData = anApp->objectBrowser()->getOpenStates( ENTRY_COLUMN );
171 QString aStatesFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO_tree_states.txt";
172 QString aStatesFullPath = aTmpDir + aStatesFileName;
173 QFile aFile( aStatesFullPath );
174 if( aFile.open( QFile::WriteOnly ) )
176 aFile.write( aStatesData );
180 // add temporary files to the list
181 theFileList.append( aTmpDir );
182 theFileList.append( aDataFileName );
183 theFileList.append( aStatesFileName );
188 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
190 QStringList& theFileList )
193 return save( theFileList );
196 bool HYDROGUI_DataModel::close()
201 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
204 QStringList& theListOfFiles )
206 LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
208 int aStudyId = theStudy->id();
210 LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
211 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
212 if ( aDocument.IsNull() || !aStudy )
215 QString aDir = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
216 QString aFileToExport = aDir + QString( QDir::separator() ) + "HYDRO.py";
218 bool aRes = aDocument->DumpToPython( aFileToExport, isMultiFile );
221 theListOfFiles.append( aDir );
222 theListOfFiles.append( aFileToExport );
228 bool HYDROGUI_DataModel::isModified() const
230 return getDocument()->IsModified();
233 bool HYDROGUI_DataModel::isSaved() const
238 void HYDROGUI_DataModel::update( const int theStudyId )
240 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
244 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
248 // create a new root object
249 CAM_DataObject* aNewRootObj = new CAM_DataObject();
251 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
252 if( aDocument.IsNull() )
255 // Create root objects:
258 LightApp_DataObject* anImageRootObj = createObject( aNewRootObj, tr( partitionName( KIND_IMAGE ).toAscii() ) );
261 LightApp_DataObject* aBathymetryRootObj = createObject( aNewRootObj, tr( partitionName( KIND_BATHYMETRY ).toAscii() ) );
263 // ARTIFICIAL OBJECTS
264 LightApp_DataObject* anArtificialObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toAscii() ) );
267 LightApp_DataObject* aNaturalObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_NATURAL_OBJECT ).toAscii() ) );
270 LightApp_DataObject* anObstaclesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_OBSTACLE ).toAscii() ) );
273 LightApp_DataObject* aStricklerTablesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_STRICKLER_TABLE ).toAscii() ) );
276 LightApp_DataObject* aLandCoversRootObj = createObject( aNewRootObj, tr( partitionName( KIND_LAND_COVER ).toAscii() ) );
279 LightApp_DataObject* aCalculRootObj = createObject( aNewRootObj, tr( partitionName( KIND_CALCULATION ).toAscii() ) );
282 LightApp_DataObject* aPolylineRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINEXY ).toAscii() ) );
285 LightApp_DataObject* aPolyline3DRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINE ).toAscii() ) );
288 LightApp_DataObject* aProfileRootObj = createObject( aNewRootObj, tr( partitionName( KIND_PROFILE ).toAscii() ) );
291 LightApp_DataObject* aVisualStateRootObj = createObject( aNewRootObj, tr( partitionName( KIND_VISUAL_STATE ).toAscii() ) );
293 int aNoStricklerTableObj = 0;
294 HYDROData_Iterator anIterator( aDocument, KIND_UNKNOWN );
295 for( ; anIterator.More(); anIterator.Next() ) {
296 LightApp_DataObject* obj = 0;
297 Handle(HYDROData_Entity) anObj = anIterator.Current();
299 if ( !anObj.IsNull() )
301 switch ( anObj->GetKind() ) {
304 Handle(HYDROData_Image) anImageObj =
305 Handle(HYDROData_Image)::DownCast( anObj );
306 if( !anImageObj.IsNull() ) {
307 obj = createObject( anImageRootObj, anImageObj );
312 case KIND_BATHYMETRY:
314 Handle(HYDROData_Bathymetry) aBathymetryObj =
315 Handle(HYDROData_Bathymetry)::DownCast( anObj );
316 if( !aBathymetryObj.IsNull() ) {
317 obj = createObject( aBathymetryRootObj, aBathymetryObj );
324 Handle(HYDROData_Channel) aChannelObj =
325 Handle(HYDROData_Channel)::DownCast( anObj );
326 if( !aChannelObj.IsNull() ) {
327 obj = createObject( anArtificialObjectsRootObj, aChannelObj );
334 Handle(HYDROData_Digue) aDigueObj =
335 Handle(HYDROData_Digue)::DownCast( anObj );
336 if( !aDigueObj.IsNull() ) {
337 obj = createObject( anArtificialObjectsRootObj, aDigueObj );
342 case KIND_IMMERSIBLE_ZONE:
344 Handle(HYDROData_ImmersibleZone) anImmersibleZoneObj =
345 Handle(HYDROData_ImmersibleZone)::DownCast( anObj );
346 if( !anImmersibleZoneObj.IsNull() ) {
347 obj = createObject( aNaturalObjectsRootObj, anImmersibleZoneObj );
354 Handle(HYDROData_River) aRiverObj =
355 Handle(HYDROData_River)::DownCast( anObj );
356 if( !aRiverObj.IsNull() ) {
357 obj = createObject( aNaturalObjectsRootObj, aRiverObj );
364 Handle(HYDROData_Stream) aStreamObj =
365 Handle(HYDROData_Stream)::DownCast( anObj );
366 if( !aStreamObj.IsNull() ) {
367 obj = createObject( aNaturalObjectsRootObj, aStreamObj );
374 Handle(HYDROData_Obstacle) anObstacleObj =
375 Handle(HYDROData_Obstacle)::DownCast( anObj );
376 if( !anObstacleObj.IsNull() ) {
377 obj = createObject( anObstaclesRootObj, anObstacleObj );
382 case KIND_STRICKLER_TABLE:
384 Handle(HYDROData_StricklerTable) aStricklerTableObj =
385 Handle(HYDROData_StricklerTable)::DownCast( anObj );
386 if( !aStricklerTableObj.IsNull() ) {
387 obj = createObject( aStricklerTablesRootObj, aStricklerTableObj );
389 aNoStricklerTableObj++;
393 case KIND_LAND_COVER:
395 Handle(HYDROData_LandCover) aLandCoverObj =
396 Handle(HYDROData_LandCover)::DownCast( anObj );
397 if( !aLandCoverObj.IsNull() ) {
398 obj = createObject( aLandCoversRootObj, aLandCoverObj );
403 case KIND_CALCULATION:
405 Handle(HYDROData_CalculationCase) aCalculObj =
406 Handle(HYDROData_CalculationCase)::DownCast( anObj );
407 if( !aCalculObj.IsNull() ) {
408 obj = createObject( aCalculRootObj, aCalculObj );
413 case KIND_POLYLINEXY:
415 Handle(HYDROData_PolylineXY) aPolylineObj =
416 Handle(HYDROData_PolylineXY)::DownCast( anObj );
417 if( !aPolylineObj.IsNull() ) {
418 obj = createObject( aPolylineRootObj, aPolylineObj );
425 Handle(HYDROData_Polyline3D) aPolylineObj =
426 Handle(HYDROData_Polyline3D)::DownCast( anObj );
427 if( !aPolylineObj.IsNull() ) {
428 obj = createObject( aPolyline3DRootObj, aPolylineObj );
435 Handle(HYDROData_Profile) aProfileObj =
436 Handle(HYDROData_Profile)::DownCast( anObj );
437 if( !aProfileObj.IsNull() ) {
438 obj = createObject( aProfileRootObj, aProfileObj );
443 case KIND_VISUAL_STATE:
445 Handle(HYDROData_VisualState) aVisualStateObj =
446 Handle(HYDROData_VisualState)::DownCast( anObj );
447 if( !aVisualStateObj.IsNull() ) {
448 obj = createObject( aVisualStateRootObj, aVisualStateObj );
457 // Create default Strickler table object
458 if ( aNoStricklerTableObj == 0 )
459 createDefaultStricklerTable( aDocument, aStricklerTablesRootObj );
461 //if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
463 // anObjectBrowser->setAutoOpenLevel( 3 );
464 // anObjectBrowser->openLevels();
467 HYDROGUI_DataModelSync aSync( aNewRootObj );
468 SUIT_DataObject* aRoot = root();
469 bool isNewDoc = aRoot==0;
471 aRoot = createRootModuleObject( aStudyRoot );
472 ::synchronize < suitPtr, suitPtr, HYDROGUI_DataModelSync >
473 ( aNewRootObj, aRoot, aSync );
475 SUIT_DataBrowser* ob = anApp->objectBrowser();
477 if ( !myStates.isEmpty() )
480 ob->setOpenStates( myStates, ENTRY_COLUMN );
485 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject )
487 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>(
488 findObject( HYDROGUI_DataObject::dataObjectEntry( theModelObject ) ) );
492 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
494 return NULL; // to do if necessary
497 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
499 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
500 return anApp ? anApp->findObject( theEntry ) : 0;
503 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
504 LightApp_Study* theStudy )
507 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
509 update( theStudy->id() );
512 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
514 CAM_ModuleObject* aRootObj = createModuleObject( theParent );
515 aRootObj->setDataModel( this );
520 void HYDROGUI_DataModel::updateModel()
522 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
524 update( aModule->getStudyId() );
527 Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
528 const ObjectKind theObjectKind )
530 QString anEntry = theEntry;
531 if( anEntry.indexOf( "_" ) != -1 ) // reference object
532 anEntry = anEntry.section( "_", -1 );
534 Handle(HYDROData_Document) aDocument = getDocument();
535 if( !aDocument.IsNull() )
537 HYDROData_Iterator anIterator( aDocument, theObjectKind );
538 for( ; anIterator.More(); anIterator.Next() )
540 Handle(HYDROData_Entity) anObject = anIterator.Current();
541 if( !anObject.IsNull() )
543 QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
544 if( anEntryRef == anEntry )
552 bool HYDROGUI_DataModel::canUndo() const
554 return getDocument()->CanUndo();
557 bool HYDROGUI_DataModel::canRedo() const
559 return getDocument()->CanRedo();
562 QStringList HYDROGUI_DataModel::undoNames() const
565 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
566 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
570 QStringList HYDROGUI_DataModel::redoNames() const
573 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
574 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
578 void HYDROGUI_DataModel::clearUndos()
580 getDocument()->ClearUndos();
583 void HYDROGUI_DataModel::clearRedos()
585 getDocument()->ClearRedos();
588 bool HYDROGUI_DataModel::undo()
592 getDocument()->Undo();
594 catch ( Standard_Failure )
601 bool HYDROGUI_DataModel::redo()
605 getDocument()->Redo();
607 catch ( Standard_Failure )
614 bool HYDROGUI_DataModel::canCopy()
616 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
617 bool isCanCopy = !aSeq.IsEmpty();
619 for ( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
620 Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
621 if( !anObject.IsNull() ) {
622 ObjectKind aKind = anObject->GetKind();
623 bool isUnrecognized = aKind <= KIND_UNKNOWN || aKind > KIND_LAST;
624 bool isChildObject = aKind == KIND_DUMMY_3D ||
625 aKind == KIND_ZONE ||
626 aKind == KIND_SHAPES_GROUP ||
627 aKind == KIND_SPLITTED_GROUP;
628 if ( isUnrecognized || isChildObject ) {
638 bool HYDROGUI_DataModel::canPaste()
640 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
642 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
643 if( !anObject.IsNull() && !anObject->IsRemoved() )
649 bool HYDROGUI_DataModel::copy()
651 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
652 changeCopyingObjects( aSeq );
656 bool HYDROGUI_DataModel::paste()
658 bool anIsChanged = false;
659 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
661 Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
662 if( !anObject.IsNull() && !anObject->IsRemoved() )
664 ObjectKind aKind = anObject->GetKind();
665 Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
666 if( !aClone.IsNull() )
668 anObject->CopyTo( aClone );
672 aClone->RemoveZLevel();
679 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
681 myCopyingObjects.Assign( theSeq );
684 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
686 switch( theObjectKind )
688 case KIND_IMAGE: return "IMAGES";
689 case KIND_POLYLINE: return "POLYLINES_3D";
690 case KIND_POLYLINEXY: return "POLYLINES";
691 case KIND_PROFILE: return "PROFILES";
692 case KIND_VISUAL_STATE: return "VISUAL_STATES";
693 case KIND_BATHYMETRY: return "BATHYMETRIES";
694 case KIND_CALCULATION: return "CALCULATION_CASES";
695 case KIND_OBSTACLE: return "OBSTACLES";
696 case KIND_ARTIFICIAL_OBJECT: return "ARTIFICIAL_OBJECTS";
697 case KIND_NATURAL_OBJECT: return "NATURAL_OBJECTS";
698 case KIND_STRICKLER_TABLE: return "STRICKLER_TABLES";
699 case KIND_LAND_COVER: return "LAND_COVERS";
705 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
707 int aStudyId = module()->application()->activeStudy()->id();
708 return HYDROData_Document::Document( aStudyId );
711 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
712 Handle(HYDROData_Entity) theModelObject,
713 const QString& theParentEntry,
714 const bool theIsBuildTree )
716 HYDROGUI_DataObject* aResObj = new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
718 const ObjectKind aKind = theModelObject->GetKind();
719 bool visibility = aKind == KIND_IMAGE || aKind == KIND_POLYLINEXY || aKind == KIND_POLYLINE ||
720 aKind == KIND_SHAPES_GROUP || aKind == KIND_SPLITTED_GROUP || aKind == KIND_ZONE ||
721 aKind == KIND_IMMERSIBLE_ZONE || aKind == KIND_REGION || aKind == KIND_BATHYMETRY ||
722 aKind == KIND_OBSTACLE || aKind == KIND_STREAM || aKind == KIND_CHANNEL ||
723 aKind == KIND_DIGUE || aKind == KIND_DUMMY_3D || aKind == KIND_LAND_COVER;
726 Handle(HYDROData_Profile) aProfObj = Handle(HYDROData_Profile)::DownCast( theModelObject );
727 visibility = !aProfObj.IsNull() && aProfObj->IsValid();
730 if ( aKind == KIND_REGION )
732 QString an = aResObj->name();
738 SUIT_AbstractModel* treeModel = 0;
739 LightApp_Application* app = dynamic_cast<LightApp_Application*>( module()->application() );
741 treeModel = dynamic_cast<SUIT_AbstractModel*>( app->objectBrowser()->model() );
745 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
746 bool isVisible = aModule->isObjectVisible( -1, theModelObject );
747 Qtx::VisibilityState aVisState = isVisible ? Qtx::ShownState : Qtx::HiddenState;
748 treeModel->setVisibilityState( aResObj->text( aResObj->customData( Qtx::IdType ).toInt() ), aVisState, false );
752 if ( theIsBuildTree )
754 buildObjectTree( theParent, aResObj, theParentEntry );
760 LightApp_DataObject* HYDROGUI_DataModel::buildObject( SUIT_DataObject* theParent,
761 HYDROGUI_DataObject* theObject,
762 const QString& theParentEntry,
763 const bool theIsBuildTree,
764 const bool theIsInOperation )
766 if ( theIsBuildTree )
768 buildObjectTree( theParent, theObject, theParentEntry, theIsInOperation );
773 LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject* theParent,
774 Handle(HYDROData_Zone) theModelObject,
775 const QString& theParentEntry,
776 const bool theIsBuildTree,
777 const bool theIsInOperation )
779 return buildObject( theParent, new HYDROGUI_Zone( theParent, theModelObject, theParentEntry, theIsInOperation ),
780 theParentEntry, theIsBuildTree, theIsInOperation );
783 LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject* theParent,
784 Handle(HYDROData_Region) theModelObject,
785 const QString& theParentEntry,
786 const bool theIsBuildTree,
787 const bool theIsInOperation )
789 return buildObject( theParent, new HYDROGUI_Region( theParent, theModelObject, theParentEntry, theIsInOperation ),
790 theParentEntry, theIsBuildTree, theIsInOperation );
793 void HYDROGUI_DataModel::createDefaultStricklerTable( const Handle(HYDROData_Document)& theDocument,
794 LightApp_DataObject* theParent )
796 // Create default Strickler table object
797 Handle(HYDROData_StricklerTable) aStricklerTableObj =
798 Handle(HYDROData_StricklerTable)::DownCast( theDocument->CreateObject(KIND_STRICKLER_TABLE) );
799 if ( !aStricklerTableObj.IsNull() )
801 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
802 QString defTablePath = resMgr->path( "resources", module()->name(), tr( "DEFAULT_STRICKLER_TABLE_FILE" ) );
803 aStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( defTablePath ) );
805 QString aStricklerTableName;
806 if ( aStricklerTableObj->GetName().isEmpty() )
808 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
810 aStricklerTableName = HYDROGUI_Tool::GenerateObjectName( aModule, tr( "DEFAULT_STRICKLER_TABLE_NAME" ) );
812 if ( aStricklerTableObj->GetName() != aStricklerTableName )
813 aStricklerTableObj->SetName( aStricklerTableName );
815 aStricklerTableObj->Update();
817 LightApp_DataObject* obj = createObject( theParent, aStricklerTableObj );
821 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
822 const QString& theName,
823 const QString& theParentEntry )
825 return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
828 void HYDROGUI_DataModel::buildObjectPartition( SUIT_DataObject* theObject,
829 const HYDROData_SequenceOfObjects& theObjects,
830 const QString& thePartName,
831 const bool theIsCreateEmpty )
833 if ( theObjects.IsEmpty() && !theIsCreateEmpty )
836 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
840 LightApp_DataObject* aPartSect =
841 createObject( aGuiObj, thePartName, aGuiObj->entry() );
843 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
844 for ( ; anIter.More(); anIter.Next() )
846 Handle(HYDROData_Entity) anObj = anIter.Value();
847 if( !anObj.IsNull() && !anObj->IsRemoved() )
848 createObject( aPartSect, anObj, aGuiObj->entry(), false );
852 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
853 SUIT_DataObject* theObject,
854 const QString& theParentEntry,
855 const bool theIsInOperation )
857 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
861 Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
862 if ( aDataObj.IsNull() )
865 if ( aDataObj->IsKind( STANDARD_TYPE(HYDROData_Object) ) )
867 Handle(HYDROData_Object) aGeomObj =
868 Handle(HYDROData_Object)::DownCast( aDataObj );
870 Handle(HYDROData_DummyObject3D) anObject3D = aGeomObj->GetObject3D();
871 if ( !anObject3D.IsNull() )
872 createObject( aGuiObj, anObject3D, "", false );
875 HYDROData_SequenceOfObjects anObjGroups = aGeomObj->GetGroups();
876 buildObjectPartition( aGuiObj, anObjGroups, tr( "OBJECT_GROUPS" ), false );
880 ObjectKind anObjectKind = aDataObj->GetKind();
882 if ( anObjectKind == KIND_IMAGE )
884 Handle(HYDROData_Image) anImageObj =
885 Handle(HYDROData_Image)::DownCast( aDataObj );
886 for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
888 Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
889 if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
890 createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
893 else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
895 Handle(HYDROData_ImmersibleZone) aZoneObj =
896 Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
898 LightApp_DataObject* aPolylineSect =
899 createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
901 Handle(HYDROData_PolylineXY) aPolyline = aZoneObj->GetPolyline();
902 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
903 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
905 LightApp_DataObject* aBathSect =
906 createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
908 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aZoneObj->GetAltitudeObject();
909 if ( !anAltitudeObj.IsNull() && !anAltitudeObj->IsRemoved() )
910 createObject( aBathSect, anAltitudeObj, aGuiObj->entry(), false );
912 else if ( anObjectKind == KIND_POLYLINE )
914 Handle(HYDROData_Polyline3D) aPolyline3D =
915 Handle(HYDROData_Polyline3D)::DownCast( aDataObj );
917 LightApp_DataObject* aPolylineSect =
918 createObject( aGuiObj, tr( "POLYLINE3D_POLYLINE" ), aGuiObj->entry() );
920 Handle(HYDROData_PolylineXY) aPolylineXY = aPolyline3D->GetPolylineXY();
921 if ( !aPolylineXY.IsNull() && !aPolylineXY->IsRemoved() )
922 createObject( aPolylineSect, aPolylineXY, aGuiObj->entry(), false );
924 LightApp_DataObject* aProfileSect =
925 createObject( aGuiObj, tr( "POLYLINE3D_PROFILE" ), aGuiObj->entry() );
927 Handle(HYDROData_ProfileUZ) aProfileUZ = aPolyline3D->GetProfileUZ();
928 if ( aProfileUZ.IsNull() || aProfileUZ->IsRemoved() )
929 aProfileUZ = aPolyline3D->GetChildProfileUZ( false );
931 if ( !aProfileUZ.IsNull() && !aProfileUZ->IsRemoved() )
933 Handle(HYDROData_Profile) aProfile =
934 Handle(HYDROData_Profile)::DownCast( aProfileUZ->GetFatherObject() );
935 if ( !aProfile.IsNull() && !aProfile->IsRemoved() )
936 createObject( aProfileSect, aProfile, aGuiObj->entry(), false );
939 LightApp_DataObject* aBathSect =
940 createObject( aGuiObj, tr( "POLYLINE3D_BATHYMETRY" ), aGuiObj->entry() );
942 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aPolyline3D->GetAltitudeObject();
943 if ( !anAltitudeObj.IsNull() && !anAltitudeObj->IsRemoved() )
944 createObject( aBathSect, anAltitudeObj, aGuiObj->entry(), false );
946 else if ( anObjectKind == KIND_CALCULATION )
948 Handle(HYDROData_CalculationCase) aCaseObj =
949 Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
951 LightApp_DataObject* aPolylineSect =
952 createObject( aGuiObj, tr( "CASE_BOUNDARY" ), aGuiObj->entry() );
954 Handle(HYDROData_PolylineXY) aPolyline = aCaseObj->GetBoundaryPolyline();
955 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
956 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
958 LightApp_DataObject* aCaseAOSect =
959 createObject( aGuiObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toAscii() ),
961 LightApp_DataObject* aCaseNOSect =
962 createObject( aGuiObj, tr( partitionName( KIND_NATURAL_OBJECT ).toAscii() ),
965 HYDROData_SequenceOfObjects aSeq = aCaseObj->GetGeometryObjects();
966 HYDROData_SequenceOfObjects::Iterator aGOIter( aSeq );
967 Handle(HYDROData_Entity) anEntity;
968 Handle(HYDROData_ArtificialObject) anAObject;
969 Handle(HYDROData_NaturalObject) aNObject;
970 for ( ; aGOIter.More(); aGOIter.Next() )
972 anEntity = aGOIter.Value();
973 if ( anEntity.IsNull() )
975 anAObject = Handle(HYDROData_ArtificialObject)::DownCast( anEntity );
976 if ( !anAObject.IsNull() )
977 createObject( aCaseAOSect, anAObject, aGuiObj->entry(), false );
980 aNObject = Handle(HYDROData_NaturalObject)::DownCast( anEntity );
981 if ( !aNObject.IsNull() )
982 createObject( aCaseNOSect, aNObject, aGuiObj->entry(), false );
985 LightApp_DataObject* aCaseRegionsSect =
986 createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
988 HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions( false );
989 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
990 for ( ; anIter.More(); anIter.Next() )
992 Handle(HYDROData_Region) aCaseRegion =
993 Handle(HYDROData_Region)::DownCast( anIter.Value() );
994 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
995 createRegion( aCaseRegionsSect, aCaseRegion, "", true, theIsInOperation );
998 LightApp_DataObject* aCaseLandCoverRegionsSect =
999 createObject( aGuiObj, tr( "CASE_LAND_COVER_REGIONS" ), aGuiObj->entry() );
1001 HYDROData_SequenceOfObjects aCaseLandCoverRegions = aCaseObj->GetRegions( true );
1002 anIter.Init( aCaseLandCoverRegions );
1003 for ( ; anIter.More(); anIter.Next() )
1005 Handle(HYDROData_Region) aCaseLCRegion =
1006 Handle(HYDROData_Region)::DownCast( anIter.Value() );
1007 if( !aCaseLCRegion.IsNull() && !aCaseLCRegion->IsRemoved() )
1008 createRegion( aCaseLandCoverRegionsSect, aCaseLCRegion, "", true, theIsInOperation );
1012 HYDROData_SequenceOfObjects aCalcGroups = aCaseObj->GetGeometryGroups();
1013 buildObjectPartition( aGuiObj, aCalcGroups, tr( "OBJECT_GROUPS" ), false );
1015 HYDROData_SequenceOfObjects aCalcSplitGroups = aCaseObj->GetSplittedGroups();
1016 buildObjectPartition( aGuiObj, aCalcSplitGroups, tr( "CASE_SPLITTED_GROUPS" ), false );
1020 else if ( anObjectKind == KIND_REGION )
1022 Handle(HYDROData_Region) aRegionObj =
1023 Handle(HYDROData_Region)::DownCast( aDataObj );
1025 HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
1026 HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
1027 for ( ; anIter.More(); anIter.Next() )
1029 Handle(HYDROData_Zone) aRegionZone =
1030 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1031 if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
1032 createZone( aGuiObj, aRegionZone, "", true, theIsInOperation );
1035 else if ( anObjectKind == KIND_PROFILE )
1037 Handle(HYDROData_Profile) aProfileObj =
1038 Handle(HYDROData_Profile)::DownCast( aDataObj );
1040 aGuiObj->setIsValid( aProfileObj->IsValid() );
1042 else if ( anObjectKind == KIND_CHANNEL || anObjectKind == KIND_DIGUE )
1044 Handle(HYDROData_Channel) aChannelObj =
1045 Handle(HYDROData_Channel)::DownCast( aDataObj );
1047 LightApp_DataObject* aGuideLineSect =
1048 createObject( aGuiObj, tr( "CHANNEL_GUIDE_LINE" ), aGuiObj->entry() );
1049 Handle(HYDROData_Polyline3D) aGuideLine = aChannelObj->GetGuideLine();
1050 if ( !aGuideLine.IsNull() && !aGuideLine->IsRemoved() ) {
1051 createObject( aGuideLineSect, aGuideLine, aGuiObj->entry(), false );
1054 LightApp_DataObject* aProfileSect =
1055 createObject( aGuiObj, tr( "CHANNEL_PROFILE" ), aGuiObj->entry() );
1056 Handle(HYDROData_Profile) aProfile = aChannelObj->GetProfile();
1057 if ( !aProfile.IsNull() && !aProfile->IsRemoved() ) {
1058 createObject( aProfileSect, aProfile, aGuiObj->entry(), false );
1061 else if ( anObjectKind == KIND_STREAM )
1063 Handle(HYDROData_Stream) aStreamObj =
1064 Handle(HYDROData_Stream)::DownCast( aDataObj );
1066 LightApp_DataObject* aHydraulicAxisSect =
1067 createObject( aGuiObj, tr( "STREAM_HYDRAULIC_AXIS" ), aGuiObj->entry() );
1068 Handle(HYDROData_PolylineXY) aHydraulicAxis = aStreamObj->GetHydraulicAxis();
1069 if ( !aHydraulicAxis.IsNull() && !aHydraulicAxis->IsRemoved() ) {
1070 createObject( aHydraulicAxisSect, aHydraulicAxis, aGuiObj->entry(), false );
1073 HYDROData_SequenceOfObjects aProfiles = aStreamObj->GetProfiles();
1074 buildObjectPartition( aGuiObj, aProfiles, tr( "STREAM_PROFILES" ), true );
1076 Handle(HYDROData_Polyline3D) aBottomPolyline = aStreamObj->GetBottomPolyline();
1077 if ( !aBottomPolyline.IsNull() && !aBottomPolyline->IsRemoved() ) {
1078 createObject( aGuiObj, aBottomPolyline, aGuiObj->entry(), false );
1083 void HYDROGUI_DataModel::buildCaseTree( SUIT_DataObject* theParent, Handle(HYDROData_CalculationCase) theCase,
1084 const bool theLandCover )
1086 if ( !theCase.IsNull() )
1090 // Remove previous objects tree
1091 DataObjectList aList;
1092 theParent->children( aList );
1093 QListIterator<SUIT_DataObject*> anIter( aList );
1094 while( anIter.hasNext() )
1095 removeChild( theParent, anIter.next() );
1098 new HYDROGUI_DropTargetObject( theParent, tr( "NEW_REGION" ), "", true );
1100 HYDROData_SequenceOfObjects aCaseRegions = theCase->GetRegions( theLandCover );
1101 HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
1102 for ( ; anIter.More(); anIter.Next() )
1104 Handle(HYDROData_Region) aCaseRegion =
1105 Handle(HYDROData_Region)::DownCast( anIter.Value() );
1106 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
1107 createRegion( theParent, aCaseRegion, "", true, true );
1112 void HYDROGUI_DataModel::updateObjectTree( Handle(HYDROData_Entity)& theObj )
1114 if ( !theObj.IsNull() )
1116 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>(
1117 findObject( HYDROGUI_DataObject::dataObjectEntry( theObj ) ) );
1120 // Remove previous objects tree
1121 DataObjectList aList;
1122 aGuiObj->children( aList );
1123 QListIterator<SUIT_DataObject*> anIter( aList );
1124 while( anIter.hasNext() )
1125 removeChild( aGuiObj, anIter.next() );
1127 // Rebuild the subtree
1128 QString aParentEntry;
1129 HYDROGUI_DataObject* aParent = dynamic_cast<HYDROGUI_DataObject*>( aGuiObj->parent() );
1132 aParentEntry = aParent->entry();
1134 buildObjectTree( aParent, aGuiObj, aParentEntry, aGuiObj->isInOperation() );
1138 // workaround for the bug in SalomeApp_Study::findObjectByEntry - it can't find LightApp_DataObjects
1139 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
1142 aModule->getApp()->updateObjectBrowser();
1148 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
1149 SUIT_DataObject* theChild )
1151 SUIT_DataObject* aSubChild = theChild->firstChild();
1152 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
1153 removeChild( theChild, aSubChild );
1154 theParent->removeChild( theChild );
1157 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
1158 const QString& theName )
1160 SUIT_DataObject* aChild = theFather->firstChild();
1163 if( aChild->name() == theName )
1164 return aChild; // found
1165 aChild = aChild->nextBrother();
1167 return NULL; // not found
1170 bool HYDROGUI_DataModel::createNewRegion( Handle(HYDROData_CalculationCase) theCase,
1171 const QList<HYDROGUI_Zone*>& theZonesList,
1172 const bool theLandCover )
1174 bool isOk = !theCase.IsNull();
1177 Handle(HYDROData_Region) aRegion;
1178 Handle(HYDROData_Zone) aZone;
1179 for (int i = 0; i < theZonesList.length(); i++ )
1181 aZone = Handle(HYDROData_Zone)::DownCast( theZonesList.at(i)->modelObject() );
1182 if ( !aZone.IsNull() )
1184 if ( aRegion.IsNull() )
1186 aRegion = theCase->AddNewRegion( aZone, theLandCover );
1187 isOk = !aRegion.IsNull();
1191 if ( !( aRegion->AddZone( aZone ) ) )
1202 bool HYDROGUI_DataModel::rename( Handle(HYDROData_Entity) theEntity, const QString& theName )
1204 if ( theName.isEmpty() )
1209 getDocument()->StartOperation();
1210 theEntity->SetName( theName );
1211 getDocument()->CommitOperation( HYDROGUI_Tool::ToExtString( tr("RENAME_TO").arg( theName ) ) );
1212 module()->application()->activeStudy()->Modified();
1214 catch ( Standard_Failure )
1216 getDocument()->AbortOperation();