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