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