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