X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FHYDROGUI%2FHYDROGUI_DataModel.cxx;h=dbe6cead511cd96d7b0dbd2f36861e88313a27c6;hb=5cae7e874afd2fc1b6f61023e8ebd33a933db3c7;hp=c2ed94bd5fbd37a15597802cc1202fc8b3093054;hpb=d6aeef5b61d85b44493cd5d93ed9870cc513e68f;p=modules%2Fhydro.git diff --git a/src/HYDROGUI/HYDROGUI_DataModel.cxx b/src/HYDROGUI/HYDROGUI_DataModel.cxx index c2ed94bd..dbe6cead 100644 --- a/src/HYDROGUI/HYDROGUI_DataModel.cxx +++ b/src/HYDROGUI/HYDROGUI_DataModel.cxx @@ -22,12 +22,22 @@ #include "HYDROGUI_DataModel.h" -#include "HYDROGUI_Module.h" #include "HYDROGUI_DataObject.h" +#include "HYDROGUI_Module.h" +#include "HYDROGUI_Tool.h" +#include "HYDROGUI_Zone.h" +#include "HYDROGUI_Region.h" +#include +#include #include #include +#include #include +#include +#include +#include +#include #include #include @@ -46,6 +56,14 @@ #include +#include +#include + +#include +#include + +static HYDROData_SequenceOfObjects myCopyingObjects; + HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule ) : LightApp_DataModel( theModule ) { @@ -63,7 +81,7 @@ bool HYDROGUI_DataModel::open( const QString& theURL, LightApp_DataModel::open( theURL, theStudy, theFileList ); const int aStudyId = theStudy->id(); - bool res = false; + Data_DocError res = DocError_UnknownProblem; if( theFileList.count() == 2 ) { QString aTmpDir = theFileList[0]; @@ -78,9 +96,9 @@ bool HYDROGUI_DataModel::open( const QString& theURL, } catch(...) { - res = false; + res = DocError_UnknownProblem; } - if (!res) + if( res != DocError_OK ) { module()->application()->putInfo( tr( "LOAD_ERROR" ) ); return false; @@ -89,7 +107,7 @@ bool HYDROGUI_DataModel::open( const QString& theURL, // if the document open was successful, the data model update happens // in the set mode of the module - if( !res ) + if( res == DocError_OK ) update( aStudyId ); return true; @@ -97,11 +115,9 @@ bool HYDROGUI_DataModel::open( const QString& theURL, bool HYDROGUI_DataModel::save( QStringList& theFileList ) { - if (!module()->application()->activeStudy()) + if( !module()->application()->activeStudy() ) return false; - const int aStudyId = module()->application()->activeStudy()->id(); - LightApp_DataModel::save( theFileList ); QString aTmpDir; @@ -114,11 +130,11 @@ bool HYDROGUI_DataModel::save( QStringList& theFileList ) // save data to temporary files LightApp_Study* aStudy = dynamic_cast( module()->application()->activeStudy() ); aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str(); - aFileName = SUIT_Tools::file (myStudyURL, false ) + "_HYDRO.cbf"; + aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf"; QString aFullPath = aTmpDir + aFileName; - bool res = HYDROData_Document::Document( aStudyId )->Save( (char*)aFullPath.toLatin1().constData() ); - if( !res ) + Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() ); + if( res != DocError_OK ) { module()->application()->putInfo( tr( "SAVE_ERROR" ) ); return false; @@ -143,10 +159,36 @@ bool HYDROGUI_DataModel::close() return true; } +bool HYDROGUI_DataModel::dumpPython( const QString& theURL, + CAM_Study* theStudy, + bool isMultiFile, + QStringList& theListOfFiles ) +{ + LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles ); + + int aStudyId = theStudy->id(); + + LightApp_Study* aStudy = ::qobject_cast( theStudy ); + Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId ); + if ( aDocument.IsNull() || !aStudy ) + return false; + + QString aFileToExport = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str(); + aFileToExport += QString( QDir::separator() ) + "HYDRO.py"; + + bool aRes = aDocument->DumpToPython( aFileToExport ); + + if ( aRes ) + { + theListOfFiles.append( aFileToExport ); + } + + return aRes; +} + bool HYDROGUI_DataModel::isModified() const { - int aStudyID = module()->application()->activeStudy()->id(); - return HYDROData_Document::Document( aStudyID )->IsModified(); + return getDocument()->IsModified(); } bool HYDROGUI_DataModel::isSaved() const @@ -164,9 +206,6 @@ void HYDROGUI_DataModel::update( const int theStudyId ) if( !aStudyRoot ) return; - if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() ) - anObjectBrowser->setAutoOpenLevel( 3 ); - // create root object if not exist CAM_DataObject* aRootObj = root(); if( !aRootObj ) @@ -185,7 +224,7 @@ void HYDROGUI_DataModel::update( const int theStudyId ) if( aDocument.IsNull() ) return; - LightApp_DataObject* anImageRootObj = createObject( aRootObj, "IMAGES" ); + LightApp_DataObject* anImageRootObj = createObject( aRootObj, partitionName( KIND_IMAGE ) ); HYDROData_Iterator anIterator( aDocument, KIND_IMAGE ); for( ; anIterator.More(); anIterator.Next() ) @@ -195,9 +234,70 @@ void HYDROGUI_DataModel::update( const int theStudyId ) if( !anImageObj.IsNull() ) createObject( anImageRootObj, anImageObj ); } + + LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, partitionName( KIND_BATHYMETRY ) ); + + anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY ); + for( ; anIterator.More(); anIterator.Next() ) + { + Handle(HYDROData_Bathymetry) aBathymetryObj = + Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() ); + if( !aBathymetryObj.IsNull() ) + createObject( aBathymetryRootObj, aBathymetryObj ); + } + + LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, partitionName( KIND_POLYLINE ) ); + + anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE ); + for( ; anIterator.More(); anIterator.Next() ) + { + Handle(HYDROData_Polyline) aPolylineObj = + Handle(HYDROData_Polyline)::DownCast( anIterator.Current() ); + if( !aPolylineObj.IsNull() ) + createObject( aPolylineRootObj, aPolylineObj ); + } + + LightApp_DataObject* aZonesRootObj = createObject( aRootObj, partitionName( KIND_IMMERSIBLE_ZONE ) ); + + anIterator = HYDROData_Iterator( aDocument, KIND_IMMERSIBLE_ZONE ); + for( ; anIterator.More(); anIterator.Next() ) + { + Handle(HYDROData_ImmersibleZone) aZoneObj = + Handle(HYDROData_ImmersibleZone)::DownCast( anIterator.Current() ); + if( !aZoneObj.IsNull() ) + createObject( aZonesRootObj, aZoneObj ); + } + + LightApp_DataObject* aCalculRootObj = createObject( aRootObj, partitionName( KIND_CALCULATION ) ); + + anIterator = HYDROData_Iterator( aDocument, KIND_CALCULATION ); + for( ; anIterator.More(); anIterator.Next() ) + { + Handle(HYDROData_CalculationCase) aCalculObj = + Handle(HYDROData_CalculationCase)::DownCast( anIterator.Current() ); + if( !aCalculObj.IsNull() ) + createObject( aCalculRootObj, aCalculObj ); + } + + LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, partitionName( KIND_VISUAL_STATE ) ); + + anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE ); + for( ; anIterator.More(); anIterator.Next() ) + { + Handle(HYDROData_VisualState) aVisualStateObj = + Handle(HYDROData_VisualState)::DownCast( anIterator.Current() ); + if( !aVisualStateObj.IsNull() ) + createObject( aVisualStateRootObj, aVisualStateObj ); + } + + if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() ) + { + anObjectBrowser->setAutoOpenLevel( 3 ); + anObjectBrowser->openLevels(); + } } -HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject ) +HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject ) { return NULL; // to do if necessary } @@ -209,13 +309,8 @@ HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataO SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const { - SUIT_DataObject* anObject = HYDROGUI_DataObject::objectByEntry( theEntry ); - if( !anObject ) - { - LightApp_Application* anApp = dynamic_cast( module()->application() ); - anObject = anApp ? anApp->findObject( theEntry ) : 0; - } - return anObject; + LightApp_Application* anApp = dynamic_cast( module()->application() ); + return anApp ? anApp->findObject( theEntry ) : 0; } void HYDROGUI_DataModel::update( LightApp_DataObject* theObject, @@ -241,16 +336,321 @@ void HYDROGUI_DataModel::updateModel() update( aModule->getStudyId() ); } -LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent, - Handle(HYDROData_Object) theModelObject ) +Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry, + const ObjectKind theObjectKind ) { - return new HYDROGUI_DataObject( theParent, theModelObject ); + QString anEntry = theEntry; + if( anEntry.indexOf( "_" ) != -1 ) // reference object + anEntry = anEntry.section( "_", -1 ); + + Handle(HYDROData_Document) aDocument = getDocument(); + if( !aDocument.IsNull() ) + { + HYDROData_Iterator anIterator( aDocument, theObjectKind ); + for( ; anIterator.More(); anIterator.Next() ) + { + Handle(HYDROData_Entity) anObject = anIterator.Current(); + if( !anObject.IsNull() ) + { + QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject ); + if( anEntryRef == anEntry ) + return anObject; + } + } + } + return NULL; +} + +bool HYDROGUI_DataModel::canUndo() const +{ + return getDocument()->CanUndo(); +} + +bool HYDROGUI_DataModel::canRedo() const +{ + return getDocument()->CanRedo(); +} + +QStringList HYDROGUI_DataModel::undoNames() const +{ + QStringList aNames; + for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() ) + aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) ); + return aNames; +} + +QStringList HYDROGUI_DataModel::redoNames() const +{ + QStringList aNames; + for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() ) + aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) ); + return aNames; +} + +void HYDROGUI_DataModel::clearUndos() +{ + getDocument()->ClearUndos(); +} + +void HYDROGUI_DataModel::clearRedos() +{ + getDocument()->ClearRedos(); +} + +bool HYDROGUI_DataModel::undo() +{ + try + { + getDocument()->Undo(); + } + catch ( Standard_Failure ) + { + return false; + } + return true; +} + +bool HYDROGUI_DataModel::redo() +{ + try + { + getDocument()->Redo(); + } + catch ( Standard_Failure ) + { + return false; + } + return true; +} + +bool HYDROGUI_DataModel::canCopy() +{ + HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() ); + if( aSeq.Length() != 1 ) + return false; + + Handle(HYDROData_Entity) anObject = aSeq.First(); + if( anObject.IsNull() ) + return false; + + ObjectKind aKind = anObject->GetKind(); + if( aKind == KIND_IMAGE || + aKind == KIND_POLYLINE || + aKind == KIND_CALCULATION ) + return true; + + return false; +} + +bool HYDROGUI_DataModel::canPaste() +{ + for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ ) + { + Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex ); + if( !anObject.IsNull() && !anObject->IsRemoved() ) + return true; + } + return false; +} + +bool HYDROGUI_DataModel::copy() +{ + HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() ); + changeCopyingObjects( aSeq ); + return true; +} + +bool HYDROGUI_DataModel::paste() +{ + bool anIsChanged = false; + for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ ) + { + Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex ); + if( !anObject.IsNull() && !anObject->IsRemoved() ) + { + ObjectKind aKind = anObject->GetKind(); + Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind ); + if( !aClone.IsNull() ) + { + anObject->CopyTo( aClone ); + anIsChanged = true; + + // generate a new unique name for the clone object: + // case 1: Image_1 -> Image_2 + // case 2: ImageObj -> ImageObj_1 + QString aName = aClone->GetName(); + QString aPrefix = aName; + if( aName.contains( '_' ) ) // case 1 + { + QString aSuffix = aName.section( '_', -1 ); + bool anIsInteger = false; + aSuffix.toInt( &anIsInteger ); + if( anIsInteger ) + aPrefix = aName.section( '_', 0, -2 ); + } + else // case 2 + aPrefix = aName; + aName = HYDROGUI_Tool::GenerateObjectName( (HYDROGUI_Module*)module(), aPrefix ); + aClone->SetName( aName ); + } + } + } + return anIsChanged; +} + +void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq ) +{ + myCopyingObjects.Assign( theSeq ); +} + +QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind ) +{ + switch( theObjectKind ) + { + case KIND_IMAGE: return "IMAGES"; + case KIND_POLYLINE: return "POLYLINES"; + case KIND_VISUAL_STATE: return "VISUAL_STATES"; + case KIND_BATHYMETRY: return "BATHYMETRIES"; + case KIND_CALCULATION: return "CALCULATION_CASES"; + case KIND_IMMERSIBLE_ZONE: return "IMMERSIBLE_ZONES"; + default: break; + } + return QString(); +} + +Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const +{ + int aStudyId = module()->application()->activeStudy()->id(); + return HYDROData_Document::Document( aStudyId ); +} + +LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent, + Handle(HYDROData_Entity) theModelObject, + const QString& theParentEntry, + const bool theIsBuildTree ) +{ + HYDROGUI_DataObject* aResObj = new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry ); + + if ( theIsBuildTree ) + { + buildObjectTree( theParent, aResObj, theParentEntry ); + } + + return aResObj; +} + +LightApp_DataObject* HYDROGUI_DataModel::buildObject( SUIT_DataObject* theParent, + HYDROGUI_DataObject* theObject, + const QString& theParentEntry, + const bool theIsBuildTree ) +{ + if ( theIsBuildTree ) + { + buildObjectTree( theParent, theObject, theParentEntry ); + } + return theObject; +} + +LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject* theParent, + Handle(HYDROData_Zone) theModelObject, + const QString& theParentEntry, + const bool theIsBuildTree ) +{ + return buildObject( theParent, new HYDROGUI_Zone( theParent, theModelObject, theParentEntry ), theParentEntry, theIsBuildTree ); +} + +LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject* theParent, + Handle(HYDROData_Region) theModelObject, + const QString& theParentEntry, + const bool theIsBuildTree ) +{ + return buildObject( theParent, new HYDROGUI_Region( theParent, theModelObject, theParentEntry ), theParentEntry, theIsBuildTree ); } LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent, - const QString& theName ) + const QString& theName, + const QString& theParentEntry ) +{ + return new HYDROGUI_NamedObject( theParent, theName, theParentEntry ); +} + +void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent, + SUIT_DataObject* theObject, + const QString& theParentEntry ) { - return new HYDROGUI_NamedObject( theParent, theName ); + HYDROGUI_DataObject* aGuiObj = dynamic_cast( theObject ); + if ( !aGuiObj ) + return; + + Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject(); + if ( aDataObj.IsNull() ) + return; + + ObjectKind anObjectKind = aDataObj->GetKind(); + + if ( anObjectKind == KIND_IMAGE ) + { + Handle(HYDROData_Image) anImageObj = + Handle(HYDROData_Image)::DownCast( aDataObj ); + for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ ) + { + Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex ); + if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() ) + createObject( aGuiObj, aRefObj, aGuiObj->entry(), false ); + } + } + else if ( anObjectKind == KIND_IMMERSIBLE_ZONE ) + { + Handle(HYDROData_ImmersibleZone) aZoneObj = + Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj ); + + LightApp_DataObject* aPolylineSect = + createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() ); + + Handle(HYDROData_Polyline) aPolyline = aZoneObj->GetPolyline(); + if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() ) + createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false ); + + LightApp_DataObject* aBathSect = + createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() ); + + Handle(HYDROData_Bathymetry) aBathymetry = aZoneObj->GetBathymetry(); + if ( !aBathymetry.IsNull() && !aBathymetry->IsRemoved() ) + createObject( aBathSect, aBathymetry, aGuiObj->entry(), false ); + } + else if ( anObjectKind == KIND_CALCULATION ) + { + Handle(HYDROData_CalculationCase) aCaseObj = + Handle(HYDROData_CalculationCase)::DownCast( aDataObj ); + + LightApp_DataObject* aCaseRegionsSect = + createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() ); + + HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions(); + HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions ); + for ( ; anIter.More(); anIter.Next() ) + { + Handle(HYDROData_Region) aCaseRegion = + Handle(HYDROData_Region)::DownCast( anIter.Value() ); + if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() ) + createRegion( aCaseRegionsSect, aCaseRegion, "", true ); + } + } + else if ( anObjectKind == KIND_REGION ) + { + Handle(HYDROData_Region) aRegionObj = + Handle(HYDROData_Region)::DownCast( aDataObj ); + + HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones(); + HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones ); + for ( ; anIter.More(); anIter.Next() ) + { + Handle(HYDROData_Zone) aRegionZone = + Handle(HYDROData_Zone)::DownCast( anIter.Value() ); + if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() ) + createZone( aGuiObj, aRegionZone, "", true ); + } + } } void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,