Salome HOME
Merge branch 'master' into cgt/devCEA
[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
35 #include <QEvent>
36
37 XGUI_ViewerProxy::XGUI_ViewerProxy(XGUI_Workshop* theParent)
38     : ModuleBase_IViewer(theParent),
39       myWorkshop(theParent)
40 {
41 }
42
43 void XGUI_ViewerProxy::connectViewProxy()
44 {
45 #ifdef HAVE_SALOME
46   connect(myWorkshop->salomeConnector()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
47          SIGNAL(trihedronVisibilityChanged(bool)));
48 #else
49   connect(myWorkshop->mainWindow()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
50          SIGNAL(trihedronVisibilityChanged(bool)));
51 #endif
52 }
53
54 Handle(AIS_InteractiveContext) XGUI_ViewerProxy::AISContext() const
55 {
56   Handle(AIS_InteractiveContext) aContext;
57 #ifdef HAVE_SALOME
58   aContext = myWorkshop->salomeConnector()->viewer()->AISContext();
59 #else
60   aContext = myWorkshop->mainWindow()->viewer()->AISContext();
61 #endif
62   return aContext;
63 }
64
65 Handle(AIS_Trihedron) XGUI_ViewerProxy::trihedron() const
66 {
67 #ifdef HAVE_SALOME
68   return myWorkshop->salomeConnector()->viewer()->trihedron();
69 #else
70   return myWorkshop->mainWindow()->viewer()->trihedron();
71 #endif
72 }
73
74 Handle(V3d_Viewer) XGUI_ViewerProxy::v3dViewer() const
75 {
76 #ifdef HAVE_SALOME
77   return myWorkshop->salomeConnector()->viewer()->v3dViewer();
78 #else
79   return myWorkshop->mainWindow()->viewer()->v3dViewer();
80 #endif
81 }
82
83 Handle(V3d_View) XGUI_ViewerProxy::activeView() const
84 {
85 #ifdef HAVE_SALOME
86   return myWorkshop->salomeConnector()->viewer()->activeView();
87 #else
88   AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
89   return (aViewer->activeViewWindow()) ?
90     aViewer->activeViewWindow()->viewPortApp()->getView() :
91     Handle(V3d_View)();
92 #endif
93 }
94
95 QWidget* XGUI_ViewerProxy::activeViewPort() const
96 {
97 #ifdef HAVE_SALOME
98   return myWorkshop->salomeConnector()->viewer()->activeViewPort();
99 #else
100   AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
101   return (aViewer->activeViewWindow()) ?
102          aViewer->activeViewWindow()->viewPortApp(): 0;
103 #endif
104 }
105
106 void XGUI_ViewerProxy::setViewProjection(double theX, double theY, double theZ, double theTwist)
107 {
108   Handle(V3d_View) aView3d = activeView();
109   if (!aView3d.IsNull()) {
110     aView3d->SetProj(theX, theY, theZ);
111     aView3d->SetTwist( theTwist );
112     aView3d->FitAll(0.01, false);
113     aView3d->SetZSize(0.);
114     if (aView3d->Depth() < 0.1)
115       aView3d->DepthFitAll();
116   }
117 }
118
119 void XGUI_ViewerProxy::fitAll()
120 {
121 #ifdef HAVE_SALOME
122   myWorkshop->salomeConnector()->viewer()->fitAll();
123 #else
124   AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
125   if (aViewer->activeViewWindow())
126     aViewer->activeViewWindow()->viewPortApp()->fitAll();
127 #endif
128 }
129
130 void XGUI_ViewerProxy::eraseAll()
131 {
132 #ifdef HAVE_SALOME
133   myWorkshop->salomeConnector()->viewer()->eraseAll();
134 #else
135 #endif
136 }
137
138 void XGUI_ViewerProxy::connectToViewer()
139 {
140 #ifdef HAVE_SALOME
141   ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
142
143   connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
144   connect(aViewer, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)),
145     this, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)));
146
147   connect(aViewer, SIGNAL(deleteView(ModuleBase_IViewWindow*)),
148     this, SIGNAL(deleteView(ModuleBase_IViewWindow*)));
149
150   connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
151     this, SLOT(onViewCreated(ModuleBase_IViewWindow*)));
152
153   connect(aViewer, SIGNAL(activated(ModuleBase_IViewWindow*)),
154     this, SIGNAL(activated(ModuleBase_IViewWindow*)));
155
156   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
157     this, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)));
158
159   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
160     this, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
161
162   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)), this,
163           SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
164
165   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
166     this, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
167
168   connect(aViewer, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)),
169     this, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)));
170
171   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
172     this, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
173
174   connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
175
176   connect(aViewer, SIGNAL(viewTransformed(int)), this, SIGNAL(viewTransformed(int)));
177
178   connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
179           SIGNAL(contextMenuRequested(QContextMenuEvent*)));
180 #else
181   AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
182
183   connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
184
185   connect(aViewer, SIGNAL(tryCloseView(AppElements_ViewWindow*)),
186           this, SLOT(onTryCloseView(AppElements_ViewWindow*)));
187
188   connect(aViewer, SIGNAL(deleteView(AppElements_ViewWindow*)),
189           this, SLOT(onDeleteView(AppElements_ViewWindow*)));
190
191   connect(aViewer, SIGNAL(viewCreated(AppElements_ViewWindow*)),
192           this, SLOT(onViewCreated(AppElements_ViewWindow*)));
193
194   connect(aViewer, SIGNAL(activated(AppElements_ViewWindow*)),
195           this, SLOT(onActivated(AppElements_ViewWindow*)));
196
197   connect(aViewer, SIGNAL(mousePress(AppElements_ViewWindow*, QMouseEvent*)), this,
198           SLOT(onMousePress(AppElements_ViewWindow*, QMouseEvent*)));
199
200   connect(aViewer, SIGNAL(mouseRelease(AppElements_ViewWindow*, QMouseEvent*)), this,
201           SLOT(onMouseRelease(AppElements_ViewWindow*, QMouseEvent*)));
202
203   connect(aViewer, SIGNAL(mouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)), this,
204           SLOT(onMouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)));
205
206   connect(aViewer, SIGNAL(mouseMove(AppElements_ViewWindow*, QMouseEvent*)), this,
207           SLOT(onMouseMove(AppElements_ViewWindow*, QMouseEvent*)));
208
209   connect(aViewer, SIGNAL(keyPress(AppElements_ViewWindow*, QKeyEvent*)), this,
210           SLOT(onKeyPress(AppElements_ViewWindow*, QKeyEvent*)));
211
212   connect(aViewer, SIGNAL(keyRelease(AppElements_ViewWindow*, QKeyEvent*)), this,
213           SLOT(onKeyRelease(AppElements_ViewWindow*, QKeyEvent*)));
214
215   connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
216   connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
217           SIGNAL(contextMenuRequested(QContextMenuEvent*)));
218 #endif
219 }
220
221 bool XGUI_ViewerProxy::eventFilter(QObject *theObject, QEvent *theEvent)
222 {
223   if (theEvent->type() == QEvent::Enter) {
224     emit enterViewPort();
225   }
226   else if (theEvent->type() == QEvent::Leave) {
227     emit leaveViewPort();
228   }
229   return ModuleBase_IViewer::eventFilter(theObject, theEvent);
230 }
231
232 void XGUI_ViewerProxy::onViewCreated(ModuleBase_IViewWindow* theWnd)
233 {
234   theWnd->viewPort()->installEventFilter(this);
235
236   myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
237
238   emit viewCreated(theWnd);
239 }
240
241 #ifndef HAVE_SALOME
242 void XGUI_ViewerProxy::onTryCloseView(AppElements_ViewWindow* theWnd)
243 {
244   emit tryCloseView(theWnd);
245 }
246
247 void XGUI_ViewerProxy::onDeleteView(AppElements_ViewWindow* theWnd)
248 {
249   if (myWindowScale.contains(theWnd->v3dView()))
250     myWindowScale.remove (theWnd->v3dView());
251   emit deleteView(theWnd);
252 }
253
254 void XGUI_ViewerProxy::onViewCreated(AppElements_ViewWindow* theWnd)
255 {
256   theWnd->viewPort()->installEventFilter(this);
257
258   connect(theWnd, SIGNAL(vpTransformationFinished(AppElements_ViewWindow::OperationType)),
259     this, SLOT(onViewTransformed(AppElements_ViewWindow::OperationType)));
260
261   myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
262
263   emit viewCreated(theWnd);
264 }
265
266 void XGUI_ViewerProxy::onActivated(AppElements_ViewWindow* theWnd)
267 {
268   emit activated(theWnd);
269 }
270
271 void XGUI_ViewerProxy::onMousePress(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
272 {
273   emit mousePress(theWnd, theEvent);
274 }
275
276 void XGUI_ViewerProxy::onMouseRelease(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
277 {
278   emit mouseRelease(theWnd, theEvent);
279 }
280
281 void XGUI_ViewerProxy::onMouseDoubleClick(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
282 {
283   emit mouseDoubleClick(theWnd, theEvent);
284 }
285
286 void XGUI_ViewerProxy::onMouseMove(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
287 {
288   emit mouseMove(theWnd, theEvent);
289 }
290
291 void XGUI_ViewerProxy::onKeyPress(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
292 {
293   emit keyPress(theWnd, theEvent);
294 }
295
296 void XGUI_ViewerProxy::onKeyRelease(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
297 {
298   emit keyRelease(theWnd, theEvent);
299 }
300
301 void XGUI_ViewerProxy::onViewTransformed(AppElements_ViewWindow::OperationType theType)
302 {
303   emit viewTransformed((int) theType);
304 }
305
306 #endif
307
308
309 //***************************************
310 void XGUI_ViewerProxy::enableSelection(bool isEnabled)
311 {
312 #ifdef HAVE_SALOME
313   myWorkshop->salomeConnector()->viewer()->enableSelection(isEnabled);
314 #else
315   myWorkshop->mainWindow()->viewer()->setSelectionEnabled(isEnabled);
316 #endif
317 }
318
319 //***************************************
320 bool XGUI_ViewerProxy::isSelectionEnabled() const
321 {
322 #ifdef HAVE_SALOME
323   return myWorkshop->salomeConnector()->viewer()->isSelectionEnabled();
324 #else
325   return myWorkshop->mainWindow()->viewer()->isSelectionEnabled();
326 #endif
327 }
328
329 //***************************************
330 void XGUI_ViewerProxy::enableMultiselection(bool isEnable)
331 {
332 #ifdef HAVE_SALOME
333   myWorkshop->salomeConnector()->viewer()->enableMultiselection(isEnable);
334 #else
335   myWorkshop->mainWindow()->viewer()->setMultiSelectionEnabled(isEnable);
336 #endif
337 }
338
339 //***************************************
340 bool XGUI_ViewerProxy::isMultiSelectionEnabled() const
341 {
342 #ifdef HAVE_SALOME
343   return myWorkshop->salomeConnector()->viewer()->isMultiSelectionEnabled();
344 #else
345   return myWorkshop->mainWindow()->viewer()->isMultiSelectionEnabled();
346 #endif
347 }
348
349 //***************************************
350 bool XGUI_ViewerProxy::enableDrawMode(bool isEnabled)
351 {
352 #ifdef HAVE_SALOME
353   return myWorkshop->salomeConnector()->viewer()->enableDrawMode(isEnabled);
354 #else
355   return myWorkshop->mainWindow()->viewer()->enableDrawMode(isEnabled);
356 #endif
357 }
358
359 //***************************************
360 void XGUI_ViewerProxy::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
361 {
362   myWorkshop->displayer()->addSelectionFilter(theFilter);
363 }
364
365 //***************************************
366 void XGUI_ViewerProxy::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
367 {
368   myWorkshop->displayer()->removeSelectionFilter(theFilter);
369 }
370
371 //***************************************
372 bool XGUI_ViewerProxy::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
373 {
374   return myWorkshop->displayer()->hasSelectionFilter(theFilter);
375 }
376
377 //***************************************
378 void XGUI_ViewerProxy::clearSelectionFilters()
379 {
380   myWorkshop->displayer()->removeFilters();
381 }
382
383 //***************************************
384 void XGUI_ViewerProxy::update()
385 {
386   myWorkshop->displayer()->updateViewer();
387 }
388
389 //***************************************
390 bool XGUI_ViewerProxy::canDragByMouse() const
391 {
392   if (myWorkshop->isSalomeMode()) {
393     ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
394     return aViewer->canDragByMouse();
395   } else {
396     return true;
397   }
398 }
399
400
401 //***************************************
402 //void XGUI_ViewerProxy::Zfitall()
403 //{
404 //#ifdef HAVE_SALOME
405 //  myWorkshop->salomeConnector()->viewer()->Zfitall();
406 //#else
407 //  AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
408 //  AppElements_ViewWindow* aView = aViewer->activeViewWindow();
409 //  if (aView) {
410 //    Handle(V3d_View) aView3d = aView->v3dView();
411 //    aView3d->ZFitAll();
412 //    if (aView3d->Depth() < 0.1)
413 //      aView3d->DepthFitAll();
414 //  }
415 //#endif
416 //}