Salome HOME
SQUISH test for refs #576
[modules/shaper.git] / src / NewGeom / NewGeom_SalomeViewer.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "NewGeom_SalomeViewer.h"
4 #include "NewGeom_OCCSelector.h"
5
6 #include <OCCViewer_ViewPort3d.h>
7 #include <OCCViewer_ViewFrame.h>
8
9 #include <SUIT_ViewManager.h>
10
11 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
12
13 #include <QMouseEvent>
14 #include <QContextMenuEvent>
15
16 NewGeom_SalomeView::NewGeom_SalomeView(OCCViewer_Viewer* theViewer)
17 : ModuleBase_IViewWindow(), myCurrentView(0)
18 {
19   myViewer = theViewer;
20 }
21
22
23 Handle(V3d_View) NewGeom_SalomeView::v3dView() const
24 {
25   Handle(V3d_View) aView;
26   if (myCurrentView) {
27     OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
28     aView = aWnd->getViewPort()->getView();
29   }
30   return aView;
31 }
32
33 QWidget* NewGeom_SalomeView::viewPort() const
34 {
35   QWidget* aViewPort = 0;
36   if (myCurrentView) {
37     OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
38     aViewPort = aWnd->getViewPort();
39   }
40   return aViewPort;
41 }
42
43 //**********************************************
44 //**********************************************
45 //**********************************************
46
47
48
49 NewGeom_SalomeViewer::NewGeom_SalomeViewer(QObject* theParent)
50     : ModuleBase_IViewer(theParent),
51       mySelector(0), myView(0), myIsSelectionChanged(false)
52 {
53 }
54
55 NewGeom_SalomeViewer::~NewGeom_SalomeViewer()
56 {
57   if (myView)
58     delete myView;
59 }
60
61
62 //**********************************************
63 Handle(AIS_InteractiveContext) NewGeom_SalomeViewer::AISContext() const
64 {
65   if (mySelector && mySelector->viewer())
66     return mySelector->viewer()->getAISContext();
67   Handle(AIS_InteractiveContext) aNull;
68   return aNull;
69 }
70
71 //**********************************************
72 Handle(V3d_Viewer) NewGeom_SalomeViewer::v3dViewer() const
73 {
74   if (mySelector)
75     return mySelector->viewer()->getViewer3d();
76   return Handle(V3d_Viewer)();
77 }
78
79 //**********************************************
80 Handle(V3d_View) NewGeom_SalomeViewer::activeView() const
81 {
82   if (mySelector) {
83     OCCViewer_Viewer* aViewer = mySelector->viewer();
84     SUIT_ViewManager* aMgr = aViewer->getViewManager();
85     OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
86     return aWnd->getViewPort()->getView();
87   }
88   return Handle(V3d_View)();
89 }
90
91 //**********************************************
92 void NewGeom_SalomeViewer::setSelector(NewGeom_OCCSelector* theSel)
93 {
94   if (mySelector) {
95     if (mySelector == theSel)
96       return;
97     else {
98       mySelector->viewer()->getViewManager()->disconnect(this);
99       OCCViewer_Viewer* aViewer = mySelector->viewer();
100       if (aViewer)
101         aViewer->disconnect(this);
102     }
103   }
104   mySelector = theSel;
105   if (!mySelector)
106     return;
107   OCCViewer_Viewer* aViewer = mySelector->viewer();
108   SUIT_ViewManager* aMgr = aViewer->getViewManager();
109
110   myView = new NewGeom_SalomeView(mySelector->viewer());
111
112   // TODO: Provide ModuleBase_IViewWindow interface
113   connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
114
115   connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)), 
116           this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
117   connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)), 
118           this, SLOT(onDeleteView(SUIT_ViewWindow*)));
119   connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)), 
120           this, SLOT(onViewCreated(SUIT_ViewWindow*)));
121   connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)), 
122           this, SLOT(onActivated(SUIT_ViewManager*)));
123
124   connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
125           SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
126   connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
127           SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
128   connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
129           SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
130   connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
131           SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
132
133   connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
134           SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
135   connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
136           SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
137
138   connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
139 }
140
141 //**********************************************
142 void NewGeom_SalomeViewer::onSelectionChanged()
143 {
144   // Selection event must be sent only after mouse release
145   myIsSelectionChanged = true;
146 }
147
148 //**********************************************
149 void NewGeom_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
150 {
151   myView->setCurrentView(theView);
152   emit mousePress(myView, theEvent);
153 }
154
155 //**********************************************
156 void NewGeom_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
157 {
158   myView->setCurrentView(theView);
159   emit mouseRelease(myView, theEvent);
160   if (myIsSelectionChanged) {
161     emit selectionChanged();
162     myIsSelectionChanged = false;
163   }
164 }
165
166 //**********************************************
167 void NewGeom_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
168 {
169   myView->setCurrentView(theView);
170   emit mouseDoubleClick(myView, theEvent);
171 }
172
173 //**********************************************
174 void NewGeom_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
175 {
176   myView->setCurrentView(theView);
177   OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(theView);
178   Handle(AIS_InteractiveContext) aContext = AISContext();
179   if (aContext->HasDetected()) // Set focus to provide key events in the view
180     aViewWnd->getViewPort()->setFocus(Qt::MouseFocusReason);
181   emit mouseMove(myView, theEvent);
182 }
183
184 //**********************************************
185 bool NewGeom_SalomeViewer::canDragByMouse() const
186 {
187   OCCViewer_Viewer* aViewer = mySelector->viewer();
188   SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
189   OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
190   if (aViewWnd) {
191     return (aViewWnd->interactionStyle() == 0);
192   }
193   return true;
194 }
195
196
197 //**********************************************
198 void NewGeom_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
199 {
200   OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(theView);
201   Handle(AIS_InteractiveContext) aContext = AISContext();
202   Handle(V3d_View) aView = aViewWnd->getViewPort()->getView();
203
204   bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
205   if ((theEvent->key() == Qt::Key_N) && noModifiers) {
206     aContext->HilightNextDetected(aView);
207   } else if ((theEvent->key() == Qt::Key_P) && noModifiers) {
208     aContext->HilightPreviousDetected(aView);
209   }
210   emit keyPress(myView, theEvent);
211 }
212
213 //**********************************************
214 void NewGeom_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
215 {
216   emit keyRelease(myView, theEvent);
217 }
218
219 //**********************************************
220 void NewGeom_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
221 {
222   emit tryCloseView(myView);
223 }
224
225 //**********************************************
226 void NewGeom_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
227 {
228   if(myWindowScale.contains(myView->v3dView()))
229     myWindowScale.remove(myView->v3dView());
230   emit deleteView(myView);
231 }
232
233 //**********************************************
234 void NewGeom_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
235 {
236   myView->setCurrentView(theView);
237
238   OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
239
240   OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
241   if (aWnd)
242     connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
243       this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
244
245   myWindowScale.insert (aView->getViewPort()->getView(), aView->getViewPort()->getView()->Camera()->Scale());
246
247   emit viewCreated(myView);
248 }
249
250 //**********************************************
251 void NewGeom_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
252 {
253   myView->setCurrentView(theMgr->getActiveView());
254   emit activated(myView);
255 }
256
257 //**********************************************
258 void NewGeom_SalomeViewer::enableSelection(bool isEnabled)
259 {
260   if (mySelector)
261     mySelector->viewer()->enableSelection(isEnabled);
262   // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
263   // After this is corrected, the first row should be recommented, the last - removed
264     //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
265     //                                                    : SUIT_ViewModel::KEY_FREE);
266 }
267
268 //**********************************************
269 bool NewGeom_SalomeViewer::isSelectionEnabled() const
270 {
271   if (mySelector)
272     return mySelector->viewer()->isSelectionEnabled();
273 }
274
275 //**********************************************
276 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
277 {
278   if (mySelector)
279     mySelector->viewer()->enableMultiselection(isEnable);
280 }
281
282 //**********************************************
283 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
284 {
285   if (mySelector)
286     return mySelector->viewer()->isMultiSelectionEnabled();
287   return false;
288 }
289
290 //**********************************************
291 void NewGeom_SalomeViewer::fitAll()
292 {
293   if (mySelector) {
294     SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
295     OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
296     if (aVFrame) {
297       aVFrame->onFitAll();
298     }
299   }
300 }
301
302 //**********************************************
303 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ)
304 {
305   if (!mySelector) 
306     return;
307
308   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
309   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
310   if (aVFrame) {
311     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
312     if (!aView3d.IsNull()) {
313       aView3d->SetProj(theX, theY, theZ);
314       aView3d->FitAll(0.01, true);
315       aView3d->SetZSize(0.);
316     }
317   }
318 }
319
320 //***************************************
321 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
322 {
323   Handle(AIS_InteractiveContext) aContext = AISContext();
324   if (!aContext.IsNull()) {
325     aContext->AddFilter(theFilter);
326   }
327 }
328
329 //***************************************
330 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
331 {
332   Handle(AIS_InteractiveContext) aContext = AISContext();
333   if (!aContext.IsNull()) {
334     aContext->RemoveFilter(theFilter);
335   }
336 }
337
338 //***************************************
339 bool NewGeom_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
340 {
341   bool aFoundFilter = false;
342   Handle(AIS_InteractiveContext) aContext = AISContext();
343   if (!aContext.IsNull()) {
344     const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
345     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
346     for (; aIt.More() && !aFoundFilter; aIt.Next()) {
347       aFoundFilter = theFilter.Access() == aIt.Value().Access();
348     }
349   }
350   return aFoundFilter;
351 }
352
353 //***************************************
354 void NewGeom_SalomeViewer::clearSelectionFilters()
355 {
356   Handle(AIS_InteractiveContext) aContext = AISContext();
357   if (!aContext.IsNull()) {
358     aContext->RemoveFilters();
359   }
360 }
361
362 //***************************************
363 void NewGeom_SalomeViewer::update()
364 {
365   Handle(AIS_InteractiveContext) aContext = AISContext();
366   if (!aContext.IsNull()) {
367     aContext->UpdateCurrentViewer();
368   }
369 }
370
371 //***************************************
372 void NewGeom_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
373 {
374   emit viewTransformed((int) theType);
375 }
376
377 //***************************************
378 void NewGeom_SalomeViewer::activateViewer(bool toActivate)
379 {
380   if (!mySelector || !mySelector->viewer())
381     return;
382   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
383   QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
384   if (toActivate) {
385     foreach (SUIT_ViewWindow* aView, aViews) {
386       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
387       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
388       connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
389         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
390     }
391   } else {
392     foreach (SUIT_ViewWindow* aView, aViews) {
393       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
394       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
395       disconnect((OCCViewer_ViewWindow*)aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
396         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
397     }
398   }
399 }
400
401 void NewGeom_SalomeViewer::Zfitall()
402 {
403   if (!mySelector || !mySelector->viewer())
404     return;
405   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
406   OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
407   if (aView) {
408     OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
409     aWnd->getViewPort()->getView()->ZFitAll();
410   }
411 }