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