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