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