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