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