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