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