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