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