Salome HOME
Display highlight for all sub-objects of a Feature only for Construction Results.
[modules/shaper.git] / src / XGUI / XGUI_ViewerProxy.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "XGUI_ViewerProxy.h"
22 #include "XGUI_Workshop.h"
23 #include "XGUI_SalomeConnector.h"
24 #include "XGUI_Displayer.h"
25
26 #ifndef HAVE_SALOME
27   #include <AppElements_MainWindow.h>
28   #include <AppElements_ViewPort.h>
29   #include <AppElements_ViewWindow.h>
30   #include <AppElements_Viewer.h>
31 #endif
32
33 #include <ModuleBase_IViewWindow.h>
34 #include <GeomAPI_Shape.h>
35 #include <ModelAPI_ResultConstruction.h>
36
37 #include <AIS_Shape.hxx>
38
39 #include <QEvent>
40
41 XGUI_ViewerProxy::XGUI_ViewerProxy(XGUI_Workshop* theParent)
42     : ModuleBase_IViewer(theParent),
43       myWorkshop(theParent)
44 {
45 }
46
47 void XGUI_ViewerProxy::connectViewProxy()
48 {
49 #ifdef HAVE_SALOME
50   connect(myWorkshop->salomeConnector()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
51          SIGNAL(trihedronVisibilityChanged(bool)));
52 #else
53   connect(myWorkshop->mainWindow()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
54          SIGNAL(trihedronVisibilityChanged(bool)));
55 #endif
56 }
57
58 Handle(AIS_InteractiveContext) XGUI_ViewerProxy::AISContext() const
59 {
60   Handle(AIS_InteractiveContext) aContext;
61 #ifdef HAVE_SALOME
62   aContext = myWorkshop->salomeConnector()->viewer()->AISContext();
63 #else
64   aContext = myWorkshop->mainWindow()->viewer()->AISContext();
65 #endif
66   return aContext;
67 }
68
69 Handle(AIS_Trihedron) XGUI_ViewerProxy::trihedron() const
70 {
71 #ifdef HAVE_SALOME
72   return myWorkshop->salomeConnector()->viewer()->trihedron();
73 #else
74   return myWorkshop->mainWindow()->viewer()->trihedron();
75 #endif
76 }
77
78 Handle(V3d_Viewer) XGUI_ViewerProxy::v3dViewer() const
79 {
80 #ifdef HAVE_SALOME
81   return myWorkshop->salomeConnector()->viewer()->v3dViewer();
82 #else
83   return myWorkshop->mainWindow()->viewer()->v3dViewer();
84 #endif
85 }
86
87 Handle(V3d_View) XGUI_ViewerProxy::activeView() const
88 {
89 #ifdef HAVE_SALOME
90   return myWorkshop->salomeConnector()->viewer()->activeView();
91 #else
92   AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
93   return (aViewer->activeViewWindow()) ?
94     aViewer->activeViewWindow()->viewPortApp()->getView() :
95     Handle(V3d_View)();
96 #endif
97 }
98
99 QWidget* XGUI_ViewerProxy::activeViewPort() const
100 {
101 #ifdef HAVE_SALOME
102   return myWorkshop->salomeConnector()->viewer()->activeViewPort();
103 #else
104   AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
105   return (aViewer->activeViewWindow()) ?
106          aViewer->activeViewWindow()->viewPortApp(): 0;
107 #endif
108 }
109
110 void XGUI_ViewerProxy::setViewProjection(double theX, double theY, double theZ, double theTwist)
111 {
112   Handle(V3d_View) aView3d = activeView();
113   if (!aView3d.IsNull()) {
114     aView3d->SetProj(theX, theY, theZ);
115     aView3d->SetTwist( theTwist );
116     aView3d->FitAll(0.01, false);
117     aView3d->SetZSize(0.);
118     if (aView3d->Depth() < 0.1)
119       aView3d->DepthFitAll();
120   }
121 }
122
123 void XGUI_ViewerProxy::fitAll()
124 {
125 #ifdef HAVE_SALOME
126   myWorkshop->salomeConnector()->viewer()->fitAll();
127 #else
128   AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
129   if (aViewer->activeViewWindow())
130     aViewer->activeViewWindow()->viewPortApp()->fitAll();
131 #endif
132 }
133
134 void XGUI_ViewerProxy::eraseAll()
135 {
136 #ifdef HAVE_SALOME
137   myWorkshop->salomeConnector()->viewer()->eraseAll();
138 #else
139 #endif
140 }
141
142 void XGUI_ViewerProxy::connectToViewer()
143 {
144 #ifdef HAVE_SALOME
145   ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
146
147   connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
148   connect(aViewer, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)),
149     this, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)));
150
151   connect(aViewer, SIGNAL(deleteView(ModuleBase_IViewWindow*)),
152     this, SIGNAL(deleteView(ModuleBase_IViewWindow*)));
153
154   connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
155     this, SLOT(onViewCreated(ModuleBase_IViewWindow*)));
156
157   connect(aViewer, SIGNAL(activated(ModuleBase_IViewWindow*)),
158     this, SIGNAL(activated(ModuleBase_IViewWindow*)));
159
160   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
161     this, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)));
162
163   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
164     this, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
165
166   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)), this,
167           SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
168
169   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
170     this, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
171
172   connect(aViewer, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)),
173     this, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)));
174
175   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
176     this, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
177
178   connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
179
180   connect(aViewer, SIGNAL(viewTransformed(int)), this, SIGNAL(viewTransformed(int)));
181
182   connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
183           SIGNAL(contextMenuRequested(QContextMenuEvent*)));
184 #else
185   AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
186
187   connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
188
189   connect(aViewer, SIGNAL(tryCloseView(AppElements_ViewWindow*)),
190           this, SLOT(onTryCloseView(AppElements_ViewWindow*)));
191
192   connect(aViewer, SIGNAL(deleteView(AppElements_ViewWindow*)),
193           this, SLOT(onDeleteView(AppElements_ViewWindow*)));
194
195   connect(aViewer, SIGNAL(viewCreated(AppElements_ViewWindow*)),
196           this, SLOT(onViewCreated(AppElements_ViewWindow*)));
197
198   connect(aViewer, SIGNAL(activated(AppElements_ViewWindow*)),
199           this, SLOT(onActivated(AppElements_ViewWindow*)));
200
201   connect(aViewer, SIGNAL(mousePress(AppElements_ViewWindow*, QMouseEvent*)), this,
202           SLOT(onMousePress(AppElements_ViewWindow*, QMouseEvent*)));
203
204   connect(aViewer, SIGNAL(mouseRelease(AppElements_ViewWindow*, QMouseEvent*)), this,
205           SLOT(onMouseRelease(AppElements_ViewWindow*, QMouseEvent*)));
206
207   connect(aViewer, SIGNAL(mouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)), this,
208           SLOT(onMouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)));
209
210   connect(aViewer, SIGNAL(mouseMove(AppElements_ViewWindow*, QMouseEvent*)), this,
211           SLOT(onMouseMove(AppElements_ViewWindow*, QMouseEvent*)));
212
213   connect(aViewer, SIGNAL(keyPress(AppElements_ViewWindow*, QKeyEvent*)), this,
214           SLOT(onKeyPress(AppElements_ViewWindow*, QKeyEvent*)));
215
216   connect(aViewer, SIGNAL(keyRelease(AppElements_ViewWindow*, QKeyEvent*)), this,
217           SLOT(onKeyRelease(AppElements_ViewWindow*, QKeyEvent*)));
218
219   connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
220   connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
221           SIGNAL(contextMenuRequested(QContextMenuEvent*)));
222 #endif
223 }
224
225 bool XGUI_ViewerProxy::eventFilter(QObject *theObject, QEvent *theEvent)
226 {
227   if (theEvent->type() == QEvent::Enter) {
228     emit enterViewPort();
229   }
230   else if (theEvent->type() == QEvent::Leave) {
231     emit leaveViewPort();
232   }
233   return ModuleBase_IViewer::eventFilter(theObject, theEvent);
234 }
235
236 void XGUI_ViewerProxy::onViewCreated(ModuleBase_IViewWindow* theWnd)
237 {
238   theWnd->viewPort()->installEventFilter(this);
239
240   myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
241
242   emit viewCreated(theWnd);
243 }
244
245 #ifndef HAVE_SALOME
246 void XGUI_ViewerProxy::onTryCloseView(AppElements_ViewWindow* theWnd)
247 {
248   emit tryCloseView(theWnd);
249 }
250
251 void XGUI_ViewerProxy::onDeleteView(AppElements_ViewWindow* theWnd)
252 {
253   if (myWindowScale.contains(theWnd->v3dView()))
254     myWindowScale.remove (theWnd->v3dView());
255   emit deleteView(theWnd);
256 }
257
258 void XGUI_ViewerProxy::onViewCreated(AppElements_ViewWindow* theWnd)
259 {
260   theWnd->viewPort()->installEventFilter(this);
261
262   connect(theWnd, SIGNAL(vpTransformationFinished(AppElements_ViewWindow::OperationType)),
263     this, SLOT(onViewTransformed(AppElements_ViewWindow::OperationType)));
264
265   myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
266
267   emit viewCreated(theWnd);
268 }
269
270 void XGUI_ViewerProxy::onActivated(AppElements_ViewWindow* theWnd)
271 {
272   emit activated(theWnd);
273 }
274
275 void XGUI_ViewerProxy::onMousePress(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
276 {
277   emit mousePress(theWnd, theEvent);
278 }
279
280 void XGUI_ViewerProxy::onMouseRelease(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
281 {
282   emit mouseRelease(theWnd, theEvent);
283 }
284
285 void XGUI_ViewerProxy::onMouseDoubleClick(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
286 {
287   emit mouseDoubleClick(theWnd, theEvent);
288 }
289
290 void XGUI_ViewerProxy::displayHighlight()
291 {
292   Handle(AIS_InteractiveContext) aContext = AISContext();
293
294   if (myResult->groupName() == ModelAPI_ResultConstruction::group()) {
295     FeaturePtr aFeature = ModelAPI_Feature::feature(myResult);
296     if (aFeature.get()) {
297       std::list<ResultPtr> aResults = aFeature->results();
298       std::list<ResultPtr>::const_iterator aIt;
299       ResultPtr aRes;
300       Handle(AIS_Shape) aAis;
301       for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
302         aRes = (*aIt);
303         TopoDS_Shape aTShape = aRes->shape()->impl<TopoDS_Shape>();
304         aAis = new AIS_Shape(aTShape);
305         aAis->SetColor(Quantity_NOC_CYAN4);
306         aAis->SetZLayer(1); //Graphic3d_ZLayerId_Topmost
307         myHighlights.Append(aAis);
308         aContext->Display(aAis, false);
309         aContext->Deactivate(aAis);
310       }
311     }
312   }
313   else {
314     TopoDS_Shape aTShape = myResult->shape()->impl<TopoDS_Shape>();
315     Handle(AIS_Shape) aAis = new AIS_Shape(aTShape);
316     aAis->SetColor(Quantity_NOC_CYAN4);
317     aAis->SetZLayer(1); //Graphic3d_ZLayerId_Topmost
318     myHighlights.Append(aAis);
319     aContext->Display(aAis, false);
320     aContext->Deactivate(aAis);
321   }
322 }
323
324 void XGUI_ViewerProxy::eraseHighlight()
325 {
326   Handle(AIS_InteractiveContext) aContext = AISContext();
327   Handle(AIS_InteractiveObject) anAISIO;
328   AIS_ListIteratorOfListOfInteractive aLIt;
329   for (aLIt.Initialize(myHighlights); aLIt.More(); aLIt.Next()) {
330     anAISIO = aLIt.Value();
331     aContext->Remove(anAISIO, false);
332   }
333   myHighlights.Clear();
334 }
335
336 void XGUI_ViewerProxy::onMouseMove(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
337 {
338   Handle(AIS_InteractiveContext) aContext = AISContext();
339   if (!aContext.IsNull()) {
340     Handle(SelectMgr_EntityOwner) aOwner;
341     Handle(AIS_InteractiveObject) anIO;
342     bool isDisplayed = false;
343     ResultPtr aRes;
344     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
345     for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
346       aOwner = aContext->DetectedOwner();
347       anIO = Handle(AIS_InteractiveObject)::DownCast(aOwner->Selectable());
348       aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aDisplayer->getObject(anIO));
349       if (aRes.get() && (aRes != myResult)) {
350         eraseHighlight();
351         myResult = aRes;
352         displayHighlight();
353         aContext->UpdateCurrentViewer();
354       }
355       isDisplayed = aRes.get();
356     }
357     if (!isDisplayed) {
358       eraseHighlight();
359       aContext->UpdateCurrentViewer();
360       myResult = ResultPtr();
361     }
362   }
363   emit mouseMove(theWnd, theEvent);
364 }
365
366 void XGUI_ViewerProxy::onKeyPress(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
367 {
368   emit keyPress(theWnd, theEvent);
369 }
370
371 void XGUI_ViewerProxy::onKeyRelease(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
372 {
373   emit keyRelease(theWnd, theEvent);
374 }
375
376 void XGUI_ViewerProxy::onViewTransformed(AppElements_ViewWindow::OperationType theType)
377 {
378   emit viewTransformed((int) theType);
379 }
380
381 #endif
382
383
384 //***************************************
385 void XGUI_ViewerProxy::enableSelection(bool isEnabled)
386 {
387 #ifdef HAVE_SALOME
388   myWorkshop->salomeConnector()->viewer()->enableSelection(isEnabled);
389 #else
390   myWorkshop->mainWindow()->viewer()->setSelectionEnabled(isEnabled);
391 #endif
392 }
393
394 //***************************************
395 bool XGUI_ViewerProxy::isSelectionEnabled() const
396 {
397 #ifdef HAVE_SALOME
398   return myWorkshop->salomeConnector()->viewer()->isSelectionEnabled();
399 #else
400   return myWorkshop->mainWindow()->viewer()->isSelectionEnabled();
401 #endif
402 }
403
404 //***************************************
405 void XGUI_ViewerProxy::enableMultiselection(bool isEnable)
406 {
407 #ifdef HAVE_SALOME
408   myWorkshop->salomeConnector()->viewer()->enableMultiselection(isEnable);
409 #else
410   myWorkshop->mainWindow()->viewer()->setMultiSelectionEnabled(isEnable);
411 #endif
412 }
413
414 //***************************************
415 bool XGUI_ViewerProxy::isMultiSelectionEnabled() const
416 {
417 #ifdef HAVE_SALOME
418   return myWorkshop->salomeConnector()->viewer()->isMultiSelectionEnabled();
419 #else
420   return myWorkshop->mainWindow()->viewer()->isMultiSelectionEnabled();
421 #endif
422 }
423
424 //***************************************
425 bool XGUI_ViewerProxy::enableDrawMode(bool isEnabled)
426 {
427 #ifdef HAVE_SALOME
428   return myWorkshop->salomeConnector()->viewer()->enableDrawMode(isEnabled);
429 #else
430   return myWorkshop->mainWindow()->viewer()->enableDrawMode(isEnabled);
431 #endif
432 }
433
434 //***************************************
435 void XGUI_ViewerProxy::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
436 {
437   myWorkshop->displayer()->addSelectionFilter(theFilter);
438 }
439
440 //***************************************
441 void XGUI_ViewerProxy::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
442 {
443   myWorkshop->displayer()->removeSelectionFilter(theFilter);
444 }
445
446 //***************************************
447 bool XGUI_ViewerProxy::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
448 {
449   return myWorkshop->displayer()->hasSelectionFilter(theFilter);
450 }
451
452 //***************************************
453 void XGUI_ViewerProxy::clearSelectionFilters()
454 {
455   myWorkshop->displayer()->removeFilters();
456 }
457
458 //***************************************
459 void XGUI_ViewerProxy::update()
460 {
461   myWorkshop->displayer()->updateViewer();
462 }
463
464 //***************************************
465 bool XGUI_ViewerProxy::canDragByMouse() const
466 {
467   if (myWorkshop->isSalomeMode()) {
468     ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
469     return aViewer->canDragByMouse();
470   } else {
471     return true;
472   }
473 }
474
475
476 //***************************************
477 //void XGUI_ViewerProxy::Zfitall()
478 //{
479 //#ifdef HAVE_SALOME
480 //  myWorkshop->salomeConnector()->viewer()->Zfitall();
481 //#else
482 //  AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
483 //  AppElements_ViewWindow* aView = aViewer->activeViewWindow();
484 //  if (aView) {
485 //    Handle(V3d_View) aView3d = aView->v3dView();
486 //    aView3d->ZFitAll();
487 //    if (aView3d->Depth() < 0.1)
488 //      aView3d->DepthFitAll();
489 //  }
490 //#endif
491 //}