Salome HOME
Merge from V6_main 01/04/2013
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewModel.cxx
1 // Copyright (C) 2007-2013  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.
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
29 #include "SUIT_ViewWindow.h"
30 #include "SUIT_ViewManager.h"
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SUIT_ResourceMgr.h"
34
35 #include "QtxActionToolMgr.h"
36 #include "QtxBackgroundTool.h"
37
38 #include <QPainter>
39 #include <QApplication>
40 #include <QColorDialog>
41 #include <QFileDialog>
42 #include <QPalette>
43 #include <QKeyEvent>
44 #include <QMenu>
45 #include <QMouseEvent>
46 #include <QToolBar>
47 #include <QDesktopWidget>
48
49 #include <AIS_Axis.hxx>
50 #include <AIS_Drawer.hxx>
51 #include <AIS_ListOfInteractive.hxx>
52 #include <AIS_ListIteratorOfListOfInteractive.hxx>
53
54 #include <Geom_Axis2Placement.hxx>
55 #include <Prs3d_Drawer.hxx>
56 #include <Prs3d_DatumAspect.hxx>
57 #include <Prs3d_LineAspect.hxx>
58 #include <Prs3d_LengthAspect.hxx>
59 #include <Prs3d_AngleAspect.hxx>
60 #include <Prs3d_TextAspect.hxx>
61
62 #include <Visual3d_View.hxx>
63
64 #include <Basics_OCCTVersion.hxx>
65
66 // VSR: Uncomment below line to allow texture background support in OCC viewer
67 #define OCC_ENABLE_TEXTURED_BACKGROUND
68
69 /*!
70   Get data for supported background modes: gradient types, identifiers and supported image formats
71 */
72 QString OCCViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
73 {
74   gradList << tr("GT_HORIZONTALGRADIENT")    << tr("GT_VERTICALGRADIENT")       <<
75               tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT") <<
76               tr("GT_FIRSTCORNERGRADIENT")   << tr("GT_SECONDCORNERGRADIENT")   <<
77               tr("GT_THIRDCORNERGRADIENT")   << tr("GT_FORTHCORNERGRADIENT");
78   idList   << HorizontalGradient             << VerticalGradient  <<
79               Diagonal1Gradient              << Diagonal2Gradient <<
80               Corner1Gradient                << Corner2Gradient   <<
81               Corner3Gradient                << Corner4Gradient;
82 #if OCC_VERSION_LARGE > 0x06050200 // enabled since OCCT 6.5.3, since in previous version this functionality is buggy
83 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
84   txtList  << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
85 #endif
86 #endif
87   return tr("BG_IMAGE_FILES");
88 }
89
90 /*!
91   Constructor
92   \param DisplayTrihedron - is trihedron displayed
93 */
94 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
95 : SUIT_ViewModel(),
96   myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
97   myIsRelative(true),
98   myTopLayerId( 0 ),
99   myTrihedronSize(100)
100 {
101   // init CasCade viewers
102   myV3dViewer = OCCViewer_VService::Viewer3d( "", (short*) "Viewer3d", "", 1000.,
103                                               V3d_XposYnegZpos, true, true );
104
105   myV3dViewer->Init();
106
107   myV3dCollector = OCCViewer_VService::Viewer3d( "", (short*) "Collector3d", "", 1000.,
108                                                  V3d_XposYnegZpos, true, true );
109   myV3dCollector->Init();
110
111   // init selector
112   myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector);
113
114   myAISContext->SelectionColor( Quantity_NOC_WHITE );
115   
116   // display isoline on planar faces (box for ex.)
117   myAISContext->IsoOnPlane( true );
118
119   double h = QApplication::desktop()->screenGeometry( QApplication::desktop()->primaryScreen() ).height() / 300. ;
120   Handle(Prs3d_Drawer) drawer = myAISContext->DefaultDrawer();
121   Handle(Prs3d_TextAspect) ta = drawer->TextAspect();
122   ta->SetHeight(100); // VSR: workaround for CAS.CADE bug (is it really needed ???)
123   ta->SetHeight(h);
124   drawer->SetTextAspect(ta);
125   drawer->AngleAspect()->SetTextAspect(ta);
126   drawer->LengthAspect()->SetTextAspect(ta);
127   
128   /* create trihedron */
129   if( DisplayTrihedron )
130   {
131     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
132     myTrihedron = new AIS_Trihedron(anAxis);
133     myTrihedron->SetInfiniteState( Standard_True );
134
135     Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
136     //myTrihedron->SetColor( Col );
137     myTrihedron->SetArrowColor( Col.Name() );
138     myTrihedron->SetSize(100);
139     Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
140     if (drawer->HasDatumAspect()) {
141         Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
142         daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
143         daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
144         daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
145     }
146
147     myAISContext->Display(myTrihedron);
148     myAISContext->Deactivate(myTrihedron);
149     }
150
151   // set interaction style to standard
152   myInteractionStyle = 0;
153
154   // set zooming style to standard
155   myZoomingStyle = 0;
156
157   // selection
158   mySelectionEnabled = true;
159   myMultiSelectionEnabled = true;
160   
161   
162   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
163   if(resMgr)
164     myShowStaticTrihedron = resMgr->booleanValue( "OCCViewer", "show_static_trihedron", true );
165 }
166
167 /*!
168   Destructor
169 */
170 OCCViewer_Viewer::~OCCViewer_Viewer() 
171 {
172   myAISContext.Nullify();
173   myV3dViewer.Nullify();
174   myV3dCollector.Nullify();
175 }
176
177 /*!
178   [obsolete]
179   \return background color of viewer
180 */
181 QColor OCCViewer_Viewer::backgroundColor() const
182 {
183   return backgroundColor(0);
184 }
185
186 /*!
187   \return background data of viewer
188 */
189 Qtx::BackgroundData OCCViewer_Viewer::background() const
190 {
191   return background(0);
192 }
193
194 /*!
195   Sets background color [obsolete]
196   \param c - new background color
197 */
198 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
199 {
200   setBackgroundColor( 0, c );
201 }
202
203 /*!
204   Sets background data
205   \param d - new background data
206 */
207 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
208 {
209   setBackground( 0, theBackground );
210 }
211
212 /*!
213   Start initialization of view window
214   \param view - view window to be initialized
215 */
216 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
217 {
218   if ( view ) {
219     view->initLayout();
220     view->initSketchers();
221     view->setInteractionStyle( interactionStyle() );
222     view->setZoomingStyle( zoomingStyle() );
223     
224     OCCViewer_ViewPort3d* vp3d = view->getViewPort();
225     if ( vp3d )
226     {
227       vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
228     }
229   }
230 }
231
232 /*!
233   Creates new view window
234   \param theDesktop - main window of application
235 */
236 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
237 {
238   // create view frame
239   OCCViewer_ViewFrame* view = new OCCViewer_ViewFrame(theDesktop, this);
240   // get main view window (created by view frame)
241   OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
242   // initialize main view window
243   initView( vw );
244   // set default background for view window
245   vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
246   // connect signal from viewport
247   connect(view->getViewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
248   return view;
249 }
250
251 /*!
252   Sets new view manager
253   \param theViewManager - new view manager
254 */
255 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
256 {
257   SUIT_ViewModel::setViewManager(theViewManager);
258   if (theViewManager) {
259     connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
260             this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
261
262     connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
263             this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
264
265     connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
266             this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
267
268     connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), 
269             this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
270   }
271 }
272
273 /*!
274   SLOT: called on mouse button press, stores current mouse position as start point for transformations
275 */
276 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
277 {
278   myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
279 }
280
281 /*!
282   SLOT: called on mouse move, processes transformation or hilighting
283 */
284 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
285 {
286   if (!mySelectionEnabled) return;
287   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
288
289   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
290
291   if ( isSelectionEnabled() ) {
292     if (aView->getViewPort()->isBusy()) {
293       QCoreApplication::processEvents();
294       return; // Check that the ViewPort initialization completed
295                                                 // To Prevent call move event if the View port is not initialized
296                                                 // IPAL 20883
297     }
298     Handle(V3d_View) aView3d = aView->getViewPort()->getView();
299     if ( !aView3d.IsNull() ) {
300       myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
301     }
302   }
303 }
304
305
306 /*!
307   SLOT: called on mouse button release, finishes transformation or selection
308 */
309 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
310 {
311   if (!mySelectionEnabled) return;
312   if (theEvent->button() != Qt::LeftButton) return;
313   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
314
315   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
316   if (!aView || aView->interactionStyle() != SUIT_ViewModel::STANDARD)
317     return;
318
319   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
320   bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
321   
322   if (!aHasShift) emit deselection();
323
324   if (myStartPnt == myEndPnt)
325   {
326     if (aHasShift && myMultiSelectionEnabled)
327       myAISContext->ShiftSelect();
328     else
329       myAISContext->Select();
330   }
331   else
332   {
333     if (aHasShift && myMultiSelectionEnabled)
334       myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
335                                 myEndPnt.x(), myEndPnt.y(),
336                                 aView->getViewPort()->getView(), Standard_False );
337     else
338       myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
339                            myEndPnt.x(), myEndPnt.y(),
340                            aView->getViewPort()->getView(), Standard_False );
341
342     int Nb = myAISContext->NbSelected();
343     if( Nb>1 && !myMultiSelectionEnabled )
344     {
345         myAISContext->InitSelected();
346         Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
347         if( !anOwner.IsNull() )
348         {
349             myAISContext->ClearSelected( Standard_False );
350             myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
351         }
352     }
353
354     myAISContext->UpdateCurrentViewer();
355   }
356   emit selectionChanged();
357 }
358
359 /*!
360   SLOT: called on key press, processes selection in "key free" interaction style
361 */
362 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
363 {
364   if (!mySelectionEnabled) return;
365   if (theEvent->key() != Qt::Key_S) return;
366   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
367
368   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
369   if (!aView || aView->interactionStyle() != SUIT_ViewModel::KEY_FREE)
370     return;
371
372   emit deselection();
373   myAISContext->Select();
374
375   emit selectionChanged();
376 }
377
378 void OCCViewer_Viewer::onViewClosed()
379 {
380   Standard_Integer aViewsNb = 0;
381   for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
382     ++aViewsNb;
383   if ( aViewsNb < 2 ) {
384     //clean up presentations before last view is closed
385     myAISContext->RemoveAll(Standard_False);
386   }
387 }
388
389 int OCCViewer_Viewer::getTopLayerId()
390 {
391 #if OCC_VERSION_LARGE > 0x06050200
392   if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )    
393     myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
394 #endif
395
396   return myTopLayerId;
397 }
398
399 /*!
400   \return interaction style
401 */
402 int OCCViewer_Viewer::interactionStyle() const
403 {
404   return myInteractionStyle;
405 }
406
407 /*!
408   Sets interaction style: 0 - standard, 1 - keyboard free interaction
409   \param theStyle - new interaction style
410 */
411 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
412 {
413   myInteractionStyle = theStyle;
414   //!! To be done for view windows
415   if ( !myViewManager )
416     return;
417
418   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
419   for ( int i = 0; i < (int)wins.count(); i++ )
420   {
421     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
422     if ( win )
423       win->setInteractionStyle( theStyle );
424   }
425 }
426
427 /*!
428   \return zooming style
429 */
430 int OCCViewer_Viewer::zoomingStyle() const
431 {
432   return myZoomingStyle;
433 }
434
435 /*!
436   Sets zooming style: 0 - standard, 1 - advanced (at cursor)
437   \param theStyle - new zooming style
438 */
439 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
440 {
441   myZoomingStyle = theStyle;
442   //!! To be done for view windows
443   if ( !myViewManager )
444     return;
445
446   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
447   for ( int i = 0; i < (int)wins.count(); i++ )
448   {
449     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
450     if ( win )
451       win->setZoomingStyle( theStyle );
452   }
453 }
454
455 /*!
456   Sets selection enabled status
457   \param isEnabled - new status
458 */
459 void OCCViewer_Viewer::enableSelection(bool isEnabled)
460 {
461   mySelectionEnabled = isEnabled;
462   //!! To be done for view windows
463   if ( !myViewManager )
464     return;
465
466   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
467   for ( int i = 0; i < (int)wins.count(); i++ )
468   {
469     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
470     if ( win )
471       win->updateEnabledDrawMode();
472   }
473 }
474
475 /*!
476   Sets multiselection enabled status
477   \param isEnabled - new status
478 */
479 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
480 {
481   myMultiSelectionEnabled = isEnable;
482   //!! To be done for view windows
483   if ( !myViewManager )
484     return;
485
486   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
487   for ( int i = 0; i < (int)wins.count(); i++ )
488   {
489     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
490     if ( win )
491       win->updateEnabledDrawMode();
492   }
493 }
494
495 /*!
496   Builds popup for occ viewer
497 */
498 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
499 {
500   thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
501   thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
502
503   thePopup->addSeparator();
504
505   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
506
507   //Support of several toolbars in the popup menu
508   QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
509   QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
510   for ( ; it!=last; it++ ) {
511     if ( (*it)->parentWidget()->isVisible() )
512       thePopup->addAction( (*it)->toggleViewAction() );
513   }
514 }
515
516 /*!
517   SLOT: called on dump view operation is activated, stores scene to raster file
518 */
519 void OCCViewer_Viewer::onDumpView()
520 {
521   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
522   if ( aView )
523     aView->onDumpView();
524 }
525
526 /*!
527   SLOT: called if background color is to be changed changed, passes new color to view port
528 */
529 void OCCViewer_Viewer::onChangeBackground()
530 {
531   OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
532   if ( !aView )
533     return;
534
535   // get supported gradient types
536   QStringList gradList;
537   QIntList    idList, txtList;
538   QString     formats = backgroundData( gradList, idList, txtList );
539
540   // invoke dialog box
541   Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(),  // initial background
542                                                                    aView,                // parent for dialog box
543                                                                    txtList,              // allowed texture modes
544                                                                    true,                 // enable solid color mode
545                                                                    true,                 // enable gradient mode
546                                                                    false,                // disable custom gradient mode
547                                                                    !txtList.isEmpty(),   // enable/disable texture mode
548                                                                    gradList,             // gradient names
549                                                                    idList,               // gradient identifiers
550                                                                    formats );            // image formats
551
552   // set chosen background data to the viewer
553   if ( bgData.isValid() )
554     aView->setBackground( bgData );
555 }
556
557 /*!
558   Updates OCC 3D viewer
559 */
560 void OCCViewer_Viewer::update()
561 {
562   if (!myV3dViewer.IsNull())
563     myV3dViewer->Update();
564
565   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
566   if ( aView )
567     aView->updateGravityCoords();
568 }
569
570 /*!
571   \return objects selected in 3D viewer
572   \param theList - list to be filled with selected objects
573 */
574 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
575 {
576   theList.Clear();
577   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
578     theList.Append(myAISContext->SelectedInteractive());
579 }
580
581 /*!
582   Selects objects in 3D viewer. Other selected objects are left as selected
583   \param theList - list objects to be selected
584 */
585 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
586 {
587   AIS_ListIteratorOfListOfInteractive aIt;
588   for (aIt.Initialize(theList); aIt.More(); aIt.Next())
589     myAISContext->AddOrRemoveSelected(aIt.Value(), false);
590   myAISContext->UpdateCurrentViewer();
591 }
592
593 /*!
594   Auxiliary method to emit signal selectionChanged()
595 */
596 void OCCViewer_Viewer::performSelectionChanged()
597 {
598     emit selectionChanged();
599 }
600
601 /*!
602   Hilights/unhilights object in viewer
603   \param obj - object to be updated
604   \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
605   \param update - update current viewer
606 */
607 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
608                                   bool hilight, bool update )
609 {
610   bool isInLocal = myAISContext->HasOpenedContext();
611   if( !obj.IsNull() )
612     if( !isInLocal )
613     {
614       if ( hilight && !myAISContext->IsSelected( obj ) )
615         myAISContext->AddOrRemoveCurrentObject( obj, false );
616       else if ( !hilight && myAISContext->IsSelected( obj ) )
617         myAISContext->AddOrRemoveCurrentObject( obj, false );
618     }
619
620   if ( update )
621     myV3dViewer->Redraw();
622     
623   return false;
624 }
625
626 /*!
627   Unhilights all objects in viewer
628   \param updateviewer - update current viewer
629 */
630 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
631 {
632   if ( myAISContext->HasOpenedContext() )
633     myAISContext->ClearSelected( updateviewer );
634   else
635     myAISContext->ClearCurrents( updateviewer );
636   return false;
637 }
638
639 /*!
640   \return true if object is in viewer or in collector
641   \param obj - object to be checked
642   \param onlyInViewer - search object only in viewer (so object must be displayed)
643 */
644 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
645                                    bool onlyInViewer )
646 {
647   AIS_ListOfInteractive List;
648   myAISContext->DisplayedObjects(List);
649
650   if( !onlyInViewer )
651   {
652     AIS_ListOfInteractive List1;
653     myAISContext->ObjectsInCollector(List1);
654     List.Append(List1);
655   }
656
657   AIS_ListIteratorOfListOfInteractive ite(List);
658   for ( ; ite.More(); ite.Next() )
659     if( ite.Value()==obj )
660       return true;
661
662   return false;
663 }
664
665 /*!
666   \return true if object is displayed in viewer
667   \param obj - object to be checked
668 */
669 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
670 {
671   return myAISContext->IsDisplayed( obj );
672 }
673
674 /*!
675   Sets color of object
676   \param obj - object to be updated
677   \param color - new color
678   \param update - update current viewer
679 */
680 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
681                                  const QColor& color,
682                                  bool update )
683 {
684   if( !obj.IsNull() )
685   {
686     Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
687                                                color.green() / 255.,
688                                                color.blue() / 255.,
689                                                Quantity_TOC_RGB );
690     obj->SetColor( CSFColor );
691   }
692
693   if( update )
694     myV3dViewer->Update();
695 }
696
697 /*!
698   Changes display mode of object
699   \param obj - object to be processed
700   \param mode - new display mode
701   \param update - update current viewer
702 */
703 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
704                                              int mode, bool update )
705 {
706   myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
707   if( update )
708     myV3dViewer->Update();
709 }
710
711 /*!
712   Changes transparency of object
713   \param obj - object to be processed
714   \param trans - new transparency
715   \param update - update current viewer
716 */
717 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
718                                         float trans, bool update )
719 {
720   myAISContext->SetTransparency( obj, trans, false );
721   myAISContext->Redisplay( obj, Standard_False, Standard_True );
722   if( update )
723     myV3dViewer->Update();
724 }
725
726 /*!
727   Changes visibility of trihedron to opposite
728 */
729 void OCCViewer_Viewer::toggleTrihedron()
730 {
731   setTrihedronShown( !isTrihedronVisible() );
732 }
733
734 /*!
735   \return true if trihedron is visible
736 */
737 bool OCCViewer_Viewer::isTrihedronVisible() const
738 {
739   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
740 }
741
742 /*!
743   Sets visibility state of trihedron
744   \param on - new state
745 */
746
747 void OCCViewer_Viewer::setTrihedronShown( const bool on )
748 {
749   if ( myTrihedron.IsNull() )
750     return;
751
752   if ( on )
753     myAISContext->Display( myTrihedron );
754   else
755     myAISContext->Erase( myTrihedron );
756 }
757
758 /*!
759   \return trihedron size
760 */
761 double OCCViewer_Viewer::trihedronSize() const
762 {
763   double sz = 0;
764   if ( !myTrihedron.IsNull() )
765     sz = myTrihedron->Size();
766   return sz;
767 }
768
769 /*!
770   Changes trihedron size
771   \param sz - new size
772 */
773 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
774 {
775   if ( myTrihedronSize != sz || isRelative != myIsRelative) {
776     myTrihedronSize = sz; 
777     myIsRelative = isRelative;
778     updateTrihedron();
779   }
780 }
781
782 /*!
783   Set number of isolines
784   \param u - u-isolines (first parametric co-ordinate)
785   \param v - v-isolines (second parametric co-ordinate)
786 */
787 void OCCViewer_Viewer::setIsos( const int u, const int v )
788 {
789   Handle(AIS_InteractiveContext) ic = getAISContext();
790   if ( ic.IsNull() )
791   return;
792
793   ic->SetIsoNumber( u, AIS_TOI_IsoU );
794   ic->SetIsoNumber( v, AIS_TOI_IsoV );
795 }
796
797 /*!
798   \return number of isolines
799   \param u - to return u-isolines (first parametric co-ordinate)
800   \param v - to return v-isolines (second parametric co-ordinate)
801 */
802 void OCCViewer_Viewer::isos( int& u, int& v ) const
803 {
804   Handle(AIS_InteractiveContext) ic = getAISContext();
805   if ( !ic.IsNull() )
806   {
807     u = ic->IsoNumber( AIS_TOI_IsoU );
808     v = ic->IsoNumber( AIS_TOI_IsoV );
809   }
810 }
811
812 /* 
813  * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
814  */
815 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
816 {
817   return new OCCViewer_ViewWindow( 0,  this);
818 }
819
820 // obsolete  
821 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
822 {
823   return background( theViewId ).color();
824 }
825
826 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
827 {
828   return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
829 }
830
831 // obsolete
832 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
833 {
834   if ( theColor.isValid() ) {
835     Qtx::BackgroundData bg = background( theViewId );
836     bg.setColor( theColor );
837     setBackground( theViewId, bg );
838   }
839 }
840
841 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
842 {
843   if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
844     myBackgrounds[theViewId] = theBackground;    
845 }
846
847
848 /*!
849   Set the show static trihedron flag
850 */
851 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on) {
852   if(myShowStaticTrihedron != on) {
853     OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
854     if(!aView)
855       return;
856
857     OCCViewer_ViewPort3d* vp3d = aView->getViewPort();
858     if(vp3d) {
859       myShowStaticTrihedron = on;
860       vp3d->updateStaticTriedronVisibility();
861     }
862   }
863 }
864
865 /*!
866   Get new and current trihedron size corresponding to the current model size
867 */
868 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
869 {
870   theNewSize = 100;
871   theSize = 100;
872
873   //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
874   Handle(V3d_Viewer) viewer = getViewer3d();
875   viewer->InitActiveViews();
876   if(!viewer->MoreActiveViews()) return false;
877
878   Handle(V3d_View) view3d = viewer->ActiveView();
879   //SRN: END of fix
880
881   if ( view3d.IsNull() )
882     return false;
883
884   double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
885   double aMaxSide;
886
887   view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
888
889   if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
890        Xmax == RealLast()  || Ymax == RealLast()  || Zmax == RealLast() )
891     return false;
892
893   aMaxSide = Xmax - Xmin;
894   if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
895   if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
896
897   // IPAL21687
898   // The boundary box of the view may be initialized but nullified
899   // (case of infinite objects)
900   if ( aMaxSide < Precision::Confusion() )
901     return false;
902
903   float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("OCCViewer","trihedron_size", 100.);
904
905   static float EPS = 5.0E-3;
906   theSize = getTrihedron()->Size();
907   theNewSize = aMaxSide*aSizeInPercents / 100.0;
908
909   return fabs( theNewSize - theSize ) > theSize * EPS ||
910          fabs( theNewSize - theSize) > theNewSize * EPS;
911 }
912
913 /*! 
914  * Update the size of the trihedron
915  */
916 void OCCViewer_Viewer::updateTrihedron() {
917   if(myIsRelative){
918     double newSz, oldSz;
919     
920     if(computeTrihedronSize(newSz, oldSz))
921       myTrihedron->SetSize(newSz);
922     
923   } else if(myTrihedron->Size() != myTrihedronSize) {
924     myTrihedron->SetSize(myTrihedronSize);
925   }
926 }