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