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>
36 #include <AIS_Shape.hxx>
40 XGUI_ViewerProxy::XGUI_ViewerProxy(XGUI_Workshop* theParent)
41 : ModuleBase_IViewer(theParent),
46 void XGUI_ViewerProxy::connectViewProxy()
49 connect(myWorkshop->salomeConnector()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
50 SIGNAL(trihedronVisibilityChanged(bool)));
52 connect(myWorkshop->mainWindow()->viewer(), SIGNAL(trihedronVisibilityChanged(bool)),
53 SIGNAL(trihedronVisibilityChanged(bool)));
57 Handle(AIS_InteractiveContext) XGUI_ViewerProxy::AISContext() const
59 Handle(AIS_InteractiveContext) aContext;
61 aContext = myWorkshop->salomeConnector()->viewer()->AISContext();
63 aContext = myWorkshop->mainWindow()->viewer()->AISContext();
68 Handle(AIS_Trihedron) XGUI_ViewerProxy::trihedron() const
71 return myWorkshop->salomeConnector()->viewer()->trihedron();
73 return myWorkshop->mainWindow()->viewer()->trihedron();
77 Handle(V3d_Viewer) XGUI_ViewerProxy::v3dViewer() const
80 return myWorkshop->salomeConnector()->viewer()->v3dViewer();
82 return myWorkshop->mainWindow()->viewer()->v3dViewer();
86 Handle(V3d_View) XGUI_ViewerProxy::activeView() const
89 return myWorkshop->salomeConnector()->viewer()->activeView();
91 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
92 return (aViewer->activeViewWindow()) ?
93 aViewer->activeViewWindow()->viewPortApp()->getView() :
98 QWidget* XGUI_ViewerProxy::activeViewPort() const
101 return myWorkshop->salomeConnector()->viewer()->activeViewPort();
103 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
104 return (aViewer->activeViewWindow()) ?
105 aViewer->activeViewWindow()->viewPortApp(): 0;
109 void XGUI_ViewerProxy::setViewProjection(double theX, double theY, double theZ, double theTwist)
111 Handle(V3d_View) aView3d = activeView();
112 if (!aView3d.IsNull()) {
113 aView3d->SetProj(theX, theY, theZ);
114 aView3d->SetTwist( theTwist );
115 aView3d->FitAll(0.01, false);
116 aView3d->SetZSize(0.);
117 if (aView3d->Depth() < 0.1)
118 aView3d->DepthFitAll();
122 void XGUI_ViewerProxy::fitAll()
125 myWorkshop->salomeConnector()->viewer()->fitAll();
127 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
128 if (aViewer->activeViewWindow())
129 aViewer->activeViewWindow()->viewPortApp()->fitAll();
133 void XGUI_ViewerProxy::eraseAll()
136 myWorkshop->salomeConnector()->viewer()->eraseAll();
141 void XGUI_ViewerProxy::connectToViewer()
144 ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
146 connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
147 connect(aViewer, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)),
148 this, SIGNAL(tryCloseView(ModuleBase_IViewWindow*)));
150 connect(aViewer, SIGNAL(deleteView(ModuleBase_IViewWindow*)),
151 this, SIGNAL(deleteView(ModuleBase_IViewWindow*)));
153 connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
154 this, SLOT(onViewCreated(ModuleBase_IViewWindow*)));
156 connect(aViewer, SIGNAL(activated(ModuleBase_IViewWindow*)),
157 this, SIGNAL(activated(ModuleBase_IViewWindow*)));
159 connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
160 this, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)));
162 connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
163 this, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
165 connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)), this,
166 SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
168 connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
169 this, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
171 connect(aViewer, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)),
172 this, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)));
174 connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
175 this, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
177 connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
179 connect(aViewer, SIGNAL(viewTransformed(int)), this, SIGNAL(viewTransformed(int)));
181 connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
182 SIGNAL(contextMenuRequested(QContextMenuEvent*)));
184 AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
186 connect(aViewer, SIGNAL(lastViewClosed()), this, SIGNAL(lastViewClosed()));
188 connect(aViewer, SIGNAL(tryCloseView(AppElements_ViewWindow*)),
189 this, SLOT(onTryCloseView(AppElements_ViewWindow*)));
191 connect(aViewer, SIGNAL(deleteView(AppElements_ViewWindow*)),
192 this, SLOT(onDeleteView(AppElements_ViewWindow*)));
194 connect(aViewer, SIGNAL(viewCreated(AppElements_ViewWindow*)),
195 this, SLOT(onViewCreated(AppElements_ViewWindow*)));
197 connect(aViewer, SIGNAL(activated(AppElements_ViewWindow*)),
198 this, SLOT(onActivated(AppElements_ViewWindow*)));
200 connect(aViewer, SIGNAL(mousePress(AppElements_ViewWindow*, QMouseEvent*)), this,
201 SLOT(onMousePress(AppElements_ViewWindow*, QMouseEvent*)));
203 connect(aViewer, SIGNAL(mouseRelease(AppElements_ViewWindow*, QMouseEvent*)), this,
204 SLOT(onMouseRelease(AppElements_ViewWindow*, QMouseEvent*)));
206 connect(aViewer, SIGNAL(mouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)), this,
207 SLOT(onMouseDoubleClick(AppElements_ViewWindow*, QMouseEvent*)));
209 connect(aViewer, SIGNAL(mouseMove(AppElements_ViewWindow*, QMouseEvent*)), this,
210 SLOT(onMouseMove(AppElements_ViewWindow*, QMouseEvent*)));
212 connect(aViewer, SIGNAL(keyPress(AppElements_ViewWindow*, QKeyEvent*)), this,
213 SLOT(onKeyPress(AppElements_ViewWindow*, QKeyEvent*)));
215 connect(aViewer, SIGNAL(keyRelease(AppElements_ViewWindow*, QKeyEvent*)), this,
216 SLOT(onKeyRelease(AppElements_ViewWindow*, QKeyEvent*)));
218 connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
219 connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
220 SIGNAL(contextMenuRequested(QContextMenuEvent*)));
224 bool XGUI_ViewerProxy::eventFilter(QObject *theObject, QEvent *theEvent)
226 if (theEvent->type() == QEvent::Enter) {
227 emit enterViewPort();
229 else if (theEvent->type() == QEvent::Leave) {
230 emit leaveViewPort();
232 return ModuleBase_IViewer::eventFilter(theObject, theEvent);
235 void XGUI_ViewerProxy::onViewCreated(ModuleBase_IViewWindow* theWnd)
237 theWnd->viewPort()->installEventFilter(this);
239 myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
241 emit viewCreated(theWnd);
245 void XGUI_ViewerProxy::onTryCloseView(AppElements_ViewWindow* theWnd)
247 emit tryCloseView(theWnd);
250 void XGUI_ViewerProxy::onDeleteView(AppElements_ViewWindow* theWnd)
252 if (myWindowScale.contains(theWnd->v3dView()))
253 myWindowScale.remove (theWnd->v3dView());
254 emit deleteView(theWnd);
257 void XGUI_ViewerProxy::onViewCreated(AppElements_ViewWindow* theWnd)
259 theWnd->viewPort()->installEventFilter(this);
261 connect(theWnd, SIGNAL(vpTransformationFinished(AppElements_ViewWindow::OperationType)),
262 this, SLOT(onViewTransformed(AppElements_ViewWindow::OperationType)));
264 myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
266 emit viewCreated(theWnd);
269 void XGUI_ViewerProxy::onActivated(AppElements_ViewWindow* theWnd)
271 emit activated(theWnd);
274 void XGUI_ViewerProxy::onMousePress(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
276 emit mousePress(theWnd, theEvent);
279 void XGUI_ViewerProxy::onMouseRelease(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
281 emit mouseRelease(theWnd, theEvent);
284 void XGUI_ViewerProxy::onMouseDoubleClick(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
286 emit mouseDoubleClick(theWnd, theEvent);
289 void XGUI_ViewerProxy::displayHighlight()
291 Handle(AIS_InteractiveContext) aContext = AISContext();
292 std::list<ResultPtr> aResults = myFeature->results();
293 std::list<ResultPtr>::const_iterator aIt;
295 Handle(AIS_Shape) aAis;
296 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
298 TopoDS_Shape aTShape = aRes->shape()->impl<TopoDS_Shape>();
299 aAis = new AIS_Shape(aTShape);
300 aAis->SetColor(Quantity_NOC_CYAN3);
301 aAis->SetZLayer(Graphic3d_ZLayerId_Topmost);
302 myHighlights.Append(aAis);
303 aContext->Display(aAis, false);
304 aContext->Deactivate(aAis);
308 void XGUI_ViewerProxy::eraseHighlight()
310 Handle(AIS_InteractiveContext) aContext = AISContext();
311 Handle(AIS_InteractiveObject) anAISIO;
312 AIS_ListIteratorOfListOfInteractive aLIt;
313 for (aLIt.Initialize(myHighlights); aLIt.More(); aLIt.Next()) {
314 anAISIO = aLIt.Value();
315 aContext->Remove(anAISIO, false);
317 myHighlights.Clear();
320 void XGUI_ViewerProxy::onMouseMove(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
322 // vsv: Display prehighlighting of detected object
323 //Handle(AIS_InteractiveContext) aContext = AISContext();
324 //if (!aContext.IsNull()) {
325 // Handle(SelectMgr_EntityOwner) aOwner;
326 // Handle(AIS_InteractiveObject) anIO;
328 // bool isDisplayed = false;
329 // XGUI_Displayer* aDisplayer = myWorkshop->displayer();
330 // for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
331 // aOwner = aContext->DetectedOwner();
332 // anIO = Handle(AIS_InteractiveObject)::DownCast(aOwner->Selectable());
333 // aObj = aDisplayer->getObject(anIO);
335 // FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
336 // if (aFeature.get()) {
337 // if (aFeature != myFeature) {
339 // myFeature = aFeature;
340 // displayHighlight();
341 // aContext->UpdateCurrentViewer();
343 // isDisplayed = true;
347 // if (!isDisplayed) {
349 // aContext->UpdateCurrentViewer();
350 // myFeature = FeaturePtr();
353 emit mouseMove(theWnd, theEvent);
356 void XGUI_ViewerProxy::onKeyPress(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
358 emit keyPress(theWnd, theEvent);
361 void XGUI_ViewerProxy::onKeyRelease(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
363 emit keyRelease(theWnd, theEvent);
366 void XGUI_ViewerProxy::onViewTransformed(AppElements_ViewWindow::OperationType theType)
368 emit viewTransformed((int) theType);
374 //***************************************
375 void XGUI_ViewerProxy::enableSelection(bool isEnabled)
378 myWorkshop->salomeConnector()->viewer()->enableSelection(isEnabled);
380 myWorkshop->mainWindow()->viewer()->setSelectionEnabled(isEnabled);
384 //***************************************
385 bool XGUI_ViewerProxy::isSelectionEnabled() const
388 return myWorkshop->salomeConnector()->viewer()->isSelectionEnabled();
390 return myWorkshop->mainWindow()->viewer()->isSelectionEnabled();
394 //***************************************
395 void XGUI_ViewerProxy::enableMultiselection(bool isEnable)
398 myWorkshop->salomeConnector()->viewer()->enableMultiselection(isEnable);
400 myWorkshop->mainWindow()->viewer()->setMultiSelectionEnabled(isEnable);
404 //***************************************
405 bool XGUI_ViewerProxy::isMultiSelectionEnabled() const
408 return myWorkshop->salomeConnector()->viewer()->isMultiSelectionEnabled();
410 return myWorkshop->mainWindow()->viewer()->isMultiSelectionEnabled();
414 //***************************************
415 bool XGUI_ViewerProxy::enableDrawMode(bool isEnabled)
418 return myWorkshop->salomeConnector()->viewer()->enableDrawMode(isEnabled);
420 return myWorkshop->mainWindow()->viewer()->enableDrawMode(isEnabled);
424 //***************************************
425 void XGUI_ViewerProxy::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
427 myWorkshop->displayer()->addSelectionFilter(theFilter);
430 //***************************************
431 void XGUI_ViewerProxy::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
433 myWorkshop->displayer()->removeSelectionFilter(theFilter);
436 //***************************************
437 bool XGUI_ViewerProxy::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
439 return myWorkshop->displayer()->hasSelectionFilter(theFilter);
442 //***************************************
443 void XGUI_ViewerProxy::clearSelectionFilters()
445 myWorkshop->displayer()->removeFilters();
448 //***************************************
449 void XGUI_ViewerProxy::update()
451 myWorkshop->displayer()->updateViewer();
454 //***************************************
455 bool XGUI_ViewerProxy::canDragByMouse() const
457 if (myWorkshop->isSalomeMode()) {
458 ModuleBase_IViewer* aViewer = myWorkshop->salomeConnector()->viewer();
459 return aViewer->canDragByMouse();
466 //***************************************
467 //void XGUI_ViewerProxy::Zfitall()
470 // myWorkshop->salomeConnector()->viewer()->Zfitall();
472 // AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
473 // AppElements_ViewWindow* aView = aViewer->activeViewWindow();
475 // Handle(V3d_View) aView3d = aView->v3dView();
476 // aView3d->ZFitAll();
477 // if (aView3d->Depth() < 0.1)
478 // aView3d->DepthFitAll();