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