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();
262 //**********************************************
263 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
266 mySelector->viewer()->enableMultiselection(isEnable);
269 //**********************************************
270 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
273 return mySelector->viewer()->isMultiSelectionEnabled();
277 //**********************************************
278 void NewGeom_SalomeViewer::fitAll()
281 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
282 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
289 //**********************************************
290 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ, double theTwist)
295 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
296 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
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();
309 //***************************************
310 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
312 Handle(AIS_InteractiveContext) aContext = AISContext();
313 if (!aContext.IsNull()) {
314 aContext->AddFilter(theFilter);
318 //***************************************
319 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
321 Handle(AIS_InteractiveContext) aContext = AISContext();
322 if (!aContext.IsNull()) {
323 aContext->RemoveFilter(theFilter);
327 //***************************************
328 bool NewGeom_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
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();
342 //***************************************
343 void NewGeom_SalomeViewer::clearSelectionFilters()
345 Handle(AIS_InteractiveContext) aContext = AISContext();
346 if (!aContext.IsNull()) {
347 aContext->RemoveFilters();
351 //***************************************
352 void NewGeom_SalomeViewer::update()
354 Handle(AIS_InteractiveContext) aContext = AISContext();
355 if (!aContext.IsNull()) {
356 aContext->UpdateCurrentViewer();
360 //***************************************
361 void NewGeom_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
363 emit viewTransformed((int) theType);
366 //***************************************
367 void NewGeom_SalomeViewer::activateViewer(bool toActivate)
369 if (!mySelector || !mySelector->viewer())
371 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
372 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
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)));
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)));
390 void NewGeom_SalomeViewer::Zfitall()
392 if (!mySelector || !mySelector->viewer())
394 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
395 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
397 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
398 aWnd->getViewPort()->getView()->ZFitAll();