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