Salome HOME
Porting to DEV version of OpenCASCADE.
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_OccGraphicView.cxx
1 // Copyright (C) 2009-2016  CEA/DEN, EDF R&D
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 <SOCC_ViewModel.h>
26 #include <SUIT_ViewManager.h>
27
28
29
30 using namespace HEXABLOCK::GUI;
31
32 // -----------------------------------------------------------------------
33 //          OccGraphicView implementation
34 // -----------------------------------------------------------------------
35
36 OccGraphicView::OccGraphicView( OCCViewer_ViewWindow* view, QWidget* parent ):
37         prs(NULL),
38         viewWindow( view ),
39         selectionMode(TopAbs_SHAPE)
40 {
41     TopoDS_Shape shapeToPreview;
42     previewShape = new AIS_Shape(shapeToPreview);
43 //    previewShape->SetColor(Quantity_NOC_RED);
44     getContext()->SetColor( previewShape, Quantity_NOC_RED, Standard_True );
45 }
46
47 void OccGraphicView::setSelectionMode(TopAbs_ShapeEnum mode)
48 {
49     if (viewWindow == NULL || selectionMode == mode)
50         return;
51
52     HEXABLOCKGUI::selectionMgr()->clearSelected();
53     globalSelection();
54     localSelection(mode);
55     selectionMode = mode;
56 }
57
58 void OccGraphicView::setSelectionMode(const QModelIndex& eltIndex)
59 {
60
61     QVariant treeVariant = eltIndex.data( HEXA_TREE_ROLE );
62     if ( !treeVariant.isValid() ) return;
63     int eltType = treeVariant.toInt();
64
65     switch ( eltType ){
66       case VERTEX_TREE :          setSelectionMode(TopAbs_VERTEX); break;
67       case VERTEX_DIR_TREE :      setSelectionMode(TopAbs_VERTEX); break;
68       case EDGE_TREE :            setSelectionMode(TopAbs_EDGE); break;
69       case EDGE_DIR_TREE :        setSelectionMode(TopAbs_EDGE); break;
70       case QUAD_TREE :            setSelectionMode(TopAbs_FACE); break;
71       case QUAD_DIR_TREE :        setSelectionMode(TopAbs_FACE); break;
72       case HEXA_TREE :            setSelectionMode(TopAbs_SHAPE); break;
73       case HEXA_DIR_TREE :        setSelectionMode(TopAbs_SHAPE); break;
74       case GEOMSHAPE_TREE:        setSelectionMode(TopAbs_SHAPE); break;
75       case GEOMSHAPE_DIR_TREE:    setSelectionMode(TopAbs_SHAPE); break;
76       case GEOMPOINT_TREE:        setSelectionMode(TopAbs_VERTEX); break;
77       case GEOMPOINT_DIR_TREE:    setSelectionMode(TopAbs_VERTEX); break;
78       case GEOMEDGE_TREE:         setSelectionMode(TopAbs_EDGE); break;
79       case GEOMEDGE_DIR_TREE:     setSelectionMode(TopAbs_EDGE); break;
80       case GEOMFACE_TREE:         setSelectionMode(TopAbs_FACE); break;
81       case GEOMFACE_DIR_TREE:     setSelectionMode(TopAbs_FACE); break;
82       case PROPAGATION_TREE :     setSelectionMode(TopAbs_EDGE); break;
83       case PROPAGATION_DIR_TREE : setSelectionMode(TopAbs_EDGE); break;
84     }
85 }
86
87 void OccGraphicView::getSelected(SALOME_ListIO& selectedObjects)
88 {
89     HEXABLOCKGUI::selectionMgr()->selectedObjects( selectedObjects/*, OCCViewer_Viewer::Type()*/ );
90
91 //    SALOME_View* vf = dynamic_cast<SALOME_View*>(HEXABLOCKGUI::currentOccGView->getViewWindow()->getViewManager()->getViewModel());
92 //    if ( vf == NULL ) return;
93 //    SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
94 //    if (viewer == NULL)
95 //        return;
96 //    AIS_ListOfInteractive theList;
97 //    viewer->getSelectedObjects (theList);
98 //    AIS_ListIteratorOfListOfInteractive it (theList);
99 //    //iterate on list:
100 //    while (it.More ())
101 //    {
102 //        Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(it.Value()->GetOwner());
103 //        if (!anIO.IsNull())
104 //            salomeSelected.Append(anIO);
105 //        it.Next ();
106 //    }
107 }
108
109 void OccGraphicView::clearSelection()
110 {
111     if (HEXABLOCKGUI::selectionMgr() == NULL)
112         return;
113
114     HEXABLOCKGUI::selectionMgr()->clearSelected();
115     globalSelection();
116     localSelection(TopAbs_SHAPE);
117     selectionMode = TopAbs_SHAPE;
118 }
119
120 void OccGraphicView::localSelection(const int theMode )
121 {
122     if (viewWindow == NULL || prs == NULL)
123         return;
124
125     // remove all filters from selection
126     LightApp_SelectionMgr* sm = HEXABLOCKGUI::selectionMgr();
127     sm->clearFilters();
128
129     SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
130     if (vf == NULL)
131         return;
132
133     vf->LocalSelection( prs, theMode );
134 }
135
136 void OccGraphicView::globalSelection(const bool update )
137 {
138     if (viewWindow == NULL)
139         return;
140
141     SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
142     if ( vf == NULL ) return;
143
144     // Close local context
145     vf->GlobalSelection( update );
146
147     // Set selection filters in accordance with current mode
148     LightApp_SelectionMgr* sm = HEXABLOCKGUI::selectionMgr();
149     if ( sm == NULL)
150         return;
151
152     sm->clearFilters();
153
154     // Remove filters from AIS_InteractiveContext
155     Handle(AIS_InteractiveContext) ic;
156     SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
157     if ( viewer )
158     {
159         ic = viewer->getAISContext();
160         if ( !ic.IsNull() )
161             ic->RemoveFilters();
162     }
163 }
164
165 void OccGraphicView::highlight( const QModelIndex & index, bool only )
166 {
167
168     // getting association(s) from model
169     QMultiMap< QString, int >     assocEntrySubIDs;
170
171     if (HEXABLOCKGUI::currentDocGView == NULL || viewWindow == NULL) return;
172     DocumentModel*              docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
173     if (docModel == NULL) return;
174
175     assocEntrySubIDs += docModel->getAssocShapesIds(index);
176     highlight( assocEntrySubIDs, only );
177 }
178
179
180 void OccGraphicView::highlight( const QModelIndexList & indexList, bool only )
181 {
182
183     QMultiMap< QString, int >     assocEntrySubIDs;
184
185     if (HEXABLOCKGUI::currentDocGView == NULL || viewWindow == NULL) return;
186     DocumentModel*              docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
187     PatternDataModel*           pModel = HEXABLOCKGUI::currentDocGView->getPatternDataModel();
188     if (docModel == NULL || pModel == NULL) return;
189
190     foreach( const QModelIndex& anEltIndex, indexList)
191     {
192        assocEntrySubIDs += docModel->getAssocShapesIds(anEltIndex);
193     }
194     highlight( assocEntrySubIDs, only );
195 }
196
197 void OccGraphicView::highlightSubShapes(const Handle(AIS_Shape)& anObj,
198                                 const TColStd_IndexedMapOfInteger& aIndexMap, bool only)
199 {
200     if (viewWindow == NULL || anObj.IsNull() )
201         return;
202
203     //get the context
204     Handle (AIS_InteractiveContext) anIC = getContext();
205     if ( anIC.IsNull() || !anIC->HasOpenedContext() )
206         return;
207
208     // ** highlight
209     Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
210     anIC->SetAutomaticHilight( false );
211
212     if (only) // * clear current highlighted if asked
213         anIC->ClearSelected( false );
214
215     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
216
217     // Get entity owners for all activated selection modes
218     getEntityOwners( anObj, anIC, anAllMap );
219
220     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
221     indicesToOwners( aIndexMap, anObj->Shape(), anAllMap, aToHiliteMap );
222
223     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
224         anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
225
226     anIC->SetAutomaticHilight( isAutoHilight );
227     anIC->HilightSelected( false );
228 }
229
230 void OccGraphicView::highlight( const QMultiMap<QString, int>& entrySubIDs, bool only)
231 {
232
233     if (HEXABLOCKGUI::currentDocGView == NULL || viewWindow == NULL)
234         return;
235
236     SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>( viewWindow->getViewManager()->getViewModel() );
237     DocumentModel* docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
238     if (soccViewer == NULL || docModel == NULL)
239         return;
240
241     foreach ( QString shapeName, entrySubIDs.uniqueKeys() )
242     {
243         QString shapeEntry = docModel->getGeomObjEntry(shapeName);
244         Handle(AIS_Shape) anObj = getShape(shapeEntry);
245         if ( anObj.IsNull() ) continue;
246         TColStd_IndexedMapOfInteger anIndexes;
247         QSet<int> subIds = entrySubIDs.values(shapeName).toSet();
248         foreach ( int subId, subIds ){
249             if ( subId != -1 )
250                 anIndexes.Add( subId );
251         }
252         if ( anIndexes.Extent() == 0 ) continue;
253         highlightSubShapes( anObj, anIndexes, only );
254     }
255     soccViewer->Repaint();
256 }
257
258 void OccGraphicView::addShape(QString& entry, const Handle(AIS_Shape)& ais_shape)
259 {
260     if (shapes.contains(entry) || entry.isEmpty())
261         return;
262
263     shapes[entry] = ais_shape;
264 }
265
266 Handle(AIS_Shape) OccGraphicView::getShape(const QString& entry)
267 {
268     Handle(AIS_Shape) aSh;
269
270     if (!shapes.contains(entry))
271         return aSh;
272
273     return shapes[entry];
274 }
275
276 Handle(AIS_InteractiveContext) OccGraphicView::getContext()
277 {
278     Handle (AIS_InteractiveContext) anIC;
279     if (viewWindow == NULL)
280         return anIC;
281
282     SUIT_ViewManager*  vm = viewWindow->getViewManager();
283     OCCViewer_Viewer* viewer = dynamic_cast<OCCViewer_Viewer*>(vm->getViewModel());
284     Handle (V3d_Viewer) v3d_viewer = viewer->getViewer3d();
285     anIC = viewer->getAISContext();
286
287     return anIC;
288 }
289
290 void OccGraphicView::addVertexToCloud(TopoDS_Vertex& vertex)
291 {
292     // * add the vertex to the cloud
293     DocumentModel* docModel = HEXABLOCKGUI::currentDocGView->getDocumentModel();
294     QString shapeName = "cloud";
295     Handle(AIS_Shape) ais_shape = shapes[docModel->getGeomObjEntry(shapeName)];
296     TopoDS_Shape sh = ais_shape->Shape();
297     BRep_Builder compoundBuilder;
298     compoundBuilder.Add(sh, vertex);
299
300     // * redisplay the cloud
301     ais_shape->Redisplay();
302     if (viewWindow == NULL)
303         return;
304     SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
305     if (vf == NULL)
306         return;
307     vf->Repaint();
308 }
309
310 void OccGraphicView::displayPreview(TopoDS_Shape& shape)
311 {
312     if (viewWindow == NULL)
313         return;
314     SALOME_View* vf = dynamic_cast<SALOME_View*>(viewWindow->getViewManager()->getViewModel());
315     if (vf == NULL)
316         return;
317
318     previewShape->Set(shape);
319 //    previewShape->SetColor(Quantity_NOC_RED);
320     getContext()->SetColor( previewShape, Quantity_NOC_RED, Standard_True );
321     previewShape->Redisplay();
322     vf->Repaint();
323 }
324
325
326 Handle(AIS_Shape) OccGraphicView::getPreviewShape()
327 {
328     TopoDS_Shape shapeToPreview;
329     previewShape->Set(shapeToPreview);
330
331     return previewShape;
332 }