Salome HOME
Merge branch 'master' of ssh://git.salome-platform.org/modules/gui
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewModel.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "OCCViewer_ViewModel.h"
24 #include "OCCViewer_ViewWindow.h"
25 #include "OCCViewer_ViewFrame.h"
26 #include "OCCViewer_VService.h"
27 #include "OCCViewer_ViewPort3d.h"
28 #include "OCCViewer_ClippingDlg.h"
29 #include "OCCViewer_Utilities.h"
30
31 #include "SUIT_ViewWindow.h"
32 #include "SUIT_ViewManager.h"
33 #include "SUIT_Desktop.h"
34 #include "SUIT_Session.h"
35 #include "SUIT_ResourceMgr.h"
36
37 #include "ViewerData_AISShape.hxx"
38
39 #include <Basics_OCCTVersion.hxx>
40
41 #include "QtxActionToolMgr.h"
42 #include "QtxBackgroundTool.h"
43
44 #include <QPainter>
45 #include <QApplication>
46 #include <QColorDialog>
47 #include <QFileDialog>
48 #include <QPalette>
49 #include <QKeyEvent>
50 #include <QMenu>
51 #include <QMouseEvent>
52 #include <QToolBar>
53 #include <QDesktopWidget>
54
55 #include <AIS_Axis.hxx>
56 #include <AIS_Drawer.hxx>
57 #include <AIS_ListOfInteractive.hxx>
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59
60 #include <Graphic3d_Texture2Dmanual.hxx>
61 #include <Graphic3d_TextureParams.hxx>
62
63 #include <Geom_Axis2Placement.hxx>
64 #include <Prs3d_Drawer.hxx>
65 #include <Prs3d_DatumAspect.hxx>
66 #include <Prs3d_LineAspect.hxx>
67 #include <Prs3d_TextAspect.hxx>
68
69 #include <Visual3d_View.hxx>
70
71 /*!
72   Get data for supported background modes: gradient types, identifiers and supported image formats
73 */
74 QString OCCViewer_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
75 {
76   gradList << tr("GT_HORIZONTALGRADIENT")    << tr("GT_VERTICALGRADIENT")       <<
77               tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT") <<
78               tr("GT_FIRSTCORNERGRADIENT")   << tr("GT_SECONDCORNERGRADIENT")   <<
79               tr("GT_THIRDCORNERGRADIENT")   << tr("GT_FORTHCORNERGRADIENT");
80   idList   << HorizontalGradient             << VerticalGradient  <<
81               Diagonal1Gradient              << Diagonal2Gradient <<
82               Corner1Gradient                << Corner2Gradient   <<
83               Corner3Gradient                << Corner4Gradient;
84   txtList  << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
85   return tr("BG_IMAGE_FILES");
86 }
87
88 /*!
89   Constructor
90   \param DisplayTrihedron - is trihedron displayed
91 */
92 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
93 : SUIT_ViewModel(),
94   myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
95   myIsRelative(true),
96   myTopLayerId( 0 ),
97   myTrihedronSize(100),
98   myClippingDlg (NULL)
99 {
100   // init CasCade viewers
101   myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
102   //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
103   myV3dViewer->SetDefaultLights();
104
105   // init selector
106   myAISContext = new AIS_InteractiveContext( myV3dViewer );
107   myAISContext->SelectionColor( Quantity_NOC_WHITE );
108   
109   // display isoline on planar faces (box for ex.)
110   myAISContext->IsoOnPlane( true );
111   
112   /* create trihedron */
113   if ( DisplayTrihedron )
114   {
115     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
116     myTrihedron = new AIS_Trihedron(anAxis);
117     myTrihedron->SetInfiniteState( Standard_True );
118
119     Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
120     //myTrihedron->SetColor( Col );
121     myTrihedron->SetArrowColor( Col.Name() );
122     myTrihedron->SetSize(100);
123     Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
124     if (drawer->HasDatumAspect()) {
125       Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
126       daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
127       daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
128       daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
129     }
130   }
131
132   // set interaction style to standard
133   myInteractionStyle = 0;
134
135   // set zooming style to standard
136   myZoomingStyle = 0;
137
138   // preselection
139   myPreselectionEnabled = true;
140
141   // selection
142   mySelectionEnabled = true;
143   myMultiSelectionEnabled = true;
144 }
145
146 /*!
147   Destructor
148 */
149 OCCViewer_Viewer::~OCCViewer_Viewer() 
150 {
151   myAISContext.Nullify();
152   myV3dViewer.Nullify();
153 }
154
155 /*!
156   [obsolete]
157   \return background color of viewer
158 */
159 QColor OCCViewer_Viewer::backgroundColor() const
160 {
161   return backgroundColor(0);
162 }
163
164 /*!
165   \return background data of viewer
166 */
167 Qtx::BackgroundData OCCViewer_Viewer::background() const
168 {
169   return background(0);
170 }
171
172 /*!
173   Sets background color [obsolete]
174   \param c - new background color
175 */
176 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
177 {
178   setBackgroundColor( 0, c );
179 }
180
181 /*!
182   Sets background data
183   \param d - new background data
184 */
185 void OCCViewer_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
186 {
187   setBackground( 0, theBackground );
188 }
189
190 /*!
191   Start initialization of view window
192   \param view - view window to be initialized
193 */
194 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
195 {
196   if ( view ) {
197     view->initLayout();
198     view->initSketchers();
199     view->setInteractionStyle( interactionStyle() );
200     view->setZoomingStyle( zoomingStyle() );
201     view->enablePreselection( isPreselectionEnabled() );
202     view->enableSelection( isSelectionEnabled() );
203     
204     OCCViewer_ViewPort3d* vp3d = view->getViewPort();
205     if ( vp3d )
206     {
207       vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
208       // connect signal from viewport
209       connect(vp3d, SIGNAL(vpClosed(OCCViewer_ViewPort3d*)), this, SLOT(onViewClosed(OCCViewer_ViewPort3d*)));
210       connect(vp3d, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewMapped(OCCViewer_ViewPort3d*)));
211     }
212   }
213 }
214
215 /*!
216   Creates new view window
217   \param theDesktop - main window of application
218 */
219 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
220 {
221   // create view frame
222   OCCViewer_ViewFrame* view = new OCCViewer_ViewFrame(theDesktop, this);
223   // get main view window (created by view frame)
224   OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
225   // initialize main view window
226   initView( vw );
227   // set default background for view window
228   vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
229
230   return view;
231 }
232
233 /*!
234   Sets new view manager
235   \param theViewManager - new view manager
236 */
237 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
238 {
239   SUIT_ViewModel::setViewManager(theViewManager);
240   if (theViewManager) {
241     connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
242             this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
243
244     connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
245             this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
246
247     connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
248             this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
249
250     connect(theViewManager, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), 
251             this, SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
252   }
253 }
254
255 /*!
256   SLOT: called on mouse button press, stores current mouse position as start point for transformations
257 */
258 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
259 {
260   myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
261 }
262
263 /*!
264   SLOT: called on mouse move, processes transformation or hilighting
265 */
266 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
267 {
268   if (!mySelectionEnabled) return;
269   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
270
271   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
272
273   myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
274
275   if ( isSelectionEnabled() && isPreselectionEnabled() ) {
276     if (aView->getViewPort()->isBusy()) {
277       QCoreApplication::processEvents();
278       return; // Check that the ViewPort initialization completed
279                                                 // To Prevent call move event if the View port is not initialized
280                                                 // IPAL 20883
281     }
282     Handle(V3d_View) aView3d = aView->getViewPort()->getView();
283     if ( !aView3d.IsNull() ) {
284       myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
285     }
286   }
287 }
288
289
290 /*!
291   SLOT: called on mouse button release, finishes transformation or selection
292 */
293 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
294 {
295   if (!mySelectionEnabled) return;
296   if (theEvent->button() != Qt::LeftButton) return;
297   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
298
299   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
300   if (!aView )
301     return;
302
303   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
304   bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
305   
306   if (!aHasShift) emit deselection();
307
308   if (myStartPnt == myEndPnt)
309   {
310     if ( !isPreselectionEnabled() ) {
311       Handle(V3d_View) aView3d = aView->getViewPort()->getView();
312       if ( !aView3d.IsNull() ) {
313         myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
314       }
315     }
316
317     if (aHasShift && myMultiSelectionEnabled)
318       myAISContext->ShiftSelect();
319     else
320       myAISContext->Select();
321   }
322   else
323   {
324     if (aHasShift && myMultiSelectionEnabled)
325       myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
326                                 myEndPnt.x(), myEndPnt.y(),
327                                 aView->getViewPort()->getView(), Standard_False );
328     else
329       myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
330                            myEndPnt.x(), myEndPnt.y(),
331                            aView->getViewPort()->getView(), Standard_False );
332
333     int Nb = myAISContext->NbSelected();
334     if( Nb>1 && !myMultiSelectionEnabled )
335     {
336         myAISContext->InitSelected();
337         Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
338         if( !anOwner.IsNull() )
339         {
340             myAISContext->ClearSelected( Standard_False );
341             myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
342         }
343     }
344
345     myAISContext->UpdateCurrentViewer();
346   }
347   emit selectionChanged();
348 }
349
350 /*!
351   SLOT: called on key press, processes selection in "key free" interaction style
352 */
353 void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEvent)
354 {
355   if (!mySelectionEnabled) return;
356   if (theEvent->key() != Qt::Key_S) return;
357   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
358
359   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
360   if (!aView || aView->interactionStyle() != SUIT_ViewModel::KEY_FREE)
361     return;
362
363   emit deselection();
364
365   if ( !isPreselectionEnabled() ) {
366     Handle(V3d_View) aView3d = aView->getViewPort()->getView();
367     if ( !aView3d.IsNull() ) {
368       myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
369     }
370   }
371
372   myAISContext->Select();
373
374   emit selectionChanged();
375 }
376
377 void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
378 {
379   Standard_Integer aViewsNb = 0;
380   for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
381     ++aViewsNb;
382   if ( aViewsNb < 2 ) {
383     //clean up presentations before last view is closed
384     myAISContext->RemoveAll(Standard_False);
385   }
386 }
387
388 void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
389 {
390   setTrihedronShown( true );
391   bool showStaticTrihedron = true;
392   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
393   if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
394   viewPort->showStaticTrihedron( showStaticTrihedron );
395 }
396
397 int OCCViewer_Viewer::getTopLayerId()
398 {
399   if ( myTopLayerId == 0 && !myAISContext->CurrentViewer().IsNull() )    
400     myAISContext->CurrentViewer()->AddZLayer( myTopLayerId );
401
402   return myTopLayerId;
403 }
404
405 /*!
406   \return interaction style
407 */
408 int OCCViewer_Viewer::interactionStyle() const
409 {
410   return myInteractionStyle;
411 }
412
413 /*!
414   Sets interaction style: 0 - standard, 1 - keyboard free interaction
415   \param theStyle - new interaction style
416 */
417 void OCCViewer_Viewer::setInteractionStyle( const int theStyle )
418 {
419   myInteractionStyle = theStyle;
420   //!! To be done for view windows
421   if ( !myViewManager )
422     return;
423
424   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
425   for ( int i = 0; i < (int)wins.count(); i++ )
426   {
427     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
428     if ( win )
429       win->setInteractionStyle( theStyle );
430   }
431 }
432
433 /*!
434   \return zooming style
435 */
436 int OCCViewer_Viewer::zoomingStyle() const
437 {
438   return myZoomingStyle;
439 }
440
441 /*!
442   Sets zooming style: 0 - standard, 1 - advanced (at cursor)
443   \param theStyle - new zooming style
444 */
445 void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
446 {
447   myZoomingStyle = theStyle;
448   //!! To be done for view windows
449   if ( !myViewManager )
450     return;
451
452   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
453   for ( int i = 0; i < (int)wins.count(); i++ )
454   {
455     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
456     if ( win )
457       win->setZoomingStyle( theStyle );
458   }
459 }
460
461 /*!
462   \return true if preselection is enabled
463 */
464 bool OCCViewer_Viewer::isPreselectionEnabled() const 
465
466   return myPreselectionEnabled; 
467 }
468
469 /*!
470   Enables/disables preselection
471   \param isEnabled - new status
472 */
473 void OCCViewer_Viewer::enablePreselection(bool isEnabled)
474 {
475   myPreselectionEnabled = isEnabled;
476
477   if ( !myViewManager )
478     return;
479
480   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
481   for ( int i = 0; i < (int)wins.count(); i++ )
482   {
483     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
484     if ( win ) {
485       win->enablePreselection( isEnabled );
486     }
487   }
488 }
489
490 /*!
491   \return true if selection is enabled
492 */
493 bool OCCViewer_Viewer::isSelectionEnabled() const 
494
495   return mySelectionEnabled; 
496 }
497
498 /*!
499   Enables/disables selection
500   \param isEnabled - new status
501 */
502 void OCCViewer_Viewer::enableSelection(bool isEnabled)
503 {
504   mySelectionEnabled = isEnabled;
505
506   //!! To be done for view windows
507   if ( !myViewManager )
508     return;
509
510   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
511   for ( int i = 0; i < (int)wins.count(); i++ )
512   {
513     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
514     if ( win ) {
515       win->updateEnabledDrawMode();
516       win->enableSelection( isEnabled );
517     }
518   }
519
520   
521   //clear current selection in the viewer
522   if(!isEnabled) {
523     myAISContext->ClearSelected( Standard_True );
524   }
525
526 }
527
528 /*!
529   Sets multiselection enabled status
530   \param isEnabled - new status
531 */
532 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
533 {
534   myMultiSelectionEnabled = isEnable;
535   //!! To be done for view windows
536   if ( !myViewManager )
537     return;
538
539   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
540   for ( int i = 0; i < (int)wins.count(); i++ )
541   {
542     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
543     if ( win )
544       win->updateEnabledDrawMode();
545   }
546 }
547
548 /*!
549   Builds popup for occ viewer
550 */
551 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
552 {
553   thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
554   thePopup->addAction( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
555
556   thePopup->addSeparator();
557
558   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
559
560   //Support of several toolbars in the popup menu
561   QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
562   QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
563   for ( ; it!=last; it++ ) {
564     if ( (*it)->parentWidget()->isVisible() )
565       thePopup->addAction( (*it)->toggleViewAction() );
566   }
567 }
568
569 /*!
570   SLOT: called on dump view operation is activated, stores scene to raster file
571 */
572 void OCCViewer_Viewer::onDumpView()
573 {
574   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
575   if ( aView )
576     aView->onDumpView();
577 }
578
579 /*!
580   SLOT: called if background color is to be changed changed, passes new color to view port
581 */
582 void OCCViewer_Viewer::onChangeBackground()
583 {
584   OCCViewer_ViewWindow* aView = dynamic_cast<OCCViewer_ViewWindow*>(myViewManager->getActiveView());
585   if ( !aView )
586     return;
587
588   // get supported gradient types
589   QStringList gradList;
590   QIntList    idList, txtList;
591   QString     formats = backgroundData( gradList, idList, txtList );
592
593   // invoke dialog box
594   Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(),  // initial background
595                                                                    aView,                // parent for dialog box
596                                                                    txtList,              // allowed texture modes
597                                                                    true,                 // enable solid color mode
598                                                                    true,                 // enable gradient mode
599                                                                    false,                // disable custom gradient mode
600                                                                    !txtList.isEmpty(),   // enable/disable texture mode
601                                                                    gradList,             // gradient names
602                                                                    idList,               // gradient identifiers
603                                                                    formats );            // image formats
604
605   // set chosen background data to the viewer
606   if ( bgData.isValid() )
607     aView->setBackground( bgData );
608 }
609
610 /*!
611   Updates OCC 3D viewer
612 */
613 void OCCViewer_Viewer::update()
614 {
615   if (!myV3dViewer.IsNull())
616     myV3dViewer->Update();
617
618   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
619   if ( aView )
620     aView->updateGravityCoords();
621 }
622
623 /*!
624   \return objects selected in 3D viewer
625   \param theList - list to be filled with selected objects
626 */
627 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
628 {
629   theList.Clear();
630   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
631     theList.Append(myAISContext->SelectedInteractive());
632 }
633
634 /*!
635   Selects objects in 3D viewer. Other selected objects are left as selected
636   \param theList - list objects to be selected
637 */
638 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
639 {
640   AIS_ListIteratorOfListOfInteractive aIt;
641   for (aIt.Initialize(theList); aIt.More(); aIt.Next())
642     myAISContext->AddOrRemoveSelected(aIt.Value(), false);
643   myAISContext->UpdateCurrentViewer();
644 }
645
646 /*!
647   Auxiliary method to emit signal selectionChanged()
648 */
649 void OCCViewer_Viewer::performSelectionChanged()
650 {
651     emit selectionChanged();
652 }
653
654 /*!
655   Hilights/unhilights object in viewer
656   \param obj - object to be updated
657   \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
658   \param update - update current viewer
659 */
660 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
661                                   bool hilight, bool update )
662 {
663   bool isInLocal = myAISContext->HasOpenedContext();
664   if( !obj.IsNull() )
665     if( !isInLocal )
666     {
667       if ( hilight && !myAISContext->IsSelected( obj ) )
668         myAISContext->AddOrRemoveCurrentObject( obj, false );
669       else if ( !hilight && myAISContext->IsSelected( obj ) )
670         myAISContext->AddOrRemoveCurrentObject( obj, false );
671     }
672
673   if ( update )
674     myV3dViewer->Redraw();
675     
676   return false;
677 }
678
679 /*!
680   Unhilights all objects in viewer
681   \param updateviewer - update current viewer
682 */
683 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
684 {
685   if ( myAISContext->HasOpenedContext() ) {
686     if ( unselect ) {
687       myAISContext->ClearSelected( updateviewer );
688     } else {
689       myAISContext->UnhilightSelected( updateviewer );
690     }
691   } else {
692     if ( unselect ) {
693       myAISContext->ClearCurrents( updateviewer );
694     } else {
695       myAISContext->UnhilightCurrents( updateviewer );
696     }
697   }
698
699   return false;
700 }
701
702 /*!
703   \return true if object is in viewer or in collector
704   \param obj - object to be checked
705   \param onlyInViewer - search object only in viewer (so object must be displayed)
706 */
707 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
708                                    bool onlyInViewer )
709 {
710   AIS_ListOfInteractive List;
711   myAISContext->DisplayedObjects(List);
712
713   AIS_ListIteratorOfListOfInteractive ite(List);
714   for ( ; ite.More(); ite.Next() )
715     if( ite.Value()==obj )
716       return true;
717
718   return false;
719 }
720
721 /*!
722   \return true if object is displayed in viewer
723   \param obj - object to be checked
724 */
725 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
726 {
727   return myAISContext->IsDisplayed( obj );
728 }
729
730 /*!
731   Sets color of object
732   \param obj - object to be updated
733   \param color - new color
734   \param update - update current viewer
735 */
736 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
737                                  const QColor& color,
738                                  bool update )
739 {
740   if( !obj.IsNull() )
741   {
742     Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
743                                                color.green() / 255.,
744                                                color.blue() / 255.,
745                                                Quantity_TOC_RGB );
746     obj->SetColor( CSFColor );
747   }
748
749   if( update )
750     myV3dViewer->Update();
751 }
752
753 /*!
754   Changes display mode of object
755   \param obj - object to be processed
756   \param mode - new display mode
757   \param update - update current viewer
758 */
759 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
760                                              int mode, bool update )
761 {
762   myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
763   if( update )
764     myV3dViewer->Update();
765 }
766
767 /*!
768   Changes transparency of object
769   \param obj - object to be processed
770   \param trans - new transparency
771   \param update - update current viewer
772 */
773 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
774                                         float trans, bool update )
775 {
776   myAISContext->SetTransparency( obj, trans, false );
777   myAISContext->Redisplay( obj, Standard_False, Standard_True );
778   if( update )
779     myV3dViewer->Update();
780 }
781
782 /*!
783   Changes visibility of trihedron to opposite
784 */
785 void OCCViewer_Viewer::toggleTrihedron()
786 {
787   setTrihedronShown( !isTrihedronVisible() );
788 }
789
790 /*!
791   \return true if trihedron is visible
792 */
793 bool OCCViewer_Viewer::isTrihedronVisible() const
794 {
795   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
796 }
797
798 /*!
799   Sets visibility state of trihedron
800   \param on - new state
801 */
802
803 void OCCViewer_Viewer::setTrihedronShown( const bool on )
804 {
805   if ( myTrihedron.IsNull() )
806     return;
807
808   if ( on ) {
809     myAISContext->Display( myTrihedron );
810     myAISContext->Deactivate(myTrihedron);
811   }
812   else {
813     myAISContext->Erase( myTrihedron );
814   }
815 }
816
817 /*!
818   \return trihedron size
819 */
820 double OCCViewer_Viewer::trihedronSize() const
821 {
822   double sz = 0;
823   if ( !myTrihedron.IsNull() )
824     sz = myTrihedron->Size();
825   return sz;
826 }
827
828 /*!
829   Changes trihedron size
830   \param sz - new size
831 */
832 void OCCViewer_Viewer::setTrihedronSize( const double sz, bool isRelative )
833 {
834   if ( myTrihedronSize != sz || isRelative != myIsRelative) {
835     myTrihedronSize = sz; 
836     myIsRelative = isRelative;
837     updateTrihedron();
838   }
839 }
840
841 /*!
842   Set number of isolines
843   \param u - u-isolines (first parametric co-ordinate)
844   \param v - v-isolines (second parametric co-ordinate)
845 */
846 void OCCViewer_Viewer::setIsos( const int u, const int v )
847 {
848   Handle(AIS_InteractiveContext) ic = getAISContext();
849   if ( ic.IsNull() )
850   return;
851
852   ic->SetIsoNumber( u, AIS_TOI_IsoU );
853   ic->SetIsoNumber( v, AIS_TOI_IsoV );
854 }
855
856 /*!
857   \return number of isolines
858   \param u - to return u-isolines (first parametric co-ordinate)
859   \param v - to return v-isolines (second parametric co-ordinate)
860 */
861 void OCCViewer_Viewer::isos( int& u, int& v ) const
862 {
863   Handle(AIS_InteractiveContext) ic = getAISContext();
864   if ( !ic.IsNull() )
865   {
866     u = ic->IsoNumber( AIS_TOI_IsoU );
867     v = ic->IsoNumber( AIS_TOI_IsoV );
868   }
869 }
870
871 /* 
872  * Returns a new OCCViewer_ViewWindow instance which will be placed as a sub window in ViewFrame
873  */
874 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
875 {
876   return new OCCViewer_ViewWindow(0,  this);
877 }
878
879 // obsolete  
880 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
881 {
882   return background( theViewId ).color();
883 }
884
885 Qtx::BackgroundData OCCViewer_Viewer::background( int theViewId ) const
886 {
887   return ( theViewId >= 0 && theViewId < myBackgrounds.count() ) ? myBackgrounds[theViewId] : Qtx::BackgroundData();
888 }
889
890 // obsolete
891 void OCCViewer_Viewer::setBackgroundColor( int theViewId, const QColor& theColor )
892 {
893   if ( theColor.isValid() ) {
894     Qtx::BackgroundData bg = background( theViewId );
895     bg.setColor( theColor );
896     setBackground( theViewId, bg );
897   }
898 }
899
900 void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData& theBackground )
901 {
902   if ( theBackground.isValid() && theViewId >= 0 && theViewId < myBackgrounds.count() )
903     myBackgrounds[theViewId] = theBackground;    
904 }
905
906
907 /*!
908   Set the show static trihedron flag
909 */
910 void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
911 {
912   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
913   if ( aView ) aView->showStaticTrihedron( on );
914 }
915
916 /*!
917   Get new and current trihedron size corresponding to the current model size
918 */
919 bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
920 {
921   theNewSize = 100;
922   theSize = 100;
923
924   //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
925   Handle(V3d_Viewer) viewer = getViewer3d();
926   viewer->InitActiveViews();
927   if(!viewer->MoreActiveViews()) return false;
928
929   Handle(V3d_View) view3d = viewer->ActiveView();
930   //SRN: END of fix
931
932   if ( view3d.IsNull() )
933     return false;
934
935   double aMaxSide = computeSceneSize( view3d );
936
937   // IPAL21687
938   // The boundary box of the view may be initialized but nullified
939   // (case of infinite objects)
940   if ( aMaxSide < Precision::Confusion() )
941     return false;
942
943   float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
944
945   static float EPS = 5.0E-3;
946   theSize = getTrihedron()->Size();
947   theNewSize = aMaxSide*aSizeInPercents / 100.0;
948
949   return fabs( theNewSize - theSize ) > theSize    * EPS ||
950          fabs( theNewSize - theSize ) > theNewSize * EPS;
951 }
952
953 /*!
954  * Compute scene size
955  */
956 double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
957 {
958   double aMaxSide = 0;
959   double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
960
961 #if OCC_VERSION_LARGE > 0x06070100
962   Bnd_Box aBox = view3d->View()->MinMaxValues();
963   Xmin = aBox.CornerMin().X();
964   Ymin = aBox.CornerMin().Y();
965   Zmin = aBox.CornerMin().Z();
966   Xmax = aBox.CornerMax().X();
967   Ymax = aBox.CornerMax().Y();
968   Zmax = aBox.CornerMax().Z();
969 #else
970   view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
971 #endif
972
973   if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
974        Xmax != RealLast()  && Ymax != RealLast()  && Zmax != RealLast() )
975   {
976     aMaxSide = Xmax - Xmin;
977     if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
978     if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
979   }
980
981   return aMaxSide;
982 }
983
984 /*! 
985  * Update the size of the trihedron
986  */
987 void OCCViewer_Viewer::updateTrihedron() {
988   if ( myTrihedron.IsNull() )
989     return;
990
991   if(myIsRelative){
992     double newSz, oldSz;
993     
994     if(computeTrihedronSize(newSz, oldSz))
995       myTrihedron->SetSize(newSz);
996     
997   } else if(myTrihedron->Size() != myTrihedronSize) {
998     myTrihedron->SetSize(myTrihedronSize);
999   }
1000 }
1001
1002 /*!
1003   Set number of isolines
1004   \param u - u-isolines (first parametric co-ordinate)
1005   \param v - v-isolines (second parametric co-ordinate)
1006 */
1007 void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
1008 {
1009   myPreselectionEnabled = isPreselectionEnabled;
1010   mySelectionEnabled = isSelectionEnabled;
1011   //clear current selection in the viewer
1012   
1013   if(!mySelectionEnabled) {
1014     myAISContext->ClearSelected( Standard_True );
1015   }
1016 }
1017
1018 /*!
1019   Creates clipping plane based on the incoming plane
1020 */
1021 Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& thePlane, const Standard_Boolean theIsOn)
1022 {
1023   Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( thePlane );
1024   aGraphic3dPlane->SetOn( theIsOn );
1025   aGraphic3dPlane->SetCapping( Standard_True );
1026
1027   // load capping texture
1028   QPixmap px( ":images/hatch.png" );
1029   if( !px.isNull() ) {
1030     const Handle(Image_PixMap) aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
1031     Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual( aPixmap );
1032     if( aTexture->IsDone() ) {
1033       aTexture->EnableModulate();
1034       aTexture->EnableRepeat();
1035       aTexture->GetParams()->SetScale( Graphic3d_Vec2( 0.01, 0.01 ) );
1036       aGraphic3dPlane->SetCappingTexture( aTexture );
1037     }
1038   }
1039   return aGraphic3dPlane;
1040 }
1041 /*!
1042   Applies clipping planes to clippable objects
1043 */
1044 void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
1045 {
1046   // 1. Remove existing clipping planes
1047   myClipPlanes.clear();
1048   myInternalClipPlanes.Clear();
1049
1050   // 2. Create new clipping planes
1051   ClipPlanesList::iterator inIt = theList.begin();
1052   for (;inIt != theList.end(); inIt++ )
1053   {
1054     OCCViewer_ClipPlane aPlane = *inIt;
1055
1056     double aDx = 0.0, aDy = 0.0, aDz = 0.0;
1057     aPlane.OrientationToXYZ( aDx, aDy, aDz );
1058
1059     gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
1060     gp_Dir aDirection( aDx, aDy, aDz );
1061
1062     myInternalClipPlanes.Append( createClipPlane( gp_Pln( anOrigin, aDirection ), aPlane.IsOn ) );
1063     myClipPlanes.push_back( aPlane );
1064   }
1065
1066   // 3. Apply clipping planes
1067   AIS_ListOfInteractive aList;
1068   myAISContext->DisplayedObjects (aList);
1069   for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
1070     Handle(AIS_InteractiveObject) anObj = anIter.Value();
1071     Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
1072     if (!aShape.IsNull() && aShape->IsClippable()) {
1073       aShape->SetClipPlanes(myInternalClipPlanes);
1074     }
1075   }
1076 }
1077
1078 /*!
1079   Returns the clipping planes applied to the displayed objects.
1080 */
1081 ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
1082   return myClipPlanes;
1083 }
1084 /*!
1085   Applies clipping planes to given object objects
1086 */
1087 void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
1088 {
1089   Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
1090   if (!aShape.IsNull() && aShape->IsClippable())
1091   {
1092     aShape->SetClipPlanes (myInternalClipPlanes);
1093   }
1094 }
1095
1096 /*!
1097   Returns the pointer to the clipping dialog box.
1098 */
1099 OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
1100   return myClippingDlg;
1101 }
1102
1103
1104 /*!
1105   Stores pointer to the clipping dialog box.
1106 */
1107 void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
1108   if(myClippingDlg != theDlg) {
1109     myClippingDlg = theDlg;
1110   }
1111 }