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