1 // Copyright (C) 2009-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "HEXABLOCKGUI_OccGraphicView.hxx"
22 #include "HEXABLOCKGUI.hxx"
23 #include "HEXABLOCKGUI_VtkDocumentGraphicView.hxx"
25 #include <Basics_OCCTVersion.hxx>
27 #include <SOCC_ViewModel.h>
28 #include <SUIT_ViewManager.h>
32 using namespace HEXABLOCK::GUI;
34 // -----------------------------------------------------------------------
35 // OccGraphicView implementation
36 // -----------------------------------------------------------------------
38 OccGraphicView::OccGraphicView( OCCViewer_ViewWindow* view, QWidget* parent ):
41 selectionMode(TopAbs_SHAPE)
43 TopoDS_Shape shapeToPreview;
44 previewShape = new AIS_Shape(shapeToPreview);
45 // previewShape->SetColor(Quantity_NOC_RED);
46 getContext()->SetColor( previewShape, Quantity_NOC_RED, Standard_True );
49 void OccGraphicView::setSelectionMode(TopAbs_ShapeEnum mode)
51 if (viewWindow == NULL || selectionMode == mode)
54 HEXABLOCKGUI::selectionMgr()->clearSelected();
60 void OccGraphicView::setSelectionMode(const QModelIndex& eltIndex)
63 QVariant treeVariant = eltIndex.data( HEXA_TREE_ROLE );
64 if ( !treeVariant.isValid() ) return;
65 int eltType = treeVariant.toInt();
68 case VERTEX_TREE : setSelectionMode(TopAbs_VERTEX); break;
69 case VERTEX_DIR_TREE : setSelectionMode(TopAbs_VERTEX); break;
70 case EDGE_TREE : setSelectionMode(TopAbs_EDGE); break;
71 case EDGE_DIR_TREE : setSelectionMode(TopAbs_EDGE); break;
72 case QUAD_TREE : setSelectionMode(TopAbs_FACE); break;
73 case QUAD_DIR_TREE : setSelectionMode(TopAbs_FACE); break;
74 case HEXA_TREE : setSelectionMode(TopAbs_SHAPE); break;
75 case HEXA_DIR_TREE : setSelectionMode(TopAbs_SHAPE); break;
76 case GEOMSHAPE_TREE: setSelectionMode(TopAbs_SHAPE); break;
77 case GEOMSHAPE_DIR_TREE: setSelectionMode(TopAbs_SHAPE); break;
78 case GEOMPOINT_TREE: setSelectionMode(TopAbs_VERTEX); break;
79 case GEOMPOINT_DIR_TREE: setSelectionMode(TopAbs_VERTEX); break;
80 case GEOMEDGE_TREE: setSelectionMode(TopAbs_EDGE); break;
81 case GEOMEDGE_DIR_TREE: setSelectionMode(TopAbs_EDGE); break;
82 case GEOMFACE_TREE: setSelectionMode(TopAbs_FACE); break;
83 case GEOMFACE_DIR_TREE: setSelectionMode(TopAbs_FACE); break;
84 case PROPAGATION_TREE : setSelectionMode(TopAbs_EDGE); break;
85 case PROPAGATION_DIR_TREE : setSelectionMode(TopAbs_EDGE); break;
89 void OccGraphicView::getSelected(SALOME_ListIO& selectedObjects)
91 HEXABLOCKGUI::selectionMgr()->selectedObjects( selectedObjects/*, OCCViewer_Viewer::Type()*/ );
93 // SALOME_View* vf = dynamic_cast<SALOME_View*>(HEXABLOCKGUI::currentOccGView->getViewWindow()->getViewManager()->getViewModel());
94 // if ( vf == NULL ) return;
95 // SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
96 // if (viewer == NULL)
98 // AIS_ListOfInteractive theList;
99 // viewer->getSelectedObjects (theList);
100 // AIS_ListIteratorOfListOfInteractive it (theList);
101 // //iterate on list:
102 // while (it.More ())
104 // Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(it.Value()->GetOwner());
105 // if (!anIO.IsNull())
106 // salomeSelected.Append(anIO);
111 void OccGraphicView::clearSelection()
113 if (HEXABLOCKGUI::selectionMgr() == NULL)
116 HEXABLOCKGUI::selectionMgr()->clearSelected();
118 localSelection(TopAbs_SHAPE);
119 selectionMode = TopAbs_SHAPE;
122 void OccGraphicView::localSelection(const int theMode )
124 if (viewWindow == NULL || prs == NULL)
127 // remove all filters from selection
128 LightApp_SelectionMgr* sm = HEXABLOCKGUI::selectionMgr();
131 SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
135 vf->LocalSelection( prs, theMode );
138 void OccGraphicView::globalSelection(const bool update )
140 if (viewWindow == NULL)
143 SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
144 if ( vf == NULL ) return;
146 // Close local context
147 vf->GlobalSelection( update );
149 // Set selection filters in accordance with current mode
150 LightApp_SelectionMgr* sm = HEXABLOCKGUI::selectionMgr();
156 // Remove filters from AIS_InteractiveContext
157 Handle(AIS_InteractiveContext) ic;
158 SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
161 ic = viewer->getAISContext();
167 void OccGraphicView::highlight( const QModelIndex & index, bool only )
170 // getting association(s) from model
171 QMultiMap< QString, int > assocEntrySubIDs;
173 if (HEXABLOCKGUI::currentDocGView == NULL || viewWindow == NULL) return;
174 DocumentModel* docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
175 if (docModel == NULL) return;
177 assocEntrySubIDs += docModel->getAssocShapesIds(index);
178 highlight( assocEntrySubIDs, only );
182 void OccGraphicView::highlight( const QModelIndexList & indexList, bool only )
185 QMultiMap< QString, int > assocEntrySubIDs;
187 if (HEXABLOCKGUI::currentDocGView == NULL || viewWindow == NULL) return;
188 DocumentModel* docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
189 PatternDataModel* pModel = HEXABLOCKGUI::currentDocGView->getPatternDataModel();
190 if (docModel == NULL || pModel == NULL) return;
192 foreach( const QModelIndex& anEltIndex, indexList)
194 assocEntrySubIDs += docModel->getAssocShapesIds(anEltIndex);
196 highlight( assocEntrySubIDs, only );
199 void OccGraphicView::highlightSubShapes(const Handle(AIS_Shape)& anObj,
200 const TColStd_IndexedMapOfInteger& aIndexMap, bool only)
202 if (viewWindow == NULL || anObj.IsNull() )
206 Handle (AIS_InteractiveContext) anIC = getContext();
208 #if OCC_VERSION_LARGE <= 0x07030000
209 || !anIC->HasOpenedContext()
215 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
216 anIC->SetAutomaticHilight( false );
218 if (only) // * clear current highlighted if asked
219 anIC->ClearSelected( false );
221 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
223 // Get entity owners for all activated selection modes
224 getEntityOwners( anObj, anIC, anAllMap );
226 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
227 indicesToOwners( aIndexMap, anObj->Shape(), anAllMap, aToHiliteMap );
229 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
230 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
232 anIC->SetAutomaticHilight( isAutoHilight );
233 anIC->HilightSelected( false );
236 void OccGraphicView::highlight( const QMultiMap<QString, int>& entrySubIDs, bool only)
239 if (HEXABLOCKGUI::currentDocGView == NULL || viewWindow == NULL)
242 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( viewWindow->getViewManager()->getViewModel() );
243 DocumentModel* docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
244 if (soccViewer == NULL || docModel == NULL)
247 foreach ( QString shapeName, entrySubIDs.uniqueKeys() )
249 QString shapeEntry = docModel->getGeomObjEntry(shapeName);
250 Handle(AIS_Shape) anObj = getShape(shapeEntry);
251 if ( anObj.IsNull() ) continue;
252 TColStd_IndexedMapOfInteger anIndexes;
253 QSet<int> subIds = entrySubIDs.values(shapeName).toSet();
254 foreach ( int subId, subIds ){
256 anIndexes.Add( subId );
258 if ( anIndexes.Extent() == 0 ) continue;
259 highlightSubShapes( anObj, anIndexes, only );
261 soccViewer->Repaint();
264 void OccGraphicView::addShape(QString& entry, const Handle(AIS_Shape)& ais_shape)
266 if (shapes.contains(entry) || entry.isEmpty())
269 shapes[entry] = ais_shape;
272 Handle(AIS_Shape) OccGraphicView::getShape(const QString& entry)
274 Handle(AIS_Shape) aSh;
276 if (!shapes.contains(entry))
279 return shapes[entry];
282 Handle(AIS_InteractiveContext) OccGraphicView::getContext()
284 Handle (AIS_InteractiveContext) anIC;
285 if (viewWindow == NULL)
288 SUIT_ViewManager* vm = viewWindow->getViewManager();
289 OCCViewer_Viewer* viewer = dynamic_cast<OCCViewer_Viewer*>(vm->getViewModel());
290 Handle (V3d_Viewer) v3d_viewer = viewer->getViewer3d();
291 anIC = viewer->getAISContext();
296 void OccGraphicView::addVertexToCloud(TopoDS_Vertex& vertex)
298 // * add the vertex to the cloud
299 DocumentModel* docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
300 QString shapeName = "cloud";
301 Handle(AIS_Shape) ais_shape = shapes[docModel->getGeomObjEntry(shapeName)];
302 TopoDS_Shape sh = ais_shape->Shape();
303 BRep_Builder compoundBuilder;
304 compoundBuilder.Add(sh, vertex);
306 // * redisplay the cloud
307 ais_shape->Redisplay();
308 if (viewWindow == NULL)
310 SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
316 void OccGraphicView::displayPreview(TopoDS_Shape& shape)
318 if (viewWindow == NULL)
320 SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
324 previewShape->Set(shape);
325 // previewShape->SetColor(Quantity_NOC_RED);
326 getContext()->SetColor( previewShape, Quantity_NOC_RED, Standard_True );
327 previewShape->Redisplay();
332 Handle(AIS_Shape) OccGraphicView::getPreviewShape()
334 TopoDS_Shape shapeToPreview;
335 previewShape->Set(shapeToPreview);