Salome HOME
8c4938463e79513cc984ee3a3f3b0a9de520e2e0
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_OccGraphicView.cxx
1 // Copyright (C) 2009-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20
21 #include "HEXABLOCKGUI_OccGraphicView.hxx"
22 #include "HEXABLOCKGUI.hxx"
23 #include "HEXABLOCKGUI_VtkDocumentGraphicView.hxx"
24
25 #include <Basics_OCCTVersion.hxx>
26
27 #include <SOCC_ViewModel.h>
28 #include <SUIT_ViewManager.h>
29
30
31
32 using namespace HEXABLOCK::GUI;
33
34 // -----------------------------------------------------------------------
35 //          OccGraphicView implementation
36 // -----------------------------------------------------------------------
37
38 OccGraphicView::OccGraphicView( OCCViewer_ViewWindow* view, QWidget* parent ):
39         prs(NULL),
40         viewWindow( view ),
41         selectionMode(TopAbs_SHAPE)
42 {
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 );
47 }
48
49 void OccGraphicView::setSelectionMode(TopAbs_ShapeEnum mode)
50 {
51     if (viewWindow == NULL || selectionMode == mode)
52         return;
53
54     HEXABLOCKGUI::selectionMgr()->clearSelected();
55     globalSelection();
56     localSelection(mode);
57     selectionMode = mode;
58 }
59
60 void OccGraphicView::setSelectionMode(const QModelIndex& eltIndex)
61 {
62
63     QVariant treeVariant = eltIndex.data( HEXA_TREE_ROLE );
64     if ( !treeVariant.isValid() ) return;
65     int eltType = treeVariant.toInt();
66
67     switch ( eltType ){
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;
86     }
87 }
88
89 void OccGraphicView::getSelected(SALOME_ListIO& selectedObjects)
90 {
91     HEXABLOCKGUI::selectionMgr()->selectedObjects( selectedObjects/*, OCCViewer_Viewer::Type()*/ );
92
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)
97 //        return;
98 //    AIS_ListOfInteractive theList;
99 //    viewer->getSelectedObjects (theList);
100 //    AIS_ListIteratorOfListOfInteractive it (theList);
101 //    //iterate on list:
102 //    while (it.More ())
103 //    {
104 //        Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(it.Value()->GetOwner());
105 //        if (!anIO.IsNull())
106 //            salomeSelected.Append(anIO);
107 //        it.Next ();
108 //    }
109 }
110
111 void OccGraphicView::clearSelection()
112 {
113     if (HEXABLOCKGUI::selectionMgr() == NULL)
114         return;
115
116     HEXABLOCKGUI::selectionMgr()->clearSelected();
117     globalSelection();
118     localSelection(TopAbs_SHAPE);
119     selectionMode = TopAbs_SHAPE;
120 }
121
122 void OccGraphicView::localSelection(const int theMode )
123 {
124     if (viewWindow == NULL || prs == NULL)
125         return;
126
127     // remove all filters from selection
128     LightApp_SelectionMgr* sm = HEXABLOCKGUI::selectionMgr();
129     sm->clearFilters();
130
131     SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
132     if (vf == NULL)
133         return;
134
135     vf->LocalSelection( prs, theMode );
136 }
137
138 void OccGraphicView::globalSelection(const bool update )
139 {
140     if (viewWindow == NULL)
141         return;
142
143     SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
144     if ( vf == NULL ) return;
145
146     // Close local context
147     vf->GlobalSelection( update );
148
149     // Set selection filters in accordance with current mode
150     LightApp_SelectionMgr* sm = HEXABLOCKGUI::selectionMgr();
151     if ( sm == NULL)
152         return;
153
154     sm->clearFilters();
155
156     // Remove filters from AIS_InteractiveContext
157     Handle(AIS_InteractiveContext) ic;
158     SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
159     if ( viewer )
160     {
161         ic = viewer->getAISContext();
162         if ( !ic.IsNull() )
163             ic->RemoveFilters();
164     }
165 }
166
167 void OccGraphicView::highlight( const QModelIndex & index, bool only )
168 {
169
170     // getting association(s) from model
171     QMultiMap< QString, int >     assocEntrySubIDs;
172
173     if (HEXABLOCKGUI::currentDocGView == NULL || viewWindow == NULL) return;
174     DocumentModel*              docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
175     if (docModel == NULL) return;
176
177     assocEntrySubIDs += docModel->getAssocShapesIds(index);
178     highlight( assocEntrySubIDs, only );
179 }
180
181
182 void OccGraphicView::highlight( const QModelIndexList & indexList, bool only )
183 {
184
185     QMultiMap< QString, int >     assocEntrySubIDs;
186
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;
191
192     foreach( const QModelIndex& anEltIndex, indexList)
193     {
194        assocEntrySubIDs += docModel->getAssocShapesIds(anEltIndex);
195     }
196     highlight( assocEntrySubIDs, only );
197 }
198
199 void OccGraphicView::highlightSubShapes(const Handle(AIS_Shape)& anObj,
200                                 const TColStd_IndexedMapOfInteger& aIndexMap, bool only)
201 {
202     if (viewWindow == NULL || anObj.IsNull() )
203         return;
204
205     //get the context
206     Handle (AIS_InteractiveContext) anIC = getContext();
207     if ( anIC.IsNull() 
208 #if OCC_VERSION_LARGE <= 0x07030000
209          || !anIC->HasOpenedContext() 
210 #endif
211          )
212         return;
213
214     // ** highlight
215     Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
216     anIC->SetAutomaticHilight( false );
217
218     if (only) // * clear current highlighted if asked
219         anIC->ClearSelected( false );
220
221     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
222
223     // Get entity owners for all activated selection modes
224     getEntityOwners( anObj, anIC, anAllMap );
225
226     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
227     indicesToOwners( aIndexMap, anObj->Shape(), anAllMap, aToHiliteMap );
228
229     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
230         anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
231
232     anIC->SetAutomaticHilight( isAutoHilight );
233     anIC->HilightSelected( false );
234 }
235
236 void OccGraphicView::highlight( const QMultiMap<QString, int>& entrySubIDs, bool only)
237 {
238
239     if (HEXABLOCKGUI::currentDocGView == NULL || viewWindow == NULL)
240         return;
241
242     SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( viewWindow->getViewManager()->getViewModel() );
243     DocumentModel* docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
244     if (soccViewer == NULL || docModel == NULL)
245         return;
246
247     foreach ( QString shapeName, entrySubIDs.uniqueKeys() )
248     {
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 ){
255             if ( subId != -1 )
256                 anIndexes.Add( subId );
257         }
258         if ( anIndexes.Extent() == 0 ) continue;
259         highlightSubShapes( anObj, anIndexes, only );
260     }
261     soccViewer->Repaint();
262 }
263
264 void OccGraphicView::addShape(QString& entry, const Handle(AIS_Shape)& ais_shape)
265 {
266     if (shapes.contains(entry) || entry.isEmpty())
267         return;
268
269     shapes[entry] = ais_shape;
270 }
271
272 Handle(AIS_Shape) OccGraphicView::getShape(const QString& entry)
273 {
274     Handle(AIS_Shape) aSh;
275
276     if (!shapes.contains(entry))
277         return aSh;
278
279     return shapes[entry];
280 }
281
282 Handle(AIS_InteractiveContext) OccGraphicView::getContext()
283 {
284     Handle (AIS_InteractiveContext) anIC;
285     if (viewWindow == NULL)
286         return anIC;
287
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();
292
293     return anIC;
294 }
295
296 void OccGraphicView::addVertexToCloud(TopoDS_Vertex& vertex)
297 {
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);
305
306     // * redisplay the cloud
307     ais_shape->Redisplay();
308     if (viewWindow == NULL)
309         return;
310     SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
311     if (vf == NULL)
312         return;
313     vf->Repaint();
314 }
315
316 void OccGraphicView::displayPreview(TopoDS_Shape& shape)
317 {
318     if (viewWindow == NULL)
319         return;
320     SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
321     if (vf == NULL)
322         return;
323
324     previewShape->Set(shape);
325 //    previewShape->SetColor(Quantity_NOC_RED);
326     getContext()->SetColor( previewShape, Quantity_NOC_RED, Standard_True );
327     previewShape->Redisplay();
328     vf->Repaint();
329 }
330
331
332 Handle(AIS_Shape) OccGraphicView::getPreviewShape()
333 {
334     TopoDS_Shape shapeToPreview;
335     previewShape->Set(shapeToPreview);
336
337     return previewShape;
338 }