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