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