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