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