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