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