1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "OCCViewer_ViewModel.h"
24 #include "OCCViewer_ViewWindow.h"
25 #include "OCCViewer_ViewFrame.h"
26 #include "OCCViewer_VService.h"
27 #include "OCCViewer_ViewPort3d.h"
28 #include "OCCViewer_ClippingDlg.h"
29 #include "OCCViewer_Utilities.h"
31 #include "SUIT_ViewWindow.h"
32 #include "SUIT_ViewManager.h"
33 #include "SUIT_Desktop.h"
34 #include "SUIT_Session.h"
35 #include "SUIT_ResourceMgr.h"
37 #include "ViewerData_AISShape.hxx"
39 #include "CAF_Tools.h"
41 #include <Basics_OCCTVersion.hxx>
43 #include "QtxActionToolMgr.h"
44 #include "QtxBackgroundTool.h"
47 #include <QApplication>
48 #include <QColorDialog>
49 #include <QFileDialog>
53 #include <QMouseEvent>
55 #include <QDesktopWidget>
57 #include <AIS_Axis.hxx>
58 #if OCC_VERSION_LARGE > 0x06080000
59 #include <Prs3d_Drawer.hxx>
61 #include <AIS_Drawer.hxx>
63 #include <AIS_ListOfInteractive.hxx>
64 #include <AIS_ListIteratorOfListOfInteractive.hxx>
66 #include <Graphic3d_Texture2Dmanual.hxx>
67 #include <Graphic3d_MaterialAspect.hxx>
68 #include <Graphic3d_TextureParams.hxx>
70 #include <Geom_Axis2Placement.hxx>
71 #include <Prs3d_Drawer.hxx>
72 #include <Prs3d_DatumAspect.hxx>
73 #include <Prs3d_LineAspect.hxx>
74 #include <Prs3d_TextAspect.hxx>
76 #include <Visual3d_View.hxx>
78 #include <V3d_DirectionalLight.hxx>
79 #include <V3d_AmbientLight.hxx>
82 Get data for supported background modes: gradient types, identifiers and supported image formats
84 QString OCCViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
86 gradList << tr("GT_HORIZONTALGRADIENT") << tr("GT_VERTICALGRADIENT") <<
87 tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT") <<
88 tr("GT_FIRSTCORNERGRADIENT") << tr("GT_SECONDCORNERGRADIENT") <<
89 tr("GT_THIRDCORNERGRADIENT") << tr("GT_FORTHCORNERGRADIENT");
90 idList << HorizontalGradient << VerticalGradient <<
91 Diagonal1Gradient << Diagonal2Gradient <<
92 Corner1Gradient << Corner2Gradient <<
93 Corner3Gradient << Corner4Gradient;
94 txtList << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
95 return tr("BG_IMAGE_FILES");
99 Get data for supported stereo pair modes: stereo types and identifiers
101 void OCCViewer_Viewer::stereoData( QStringList& typeList, QIntList& idList)
103 typeList << tr("ST_QUADBUFFER") << tr("ST_ANAGLYPH") <<
104 tr("ST_ROWINTERLACED") << tr("ST_COLUMNINTERLACED") <<
105 tr("ST_CHESSBOARD") << tr("ST_SIDEBYSIDE") <<
107 idList << QuadBufferType << AnaglyphType <<
108 RowInterlacedType << ColumnInterlacedType <<
109 ChessBoardType << SideBySideType <<
115 \param DisplayTrihedron - is trihedron displayed
117 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
119 myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
122 myTrihedronSize(100),
125 // init CasCade viewers
126 myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
127 //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
131 myAISContext = new AIS_InteractiveContext( myV3dViewer );
132 myAISContext->SelectionColor( Quantity_NOC_WHITE );
134 // display isoline on planar faces (box for ex.)
135 myAISContext->IsoOnPlane( true );
137 /* create trihedron */
138 if ( DisplayTrihedron )
140 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
141 myTrihedron = new AIS_Trihedron(anAxis);
142 myTrihedron->SetInfiniteState( Standard_True );
144 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
145 //myTrihedron->SetColor( Col );
146 myTrihedron->SetArrowColor( Col.Name() );
147 myTrihedron->SetSize(100);
148 #if OCC_VERSION_LARGE > 0x06080000
149 Handle(Prs3d_Drawer) drawer = myTrihedron->Attributes();
150 if (drawer->HasOwnDatumAspect()) {
152 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
153 if (drawer->HasDatumAspect()) {
155 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
156 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
157 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
158 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
162 // set interaction style to standard
163 myInteractionStyle = 0;
165 // set zooming style to standard
169 myPreselectionEnabled = true;
172 mySelectionEnabled = true;
173 myMultiSelectionEnabled = true;
175 // set projection type to orthographic
176 myProjectionType = 0;
177 // set stereo parameters
179 myAnaglyphFilter = 0;
180 myToReverseStereo = 0;
182 myQuadBufferSupport = 0;
183 myStereographicFocusType = 1;
184 myInterocularDistanceType = 1;
185 myStereographicFocusValue = 1.0;
186 myInterocularDistanceValue = 0.05;
187 //set clipping color and texture to standard
188 myClippingColor = QColor( 50, 50, 50 );
189 myDefaultTextureUsed = true;
190 myClippingTexture = QString();
191 myTextureModulated = true;
192 myClippingTextureScale = 1.0;
199 OCCViewer_Viewer::~OCCViewer_Viewer()
201 myAISContext.Nullify();
202 myV3dViewer.Nullify();
207 \return background color of viewer
209 QColor OCCViewer_Viewer::backgroundColor() const
211 return backgroundColor(0);
215 \return background data of viewer
217 Qtx::BackgroundData OCCViewer_Viewer::background() const
219 return background(0);
223 Sets background color [obsolete]
224 \param c - new background color
226 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
228 setBackgroundColor( 0, c );
233 \param d - new background data
235 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
237 setBackground( 0, theBackground );
241 Start initialization of view window
242 \param view - view window to be initialized
244 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
248 view->initSketchers();
249 view->setInteractionStyle( interactionStyle() );
250 view->setProjectionType( projectionType() );
251 view->setStereoType( stereoType() );
252 view->setAnaglyphFilter( anaglyphFilter() );
253 view->setStereographicFocus( stereographicFocusType(), stereographicFocusValue() );
254 view->setInterocularDistance( interocularDistanceType(), interocularDistanceValue() );
255 view->setReverseStereo( isReverseStereo() );
256 view->setVSync( isVSync() );
257 view->setQuadBufferSupport( isQuadBufferSupport() );
258 view->setZoomingStyle( zoomingStyle() );
259 view->enablePreselection( isPreselectionEnabled() );
260 view->enableSelection( isSelectionEnabled() );
262 OCCViewer_ViewPort3d* vp3d = view->getViewPort();
265 vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
266 // connect signal from viewport
267 connect(vp3d, SIGNAL(vpClosed(OCCViewer_ViewPort3d*)), this, SLOT(onViewClosed(OCCViewer_ViewPort3d*)));
268 connect(vp3d, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewMapped(OCCViewer_ViewPort3d*)));
274 Creates new view window
275 \param theDesktop - main window of application
277 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
280 OCCViewer_ViewFrame* view = new OCCViewer_ViewFrame(theDesktop, this);
281 // get main view window (created by view frame)
282 OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
283 // initialize main view window
285 // set default background for view window
286 vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
292 Sets new view manager
293 \param theViewManager - new view manager
295 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
297 SUIT_ViewModel::setViewManager(theViewManager);
298 if (theViewManager) {
299 connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
300 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
302 connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)),
303 this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
305 connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
306 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
308 connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)),
309 this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
314 SLOT: called on mouse button press, stores current mouse position as start point for transformations
316 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
318 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
322 SLOT: called on mouse move, processes transformation or hilighting
324 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
326 if (!mySelectionEnabled) return;
327 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
329 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
331 myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
333 if ( isSelectionEnabled() && isPreselectionEnabled() ) {
334 if (aView->getViewPort()->isBusy()) {
335 QCoreApplication::processEvents();
336 return; // Check that the ViewPort initialization completed
337 // To Prevent call move event if the View port is not initialized
340 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
341 if ( !aView3d.IsNull() ) {
342 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
349 SLOT: called on mouse button release, finishes transformation or selection
351 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
353 if (!mySelectionEnabled) return;
354 if (theEvent->button() != Qt::LeftButton) return;
355 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
357 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
361 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
362 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
365 myAISContext->ClearCurrents( false );
369 if (myStartPnt == myEndPnt)
371 if ( !isPreselectionEnabled() ) {
372 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
373 if ( !aView3d.IsNull() ) {
374 myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
378 if (aHasShift && myMultiSelectionEnabled)
379 myAISContext->ShiftSelect();
381 myAISContext->Select();
385 if (aHasShift && myMultiSelectionEnabled)
386 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
387 myEndPnt.x(), myEndPnt.y(),
388 aView->getViewPort()->getView(), Standard_False );
390 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
391 myEndPnt.x(), myEndPnt.y(),
392 aView->getViewPort()->getView(), Standard_False );
394 int Nb = myAISContext->NbSelected();
395 if( Nb>1 && !myMultiSelectionEnabled )
397 myAISContext->InitSelected();
398 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
399 if( !anOwner.IsNull() )
401 myAISContext->ClearSelected( Standard_False );
402 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
406 myAISContext->UpdateCurrentViewer();
408 emit selectionChanged();
412 SLOT: called on key press, processes selection in "key free" interaction style
414 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
416 if (!mySelectionEnabled) return;
418 OCCViewer_ViewWindow* aView = qobject_cast<OCCViewer_ViewWindow*>( theWindow );
419 if ( !aView ) return;
421 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
423 switch ( theEvent->key() ) {
426 myAISContext->ClearCurrents( false );
430 if ( !isPreselectionEnabled() ) {
431 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
432 if ( !aView3d.IsNull() ) {
433 myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
437 if (aHasShift && myMultiSelectionEnabled)
438 myAISContext->ShiftSelect();
440 myAISContext->Select();
442 emit selectionChanged();
446 if ( isPreselectionEnabled() ) {
447 if ( getAISContext()->HasOpenedContext() )
448 getAISContext()->HilightNextDetected( aView->getViewPort()->getView() );
452 if ( isPreselectionEnabled() ) {
453 if ( getAISContext()->HasOpenedContext() )
454 getAISContext()->HilightPreviousDetected( aView->getViewPort()->getView() );
462 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
464 Standard_Integer aViewsNb = 0;
465 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
467 if ( aViewsNb < 2 ) {
468 //clean up presentations before last view is closed
469 myAISContext->RemoveAll(Standard_False);
473 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
475 setTrihedronShown( true );
476 bool showStaticTrihedron = true;
477 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
478 if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
479 viewPort->showStaticTrihedron( showStaticTrihedron );
482 int OCCViewer_Viewer::getTopLayerId()
484 if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )
485 myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
491 \return interaction style
493 int OCCViewer_Viewer::interactionStyle() const
495 return myInteractionStyle;
499 Sets interaction style: 0 - standard, 1 - keyboard free interaction
500 \param theStyle - new interaction style
502 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
504 myInteractionStyle = theStyle;
505 //!! To be done for view windows
506 if ( !myViewManager )
509 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
510 for ( int i = 0; i < (int)wins.count(); i++ )
512 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
514 win->setInteractionStyle( theStyle );
519 \return projection type
521 int OCCViewer_Viewer::projectionType() const
523 return myProjectionType;
527 Sets projection type: 0 - orthographic, 1 - perspective
528 \param theType - new projection type
530 void OCCViewer_Viewer::setProjectionType( const int theType )
532 if ( myProjectionType != theType ) {
533 if ( theType != OCCViewer_ViewWindow::Stereo )
534 myProjectionType = theType;
536 if ( !myViewManager )
539 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
540 for ( int i = 0; i < (int)wins.count(); i++ )
542 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
544 win->setProjectionType( (OCCViewer_ViewWindow::ProjectionType)theType );
552 int OCCViewer_Viewer::stereoType() const
559 \param theType - new stereo type
561 void OCCViewer_Viewer::setStereoType( const int theType )
563 myStereoType = theType;
565 if ( !myViewManager )
568 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
569 for ( int i = 0; i < (int)wins.count(); i++ )
571 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
573 win->setStereoType( (OCCViewer_ViewWindow::StereoType)theType );
578 \return stereographic focus type
580 int OCCViewer_Viewer::stereographicFocusType() const
582 return myStereographicFocusType;
586 \return stereographic focus value
588 double OCCViewer_Viewer::stereographicFocusValue() const
590 return myStereographicFocusValue;
594 Sets stereographic focus parameters
595 \param theType - new stereographic focus type
596 \param theValue - new stereographic focus value
598 void OCCViewer_Viewer::setStereographicFocus( const int theType, const double theValue )
600 myStereographicFocusType = theType;
601 myStereographicFocusValue = theValue;
603 if ( !myViewManager )
606 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
607 for ( int i = 0; i < (int)wins.count(); i++ )
609 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
611 win->setStereographicFocus( (OCCViewer_ViewWindow::FocusIODType)theType, theValue );
616 \return stereographic focus type
618 int OCCViewer_Viewer::interocularDistanceType() const
620 return myInterocularDistanceType;
624 \return stereographic focus value
626 double OCCViewer_Viewer::interocularDistanceValue() const
628 return myInterocularDistanceValue;
632 Sets interocular distance parameters
633 \param theType - new IOD type
634 \param theValue - new IOD value
636 void OCCViewer_Viewer::setInterocularDistance( const int theType, const double theValue )
638 myInterocularDistanceType = theType;
639 myInterocularDistanceValue = theValue;
641 if ( !myViewManager )
644 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
645 for ( int i = 0; i < (int)wins.count(); i++ )
647 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
649 win->setInterocularDistance( (OCCViewer_ViewWindow::FocusIODType)theType, theValue );
654 \return anaglyph filter
656 int OCCViewer_Viewer::anaglyphFilter() const
658 return myAnaglyphFilter;
663 \param theType - new anaglyph filter
665 void OCCViewer_Viewer::setAnaglyphFilter( const int theType )
667 myAnaglyphFilter = theType;
669 if ( !myViewManager )
672 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
673 for ( int i = 0; i < (int)wins.count(); i++ )
675 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
677 win->setAnaglyphFilter( (OCCViewer_ViewWindow::AnaglyphFilter)theType );
682 \return reverse stereo
684 bool OCCViewer_Viewer::isReverseStereo() const
686 return myToReverseStereo;
691 \param theReverse - enable/disable reverse mode
693 void OCCViewer_Viewer::setReverseStereo( const bool theReverse )
695 myToReverseStereo = theReverse;
697 if ( !myViewManager )
700 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
701 for ( int i = 0; i < (int)wins.count(); i++ )
703 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
705 win->setReverseStereo( theReverse );
712 bool OCCViewer_Viewer::isVSync() const
719 \param theEnable - enable/disable V-Sync mode
721 void OCCViewer_Viewer::setVSync( const bool theEnable )
723 myVSyncMode = theEnable;
725 if ( !myViewManager )
728 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
729 for ( int i = 0; i < (int)wins.count(); i++ )
731 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
733 win->setVSync( theEnable );
738 \return support quad-buffered stereo
740 bool OCCViewer_Viewer::isQuadBufferSupport() const
742 return myQuadBufferSupport;
746 Set support quad-buffered stereo
747 \param theEnable - enable/disable support quad-buffered stereo
749 void OCCViewer_Viewer::setQuadBufferSupport( const bool theEnable )
751 myQuadBufferSupport = theEnable;
753 if ( !myViewManager )
756 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
757 for ( int i = 0; i < (int)wins.count(); i++ )
759 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
761 win->setQuadBufferSupport( theEnable );
766 \return zooming style
768 int OCCViewer_Viewer::zoomingStyle() const
770 return myZoomingStyle;
774 Sets zooming style: 0 - standard, 1 - advanced (at cursor)
775 \param theStyle - new zooming style
777 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
779 myZoomingStyle = theStyle;
780 //!! To be done for view windows
781 if ( !myViewManager )
784 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
785 for ( int i = 0; i < (int)wins.count(); i++ )
787 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
789 win->setZoomingStyle( theStyle );
794 \return true if preselection is enabled
796 bool OCCViewer_Viewer::isPreselectionEnabled() const
798 return myPreselectionEnabled;
802 Enables/disables preselection
803 \param isEnabled - new status
805 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
807 myPreselectionEnabled = isEnabled;
809 if ( !myViewManager )
812 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
813 for ( int i = 0; i < (int)wins.count(); i++ )
815 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
817 win->enablePreselection( isEnabled );
823 \return true if selection is enabled
825 bool OCCViewer_Viewer::isSelectionEnabled() const
827 return mySelectionEnabled;
831 Enables/disables selection
832 \param isEnabled - new status
834 void OCCViewer_Viewer::enableSelection(bool isEnabled)
836 mySelectionEnabled = isEnabled;
838 //!! To be done for view windows
839 if ( !myViewManager )
842 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
843 for ( int i = 0; i < (int)wins.count(); i++ )
845 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
847 win->updateEnabledDrawMode();
848 win->enableSelection( isEnabled );
853 //clear current selection in the viewer
855 myAISContext->ClearSelected( Standard_True );
861 Sets multiselection enabled status
862 \param isEnabled - new status
864 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
866 myMultiSelectionEnabled = isEnable;
867 //!! To be done for view windows
868 if ( !myViewManager )
871 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
872 for ( int i = 0; i < (int)wins.count(); i++ )
874 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
876 win->updateEnabledDrawMode();
881 Sets a color of the clipped region
882 \param theColor - a new color of the clipped region
884 void OCCViewer_Viewer::setClippingColor( const QColor& theColor )
886 myClippingColor = theColor;
888 if( myInternalClipPlanes.IsEmpty() )
891 Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
892 aMaterialAspect.SetColor( Quantity_Color( theColor.redF(), theColor.greenF(),
893 theColor.blueF(), Quantity_TOC_RGB ) );
895 for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
896 myInternalClipPlanes.Value(i)->SetCappingMaterial( aMaterialAspect );
902 \return clipping color
904 QColor OCCViewer_Viewer::clippingColor() const
906 return myClippingColor;
909 // initialize a texture for clipped region
910 Handle(Graphic3d_Texture2Dmanual) initClippingTexture( const bool isDefault, const QString& theTexture,
911 const bool isModulate, const double theScale )
913 QString aTextureFile = isDefault ? ":images/hatch.png" : theTexture;
914 QPixmap px( aTextureFile );
915 const Handle(Image_PixMap) aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
916 Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual( aPixmap );
917 if( aTexture->IsDone() ) {
918 aTexture->EnableRepeat();
919 isModulate ? aTexture->EnableModulate() : aTexture->DisableModulate();
920 aTexture->GetParams()->SetScale( Graphic3d_Vec2( 1/( theScale*100 ), -1 / ( theScale*100 ) ) );
926 Sets default texture parameters
927 \param isDefault - use/non-use default texture
928 \param theTexture - new texture of the clipped region
929 \param isModulate - enable/disable texture modulate mode
930 \param theScale - scale factor.
932 void OCCViewer_Viewer::setClippingTextureParams( const bool isDefault, const QString& theTexture,
933 const bool isModulate, const double theScale )
935 myDefaultTextureUsed = isDefault;
936 myClippingTexture = theTexture;
937 myTextureModulated = isModulate;
938 myClippingTextureScale = theScale;
940 if( myInternalClipPlanes.IsEmpty() )
943 Handle(Graphic3d_Texture2Dmanual) aTexture =
944 initClippingTexture( myDefaultTextureUsed, myClippingTexture,
945 myTextureModulated, myClippingTextureScale );
947 for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
948 myInternalClipPlanes.Value(i)->SetCappingTexture( aTexture );
954 \return true if default texture is used
956 bool OCCViewer_Viewer::isDefaultTextureUsed() const
958 return myDefaultTextureUsed;
962 \return clipping texture
964 QString OCCViewer_Viewer::clippingTexture() const
966 return myClippingTexture;
970 \return true if texture is modulated
972 bool OCCViewer_Viewer::isTextureModulated() const
974 return myTextureModulated;
978 \return scale factor of texture
980 double OCCViewer_Viewer::clippingTextureScale() const
982 return myClippingTextureScale;
986 Builds popup for occ viewer
988 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
990 thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
991 thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
993 thePopup->addSeparator();
995 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
997 //Support of several toolbars in the popup menu
998 QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
999 QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
1000 for ( ; it!=last; it++ ) {
1001 if ( (*it)->parentWidget()->isVisible() )
1002 thePopup->addAction( (*it)->toggleViewAction() );
1007 SLOT: called on dump view operation is activated, stores scene to raster file
1009 void OCCViewer_Viewer::onDumpView()
1011 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1013 aView->onDumpView();
1017 SLOT: called if background color is to be changed changed, passes new color to view port
1019 void OCCViewer_Viewer::onChangeBackground()
1021 OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
1025 // get supported gradient types
1026 QStringList gradList;
1027 QIntList idList, txtList;
1028 QString formats = backgroundData( gradList, idList, txtList );
1030 // invoke dialog box
1031 Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(), // initial background
1032 aView, // parent for dialog box
1033 txtList, // allowed texture modes
1034 true, // enable solid color mode
1035 true, // enable gradient mode
1036 false, // disable custom gradient mode
1037 !txtList.isEmpty(), // enable/disable texture mode
1038 gradList, // gradient names
1039 idList, // gradient identifiers
1040 formats ); // image formats
1042 // set chosen background data to the viewer
1043 if ( bgData.isValid() )
1044 aView->setBackground( bgData );
1048 Updates OCC 3D viewer
1050 void OCCViewer_Viewer::update()
1052 if (!myV3dViewer.IsNull())
1053 myV3dViewer->Update();
1055 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1057 aView->updateGravityCoords();
1061 \return objects selected in 3D viewer
1062 \param theList - list to be filled with selected objects
1064 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
1067 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
1068 theList.Append(myAISContext->SelectedInteractive());
1072 Selects objects in 3D viewer. Other selected objects are left as selected
1073 \param theList - list objects to be selected
1075 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
1077 AIS_ListIteratorOfListOfInteractive aIt;
1078 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
1079 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
1080 myAISContext->UpdateCurrentViewer();
1084 Auxiliary method to emit signal selectionChanged()
1086 void OCCViewer_Viewer::performSelectionChanged()
1088 emit selectionChanged();
1092 * Defines default lights
1094 void OCCViewer_Viewer::setDefaultLights()
1096 // clear all light sources
1097 myV3dViewer->InitDefinedLights();
1098 while ( myV3dViewer->MoreDefinedLights() )
1100 myV3dViewer->DelLight( myV3dViewer->DefinedLight() );
1101 myV3dViewer->InitDefinedLights();
1104 // get light source parameters from preferences
1105 QColor aColor = SUIT_Session::session()->resourceMgr()->colorValue( "OCCViewer", "light_color", QColor( 0, 0, 0 ) );
1106 double aDx = SUIT_Session::session()->resourceMgr()->doubleValue( "OCCViewer", "light_dx", 0.0 );
1107 double aDy = SUIT_Session::session()->resourceMgr()->doubleValue( "OCCViewer", "light_dy", 0.0 );
1108 double aDz = SUIT_Session::session()->resourceMgr()->doubleValue( "OCCViewer", "light_dz", -1.0 );
1110 Handle(V3d_DirectionalLight) aLight =
1111 new V3d_DirectionalLight( myV3dViewer, V3d_Zneg, CAF_Tools::color( aColor ).Name(), Standard_True );
1112 if( !( aDx == 0 && aDy == 0 && aDz == 0 ) )
1113 aLight->SetDirection( aDx, aDy, aDz );
1114 myV3dViewer->SetLightOn( aLight );
1115 myV3dViewer->SetLightOn( new V3d_AmbientLight( myV3dViewer ) );
1119 Hilights/unhilights object in viewer
1120 \param obj - object to be updated
1121 \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
1122 \param update - update current viewer
1124 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
1125 bool hilight, bool update )
1127 bool isInLocal = myAISContext->HasOpenedContext();
1131 if ( hilight && !myAISContext->IsSelected( obj ) )
1132 myAISContext->AddOrRemoveCurrentObject( obj, false );
1133 else if ( !hilight && myAISContext->IsSelected( obj ) )
1134 myAISContext->AddOrRemoveCurrentObject( obj, false );
1138 myV3dViewer->Redraw();
1144 Unhilights all objects in viewer
1145 \param updateviewer - update current viewer
1147 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
1149 if ( myAISContext->HasOpenedContext() ) {
1151 myAISContext->ClearSelected( updateviewer );
1153 myAISContext->UnhilightSelected( updateviewer );
1157 myAISContext->ClearCurrents( updateviewer );
1159 myAISContext->UnhilightCurrents( updateviewer );
1167 \return true if object is in viewer or in collector
1168 \param obj - object to be checked
1169 \param onlyInViewer - search object only in viewer (so object must be displayed)
1171 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
1174 AIS_ListOfInteractive List;
1175 myAISContext->DisplayedObjects(List);
1177 AIS_ListIteratorOfListOfInteractive ite(List);
1178 for ( ; ite.More(); ite.Next() )
1179 if( ite.Value()==obj )
1186 \return true if object is displayed in viewer
1187 \param obj - object to be checked
1189 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
1191 return myAISContext->IsDisplayed( obj );
1195 Sets color of object
1196 \param obj - object to be updated
1197 \param color - new color
1198 \param update - update current viewer
1200 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
1201 const QColor& color,
1206 Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
1207 color.green() / 255.,
1208 color.blue() / 255.,
1210 obj->SetColor( CSFColor );
1214 myV3dViewer->Update();
1218 Changes display mode of object
1219 \param obj - object to be processed
1220 \param mode - new display mode
1221 \param update - update current viewer
1223 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
1224 int mode, bool update )
1226 myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
1228 myV3dViewer->Update();
1232 Changes transparency of object
1233 \param obj - object to be processed
1234 \param trans - new transparency
1235 \param update - update current viewer
1237 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
1238 float trans, bool update )
1240 myAISContext->SetTransparency( obj, trans, false );
1241 myAISContext->Redisplay( obj, Standard_False, Standard_True );
1243 myV3dViewer->Update();
1247 Changes visibility of trihedron to opposite
1249 void OCCViewer_Viewer::toggleTrihedron()
1251 setTrihedronShown( !isTrihedronVisible() );
1255 \return true if trihedron is visible
1257 bool OCCViewer_Viewer::isTrihedronVisible() const
1259 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
1263 Sets visibility state of trihedron
1264 \param on - new state
1267 void OCCViewer_Viewer::setTrihedronShown( const bool on )
1269 if ( myTrihedron.IsNull() )
1273 myAISContext->Display( myTrihedron,
1275 -1 /* selection mode */,
1276 Standard_True /* update viewer*/,
1277 Standard_False /* allow decomposition */,
1278 AIS_DS_Displayed /* display status */);
1279 myAISContext->Deactivate( myTrihedron );
1282 myAISContext->Erase( myTrihedron );
1287 \return trihedron size
1289 double OCCViewer_Viewer::trihedronSize() const
1292 if ( !myTrihedron.IsNull() )
1293 sz = myTrihedron->Size();
1298 Changes trihedron size
1299 \param sz - new size
1301 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
1303 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
1304 myTrihedronSize = sz;
1305 myIsRelative = isRelative;
1311 Set number of isolines
1312 \param u - u-isolines (first parametric co-ordinate)
1313 \param v - v-isolines (second parametric co-ordinate)
1315 void OCCViewer_Viewer::setIsos( const int u, const int v )
1317 Handle(AIS_InteractiveContext) ic = getAISContext();
1321 ic->SetIsoNumber( u, AIS_TOI_IsoU );
1322 ic->SetIsoNumber( v, AIS_TOI_IsoV );
1326 \return number of isolines
1327 \param u - to return u-isolines (first parametric co-ordinate)
1328 \param v - to return v-isolines (second parametric co-ordinate)
1330 void OCCViewer_Viewer::isos( int& u, int& v ) const
1332 Handle(AIS_InteractiveContext) ic = getAISContext();
1335 u = ic->IsoNumber( AIS_TOI_IsoU );
1336 v = ic->IsoNumber( AIS_TOI_IsoV );
1341 * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
1343 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
1345 return new OCCViewer_ViewWindow(0, this);
1349 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
1351 return background( theViewId ).color();
1354 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
1356 return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
1360 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
1362 if ( theColor.isValid() ) {
1363 Qtx::BackgroundData bg = background( theViewId );
1364 bg.setColor( theColor );
1365 setBackground( theViewId, bg );
1369 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
1371 if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
1372 myBackgrounds[theViewId] = theBackground;
1377 Set the show static trihedron flag
1379 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
1381 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1382 if ( aView ) aView->showStaticTrihedron( on );
1386 Get new and current trihedron size corresponding to the current model size
1388 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
1393 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
1394 Handle(V3d_Viewer) viewer = getViewer3d();
1395 viewer->InitActiveViews();
1396 if(!viewer->MoreActiveViews()) return false;
1398 Handle(V3d_View) view3d = viewer->ActiveView();
1401 if ( view3d.IsNull() )
1404 double aMaxSide = computeSceneSize( view3d );
1407 // The boundary box of the view may be initialized but nullified
1408 // (case of infinite objects)
1409 if ( aMaxSide < Precision::Confusion() )
1412 float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
1414 static float EPS = 5.0E-3;
1415 theSize = getTrihedron()->Size();
1416 theNewSize = aMaxSide*aSizeInPercents / 100.0;
1418 return fabs( theNewSize - theSize ) > theSize * EPS ||
1419 fabs( theNewSize - theSize ) > theNewSize * EPS;
1423 * Compute scene size
1425 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
1427 double aMaxSide = 0;
1428 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
1430 #if OCC_VERSION_LARGE > 0x06070100
1431 Bnd_Box aBox = view3d->View()->MinMaxValues();
1432 Xmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().X();
1433 Ymin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Y();
1434 Zmin = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Z();
1435 Xmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().X();
1436 Ymax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Y();
1437 Zmax = aBox.IsVoid() ? RealLast() : aBox.CornerMax().Z();
1439 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
1442 if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
1443 Xmax != RealLast() && Ymax != RealLast() && Zmax != RealLast() )
1445 aMaxSide = Xmax - Xmin;
1446 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
1447 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
1454 * Update the size of the trihedron
1456 void OCCViewer_Viewer::updateTrihedron() {
1457 if ( myTrihedron.IsNull() )
1461 double newSz, oldSz;
1463 if(computeTrihedronSize(newSz, oldSz))
1464 myTrihedron->SetSize(newSz);
1466 } else if(myTrihedron->Size() != myTrihedronSize) {
1467 myTrihedron->SetSize(myTrihedronSize);
1472 Set number of isolines
1473 \param u - u-isolines (first parametric co-ordinate)
1474 \param v - v-isolines (second parametric co-ordinate)
1476 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
1478 myPreselectionEnabled = isPreselectionEnabled;
1479 mySelectionEnabled = isSelectionEnabled;
1480 //clear current selection in the viewer
1482 if(!mySelectionEnabled) {
1483 myAISContext->ClearSelected( Standard_True );
1488 Creates clipping plane based on the incoming plane
1490 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1492 Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1493 aGraphic3dPlane->SetOn( theIsOn );
1494 aGraphic3dPlane->SetCapping( Standard_True );
1496 // set capping color
1497 Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
1498 aMaterialAspect.SetColor( Quantity_Color( myClippingColor.redF(), myClippingColor.greenF(),
1499 myClippingColor.blueF(), Quantity_TOC_RGB ) );
1500 aGraphic3dPlane->SetCappingMaterial( aMaterialAspect );
1502 // set capping texture
1503 aGraphic3dPlane->SetCappingTexture( initClippingTexture( myDefaultTextureUsed, myClippingTexture,
1504 myTextureModulated, myClippingTextureScale ) );
1506 return aGraphic3dPlane;
1509 Applies clipping planes to clippable objects
1511 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1513 // 1. Remove existing clipping planes
1514 myClipPlanes.clear();
1515 myInternalClipPlanes.Clear();
1517 // 2. Create new clipping planes
1518 ClipPlanesList::iterator inIt = theList.begin();
1519 for (;inIt != theList.end(); inIt++ )
1521 OCCViewer_ClipPlane aPlane = *inIt;
1523 double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1524 aPlane.OrientationToXYZ( aDx, aDy, aDz );
1526 gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1527 gp_Dir aDirection( aDx, aDy, aDz );
1529 myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1530 myClipPlanes.push_back( aPlane );
1533 // 3. Apply clipping planes
1534 AIS_ListOfInteractive aList;
1535 myAISContext->DisplayedObjects (aList);
1536 for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
1537 Handle(AIS_InteractiveObject) anObj = anIter.Value();
1538 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
1539 if (!aShape.IsNull() && aShape->IsClippable()) {
1540 aShape->SetClipPlanes(myInternalClipPlanes);
1546 Returns the clipping planes applied to the displayed objects.
1548 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1549 return myClipPlanes;
1552 Applies clipping planes to given object objects
1554 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1556 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1557 if (!aShape.IsNull() && aShape->IsClippable())
1559 aShape->SetClipPlanes (myInternalClipPlanes);
1564 Returns the pointer to the clipping dialog box.
1566 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1567 return myClippingDlg;
1572 Stores pointer to the clipping dialog box.
1574 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1575 if(myClippingDlg != theDlg) {
1576 myClippingDlg = theDlg;
1581 bool OCCViewer_Viewer::enableDrawMode( bool on )
1583 //!! To be done for view windows
1584 if ( !myViewManager )
1588 QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
1589 for ( int i = 0; i < (int)wins.count(); i++ )
1591 OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
1593 prev = prev || win->enableDrawMode( on );