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>
44 #define HIGHLIGHT_COLOR Quantity_NOC_YELLOW
46 XGUI_ViewerProxy::XGUI_ViewerProxy(XGUI_Workshop* theParent)
47 : ModuleBase_IViewer(theParent),
52 void XGUI_ViewerProxy::connectViewProxy()
55 connect(myWorkshop->salomeConnector()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
56 SIGNAL(trihedronVisibilityChanged(bool)));
58 connect(myWorkshop->mainWindow()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
59 SIGNAL(trihedronVisibilityChanged(bool)));
63 Handle(AIS_InteractiveContext) XGUI_ViewerProxy::AISContext() const
65 Handle(AIS_InteractiveContext) aContext;
67 aContext = myWorkshop->salomeConnector()->viewer()->AISContext();
69 aContext = myWorkshop->mainWindow()->viewer()->AISContext();
74 Handle(AIS_Trihedron) XGUI_ViewerProxy::trihedron() const
77 return myWorkshop->salomeConnector()->viewer()->trihedron();
79 return myWorkshop->mainWindow()->viewer()->trihedron();
83 Handle(V3d_Viewer) XGUI_ViewerProxy::v3dViewer() const
86 return myWorkshop->salomeConnector()->viewer()->v3dViewer();
88 return myWorkshop->mainWindow()->viewer()->v3dViewer();
92 Handle(V3d_View) XGUI_ViewerProxy::activeView() const
95 return myWorkshop->salomeConnector()->viewer()->activeView();
97 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
98 return (aViewer->activeViewWindow()) ?
99 aViewer->activeViewWindow()->viewPortApp()->getView() :
104 QWidget* XGUI_ViewerProxy::activeViewPort() const
107 return myWorkshop->salomeConnector()->viewer()->activeViewPort();
109 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
110 return (aViewer->activeViewWindow()) ?
111 aViewer->activeViewWindow()->viewPortApp(): 0;
115 void XGUI_ViewerProxy::setViewProjection(double theX, double theY, double theZ, double theTwist)
117 Handle(V3d_View) aView3d = activeView();
118 if (!aView3d.IsNull()) {
119 aView3d->SetProj(theX, theY, theZ);
120 aView3d->SetTwist( theTwist );
121 aView3d->FitAll(0.01, false);
122 aView3d->SetZSize(0.);
123 if (aView3d->Depth() < 0.1)
124 aView3d->DepthFitAll();
128 void XGUI_ViewerProxy::fitAll()
131 myWorkshop->salomeConnector()->viewer()->fitAll();
133 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
134 if (aViewer->activeViewWindow())
135 aViewer->activeViewWindow()->viewPortApp()->fitAll();
139 void XGUI_ViewerProxy::eraseAll()
142 myWorkshop->salomeConnector()->viewer()->eraseAll();
147 void XGUI_ViewerProxy::connectToViewer()
150 ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
152 connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
153 connect(aViewer, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)),
154 this, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)));
156 connect(aViewer, SIGNAL(deleteView(ModuleBase_IViewWindow*)),
157 this, SIGNAL(deleteView(ModuleBase_IViewWindow*)));
159 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
160 this, SLOT(onViewCreated(ModuleBase_IViewWindow*)));
162 connect(aViewer, SIGNAL(activated(ModuleBase_IViewWindow*)),
163 this, SIGNAL(activated(ModuleBase_IViewWindow*)));
165 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
166 this, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)));
168 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
169 this, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
171 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)), this,
172 SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
174 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
175 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
177 connect(aViewer, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)),
178 this, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)));
180 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
181 this, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
183 connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
185 connect(aViewer, SIGNAL(viewTransformed(int)), this, SIGNAL(viewTransformed(int)));
187 connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
188 SIGNAL(contextMenuRequested(QContextMenuEvent*)));
190 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
192 connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
194 connect(aViewer, SIGNAL(tryCloseView(AppElements_ViewWindow*)),
195 this, SLOT(onTryCloseView(AppElements_ViewWindow*)));
197 connect(aViewer, SIGNAL(deleteView(AppElements_ViewWindow*)),
198 this, SLOT(onDeleteView(AppElements_ViewWindow*)));
200 connect(aViewer, SIGNAL(viewCreated(AppElements_ViewWindow*)),
201 this, SLOT(onViewCreated(AppElements_ViewWindow*)));
203 connect(aViewer, SIGNAL(activated(AppElements_ViewWindow*)),
204 this, SLOT(onActivated(AppElements_ViewWindow*)));
206 connect(aViewer, SIGNAL(mousePress(AppElements_ViewWindow*, QMouseEvent*)), this,
207 SLOT(onMousePress(AppElements_ViewWindow*, QMouseEvent*)));
209 connect(aViewer, SIGNAL(mouseRelease(AppElements_ViewWindow*, QMouseEvent*)), this,
210 SLOT(onMouseRelease(AppElements_ViewWindow*, QMouseEvent*)));
212 connect(aViewer, SIGNAL(mouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)), this,
213 SLOT(onMouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)));
215 connect(aViewer, SIGNAL(mouseMove(AppElements_ViewWindow*, QMouseEvent*)), this,
216 SLOT(onMouseMove(AppElements_ViewWindow*, QMouseEvent*)));
218 connect(aViewer, SIGNAL(keyPress(AppElements_ViewWindow*, QKeyEvent*)), this,
219 SLOT(onKeyPress(AppElements_ViewWindow*, QKeyEvent*)));
221 connect(aViewer, SIGNAL(keyRelease(AppElements_ViewWindow*, QKeyEvent*)), this,
222 SLOT(onKeyRelease(AppElements_ViewWindow*, QKeyEvent*)));
224 connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
225 connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
226 SIGNAL(contextMenuRequested(QContextMenuEvent*)));
230 bool XGUI_ViewerProxy::eventFilter(QObject *theObject, QEvent *theEvent)
232 if (theEvent->type() == QEvent::Enter) {
233 emit enterViewPort();
235 else if (theEvent->type() == QEvent::Leave) {
236 emit leaveViewPort();
238 return ModuleBase_IViewer::eventFilter(theObject, theEvent);
241 void XGUI_ViewerProxy::onViewCreated(ModuleBase_IViewWindow* theWnd)
243 theWnd->viewPort()->installEventFilter(this);
245 myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
247 emit viewCreated(theWnd);
251 void XGUI_ViewerProxy::onTryCloseView(AppElements_ViewWindow* theWnd)
253 emit tryCloseView(theWnd);
256 void XGUI_ViewerProxy::onDeleteView(AppElements_ViewWindow* theWnd)
258 if (myWindowScale.contains(theWnd->v3dView()))
259 myWindowScale.remove (theWnd->v3dView());
260 emit deleteView(theWnd);
263 void XGUI_ViewerProxy::onViewCreated(AppElements_ViewWindow* theWnd)
265 theWnd->viewPort()->installEventFilter(this);
267 connect(theWnd, SIGNAL(vpTransformationFinished(AppElements_ViewWindow::OperationType)),
268 this, SLOT(onViewTransformed(AppElements_ViewWindow::OperationType)));
270 myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
272 emit viewCreated(theWnd);
275 void XGUI_ViewerProxy::onActivated(AppElements_ViewWindow* theWnd)
277 emit activated(theWnd);
280 void XGUI_ViewerProxy::onMousePress(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
282 emit mousePress(theWnd, theEvent);
285 void XGUI_ViewerProxy::onMouseRelease(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
287 emit mouseRelease(theWnd, theEvent);
290 void XGUI_ViewerProxy::onMouseDoubleClick(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
292 emit mouseDoubleClick(theWnd, theEvent);
295 void XGUI_ViewerProxy::onMouseMove(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
298 emit mouseMove(theWnd, theEvent);
301 void XGUI_ViewerProxy::onKeyPress(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
303 emit keyPress(theWnd, theEvent);
306 void XGUI_ViewerProxy::onKeyRelease(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
308 emit keyRelease(theWnd, theEvent);
311 void XGUI_ViewerProxy::onViewTransformed(AppElements_ViewWindow::OperationType theType)
313 emit viewTransformed((int) theType);
319 //***************************************
320 void XGUI_ViewerProxy::enableSelection(bool isEnabled)
323 myWorkshop->salomeConnector()->viewer()->enableSelection(isEnabled);
325 myWorkshop->mainWindow()->viewer()->setSelectionEnabled(isEnabled);
329 //***************************************
330 bool XGUI_ViewerProxy::isSelectionEnabled() const
333 return myWorkshop->salomeConnector()->viewer()->isSelectionEnabled();
335 return myWorkshop->mainWindow()->viewer()->isSelectionEnabled();
339 //***************************************
340 void XGUI_ViewerProxy::enableMultiselection(bool isEnable)
343 myWorkshop->salomeConnector()->viewer()->enableMultiselection(isEnable);
345 myWorkshop->mainWindow()->viewer()->setMultiSelectionEnabled(isEnable);
349 //***************************************
350 bool XGUI_ViewerProxy::isMultiSelectionEnabled() const
353 return myWorkshop->salomeConnector()->viewer()->isMultiSelectionEnabled();
355 return myWorkshop->mainWindow()->viewer()->isMultiSelectionEnabled();
359 //***************************************
360 bool XGUI_ViewerProxy::enableDrawMode(bool isEnabled)
363 return myWorkshop->salomeConnector()->viewer()->enableDrawMode(isEnabled);
365 return myWorkshop->mainWindow()->viewer()->enableDrawMode(isEnabled);
369 //***************************************
370 void XGUI_ViewerProxy::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
372 myWorkshop->displayer()->addSelectionFilter(theFilter);
375 //***************************************
376 void XGUI_ViewerProxy::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
378 myWorkshop->displayer()->removeSelectionFilter(theFilter);
381 //***************************************
382 bool XGUI_ViewerProxy::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
384 return myWorkshop->displayer()->hasSelectionFilter(theFilter);
387 //***************************************
388 void XGUI_ViewerProxy::clearSelectionFilters()
390 myWorkshop->displayer()->removeFilters();
393 //***************************************
394 void XGUI_ViewerProxy::update()
396 myWorkshop->displayer()->updateViewer();
399 //***************************************
400 bool XGUI_ViewerProxy::canDragByMouse() const
402 if (myWorkshop->isSalomeMode()) {
403 ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
404 return aViewer->canDragByMouse();
410 //***************************************
411 void XGUI_ViewerProxy::displayHighlight()
413 Handle(AIS_InteractiveContext) aContext = AISContext();
416 if (myResult->groupName() == ModelAPI_ResultConstruction::group()) {
417 FeaturePtr aFeature = ModelAPI_Feature::feature(myResult);
418 if (aFeature.get()) {
419 std::list<ResultPtr> aResults = aFeature->results();
420 std::list<ResultPtr>::const_iterator aIt;
422 Handle(AIS_Shape) aAis;
423 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
425 TopoDS_Shape aTShape = aRes->shape()->impl<TopoDS_Shape>();
426 aAis = new AIS_Shape(aTShape);
427 aAis->SetColor(HIGHLIGHT_COLOR);
428 aAis->SetZLayer(1); //Graphic3d_ZLayerId_Topmost
429 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
430 aAis->Attributes()->SetDeviationCoefficient(aDeflection);
431 myHighlights.Append(aAis);
432 aContext->Display(aAis, false);
433 aContext->Deactivate(aAis);
438 TopoDS_Shape aTShape = myResult->shape()->impl<TopoDS_Shape>();
439 Handle(AIS_Shape) aAis = new AIS_Shape(aTShape);
440 aAis->SetColor(HIGHLIGHT_COLOR);
441 aAis->SetZLayer(1); //Graphic3d_ZLayerId_Topmost
442 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
443 aAis->Attributes()->SetDeviationCoefficient(aDeflection);
444 myHighlights.Append(aAis);
445 aContext->Display(aAis, false);
446 aContext->Deactivate(aAis);
450 void XGUI_ViewerProxy::eraseHighlight()
452 Handle(AIS_InteractiveContext) aContext = AISContext();
453 Handle(AIS_InteractiveObject) anAISIO;
454 AIS_ListIteratorOfListOfInteractive aLIt;
455 for (aLIt.Initialize(myHighlights); aLIt.More(); aLIt.Next()) {
456 anAISIO = aLIt.Value();
457 aContext->Remove(anAISIO, false);
459 myHighlights.Clear();
462 void XGUI_ViewerProxy::updateHighlight()
464 Handle(AIS_InteractiveContext) aContext = AISContext();
465 if (!aContext.IsNull()) {
466 Handle(SelectMgr_EntityOwner) aOwner;
467 Handle(AIS_InteractiveObject) anIO;
468 bool isDisplayed = false;
470 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
471 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
472 aOwner = aContext->DetectedOwner();
473 anIO = Handle(AIS_InteractiveObject)::DownCast(aOwner->Selectable());
474 aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aDisplayer->getObject(anIO));
475 if (aRes.get() && (aRes != myResult)) {
479 aContext->UpdateCurrentViewer();
481 isDisplayed = aRes.get();
485 aContext->UpdateCurrentViewer();
486 myResult = ResultPtr();
492 void XGUI_ViewerProxy::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
495 emit mouseMove(theWnd, theEvent);
499 //***************************************
500 //void XGUI_ViewerProxy::Zfitall()
503 // myWorkshop->salomeConnector()->viewer()->Zfitall();
505 // AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
506 // AppElements_ViewWindow* aView = aViewer->activeViewWindow();
508 // Handle(V3d_View) aView3d = aView->v3dView();
509 // aView3d->ZFitAll();
510 // if (aView3d->Depth() < 0.1)
511 // aView3d->DepthFitAll();