Salome HOME
Move MakeBrick*.py to examples module for availability for testing.
[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   emit mouseMove(myView, theEvent);
178 }
179
180 //**********************************************
181 bool NewGeom_SalomeViewer::canDragByMouse() const
182 {
183   OCCViewer_Viewer* aViewer = mySelector->viewer();
184   SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
185   OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
186   if (aViewWnd) {
187     return (aViewWnd->interactionStyle() == 0);
188   }
189   return true;
190 }
191
192
193 //**********************************************
194 void NewGeom_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
195 {
196   emit keyPress(myView, theEvent);
197 }
198
199 //**********************************************
200 void NewGeom_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
201 {
202   emit keyRelease(myView, theEvent);
203 }
204
205 //**********************************************
206 void NewGeom_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
207 {
208   emit tryCloseView(myView);
209 }
210
211 //**********************************************
212 void NewGeom_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
213 {
214   if(myWindowScale.contains(myView->v3dView()))
215     myWindowScale.remove(myView->v3dView());
216   emit deleteView(myView);
217 }
218
219 //**********************************************
220 void NewGeom_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
221 {
222   myView->setCurrentView(theView);
223
224   OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
225
226   OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
227   if (aWnd)
228     connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
229       this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
230
231   myWindowScale.insert (aView->getViewPort()->getView(), aView->getViewPort()->getView()->Camera()->Scale());
232
233   emit viewCreated(myView);
234 }
235
236 //**********************************************
237 void NewGeom_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
238 {
239   myView->setCurrentView(theMgr->getActiveView());
240   emit activated(myView);
241 }
242
243 //**********************************************
244 void NewGeom_SalomeViewer::enableSelection(bool isEnabled)
245 {
246   if (mySelector)
247     if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
248       mySelector->viewer()->enableSelection(isEnabled);
249   // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
250   // After this is corrected, the first row should be recommented, the last - removed
251     //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
252     //                                                    : SUIT_ViewModel::KEY_FREE);
253 }
254
255 //**********************************************
256 bool NewGeom_SalomeViewer::isSelectionEnabled() const
257 {
258   if (mySelector)
259     return mySelector->viewer()->isSelectionEnabled();
260 }
261
262 //**********************************************
263 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
264 {
265   if (mySelector)
266     mySelector->viewer()->enableMultiselection(isEnable);
267 }
268
269 //**********************************************
270 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
271 {
272   if (mySelector)
273     return mySelector->viewer()->isMultiSelectionEnabled();
274   return false;
275 }
276
277 //**********************************************
278 void NewGeom_SalomeViewer::fitAll()
279 {
280   if (mySelector) {
281     SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
282     OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
283     if (aVFrame) {
284       aVFrame->onFitAll();
285     }
286   }
287 }
288
289 //**********************************************
290 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ, double theTwist)
291 {
292   if (!mySelector) 
293     return;
294
295   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
296   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
297   if (aVFrame) {
298     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
299     if (!aView3d.IsNull()) {
300       aView3d->SetProj(theX, theY, theZ);
301       aView3d->SetTwist( theTwist );
302       aView3d->FitAll(0.01, true);
303       aView3d->SetZSize(0.);
304       aView3d->DepthFitAll();
305     }
306   }
307 }
308
309 //***************************************
310 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
311 {
312   Handle(AIS_InteractiveContext) aContext = AISContext();
313   if (!aContext.IsNull()) {
314     aContext->AddFilter(theFilter);
315   }
316 }
317
318 //***************************************
319 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
320 {
321   Handle(AIS_InteractiveContext) aContext = AISContext();
322   if (!aContext.IsNull()) {
323     aContext->RemoveFilter(theFilter);
324   }
325 }
326
327 //***************************************
328 bool NewGeom_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
329 {
330   bool aFoundFilter = false;
331   Handle(AIS_InteractiveContext) aContext = AISContext();
332   if (!aContext.IsNull()) {
333     const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
334     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
335     for (; aIt.More() && !aFoundFilter; aIt.Next()) {
336       aFoundFilter = theFilter.Access() == aIt.Value().Access();
337     }
338   }
339   return aFoundFilter;
340 }
341
342 //***************************************
343 void NewGeom_SalomeViewer::clearSelectionFilters()
344 {
345   Handle(AIS_InteractiveContext) aContext = AISContext();
346   if (!aContext.IsNull()) {
347     aContext->RemoveFilters();
348   }
349 }
350
351 //***************************************
352 void NewGeom_SalomeViewer::update()
353 {
354   Handle(AIS_InteractiveContext) aContext = AISContext();
355   if (!aContext.IsNull()) {
356     aContext->UpdateCurrentViewer();
357   }
358 }
359
360 //***************************************
361 void NewGeom_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
362 {
363   emit viewTransformed((int) theType);
364 }
365
366 //***************************************
367 void NewGeom_SalomeViewer::activateViewer(bool toActivate)
368 {
369   if (!mySelector || !mySelector->viewer())
370     return;
371   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
372   QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
373   if (toActivate) {
374     foreach (SUIT_ViewWindow* aView, aViews) {
375       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
376       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
377       connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
378         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
379     }
380   } else {
381     foreach (SUIT_ViewWindow* aView, aViews) {
382       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
383       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
384       disconnect((OCCViewer_ViewWindow*)aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
385         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
386     }
387   }
388 }
389
390 void NewGeom_SalomeViewer::Zfitall()
391 {
392   if (!mySelector || !mySelector->viewer())
393     return;
394   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
395   OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
396   if (aView) {
397     OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
398     aWnd->getViewPort()->getView()->ZFitAll();
399     aWnd->getViewPort()->getView()->DepthFitAll();
400   }
401 }