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