Salome HOME
0023124: EDF 11219 GEOM: Ray tracing in the OCC viewer
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewModel.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
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"
30
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"
36
37 #include "ViewerData_AISShape.hxx"
38
39 #include "CAF_Tools.h"
40
41 #include <Basics_OCCTVersion.hxx>
42
43 #include "QtxActionToolMgr.h"
44 #include "QtxBackgroundTool.h"
45
46 #include <QPainter>
47 #include <QApplication>
48 #include <QColorDialog>
49 #include <QFileDialog>
50 #include <QPalette>
51 #include <QKeyEvent>
52 #include <QMenu>
53 #include <QMouseEvent>
54 #include <QToolBar>
55 #include <QDesktopWidget>
56
57 #include <AIS_Axis.hxx>
58 #if OCC_VERSION_LARGE > 0x06080000
59   #include <Prs3d_Drawer.hxx>
60 #else
61   #include <AIS_Drawer.hxx>
62 #endif
63 #include <AIS_ListOfInteractive.hxx>
64 #include <AIS_ListIteratorOfListOfInteractive.hxx>
65
66 #include <Graphic3d_Texture2Dmanual.hxx>
67 #include <Graphic3d_MaterialAspect.hxx>
68 #include <Graphic3d_TextureParams.hxx>
69
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>
75
76 #include <Visual3d_View.hxx>
77
78 #include <V3d_DirectionalLight.hxx>
79 #include <V3d_AmbientLight.hxx>
80
81 /*!
82   Get data for supported background modes: gradient types, identifiers and supported image formats
83 */
84 QString OCCViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
85 {
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");
96 }
97
98 /*!
99   Get data for supported stereo pair modes: stereo types and identifiers
100 */
101 void OCCViewer_Viewer::stereoData( QStringList& typeList, QIntList& idList)
102 {
103   typeList << tr("ST_QUADBUFFER")    << tr("ST_ANAGLYPH")         <<
104               tr("ST_ROWINTERLACED") << tr("ST_COLUMNINTERLACED") <<
105               tr("ST_CHESSBOARD")    << tr("ST_SIDEBYSIDE")       <<
106               tr("ST_OVERUNDER");
107   idList   << QuadBufferType    << AnaglyphType         <<
108               RowInterlacedType << ColumnInterlacedType <<
109               ChessBoardType    << SideBySideType       <<
110               OverUnderType;
111 }
112
113 /*!
114   Constructor
115   \param DisplayTrihedron - is trihedron displayed
116 */
117 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
118 : SUIT_ViewModel(),
119   myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
120   myIsRelative(true),
121   myTopLayerId( 0 ),
122   myTrihedronSize(100),
123   myClippingDlg (NULL)
124 {
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)
128   setDefaultLights();
129
130   // init selector
131   myAISContext = new AIS_InteractiveContext( myV3dViewer );
132   myAISContext->SelectionColor( Quantity_NOC_WHITE );
133   
134   // display isoline on planar faces (box for ex.)
135   myAISContext->IsoOnPlane( true );
136   
137   /* create trihedron */
138   if ( DisplayTrihedron )
139   {
140     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
141     myTrihedron = new AIS_Trihedron(anAxis);
142     myTrihedron->SetInfiniteState( Standard_True );
143
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()) {
151 #else
152       Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
153       if (drawer->HasDatumAspect()) {
154 #endif
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));
159     }
160   }
161
162   // set interaction style to standard
163   myInteractionStyle = 0;
164
165   // set zooming style to standard
166   myZoomingStyle = 0;
167
168   // preselection
169   myPreselectionEnabled = true;
170
171   // selection
172   mySelectionEnabled = true;
173   myMultiSelectionEnabled = true;
174
175   // set projection type to orthographic
176   myProjectionType = 0;
177   // set stereo parameters
178   myStereoType = 0;
179   myAnaglyphFilter = 0;
180   myToReverseStereo = 0;
181   myVSyncMode = 1;
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;
193
194 }
195
196 /*!
197   Destructor
198 */
199 OCCViewer_Viewer::~OCCViewer_Viewer() 
200 {
201   myAISContext.Nullify();
202   myV3dViewer.Nullify();
203 }
204
205 /*!
206   [obsolete]
207   \return background color of viewer
208 */
209 QColor OCCViewer_Viewer::backgroundColor() const
210 {
211   return backgroundColor(0);
212 }
213
214 /*!
215   \return background data of viewer
216 */
217 Qtx::BackgroundData OCCViewer_Viewer::background() const
218 {
219   return background(0);
220 }
221
222 /*!
223   Sets background color [obsolete]
224   \param c - new background color
225 */
226 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
227 {
228   setBackgroundColor( 0, c );
229 }
230
231 /*!
232   Sets background data
233   \param d - new background data
234 */
235 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
236 {
237   setBackground( 0, theBackground );
238 }
239
240 /*!
241   Start initialization of view window
242   \param view - view window to be initialized
243 */
244 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
245 {
246   if ( view ) {
247     view->initLayout();
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() );
261
262     OCCViewer_ViewPort3d* vp3d = view->getViewPort();
263     if ( vp3d )
264     {
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*)));
269     }
270   }
271 }
272
273 /*!
274   Creates new view window
275   \param theDesktop - main window of application
276 */
277 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
278 {
279   // create view frame
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
284   initView( vw );
285   // set default background for view window
286   vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
287
288   return view;
289 }
290
291 /*!
292   Sets new view manager
293   \param theViewManager - new view manager
294 */
295 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
296 {
297   SUIT_ViewModel::setViewManager(theViewManager);
298   if (theViewManager) {
299     connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
300             this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
301
302     connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
303             this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
304
305     connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
306             this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
307
308     connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), 
309             this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
310   }
311 }
312
313 /*!
314   SLOT: called on mouse button press, stores current mouse position as start point for transformations
315 */
316 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
317 {
318   myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
319 }
320
321 /*!
322   SLOT: called on mouse move, processes transformation or hilighting
323 */
324 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
325 {
326   if (!mySelectionEnabled) return;
327   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
328
329   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
330
331   myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
332
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
338                                                 // IPAL 20883
339     }
340     Handle(V3d_View) aView3d = aView->getViewPort()->getView();
341     if ( !aView3d.IsNull() ) {
342       myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
343     }
344   }
345 }
346
347
348 /*!
349   SLOT: called on mouse button release, finishes transformation or selection
350 */
351 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
352 {
353   if (!mySelectionEnabled) return;
354   if (theEvent->button() != Qt::LeftButton) return;
355   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
356
357   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
358   if (!aView )
359     return;
360
361   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
362   bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
363   
364   if (!aHasShift) {
365     myAISContext->ClearCurrents( false );
366     emit deselection();
367   }
368
369   if (myStartPnt == myEndPnt)
370   {
371     if ( !isPreselectionEnabled() ) {
372       Handle(V3d_View) aView3d = aView->getViewPort()->getView();
373       if ( !aView3d.IsNull() ) {
374         myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
375       }
376     }
377
378     if (aHasShift && myMultiSelectionEnabled)
379       myAISContext->ShiftSelect();
380     else
381       myAISContext->Select();
382   }
383   else
384   {
385     if (aHasShift && myMultiSelectionEnabled)
386       myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
387                                 myEndPnt.x(), myEndPnt.y(),
388                                 aView->getViewPort()->getView(), Standard_False );
389     else
390       myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
391                            myEndPnt.x(), myEndPnt.y(),
392                            aView->getViewPort()->getView(), Standard_False );
393
394     int Nb = myAISContext->NbSelected();
395     if( Nb>1 && !myMultiSelectionEnabled )
396     {
397         myAISContext->InitSelected();
398         Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
399         if( !anOwner.IsNull() )
400         {
401             myAISContext->ClearSelected( Standard_False );
402             myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
403         }
404     }
405
406     myAISContext->UpdateCurrentViewer();
407   }
408   emit selectionChanged();
409 }
410
411 /*!
412   SLOT: called on key press, processes selection in "key free" interaction style
413 */
414 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
415 {
416   if (!mySelectionEnabled) return;
417
418   OCCViewer_ViewWindow* aView = qobject_cast<OCCViewer_ViewWindow*>( theWindow );
419   if ( !aView ) return;
420
421   bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
422
423   switch ( theEvent->key() ) {
424   case  Qt::Key_S:
425     if (!aHasShift) {
426       myAISContext->ClearCurrents( false );
427       emit deselection();
428     }
429
430     if ( !isPreselectionEnabled() ) {
431       Handle(V3d_View) aView3d = aView->getViewPort()->getView();
432       if ( !aView3d.IsNull() ) {
433         myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
434       }
435     }
436
437     if (aHasShift && myMultiSelectionEnabled)
438       myAISContext->ShiftSelect();
439     else
440       myAISContext->Select();
441
442     emit selectionChanged();
443
444     break;
445   case  Qt::Key_N:
446     if ( isPreselectionEnabled() ) {
447       if ( getAISContext()->HasOpenedContext() )
448         getAISContext()->HilightNextDetected( aView->getViewPort()->getView() );
449     }
450     break;
451   case  Qt::Key_P:
452     if ( isPreselectionEnabled() ) {
453       if ( getAISContext()->HasOpenedContext() )
454         getAISContext()->HilightPreviousDetected( aView->getViewPort()->getView() );
455     }
456     break;
457   default:
458     break;
459   }
460 }
461
462 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
463 {
464   Standard_Integer aViewsNb = 0;
465   for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
466     ++aViewsNb;
467   if ( aViewsNb < 2 ) {
468     //clean up presentations before last view is closed
469     myAISContext->RemoveAll(Standard_False);
470   }
471 }
472
473 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
474 {
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 );
480 }
481
482 int OCCViewer_Viewer::getTopLayerId()
483 {
484   if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )    
485     myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
486
487   return myTopLayerId;
488 }
489
490 /*!
491   \return interaction style
492 */
493 int OCCViewer_Viewer::interactionStyle() const
494 {
495   return myInteractionStyle;
496 }
497
498 /*!
499   Sets interaction style: 0 - standard, 1 - keyboard free interaction
500   \param theStyle - new interaction style
501 */
502 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
503 {
504   myInteractionStyle = theStyle;
505   //!! To be done for view windows
506   if ( !myViewManager )
507     return;
508
509   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
510   for ( int i = 0; i < (int)wins.count(); i++ )
511   {
512     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
513     if ( win )
514       win->setInteractionStyle( theStyle );
515   }
516 }
517
518 /*!
519   \return projection type
520 */
521 int OCCViewer_Viewer::projectionType() const
522 {
523   return myProjectionType;
524 }
525
526 /*!
527   Sets projection type: 0 - orthographic, 1 - perspective
528   \param theType - new projection type
529 */
530 void OCCViewer_Viewer::setProjectionType( const int theType )
531 {
532   if ( myProjectionType != theType ) {
533     if ( theType != OCCViewer_ViewWindow::Stereo )
534       myProjectionType = theType;
535
536     if ( !myViewManager )
537       return;
538
539     QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
540     for ( int i = 0; i < (int)wins.count(); i++ )
541     {
542       OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
543       if ( win )
544         win->setProjectionType( (OCCViewer_ViewWindow::ProjectionType)theType );
545     }
546   }
547 }
548
549 /*!
550   \return stereo type
551 */
552 int OCCViewer_Viewer::stereoType() const
553 {
554   return myStereoType;
555 }
556
557 /*!
558   Sets stereo type
559   \param theType - new stereo type
560 */
561 void OCCViewer_Viewer::setStereoType( const int theType )
562 {
563   myStereoType = theType;
564
565   if ( !myViewManager )
566     return;
567
568   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
569   for ( int i = 0; i < (int)wins.count(); i++ )
570   {
571     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
572     if ( win )
573       win->setStereoType( (OCCViewer_ViewWindow::StereoType)theType );
574   }
575 }
576
577 /*!
578   \return stereographic focus type
579 */
580 int OCCViewer_Viewer::stereographicFocusType() const
581 {
582   return myStereographicFocusType;
583 }
584
585 /*!
586   \return stereographic focus value
587 */
588 double OCCViewer_Viewer::stereographicFocusValue() const
589 {
590   return myStereographicFocusValue;
591 }
592
593 /*!
594   Sets stereographic focus parameters
595   \param theType - new stereographic focus type
596   \param theValue - new stereographic focus value
597 */
598 void OCCViewer_Viewer::setStereographicFocus( const int theType, const double theValue )
599 {
600   myStereographicFocusType = theType;
601   myStereographicFocusValue = theValue;
602
603   if ( !myViewManager )
604     return;
605
606   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
607   for ( int i = 0; i < (int)wins.count(); i++ )
608   {
609     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
610     if ( win )
611       win->setStereographicFocus( (OCCViewer_ViewWindow::FocusIODType)theType, theValue );
612   }
613 }
614
615 /*!
616   \return stereographic focus type
617 */
618 int OCCViewer_Viewer::interocularDistanceType() const
619 {
620   return myInterocularDistanceType;
621 }
622
623 /*!
624   \return stereographic focus value
625 */
626 double OCCViewer_Viewer::interocularDistanceValue() const
627 {
628   return myInterocularDistanceValue;
629 }
630
631 /*!
632   Sets interocular distance parameters
633   \param theType - new IOD type
634   \param theValue - new IOD value
635 */
636 void OCCViewer_Viewer::setInterocularDistance( const int theType, const double theValue )
637 {
638   myInterocularDistanceType = theType;
639   myInterocularDistanceValue = theValue;
640
641   if ( !myViewManager )
642     return;
643
644   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
645   for ( int i = 0; i < (int)wins.count(); i++ )
646   {
647     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
648     if ( win )
649       win->setInterocularDistance( (OCCViewer_ViewWindow::FocusIODType)theType, theValue );
650   }
651 }
652
653 /*!
654   \return anaglyph filter
655 */
656 int OCCViewer_Viewer::anaglyphFilter() const
657 {
658   return myAnaglyphFilter;
659 }
660
661 /*!
662   Sets anaglyph filter
663   \param theType - new anaglyph filter
664 */
665 void OCCViewer_Viewer::setAnaglyphFilter( const int theType )
666 {
667   myAnaglyphFilter = theType;
668
669   if ( !myViewManager )
670     return;
671
672   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
673   for ( int i = 0; i < (int)wins.count(); i++ )
674   {
675     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
676     if ( win )
677       win->setAnaglyphFilter( (OCCViewer_ViewWindow::AnaglyphFilter)theType );
678   }
679 }
680
681 /*!
682   \return reverse stereo
683 */
684 bool OCCViewer_Viewer::isReverseStereo() const
685 {
686   return myToReverseStereo;
687 }
688
689 /*!
690   Sets reverse stereo
691   \param theReverse - enable/disable reverse mode
692 */
693 void OCCViewer_Viewer::setReverseStereo( const bool theReverse )
694 {
695   myToReverseStereo = theReverse;
696
697   if ( !myViewManager )
698     return;
699
700   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
701   for ( int i = 0; i < (int)wins.count(); i++ )
702   {
703     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
704     if ( win )
705       win->setReverseStereo( theReverse );
706   }
707 }
708
709 /*!
710   \return V-Sync mode
711 */
712 bool OCCViewer_Viewer::isVSync() const
713 {
714   return myVSyncMode;
715 }
716
717 /*!
718   Set V-Sync mode
719   \param theEnable - enable/disable V-Sync mode
720 */
721 void OCCViewer_Viewer::setVSync( const bool theEnable )
722 {
723   myVSyncMode = theEnable;
724
725   if ( !myViewManager )
726     return;
727
728   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
729   for ( int i = 0; i < (int)wins.count(); i++ )
730   {
731     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
732     if ( win )
733       win->setVSync( theEnable );
734   }
735 }
736
737 /*!
738   \return support quad-buffered stereo
739 */
740 bool OCCViewer_Viewer::isQuadBufferSupport() const
741 {
742   return myQuadBufferSupport;
743 }
744
745 /*!
746   Set support quad-buffered stereo
747   \param theEnable - enable/disable support quad-buffered stereo
748 */
749 void OCCViewer_Viewer::setQuadBufferSupport( const bool theEnable )
750 {
751   myQuadBufferSupport = theEnable;
752
753   if ( !myViewManager )
754     return;
755
756   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
757   for ( int i = 0; i < (int)wins.count(); i++ )
758   {
759     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
760     if ( win )
761       win->setQuadBufferSupport( theEnable );
762   }
763 }
764
765 /*!
766   \return zooming style
767 */
768 int OCCViewer_Viewer::zoomingStyle() const
769 {
770   return myZoomingStyle;
771 }
772
773 /*!
774   Sets zooming style: 0 - standard, 1 - advanced (at cursor)
775   \param theStyle - new zooming style
776 */
777 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
778 {
779   myZoomingStyle = theStyle;
780   //!! To be done for view windows
781   if ( !myViewManager )
782     return;
783
784   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
785   for ( int i = 0; i < (int)wins.count(); i++ )
786   {
787     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
788     if ( win )
789       win->setZoomingStyle( theStyle );
790   }
791 }
792
793 /*!
794   \return true if preselection is enabled
795 */
796 bool OCCViewer_Viewer::isPreselectionEnabled() const 
797
798   return myPreselectionEnabled; 
799 }
800
801 /*!
802   Enables/disables preselection
803   \param isEnabled - new status
804 */
805 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
806 {
807   myPreselectionEnabled = isEnabled;
808
809   if ( !myViewManager )
810     return;
811
812   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
813   for ( int i = 0; i < (int)wins.count(); i++ )
814   {
815     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
816     if ( win ) {
817       win->enablePreselection( isEnabled );
818     }
819   }
820 }
821
822 /*!
823   \return true if selection is enabled
824 */
825 bool OCCViewer_Viewer::isSelectionEnabled() const 
826
827   return mySelectionEnabled; 
828 }
829
830 /*!
831   Enables/disables selection
832   \param isEnabled - new status
833 */
834 void OCCViewer_Viewer::enableSelection(bool isEnabled)
835 {
836   mySelectionEnabled = isEnabled;
837
838   //!! To be done for view windows
839   if ( !myViewManager )
840     return;
841
842   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
843   for ( int i = 0; i < (int)wins.count(); i++ )
844   {
845     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
846     if ( win ) {
847       win->updateEnabledDrawMode();
848       win->enableSelection( isEnabled );
849     }
850   }
851
852   
853   //clear current selection in the viewer
854   if(!isEnabled) {
855     myAISContext->ClearSelected( Standard_True );
856   }
857
858 }
859
860 /*!
861   Sets multiselection enabled status
862   \param isEnabled - new status
863 */
864 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
865 {
866   myMultiSelectionEnabled = isEnable;
867   //!! To be done for view windows
868   if ( !myViewManager )
869     return;
870
871   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
872   for ( int i = 0; i < (int)wins.count(); i++ )
873   {
874     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
875     if ( win )
876       win->updateEnabledDrawMode();
877   }
878 }
879
880 /*!
881   Sets a color of the clipped region
882   \param theColor - a new color of the clipped region
883 */
884 void OCCViewer_Viewer::setClippingColor( const QColor& theColor )
885 {
886   myClippingColor = theColor;
887
888   if( myInternalClipPlanes.IsEmpty() )
889     return;
890
891   Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
892   aMaterialAspect.SetColor( Quantity_Color( theColor.redF(), theColor.greenF(),
893                                             theColor.blueF(), Quantity_TOC_RGB ) );
894
895   for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
896     myInternalClipPlanes.Value(i)->SetCappingMaterial( aMaterialAspect );
897
898   update();
899 }
900
901 /*!
902   \return clipping color
903 */
904 QColor OCCViewer_Viewer::clippingColor() const
905 {
906   return myClippingColor;
907 }
908
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 )
912 {
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 ) ) );
921   }
922   return aTexture;
923 }
924
925 /*!
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.
931 */
932 void OCCViewer_Viewer::setClippingTextureParams( const bool isDefault, const QString& theTexture,
933                                                  const bool isModulate, const double theScale )
934 {
935   myDefaultTextureUsed = isDefault;
936   myClippingTexture = theTexture;
937   myTextureModulated = isModulate;
938   myClippingTextureScale = theScale;
939
940   if( myInternalClipPlanes.IsEmpty() )
941     return;
942
943   Handle(Graphic3d_Texture2Dmanual) aTexture =
944     initClippingTexture( myDefaultTextureUsed, myClippingTexture,
945                          myTextureModulated, myClippingTextureScale );
946
947   for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
948     myInternalClipPlanes.Value(i)->SetCappingTexture( aTexture );
949
950   update();
951 }
952
953 /*!
954   \return true if default texture is used
955 */
956 bool OCCViewer_Viewer::isDefaultTextureUsed() const
957 {
958   return myDefaultTextureUsed;
959 }
960
961 /*!
962   \return clipping texture
963 */
964 QString OCCViewer_Viewer::clippingTexture() const
965 {
966   return myClippingTexture;
967 }
968
969 /*!
970   \return true if texture is modulated
971 */
972 bool OCCViewer_Viewer::isTextureModulated() const
973 {
974   return myTextureModulated;
975 }
976
977 /*!
978   \return scale factor of texture
979 */
980 double OCCViewer_Viewer::clippingTextureScale() const
981 {
982   return myClippingTextureScale;
983 }
984
985 /*!
986   Builds popup for occ viewer
987 */
988 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
989 {
990   thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
991   thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
992
993   thePopup->addSeparator();
994
995   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
996
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() );
1003   }
1004 }
1005
1006 /*!
1007   SLOT: called on dump view operation is activated, stores scene to raster file
1008 */
1009 void OCCViewer_Viewer::onDumpView()
1010 {
1011   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1012   if ( aView )
1013     aView->onDumpView();
1014 }
1015
1016 /*!
1017   SLOT: called if background color is to be changed changed, passes new color to view port
1018 */
1019 void OCCViewer_Viewer::onChangeBackground()
1020 {
1021   OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
1022   if ( !aView )
1023     return;
1024
1025   // get supported gradient types
1026   QStringList gradList;
1027   QIntList    idList, txtList;
1028   QString     formats = backgroundData( gradList, idList, txtList );
1029
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
1041
1042   // set chosen background data to the viewer
1043   if ( bgData.isValid() )
1044     aView->setBackground( bgData );
1045 }
1046
1047 /*!
1048   Updates OCC 3D viewer
1049 */
1050 void OCCViewer_Viewer::update()
1051 {
1052   if (!myV3dViewer.IsNull())
1053     myV3dViewer->Update();
1054
1055   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1056   if ( aView )
1057     aView->updateGravityCoords();
1058 }
1059
1060 /*!
1061   \return objects selected in 3D viewer
1062   \param theList - list to be filled with selected objects
1063 */
1064 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
1065 {
1066   theList.Clear();
1067   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
1068     theList.Append(myAISContext->SelectedInteractive());
1069 }
1070
1071 /*!
1072   Selects objects in 3D viewer. Other selected objects are left as selected
1073   \param theList - list objects to be selected
1074 */
1075 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
1076 {
1077   AIS_ListIteratorOfListOfInteractive aIt;
1078   for (aIt.Initialize(theList); aIt.More(); aIt.Next())
1079     myAISContext->AddOrRemoveSelected(aIt.Value(), false);
1080   myAISContext->UpdateCurrentViewer();
1081 }
1082
1083 /*!
1084   Auxiliary method to emit signal selectionChanged()
1085 */
1086 void OCCViewer_Viewer::performSelectionChanged()
1087 {
1088     emit selectionChanged();
1089 }
1090
1091 /*
1092  * Defines default lights
1093  */
1094 void OCCViewer_Viewer::setDefaultLights()
1095 {
1096   // clear all light sources
1097   myV3dViewer->InitDefinedLights();
1098   while ( myV3dViewer->MoreDefinedLights() )
1099   {
1100     myV3dViewer->DelLight( myV3dViewer->DefinedLight() );
1101     myV3dViewer->InitDefinedLights();
1102   }
1103
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 );
1109
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 ) );
1116 }
1117
1118 /*!
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
1123 */
1124 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
1125                                   bool hilight, bool update )
1126 {
1127   bool isInLocal = myAISContext->HasOpenedContext();
1128   if( !obj.IsNull() )
1129     if( !isInLocal )
1130     {
1131       if ( hilight && !myAISContext->IsSelected( obj ) )
1132         myAISContext->AddOrRemoveCurrentObject( obj, false );
1133       else if ( !hilight && myAISContext->IsSelected( obj ) )
1134         myAISContext->AddOrRemoveCurrentObject( obj, false );
1135     }
1136
1137   if ( update )
1138     myV3dViewer->Redraw();
1139     
1140   return false;
1141 }
1142
1143 /*!
1144   Unhilights all objects in viewer
1145   \param updateviewer - update current viewer
1146 */
1147 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
1148 {
1149   if ( myAISContext->HasOpenedContext() ) {
1150     if ( unselect ) {
1151       myAISContext->ClearSelected( updateviewer );
1152     } else {
1153       myAISContext->UnhilightSelected( updateviewer );
1154     }
1155   } else {
1156     if ( unselect ) {
1157       myAISContext->ClearCurrents( updateviewer );
1158     } else {
1159       myAISContext->UnhilightCurrents( updateviewer );
1160     }
1161   }
1162
1163   return false;
1164 }
1165
1166 /*!
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)
1170 */
1171 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
1172                                    bool onlyInViewer )
1173 {
1174   AIS_ListOfInteractive List;
1175   myAISContext->DisplayedObjects(List);
1176
1177   AIS_ListIteratorOfListOfInteractive ite(List);
1178   for ( ; ite.More(); ite.Next() )
1179     if( ite.Value()==obj )
1180       return true;
1181
1182   return false;
1183 }
1184
1185 /*!
1186   \return true if object is displayed in viewer
1187   \param obj - object to be checked
1188 */
1189 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
1190 {
1191   return myAISContext->IsDisplayed( obj );
1192 }
1193
1194 /*!
1195   Sets color of object
1196   \param obj - object to be updated
1197   \param color - new color
1198   \param update - update current viewer
1199 */
1200 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
1201                                  const QColor& color,
1202                                  bool update )
1203 {
1204   if( !obj.IsNull() )
1205   {
1206     Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
1207                                                color.green() / 255.,
1208                                                color.blue() / 255.,
1209                                                Quantity_TOC_RGB );
1210     obj->SetColor( CSFColor );
1211   }
1212
1213   if( update )
1214     myV3dViewer->Update();
1215 }
1216
1217 /*!
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
1222 */
1223 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
1224                                              int mode, bool update )
1225 {
1226   myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
1227   if( update )
1228     myV3dViewer->Update();
1229 }
1230
1231 /*!
1232   Changes transparency of object
1233   \param obj - object to be processed
1234   \param trans - new transparency
1235   \param update - update current viewer
1236 */
1237 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
1238                                         float trans, bool update )
1239 {
1240   myAISContext->SetTransparency( obj, trans, false );
1241   myAISContext->Redisplay( obj, Standard_False, Standard_True );
1242   if( update )
1243     myV3dViewer->Update();
1244 }
1245
1246 /*!
1247   Changes visibility of trihedron to opposite
1248 */
1249 void OCCViewer_Viewer::toggleTrihedron()
1250 {
1251   setTrihedronShown( !isTrihedronVisible() );
1252 }
1253
1254 /*!
1255   \return true if trihedron is visible
1256 */
1257 bool OCCViewer_Viewer::isTrihedronVisible() const
1258 {
1259   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
1260 }
1261
1262 /*!
1263   Sets visibility state of trihedron
1264   \param on - new state
1265 */
1266
1267 void OCCViewer_Viewer::setTrihedronShown( const bool on )
1268 {
1269   if ( myTrihedron.IsNull() )
1270     return;
1271
1272   if ( on ) {
1273     myAISContext->Display( myTrihedron,
1274                            0 /*wireframe*/,
1275                            -1 /* selection mode */,
1276                            Standard_True /* update viewer*/,
1277                            Standard_False /* allow decomposition */,
1278                            AIS_DS_Displayed /* display status */);
1279     myAISContext->Deactivate( myTrihedron );
1280   }
1281   else {
1282     myAISContext->Erase( myTrihedron );
1283   }
1284 }
1285
1286 /*!
1287   \return trihedron size
1288 */
1289 double OCCViewer_Viewer::trihedronSize() const
1290 {
1291   double sz = 0;
1292   if ( !myTrihedron.IsNull() )
1293     sz = myTrihedron->Size();
1294   return sz;
1295 }
1296
1297 /*!
1298   Changes trihedron size
1299   \param sz - new size
1300 */
1301 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
1302 {
1303   if ( myTrihedronSize != sz || isRelative != myIsRelative) {
1304     myTrihedronSize = sz; 
1305     myIsRelative = isRelative;
1306     updateTrihedron();
1307   }
1308 }
1309
1310 /*!
1311   Set number of isolines
1312   \param u - u-isolines (first parametric co-ordinate)
1313   \param v - v-isolines (second parametric co-ordinate)
1314 */
1315 void OCCViewer_Viewer::setIsos( const int u, const int v )
1316 {
1317   Handle(AIS_InteractiveContext) ic = getAISContext();
1318   if ( ic.IsNull() )
1319   return;
1320
1321   ic->SetIsoNumber( u, AIS_TOI_IsoU );
1322   ic->SetIsoNumber( v, AIS_TOI_IsoV );
1323 }
1324
1325 /*!
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)
1329 */
1330 void OCCViewer_Viewer::isos( int& u, int& v ) const
1331 {
1332   Handle(AIS_InteractiveContext) ic = getAISContext();
1333   if ( !ic.IsNull() )
1334   {
1335     u = ic->IsoNumber( AIS_TOI_IsoU );
1336     v = ic->IsoNumber( AIS_TOI_IsoV );
1337   }
1338 }
1339
1340 /* 
1341  * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
1342  */
1343 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
1344 {
1345   return new OCCViewer_ViewWindow(0,  this);
1346 }
1347
1348 // obsolete  
1349 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
1350 {
1351   return background( theViewId ).color();
1352 }
1353
1354 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
1355 {
1356   return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
1357 }
1358
1359 // obsolete
1360 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
1361 {
1362   if ( theColor.isValid() ) {
1363     Qtx::BackgroundData bg = background( theViewId );
1364     bg.setColor( theColor );
1365     setBackground( theViewId, bg );
1366   }
1367 }
1368
1369 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
1370 {
1371   if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
1372     myBackgrounds[theViewId] = theBackground;    
1373 }
1374
1375
1376 /*!
1377   Set the show static trihedron flag
1378 */
1379 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
1380 {
1381   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
1382   if ( aView ) aView->showStaticTrihedron( on );
1383 }
1384
1385 /*!
1386   Get new and current trihedron size corresponding to the current model size
1387 */
1388 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
1389 {
1390   theNewSize = 100;
1391   theSize = 100;
1392
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;
1397
1398   Handle(V3d_View) view3d = viewer->ActiveView();
1399   //SRN: END of fix
1400
1401   if ( view3d.IsNull() )
1402     return false;
1403
1404   double aMaxSide = computeSceneSize( view3d );
1405
1406   // IPAL21687
1407   // The boundary box of the view may be initialized but nullified
1408   // (case of infinite objects)
1409   if ( aMaxSide < Precision::Confusion() )
1410     return false;
1411
1412   float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
1413
1414   static float EPS = 5.0E-3;
1415   theSize = getTrihedron()->Size();
1416   theNewSize = aMaxSide*aSizeInPercents / 100.0;
1417
1418   return fabs( theNewSize - theSize ) > theSize    * EPS ||
1419          fabs( theNewSize - theSize ) > theNewSize * EPS;
1420 }
1421
1422 /*!
1423  * Compute scene size
1424  */
1425 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
1426 {
1427   double aMaxSide = 0;
1428   double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
1429
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();
1438 #else
1439   view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
1440 #endif
1441
1442   if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
1443        Xmax != RealLast()  && Ymax != RealLast()  && Zmax != RealLast() )
1444   {
1445     aMaxSide = Xmax - Xmin;
1446     if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
1447     if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
1448   }
1449
1450   return aMaxSide;
1451 }
1452
1453 /*! 
1454  * Update the size of the trihedron
1455  */
1456 void OCCViewer_Viewer::updateTrihedron() {
1457   if ( myTrihedron.IsNull() )
1458     return;
1459
1460   if(myIsRelative){
1461     double newSz, oldSz;
1462     
1463     if(computeTrihedronSize(newSz, oldSz))
1464       myTrihedron->SetSize(newSz);
1465     
1466   } else if(myTrihedron->Size() != myTrihedronSize) {
1467     myTrihedron->SetSize(myTrihedronSize);
1468   }
1469 }
1470
1471 /*!
1472   Set number of isolines
1473   \param u - u-isolines (first parametric co-ordinate)
1474   \param v - v-isolines (second parametric co-ordinate)
1475 */
1476 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
1477 {
1478   myPreselectionEnabled = isPreselectionEnabled;
1479   mySelectionEnabled = isSelectionEnabled;
1480   //clear current selection in the viewer
1481   
1482   if(!mySelectionEnabled) {
1483     myAISContext->ClearSelected( Standard_True );
1484   }
1485 }
1486
1487 /*!
1488   Creates clipping plane based on the incoming plane
1489 */
1490 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1491 {
1492   Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1493   aGraphic3dPlane->SetOn( theIsOn );
1494   aGraphic3dPlane->SetCapping( Standard_True );
1495
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 );
1501
1502   // set capping texture
1503   aGraphic3dPlane->SetCappingTexture( initClippingTexture( myDefaultTextureUsed, myClippingTexture,
1504                                                            myTextureModulated, myClippingTextureScale ) );
1505
1506   return aGraphic3dPlane;
1507 }
1508 /*!
1509   Applies clipping planes to clippable objects
1510 */
1511 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1512 {
1513   // 1. Remove existing clipping planes
1514   myClipPlanes.clear();
1515   myInternalClipPlanes.Clear();
1516
1517   // 2. Create new clipping planes
1518   ClipPlanesList::iterator inIt = theList.begin();
1519   for (;inIt != theList.end(); inIt++ )
1520   {
1521     OCCViewer_ClipPlane aPlane = *inIt;
1522
1523     double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1524     aPlane.OrientationToXYZ( aDx, aDy, aDz );
1525
1526     gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1527     gp_Dir aDirection( aDx, aDy, aDz );
1528
1529     myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1530     myClipPlanes.push_back( aPlane );
1531   }
1532
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);
1541     }
1542   }
1543 }
1544
1545 /*!
1546   Returns the clipping planes applied to the displayed objects.
1547 */
1548 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1549   return myClipPlanes;
1550 }
1551 /*!
1552   Applies clipping planes to given object objects
1553 */
1554 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1555 {
1556   Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1557   if (!aShape.IsNull() && aShape->IsClippable())
1558   {
1559     aShape->SetClipPlanes (myInternalClipPlanes);
1560   }
1561 }
1562
1563 /*!
1564   Returns the pointer to the clipping dialog box.
1565 */
1566 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1567   return myClippingDlg;
1568 }
1569
1570
1571 /*!
1572   Stores pointer to the clipping dialog box.
1573 */
1574 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1575   if(myClippingDlg != theDlg) {
1576     myClippingDlg = theDlg;
1577   }
1578 }
1579
1580
1581 bool OCCViewer_Viewer::enableDrawMode( bool on )
1582 {
1583   //!! To be done for view windows
1584   if ( !myViewManager )
1585     return false;
1586
1587   bool prev = false;
1588   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
1589   for ( int i = 0; i < (int)wins.count(); i++ )
1590   {
1591     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
1592     if ( win ) {
1593       prev = prev || win->enableDrawMode( on ); 
1594     }
1595   }
1596   return prev;
1597 }