]> SALOME platform Git repositories - modules/shaper.git/blob - src/NewGeom/NewGeom_SalomeViewer.cpp
Salome HOME
Make extrusion sketch created on the face is created outside of the material of the...
[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     mySelector->viewer()->enableSelection(isEnabled);
248   // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
249   // After this is corrected, the first row should be recommented, the last - removed
250     //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
251     //                                                    : SUIT_ViewModel::KEY_FREE);
252 }
253
254 //**********************************************
255 bool NewGeom_SalomeViewer::isSelectionEnabled() const
256 {
257   if (mySelector)
258     return mySelector->viewer()->isSelectionEnabled();
259 }
260
261 //**********************************************
262 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
263 {
264   if (mySelector)
265     mySelector->viewer()->enableMultiselection(isEnable);
266 }
267
268 //**********************************************
269 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
270 {
271   if (mySelector)
272     return mySelector->viewer()->isMultiSelectionEnabled();
273   return false;
274 }
275
276 //**********************************************
277 void NewGeom_SalomeViewer::fitAll()
278 {
279   if (mySelector) {
280     SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
281     OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
282     if (aVFrame) {
283       aVFrame->onFitAll();
284     }
285   }
286 }
287
288 //**********************************************
289 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ, double theTwist)
290 {
291   if (!mySelector) 
292     return;
293
294   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
295   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
296   if (aVFrame) {
297     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
298     if (!aView3d.IsNull()) {
299       aView3d->SetProj(theX, theY, theZ);
300       aView3d->SetTwist( theTwist );
301       aView3d->FitAll(0.01, true);
302       aView3d->SetZSize(0.);
303     }
304   }
305 }
306
307 //***************************************
308 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
309 {
310   Handle(AIS_InteractiveContext) aContext = AISContext();
311   if (!aContext.IsNull()) {
312     aContext->AddFilter(theFilter);
313   }
314 }
315
316 //***************************************
317 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
318 {
319   Handle(AIS_InteractiveContext) aContext = AISContext();
320   if (!aContext.IsNull()) {
321     aContext->RemoveFilter(theFilter);
322   }
323 }
324
325 //***************************************
326 bool NewGeom_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
327 {
328   bool aFoundFilter = false;
329   Handle(AIS_InteractiveContext) aContext = AISContext();
330   if (!aContext.IsNull()) {
331     const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
332     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
333     for (; aIt.More() && !aFoundFilter; aIt.Next()) {
334       aFoundFilter = theFilter.Access() == aIt.Value().Access();
335     }
336   }
337   return aFoundFilter;
338 }
339
340 //***************************************
341 void NewGeom_SalomeViewer::clearSelectionFilters()
342 {
343   Handle(AIS_InteractiveContext) aContext = AISContext();
344   if (!aContext.IsNull()) {
345     aContext->RemoveFilters();
346   }
347 }
348
349 //***************************************
350 void NewGeom_SalomeViewer::update()
351 {
352   Handle(AIS_InteractiveContext) aContext = AISContext();
353   if (!aContext.IsNull()) {
354     aContext->UpdateCurrentViewer();
355   }
356 }
357
358 //***************************************
359 void NewGeom_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
360 {
361   emit viewTransformed((int) theType);
362 }
363
364 //***************************************
365 void NewGeom_SalomeViewer::activateViewer(bool toActivate)
366 {
367   if (!mySelector || !mySelector->viewer())
368     return;
369   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
370   QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
371   if (toActivate) {
372     foreach (SUIT_ViewWindow* aView, aViews) {
373       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
374       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
375       connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
376         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
377     }
378   } else {
379     foreach (SUIT_ViewWindow* aView, aViews) {
380       OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
381       OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
382       disconnect((OCCViewer_ViewWindow*)aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
383         this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
384     }
385   }
386 }
387
388 void NewGeom_SalomeViewer::Zfitall()
389 {
390   if (!mySelector || !mySelector->viewer())
391     return;
392   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
393   OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
394   if (aView) {
395     OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
396     aWnd->getViewPort()->getView()->ZFitAll();
397   }
398 }