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 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);
254 //**********************************************
255 bool NewGeom_SalomeViewer::isSelectionEnabled() const
258 return mySelector->viewer()->isSelectionEnabled();
261 //**********************************************
262 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
265 mySelector->viewer()->enableMultiselection(isEnable);
268 //**********************************************
269 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
272 return mySelector->viewer()->isMultiSelectionEnabled();
276 //**********************************************
277 void NewGeom_SalomeViewer::fitAll()
280 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
281 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
288 //**********************************************
289 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ)
294 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
295 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
297 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
298 if (!aView3d.IsNull()) {
299 aView3d->SetProj(theX, theY, theZ);
300 aView3d->FitAll(0.01, true);
301 aView3d->SetZSize(0.);
306 //***************************************
307 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
309 Handle(AIS_InteractiveContext) aContext = AISContext();
310 if (!aContext.IsNull()) {
311 aContext->AddFilter(theFilter);
315 //***************************************
316 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
318 Handle(AIS_InteractiveContext) aContext = AISContext();
319 if (!aContext.IsNull()) {
320 aContext->RemoveFilter(theFilter);
324 //***************************************
325 bool NewGeom_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
327 bool aFoundFilter = false;
328 Handle(AIS_InteractiveContext) aContext = AISContext();
329 if (!aContext.IsNull()) {
330 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
331 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
332 for (; aIt.More() && !aFoundFilter; aIt.Next()) {
333 aFoundFilter = theFilter.Access() == aIt.Value().Access();
339 //***************************************
340 void NewGeom_SalomeViewer::clearSelectionFilters()
342 Handle(AIS_InteractiveContext) aContext = AISContext();
343 if (!aContext.IsNull()) {
344 aContext->RemoveFilters();
348 //***************************************
349 void NewGeom_SalomeViewer::update()
351 Handle(AIS_InteractiveContext) aContext = AISContext();
352 if (!aContext.IsNull()) {
353 aContext->UpdateCurrentViewer();
357 //***************************************
358 void NewGeom_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
360 emit viewTransformed((int) theType);
363 //***************************************
364 void NewGeom_SalomeViewer::activateViewer(bool toActivate)
366 if (!mySelector || !mySelector->viewer())
368 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
369 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
371 foreach (SUIT_ViewWindow* aView, aViews) {
372 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
373 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
374 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
375 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
378 foreach (SUIT_ViewWindow* aView, aViews) {
379 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
380 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
381 disconnect((OCCViewer_ViewWindow*)aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
382 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
387 void NewGeom_SalomeViewer::Zfitall()
389 if (!mySelector || !mySelector->viewer())
391 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
392 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
394 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
395 aWnd->getViewPort()->getView()->ZFitAll();