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