1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "NewGeom_SalomeViewer.h"
4 #include "NewGeom_OCCSelector.h"
6 #include <OCCViewer_ViewPort3d.h>
7 #include <OCCViewer_ViewFrame.h>
9 #include <SUIT_ViewManager.h>
11 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
13 #include <QMouseEvent>
14 #include <QContextMenuEvent>
16 NewGeom_SalomeView::NewGeom_SalomeView(OCCViewer_Viewer* theViewer)
17 : ModuleBase_IViewWindow(), myCurrentView(0)
23 Handle(V3d_View) NewGeom_SalomeView::v3dView() const
25 Handle(V3d_View) aView;
27 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
28 aView = aWnd->getViewPort()->getView();
33 QWidget* NewGeom_SalomeView::viewPort() const
35 QWidget* aViewPort = 0;
37 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
38 aViewPort = aWnd->getViewPort();
43 //**********************************************
44 //**********************************************
45 //**********************************************
49 NewGeom_SalomeViewer::NewGeom_SalomeViewer(QObject* theParent)
50 : ModuleBase_IViewer(theParent),
51 mySelector(0), myView(0), myIsSelectionChanged(false)
55 NewGeom_SalomeViewer::~NewGeom_SalomeViewer()
62 //**********************************************
63 Handle(AIS_InteractiveContext) NewGeom_SalomeViewer::AISContext() const
65 if (mySelector && mySelector->viewer())
66 return mySelector->viewer()->getAISContext();
67 Handle(AIS_InteractiveContext) aNull;
71 //**********************************************
72 Handle(V3d_Viewer) NewGeom_SalomeViewer::v3dViewer() const
75 return mySelector->viewer()->getViewer3d();
76 return Handle(V3d_Viewer)();
79 //**********************************************
80 Handle(V3d_View) NewGeom_SalomeViewer::activeView() const
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();
88 return Handle(V3d_View)();
91 //**********************************************
92 void NewGeom_SalomeViewer::setSelector(NewGeom_OCCSelector* theSel)
95 if (mySelector == theSel)
98 mySelector->viewer()->getViewManager()->disconnect(this);
99 OCCViewer_Viewer* aViewer = mySelector->viewer();
101 aViewer->disconnect(this);
107 OCCViewer_Viewer* aViewer = mySelector->viewer();
108 SUIT_ViewManager* aMgr = aViewer->getViewManager();
110 myView = new NewGeom_SalomeView(mySelector->viewer());
112 // TODO: Provide ModuleBase_IViewWindow interface
113 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
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*)));
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*)));
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*)));
138 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
141 //**********************************************
142 void NewGeom_SalomeViewer::onSelectionChanged()
144 // Selection event must be sent only after mouse release
145 myIsSelectionChanged = true;
148 //**********************************************
149 void NewGeom_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
151 myView->setCurrentView(theView);
152 emit mousePress(myView, theEvent);
155 //**********************************************
156 void NewGeom_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
158 myView->setCurrentView(theView);
159 emit mouseRelease(myView, theEvent);
160 if (myIsSelectionChanged) {
161 emit selectionChanged();
162 myIsSelectionChanged = false;
166 //**********************************************
167 void NewGeom_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
169 myView->setCurrentView(theView);
170 emit mouseDoubleClick(myView, theEvent);
173 //**********************************************
174 void NewGeom_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
176 myView->setCurrentView(theView);
177 emit mouseMove(myView, theEvent);
180 //**********************************************
181 bool NewGeom_SalomeViewer::canDragByMouse() const
183 OCCViewer_Viewer* aViewer = mySelector->viewer();
184 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
185 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
187 return (aViewWnd->interactionStyle() == 0);
193 //**********************************************
194 void NewGeom_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
196 emit keyPress(myView, theEvent);
199 //**********************************************
200 void NewGeom_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
202 emit keyRelease(myView, theEvent);
205 //**********************************************
206 void NewGeom_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
208 emit tryCloseView(myView);
211 //**********************************************
212 void NewGeom_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
214 if(myWindowScale.contains(myView->v3dView()))
215 myWindowScale.remove(myView->v3dView());
216 emit deleteView(myView);
219 //**********************************************
220 void NewGeom_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
222 myView->setCurrentView(theView);
224 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
226 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
228 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
229 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
231 myWindowScale.insert (aView->getViewPort()->getView(), aView->getViewPort()->getView()->Camera()->Scale());
233 emit viewCreated(myView);
236 //**********************************************
237 void NewGeom_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
239 myView->setCurrentView(theMgr->getActiveView());
240 emit activated(myView);
243 //**********************************************
244 void NewGeom_SalomeViewer::enableSelection(bool isEnabled)
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);
255 //**********************************************
256 bool NewGeom_SalomeViewer::isSelectionEnabled() const
259 return mySelector->viewer()->isSelectionEnabled();
263 //**********************************************
264 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
267 mySelector->viewer()->enableMultiselection(isEnable);
270 //**********************************************
271 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
274 return mySelector->viewer()->isMultiSelectionEnabled();
278 //**********************************************
279 bool NewGeom_SalomeViewer::enableDrawMode(bool isEnabled)
281 // TODO: Has to be replaced when SALOME patch become avialable
283 return mySelector->viewer()->enableDrawMode(isEnabled);
287 //**********************************************
288 void NewGeom_SalomeViewer::fitAll()
291 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
292 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
299 //**********************************************
300 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ, double theTwist)
305 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
306 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
308 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
309 if (!aView3d.IsNull()) {
310 aView3d->SetProj(theX, theY, theZ);
311 aView3d->SetTwist( theTwist );
312 aView3d->FitAll(0.01, true);
313 aView3d->SetZSize(0.);
314 if (aView3d->Depth() < 0.1)
315 aView3d->DepthFitAll();
320 //***************************************
321 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
323 Handle(AIS_InteractiveContext) aContext = AISContext();
324 if (!aContext.IsNull()) {
325 aContext->AddFilter(theFilter);
329 //***************************************
330 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
332 Handle(AIS_InteractiveContext) aContext = AISContext();
333 if (!aContext.IsNull()) {
334 aContext->RemoveFilter(theFilter);
338 //***************************************
339 bool NewGeom_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
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();
353 //***************************************
354 void NewGeom_SalomeViewer::clearSelectionFilters()
356 Handle(AIS_InteractiveContext) aContext = AISContext();
357 if (!aContext.IsNull()) {
358 aContext->RemoveFilters();
362 //***************************************
363 void NewGeom_SalomeViewer::update()
365 Handle(AIS_InteractiveContext) aContext = AISContext();
366 if (!aContext.IsNull()) {
367 aContext->UpdateCurrentViewer();
371 //***************************************
372 void NewGeom_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
374 emit viewTransformed((int) theType);
377 //***************************************
378 void NewGeom_SalomeViewer::activateViewer(bool toActivate)
380 if (!mySelector || !mySelector->viewer())
382 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
383 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
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)));
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)));
401 void NewGeom_SalomeViewer::Zfitall()
403 if (!mySelector || !mySelector->viewer())
405 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
406 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
408 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
409 Handle(V3d_View) aView3d = aWnd->getViewPort()->getView();
411 if (aView3d->Depth() < 0.1)
412 aView3d->DepthFitAll();