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