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