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 <AIS_Shape.hxx>
42 #define HIGHLIGHT_COLOR Quantity_NOC_YELLOW
44 XGUI_ViewerProxy::XGUI_ViewerProxy(XGUI_Workshop* theParent)
45 : ModuleBase_IViewer(theParent),
50 void XGUI_ViewerProxy::connectViewProxy()
53 connect(myWorkshop->salomeConnector()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
54 SIGNAL(trihedronVisibilityChanged(bool)));
56 connect(myWorkshop->mainWindow()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
57 SIGNAL(trihedronVisibilityChanged(bool)));
61 Handle(AIS_InteractiveContext) XGUI_ViewerProxy::AISContext() const
63 Handle(AIS_InteractiveContext) aContext;
65 aContext = myWorkshop->salomeConnector()->viewer()->AISContext();
67 aContext = myWorkshop->mainWindow()->viewer()->AISContext();
72 Handle(AIS_Trihedron) XGUI_ViewerProxy::trihedron() const
75 return myWorkshop->salomeConnector()->viewer()->trihedron();
77 return myWorkshop->mainWindow()->viewer()->trihedron();
81 Handle(V3d_Viewer) XGUI_ViewerProxy::v3dViewer() const
84 return myWorkshop->salomeConnector()->viewer()->v3dViewer();
86 return myWorkshop->mainWindow()->viewer()->v3dViewer();
90 Handle(V3d_View) XGUI_ViewerProxy::activeView() const
93 return myWorkshop->salomeConnector()->viewer()->activeView();
95 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
96 return (aViewer->activeViewWindow()) ?
97 aViewer->activeViewWindow()->viewPortApp()->getView() :
102 QWidget* XGUI_ViewerProxy::activeViewPort() const
105 return myWorkshop->salomeConnector()->viewer()->activeViewPort();
107 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
108 return (aViewer->activeViewWindow()) ?
109 aViewer->activeViewWindow()->viewPortApp(): 0;
113 void XGUI_ViewerProxy::setViewProjection(double theX, double theY, double theZ, double theTwist)
115 Handle(V3d_View) aView3d = activeView();
116 if (!aView3d.IsNull()) {
117 aView3d->SetProj(theX, theY, theZ);
118 aView3d->SetTwist( theTwist );
119 aView3d->FitAll(0.01, false);
120 aView3d->SetZSize(0.);
121 if (aView3d->Depth() < 0.1)
122 aView3d->DepthFitAll();
126 void XGUI_ViewerProxy::fitAll()
129 myWorkshop->salomeConnector()->viewer()->fitAll();
131 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
132 if (aViewer->activeViewWindow())
133 aViewer->activeViewWindow()->viewPortApp()->fitAll();
137 void XGUI_ViewerProxy::eraseAll()
140 myWorkshop->salomeConnector()->viewer()->eraseAll();
145 void XGUI_ViewerProxy::connectToViewer()
148 ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
150 connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
151 connect(aViewer, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)),
152 this, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)));
154 connect(aViewer, SIGNAL(deleteView(ModuleBase_IViewWindow*)),
155 this, SIGNAL(deleteView(ModuleBase_IViewWindow*)));
157 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
158 this, SLOT(onViewCreated(ModuleBase_IViewWindow*)));
160 connect(aViewer, SIGNAL(activated(ModuleBase_IViewWindow*)),
161 this, SIGNAL(activated(ModuleBase_IViewWindow*)));
163 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
164 this, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)));
166 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
167 this, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
169 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)), this,
170 SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
172 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
173 this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
175 connect(aViewer, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)),
176 this, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)));
178 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
179 this, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
181 connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
183 connect(aViewer, SIGNAL(viewTransformed(int)), this, SIGNAL(viewTransformed(int)));
185 connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
186 SIGNAL(contextMenuRequested(QContextMenuEvent*)));
188 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
190 connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
192 connect(aViewer, SIGNAL(tryCloseView(AppElements_ViewWindow*)),
193 this, SLOT(onTryCloseView(AppElements_ViewWindow*)));
195 connect(aViewer, SIGNAL(deleteView(AppElements_ViewWindow*)),
196 this, SLOT(onDeleteView(AppElements_ViewWindow*)));
198 connect(aViewer, SIGNAL(viewCreated(AppElements_ViewWindow*)),
199 this, SLOT(onViewCreated(AppElements_ViewWindow*)));
201 connect(aViewer, SIGNAL(activated(AppElements_ViewWindow*)),
202 this, SLOT(onActivated(AppElements_ViewWindow*)));
204 connect(aViewer, SIGNAL(mousePress(AppElements_ViewWindow*, QMouseEvent*)), this,
205 SLOT(onMousePress(AppElements_ViewWindow*, QMouseEvent*)));
207 connect(aViewer, SIGNAL(mouseRelease(AppElements_ViewWindow*, QMouseEvent*)), this,
208 SLOT(onMouseRelease(AppElements_ViewWindow*, QMouseEvent*)));
210 connect(aViewer, SIGNAL(mouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)), this,
211 SLOT(onMouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)));
213 connect(aViewer, SIGNAL(mouseMove(AppElements_ViewWindow*, QMouseEvent*)), this,
214 SLOT(onMouseMove(AppElements_ViewWindow*, QMouseEvent*)));
216 connect(aViewer, SIGNAL(keyPress(AppElements_ViewWindow*, QKeyEvent*)), this,
217 SLOT(onKeyPress(AppElements_ViewWindow*, QKeyEvent*)));
219 connect(aViewer, SIGNAL(keyRelease(AppElements_ViewWindow*, QKeyEvent*)), this,
220 SLOT(onKeyRelease(AppElements_ViewWindow*, QKeyEvent*)));
222 connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
223 connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
224 SIGNAL(contextMenuRequested(QContextMenuEvent*)));
228 bool XGUI_ViewerProxy::eventFilter(QObject *theObject, QEvent *theEvent)
230 if (theEvent->type() == QEvent::Enter) {
231 emit enterViewPort();
233 else if (theEvent->type() == QEvent::Leave) {
234 emit leaveViewPort();
236 return ModuleBase_IViewer::eventFilter(theObject, theEvent);
239 void XGUI_ViewerProxy::onViewCreated(ModuleBase_IViewWindow* theWnd)
241 theWnd->viewPort()->installEventFilter(this);
243 myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
245 emit viewCreated(theWnd);
249 void XGUI_ViewerProxy::onTryCloseView(AppElements_ViewWindow* theWnd)
251 emit tryCloseView(theWnd);
254 void XGUI_ViewerProxy::onDeleteView(AppElements_ViewWindow* theWnd)
256 if (myWindowScale.contains(theWnd->v3dView()))
257 myWindowScale.remove (theWnd->v3dView());
258 emit deleteView(theWnd);
261 void XGUI_ViewerProxy::onViewCreated(AppElements_ViewWindow* theWnd)
263 theWnd->viewPort()->installEventFilter(this);
265 connect(theWnd, SIGNAL(vpTransformationFinished(AppElements_ViewWindow::OperationType)),
266 this, SLOT(onViewTransformed(AppElements_ViewWindow::OperationType)));
268 myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
270 emit viewCreated(theWnd);
273 void XGUI_ViewerProxy::onActivated(AppElements_ViewWindow* theWnd)
275 emit activated(theWnd);
278 void XGUI_ViewerProxy::onMousePress(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
280 emit mousePress(theWnd, theEvent);
283 void XGUI_ViewerProxy::onMouseRelease(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
285 emit mouseRelease(theWnd, theEvent);
288 void XGUI_ViewerProxy::onMouseDoubleClick(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
290 emit mouseDoubleClick(theWnd, theEvent);
293 void XGUI_ViewerProxy::onMouseMove(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
296 emit mouseMove(theWnd, theEvent);
299 void XGUI_ViewerProxy::onKeyPress(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
301 emit keyPress(theWnd, theEvent);
304 void XGUI_ViewerProxy::onKeyRelease(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
306 emit keyRelease(theWnd, theEvent);
309 void XGUI_ViewerProxy::onViewTransformed(AppElements_ViewWindow::OperationType theType)
311 emit viewTransformed((int) theType);
317 //***************************************
318 void XGUI_ViewerProxy::enableSelection(bool isEnabled)
321 myWorkshop->salomeConnector()->viewer()->enableSelection(isEnabled);
323 myWorkshop->mainWindow()->viewer()->setSelectionEnabled(isEnabled);
327 //***************************************
328 bool XGUI_ViewerProxy::isSelectionEnabled() const
331 return myWorkshop->salomeConnector()->viewer()->isSelectionEnabled();
333 return myWorkshop->mainWindow()->viewer()->isSelectionEnabled();
337 //***************************************
338 void XGUI_ViewerProxy::enableMultiselection(bool isEnable)
341 myWorkshop->salomeConnector()->viewer()->enableMultiselection(isEnable);
343 myWorkshop->mainWindow()->viewer()->setMultiSelectionEnabled(isEnable);
347 //***************************************
348 bool XGUI_ViewerProxy::isMultiSelectionEnabled() const
351 return myWorkshop->salomeConnector()->viewer()->isMultiSelectionEnabled();
353 return myWorkshop->mainWindow()->viewer()->isMultiSelectionEnabled();
357 //***************************************
358 bool XGUI_ViewerProxy::enableDrawMode(bool isEnabled)
361 return myWorkshop->salomeConnector()->viewer()->enableDrawMode(isEnabled);
363 return myWorkshop->mainWindow()->viewer()->enableDrawMode(isEnabled);
367 //***************************************
368 void XGUI_ViewerProxy::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
370 myWorkshop->displayer()->addSelectionFilter(theFilter);
373 //***************************************
374 void XGUI_ViewerProxy::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
376 myWorkshop->displayer()->removeSelectionFilter(theFilter);
379 //***************************************
380 bool XGUI_ViewerProxy::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
382 return myWorkshop->displayer()->hasSelectionFilter(theFilter);
385 //***************************************
386 void XGUI_ViewerProxy::clearSelectionFilters()
388 myWorkshop->displayer()->removeFilters();
391 //***************************************
392 void XGUI_ViewerProxy::update()
394 myWorkshop->displayer()->updateViewer();
397 //***************************************
398 bool XGUI_ViewerProxy::canDragByMouse() const
400 if (myWorkshop->isSalomeMode()) {
401 ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
402 return aViewer->canDragByMouse();
408 //***************************************
409 void XGUI_ViewerProxy::displayHighlight()
411 Handle(AIS_InteractiveContext) aContext = AISContext();
413 if (myResult->groupName() == ModelAPI_ResultConstruction::group()) {
414 FeaturePtr aFeature = ModelAPI_Feature::feature(myResult);
415 if (aFeature.get()) {
416 std::list<ResultPtr> aResults = aFeature->results();
417 std::list<ResultPtr>::const_iterator aIt;
419 Handle(AIS_Shape) aAis;
420 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
422 TopoDS_Shape aTShape = aRes->shape()->impl<TopoDS_Shape>();
423 aAis = new AIS_Shape(aTShape);
424 aAis->SetColor(HIGHLIGHT_COLOR);
425 aAis->SetZLayer(1); //Graphic3d_ZLayerId_Topmost
426 myHighlights.Append(aAis);
427 aContext->Display(aAis, false);
428 aContext->Deactivate(aAis);
433 TopoDS_Shape aTShape = myResult->shape()->impl<TopoDS_Shape>();
434 Handle(AIS_Shape) aAis = new AIS_Shape(aTShape);
435 aAis->SetColor(HIGHLIGHT_COLOR);
436 aAis->SetZLayer(1); //Graphic3d_ZLayerId_Topmost
437 myHighlights.Append(aAis);
438 aContext->Display(aAis, false);
439 aContext->Deactivate(aAis);
443 void XGUI_ViewerProxy::eraseHighlight()
445 Handle(AIS_InteractiveContext) aContext = AISContext();
446 Handle(AIS_InteractiveObject) anAISIO;
447 AIS_ListIteratorOfListOfInteractive aLIt;
448 for (aLIt.Initialize(myHighlights); aLIt.More(); aLIt.Next()) {
449 anAISIO = aLIt.Value();
450 aContext->Remove(anAISIO, false);
452 myHighlights.Clear();
455 void XGUI_ViewerProxy::updateHighlight()
457 Handle(AIS_InteractiveContext) aContext = AISContext();
458 if (!aContext.IsNull()) {
459 Handle(SelectMgr_EntityOwner) aOwner;
460 Handle(AIS_InteractiveObject) anIO;
461 bool isDisplayed = false;
463 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
464 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
465 aOwner = aContext->DetectedOwner();
466 anIO = Handle(AIS_InteractiveObject)::DownCast(aOwner->Selectable());
467 aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aDisplayer->getObject(anIO));
468 if (aRes.get() && (aRes != myResult)) {
472 aContext->UpdateCurrentViewer();
474 isDisplayed = aRes.get();
478 aContext->UpdateCurrentViewer();
479 myResult = ResultPtr();
485 void XGUI_ViewerProxy::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
488 emit mouseMove(theWnd, theEvent);
492 //***************************************
493 //void XGUI_ViewerProxy::Zfitall()
496 // myWorkshop->salomeConnector()->viewer()->Zfitall();
498 // AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
499 // AppElements_ViewWindow* aView = aViewer->activeViewWindow();
501 // Handle(V3d_View) aView3d = aView->v3dView();
502 // aView3d->ZFitAll();
503 // if (aView3d->Depth() < 0.1)
504 // aView3d->DepthFitAll();