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