Salome HOME
8687cba0648e66485909f7e2492ff3f831f82dd5
[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_CYAN3);
301     aAis->SetZLayer(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   // vsv: Display prehighlighting of detected object
323   //Handle(AIS_InteractiveContext) aContext = AISContext();
324   //if (!aContext.IsNull()) {
325   //  Handle(SelectMgr_EntityOwner) aOwner;
326   //  Handle(AIS_InteractiveObject) anIO;
327   //  ObjectPtr aObj;
328   //  bool isDisplayed = false;
329   //  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
330   //  for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
331   //    aOwner = aContext->DetectedOwner();
332   //    anIO = Handle(AIS_InteractiveObject)::DownCast(aOwner->Selectable());
333   //    aObj = aDisplayer->getObject(anIO);
334   //    if (aObj.get()) {
335   //      FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
336   //      if (aFeature.get()) {
337   //        if (aFeature != myFeature) {
338   //          eraseHighlight();
339   //          myFeature = aFeature;
340   //          displayHighlight();
341   //          aContext->UpdateCurrentViewer();
342   //        }
343   //        isDisplayed = true;
344   //      }
345   //    }
346   //  }
347   //  if (!isDisplayed) {
348   //    eraseHighlight();
349   //    aContext->UpdateCurrentViewer();
350   //    myFeature = FeaturePtr();
351   //  }
352   //}
353   emit mouseMove(theWnd, theEvent);
354 }
355
356 void XGUI_ViewerProxy::onKeyPress(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
357 {
358   emit keyPress(theWnd, theEvent);
359 }
360
361 void XGUI_ViewerProxy::onKeyRelease(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
362 {
363   emit keyRelease(theWnd, theEvent);
364 }
365
366 void XGUI_ViewerProxy::onViewTransformed(AppElements_ViewWindow::OperationType theType)
367 {
368   emit viewTransformed((int) theType);
369 }
370
371 #endif
372
373
374 //***************************************
375 void XGUI_ViewerProxy::enableSelection(bool isEnabled)
376 {
377 #ifdef HAVE_SALOME
378   myWorkshop->salomeConnector()->viewer()->enableSelection(isEnabled);
379 #else
380   myWorkshop->mainWindow()->viewer()->setSelectionEnabled(isEnabled);
381 #endif
382 }
383
384 //***************************************
385 bool XGUI_ViewerProxy::isSelectionEnabled() const
386 {
387 #ifdef HAVE_SALOME
388   return myWorkshop->salomeConnector()->viewer()->isSelectionEnabled();
389 #else
390   return myWorkshop->mainWindow()->viewer()->isSelectionEnabled();
391 #endif
392 }
393
394 //***************************************
395 void XGUI_ViewerProxy::enableMultiselection(bool isEnable)
396 {
397 #ifdef HAVE_SALOME
398   myWorkshop->salomeConnector()->viewer()->enableMultiselection(isEnable);
399 #else
400   myWorkshop->mainWindow()->viewer()->setMultiSelectionEnabled(isEnable);
401 #endif
402 }
403
404 //***************************************
405 bool XGUI_ViewerProxy::isMultiSelectionEnabled() const
406 {
407 #ifdef HAVE_SALOME
408   return myWorkshop->salomeConnector()->viewer()->isMultiSelectionEnabled();
409 #else
410   return myWorkshop->mainWindow()->viewer()->isMultiSelectionEnabled();
411 #endif
412 }
413
414 //***************************************
415 bool XGUI_ViewerProxy::enableDrawMode(bool isEnabled)
416 {
417 #ifdef HAVE_SALOME
418   return myWorkshop->salomeConnector()->viewer()->enableDrawMode(isEnabled);
419 #else
420   return myWorkshop->mainWindow()->viewer()->enableDrawMode(isEnabled);
421 #endif
422 }
423
424 //***************************************
425 void XGUI_ViewerProxy::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
426 {
427   myWorkshop->displayer()->addSelectionFilter(theFilter);
428 }
429
430 //***************************************
431 void XGUI_ViewerProxy::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
432 {
433   myWorkshop->displayer()->removeSelectionFilter(theFilter);
434 }
435
436 //***************************************
437 bool XGUI_ViewerProxy::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
438 {
439   return myWorkshop->displayer()->hasSelectionFilter(theFilter);
440 }
441
442 //***************************************
443 void XGUI_ViewerProxy::clearSelectionFilters()
444 {
445   myWorkshop->displayer()->removeFilters();
446 }
447
448 //***************************************
449 void XGUI_ViewerProxy::update()
450 {
451   myWorkshop->displayer()->updateViewer();
452 }
453
454 //***************************************
455 bool XGUI_ViewerProxy::canDragByMouse() const
456 {
457   if (myWorkshop->isSalomeMode()) {
458     ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
459     return aViewer->canDragByMouse();
460   } else {
461     return true;
462   }
463 }
464
465
466 //***************************************
467 //void XGUI_ViewerProxy::Zfitall()
468 //{
469 //#ifdef HAVE_SALOME
470 //  myWorkshop->salomeConnector()->viewer()->Zfitall();
471 //#else
472 //  AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
473 //  AppElements_ViewWindow* aView = aViewer->activeViewWindow();
474 //  if (aView) {
475 //    Handle(V3d_View) aView3d = aView->v3dView();
476 //    aView3d->ZFitAll();
477 //    if (aView3d->Depth() < 0.1)
478 //      aView3d->DepthFitAll();
479 //  }
480 //#endif
481 //}