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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_ViewerProxy.h"
22 #include "XGUI_Workshop.h"
23 #include "XGUI_SalomeConnector.h"
24 #include "XGUI_Displayer.h"
27 #include <AppElements_MainWindow.h>
28 #include <AppElements_ViewPort.h>
29 #include <AppElements_ViewWindow.h>
30 #include <AppElements_Viewer.h>
33 #include <ModuleBase_IViewWindow.h>
34 #include <GeomAPI_Shape.h>
35 #include <ModelAPI_ResultConstruction.h>
37 #include <Config_PropManager.h>
39 #include <AIS_Shape.hxx>
40 #include <StdSelect_BRepOwner.hxx>
45 #define HIGHLIGHT_COLOR Quantity_NOC_YELLOW
47 XGUI_ViewerProxy::XGUI_ViewerProxy(XGUI_Workshop* theParent)
48 : ModuleBase_IViewer(theParent),
53 void XGUI_ViewerProxy::connectViewProxy()
56 connect(myWorkshop->salomeConnector()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
57 SIGNAL(trihedronVisibilityChanged(bool)));
59 connect(myWorkshop->mainWindow()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
60 SIGNAL(trihedronVisibilityChanged(bool)));
64 Handle(AIS_InteractiveContext) XGUI_ViewerProxy::AISContext() const
66 Handle(AIS_InteractiveContext) aContext;
68 aContext = myWorkshop->salomeConnector()->viewer()->AISContext();
70 aContext = myWorkshop->mainWindow()->viewer()->AISContext();
75 Handle(AIS_Trihedron) XGUI_ViewerProxy::trihedron() const
78 return myWorkshop->salomeConnector()->viewer()->trihedron();
80 return myWorkshop->mainWindow()->viewer()->trihedron();
84 Handle(V3d_Viewer) XGUI_ViewerProxy::v3dViewer() const
87 return myWorkshop->salomeConnector()->viewer()->v3dViewer();
89 return myWorkshop->mainWindow()->viewer()->v3dViewer();
93 Handle(V3d_View) XGUI_ViewerProxy::activeView() const
96 return myWorkshop->salomeConnector()->viewer()->activeView();
98 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
99 return (aViewer->activeViewWindow()) ?
100 aViewer->activeViewWindow()->viewPortApp()->getView() :
105 QWidget* XGUI_ViewerProxy::activeViewPort() const
108 return myWorkshop->salomeConnector()->viewer()->activeViewPort();
110 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
111 return (aViewer->activeViewWindow()) ?
112 aViewer->activeViewWindow()->viewPortApp(): 0;
116 void XGUI_ViewerProxy::setViewProjection(double theX, double theY, double theZ, double theTwist)
118 Handle(V3d_View) aView3d = activeView();
119 if (!aView3d.IsNull()) {
120 aView3d->SetProj(theX, theY, theZ);
121 aView3d->SetTwist( theTwist );
122 aView3d->FitAll(0.01, false);
123 aView3d->SetZSize(0.);
124 if (aView3d->Depth() < 0.1)
125 aView3d->DepthFitAll();
129 void XGUI_ViewerProxy::fitAll()
132 myWorkshop->salomeConnector()->viewer()->fitAll();
134 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
135 if (aViewer->activeViewWindow())
136 aViewer->activeViewWindow()->viewPortApp()->fitAll();
140 void XGUI_ViewerProxy::eraseAll()
143 myWorkshop->salomeConnector()->viewer()->eraseAll();
148 void XGUI_ViewerProxy::connectToViewer()
151 ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
153 connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
154 connect(aViewer, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)),
155 this, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)));
157 connect(aViewer, SIGNAL(deleteView(ModuleBase_IViewWindow*)),
158 this, SIGNAL(deleteView(ModuleBase_IViewWindow*)));
160 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
161 this, SLOT(onViewCreated(ModuleBase_IViewWindow*)));
163 connect(aViewer, SIGNAL(activated(ModuleBase_IViewWindow*)),
164 this, SIGNAL(activated(ModuleBase_IViewWindow*)));
166 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
167 this, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)));
169 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
170 this, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
172 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)), this,
173 SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
175 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
176 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
178 connect(aViewer, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)),
179 this, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)));
181 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
182 this, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
184 connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
186 connect(aViewer, SIGNAL(viewTransformed(int)), this, SIGNAL(viewTransformed(int)));
188 connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
189 SIGNAL(contextMenuRequested(QContextMenuEvent*)));
191 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
193 connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
195 connect(aViewer, SIGNAL(tryCloseView(AppElements_ViewWindow*)),
196 this, SLOT(onTryCloseView(AppElements_ViewWindow*)));
198 connect(aViewer, SIGNAL(deleteView(AppElements_ViewWindow*)),
199 this, SLOT(onDeleteView(AppElements_ViewWindow*)));
201 connect(aViewer, SIGNAL(viewCreated(AppElements_ViewWindow*)),
202 this, SLOT(onViewCreated(AppElements_ViewWindow*)));
204 connect(aViewer, SIGNAL(activated(AppElements_ViewWindow*)),
205 this, SLOT(onActivated(AppElements_ViewWindow*)));
207 connect(aViewer, SIGNAL(mousePress(AppElements_ViewWindow*, QMouseEvent*)), this,
208 SLOT(onMousePress(AppElements_ViewWindow*, QMouseEvent*)));
210 connect(aViewer, SIGNAL(mouseRelease(AppElements_ViewWindow*, QMouseEvent*)), this,
211 SLOT(onMouseRelease(AppElements_ViewWindow*, QMouseEvent*)));
213 connect(aViewer, SIGNAL(mouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)), this,
214 SLOT(onMouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)));
216 connect(aViewer, SIGNAL(mouseMove(AppElements_ViewWindow*, QMouseEvent*)), this,
217 SLOT(onMouseMove(AppElements_ViewWindow*, QMouseEvent*)));
219 connect(aViewer, SIGNAL(keyPress(AppElements_ViewWindow*, QKeyEvent*)), this,
220 SLOT(onKeyPress(AppElements_ViewWindow*, QKeyEvent*)));
222 connect(aViewer, SIGNAL(keyRelease(AppElements_ViewWindow*, QKeyEvent*)), this,
223 SLOT(onKeyRelease(AppElements_ViewWindow*, QKeyEvent*)));
225 connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
226 connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
227 SIGNAL(contextMenuRequested(QContextMenuEvent*)));
231 bool XGUI_ViewerProxy::eventFilter(QObject *theObject, QEvent *theEvent)
233 if (theEvent->type() == QEvent::Enter) {
234 emit enterViewPort();
236 else if (theEvent->type() == QEvent::Leave) {
237 emit leaveViewPort();
239 return ModuleBase_IViewer::eventFilter(theObject, theEvent);
242 void XGUI_ViewerProxy::onViewCreated(ModuleBase_IViewWindow* theWnd)
244 theWnd->viewPort()->installEventFilter(this);
246 myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
248 emit viewCreated(theWnd);
252 void XGUI_ViewerProxy::onTryCloseView(AppElements_ViewWindow* theWnd)
254 emit tryCloseView(theWnd);
257 void XGUI_ViewerProxy::onDeleteView(AppElements_ViewWindow* theWnd)
259 if (myWindowScale.contains(theWnd->v3dView()))
260 myWindowScale.remove (theWnd->v3dView());
261 emit deleteView(theWnd);
264 void XGUI_ViewerProxy::onViewCreated(AppElements_ViewWindow* theWnd)
266 theWnd->viewPort()->installEventFilter(this);
268 connect(theWnd, SIGNAL(vpTransformationFinished(AppElements_ViewWindow::OperationType)),
269 this, SLOT(onViewTransformed(AppElements_ViewWindow::OperationType)));
271 myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
273 emit viewCreated(theWnd);
276 void XGUI_ViewerProxy::onActivated(AppElements_ViewWindow* theWnd)
278 emit activated(theWnd);
281 void XGUI_ViewerProxy::onMousePress(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
283 emit mousePress(theWnd, theEvent);
286 void XGUI_ViewerProxy::onMouseRelease(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
288 emit mouseRelease(theWnd, theEvent);
291 void XGUI_ViewerProxy::onMouseDoubleClick(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
293 emit mouseDoubleClick(theWnd, theEvent);
296 void XGUI_ViewerProxy::onMouseMove(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
299 emit mouseMove(theWnd, theEvent);
302 void XGUI_ViewerProxy::onKeyPress(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
304 emit keyPress(theWnd, theEvent);
307 void XGUI_ViewerProxy::onKeyRelease(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
309 emit keyRelease(theWnd, theEvent);
312 void XGUI_ViewerProxy::onViewTransformed(AppElements_ViewWindow::OperationType theType)
314 emit viewTransformed((int) theType);
320 //***************************************
321 void XGUI_ViewerProxy::enableSelection(bool isEnabled)
324 myWorkshop->salomeConnector()->viewer()->enableSelection(isEnabled);
326 myWorkshop->mainWindow()->viewer()->setSelectionEnabled(isEnabled);
330 //***************************************
331 bool XGUI_ViewerProxy::isSelectionEnabled() const
334 return myWorkshop->salomeConnector()->viewer()->isSelectionEnabled();
336 return myWorkshop->mainWindow()->viewer()->isSelectionEnabled();
340 //***************************************
341 void XGUI_ViewerProxy::enableMultiselection(bool isEnable)
344 myWorkshop->salomeConnector()->viewer()->enableMultiselection(isEnable);
346 myWorkshop->mainWindow()->viewer()->setMultiSelectionEnabled(isEnable);
350 //***************************************
351 bool XGUI_ViewerProxy::isMultiSelectionEnabled() const
354 return myWorkshop->salomeConnector()->viewer()->isMultiSelectionEnabled();
356 return myWorkshop->mainWindow()->viewer()->isMultiSelectionEnabled();
360 //***************************************
361 bool XGUI_ViewerProxy::enableDrawMode(bool isEnabled)
364 return myWorkshop->salomeConnector()->viewer()->enableDrawMode(isEnabled);
366 return myWorkshop->mainWindow()->viewer()->enableDrawMode(isEnabled);
370 //***************************************
371 void XGUI_ViewerProxy::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
373 myWorkshop->displayer()->addSelectionFilter(theFilter);
376 //***************************************
377 void XGUI_ViewerProxy::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
379 myWorkshop->displayer()->removeSelectionFilter(theFilter);
382 //***************************************
383 bool XGUI_ViewerProxy::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
385 return myWorkshop->displayer()->hasSelectionFilter(theFilter);
388 //***************************************
389 void XGUI_ViewerProxy::clearSelectionFilters()
391 myWorkshop->displayer()->removeFilters();
394 //***************************************
395 void XGUI_ViewerProxy::update()
397 myWorkshop->displayer()->updateViewer();
400 //***************************************
401 bool XGUI_ViewerProxy::canDragByMouse() const
403 if (myWorkshop->isSalomeMode()) {
404 ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
405 return aViewer->canDragByMouse();
411 //***************************************
412 void XGUI_ViewerProxy::displayHighlight(FeaturePtr theFeature, const TopoDS_Shape& theIgnoreShape)
414 Handle(AIS_InteractiveContext) aContext = AISContext();
417 if (myResult->groupName() == ModelAPI_ResultConstruction::group()) {
418 //FeaturePtr aFeature = ModelAPI_Feature::feature(myResult);
419 if (theFeature.get()) {
420 std::list<ResultPtr> aResults = theFeature->results();
421 std::list<ResultPtr>::const_iterator aIt;
423 Handle(AIS_Shape) aAis;
424 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
426 TopoDS_Shape aTShape = aRes->shape()->impl<TopoDS_Shape>();
427 if (!aTShape.IsSame(theIgnoreShape)) {
428 aAis = new AIS_Shape(aTShape);
429 aAis->SetColor(HIGHLIGHT_COLOR);
430 aAis->SetZLayer(Graphic3d_ZLayerId_Top); //Graphic3d_ZLayerId_Topmost
431 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
432 aAis->Attributes()->SetDeviationCoefficient(aDeflection);
433 myHighlights.Append(aAis);
434 aContext->Display(aAis, false);
435 aContext->Deactivate(aAis);
441 TopoDS_Shape aTShape = myResult->shape()->impl<TopoDS_Shape>();
442 Handle(AIS_Shape) aAis = new AIS_Shape(aTShape);
443 aAis->SetColor(HIGHLIGHT_COLOR);
444 aAis->SetZLayer(Graphic3d_ZLayerId_Top); //Graphic3d_ZLayerId_Topmost
445 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
446 aAis->Attributes()->SetDeviationCoefficient(aDeflection);
447 myHighlights.Append(aAis);
448 aContext->Display(aAis, false);
449 aContext->Deactivate(aAis);
453 void XGUI_ViewerProxy::eraseHighlight()
455 Handle(AIS_InteractiveContext) aContext = AISContext();
456 Handle(AIS_InteractiveObject) anAISIO;
457 AIS_ListIteratorOfListOfInteractive aLIt;
458 for (aLIt.Initialize(myHighlights); aLIt.More(); aLIt.Next()) {
459 anAISIO = aLIt.Value();
460 aContext->Remove(anAISIO, false);
462 myHighlights.Clear();
465 void XGUI_ViewerProxy::updateHighlight()
467 Handle(AIS_InteractiveContext) aContext = AISContext();
468 if (!aContext.IsNull()) {
469 Handle(StdSelect_BRepOwner) aOwner;
470 Handle(AIS_InteractiveObject) anIO;
471 bool isDisplayed = false;
472 TopoDS_Shape aShape, aShp;
474 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
475 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
476 aOwner = Handle(StdSelect_BRepOwner)::DownCast(aContext->DetectedOwner());
477 if ((!aOwner.IsNull()) && aOwner->HasShape()) {
478 aShape = aOwner->Shape();
479 anIO = Handle(AIS_InteractiveObject)::DownCast(aOwner->Selectable());
480 aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aDisplayer->getObject(anIO));
481 if (aRes.get() && (aRes != myResult)) {
483 FeaturePtr aFeature = ModelAPI_Feature::feature(aRes);
484 aShp = aRes->shape()->impl<TopoDS_Shape>();
485 if ((aFeature->results().size() > 1) || (!aShp.IsSame(aShape))) {
487 displayHighlight(aFeature, aShape);
490 myResult = ResultPtr();
492 aContext->UpdateCurrentViewer();
494 isDisplayed = aRes.get();
499 aContext->UpdateCurrentViewer();
500 myResult = ResultPtr();
506 void XGUI_ViewerProxy::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
509 emit mouseMove(theWnd, theEvent);
513 //***************************************
514 //void XGUI_ViewerProxy::Zfitall()
517 // myWorkshop->salomeConnector()->viewer()->Zfitall();
519 // AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
520 // AppElements_ViewWindow* aView = aViewer->activeViewWindow();
522 // Handle(V3d_View) aView3d = aView->v3dView();
523 // aView3d->ZFitAll();
524 // if (aView3d->Depth() < 0.1)
525 // aView3d->DepthFitAll();