From a5961fd772bf86c3153bbc2388b8fc98974537de Mon Sep 17 00:00:00 2001 From: mzn Date: Tue, 8 Apr 2014 15:39:41 +0000 Subject: [PATCH] Refactoring. Hilight presentation on top. --- src/HYDROGUI/HYDROGUI_OCCDisplayer.cxx | 159 +++++++++++-------------- src/HYDROGUI/HYDROGUI_OCCDisplayer.h | 22 ++-- src/HYDROGUI/HYDROGUI_Operation.cxx | 7 +- src/HYDROGUI/HYDROGUI_Shape.cxx | 19 ++- src/HYDROGUI/HYDROGUI_ZLayers.cxx | 71 ++++++++--- src/HYDROGUI/HYDROGUI_ZLayers.h | 4 +- src/HYDROGUI/HYDROGUI_ZLayers2.cxx | 2 +- 7 files changed, 163 insertions(+), 121 deletions(-) diff --git a/src/HYDROGUI/HYDROGUI_OCCDisplayer.cxx b/src/HYDROGUI/HYDROGUI_OCCDisplayer.cxx index 8fed86b5..310e84de 100644 --- a/src/HYDROGUI/HYDROGUI_OCCDisplayer.cxx +++ b/src/HYDROGUI/HYDROGUI_OCCDisplayer.cxx @@ -73,16 +73,23 @@ void HYDROGUI_OCCDisplayer::SetToUpdate( const HYDROData_SequenceOfObjects& theO } } -int HYDROGUI_OCCDisplayer::AddTopZLayer( OCCViewer_ViewManager* theMgr ) +int HYDROGUI_OCCDisplayer::AddPreviewZLayer( OCCViewer_ViewManager* theMgr ) { int aLayer = -1; OCCViewer_Viewer* aViewer = theMgr->getOCCViewer(); if ( !aViewer ) return aLayer; - Standard_Integer aNewId = -1; - if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) - aLayer = aNewId; + aLayer = CreateTopZLayer( aViewer->getViewer3d() ); + + // Hilight presentation should be on top + Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext(); + if( !aCtx.IsNull() ) { + int aTopLayer = CreateTopZLayer( aViewer->getViewer3d() ); + if ( aTopLayer > 0 ) { + UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aTopLayer ); + } + } return aLayer; } @@ -198,77 +205,74 @@ void HYDROGUI_OCCDisplayer::Display( const HYDROData_SequenceOfObjects& theObjs, // Get 3d viewer Handle(V3d_Viewer) aViewer3d = aViewer->getViewer3d(); - // Get existing Z layers - TColStd_SequenceOfInteger anExistingZLayers; - aViewer3d->GetAllZLayers( anExistingZLayers ); - int aNbLayers = anExistingZLayers.Length(); - - // Display objects - int aNextZLayerIndex = 2; // don't use the first default Z layer ( which index = 1 ) + // Display objects: + HYDROGUI_ZLayersIterator aZLayersIt( aViewer->getViewer3d() ); + if ( !aZLayersIt.More() ) { + aZLayersIt.Next(); + } // 1. Display the ordered objects: HYDROData_SequenceOfObjects::Iterator anOrderedIter( anOrderedToDisplay ); for ( ; anOrderedIter.More(); anOrderedIter.Next() ) { Handle(HYDROData_Entity) anObj = anOrderedIter.Value(); - if ( Display( anObj, aViewer, theIsForced, aNextZLayerIndex, true ) ) { - aNextZLayerIndex++; + if ( Display( anObj, aViewer, theIsForced ) ) { + // set Z layer ( one Z layer for each ordered object ) + int aZLayerId = aZLayersIt.LayerId(); + SetZLayer( aViewer, anObj, aZLayerId ); + SetZLayerSettings( aViewer3d, aZLayerId, true ); + aZLayersIt.Next(); } } // 2. Display the unordered objects: bool isDisplayed = false; + int anUnorderedZLayerId = aZLayersIt.LayerId(); HYDROData_SequenceOfObjects::Iterator anUnorderedIter( anUnorderedToDisplay ); for ( ; anUnorderedIter.More(); anUnorderedIter.Next() ) { Handle(HYDROData_Entity) anObj = anUnorderedIter.Value(); - if ( Display( anObj, aViewer, theIsForced, aNextZLayerIndex, false ) ) { + if ( Display( anObj, aViewer, theIsForced) ) { + // set Z layer ( one Z layer for all unordered objects ) + SetZLayer( aViewer, anObj, anUnorderedZLayerId ); + if ( !isDisplayed ) { + SetZLayerSettings( aViewer3d, anUnorderedZLayerId, false ); + } isDisplayed = true; } } // 3. Update the top Z layer index if ( isDisplayed ) { - aNextZLayerIndex++; + aZLayersIt.Next(); } // Update Z layer of the active operation - aViewer3d->GetAllZLayers( anExistingZLayers ); + int aPreviewZLayerId = aZLayersIt.LayerId(); HYDROGUI_Module* aModule = module(); SUIT_Operation* anOp = aModule->activeOperation(); HYDROGUI_Operation* aHOp = anOp ? dynamic_cast( anOp ) : 0; if ( aHOp && aHOp->getPreviewZLayer() >= 0 ) { - Standard_Integer aLayerId = -1; - if ( aNextZLayerIndex <= aNbLayers ) - aLayerId = anExistingZLayers.Value( aNextZLayerIndex ); - else { - Standard_Integer aNewId = -1; - if ( aViewer->getViewer3d()->AddZLayer( aNewId ) ) { - aLayerId = aNewId; - } - } - aHOp->updatePreviewZLayer( aLayerId ); + aHOp->updatePreviewZLayer( aPreviewZLayerId ); + aZLayersIt.Next(); } - HYDROGUI_ZLayersIterator anIt( aViewer->getViewer3d() ); - int aMaxLayer = anIt.MaxLayer(); - UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aMaxLayer ); + // Update Z layer of hilight presentations + int aHilightLayer = aZLayersIt.TopLayer(); + UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, aHilightLayer ); - if ( theDoFitAll ) - { + // Fit all / update selection + if ( theDoFitAll ) { OCCViewer_ViewManager* aViewManager = ::qobject_cast( aViewer->getViewManager() ); - if ( aViewManager ) - { + if ( aViewManager ) { OCCViewer_ViewWindow* aViewWindow = ::qobject_cast( aViewManager->getActiveView() ); - if ( aViewWindow ) - { + if ( aViewWindow ) { aViewWindow->onFitAll(); } } - } - else if ( !aCtx.IsNull() ) // TODO: determine if this code is necessary (added as a fix for issue# 359) - { + } + else if ( !aCtx.IsNull() ) { // TODO: determine if this code is necessary (added as a fix for issue# 359) aCtx->UpdateSelected(); } } @@ -307,9 +311,7 @@ QString HYDROGUI_OCCDisplayer::GetType() const bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject, const OCCViewer_Viewer* theViewer, - const bool theIsForced, - const int theZLayerIndex, - const bool theIsOrdered ) + const bool theIsForced ) { bool aRes = false; @@ -347,55 +349,34 @@ bool HYDROGUI_OCCDisplayer::Display( const Handle(HYDROData_Entity)& theObject, bool anIsVisible = module()->isObjectVisible( aViewerId, theObject ); anObjShape->setVisible( anIsVisible, false ); - // Set Z layer - Handle(V3d_Viewer) aViewer3d = theViewer->getViewer3d(); - if ( !aViewer3d.IsNull() ) { - // Get existing layers - TColStd_SequenceOfInteger anExistingZLayers; - aViewer3d->GetAllZLayers( anExistingZLayers ); - int aNbLayers = anExistingZLayers.Length(); - - // Get or create the appropriate layer - Standard_Integer aLayerId = -1; - if ( theZLayerIndex <= aNbLayers ) { - aLayerId = anExistingZLayers.Value( theZLayerIndex ); - } else { - Standard_Integer aNewId = -1; - if ( aViewer3d->AddZLayer( aNewId ) ) { - aLayerId = aNewId; - } - } - - if ( aLayerId >= 0 ) { - // set Z layer - aCtx->SetZLayer( anObjShape->getAISShape(), aLayerId ); - - // set Z layer settings for the 3d viewer: - // current settings - Graphic3d_ZLayerSettings aSettings = aViewer3d->ZLayerSettings( aLayerId ); - // enable depth write - aSettings.EnableSetting( Graphic3d_ZLayerDepthWrite ); - // disable depth clear - aSettings.DisableSetting( Graphic3d_ZLayerDepthClear ); - if ( theIsOrdered ) { - // disable depth test - aSettings.DisableSetting( Graphic3d_ZLayerDepthTest ); - // disable depth offset - aSettings.DisableSetting( Graphic3d_ZLayerDepthOffset ); - } else { - // enable depth test - aSettings.EnableSetting( Graphic3d_ZLayerDepthTest ); - // set depth offset - aSettings.SetDepthOffsetPositive(); - } - - // set new settings - aViewer3d->SetZLayerSettings( aLayerId, aSettings ); - - aRes = true; - } - } + aRes = true; } return aRes; +} + +void HYDROGUI_OCCDisplayer::SetZLayer( const OCCViewer_Viewer* theViewer, + const Handle(HYDROData_Entity)& theObject, + const int theZLayerId ) +{ + if ( !theViewer || ( theZLayerId < 0 ) ) { + return; + } + + // Get interactive context + Handle(AIS_InteractiveContext) aCtx = theViewer->getAISContext(); + if( aCtx.IsNull() ) { + return; + } + + // Get viewer id + size_t aViewerId = (size_t)theViewer; + + // Get object shape + HYDROGUI_Shape* anObjShape = module()->getObjectShape( aViewerId, theObject ); + + // Set Z layer + if ( anObjShape ) { + aCtx->SetZLayer( anObjShape->getAISShape(), theZLayerId ); + } } \ No newline at end of file diff --git a/src/HYDROGUI/HYDROGUI_OCCDisplayer.h b/src/HYDROGUI/HYDROGUI_OCCDisplayer.h index f1cb0256..ffa2ef9f 100644 --- a/src/HYDROGUI/HYDROGUI_OCCDisplayer.h +++ b/src/HYDROGUI/HYDROGUI_OCCDisplayer.h @@ -63,10 +63,10 @@ public: virtual QString GetType() const; /** - * \brief Add the top z layer and returns its index. + * \brief Add the z layer for preview and returns its index. * \param theMgr OCC view manager */ - int AddTopZLayer( OCCViewer_ViewManager* theMgr ); + int AddPreviewZLayer( OCCViewer_ViewManager* theMgr ); /** * \brief Removes the z layer. @@ -114,7 +114,7 @@ private: /** * \brief Creates new OCC shape. * \param theViewerId viewer identifier - * \param theContext context of OCC viwer + * \param theContext context of OCC viewer * \param theObject data model object * \return pointer to new created shape */ @@ -127,15 +127,21 @@ private: * \param theObject the object to display * \param theViewer the viewer for displaying * \param theIsForced the flag used to update the object shape - * \param theZLayerIndex the Z layer index - * \param theIsOrdered the flag indicating if the Z layer ordering available for the object * \return true in case of success */ bool Display( const Handle(HYDROData_Entity)& theObject, const OCCViewer_Viewer* theViewer, - const bool theIsForced, - const int theZLayerIndex, - const bool theIsOrdered ); + const bool theIsForced ); + + /** + * \brief Set Z layer to the shape corresponding to the HYDRO data object. + * \param theViewer the viewer for Z layer setting + * \param theObject the HYDRO object + * \param theZLayerId the Z layer ID + */ + void SetZLayer( const OCCViewer_Viewer* theViewer, + const Handle(HYDROData_Entity)& theObject, + const int theZLayerId ); }; #endif diff --git a/src/HYDROGUI/HYDROGUI_Operation.cxx b/src/HYDROGUI/HYDROGUI_Operation.cxx index 761083b8..6c9d4ed9 100644 --- a/src/HYDROGUI/HYDROGUI_Operation.cxx +++ b/src/HYDROGUI/HYDROGUI_Operation.cxx @@ -144,13 +144,14 @@ OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager() */ void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager ) { - if ( !theManager && myPreviewManager ) - module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() ); + //No good: preview Z layer could be used by usual presentations + //if ( !theManager && myPreviewManager ) + // module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() ); myPreviewManager = theManager; if ( myPreviewManager ) - setPreviewZLayer( module()->getOCCDisplayer()->AddTopZLayer( myPreviewManager ) ); + setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) ); } void HYDROGUI_Operation::startOperation() diff --git a/src/HYDROGUI/HYDROGUI_Shape.cxx b/src/HYDROGUI/HYDROGUI_Shape.cxx index 0dd9f0ba..d855380c 100644 --- a/src/HYDROGUI/HYDROGUI_Shape.cxx +++ b/src/HYDROGUI/HYDROGUI_Shape.cxx @@ -165,7 +165,9 @@ void HYDROGUI_Shape::update( const bool theIsUpdateViewer ) setWire( aPolylineWire, false, false ); } else { myTopoShape = aPolylineShape; - myDisplayMode = AIS_WireFrame; + // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation. + // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ). + myDisplayMode = AIS_Shaded; buildShape(); updateShape( false, false ); @@ -188,7 +190,9 @@ void HYDROGUI_Shape::update( const bool theIsUpdateViewer ) setWire( aPolylineWire, false, false ); } else { myTopoShape = aPolylineShape; - myDisplayMode = AIS_WireFrame; + // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation. + // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ). + myDisplayMode = AIS_Shaded; buildShape(); updateShape( false, false ); @@ -441,7 +445,9 @@ void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire, const bool theIsUpdateViewer ) { myTopoShape = theWire; - myDisplayMode = AIS_WireFrame; + // To avoid that hilight presentation is equal to "normal" object presentation. + // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ). + myDisplayMode = AIS_Shaded; buildShape(); updateShape( theToDisplay, theIsUpdateViewer ); @@ -721,7 +727,12 @@ void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor ) getQuantityColorVal( theColor.green() ), getQuantityColorVal( theColor.blue() ), Quantity_TOC_RGB ); - if ( myDisplayMode == AIS_Shaded ) + + + if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode + { + myShape->SetColor( aBorderColor ); + } else if ( myDisplayMode == AIS_Shaded ) { if ( theColor.alpha() == 0 ) { diff --git a/src/HYDROGUI/HYDROGUI_ZLayers.cxx b/src/HYDROGUI/HYDROGUI_ZLayers.cxx index 2e9fad62..3e06521d 100644 --- a/src/HYDROGUI/HYDROGUI_ZLayers.cxx +++ b/src/HYDROGUI/HYDROGUI_ZLayers.cxx @@ -48,6 +48,45 @@ void SetPrsZLayer( const Handle_PrsMgr_PresentableObject& thePresentableObject, PrsMgr_PresentationManager::SetZLayer( thePresentableObject, theMode, theLayerId ); } +void SetZLayerSettings( const Handle_V3d_Viewer& theViewer3d, int theLayerId, bool theIsOrdered ) +{ + if ( theViewer3d.IsNull() || theLayerId < 0 ) { + return; + } + + Graphic3d_ZLayerSettings aSettings = theViewer3d->ZLayerSettings( theLayerId ); + // enable depth write + aSettings.EnableSetting( Graphic3d_ZLayerDepthWrite ); + // disable depth clear + aSettings.DisableSetting( Graphic3d_ZLayerDepthClear ); + if ( theIsOrdered ) { + // disable depth test + aSettings.DisableSetting( Graphic3d_ZLayerDepthTest ); + // disable depth offset + aSettings.DisableSetting( Graphic3d_ZLayerDepthOffset ); + } else { + // enable depth test + aSettings.EnableSetting( Graphic3d_ZLayerDepthTest ); + // set depth offset + aSettings.SetDepthOffsetPositive(); + } + + // set new settings + theViewer3d->SetZLayerSettings( theLayerId, aSettings ); +} + +int CreateTopZLayer( const Handle_V3d_Viewer& theViewer3d ) +{ + int aTopZLayer = -1; + + if ( theViewer3d && !theViewer3d->AddZLayer( aTopZLayer ) ) { + aTopZLayer = -1; + } + + return aTopZLayer; +} + + HYDROGUI_ZLayersIterator::HYDROGUI_ZLayersIterator( const Handle_V3d_Viewer& theViewer ) : myIndex( 0 ), myNewZLayer( -1 ), myViewer( theViewer ) { @@ -73,34 +112,36 @@ void HYDROGUI_ZLayersIterator::Init( const Handle_V3d_Viewer& theViewer ) bool HYDROGUI_ZLayersIterator::More() const { - return myIndex <= (int)myZLayers.size() || myNewZLayer >= 0; + return myIndex < (int)myZLayers.size() || myNewZLayer >= 0; } void HYDROGUI_ZLayersIterator::Next() { - if( myIndex < (int)myZLayers.size() ) - myIndex++; - else if( !myViewer.IsNull() ) - { - bool isOK = myViewer->AddZLayer( myNewZLayer )==Standard_True; - if( !isOK ) - myNewZLayer = -1; + myIndex++; + + if( myIndex >= (int)myZLayers.size() && + !myViewer.IsNull() ) { + myNewZLayer = CreateTopZLayer( myViewer ); } } -int HYDROGUI_ZLayersIterator::MaxLayer() const +int HYDROGUI_ZLayersIterator::TopLayer() const { - int aMaxLayer = -1; - for( int i=0, n=myZLayers.size(); i aMaxLayer ) - aMaxLayer = myZLayers[i]; - return aMaxLayer; + int aTopLayer = -1; + + if ( myNewZLayer >= 0 ) { + aTopLayer = myNewZLayer; + } else if ( !myZLayers.empty() ) { + aTopLayer = myZLayers.back(); + } + + return aTopLayer; } int HYDROGUI_ZLayersIterator::LayerId() const { - if( More() ) + if( myIndex < (int)myZLayers.size() ) return myZLayers[myIndex]; else return myNewZLayer; diff --git a/src/HYDROGUI/HYDROGUI_ZLayers.h b/src/HYDROGUI/HYDROGUI_ZLayers.h index c8b6cbf1..e0214e7e 100644 --- a/src/HYDROGUI/HYDROGUI_ZLayers.h +++ b/src/HYDROGUI/HYDROGUI_ZLayers.h @@ -39,7 +39,7 @@ public: bool More() const; void Next(); int LayerId() const; - int MaxLayer() const; + int TopLayer() const; private: std::vector myZLayers; @@ -51,5 +51,7 @@ private: void SetPrsZLayer( const Handle_PrsMgr_PresentableObject& thePresentableObject, const int theMode, const int theLayerId ); void UpdateZLayersOfHilightPresentationsOfDisplayedObjects( const Handle_AIS_InteractiveContext&, int theLayer ); +void SetZLayerSettings( const Handle_V3d_Viewer&, int theLayerId, bool theIsOrdered ); +int CreateTopZLayer( const Handle_V3d_Viewer& ); #endif diff --git a/src/HYDROGUI/HYDROGUI_ZLayers2.cxx b/src/HYDROGUI/HYDROGUI_ZLayers2.cxx index 0115def5..bacb4db1 100644 --- a/src/HYDROGUI/HYDROGUI_ZLayers2.cxx +++ b/src/HYDROGUI/HYDROGUI_ZLayers2.cxx @@ -40,7 +40,7 @@ void UpdateZLayersOfHilightPresentationsOfDisplayedObjects( const Handle_AIS_Int if( !aPrsObj.IsNull() ) { theContext->MainPrsMgr()->Color( aPrsObj ); - int aMode = 0;//aPrsObj->HilightMode(); + int aMode = aPrsObj->HasHilightMode() ? aPrsObj->HilightMode() : 0; SetPrsZLayer( aPrsObj, aMode, theLayer ); theContext->MainPrsMgr()->Unhighlight( aPrsObj, aMode ); } -- 2.39.2