1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "SHAPERGUI_SalomeViewer.h"
21 #include "SHAPERGUI_OCCSelector.h"
23 #include <OCCViewer_ViewPort3d.h>
24 #include <OCCViewer_ViewFrame.h>
25 #include <SOCC_ViewModel.h>
26 #include <SUIT_ViewManager.h>
28 #include <QtxActionToolMgr.h>
30 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
32 #include <QMouseEvent>
33 #include <QContextMenuEvent>
35 //#define SALOME_PATCH_FOR_CTRL_WHEEL
37 SHAPERGUI_SalomeView::SHAPERGUI_SalomeView(OCCViewer_Viewer* theViewer)
38 : ModuleBase_IViewWindow(), myCurrentView(0)
44 Handle(V3d_View) SHAPERGUI_SalomeView::v3dView() const
46 Handle(V3d_View) aView;
48 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
49 aView = aWnd->getViewPort()->getView();
54 QWidget* SHAPERGUI_SalomeView::viewPort() const
56 QWidget* aViewPort = 0;
58 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(myCurrentView);
59 aViewPort = aWnd->getViewPort();
64 //**********************************************
65 //**********************************************
66 //**********************************************
70 SHAPERGUI_SalomeViewer::SHAPERGUI_SalomeViewer(QObject* theParent)
71 : ModuleBase_IViewer(theParent),
72 mySelector(0), myView(0), myIsSelectionChanged(false)
76 SHAPERGUI_SalomeViewer::~SHAPERGUI_SalomeViewer()
83 //**********************************************
84 Handle(AIS_InteractiveContext) SHAPERGUI_SalomeViewer::AISContext() const
86 if (mySelector && mySelector->viewer())
87 return mySelector->viewer()->getAISContext();
88 Handle(AIS_InteractiveContext) aNull;
92 //**********************************************
93 Handle(V3d_Viewer) SHAPERGUI_SalomeViewer::v3dViewer() const
96 return mySelector->viewer()->getViewer3d();
97 return Handle(V3d_Viewer)();
100 //**********************************************
101 Handle(AIS_Trihedron) SHAPERGUI_SalomeViewer::trihedron() const
103 return mySelector->viewer()->getTrihedron();
106 //**********************************************
107 Handle(V3d_View) SHAPERGUI_SalomeViewer::activeView() const
110 OCCViewer_Viewer* aViewer = mySelector->viewer();
111 SUIT_ViewManager* aMgr = aViewer->getViewManager();
112 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
113 return aWnd->getViewPort()->getView();
115 return Handle(V3d_View)();
118 //**********************************************
119 QWidget* SHAPERGUI_SalomeViewer::activeViewPort() const
123 OCCViewer_Viewer* aViewer = mySelector->viewer();
124 SUIT_ViewManager* aMgr = aViewer->getViewManager();
125 OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
126 aViewPort = aWnd->getViewPort();
131 //**********************************************
132 void SHAPERGUI_SalomeViewer::setSelector(SHAPERGUI_OCCSelector* theSel)
135 if (mySelector == theSel)
138 mySelector->viewer()->getViewManager()->disconnect(this);
139 OCCViewer_Viewer* aViewer = mySelector->viewer();
141 aViewer->disconnect(this);
147 OCCViewer_Viewer* aViewer = mySelector->viewer();
148 SUIT_ViewManager* aMgr = aViewer->getViewManager();
150 myView = new SHAPERGUI_SalomeView(mySelector->viewer());
152 // TODO: Provide ModuleBase_IViewWindow interface
153 connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
155 connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)),
156 this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
157 connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)),
158 this, SLOT(onDeleteView(SUIT_ViewWindow*)));
159 connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)),
160 this, SLOT(onViewCreated(SUIT_ViewWindow*)));
161 connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)),
162 this, SLOT(onActivated(SUIT_ViewManager*)));
164 connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
165 SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
166 connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
167 SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
168 connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
169 SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
170 connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
171 SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
173 connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
174 SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
175 connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
176 SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
178 connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
181 //**********************************************
182 void SHAPERGUI_SalomeViewer::onSelectionChanged()
184 // Selection event must be sent only after mouse release
185 myIsSelectionChanged = true;
188 //**********************************************
189 void SHAPERGUI_SalomeViewer::onMousePress(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
191 myView->setCurrentView(theView);
192 emit mousePress(myView, theEvent);
195 //**********************************************
196 void SHAPERGUI_SalomeViewer::onMouseRelease(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
198 myView->setCurrentView(theView);
199 emit mouseRelease(myView, theEvent);
200 if (myIsSelectionChanged) {
201 emit selectionChanged();
202 myIsSelectionChanged = false;
206 //**********************************************
207 void SHAPERGUI_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
209 myView->setCurrentView(theView);
210 emit mouseDoubleClick(myView, theEvent);
213 //**********************************************
214 void SHAPERGUI_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
216 myView->setCurrentView(theView);
217 emit mouseMove(myView, theEvent);
220 //**********************************************
221 bool SHAPERGUI_SalomeViewer::canDragByMouse() const
223 OCCViewer_Viewer* aViewer = mySelector->viewer();
224 SUIT_ViewWindow* aWnd = aViewer->getViewManager()->getActiveView();
225 OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(aWnd);
227 return (aViewWnd->interactionStyle() == 0);
233 //**********************************************
234 void SHAPERGUI_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
236 emit keyPress(myView, theEvent);
239 //**********************************************
240 void SHAPERGUI_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
242 emit keyRelease(myView, theEvent);
245 //**********************************************
246 void SHAPERGUI_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
248 emit tryCloseView(myView);
251 //**********************************************
252 void SHAPERGUI_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
254 if(myWindowScale.contains(myView->v3dView()))
255 myWindowScale.remove(myView->v3dView());
256 emit deleteView(myView);
259 //**********************************************
260 void SHAPERGUI_SalomeViewer::onViewCreated(SUIT_ViewWindow* theView)
262 myView->setCurrentView(theView);
264 OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(theView);
266 OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
268 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
269 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
270 OCCViewer_ViewPort3d* aViewPort = aWnd->getViewPort();
272 connect(aViewPort, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewPortMapped()));
274 reconnectActions(aWnd, true);
276 myWindowScale.insert(aView->getViewPort()->getView(),
277 aView->getViewPort()->getView()->Camera()->Scale());
279 emit viewCreated(myView);
284 //**********************************************
285 void SHAPERGUI_SalomeViewer::onActivated(SUIT_ViewManager* theMgr)
287 myView->setCurrentView(theMgr->getActiveView());
288 emit activated(myView);
291 //**********************************************
292 void SHAPERGUI_SalomeViewer::enableSelection(bool isEnabled)
295 if (mySelector->viewer()->isSelectionEnabled() != isEnabled)
296 mySelector->viewer()->enableSelection(isEnabled);
297 // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
298 // After this is corrected, the first row should be recommented, the last - removed
299 //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
300 // : SUIT_ViewModel::KEY_FREE);
303 //**********************************************
304 bool SHAPERGUI_SalomeViewer::isSelectionEnabled() const
307 return mySelector->viewer()->isSelectionEnabled();
311 //**********************************************
312 void SHAPERGUI_SalomeViewer::enableMultiselection(bool isEnable)
315 mySelector->viewer()->enableMultiselection(isEnable);
318 //**********************************************
319 bool SHAPERGUI_SalomeViewer::isMultiSelectionEnabled() const
322 return mySelector->viewer()->isMultiSelectionEnabled();
326 //**********************************************
327 bool SHAPERGUI_SalomeViewer::enableDrawMode(bool isEnabled)
329 // TODO: Has to be replaced when SALOME patch become available
331 return mySelector->viewer()->enableDrawMode(isEnabled);
335 //**********************************************
336 void SHAPERGUI_SalomeViewer::reconnectActions(SUIT_ViewWindow* theWindow,
337 const bool theUseSHAPERSlot)
339 OCCViewer_ViewWindow* aWindow = dynamic_cast<OCCViewer_ViewWindow*>(theWindow);
343 QAction* anAction = theWindow->toolMgr()->action(OCCViewer_ViewWindow::TrihedronShowId);
347 if (theUseSHAPERSlot) {
348 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
349 theWindow, SLOT(onTrihedronShow(bool)));
350 anAction->connect(anAction, SIGNAL(toggled(bool)),
351 this, SIGNAL(trihedronVisibilityChanged(bool)));
354 anAction->connect(anAction, SIGNAL(toggled(bool)),
355 theWindow, SLOT(onTrihedronShow(bool)));
356 anAction->disconnect(anAction, SIGNAL(toggled(bool)),
357 this, SIGNAL(trihedronVisibilityChanged(bool)));
361 //**********************************************
362 void SHAPERGUI_SalomeViewer::fitAll()
365 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
366 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
373 //**********************************************
374 void SHAPERGUI_SalomeViewer::eraseAll()
376 SOCC_Viewer* aViewer = dynamic_cast<SOCC_Viewer*>(myView->viewer());
377 aViewer->EraseAll(0);
380 //**********************************************
381 void SHAPERGUI_SalomeViewer::setViewProjection(double theX, double theY,
382 double theZ, double theTwist)
387 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
388 OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
390 Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
391 if (!aView3d.IsNull()) {
392 aView3d->SetProj(theX, theY, theZ);
393 aView3d->SetTwist( theTwist );
394 aView3d->FitAll(0.01, false);
395 aView3d->SetZSize(0.);
396 if (aView3d->Depth() < 0.1)
397 aView3d->DepthFitAll();
402 //***************************************
403 void SHAPERGUI_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
405 Handle(AIS_InteractiveContext) aContext = AISContext();
406 if (!aContext.IsNull()) {
407 aContext->AddFilter(theFilter);
411 //***************************************
412 void SHAPERGUI_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
414 Handle(AIS_InteractiveContext) aContext = AISContext();
415 if (!aContext.IsNull()) {
416 aContext->RemoveFilter(theFilter);
420 //***************************************
421 bool SHAPERGUI_SalomeViewer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
423 bool aFoundFilter = false;
424 Handle(AIS_InteractiveContext) aContext = AISContext();
425 if (!aContext.IsNull()) {
426 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
427 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
428 for (; aIt.More() && !aFoundFilter; aIt.Next()) {
429 aFoundFilter = theFilter.get() == aIt.Value().get();
435 //***************************************
436 void SHAPERGUI_SalomeViewer::clearSelectionFilters()
438 Handle(AIS_InteractiveContext) aContext = AISContext();
439 if (!aContext.IsNull()) {
440 aContext->RemoveFilters();
444 //***************************************
445 void SHAPERGUI_SalomeViewer::update()
447 Handle(AIS_InteractiveContext) aContext = AISContext();
448 if (!aContext.IsNull()) {
449 aContext->UpdateCurrentViewer();
453 //***************************************
454 void SHAPERGUI_SalomeViewer::onViewTransformed(OCCViewer_ViewWindow::OperationType theType)
456 emit viewTransformed((int) theType);
459 //***************************************
460 void SHAPERGUI_SalomeViewer::onViewPortMapped()
462 emit trihedronVisibilityChanged(true);
465 //***************************************
466 void SHAPERGUI_SalomeViewer::activateViewer(bool toActivate)
468 if (!mySelector || !mySelector->viewer())
470 SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
471 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
472 OCCViewer_Viewer* aViewer = dynamic_cast<OCCViewer_Viewer*>(aMgr->getViewModel());
474 aViewer->setUseLocalSelection(toActivate);
476 QVector<SUIT_ViewWindow*> aViews = aMgr->getViews();
478 foreach (SUIT_ViewWindow* aView, aViews) {
479 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
480 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
481 connect(aWnd, SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
482 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
483 reconnectActions(aWnd, true);
486 foreach (SUIT_ViewWindow* aView, aViews) {
487 OCCViewer_ViewFrame* aOCCView = dynamic_cast<OCCViewer_ViewFrame*>(aView);
488 OCCViewer_ViewWindow* aWnd = aOCCView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
489 disconnect((OCCViewer_ViewWindow*)aWnd,
490 SIGNAL(vpTransformationFinished(OCCViewer_ViewWindow::OperationType)),
491 this, SLOT(onViewTransformed(OCCViewer_ViewWindow::OperationType)));
492 reconnectActions(aWnd, false);
497 //void SHAPERGUI_SalomeViewer::Zfitall()
499 // if (!mySelector || !mySelector->viewer())
501 // SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
502 // /// WORKAROUND for issue #1798. SUIT_ViewManager::closeAllViews() should nullify myActiveView
503 // /// As a result, we need to check views count in manager
504 // if (aMgr->getViews().size() > 0) {
505 // OCCViewer_ViewFrame* aView = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
507 // OCCViewer_ViewWindow* aWnd = aView->getView(OCCViewer_ViewFrame::MAIN_VIEW);
508 // Handle(V3d_View) aView3d = aWnd->getViewPort()->getView();
509 // aView3d->ZFitAll();
510 // if (aView3d->Depth() < 0.1)
511 // aView3d->DepthFitAll();