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