Salome HOME
f8abc653d0ef012c3e7d8ec1a87fe2751e78ee5e
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewWindow.cxx
1 // Copyright (C) 2007-2019  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 // File   : OCCViewer_ViewWindow.cxx
24 // Author :
25
26 #include "OCCViewer_ViewWindow.h"
27 #include "OCCViewer_ViewFrame.h"
28 #include "OCCViewer_ViewModel.h"
29 #include "OCCViewer_ViewPort3d.h"
30 #include "OCCViewer_ViewManager.h"
31 #include "OCCViewer_ViewSketcher.h"
32 #include "OCCViewer_CreateRestoreViewDlg.h"
33 #include "OCCViewer_ClipPlane.h"
34 #include "OCCViewer_SetRotationPointDlg.h"
35 #include "OCCViewer_AxialScaleDlg.h"
36 #include "OCCViewer_CubeAxesDlg.h"
37 #include "OCCViewer_ClippingDlg.h"
38 #include "OCCViewer_RayTracingDlg.h"
39 #include "OCCViewer_EnvTextureDlg.h"
40 #include "OCCViewer_LightSourceDlg.h"
41 #include "OCCViewer_Utilities.h"
42
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_ViewManager.h>
46 #include <SUIT_Tools.h>
47 #include <SUIT_ResourceMgr.h>
48 #include <SUIT_MessageBox.h>
49 #include <SUIT_Application.h>
50
51 #include <QtxActionToolMgr.h>
52 #include <QtxMultiAction.h>
53 #include <QtxRubberBand.h>
54
55 #include <QPainter>
56 #include <QTime>
57 #include <QImage>
58 #include <QKeyEvent>
59 #include <QMouseEvent>
60 #include <QApplication>
61 #include <QActionGroup>
62 #include <QMenu>
63
64 #include <AIS_ListOfInteractive.hxx>
65 #include <AIS_ListIteratorOfListOfInteractive.hxx>
66 #include <AIS_Shape.hxx>
67
68 #include <BRep_Tool.hxx>
69 #include <BRepBndLib.hxx>
70 #include <BRepGProp.hxx>
71 #include <GProp_GProps.hxx>
72 #include <TopoDS.hxx>
73
74 #include <Graphic3d_SequenceOfHClipPlane.hxx>
75 #include <Graphic3d_ClipPlane.hxx>
76 #include <OpenGl_GraphicDriver.hxx>
77 #include <OpenGLUtils_FrameBuffer.h>
78 #include <Basics_OCCTVersion.hxx>
79 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
80 #include <Graphic3d_MapOfStructure.hxx>
81 #include <Graphic3d_Structure.hxx>
82 #if OCC_VERSION_LARGE <= 0x07030000
83 #include <Graphic3d_ExportFormat.hxx>
84 #endif
85 #include <Graphic3d_StereoMode.hxx>
86 #include <Graphic3d_RenderingParams.hxx>
87 #include <Graphic3d_BndBox3d.hxx>
88
89 #include <V3d_Plane.hxx>
90 #include <V3d_Light.hxx>
91
92 #include <gp_Dir.hxx>
93 #include <gp_Pln.hxx>
94 #include <gp_GTrsf.hxx>
95 #include <TColgp_Array1OfPnt2d.hxx>
96
97 #include <Image_PixMap.hxx>
98
99 #include <Standard_Version.hxx>
100
101 #include "utilities.h"
102
103 // // OpenCV includes
104 // #include <cv.h>
105 // #include <highgui.h>
106
107 static QEvent* l_mbPressEvent = 0;
108
109 //#ifdef WIN32
110 //# include <QWindowsStyle>
111 //#endif
112
113 #ifdef __APPLE__
114 #include <OpenGL/gl.h>
115 #else
116 #include <GL/gl.h>
117 #endif
118
119 // To avoid conflict between KeyPress from the X.h (define KeyPress 2)
120 // and QEvent::KeyPress (qevent.h)
121 #ifdef KeyPress
122 #undef KeyPress
123 #endif
124
125 // Enable ray tracing features
126 #define ENABLE_RAY_TRACING
127
128 const char* imageZoomCursor[] = {
129 "32 32 3 1",
130 ". c None",
131 "a c #000000",
132 "# c #ffffff",
133 "................................",
134 "................................",
135 ".#######........................",
136 "..aaaaaaa.......................",
137 "................................",
138 ".............#####..............",
139 "...........##.aaaa##............",
140 "..........#.aa.....a#...........",
141 ".........#.a.........#..........",
142 ".........#a..........#a.........",
143 "........#.a...........#.........",
144 "........#a............#a........",
145 "........#a............#a........",
146 "........#a............#a........",
147 "........#a............#a........",
148 ".........#...........#.a........",
149 ".........#a..........#a.........",
150 ".........##.........#.a.........",
151 "........#####.....##.a..........",
152 ".......###aaa#####.aa...........",
153 "......###aa...aaaaa.......#.....",
154 ".....###aa................#a....",
155 "....###aa.................#a....",
156 "...###aa...............#######..",
157 "....#aa.................aa#aaaa.",
158 ".....a....................#a....",
159 "..........................#a....",
160 "...........................a....",
161 "................................",
162 "................................",
163 "................................",
164 "................................"};
165
166 const char* imageRotateCursor[] = {
167 "32 32 3 1",
168 ". c None",
169 "a c #000000",
170 "# c #ffffff",
171 "................................",
172 "................................",
173 "................................",
174 "................................",
175 "........#.......................",
176 ".......#.a......................",
177 "......#######...................",
178 ".......#aaaaa#####..............",
179 "........#..##.a#aa##........##..",
180 ".........a#.aa..#..a#.....##.aa.",
181 ".........#.a.....#...#..##.aa...",
182 ".........#a.......#..###.aa.....",
183 "........#.a.......#a..#aa.......",
184 "........#a.........#..#a........",
185 "........#a.........#a.#a........",
186 "........#a.........#a.#a........",
187 "........#a.........#a.#a........",
188 ".........#.........#a#.a........",
189 "........##a........#a#a.........",
190 "......##.a#.......#.#.a.........",
191 "....##.aa..##.....##.a..........",
192 "..##.aa.....a#####.aa...........",
193 "...aa.........aaa#a.............",
194 "................#.a.............",
195 "...............#.a..............",
196 "..............#.a...............",
197 "...............a................",
198 "................................",
199 "................................",
200 "................................",
201 "................................",
202 "................................"};
203
204 const char* imageCrossCursor[] = {
205   "32 32 3 1",
206   ". c None",
207   "a c #000000",
208   "# c #ffffff",
209   "................................",
210   "................................",
211   "................................",
212   "................................",
213   "................................",
214   "................................",
215   "................................",
216   "...............#................",
217   "...............#a...............",
218   "...............#a...............",
219   "...............#a...............",
220   "...............#a...............",
221   "...............#a...............",
222   "...............#a...............",
223   "...............#a...............",
224   ".......#################........",
225   "........aaaaaaa#aaaaaaaaa.......",
226   "...............#a...............",
227   "...............#a...............",
228   "...............#a...............",
229   "...............#a...............",
230   "...............#a...............",
231   "...............#a...............",
232   "...............#a...............",
233   "................a...............",
234   "................................",
235   "................................",
236   "................................",
237   "................................",
238   "................................",
239   "................................",
240   "................................"};
241
242  
243   /*!
244   \brief Constructor
245   \param theDesktop main window of application
246   \param theModel OCC 3D viewer
247 */
248 OCCViewer_ViewWindow::OCCViewer_ViewWindow( SUIT_Desktop*     theDesktop,
249                                             OCCViewer_Viewer* theModel )
250 : SUIT_ViewWindow( theDesktop )
251 {
252   myModel = theModel;
253   myRestoreFlag = 0;
254   myEnableDrawMode = false;
255   myDrawRectEnabled = true;
256   myDrawRect=false;
257   updateEnabledDrawMode();
258   myScalingDlg = 0;
259   mySetRotationPointDlg = 0;
260   myRectBand = 0;
261
262   IsSketcherStyle = false;
263   myIsKeyFree = false;
264
265   mypSketcher = 0;
266   myCurSketch = -1;
267   my2dMode = No2dMode;
268
269   myInteractionStyle = SUIT_ViewModel::STANDARD;
270   myPreselectionEnabled = true;
271   mySelectionEnabled = true;
272
273   myCursorIsHand = false;
274   myPanningByBtn = false;
275   myAutomaticZoom = true;
276
277   clearViewAspects();
278 }
279
280 /*!
281   \brief Destructor.
282 */
283 OCCViewer_ViewWindow::~OCCViewer_ViewWindow()
284 {
285   endDrawRect();
286   qDeleteAll( mySketchers );
287 }
288
289 /*!
290   \brief Internal initialization.
291 */
292 void OCCViewer_ViewWindow::initLayout()
293 {
294   myViewPort = new OCCViewer_ViewPort3d( this, myModel->getViewer3d(), V3d_ORTHOGRAPHIC );
295   myViewPort->installEventFilter(this);
296   setCentralWidget(myViewPort);
297   myOperation = NOTHING;
298
299   myCurrPointType = BBCENTER;
300   myPrevPointType = BBCENTER;
301   mySelectedPoint = gp_Pnt(0.,0.,0.);
302   myRotationPointSelection = false;
303
304   setTransformRequested ( NOTHING );
305   setTransformInProcess ( false );
306
307   createActions();
308   createToolBar();
309
310   switch (my2dMode) {
311   case XYPlane:
312     onTopView();
313     break;
314   case XZPlane:
315     onLeftView();
316     break;
317   case YZPlane:
318     onFrontView();
319     break;
320   }
321
322   // Graduated axes dialog
323   QtxAction* anAction = dynamic_cast<QtxAction*>( toolMgr()->action( GraduatedAxesId ) );
324   myCubeAxesDlg = new OCCViewer_CubeAxesDlg( anAction, this, "OCCViewer_CubeAxesDlg" );
325   myCubeAxesDlg->initialize();
326
327   connect( myViewPort, SIGNAL( vpTransformed( OCCViewer_ViewPort* ) ), this, SLOT( emitViewModified() ) );
328 }
329
330 OCCViewer_ViewWindow* OCCViewer_ViewWindow::getView( const int mode ) const
331 {
332   return mode == get2dMode() ? const_cast<OCCViewer_ViewWindow*>( this ) : 0;
333 }
334
335 /*!
336   \brief Detect viewer operation according the the mouse button pressed
337   and key modifiers used.
338   \param theEvent mouse event
339   \return type of the operation
340 */
341 OCCViewer_ViewWindow::OperationType
342 OCCViewer_ViewWindow::getButtonState( QMouseEvent* theEvent, int theInteractionStyle )
343 {
344   OperationType aOp = NOTHING;
345   SUIT_ViewModel::InteractionStyle aStyle = (SUIT_ViewModel::InteractionStyle)theInteractionStyle;
346   if( (theEvent->modifiers() == SUIT_ViewModel::myStateMap[aStyle][SUIT_ViewModel::ZOOM]) &&
347       (theEvent->buttons() == SUIT_ViewModel::myButtonMap[aStyle][SUIT_ViewModel::ZOOM]) )
348     aOp = ZOOMVIEW;
349   else if( (theEvent->modifiers() == SUIT_ViewModel::myStateMap[aStyle][SUIT_ViewModel::PAN]) &&
350            (theEvent->buttons() == SUIT_ViewModel::myButtonMap[aStyle][SUIT_ViewModel::PAN]) )
351     aOp = PANVIEW;
352   else if( (theEvent->modifiers()  == SUIT_ViewModel::myStateMap[aStyle][SUIT_ViewModel::ROTATE]) &&
353            (theEvent->buttons() == SUIT_ViewModel::myButtonMap[aStyle][SUIT_ViewModel::ROTATE]) &&
354            (my2dMode == No2dMode))
355     aOp = ROTATE;
356
357   return aOp;
358 }
359
360 /*!
361   \brief Customize event handling
362   \param watched event receiver object
363   \param e event
364   \return \c true if the event processing should be stopped
365 */
366 bool OCCViewer_ViewWindow::eventFilter( QObject* watched, QEvent* e )
367 {
368   if ( watched == myViewPort ) {
369     int aType = e->type();
370     switch(aType) {
371     case QEvent::MouseButtonPress:
372       vpMousePressEvent((QMouseEvent*) e);
373       return true;
374
375     case QEvent::MouseButtonRelease:
376       vpMouseReleaseEvent((QMouseEvent*) e);
377       return true;
378
379     case QEvent::MouseMove:
380       vpMouseMoveEvent((QMouseEvent*) e);
381       return true;
382
383     case QEvent::MouseButtonDblClick:
384       emit mouseDoubleClicked(this, (QMouseEvent*)e);
385       return true;
386
387     case QEvent::Wheel:
388       {
389         QWheelEvent* aEvent = (QWheelEvent*) e;
390
391         if ( aEvent->modifiers().testFlag(Qt::ControlModifier) ) {
392           Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
393           if ( isPreselectionEnabled() 
394 #if OCC_VERSION_LARGE <= 0x07030000
395                && myModel->useLocalSelection()
396 #endif
397              ) {
398             if ( aEvent->delta() > 0 ) {
399               ic->HilightNextDetected( myViewPort->getView() );
400             } else {
401               ic->HilightPreviousDetected( myViewPort->getView() );
402             }
403           }
404         }
405         else {
406           emit vpTransformationStarted ( ZOOMVIEW );
407           myViewPort->startZoomAtPoint( aEvent->x(), aEvent->y() );
408           double delta = (double)( aEvent->delta() ) / ( 15 * 8 );
409           int x  = aEvent->x();
410           int y  = aEvent->y();
411           int x1 = (int)( aEvent->x() + width()*delta/100 );
412           int y1 = (int)( aEvent->y() + height()*delta/100 );
413           myViewPort->zoom( x, y, x1, y1 );
414           emit vpTransformationFinished ( ZOOMVIEW );
415         }
416       }
417       return true;
418
419     case QEvent::ContextMenu:
420       {
421         QContextMenuEvent * aEvent = (QContextMenuEvent*)e;
422         if ( aEvent->reason() != QContextMenuEvent::Mouse )
423           emit contextMenuRequested( aEvent );
424       }
425       return true;
426
427     case QEvent::KeyPress:
428       emit keyPressed(this, (QKeyEvent*) e);
429       return true;
430
431     default:
432       break;
433     }
434   }
435   return SUIT_ViewWindow::eventFilter(watched, e);
436 }
437
438 /*!
439   \brief Enable / disable draw rect (rubber band) mode
440 */
441 bool OCCViewer_ViewWindow::enableDrawMode( bool on )
442 {
443   bool prev = myDrawRectEnabled;
444   myDrawRectEnabled = on;
445   updateEnabledDrawMode();
446   return prev;
447 }
448
449 /*!
450   \brief Update state of enable draw mode state.
451 */
452 void OCCViewer_ViewWindow::updateEnabledDrawMode()
453 {
454   myEnableDrawMode = myDrawRectEnabled;
455   if ( myModel )
456     myEnableDrawMode = myEnableDrawMode && myModel->isSelectionEnabled() && myModel->isMultiSelectionEnabled();
457 }
458
459 /*!
460   \brief Handle mouse press event
461   \param theEvent mouse event
462 */
463 void OCCViewer_ViewWindow::vpMousePressEvent( QMouseEvent* theEvent )
464 {
465   myStartX = theEvent->x();
466   myStartY = theEvent->y();
467   int anInteractionStyle = interactionStyle();
468
469   // in "key free" interaction style zoom operation is activated by two buttons (simultaneously pressed),
470   // which are assigned for pan and rotate - these operations are activated immediately after pressing
471   // of the first button, so it is necessary to switch to zoom when the second button is pressed
472   bool aSwitchToZoom = false;
473   if ( anInteractionStyle == SUIT_ViewModel::KEY_FREE &&
474        ( myOperation == PANVIEW || myOperation == ROTATE ) ) {
475     aSwitchToZoom = getButtonState( theEvent, anInteractionStyle ) == ZOOMVIEW;
476   }
477
478   switch ( myOperation ) {
479   case WINDOWFIT:
480     if ( theEvent->button() == Qt::LeftButton )
481       emit vpTransformationStarted ( WINDOWFIT );
482     break;
483
484   case PANGLOBAL:
485     if ( theEvent->button() == Qt::LeftButton )
486       emit vpTransformationStarted ( PANGLOBAL );
487     break;
488
489   case ZOOMVIEW:
490     if ( theEvent->button() == Qt::LeftButton ) {
491       myViewPort->startZoomAtPoint( myStartX, myStartY );
492       emit vpTransformationStarted ( ZOOMVIEW );
493     }
494     break;
495
496   case PANVIEW:
497     if ( aSwitchToZoom ) {
498       myViewPort->startZoomAtPoint( myStartX, myStartY );
499       activateZoom();
500     }
501     else if ( theEvent->button() == Qt::LeftButton )
502       emit vpTransformationStarted ( PANVIEW );
503     break;
504
505   case ROTATE:
506     if ( aSwitchToZoom ) {
507       myViewPort->startZoomAtPoint( myStartX, myStartY );
508       activateZoom();
509     }
510     else if ( theEvent->button() == Qt::LeftButton ) {
511       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
512       emit vpTransformationStarted ( ROTATE );
513     }
514     break;
515
516   default:
517   /*  Try to activate a transformation */
518     OperationType aState;
519     if ( interactionStyle() == SUIT_ViewModel::STANDARD )
520       aState = getButtonState(theEvent, anInteractionStyle);
521     else {
522       aState = OCCViewer_ViewWindow::NOTHING;
523       myIsKeyFree = true;
524     }
525     switch ( aState ) {
526     case ZOOMVIEW:
527       myViewPort->startZoomAtPoint( myStartX, myStartY );
528       activateZoom();
529       break;
530     case PANVIEW:
531       activatePanning();
532       break;
533     case ROTATE:
534       activateRotation();
535       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
536       break;
537     default:
538       if ( myRotationPointSelection )
539       {
540         if ( theEvent->button() == Qt::LeftButton )
541         {
542           Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
543           ic->Select( Standard_True );
544           for ( ic->InitSelected(); ic->MoreSelected(); ic->NextSelected() )
545           {
546             TopoDS_Shape aShape = ic->SelectedShape();
547             GProp_GProps aSystem;
548             gp_Pnt aPnt;
549             if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
550             {
551               aPnt = BRep_Tool::Pnt( TopoDS::Vertex( aShape ) );
552             }
553             else if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE )
554             {
555               BRepGProp::LinearProperties( aShape, aSystem );
556               aPnt = aSystem.CentreOfMass();
557             }
558             else if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE )
559             {
560               BRepGProp::SurfaceProperties( aShape, aSystem );
561               aPnt = aSystem.CentreOfMass();
562             }
563             else if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID )
564             {
565               BRepGProp::VolumeProperties( aShape, aSystem );
566               aPnt = aSystem.CentreOfMass();
567             }
568             else
569             {
570               myCurrPointType = myPrevPointType;
571               break;
572             }
573
574             if ( mySetRotationPointDlg )
575             {
576               myRotationPointSelection = false;
577               mySetRotationPointDlg->setCoords(aPnt.X(), aPnt.Y(), aPnt.Z());
578             }
579           }
580           if ( ic->NbSelected() == 0 ) myCurrPointType = myPrevPointType;
581           if ( mySetRotationPointDlg ) mySetRotationPointDlg->toggleChange();
582 #if OCC_VERSION_LARGE <= 0x07030000
583           ic->CloseAllContexts( Standard_True );
584 #else
585           ic->Deactivate();
586           ic->Activate(0);
587 #endif
588           myOperation = NOTHING;
589           myViewPort->setCursor( myCursor );
590           myCursorIsHand = false;
591           myRotationPointSelection = false;
592         }
593       }
594       else
595         emit mousePressed(this, theEvent);
596       break;
597     }
598     /* notify that we start a transformation */
599     if ( transformRequested() )
600       emit vpTransformationStarted ( myOperation );
601   }
602   if ( transformRequested() )
603     setTransformInProcess( true );
604
605   /* we may need it for sketching... */
606   if ( l_mbPressEvent )
607     delete l_mbPressEvent;
608   l_mbPressEvent = new QMouseEvent( *theEvent );
609 }
610
611
612 /*!
613   \brief Start zooming operation.
614
615   Sets the corresponding cursor for the widget.
616 */
617 void OCCViewer_ViewWindow::activateZoom()
618 {
619   if ( !transformRequested() && !myCursorIsHand )
620     saveCursor();                /* save old cursor */
621
622   if ( myOperation != ZOOMVIEW ) {
623     QPixmap zoomPixmap (imageZoomCursor);
624     QCursor zoomCursor (zoomPixmap);
625     if( setTransformRequested ( ZOOMVIEW ) )
626       myViewPort->setCursor( zoomCursor );
627   }
628 }
629
630
631 void OCCViewer_ViewWindow::onPanning()
632 {
633   OCCViewer_ViewManager* aMgr = dynamic_cast<OCCViewer_ViewManager*>( getViewManager() );
634   bool isChained = aMgr->isChainedOperations();
635   bool isReset = ( myPanningByBtn && isChained );
636   if( isReset )
637   {
638     resetState();
639   }
640   else
641   {
642     myPanningByBtn = true;
643     activatePanning();
644   }
645 }
646
647 /*!
648   \brief Start panning operation.
649
650   Sets the corresponding cursor for the widget.
651 */
652 void OCCViewer_ViewWindow::activatePanning()
653 {
654   if ( !transformRequested() && !myCursorIsHand )
655     saveCursor();                // save old cursor
656
657   if ( myOperation != PANVIEW ) {
658     QCursor panCursor (Qt::SizeAllCursor);
659     if( setTransformRequested ( PANVIEW ) )
660       myViewPort->setCursor( panCursor );
661   }
662 }
663
664 /*!
665   \brief Start rotation operation
666
667   Sets the corresponding cursor for the widget.
668 */
669 void OCCViewer_ViewWindow::activateRotation()
670 {
671   if ( !transformRequested() && !myCursorIsHand )
672     saveCursor();                // save old cursor
673
674   if ( myOperation != ROTATE ) {
675     QPixmap rotatePixmap (imageRotateCursor);
676     QCursor rotCursor (rotatePixmap);
677     if( setTransformRequested ( ROTATE ) )
678       myViewPort->setCursor( rotCursor );
679   }
680 }
681
682 /*!
683   \brief Compute the gravity center.
684   \param theX used to return X coordinate of the gravity center
685   \param theY used to return Y coordinate of the gravity center
686   \param theZ used to return Z coordinate of the gravity center
687   \return \c true if the gravity center is computed
688 */
689 bool OCCViewer_ViewWindow::computeGravityCenter( double& theX, double& theY, double& theZ )
690 {
691   Handle(V3d_View) aView3d = myViewPort->getView();
692
693   // Project boundaries points and add to avergae gravity
694   // the ones which lie within the screen limits
695   Standard_Real aScreenLimits[4] = { 0.0, 0.0, 0.0, 0.0 };
696
697   // NDC space screen limits
698   aScreenLimits[0] = -1.0;
699   aScreenLimits[1] =  1.0;
700   aScreenLimits[2] = -1.0;
701   aScreenLimits[3] =  1.0;
702
703   Standard_Integer aPointsNb = 0;
704
705   Standard_Real aXmin = 0.0;
706   Standard_Real aYmin = 0.0;
707   Standard_Real aZmin = 0.0;
708   Standard_Real aXmax = 0.0;
709   Standard_Real aYmax = 0.0;
710   Standard_Real aZmax = 0.0;
711
712   Graphic3d_MapOfStructure aSetOfStructures;
713   aView3d->View()->DisplayedStructures( aSetOfStructures );
714   Graphic3d_MapIteratorOfMapOfStructure aStructureIt( aSetOfStructures );
715
716   for( ; aStructureIt.More(); aStructureIt.Next() ) {
717     const Handle(Graphic3d_Structure)& aStructure = aStructureIt.Key();
718     if ( aStructure->IsEmpty() || !aStructure->IsVisible() || aStructure->CStructure()->IsForHighlight )
719       continue;
720
721     Bnd_Box aBox1 = aStructure->MinMaxValues();
722     const Graphic3d_BndBox3d& aBox = aStructure->CStructure()->BoundingBox();
723     if (!aBox.IsValid())
724       continue;
725     aXmin = /*aBox.IsVoid() ? RealFirst() :*/ aBox.CornerMin().x();
726     aYmin = /*aBox.IsVoid() ? RealFirst() : */aBox.CornerMin().y();
727     aZmin = /*aBox.IsVoid() ? RealFirst() : */aBox.CornerMin().z();
728     aXmax = /*aBox.IsVoid() ? RealLast()  : */aBox.CornerMax().x();
729     aYmax = /*aBox.IsVoid() ? RealLast()  : */aBox.CornerMax().y();
730     aZmax = /*aBox.IsVoid() ? RealLast()  : */aBox.CornerMax().z();
731
732     // Infinite structures are skipped
733     Standard_Real aLIM = ShortRealLast() - 1.0;
734     if ( Abs( aXmin ) > aLIM || Abs( aYmin ) > aLIM || Abs( aZmin ) > aLIM
735       || Abs( aXmax ) > aLIM || Abs( aYmax ) > aLIM || Abs( aZmax ) > aLIM ) {
736       continue;
737     }
738
739     gp_Pnt aPoints[8] = {
740       gp_Pnt( aXmin, aYmin, aZmin ), gp_Pnt( aXmin, aYmin, aZmax ),
741       gp_Pnt( aXmin, aYmax, aZmin ), gp_Pnt( aXmin, aYmax, aZmax ),
742       gp_Pnt( aXmax, aYmin, aZmin ), gp_Pnt( aXmax, aYmin, aZmax ),
743       gp_Pnt( aXmax, aYmax, aZmin ), gp_Pnt( aXmax, aYmax, aZmax )
744     };
745
746     for ( Standard_Integer aPointIt = 0; aPointIt < 8; ++aPointIt ) {
747       const gp_Pnt& aBBPoint = aPoints[aPointIt];
748
749       gp_Pnt aProjected = aView3d->Camera()->Project( aBBPoint );
750       const Standard_Real& U = aProjected.X();
751       const Standard_Real& V = aProjected.Y();
752
753       if (U >= aScreenLimits[0]
754        && U <= aScreenLimits[1]
755        && V >= aScreenLimits[2]
756        && V <= aScreenLimits[3])
757       {
758         aPointsNb++;
759         theX += aBBPoint.X();
760         theY += aBBPoint.Y();
761         theZ += aBBPoint.Z();
762       }
763     }
764   }
765
766   if ( aPointsNb > 0 )
767   {
768     theX /= aPointsNb;
769     theY /= aPointsNb;
770     theZ /= aPointsNb;
771     return true;
772   }
773   else
774     return false;
775 }
776
777 /*!
778   \brief Set the gravity center as a rotation point.
779 */
780 void OCCViewer_ViewWindow::activateSetRotationGravity()
781 {
782   if ( myRotationPointSelection )
783   {
784     Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
785 #if OCC_VERSION_LARGE <= 0x07030000
786     ic->CloseAllContexts( Standard_True );
787 #else
788     ic->Deactivate();
789     ic->Activate(0);
790 #endif
791     myOperation = NOTHING;
792     myViewPort->setCursor( myCursor );
793     myCursorIsHand = false;
794     myRotationPointSelection = false;
795   }
796
797   myPrevPointType = myCurrPointType;
798   myCurrPointType = BBCENTER;
799
800   Standard_Real Xcenter, Ycenter, Zcenter;
801   if ( OCCViewer_Utilities::computeVisibleBBCenter( myViewPort->getView(), Xcenter, Ycenter, Zcenter ) )
802     mySetRotationPointDlg->setCoords( Xcenter, Ycenter, Zcenter );
803 }
804
805 /*!
806   \brief Update gravity center in the "Set Rotation Point" dialog box.
807   \sa OCCViewer_SetRotationPointDlg class
808 */
809 void OCCViewer_ViewWindow::updateGravityCoords()
810 {
811   if ( mySetRotationPointDlg && mySetRotationPointDlg->isVisible() && myCurrPointType == BBCENTER )
812   {
813     Standard_Real Xcenter, Ycenter, Zcenter;
814     if ( OCCViewer_Utilities::computeVisibleBBCenter( myViewPort->getView(), Xcenter, Ycenter, Zcenter ) )
815       mySetRotationPointDlg->setCoords( Xcenter, Ycenter, Zcenter );
816   }
817 }
818
819 /*!
820   \brief Set the point selected by the user as a rotation point.
821   \param theX X coordinate of the rotation point
822   \param theY Y coordinate of the rotation point
823   \param theZ Z coordinate of the rotation point
824 */
825 void OCCViewer_ViewWindow::activateSetRotationSelected( double theX, double theY, double theZ )
826 {
827   if ( myRotationPointSelection )
828   {
829     Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
830 #if OCC_VERSION_LARGE < 0x07030000
831     ic->CloseAllContexts( Standard_True );
832 #else
833     ic->Deactivate();
834     ic->Activate(0);
835 #endif
836     myOperation = NOTHING;
837     myViewPort->setCursor( myCursor );
838     myCursorIsHand = false;
839     myRotationPointSelection = false;
840   }
841
842   myPrevPointType = myCurrPointType;
843   myCurrPointType = SELECTED;
844   mySelectedPoint.SetCoord(theX,theY,theZ);
845 }
846
847 /*!
848   \brief Start the shape selection process.
849 */
850 void OCCViewer_ViewWindow::activateStartPointSelection( TopAbs_ShapeEnum theShapeType )
851 {
852   myPrevPointType = myCurrPointType;
853   myCurrPointType = SELECTED;
854
855   // activate selection ------>
856   Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
857 #if OCC_VERSION_LARGE <= 0x07030000
858   ic->OpenLocalContext();
859 #else
860   ic->Deactivate();
861 #endif
862
863   AIS_ListOfInteractive aList;
864   ic->DisplayedObjects( aList );
865   for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
866   {
867     Handle(AIS_InteractiveObject) anObj = it.Value();
868     if ( !anObj.IsNull() && anObj->HasPresentation() &&
869          anObj->IsKind( STANDARD_TYPE(AIS_Shape) ) )
870     {
871       ic->Load(anObj,-1);
872       ic->Activate(anObj,AIS_Shape::SelectionMode(theShapeType));
873      }
874   }
875   // activate selection <------
876
877   if ( !myCursorIsHand )
878   {
879     QCursor handCursor (Qt::PointingHandCursor);
880     myCursorIsHand = true;
881     saveCursor();
882     myViewPort->setCursor( handCursor );
883   }
884   myRotationPointSelection = true;
885 }
886
887 /*!
888   \brief Start global panning operation
889
890   Sets the corresponding cursor for the widget.
891 */
892 void OCCViewer_ViewWindow::activateGlobalPanning()
893 {
894   Handle(V3d_View) aView3d = myViewPort->getView();
895   if ( !aView3d.IsNull() ) {
896     QPixmap globalPanPixmap (imageCrossCursor);
897     QCursor glPanCursor (globalPanPixmap);
898     myCurScale = aView3d->Scale();
899     aView3d->FitAll(0.01, false);
900     saveCursor();                // save old cursor
901     myViewPort->fitAll(); // fits view before selecting a new scene center
902     if( setTransformRequested( PANGLOBAL ) )
903       myViewPort->setCursor( glPanCursor );
904   }
905 }
906
907 /*!
908   \brief Starts fit operation.
909
910   Sets the corresponding cursor for the widget.
911 */
912 void OCCViewer_ViewWindow::activateWindowFit()
913 {
914   if ( !transformRequested() && !myCursorIsHand )
915     saveCursor();                /* save old cursor */
916
917   if ( myOperation != WINDOWFIT ) {
918     QCursor handCursor (Qt::PointingHandCursor);
919     if( setTransformRequested ( WINDOWFIT ) )
920     {
921       myViewPort->setCursor ( handCursor );
922       myCursorIsHand = true;
923     }
924   }
925 }
926
927 /*!
928   \brief Start delayed viewer operation.
929 */
930 bool OCCViewer_ViewWindow::setTransformRequested( OperationType op )
931 {
932   bool ok = transformEnabled( op );
933   myOperation = ok ? op : NOTHING;
934   myViewPort->setMouseTracking( myOperation == NOTHING );
935   return ok;
936 }
937
938 /*!
939   \brief Handle mouse move event.
940   \param theEvent mouse event
941 */
942 void OCCViewer_ViewWindow::vpMouseMoveEvent( QMouseEvent* theEvent )
943 {
944   if ( myIsKeyFree && interactionStyle() == SUIT_ViewModel::KEY_FREE ) {
945     myIsKeyFree = false;
946     switch ( getButtonState( theEvent, interactionStyle() ) ) {
947     case ZOOMVIEW:
948       myViewPort->startZoomAtPoint( myStartX, myStartY );
949       activateZoom();
950       break;
951     case PANVIEW:
952       activatePanning();
953       break;
954     case ROTATE:
955       activateRotation();
956       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
957       break;
958     default:
959       break;
960     }
961   }
962
963   myCurrX = theEvent->x();
964   myCurrY = theEvent->y();
965   switch (myOperation) {
966   case ROTATE:
967     myViewPort->rotate(myCurrX, myCurrY, myCurrPointType, mySelectedPoint);
968     break;
969
970   case ZOOMVIEW:
971     myViewPort->zoom(myStartX, myStartY, myCurrX, myCurrY);
972     myStartX = myCurrX;
973     myStartY = myCurrY;
974     break;
975
976   case PANVIEW:
977     myViewPort->pan(myCurrX - myStartX, myStartY - myCurrY);
978     myStartX = myCurrX;
979     myStartY = myCurrY;
980     break;
981
982 /*    case WINDOWFIT:
983     myDrawRect = true;
984     repaint();
985     break;
986 */
987   case PANGLOBAL:
988     break;
989
990   default:
991     if ( myRotationPointSelection || isSketcherStyle() )
992     {
993       emit mouseMoving( this, theEvent );
994     }
995     else
996     {
997       int aState = theEvent->modifiers();
998       int aButton = theEvent->buttons();
999       int anInteractionStyle = interactionStyle();
1000       if ( ( anInteractionStyle == SUIT_ViewModel::STANDARD &&
1001            aButton == Qt::LeftButton && ( aState == Qt::NoModifier || Qt::ShiftModifier ) ) ||
1002          ( anInteractionStyle == SUIT_ViewModel::KEY_FREE &&
1003          aButton == Qt::LeftButton && ( aState == Qt::ControlModifier || aState == ( Qt::ControlModifier|Qt::ShiftModifier ) ) ) ) {
1004         myDrawRect = myEnableDrawMode;
1005         if ( myDrawRect ) {
1006           drawRect();
1007           if ( !myCursorIsHand )        {   // we are going to sketch a rectangle
1008             QCursor handCursor (Qt::PointingHandCursor);
1009             myCursorIsHand = true;
1010             saveCursor();
1011             myViewPort->setCursor( handCursor );
1012           }
1013         }
1014         emit mouseMoving( this, theEvent );
1015       }
1016       else if ( ( anInteractionStyle == SUIT_ViewModel::STANDARD &&
1017                 aButton == Qt::RightButton && ( aState == Qt::NoModifier || Qt::ShiftModifier ) ) ||
1018                 ( anInteractionStyle == SUIT_ViewModel::KEY_FREE &&
1019                 aButton == Qt::RightButton && ( aState == Qt::ControlModifier || aState == ( Qt::ControlModifier|Qt::ShiftModifier ) ) ) ) {
1020         OCCViewer_ViewSketcher* sketcher = 0;
1021         QList<OCCViewer_ViewSketcher*>::Iterator it;
1022         for ( it = mySketchers.begin(); it != mySketchers.end() && !sketcher; ++it )
1023         {
1024           OCCViewer_ViewSketcher* sk = (*it);
1025           if( sk->isDefault() && sk->sketchButton() == aButton )
1026             sketcher = sk;
1027         }
1028         if ( sketcher && myCurSketch == -1 )
1029         {
1030           activateSketching( sketcher->type() );
1031           if ( mypSketcher )
1032           {
1033             myCurSketch = mypSketcher->sketchButton();
1034
1035             if ( l_mbPressEvent )
1036             {
1037               QApplication::sendEvent( getViewPort(), l_mbPressEvent );
1038               delete l_mbPressEvent;
1039               l_mbPressEvent = 0;
1040             }
1041             QApplication::sendEvent( getViewPort(), theEvent );
1042           }
1043         }
1044       }
1045       else
1046         emit mouseMoving( this, theEvent );
1047     }
1048   }
1049 }
1050
1051 /*!
1052   \brief Handle mouse release event.
1053   \param theEvent mouse event
1054 */
1055 void OCCViewer_ViewWindow::vpMouseReleaseEvent(QMouseEvent* theEvent)
1056 {
1057   switch ( myOperation ) {
1058   case NOTHING:
1059     {
1060       int prevState = myCurSketch;
1061       if(theEvent->button() == Qt::RightButton)
1062       {
1063         QList<OCCViewer_ViewSketcher*>::Iterator it;
1064         for ( it = mySketchers.begin(); it != mySketchers.end() && myCurSketch != -1; ++it )
1065         {
1066           OCCViewer_ViewSketcher* sk = (*it);
1067           if( ( sk->sketchButton() & theEvent->button() ) && sk->sketchButton() == myCurSketch )
1068             myCurSketch = -1;
1069         }
1070       }
1071
1072       emit mouseReleased(this, theEvent);
1073       if(theEvent->button() == Qt::RightButton && prevState == -1)
1074       {
1075         QContextMenuEvent aEvent( QContextMenuEvent::Mouse,
1076                                   theEvent->pos(), theEvent->globalPos() );
1077         emit contextMenuRequested( &aEvent );
1078       }
1079     }
1080     break;
1081   case ROTATE:
1082     myViewPort->endRotation();
1083     resetState();
1084     break;
1085
1086   case PANVIEW:
1087   case ZOOMVIEW:
1088     {
1089       OCCViewer_ViewManager* aMgr = dynamic_cast<OCCViewer_ViewManager*>( getViewManager() );
1090       bool isChained = aMgr->isChainedOperations();
1091       bool isReset = !( myOperation==PANVIEW && myPanningByBtn && isChained ) || theEvent->button() == Qt::RightButton;
1092       if( isReset )
1093         resetState();
1094       break;
1095     }
1096
1097   case PANGLOBAL:
1098     if ( theEvent->button() == Qt::LeftButton ) {
1099       myViewPort->setCenter( theEvent->x(), theEvent->y() );
1100       myViewPort->getView()->SetScale(myCurScale);
1101       resetState();
1102     }
1103     break;
1104
1105   case WINDOWFIT:
1106     if ( theEvent->button() == Qt::LeftButton ) {
1107       myCurrX = theEvent->x();
1108       myCurrY = theEvent->y();
1109       drawRect();
1110       QRect rect = SUIT_Tools::makeRect(myStartX, myStartY, myCurrX, myCurrY);
1111       if ( !rect.isEmpty() ) myViewPort->fitRect(rect);
1112       endDrawRect();
1113       resetState();
1114     }
1115     break;
1116   }
1117
1118   // NOTE: viewer 3D detects a rectangle of selection using this event
1119   // so we must emit it BEFORE resetting the selection rectangle
1120
1121   if ( theEvent->button() == Qt::LeftButton && myDrawRect ) {
1122     drawRect();
1123     endDrawRect();
1124     resetState();
1125     myViewPort->update();
1126   }
1127
1128   if ( l_mbPressEvent )
1129   {
1130     delete l_mbPressEvent;
1131     l_mbPressEvent = 0;
1132   }
1133 }
1134
1135 /*!
1136   \brief Reset the viewport to its initial state
1137   ( no transformations in process etc. )
1138 */
1139 void OCCViewer_ViewWindow::resetState()
1140 {
1141   myDrawRect = false;
1142
1143   if ( myRotationPointSelection )
1144   {
1145     QCursor handCursor (Qt::PointingHandCursor);
1146     myViewPort->setCursor( handCursor );
1147   }
1148   else
1149   {
1150     if ( transformRequested() || myCursorIsHand )
1151       myViewPort->setCursor( myCursor );
1152     myCursorIsHand = false;
1153   }
1154
1155   if ( transformRequested() )
1156     emit vpTransformationFinished (myOperation);
1157
1158   setTransformInProcess( false );
1159   setTransformRequested( NOTHING );
1160
1161   myPanningByBtn = false;
1162 }
1163
1164
1165 /*!
1166   \brief Draw rubber band rectangle.
1167 */
1168 void OCCViewer_ViewWindow::drawRect()
1169 {
1170   if ( !myRectBand ) {
1171     myRectBand = new QtxRectRubberBand( myViewPort );
1172     //QPalette palette;
1173     //palette.setColor(myRectBand->foregroundRole(), Qt::white);
1174     //myRectBand->setPalette(palette);
1175   }
1176   //myRectBand->hide();
1177
1178   myRectBand->setUpdatesEnabled ( false );
1179   QRect aRect = SUIT_Tools::makeRect(myStartX, myStartY, myCurrX, myCurrY);
1180   myRectBand->initGeometry( aRect );
1181
1182   if ( !myRectBand->isVisible() )
1183     myRectBand->show();
1184
1185   myRectBand->setUpdatesEnabled ( true );
1186   //myRectBand->repaint();
1187
1188   //myRectBand->setVisible( aRect.isValid() );
1189   //if ( myRectBand->isVisible() )
1190   //  myRectBand->repaint();
1191   //else
1192   //  myRectBand->show();
1193   //myRectBand->repaint();
1194 }
1195
1196 /*!
1197   \brief Clear rubber band rectangle on the end on the dragging operation.
1198 */
1199 void OCCViewer_ViewWindow::endDrawRect()
1200 {
1201   //delete myRectBand;
1202   //myRectBand = 0;
1203   if ( myRectBand )
1204     {
1205       myRectBand->clearGeometry();
1206       myRectBand->hide();
1207     }
1208 }
1209
1210 /*!
1211   \brief Create actions.
1212 */
1213 void OCCViewer_ViewWindow::createActions()
1214 {
1215   if( !toolMgr()->isEmpty() )
1216     return;
1217
1218   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
1219   if( !aResMgr )
1220     return;
1221
1222   QtxAction* aAction;
1223
1224   // Dump view
1225   aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_DUMP" ) ),
1226                            tr( "MNU_DUMP_VIEW" ), 0, this);
1227   aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
1228   connect(aAction, SIGNAL(triggered()), this, SLOT(onDumpView()));
1229   toolMgr()->registerAction( aAction, DumpId );
1230
1231   // FitAll
1232   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_FITALL" ) ),
1233                            tr( "MNU_FITALL" ), 0, this);
1234   aAction->setStatusTip(tr("DSC_FITALL"));
1235   connect(aAction, SIGNAL(triggered()), this, SLOT(onFitAll()));
1236   toolMgr()->registerAction( aAction, FitAllId );
1237
1238   // FitRect
1239   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_FITAREA" ) ),
1240                            tr( "MNU_FITRECT" ), 0, this);
1241   aAction->setStatusTip(tr("DSC_FITRECT"));
1242   connect(aAction, SIGNAL(triggered()), this, SLOT(activateWindowFit()));
1243   toolMgr()->registerAction( aAction, FitRectId );
1244
1245   // FitSelection
1246   aAction = new QtxAction(tr("MNU_FITSELECTION"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_FITSELECTION" ) ),
1247                            tr( "MNU_FITSELECTION" ), 0, this);
1248   aAction->setStatusTip(tr("DSC_FITSELECTION"));
1249   connect(aAction, SIGNAL(triggered()), this, SLOT(onFitSelection()));
1250   toolMgr()->registerAction( aAction, FitSelectionId );
1251
1252   // Zoom
1253   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_ZOOM" ) ),
1254                            tr( "MNU_ZOOM_VIEW" ), 0, this);
1255   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
1256   connect(aAction, SIGNAL(triggered()), this, SLOT(activateZoom()));
1257   toolMgr()->registerAction( aAction, ZoomId );
1258
1259   // Panning
1260   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_PAN" ) ),
1261                            tr( "MNU_PAN_VIEW" ), 0, this);
1262   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
1263   connect(aAction, SIGNAL(triggered()), this, SLOT(onPanning()));
1264   toolMgr()->registerAction( aAction, PanId );
1265
1266   // Global Panning
1267   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_GLOBALPAN" ) ),
1268                            tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
1269   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
1270   connect(aAction, SIGNAL(triggered()), this, SLOT(activateGlobalPanning()));
1271   toolMgr()->registerAction( aAction, GlobalPanId );
1272
1273   // Rotation Point
1274   mySetRotationPointAction = new QtxAction(tr("MNU_CHANGINGROTATIONPOINT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_ROTATION_POINT" ) ),
1275                            tr( "MNU_CHANGINGROTATIONPOINT_VIEW" ), 0, this);
1276   mySetRotationPointAction->setStatusTip(tr("DSC_CHANGINGROTATIONPOINT_VIEW"));
1277   mySetRotationPointAction->setCheckable( true );
1278   connect(mySetRotationPointAction, SIGNAL(toggled( bool )), this, SLOT(onSetRotationPoint( bool )));
1279   toolMgr()->registerAction( mySetRotationPointAction, ChangeRotationPointId );
1280
1281   // Rotation
1282   aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_ROTATE" ) ),
1283                            tr( "MNU_ROTATE_VIEW" ), 0, this);
1284   aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
1285   connect(aAction, SIGNAL(triggered()), this, SLOT(activateRotation()));
1286   toolMgr()->registerAction( aAction, RotationId );
1287
1288   // Projections
1289   aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_FRONT" ) ),
1290                            tr( "MNU_FRONT_VIEW" ), 0, this, false, "Viewers:Front view");
1291   aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
1292   connect(aAction, SIGNAL(triggered()), this, SLOT(onFrontView()));
1293   this->addAction(aAction);
1294   toolMgr()->registerAction( aAction, FrontId );
1295
1296   aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_BACK" ) ),
1297                            tr( "MNU_BACK_VIEW" ), 0, this, false, "Viewers:Back view");
1298   aAction->setStatusTip(tr("DSC_BACK_VIEW"));
1299   connect(aAction, SIGNAL(triggered()), this, SLOT(onBackView()));
1300   this->addAction(aAction);
1301   toolMgr()->registerAction( aAction, BackId );
1302
1303   aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_TOP" ) ),
1304                            tr( "MNU_TOP_VIEW" ), 0, this, false, "Viewers:Top view");
1305   aAction->setStatusTip(tr("DSC_TOP_VIEW"));
1306   connect(aAction, SIGNAL(triggered()), this, SLOT(onTopView()));
1307   this->addAction(aAction);
1308   toolMgr()->registerAction( aAction, TopId );
1309
1310   aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_BOTTOM" ) ),
1311                            tr( "MNU_BOTTOM_VIEW" ), 0, this, false, "Viewers:Bottom view");
1312   aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
1313   connect(aAction, SIGNAL(triggered()), this, SLOT(onBottomView()));
1314   this->addAction(aAction);
1315   toolMgr()->registerAction( aAction, BottomId );
1316
1317   aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_LEFT" ) ),
1318                            tr( "MNU_LEFT_VIEW" ), 0, this, false, "Viewers:Left view");
1319   aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
1320   connect(aAction, SIGNAL(triggered()), this, SLOT(onLeftView()));
1321   this->addAction(aAction);
1322   toolMgr()->registerAction( aAction, LeftId );
1323
1324   aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_RIGHT" ) ),
1325                            tr( "MNU_RIGHT_VIEW" ), 0, this, false, "Viewers:Right view");
1326   aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
1327   connect(aAction, SIGNAL(triggered()), this, SLOT(onRightView()));
1328   this->addAction(aAction);
1329   toolMgr()->registerAction( aAction, RightId );
1330
1331   // rotate anticlockwise
1332   aAction = new QtxAction(tr("MNU_ANTICLOCKWISE_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_ANTICLOCKWISE" ) ),
1333                            tr( "MNU_ANTICLOCKWISE_VIEW" ), 0, this, false, "Viewers:Rotate anticlockwise");
1334   aAction->setStatusTip(tr("DSC_ANTICLOCKWISE_VIEW"));
1335   connect(aAction, SIGNAL(triggered()), this, SLOT(onAntiClockWiseView()));
1336   this->addAction(aAction);
1337   toolMgr()->registerAction( aAction, AntiClockWiseId );
1338
1339   // rotate clockwise
1340   aAction = new QtxAction(tr("MNU_CLOCKWISE_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_CLOCKWISE" ) ),
1341                            tr( "MNU_CLOCKWISE_VIEW" ), 0, this, false, "Viewers:Rotate clockwise");
1342   aAction->setStatusTip(tr("DSC_CLOCKWISE_VIEW"));
1343   connect(aAction, SIGNAL(triggered()), this, SLOT(onClockWiseView()));
1344   this->addAction(aAction);
1345   toolMgr()->registerAction( aAction, ClockWiseId );
1346
1347   // Projection mode group
1348
1349   // - orthographic projection
1350   aAction = new QtxAction(tr("MNU_ORTHOGRAPHIC_MODE"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_ORTHOGRAPHIC" ) ),
1351                           tr( "MNU_ORTHOGRAPHIC_MODE" ), 0, this);
1352   aAction->setStatusTip(tr("DSC_ORTHOGRAPHIC_MODE"));
1353   aAction->setCheckable(true);
1354   toolMgr()->registerAction( aAction, OrthographicId );
1355
1356   // - perspective projection
1357   aAction = new QtxAction(tr("MNU_PERSPECTIVE_MODE"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_PERSPECTIVE" ) ),
1358                           tr( "MNU_PERSPECTIVE_MODE" ), 0, this);
1359   aAction->setStatusTip(tr("DSC_PERSPECTIVE_MODE"));
1360   aAction->setCheckable(true);
1361   toolMgr()->registerAction( aAction, PerspectiveId );
1362
1363   // - stereo projection
1364   aAction = new QtxAction(tr("MNU_STEREO_MODE"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_STEREO" ) ),
1365                           tr( "MNU_STEREO_MODE" ), 0, this);
1366   aAction->setStatusTip(tr("DSC_STEREO_MODE"));
1367   aAction->setCheckable(true);
1368   toolMgr()->registerAction( aAction, StereoId );
1369   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onStereoType(bool)));
1370
1371   // - add exclusive action group
1372   QActionGroup* aProjectionGroup = new QActionGroup( this );
1373   aProjectionGroup->addAction( toolMgr()->action( OrthographicId ) );
1374   aProjectionGroup->addAction( toolMgr()->action( PerspectiveId ) );
1375   connect(aProjectionGroup, SIGNAL(triggered(QAction*)), this, SLOT(onProjectionType(QAction*)));
1376   
1377   // Reset
1378   aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_RESET" ) ),
1379                            tr( "MNU_RESET_VIEW" ), 0, this, false, "Viewers:Reset view");
1380   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
1381   connect(aAction, SIGNAL(triggered()), this, SLOT(onResetView()));
1382   this->addAction(aAction);
1383   toolMgr()->registerAction( aAction, ResetId );
1384
1385   // Clone
1386   aAction = new QtxAction(tr("MNU_CLONE_VIEW"),
1387                           aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_CLONE_VIEW")),
1388                           tr("MNU_CLONE_VIEW"), 0, this);
1389   aAction->setStatusTip(tr("DSC_CLONE_VIEW"));
1390   connect(aAction, SIGNAL(triggered()), this, SLOT(onCloneView()));
1391   toolMgr()->registerAction( aAction, CloneId );
1392
1393   aAction = new QtxAction (tr("MNU_CLIPPING"), aResMgr->loadPixmap ("OCCViewer", tr("ICON_OCCVIEWER_CLIPPING")),
1394                                       tr("MNU_CLIPPING"), 0, this);
1395   aAction->setStatusTip (tr("DSC_CLIPPING"));
1396   aAction->setCheckable (true);
1397   connect (aAction, SIGNAL (toggled (bool)), this, SLOT (onClipping (bool)));
1398   toolMgr()->registerAction (aAction, ClippingId);
1399
1400   aAction = new QtxAction(tr("MNU_SHOOT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_SHOOT_VIEW" ) ),
1401                            tr( "MNU_SHOOT_VIEW" ), 0, this);
1402   aAction->setStatusTip(tr("DSC_SHOOT_VIEW"));
1403   connect(aAction, SIGNAL(triggered()), this, SLOT(onMemorizeView()));
1404   toolMgr()->registerAction( aAction, MemId );
1405
1406   aAction = new QtxAction(tr("MNU_PRESETS_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_PRESETS_VIEW" ) ),
1407                            tr( "MNU_PRESETS_VIEW" ), 0, this);
1408   aAction->setStatusTip(tr("DSC_PRESETS_VIEW"));
1409   connect(aAction, SIGNAL(triggered()), this, SLOT(onRestoreView()));
1410   toolMgr()->registerAction( aAction, RestoreId );
1411
1412   if (myModel->trihedronActivated()) {
1413     aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRE"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_TRIHEDRON" ) ),
1414                              tr( "MNU_SHOW_TRIHEDRE" ), 0, this);
1415     aAction->setCheckable( true );
1416     aAction->setChecked( true );
1417     aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRE"));
1418     connect(aAction, SIGNAL(toggled(bool)), this, SLOT(onTrihedronShow(bool)));
1419     toolMgr()->registerAction( aAction, TrihedronShowId );
1420   }
1421
1422   // Scale
1423   aAction = new QtxAction(tr("MNU_SCALING"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_SCALING" ) ),
1424                            tr( "MNU_SCALING" ), 0, this);
1425   aAction->setStatusTip(tr("DSC_SCALING"));
1426   connect(aAction, SIGNAL(triggered()), this, SLOT(onAxialScale()));
1427   toolMgr()->registerAction( aAction, AxialScaleId );
1428
1429   // Enable/disable preselection
1430   aAction = new QtxAction(tr("MNU_ENABLE_PRESELECTION"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_PRESELECTION" ) ),
1431                           tr( "MNU_ENABLE_PRESELECTION" ), 0, this);
1432   aAction->setStatusTip(tr("DSC_ENABLE_PRESELECTION"));
1433   aAction->setCheckable(true);
1434   connect(aAction, SIGNAL(toggled(bool)), this, SLOT(onSwitchPreselection(bool)));
1435   toolMgr()->registerAction( aAction, SwitchPreselectionId );
1436
1437   // Enable/disable selection
1438   aAction = new QtxAction(tr("MNU_ENABLE_SELECTION"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_SELECTION" ) ),
1439                           tr( "MNU_ENABLE_SELECTION" ), 0, this);
1440   aAction->setStatusTip(tr("DSC_ENABLE_SELECTION"));
1441   aAction->setCheckable(true);
1442   connect(aAction, SIGNAL(toggled(bool)), this, SLOT(onSwitchSelection(bool)));
1443   toolMgr()->registerAction( aAction, SwitchSelectionId );
1444
1445   // Graduated axes
1446   aAction = new QtxAction(tr("MNU_GRADUATED_AXES"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_GRADUATED_AXES" ) ),
1447                            tr( "MNU_GRADUATED_AXES" ), 0, this);
1448   aAction->setStatusTip(tr("DSC_GRADUATED_AXES"));
1449   connect(aAction, SIGNAL(triggered()), this, SLOT(onGraduatedAxes()));
1450   toolMgr()->registerAction( aAction, GraduatedAxesId );
1451
1452   // Active only ambient light or not
1453   aAction = new QtxAction(tr("MNU_AMBIENT"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_AMBIENT" ) ),
1454                            tr( "MNU_AMBIENT" ), 0, this);
1455   aAction->setStatusTip(tr("DSC_AMBIENT"));
1456   connect(aAction, SIGNAL(triggered()), this, SLOT(onAmbientToogle()));
1457   toolMgr()->registerAction( aAction, AmbientId );
1458
1459   // Switch between interaction styles
1460   aAction = new QtxAction(tr("MNU_STYLE_SWITCH"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_STYLE_SWITCH" ) ),
1461                           tr( "MNU_STYLE_SWITCH" ), 0, this);
1462   aAction->setStatusTip(tr("DSC_STYLE_SWITCH"));
1463   aAction->setCheckable(true);
1464   connect(aAction, SIGNAL(toggled(bool)), this, SLOT(onSwitchInteractionStyle(bool)));
1465   toolMgr()->registerAction( aAction, SwitchInteractionStyleId );
1466
1467   // Switch between zooming styles
1468   aAction = new QtxAction(tr("MNU_ZOOMING_STYLE_SWITCH"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_ZOOMING_STYLE_SWITCH" ) ),
1469                           tr( "MNU_ZOOMING_STYLE_SWITCH" ), 0, this);
1470   aAction->setStatusTip(tr("DSC_ZOOMING_STYLE_SWITCH"));
1471   aAction->setCheckable(true);
1472   connect(aAction, SIGNAL(toggled(bool)), this, SLOT(onSwitchZoomingStyle(bool)));
1473   toolMgr()->registerAction( aAction, SwitchZoomingStyleId );
1474
1475   // Maximized view
1476   aAction = new QtxAction(tr("MNU_MINIMIZE_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_MINIMIZE" ) ),
1477                           tr( "MNU_MINIMIZE_VIEW" ), 0, this );
1478   aAction->setStatusTip(tr("DSC_MINIMIZE_VIEW"));
1479   connect(aAction, SIGNAL(triggered()), this, SLOT(onMaximizedView()));
1480   toolMgr()->registerAction( aAction, MaximizedId );
1481
1482   // Return to 3d view
1483   if (my2dMode!=No2dMode){
1484     aAction = new QtxAction(tr("MNU_RETURN_3D_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_RETURN_3D_VIEW" ) ),
1485                             tr( "MNU_RETURN_3D_VIEW" ), 0, this );
1486     aAction->setStatusTip(tr("DSC_RETURN_3D_VIEW"));
1487     connect(aAction, SIGNAL(triggered()), this, SLOT(returnTo3dView()));
1488     toolMgr()->registerAction( aAction, ReturnTo3dViewId );
1489   }
1490
1491   // Synchronize View
1492   toolMgr()->registerAction( synchronizeAction(), SynchronizeId );
1493 #ifdef ENABLE_RAY_TRACING
1494   // Ray tracing
1495   aAction = new QtxAction( tr("MNU_RAY_TRACING"), aResMgr->loadPixmap( "OCCViewer", tr("ICON_OCCVIEWER_RAY_TRACING") ),
1496                            tr("MNU_RAY_TRACING"), 0, this );
1497   aAction->setStatusTip( tr("DSC_RAY_TRACING") );
1498   connect( aAction, SIGNAL( triggered() ), this, SLOT( onRayTracing() ) );
1499   toolMgr()->registerAction( aAction, RayTracingId );
1500
1501   // Environment texture
1502   aAction = new QtxAction( tr("MNU_ENV_TEXTURE"), aResMgr->loadPixmap( "OCCViewer", tr("ICON_OCCVIEWER_ENV_TEXTURE") ),
1503                            tr("MNU_ENV_TEXTURE"), 0, this );
1504   aAction->setStatusTip( tr("DSC_ENV_TEXTURE") );
1505   connect( aAction, SIGNAL( triggered() ), this, SLOT( onEnvTexture() ) );
1506   toolMgr()->registerAction( aAction, EnvTextureId );
1507
1508   // Light source
1509   aAction = new QtxAction( tr("MNU_LIGHT_SOURCE"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_LIGHT_SOURCE" ) ),
1510                            tr( "MNU_LIGHT_SOURCE" ), 0, this );
1511   aAction->setStatusTip( tr("DSC_LIGHT_SOURCE") );
1512   connect( aAction, SIGNAL( triggered() ), this, SLOT( onLightSource() ) );
1513   toolMgr()->registerAction( aAction, LightSourceId );
1514 #endif
1515 }
1516
1517 /*!
1518   \brief Create toolbar.
1519 */
1520 void OCCViewer_ViewWindow::createToolBar()
1521 {
1522   static const char* titles[] = {
1523     "LBL_3DTOOLBAR_LABEL",
1524     "LBL_XYTOOLBAR_LABEL",
1525     "LBL_XZTOOLBAR_LABEL",
1526     "LBL_YZTOOLBAR_LABEL",
1527   };
1528   static const char* names[] = {
1529     "OCCViewer3DViewOperations",
1530     "OCCViewerXYViewOperations",
1531     "OCCViewerXZViewOperations",
1532     "OCCViewerYZViewOperations",
1533   };
1534   int tid = toolMgr()->createToolBar( tr( titles[my2dMode] ),        // title (language-dependant)
1535                                       QString( names[my2dMode] ),    // name (language-independant)
1536                                       false );                       // disable floatable toolbar
1537   if ( my2dMode != No2dMode ){
1538     toolMgr()->append( ReturnTo3dViewId, tid );
1539     toolMgr()->append( toolMgr()->separator(), tid );
1540   }
1541   toolMgr()->append( DumpId, tid );
1542   toolMgr()->append( SwitchInteractionStyleId, tid );
1543   toolMgr()->append( SwitchZoomingStyleId, tid );
1544   toolMgr()->append( SwitchPreselectionId, tid );
1545   toolMgr()->append( SwitchSelectionId, tid );
1546   if( myModel->trihedronActivated() )
1547     toolMgr()->append( TrihedronShowId, tid );
1548
1549   QtxMultiAction* aScaleAction = new QtxMultiAction( this );
1550   aScaleAction->insertAction( toolMgr()->action( FitAllId ) );
1551   aScaleAction->insertAction( toolMgr()->action( FitRectId ) );
1552   aScaleAction->insertAction( toolMgr()->action( FitSelectionId ) );
1553   aScaleAction->insertAction( toolMgr()->action( ZoomId ) );
1554   toolMgr()->append( aScaleAction, tid );
1555
1556   QtxMultiAction* aPanningAction = new QtxMultiAction( this );
1557   aPanningAction->insertAction( toolMgr()->action( PanId ) );
1558   aPanningAction->insertAction( toolMgr()->action( GlobalPanId ) );
1559   toolMgr()->append( aPanningAction, tid );
1560
1561   if (my2dMode == No2dMode) {
1562     toolMgr()->append( ChangeRotationPointId, tid );
1563     toolMgr()->append( RotationId, tid );
1564
1565     QtxMultiAction* aViewsAction = new QtxMultiAction( this );
1566     aViewsAction->insertAction( toolMgr()->action( FrontId ) );
1567     aViewsAction->insertAction( toolMgr()->action( BackId ) );
1568     aViewsAction->insertAction( toolMgr()->action( TopId ) );
1569     aViewsAction->insertAction( toolMgr()->action( BottomId ) );
1570     aViewsAction->insertAction( toolMgr()->action( LeftId ) );
1571     aViewsAction->insertAction( toolMgr()->action( RightId ) );
1572     toolMgr()->append( aViewsAction, tid );
1573
1574     toolMgr()->append( AntiClockWiseId, tid );
1575     toolMgr()->append( ClockWiseId, tid );
1576
1577     toolMgr()->append( OrthographicId, tid );
1578     toolMgr()->append( PerspectiveId, tid );
1579     toolMgr()->append( StereoId, tid );
1580
1581     toolMgr()->append( ResetId, tid );
1582   }
1583
1584   QtxMultiAction* aMemAction = new QtxMultiAction( this );
1585   aMemAction->insertAction( toolMgr()->action( MemId ) );
1586   aMemAction->insertAction( toolMgr()->action( RestoreId ) );
1587   toolMgr()->append( aMemAction, tid );
1588
1589   toolMgr()->append( toolMgr()->separator(), tid );
1590   toolMgr()->append( CloneId, tid );
1591
1592   toolMgr()->append( toolMgr()->separator(), tid );
1593   toolMgr()->append( ClippingId, tid );
1594   toolMgr()->append( AxialScaleId, tid );
1595   toolMgr()->append( GraduatedAxesId, tid );
1596   toolMgr()->append( AmbientId, tid );
1597
1598   toolMgr()->append( MaximizedId, tid );
1599   toolMgr()->append( SynchronizeId, tid );
1600 #ifdef ENABLE_RAY_TRACING
1601   toolMgr()->append( RayTracingId, tid );
1602   toolMgr()->append( EnvTextureId, tid );
1603   toolMgr()->append( LightSourceId, tid );
1604 #endif
1605 }
1606
1607 /*!
1608   \brief Perform 'fit all' operation.
1609 */
1610 void OCCViewer_ViewWindow::onViewFitAll()
1611 {
1612   myViewPort->fitAll();
1613 }
1614
1615 /*!
1616   \brief Perform "front view" transformation.
1617 */
1618 void OCCViewer_ViewWindow::onFrontView()
1619 {
1620   emit vpTransformationStarted ( FRONTVIEW );
1621   Handle(V3d_View) aView3d = myViewPort->getView();
1622   if (myAutomaticZoom)
1623   {
1624     if ( !aView3d.IsNull() ) 
1625       aView3d->SetProj (V3d_Xpos);
1626     onViewFitAll();
1627   }
1628   else
1629     projAndPanToGravity(V3d_Xpos);
1630   emit vpTransformationFinished ( FRONTVIEW );
1631 }
1632
1633 /*!
1634   \brief Perform "back view" transformation.
1635 */
1636 void OCCViewer_ViewWindow::onBackView()
1637 {
1638   emit vpTransformationStarted ( BACKVIEW );
1639   Handle(V3d_View) aView3d = myViewPort->getView();
1640   if (myAutomaticZoom)
1641   {
1642     if ( !aView3d.IsNull() ) 
1643       aView3d->SetProj (V3d_Xneg);
1644     onViewFitAll();
1645   }
1646   else
1647     projAndPanToGravity(V3d_Xneg);
1648   emit vpTransformationFinished ( BACKVIEW );
1649 }
1650
1651 /*!
1652   \brief Perform "top view" transformation.
1653 */
1654 void OCCViewer_ViewWindow::onTopView()
1655 {
1656   emit vpTransformationStarted ( TOPVIEW );
1657   Handle(V3d_View) aView3d = myViewPort->getView();
1658   if (myAutomaticZoom)
1659   {
1660     if ( !aView3d.IsNull() ) 
1661       aView3d->SetProj (V3d_Zpos);
1662     onViewFitAll();
1663   }
1664   else
1665     projAndPanToGravity(V3d_Zpos);
1666   emit vpTransformationFinished ( TOPVIEW );
1667 }
1668
1669 /*!
1670   \brief Perform "bottom view" transformation.
1671 */
1672 void OCCViewer_ViewWindow::onBottomView()
1673 {
1674   emit vpTransformationStarted ( BOTTOMVIEW );
1675   Handle(V3d_View) aView3d = myViewPort->getView();
1676   if (myAutomaticZoom)
1677   {
1678     if ( !aView3d.IsNull() ) 
1679       aView3d->SetProj (V3d_Zneg);
1680     onViewFitAll();
1681   }
1682   else
1683     projAndPanToGravity(V3d_Zneg);
1684   emit vpTransformationFinished ( BOTTOMVIEW );
1685 }
1686
1687 /*!
1688   \brief Perform "left view" transformation.
1689 */
1690 void OCCViewer_ViewWindow::onLeftView()
1691 {
1692   emit vpTransformationStarted ( LEFTVIEW );
1693   Handle(V3d_View) aView3d = myViewPort->getView();
1694   if (myAutomaticZoom)
1695   {
1696     if ( !aView3d.IsNull() ) 
1697       aView3d->SetProj (V3d_Yneg);
1698     onViewFitAll();
1699   }
1700   else
1701     projAndPanToGravity(V3d_Yneg);
1702   emit vpTransformationFinished ( LEFTVIEW );
1703 }
1704
1705 /*!
1706   \brief Perform "right view" transformation.
1707 */
1708 void OCCViewer_ViewWindow::onRightView()
1709 {
1710   emit vpTransformationStarted ( RIGHTVIEW );
1711   Handle(V3d_View) aView3d = myViewPort->getView();
1712   if (myAutomaticZoom)
1713   {
1714     if ( !aView3d.IsNull() ) 
1715       aView3d->SetProj (V3d_Ypos);
1716     onViewFitAll();
1717   }
1718   else
1719     projAndPanToGravity(V3d_Ypos);
1720   emit vpTransformationFinished ( RIGHTVIEW );
1721 }
1722
1723 /*!
1724   \brief Rotate view 90 degrees clockwise
1725 */
1726 void OCCViewer_ViewWindow::onClockWiseView()
1727 {
1728   emit vpTransformationStarted ( CLOCKWISEVIEW );
1729   myViewPort->rotateXY( 90. );
1730   emit vpTransformationFinished ( CLOCKWISEVIEW );
1731 }
1732
1733 /*!
1734   \brief Rotate view 90 degrees conterclockwise
1735 */
1736 void OCCViewer_ViewWindow::onAntiClockWiseView()
1737 {
1738   emit vpTransformationStarted ( ANTICLOCKWISEVIEW );
1739   myViewPort->rotateXY( -90. );
1740   emit vpTransformationFinished ( ANTICLOCKWISEVIEW );
1741 }
1742
1743 /*!
1744   \brief Perform "reset view" transformation.
1745
1746   Sets default orientation of the viewport camera.
1747 */
1748 void OCCViewer_ViewWindow::onResetView()
1749 {
1750   emit vpTransformationStarted( RESETVIEW );
1751   bool upd = myViewPort->getView()->SetImmediateUpdate( false );
1752   myViewPort->getView()->Reset( false );
1753   myViewPort->fitAll( false, true, false );
1754   myViewPort->getView()->SetImmediateUpdate( upd );
1755   onProjectionType(); // needed to apply projection type properly after reset
1756   myViewPort->getView()->Update();
1757   emit vpTransformationFinished( RESETVIEW );
1758 }
1759
1760 /*!
1761   \brief Set the given projection mode.
1762
1763   Set the given projection mode: Orthographic or Perspective.
1764 */
1765 void OCCViewer_ViewWindow::onProjectionType( QAction* theAction )
1766 {
1767   Handle(V3d_View) aView3d = myViewPort->getView();
1768   if ( !aView3d.IsNull() ) {
1769     Handle(Graphic3d_Camera) aCamera = aView3d->Camera();
1770     if (theAction == toolMgr()->action( OrthographicId )) {
1771       myModel->setProjectionType(Orthographic);
1772       aCamera->SetProjectionType ( Graphic3d_Camera::Projection_Orthographic );
1773       aCamera->SetFOVy(45.0);
1774     }
1775     else if (theAction == toolMgr()->action( PerspectiveId )) {
1776       myModel->setProjectionType(Perspective);
1777       aCamera->SetProjectionType ( Graphic3d_Camera::Projection_Perspective );
1778       aCamera->SetFOVy(30.0);
1779     }
1780     if (toolMgr()->action( StereoId )->isChecked()) {
1781       aCamera->SetProjectionType ( Graphic3d_Camera::Projection_Stereo );
1782       aCamera->SetFOVy(30.0);
1783     }
1784     aView3d->Redraw();
1785     onViewFitAll();
1786   }
1787 }
1788
1789 /*!
1790   \brief Sets Stereo projection mode.
1791
1792   Sets Stereo projection mode.
1793 */
1794 void OCCViewer_ViewWindow::onStereoType( bool activate )
1795 {
1796   Handle(V3d_View) aView3d = myViewPort->getView();
1797   if ( !aView3d.IsNull() ) {
1798     Handle(Graphic3d_Camera) aCamera = aView3d->Camera();
1799     if (activate) {
1800       toolMgr()->action( PerspectiveId )->setChecked(true);
1801       aCamera->SetProjectionType(Graphic3d_Camera::Projection_Perspective);
1802       toolMgr()->action( PerspectiveId )->actionGroup()->setEnabled(false);
1803
1804       aCamera->SetProjectionType ( Graphic3d_Camera::Projection_Stereo );
1805       SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
1806       setStereoType( aResMgr->integerValue( "OCCViewer", "stereo_type", 0 ) );
1807       setAnaglyphFilter( aResMgr->integerValue( "OCCViewer", "anaglyph_filter", 0 ) );
1808       setReverseStereo( aResMgr->booleanValue( "OCCViewer", "reverse_stereo", false ) );
1809       setVSync( aResMgr->booleanValue( "OCCViewer", "enable_vsync", true ) );
1810       setQuadBufferSupport( aResMgr->booleanValue( "OCCViewer", "enable_quad_buffer_support", false ) );
1811     }
1812     else {
1813       toolMgr()->action( PerspectiveId )->actionGroup()->setEnabled(true);
1814       if (myModel->projectionType() == Orthographic) {
1815         toolMgr()->action( OrthographicId )->setChecked(true);
1816         aCamera->SetProjectionType(Graphic3d_Camera::Projection_Orthographic);
1817       }
1818       else if (myModel->projectionType() == Perspective) {
1819         toolMgr()->action( PerspectiveId )->setChecked(true);
1820         aCamera->SetProjectionType(Graphic3d_Camera::Projection_Perspective);
1821       }
1822     }
1823     aView3d->Redraw();
1824     onViewFitAll();
1825   }
1826
1827   if ( isQuadBufferSupport() && !isOpenGlStereoSupport() && stereoType() == QuadBuffer &&
1828        toolMgr()->action( StereoId )->isChecked() )
1829     SUIT_MessageBox::warning( 0, tr( "WRN_WARNING" ),  tr( "WRN_SUPPORT_QUAD_BUFFER" ) );
1830 }
1831
1832 /*!
1833   \brief Restore the view.
1834
1835   Restore the projection mode based on tool-buttons states.
1836 */
1837 void OCCViewer_ViewWindow::onProjectionType()
1838 {
1839   emit vpTransformationStarted( PROJECTION );
1840   if (toolMgr()->action( OrthographicId )->isChecked())
1841     setProjectionType( Orthographic );
1842   if (toolMgr()->action( PerspectiveId )->isChecked())
1843     setProjectionType( Perspective );
1844   if (toolMgr()->action( StereoId )->isChecked())
1845     setProjectionType( Stereo );
1846   emit vpTransformationFinished( PROJECTION );
1847 }
1848
1849 void OCCViewer_ViewWindow::setProjectionType( int mode )
1850 {
1851   QtxAction* anOrthographicAction = dynamic_cast<QtxAction*>( toolMgr()->action( OrthographicId ) );
1852   QtxAction* aPerspectiveAction = dynamic_cast<QtxAction*>( toolMgr()->action( PerspectiveId ) );
1853   QtxAction* aStereoAction = dynamic_cast<QtxAction*>( toolMgr()->action( StereoId ) );
1854   switch ( mode ) {
1855     case Orthographic:
1856       onProjectionType( anOrthographicAction );
1857       break;
1858     case Perspective:
1859       onProjectionType( aPerspectiveAction );
1860       break;
1861     case Stereo:
1862       onStereoType( true );
1863       break;
1864   }
1865   // update action state if method is called outside
1866   if ( mode == Orthographic && !anOrthographicAction->isChecked() ) {
1867           anOrthographicAction->setChecked( true );
1868           aStereoAction->setChecked( false );
1869   }
1870   if ( mode == Perspective && !aPerspectiveAction->isChecked() ) {
1871           aPerspectiveAction->setChecked( true );
1872           aStereoAction->setChecked( false );
1873   }
1874   if ( mode == Stereo ) {
1875     aStereoAction->setChecked( true );
1876     if ( anOrthographicAction->isEnabled() ) {
1877       anOrthographicAction->setEnabled( false );
1878       anOrthographicAction->setChecked( false );
1879       aStereoAction->setChecked( false );
1880     }
1881     else {
1882       anOrthographicAction->setEnabled( true );
1883       aStereoAction->setChecked( false );
1884       anOrthographicAction->setChecked(myModel->projectionType() == Orthographic);
1885     }
1886     if ( aPerspectiveAction->isEnabled() ) {
1887       aPerspectiveAction->setEnabled( false );
1888       aPerspectiveAction->setChecked( true );
1889       if ( isQuadBufferSupport() && !isOpenGlStereoSupport() && stereoType() == QuadBuffer &&
1890            toolMgr()->action( StereoId )->isChecked() )
1891         SUIT_MessageBox::warning( 0, tr( "WRN_WARNING" ),  tr( "WRN_SUPPORT_QUAD_BUFFER" ) );
1892     }
1893     else {
1894       aPerspectiveAction->setEnabled( true );
1895       aStereoAction->setChecked( false );
1896       aPerspectiveAction->setChecked(myModel->projectionType() == Perspective);
1897       onProjectionType();
1898     }
1899   }
1900   else {
1901     if ( !anOrthographicAction->isEnabled() )
1902       anOrthographicAction->setEnabled( true );
1903     if ( !aPerspectiveAction->isEnabled() )
1904       aPerspectiveAction->setEnabled( true );
1905   }
1906 }
1907
1908 /*!
1909   \brief Perform "fit all" transformation.
1910 */
1911 void OCCViewer_ViewWindow::onFitAll()
1912 {
1913   emit vpTransformationStarted( FITALLVIEW );
1914   myViewPort->fitAll();
1915   emit vpTransformationFinished( FITALLVIEW );
1916 }
1917
1918 /*!
1919   \brief Perform "fit selection" transformation.
1920 */
1921 void OCCViewer_ViewWindow::onFitSelection()
1922 {
1923   emit vpTransformationStarted( FITSELECTION );
1924   myModel->getAISContext()->FitSelected( getViewPort()->getView() );
1925   emit vpTransformationFinished( FITSELECTION );
1926 }
1927
1928 /*!
1929   \brief Called if 'change rotation point' operation is activated.
1930   \param on action state
1931 */
1932 void OCCViewer_ViewWindow::onSetRotationPoint( bool on )
1933 {
1934   if (on)
1935   {
1936     if (!mySetRotationPointDlg)
1937     {
1938       mySetRotationPointDlg = new OCCViewer_SetRotationPointDlg (this);
1939       mySetRotationPointDlg->SetAction(mySetRotationPointAction);
1940     }
1941
1942     if (!mySetRotationPointDlg->isVisible())
1943     {
1944       //if (mySetRotationPointDlg->IsFirstShown())
1945       if (myCurrPointType == BBCENTER)
1946       {
1947         Standard_Real Xcenter, Ycenter, Zcenter;
1948         if (OCCViewer_Utilities::computeVisibleBBCenter(myViewPort->getView(), Xcenter, Ycenter, Zcenter))
1949           mySetRotationPointDlg->setCoords(Xcenter, Ycenter, Zcenter);
1950       }
1951       mySetRotationPointDlg->show();
1952     }
1953   }
1954   else
1955   {
1956     if (mySetRotationPointDlg->isVisible())
1957       mySetRotationPointDlg->hide();
1958   }
1959 }
1960
1961 /*!
1962    \brief Create one more window with same content.
1963 */
1964 void OCCViewer_ViewWindow::onCloneView()
1965 {
1966   SUIT_ViewWindow* vw = myManager->createViewWindow();
1967   //vw->show();
1968   emit viewCloned( vw );
1969   OCCViewer_ViewWindow* occVw = dynamic_cast<OCCViewer_ViewWindow*> (vw);
1970   if(occVw && occVw->getView(OCCViewer_ViewFrame::MAIN_VIEW)) {
1971     occVw->getView(OCCViewer_ViewFrame::MAIN_VIEW)->synchronize(this);
1972   }
1973 }
1974
1975 /*!
1976   Creates one more window with same content
1977 */
1978 void OCCViewer_ViewWindow::onAxialScale()
1979 {
1980   if ( !myScalingDlg )
1981     myScalingDlg = new OCCViewer_AxialScaleDlg( this , myModel );
1982
1983   if ( !myScalingDlg->isVisible() )
1984   {
1985     myScalingDlg->Update();
1986     myScalingDlg->show();
1987   }
1988 }
1989
1990 /*!
1991   Shows Graduated Axes dialog
1992 */
1993 void OCCViewer_ViewWindow::onGraduatedAxes()
1994 {
1995   myCubeAxesDlg->Update();
1996   myCubeAxesDlg->show();
1997 }
1998
1999 void OCCViewer_ViewWindow::onAmbientToogle()
2000 {
2001   Handle(V3d_Viewer) viewer = myViewPort->getViewer();
2002   viewer->InitDefinedLights();
2003   while(viewer->MoreDefinedLights())
2004     {
2005       Handle(V3d_Light) light = viewer->DefinedLight();
2006       if(light->Type() != V3d_AMBIENT)
2007         {
2008           Handle(V3d_View) aView3d = myViewPort->getView();
2009           if( aView3d->IsActiveLight(light) ) viewer->SetLightOff(light);
2010           else viewer->SetLightOn(light);
2011         }
2012       viewer->NextDefinedLights();
2013     }
2014   viewer->Update();
2015 }
2016
2017 /*!
2018   \brief Store view parameters.
2019 */
2020 void OCCViewer_ViewWindow::onMemorizeView()
2021 {
2022   appendViewAspect( getViewParams() );
2023 }
2024
2025 /*!
2026   \brief Restore view parameters.
2027 */
2028 void OCCViewer_ViewWindow::onRestoreView()
2029 {
2030   OCCViewer_CreateRestoreViewDlg* aDlg = new OCCViewer_CreateRestoreViewDlg( centralWidget(), this );
2031   connect( aDlg, SIGNAL( dlgOk() ), this, SLOT( setRestoreFlag() ) );
2032   aDlg->exec();
2033   updateViewAspects( aDlg->parameters() );
2034   if( myRestoreFlag && aDlg->parameters().count() )
2035     performRestoring( aDlg->currentItem() );
2036 }
2037
2038 /*!
2039   \brief Restore view parameters.
2040   \param anItem view parameters
2041 */
2042 void OCCViewer_ViewWindow::performRestoring( const viewAspect& anItem, bool baseParamsOnly )
2043 {
2044   Handle(V3d_View) aView3d = myViewPort->getView();
2045
2046   Standard_Boolean prev = aView3d->SetImmediateUpdate( Standard_False );
2047   aView3d->SetScale( anItem.scale );
2048   aView3d->SetTwist( anItem.twist );
2049   aView3d->SetAt( anItem.atX, anItem.atY, anItem.atZ );
2050   aView3d->SetImmediateUpdate( prev );
2051   aView3d->SetEye( anItem.eyeX, anItem.eyeY, anItem.eyeZ );
2052   aView3d->SetProj( anItem.projX, anItem.projY, anItem.projZ );
2053   aView3d->SetAxialScale( anItem.scaleX, anItem.scaleY, anItem.scaleZ );
2054
2055   if ( anItem.centerX != 0.0 || anItem.centerY != 0.0 )
2056   {
2057     double anUpX = 0.0, anUpY = 0.0, anUpZ = 0.0;
2058
2059     // "eye" and "at" require conversion to represent center panning
2060     // up direction is only available after setting angle of twist and
2061     // other view parameters
2062     aView3d->Up( anUpX, anUpY, anUpZ );
2063
2064     gp_Dir aProj( -anItem.projX, -anItem.projY, -anItem.projZ );
2065     gp_Dir anUp( anUpX, anUpY, anUpZ );
2066     gp_Pnt anAt( anItem.atX, anItem.atY, anItem.atZ );
2067     gp_Pnt anEye( anItem.eyeX, anItem.eyeY, anItem.eyeZ );
2068     gp_Dir aSide = aProj ^ anUp;
2069
2070     anAt.Translate( gp_Vec( aSide ) * anItem.centerX );
2071     anAt.Translate( gp_Vec( anUp  ) * anItem.centerY );
2072
2073     aView3d->SetAt( anAt.X(), anAt.Y(), anAt.Z() );
2074     aView3d->SetProj( anItem.projX, anItem.projY, anItem.projZ );
2075   }
2076
2077   if ( !baseParamsOnly ) {
2078
2079     myModel->setTrihedronShown( anItem.isVisible );
2080     myModel->setTrihedronSize( anItem.size );
2081
2082     // graduated trihedron
2083     bool anIsVisible = anItem.gtIsVisible;
2084     OCCViewer_AxisWidget::AxisData anAxisData[3];
2085     anAxisData[0].DrawName = anItem.gtDrawNameX;
2086     anAxisData[1].DrawName = anItem.gtDrawNameZ;
2087     anAxisData[2].DrawName = anItem.gtDrawNameZ;
2088     anAxisData[0].Name = anItem.gtNameX;
2089     anAxisData[1].Name = anItem.gtNameZ;
2090     anAxisData[2].Name = anItem.gtNameZ;
2091     anAxisData[0].NameColor = QColor( anItem.gtNameColorRX,
2092               anItem.gtNameColorGX,
2093               anItem.gtNameColorBX );
2094     anAxisData[1].NameColor = QColor( anItem.gtNameColorRY,
2095               anItem.gtNameColorGY,
2096               anItem.gtNameColorBY );
2097     anAxisData[2].NameColor = QColor( anItem.gtNameColorRZ,
2098               anItem.gtNameColorGZ,
2099               anItem.gtNameColorBZ );
2100     anAxisData[0].DrawValues = anItem.gtDrawValuesX;
2101     anAxisData[1].DrawValues = anItem.gtDrawValuesY;
2102     anAxisData[2].DrawValues = anItem.gtDrawValuesZ;
2103     anAxisData[0].NbValues = anItem.gtNbValuesX;
2104     anAxisData[1].NbValues = anItem.gtNbValuesY;
2105     anAxisData[2].NbValues = anItem.gtNbValuesZ;
2106     anAxisData[0].Offset = anItem.gtOffsetX;
2107     anAxisData[1].Offset = anItem.gtOffsetY;
2108     anAxisData[2].Offset = anItem.gtOffsetZ;
2109     anAxisData[0].Color = QColor( anItem.gtColorRX,
2110           anItem.gtColorGX,
2111           anItem.gtColorBX );
2112     anAxisData[1].Color = QColor( anItem.gtColorRY,
2113           anItem.gtColorGY,
2114           anItem.gtColorBY );
2115     anAxisData[2].Color = QColor( anItem.gtColorRZ,
2116           anItem.gtColorGZ,
2117           anItem.gtColorBZ );
2118     anAxisData[0].DrawTickmarks = anItem.gtDrawTickmarksX;
2119     anAxisData[1].DrawTickmarks = anItem.gtDrawTickmarksY;
2120     anAxisData[2].DrawTickmarks = anItem.gtDrawTickmarksZ;
2121     anAxisData[0].TickmarkLength = anItem.gtTickmarkLengthX;
2122     anAxisData[1].TickmarkLength = anItem.gtTickmarkLengthY;
2123     anAxisData[2].TickmarkLength = anItem.gtTickmarkLengthZ;
2124
2125     myCubeAxesDlg->SetData( anIsVisible, anAxisData );
2126     myCubeAxesDlg->ApplyData( aView3d );
2127
2128   } // if ( !baseParamsOnly )
2129
2130   myRestoreFlag = 0;
2131 }
2132
2133 /*!
2134   \brief Set restore flag.
2135 */
2136 void OCCViewer_ViewWindow::setRestoreFlag()
2137 {
2138   myRestoreFlag = 1;
2139 }
2140
2141 /*!
2142   \brief Called when action "show/hide trihedron" is activated.
2143 */
2144 void OCCViewer_ViewWindow::onTrihedronShow(bool show)
2145 {
2146   myModel->setTrihedronShown(show);
2147 }
2148
2149 /*!
2150   \brief Toggles preselection (highlighting) on/off
2151 */
2152 void OCCViewer_ViewWindow::onSwitchPreselection( bool on )
2153 {
2154   myPreselectionEnabled = on;
2155   myModel->setSelectionOptions( isPreselectionEnabled(), myModel->isSelectionEnabled() );
2156
2157   // unhighlight all highlighted objects
2158   /*if ( !on ) {
2159     myModel->unHighlightAll( true, false );
2160   }*/
2161
2162   // update action state if method is called outside
2163   QtxAction* a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchPreselectionId ) );
2164   if ( a && a->isChecked() != on ) {
2165     a->setChecked( on );
2166   }
2167 }
2168
2169 /*!
2170   \brief Toggles selection on/off
2171 */
2172 void OCCViewer_ViewWindow::onSwitchSelection( bool on )
2173 {
2174   mySelectionEnabled = on;
2175   myModel->setSelectionOptions( myModel->isPreselectionEnabled(), isSelectionEnabled() );
2176
2177   // update action state if method is called outside
2178
2179   // preselection
2180   QtxAction* a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchPreselectionId ) );
2181   if ( a ) {
2182     a->setEnabled( on );
2183   }
2184
2185   // selection
2186   a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchSelectionId ) );
2187   if ( a && a->isChecked() != on ) {
2188     a->setChecked( on );
2189   }
2190 }
2191
2192 /*!
2193   \brief Switches "keyboard free" interaction style on/off
2194 */
2195 void OCCViewer_ViewWindow::onSwitchInteractionStyle( bool on )
2196 {
2197   myInteractionStyle = on ? (int)SUIT_ViewModel::KEY_FREE : (int)SUIT_ViewModel::STANDARD;
2198
2199   // update action state if method is called outside
2200   QtxAction* a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchInteractionStyleId ) );
2201   if ( a && a->isChecked() != on )
2202     a->setChecked( on );
2203 }
2204
2205 /*!
2206   \brief Toogles advanced zooming style (relatively to the cursor position) on/off
2207 */
2208 void OCCViewer_ViewWindow::onSwitchZoomingStyle( bool on )
2209 {
2210   myViewPort->setAdvancedZoomingEnabled( on );
2211
2212   // update action state if method is called outside
2213   QtxAction* a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchZoomingStyleId ) );
2214   if ( a && a->isChecked() != on )
2215     a->setChecked( on );
2216 }
2217
2218 /*!
2219   \brief Get current interaction style
2220   \return interaction style
2221 */
2222 int OCCViewer_ViewWindow::interactionStyle() const
2223 {
2224   return myInteractionStyle;
2225 }
2226
2227 /*!
2228   \brief Set current interaction style
2229   \param theStyle interaction style
2230 */
2231 void OCCViewer_ViewWindow::setInteractionStyle( const int theStyle )
2232 {
2233   onSwitchInteractionStyle( theStyle == (int)SUIT_ViewModel::KEY_FREE );
2234 }
2235
2236 /*!
2237   \brief Get current zooming style
2238   \return zooming style
2239 */
2240 int OCCViewer_ViewWindow::zoomingStyle() const
2241 {
2242   return myViewPort->isAdvancedZoomingEnabled() ? 1 : 0;
2243 }
2244
2245 /*!
2246   \brief Set current zooming style
2247   \param theStyle zooming style
2248 */
2249 void OCCViewer_ViewWindow::setZoomingStyle( const int theStyle )
2250 {
2251   onSwitchZoomingStyle( theStyle == 1 );
2252 }
2253
2254 /*!
2255   \brief Dump view window contents to the pixmap.
2256   \return pixmap containing all scene rendered in the window
2257 */
2258 //#define USE_OLD_IMPLEMENTATION
2259 QImage OCCViewer_ViewWindow::dumpView()
2260 {
2261   Handle(V3d_View) view = myViewPort->getView();
2262   if ( view.IsNull() )
2263     return QImage();
2264
2265   int aWidth = myViewPort->width();
2266   int aHeight = myViewPort->height();
2267
2268 #ifdef USE_OLD_IMPLEMENTATION
2269   // rnv: Old approach to dump the OCCViewer content via Frame Buffer Object
2270
2271   view->Redraw();
2272
2273 #ifndef DISABLE_GLVIEWER
2274   OpenGLUtils_FrameBuffer aFrameBuffer;
2275   if ( aFrameBuffer.init( aWidth, aHeight ) )
2276   {
2277     glPushAttrib( GL_VIEWPORT_BIT );
2278     glViewport( 0, 0, aWidth, aHeight );
2279     aFrameBuffer.bind();
2280
2281     // draw scene
2282     view->Redraw();
2283
2284     aFrameBuffer.unbind();
2285     glPopAttrib();
2286
2287     QImage anImage( aWidth, aHeight, QImage::Format_RGB32 );
2288
2289     aFrameBuffer.bind();
2290     glReadPixels( 0, 0, aWidth, aHeight, GL_RGBA, GL_UNSIGNED_BYTE, anImage.bits() );
2291     aFrameBuffer.unbind();
2292
2293     anImage = anImage.rgbSwapped();
2294     anImage = anImage.mirrored();
2295     return anImage;
2296   }
2297
2298   // if frame buffers are unsupported, use old approach
2299
2300   unsigned char* data = new unsigned char[ aWidth*aHeight*4 ];
2301   QPoint p = myViewPort->mapFromParent( myViewPort->geometry().topLeft() );
2302   glReadPixels( p.x(), p.y(), aWidth, aHeight, GL_RGBA, GL_UNSIGNED_BYTE,
2303                 data);
2304   QImage anImage( data, aWidth, aHeight, QImage::Format_ARGB32 );
2305   anImage = anImage.mirrored();
2306   anImage = anImage.rgbSwapped();
2307   return anImage;
2308
2309 #else // DISABLE_GLVIEWER
2310
2311   return QImage();
2312
2313 #endif // DISABLE_GLVIEWER
2314
2315 #else // USE_OLD_IMPLEMENTATION
2316   // rnv: New approach is to use OCCT built-in procedure
2317
2318   Image_PixMap aPix;
2319   view->ToPixMap(aPix, aWidth, aHeight, Graphic3d_BT_RGB);
2320   
2321   QImage anImage( aWidth, aHeight, QImage::Format_ARGB32 );
2322   for ( int i = 0; i < aWidth; i++ ) {
2323     for ( int j = 0; j < aHeight; j++ ) {
2324       Quantity_Color pixel = aPix.PixelColor( i, j ).GetRGB();
2325       QColor color = QColor::fromRgbF( pixel.Red(), pixel.Green(), pixel.Blue() );
2326       anImage.setPixelColor( i, j, color );
2327     }
2328   }
2329     
2330   if ( aPix.IsTopDown() )
2331     anImage = anImage.mirrored();
2332   return anImage;
2333
2334 #endif // USE_OLD_IMPLEMENTATION
2335 }
2336
2337 bool OCCViewer_ViewWindow::dumpViewToFormat( const QImage& img,
2338                                              const QString& fileName,
2339                                              const QString& format )
2340 {
2341   bool res = false;
2342   QApplication::setOverrideCursor( Qt::WaitCursor );
2343
2344   // OCCT version > 7.3.0 is not support export to PS and EPS
2345 #if OCC_VERSION_LARGE <= 0x07030000
2346   Handle(Graphic3d_CView) a3dView = myViewPort->getView()->View();
2347
2348   if (format == "PS") {
2349     Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(myViewPort->getViewer()->Driver());
2350     OpenGl_Caps* aCaps = &aDriver->ChangeOptions();
2351     int prev = aCaps->ffpEnable;
2352     aCaps->ffpEnable = 1;
2353     res = a3dView->Export(strdup(qUtf8Printable(fileName)), Graphic3d_EF_PostScript);
2354     aCaps->ffpEnable = prev;
2355   }
2356   else if (format == "EPS") {
2357     Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(myViewPort->getViewer()->Driver());
2358     OpenGl_Caps* aCaps = &aDriver->ChangeOptions();
2359     int prev = aCaps->ffpEnable;
2360     aCaps->ffpEnable = 1;
2361     res = a3dView->Export(strdup(qUtf8Printable(fileName)), Graphic3d_EF_EnhPostScript);
2362     aCaps->ffpEnable = prev;
2363   }
2364   else { 
2365 #endif
2366     res = myViewPort->getView()->Dump( fileName.toStdString().c_str() );
2367 #if OCC_VERSION_LARGE <= 0x07030000
2368   }
2369 #endif
2370
2371   QApplication::restoreOverrideCursor();
2372   return res;
2373 }
2374
2375
2376 QString OCCViewer_ViewWindow::filter() const
2377 {
2378 #if OCC_VERSION_LARGE <= 0x07030000
2379   return tr( "OCC_IMAGE_FILES" );
2380 #else
2381   QString formats = tr( "OCC_IMAGE_FILES" );
2382   return formats.remove(" *.eps *.ps");
2383 #endif
2384 }
2385
2386
2387 /*!
2388   \brief Set parameters of the cutting plane
2389   \param on if \c true, cutting plane is enabled
2390   \param x X position of plane point
2391   \param y Y position of plane point
2392   \param z Z position of plane point
2393   \param dx X coordinate of plane normal
2394   \param dy Y coordinate of plane normal
2395   \param dz Z coordinate of plane normal
2396 */
2397 void OCCViewer_ViewWindow::setCuttingPlane( bool on, const double x,  const double y,  const double z,
2398                                             const double dx, const double dy, const double dz )
2399 {
2400   Handle(V3d_View) view = myViewPort->getView();
2401   if ( view.IsNull() )
2402     return;
2403
2404   if ( on ) {
2405     Handle(V3d_Viewer) viewer = myViewPort->getViewer();
2406
2407     // try to use already existing plane or create a new one
2408     Handle(V3d_Plane) clipPlane;
2409
2410     // calculate new a,b,c,d values for the plane
2411     gp_Pln pln (gp_Pnt(x, y, z), gp_Dir(dx, dy, dz));
2412     double a, b, c, d;
2413     pln.Coefficients(a, b, c, d);
2414     Handle(Graphic3d_SequenceOfHClipPlane) aPlanes = view->ClipPlanes();
2415     Graphic3d_SequenceOfHClipPlane::Iterator anIter (*aPlanes);
2416     if(aPlanes->Size() > 0 ) {
2417       Handle(Graphic3d_ClipPlane) aClipPlane = anIter.Value();
2418       aClipPlane->SetEquation(pln);
2419       aClipPlane->SetOn(Standard_True);
2420     } else {
2421       view->AddClipPlane( myModel->createClipPlane( pln, Standard_True ) );
2422     }
2423   }
2424   else {
2425     Handle(Graphic3d_SequenceOfHClipPlane) aPlanes = view->ClipPlanes();
2426     Graphic3d_SequenceOfHClipPlane::Iterator anIter (*aPlanes);
2427     for( ;anIter.More();anIter.Next() ){
2428       Handle(Graphic3d_ClipPlane) aClipPlane = anIter.Value();
2429       aClipPlane->SetOn(Standard_False);
2430     }
2431   }
2432
2433   view->Update();
2434   view->Redraw();
2435 }
2436
2437 void OCCViewer_ViewWindow::setCuttingPlane( bool on, const gp_Pln pln )
2438 {
2439   gp_Dir aDir = pln.Axis().Direction();
2440   gp_Pnt aPnt = pln.Location();
2441   setCuttingPlane(on, aPnt.X(), aPnt.Y(), aPnt.Z(), aDir.X(), aDir.Y(), aDir.Z());
2442 }
2443
2444
2445 /*!
2446   \brief Check if any cutting plane is enabled
2447   \return \c true if at least one cutting plane is enabled
2448 */
2449 bool OCCViewer_ViewWindow::isCuttingPlane()
2450 {
2451   Handle(V3d_View) view = myViewPort->getView();
2452   bool res = false;
2453   Handle(Graphic3d_SequenceOfHClipPlane) aPlanes = view->ClipPlanes();
2454   Graphic3d_SequenceOfHClipPlane::Iterator anIter (*aPlanes);
2455   for( ;anIter.More();anIter.Next() ) {
2456     Handle(Graphic3d_ClipPlane) aClipPlane = anIter.Value();
2457     if(aClipPlane->IsOn()) {
2458       res = true;
2459       break;
2460     }
2461   }
2462   return res;
2463 }
2464
2465 /*!
2466   \brief Get the visual parameters of the view window.
2467   \return visual parameters of view window
2468 */
2469 viewAspect OCCViewer_ViewWindow::getViewParams() const
2470 {
2471   double projX, projY, projZ, twist;
2472   double atX, atY, atZ, eyeX, eyeY, eyeZ;
2473   double aScaleX, aScaleY, aScaleZ;
2474
2475   Handle(V3d_View) aView3d = myViewPort->getView();
2476
2477   aView3d->Proj( projX, projY, projZ );
2478   aView3d->At( atX, atY, atZ );
2479   aView3d->Eye( eyeX, eyeY, eyeZ );
2480   twist = aView3d->Twist();
2481
2482   aView3d->AxialScale(aScaleX,aScaleY,aScaleZ);
2483
2484   bool isShown = myModel->isTrihedronVisible();
2485   double size = myModel->trihedronSize();
2486
2487   QString aName = QTime::currentTime().toString() + QString::fromLatin1( " h:m:s" );
2488
2489   viewAspect params;
2490   params.scale    = aView3d->Scale();
2491   params.projX    = projX;
2492   params.projY    = projY;
2493   params.projZ    = projZ;
2494   params.twist    = twist;
2495   params.atX      = atX;
2496   params.atY      = atY;
2497   params.atZ      = atZ;
2498   params.eyeX     = eyeX;
2499   params.eyeY     = eyeY;
2500   params.eyeZ     = eyeZ;
2501   params.scaleX   = aScaleX;
2502   params.scaleY   = aScaleY;
2503   params.scaleZ   = aScaleZ;
2504   params.name     = aName;
2505   params.isVisible= isShown;
2506   params.size     = size;
2507
2508   // graduated trihedron
2509   bool anIsVisible = false;
2510   OCCViewer_AxisWidget::AxisData anAxisData[3];
2511   myCubeAxesDlg->GetData( anIsVisible, anAxisData );
2512
2513   params.gtIsVisible = anIsVisible;
2514   params.gtDrawNameX = anAxisData[0].DrawName;
2515   params.gtDrawNameY = anAxisData[1].DrawName;
2516   params.gtDrawNameZ = anAxisData[2].DrawName;
2517   params.gtNameX = anAxisData[0].Name;
2518   params.gtNameY = anAxisData[1].Name;
2519   params.gtNameZ = anAxisData[2].Name;
2520   params.gtNameColorRX = anAxisData[0].NameColor.red();
2521   params.gtNameColorGX = anAxisData[0].NameColor.green();
2522   params.gtNameColorBX = anAxisData[0].NameColor.blue();
2523   params.gtNameColorRY = anAxisData[1].NameColor.red();
2524   params.gtNameColorGY = anAxisData[1].NameColor.green();
2525   params.gtNameColorBY = anAxisData[1].NameColor.blue();
2526   params.gtNameColorRZ = anAxisData[2].NameColor.red();
2527   params.gtNameColorGZ = anAxisData[2].NameColor.green();
2528   params.gtNameColorBZ = anAxisData[2].NameColor.blue();
2529   params.gtDrawValuesX = anAxisData[0].DrawValues;
2530   params.gtDrawValuesY = anAxisData[1].DrawValues;
2531   params.gtDrawValuesZ = anAxisData[2].DrawValues;
2532   params.gtNbValuesX = anAxisData[0].NbValues;
2533   params.gtNbValuesY = anAxisData[1].NbValues;
2534   params.gtNbValuesZ = anAxisData[2].NbValues;
2535   params.gtOffsetX = anAxisData[0].Offset;
2536   params.gtOffsetY = anAxisData[1].Offset;
2537   params.gtOffsetZ = anAxisData[2].Offset;
2538   params.gtColorRX = anAxisData[0].Color.red();
2539   params.gtColorGX = anAxisData[0].Color.green();
2540   params.gtColorBX = anAxisData[0].Color.blue();
2541   params.gtColorRY = anAxisData[1].Color.red();
2542   params.gtColorGY = anAxisData[1].Color.green();
2543   params.gtColorBY = anAxisData[1].Color.blue();
2544   params.gtColorRZ = anAxisData[2].Color.red();
2545   params.gtColorGZ = anAxisData[2].Color.green();
2546   params.gtColorBZ = anAxisData[2].Color.blue();
2547   params.gtDrawTickmarksX = anAxisData[0].DrawTickmarks;
2548   params.gtDrawTickmarksY = anAxisData[1].DrawTickmarks;
2549   params.gtDrawTickmarksZ = anAxisData[2].DrawTickmarks;
2550   params.gtTickmarkLengthX = anAxisData[0].TickmarkLength;
2551   params.gtTickmarkLengthY = anAxisData[1].TickmarkLength;
2552   params.gtTickmarkLengthZ = anAxisData[2].TickmarkLength;
2553
2554   return params;
2555 }
2556
2557 /*!
2558   \brief Get visual parameters of this view window.
2559   \return visual parameters of view window
2560 */
2561 QString OCCViewer_ViewWindow::getVisualParameters()
2562 {
2563   viewAspect params = getViewParams();
2564
2565   QStringList data;
2566
2567   data << QString( "scale=%1" )    .arg( params.scale,   0, 'e', 12 );
2568   data << QString( "projX=%1" )    .arg( params.projX,   0, 'e', 12 );
2569   data << QString( "projY=%1" )    .arg( params.projY,   0, 'e', 12 );
2570   data << QString( "projZ=%1" )    .arg( params.projZ,   0, 'e', 12 );
2571   data << QString( "twist=%1" )    .arg( params.twist,   0, 'e', 12 );
2572   data << QString( "atX=%1" )      .arg( params.atX,     0, 'e', 12 );
2573   data << QString( "atY=%1" )      .arg( params.atY,     0, 'e', 12 );
2574   data << QString( "atZ=%1" )      .arg( params.atZ,     0, 'e', 12 );
2575   data << QString( "eyeX=%1" )     .arg( params.eyeX,    0, 'e', 12 );
2576   data << QString( "eyeY=%1" )     .arg( params.eyeY,    0, 'e', 12 );
2577   data << QString( "eyeZ=%1" )     .arg( params.eyeZ,    0, 'e', 12 );
2578   data << QString( "scaleX=%1" )   .arg( params.scaleX,  0, 'e', 12 );
2579   data << QString( "scaleY=%1" )   .arg( params.scaleY,  0, 'e', 12 );
2580   data << QString( "scaleZ=%1" )   .arg( params.scaleZ,  0, 'e', 12 );
2581   data << QString( "isVisible=%1" ).arg( params.isVisible );
2582   data << QString( "size=%1" )     .arg( params.size,    0, 'f',  2 );
2583
2584   ClipPlanesList aPlanes =  myModel->getClipPlanes();
2585   for ( int i=0; i < aPlanes.size(); i++ )
2586   {
2587     OCCViewer_ClipPlane& aPlane = aPlanes[i];
2588     QString ClippingPlane = QString( "ClippingPlane%1=").arg( i+1 );
2589     ClippingPlane +=  QString( "Mode~%1;").arg( (int)aPlane.Mode );
2590     ClippingPlane +=  QString( "IsActive~%1;").arg( aPlane.IsOn );
2591     switch ( aPlane.Mode )
2592     {
2593       case OCCViewer_ClipPlane::Absolute :
2594       {
2595         ClippingPlane += QString( "AbsoluteOrientation~%1;" ).arg( aPlane.OrientationType );
2596
2597         if ( aPlane.OrientationType == OCCViewer_ClipPlane::AbsoluteCustom )
2598         {
2599           ClippingPlane += QString( "Dx~%1;" ).arg( aPlane.AbsoluteOrientation.Dx );
2600           ClippingPlane += QString( "Dy~%1;" ).arg( aPlane.AbsoluteOrientation.Dy );
2601           ClippingPlane += QString( "Dz~%1;" ).arg( aPlane.AbsoluteOrientation.Dz );
2602         }
2603         else
2604         {
2605           ClippingPlane += QString( "IsInvert~%1;" ).arg( aPlane.AbsoluteOrientation.IsInvert );
2606         }
2607       }
2608       break;
2609
2610       case OCCViewer_ClipPlane::Relative :
2611       {
2612         ClippingPlane += QString( "RelativeOrientation~%1;" ).arg( aPlane.OrientationType );
2613         ClippingPlane += QString( "Rotation1~%1;" ).arg( aPlane.RelativeOrientation.Rotation1 );
2614         ClippingPlane += QString( "Rotation2~%1" ).arg( aPlane.RelativeOrientation.Rotation2 );
2615       }
2616       break;
2617     }
2618
2619     ClippingPlane +=  QString( "X~%1;" ).arg( aPlane.X );
2620     ClippingPlane +=  QString( "Y~%1;" ).arg( aPlane.Y );
2621     ClippingPlane +=  QString( "Z~%1;" ).arg( aPlane.Z );
2622     data << ClippingPlane;
2623   }
2624
2625   // graduated trihedron
2626   data << QString( "gtIsVisible=%1" )      .arg( params.gtIsVisible );
2627   data << QString( "gtDrawNameX=%1" )      .arg( params.gtDrawNameX );
2628   data << QString( "gtDrawNameY=%1" )      .arg( params.gtDrawNameY );
2629   data << QString( "gtDrawNameZ=%1" )      .arg( params.gtDrawNameZ );
2630   data << QString( "gtNameX=%1" )          .arg( params.gtNameX );
2631   data << QString( "gtNameY=%1" )          .arg( params.gtNameY );
2632   data << QString( "gtNameZ=%1" )          .arg( params.gtNameZ );
2633   data << QString( "gtNameColorRX=%1" )    .arg( params.gtNameColorRX );
2634   data << QString( "gtNameColorGX=%1" )    .arg( params.gtNameColorGX );
2635   data << QString( "gtNameColorBX=%1" )    .arg( params.gtNameColorBX );
2636   data << QString( "gtNameColorRY=%1" )    .arg( params.gtNameColorRY );
2637   data << QString( "gtNameColorGY=%1" )    .arg( params.gtNameColorGY );
2638   data << QString( "gtNameColorBY=%1" )    .arg( params.gtNameColorBY );
2639   data << QString( "gtNameColorRZ=%1" )    .arg( params.gtNameColorRZ );
2640   data << QString( "gtNameColorGZ=%1" )    .arg( params.gtNameColorGZ );
2641   data << QString( "gtNameColorBZ=%1" )    .arg( params.gtNameColorBZ );
2642   data << QString( "gtDrawValuesX=%1" )    .arg( params.gtDrawValuesX );
2643   data << QString( "gtDrawValuesY=%1" )    .arg( params.gtDrawValuesY );
2644   data << QString( "gtDrawValuesZ=%1" )    .arg( params.gtDrawValuesZ );
2645   data << QString( "gtNbValuesX=%1" )      .arg( params.gtNbValuesX );
2646   data << QString( "gtNbValuesY=%1" )      .arg( params.gtNbValuesY );
2647   data << QString( "gtNbValuesZ=%1" )      .arg( params.gtNbValuesZ );
2648   data << QString( "gtOffsetX=%1" )        .arg( params.gtOffsetX );
2649   data << QString( "gtOffsetY=%1" )        .arg( params.gtOffsetY );
2650   data << QString( "gtOffsetZ=%1" )        .arg( params.gtOffsetZ );
2651   data << QString( "gtColorRX=%1" )        .arg( params.gtColorRX );
2652   data << QString( "gtColorGX=%1" )        .arg( params.gtColorGX );
2653   data << QString( "gtColorBX=%1" )        .arg( params.gtColorBX );
2654   data << QString( "gtColorRY=%1" )        .arg( params.gtColorRY );
2655   data << QString( "gtColorGY=%1" )        .arg( params.gtColorGY );
2656   data << QString( "gtColorBY=%1" )        .arg( params.gtColorBY );
2657   data << QString( "gtColorRZ=%1" )        .arg( params.gtColorRZ );
2658   data << QString( "gtColorGZ=%1" )        .arg( params.gtColorGZ );
2659   data << QString( "gtColorBZ=%1" )        .arg( params.gtColorBZ );
2660   data << QString( "gtDrawTickmarksX=%1" ) .arg( params.gtDrawTickmarksX );
2661   data << QString( "gtDrawTickmarksY=%1" ) .arg( params.gtDrawTickmarksY );
2662   data << QString( "gtDrawTickmarksZ=%1" ) .arg( params.gtDrawTickmarksZ );
2663   data << QString( "gtTickmarkLengthX=%1" ).arg( params.gtTickmarkLengthX );
2664   data << QString( "gtTickmarkLengthY=%1" ).arg( params.gtTickmarkLengthY );
2665   data << QString( "gtTickmarkLengthZ=%1" ).arg( params.gtTickmarkLengthZ );
2666
2667   // ray tracing parameters
2668   Graphic3d_RenderingParams rendParams = this->getViewPort()->getView()->RenderingParams();
2669   if ( rendParams.Method == Graphic3d_RM_RAYTRACING ) {
2670     QString RayTracing = "rayTracing=";
2671     RayTracing += QString( "rtDepth~%1;" ).arg( rendParams.RaytracingDepth );
2672     RayTracing += QString( "rtReflection~%1;" ).arg( rendParams.IsReflectionEnabled );
2673     RayTracing += QString( "rtAntialiasing~%1;" ).arg( rendParams.IsAntialiasingEnabled );
2674     RayTracing += QString( "rtShadow~%1;" ).arg( rendParams.IsShadowEnabled );
2675     RayTracing += QString( "rtTransShadow~%1;" ).arg( rendParams.IsTransparentShadowEnabled );
2676     data << RayTracing;
2677   }
2678
2679   // environment texture parameters
2680   Handle(Graphic3d_TextureEnv) aTexture = this->getViewPort()->getView()->TextureEnv();
2681   if ( !aTexture.IsNull() ) {
2682     QString EnvTexture = "envTexture=";
2683     if ( aTexture->Name() == Graphic3d_NOT_ENV_UNKNOWN ) {
2684       TCollection_AsciiString aFileName;
2685       aTexture->Path().SystemName( aFileName );
2686       EnvTexture += QString( "etFile~%1;" ).arg( aFileName.ToCString() );
2687     }
2688     else
2689       EnvTexture += QString( "etNumber~%1;" ).arg( aTexture->Name() );
2690     data << EnvTexture;
2691   }
2692
2693   // light source parameters
2694   myModel->getViewer3d()->InitDefinedLights();
2695   while ( myModel->getViewer3d()->MoreDefinedLights() )
2696   {
2697     Handle(V3d_Light) aLight = myModel->getViewer3d()->DefinedLight();
2698     if ( aLight->Type() != V3d_AMBIENT ) {
2699       QString LightSource = QString( "lightSource=" );
2700       LightSource += QString( "lightType~%1;" ).arg( aLight->Type() );
2701       double aX, aY, aZ;
2702       if ( aLight->Type() == V3d_DIRECTIONAL )
2703         Handle(V3d_DirectionalLight)::DownCast( aLight )->Direction( aX, aY, aZ );
2704       else if ( aLight->Type() == V3d_POSITIONAL )
2705         Handle(V3d_PositionalLight)::DownCast( aLight )->Position( aX, aY, aZ );
2706       LightSource += QString( "lightX~%1;" ).arg( aX );
2707       LightSource += QString( "lightY~%1;" ).arg( aY );
2708       LightSource += QString( "lightZ~%1;" ).arg( aZ );
2709       LightSource += QString( "lightColorR~%1;" ).arg( aLight->Color().Red() );
2710       LightSource += QString( "lightColorG~%1;" ).arg( aLight->Color().Green() );
2711       LightSource += QString( "lightColorB~%1;" ).arg( aLight->Color().Blue() );
2712       LightSource += QString( "lightHeadlight~%1;" ).arg( aLight->Headlight() );
2713       data << LightSource;
2714     }
2715     myModel->getViewer3d()->NextDefinedLights();
2716   }
2717
2718   QString bg = Qtx::backgroundToString( background() ).replace( "=", "$" );
2719   data << QString( "background=%1" ).arg( bg );
2720
2721   return data.join("*");
2722 }
2723
2724 /*!
2725   \brief Restore visual parameters of the view window.
2726   \param parameters visual parameters of view window
2727 */
2728 void OCCViewer_ViewWindow::setVisualParameters( const QString& parameters )
2729 {
2730   viewAspect params;
2731   ClipPlanesList aClipPlanes;
2732   QStringList data = parameters.split( '*' );
2733   Qtx::BackgroundData bgData;
2734   if ( parameters.contains( '=' )  ) // new format - "scale=1.000e+00*centerX=0.000e+00..."
2735   {
2736     foreach( QString param, data ) {
2737       QString paramName  = param.section( '=', 0, 0 ).trimmed();
2738       QString paramValue = param.section( '=', 1, 1 ).trimmed();
2739       if      ( paramName == "scale" )             params.scale             = paramValue.toDouble();
2740       else if ( paramName == "centerX" )           params.centerX           = paramValue.toDouble();
2741       else if ( paramName == "centerY" )           params.centerY           = paramValue.toDouble();
2742       else if ( paramName == "projX" )             params.projX             = paramValue.toDouble();
2743       else if ( paramName == "projY" )             params.projY             = paramValue.toDouble();
2744       else if ( paramName == "projZ" )             params.projZ             = paramValue.toDouble();
2745       else if ( paramName == "twist" )             params.twist             = paramValue.toDouble();
2746       else if ( paramName == "atX" )               params.atX               = paramValue.toDouble();
2747       else if ( paramName == "atY" )               params.atY               = paramValue.toDouble();
2748       else if ( paramName == "atZ" )               params.atZ               = paramValue.toDouble();
2749       else if ( paramName == "eyeX" )              params.eyeX              = paramValue.toDouble();
2750       else if ( paramName == "eyeY" )              params.eyeY              = paramValue.toDouble();
2751       else if ( paramName == "eyeZ" )              params.eyeZ              = paramValue.toDouble();
2752       else if ( paramName == "scaleX" )            params.scaleX            = paramValue.toDouble();
2753       else if ( paramName == "scaleY" )            params.scaleY            = paramValue.toDouble();
2754       else if ( paramName == "scaleZ" )            params.scaleZ            = paramValue.toDouble();
2755       else if ( paramName == "isVisible" )         params.isVisible         = paramValue.toInt();
2756       else if ( paramName == "size" )              params.size              = paramValue.toDouble();
2757       else if ( paramName.contains( "ClippingPlane" ) )
2758       {
2759         QStringList ClipPlaneData = paramValue.split( ';' );
2760         OCCViewer_ClipPlane aPlane;
2761         foreach( QString ClipPlaneParam, ClipPlaneData )
2762         {
2763           QString ClipPlane_paramName  = ClipPlaneParam.section( '~', 0, 0 ).trimmed();
2764           QString ClipPlane_paramValue = ClipPlaneParam.section( '~', 1, 1 ).trimmed();
2765           if ( ClipPlane_paramName == "Mode" )
2766           {
2767             aPlane.Mode = ( OCCViewer_ClipPlane::PlaneMode ) ClipPlane_paramValue.toInt();
2768           }
2769           else if ( ClipPlane_paramName == "IsActive" ) aPlane.IsOn = ClipPlane_paramValue.toInt();
2770           else if ( ClipPlane_paramName == "X" )        aPlane.X    = ClipPlane_paramValue.toDouble();
2771           else if ( ClipPlane_paramName == "Y" )        aPlane.Y    = ClipPlane_paramValue.toDouble();
2772           else if ( ClipPlane_paramName == "Z" )        aPlane.Z    = ClipPlane_paramValue.toDouble();
2773           else
2774           {
2775             switch ( aPlane.Mode )
2776             {
2777               case OCCViewer_ClipPlane::Absolute :
2778                 if      ( ClipPlane_paramName == "Dx" ) aPlane.AbsoluteOrientation.Dx = ClipPlane_paramValue.toDouble();
2779                 else if ( ClipPlane_paramName == "Dy" ) aPlane.AbsoluteOrientation.Dy = ClipPlane_paramValue.toDouble();
2780                 else if ( ClipPlane_paramName == "Dz" ) aPlane.AbsoluteOrientation.Dz = ClipPlane_paramValue.toDouble();
2781                 else if ( ClipPlane_paramName == "IsInvert" ) aPlane.AbsoluteOrientation.IsInvert = ClipPlane_paramValue.toInt();
2782                 else if ( ClipPlane_paramName == "AbsoluteOrientation" ) aPlane.OrientationType = ClipPlane_paramValue.toInt();
2783                 break;
2784
2785               case OCCViewer_ClipPlane::Relative :
2786                 if      ( ClipPlane_paramName == "RelativeOrientation" ) aPlane.OrientationType = ClipPlane_paramValue.toInt();
2787                 else if ( ClipPlane_paramName == "Rotation1" )           aPlane.RelativeOrientation.Rotation1 = ClipPlane_paramValue.toDouble();
2788                 else if ( ClipPlane_paramName == "Rotation2" )           aPlane.RelativeOrientation.Rotation2 = ClipPlane_paramValue.toDouble();
2789                 break;
2790             }
2791           }
2792         }
2793         aClipPlanes.push_back(aPlane);
2794       }
2795       // graduated trihedron
2796       else if ( paramName == "gtIsVisible" )       params.gtIsVisible       = paramValue.toInt();
2797       else if ( paramName == "gtDrawNameX" )       params.gtDrawNameX       = paramValue.toInt();
2798       else if ( paramName == "gtDrawNameY" )       params.gtDrawNameY       = paramValue.toInt();
2799       else if ( paramName == "gtDrawNameZ" )       params.gtDrawNameZ       = paramValue.toInt();
2800       else if ( paramName == "gtNameX" )           params.gtNameX           = paramValue;
2801       else if ( paramName == "gtNameY" )           params.gtNameY           = paramValue;
2802       else if ( paramName == "gtNameZ" )           params.gtNameZ           = paramValue;
2803       else if ( paramName == "gtNameColorRX" )     params.gtNameColorRX     = paramValue.toInt();
2804       else if ( paramName == "gtNameColorGX" )     params.gtNameColorGX     = paramValue.toInt();
2805       else if ( paramName == "gtNameColorBX" )     params.gtNameColorBX     = paramValue.toInt();
2806       else if ( paramName == "gtNameColorRY" )     params.gtNameColorRY     = paramValue.toInt();
2807       else if ( paramName == "gtNameColorGY" )     params.gtNameColorGY     = paramValue.toInt();
2808       else if ( paramName == "gtNameColorBY" )     params.gtNameColorBY     = paramValue.toInt();
2809       else if ( paramName == "gtNameColorRZ" )     params.gtNameColorRZ     = paramValue.toInt();
2810       else if ( paramName == "gtNameColorGZ" )     params.gtNameColorGZ     = paramValue.toInt();
2811       else if ( paramName == "gtNameColorBZ" )     params.gtNameColorBZ     = paramValue.toInt();
2812       else if ( paramName == "gtDrawValuesX" )     params.gtDrawValuesX     = paramValue.toInt();
2813       else if ( paramName == "gtDrawValuesY" )     params.gtDrawValuesY     = paramValue.toInt();
2814       else if ( paramName == "gtDrawValuesZ" )     params.gtDrawValuesZ     = paramValue.toInt();
2815       else if ( paramName == "gtNbValuesX" )       params.gtNbValuesX       = paramValue.toInt();
2816       else if ( paramName == "gtNbValuesY" )       params.gtNbValuesY       = paramValue.toInt();
2817       else if ( paramName == "gtNbValuesZ" )       params.gtNbValuesZ       = paramValue.toInt();
2818       else if ( paramName == "gtOffsetX" )         params.gtOffsetX         = paramValue.toInt();
2819       else if ( paramName == "gtOffsetY" )         params.gtOffsetY         = paramValue.toInt();
2820       else if ( paramName == "gtOffsetZ" )         params.gtOffsetZ         = paramValue.toInt();
2821       else if ( paramName == "gtColorRX" )         params.gtColorRX         = paramValue.toInt();
2822       else if ( paramName == "gtColorGX" )         params.gtColorGX         = paramValue.toInt();
2823       else if ( paramName == "gtColorBX" )         params.gtColorBX         = paramValue.toInt();
2824       else if ( paramName == "gtColorRY" )         params.gtColorRY         = paramValue.toInt();
2825       else if ( paramName == "gtColorGY" )         params.gtColorGY         = paramValue.toInt();
2826       else if ( paramName == "gtColorBY" )         params.gtColorBY         = paramValue.toInt();
2827       else if ( paramName == "gtColorRZ" )         params.gtColorRZ         = paramValue.toInt();
2828       else if ( paramName == "gtColorGZ" )         params.gtColorGZ         = paramValue.toInt();
2829       else if ( paramName == "gtColorBZ" )         params.gtColorBZ         = paramValue.toInt();
2830       else if ( paramName == "gtDrawTickmarksX" )  params.gtDrawTickmarksX  = paramValue.toInt();
2831       else if ( paramName == "gtDrawTickmarksY" )  params.gtDrawTickmarksY  = paramValue.toInt();
2832       else if ( paramName == "gtDrawTickmarksZ" )  params.gtDrawTickmarksZ  = paramValue.toInt();
2833       else if ( paramName == "gtTickmarkLengthX" ) params.gtTickmarkLengthX = paramValue.toInt();
2834       else if ( paramName == "gtTickmarkLengthY" ) params.gtTickmarkLengthY = paramValue.toInt();
2835       else if ( paramName == "gtTickmarkLengthZ" ) params.gtTickmarkLengthZ = paramValue.toInt();
2836       else if ( paramName == "rayTracing" )
2837       {
2838         Graphic3d_RenderingParams& rendParams = this->getViewPort()->getView()->ChangeRenderingParams();
2839         rendParams.Method = Graphic3d_RM_RAYTRACING;
2840         QStringList rtData = paramValue.split( ';' );
2841         foreach( QString rtParam, rtData )
2842         {
2843           QString rt_paramName  = rtParam.section( '~', 0, 0 ).trimmed();
2844           QString rt_paramValue = rtParam.section( '~', 1, 1 ).trimmed();
2845           if ( rt_paramName == "rtDepth" ) rendParams.RaytracingDepth = rt_paramValue.toInt();
2846           else if ( rt_paramName == "rtReflection" ) rendParams.IsReflectionEnabled = rt_paramValue.toInt();
2847           else if ( rt_paramName == "rtAntialiasing" ) rendParams.IsAntialiasingEnabled = rt_paramValue.toInt();
2848           else if ( rt_paramName == "rtShadow" ) rendParams.IsShadowEnabled = rt_paramValue.toInt();
2849           else if ( rt_paramName == "rtTransShadow" ) rendParams.IsTransparentShadowEnabled = rt_paramValue.toInt();
2850         }
2851       }
2852       else if ( paramName == "envTexture" )
2853       {
2854         Handle(Graphic3d_TextureEnv) aTexture;
2855         QStringList etData = paramValue.split( ';' );
2856         foreach( QString etParam, etData )
2857         {
2858           QString et_paramName  = etParam.section( '~', 0, 0 ).trimmed();
2859           QString et_paramValue = etParam.section( '~', 1, 1 ).trimmed();
2860           if ( et_paramName == "etNumber" )
2861             aTexture = new Graphic3d_TextureEnv( Graphic3d_NameOfTextureEnv( et_paramValue.toInt() ) );
2862           else if ( et_paramName == "etFile" )
2863             aTexture = new Graphic3d_TextureEnv( TCollection_AsciiString( et_paramValue.toStdString().c_str() ) );
2864           Handle(V3d_View) aView = this->getViewPort()->getView();
2865           aView->SetTextureEnv( aTexture );
2866         }
2867       }
2868       else if ( paramName == "lightSource" )
2869       {
2870         myModel->getViewer3d()->InitDefinedLights();
2871         while ( myModel->getViewer3d()->MoreDefinedLights() )
2872         {
2873           Handle(V3d_Light) aLight = myModel->getViewer3d()->DefinedLight();
2874           if( aLight->Type() != V3d_AMBIENT ) {
2875             myModel->getViewer3d()->DelLight( aLight );
2876             myModel->getViewer3d()->InitDefinedLights();
2877           } else {
2878             myModel->getViewer3d()->NextDefinedLights();
2879           }
2880         }
2881         double aX, aY, aZ;
2882         double cR, cG, cB;
2883         V3d_TypeOfLight aType;
2884         bool isHeadlight;
2885         QStringList lsData = paramValue.split( ';' );
2886         foreach( QString lsParam, lsData )
2887         {
2888           QString ls_paramName  = lsParam.section( '~', 0, 0 ).trimmed();
2889           QString ls_paramValue = lsParam.section( '~', 1, 1 ).trimmed();
2890           if ( ls_paramName == "lightType" ) aType = V3d_TypeOfLight( ls_paramValue.toInt() );
2891           else if ( ls_paramName == "lightX" ) aX = ls_paramValue.toDouble();
2892           else if ( ls_paramName == "lightY" ) aY = ls_paramValue.toDouble();
2893           else if ( ls_paramName == "lightZ" ) aZ = ls_paramValue.toDouble();
2894           else if ( ls_paramName == "lightColorR" ) cR = ls_paramValue.toDouble();
2895           else if ( ls_paramName == "lightColorG" ) cG = ls_paramValue.toDouble();
2896           else if ( ls_paramName == "lightColorB" ) cB = ls_paramValue.toDouble();
2897           else if ( ls_paramName == "lightHeadlight" ) isHeadlight = ls_paramValue.toInt();
2898         }
2899         Quantity_Color aColor = Quantity_Color( cR, cG, cB, Quantity_TOC_RGB );
2900         if( aType == V3d_DIRECTIONAL ) {
2901           Handle(V3d_DirectionalLight) aLight = new V3d_DirectionalLight( myModel->getViewer3d() );
2902           aLight->SetDirection( aX, aY, aZ );
2903           aLight->SetColor( aColor );
2904           aLight->SetHeadlight( isHeadlight );
2905           myModel->getViewer3d()->SetLightOn( aLight );
2906         }
2907         else if( aType == V3d_POSITIONAL ) {
2908           Handle(V3d_PositionalLight) aLight = new V3d_PositionalLight( myModel->getViewer3d(), aX, aY, aZ, aColor.Name() );
2909           aLight->SetHeadlight( isHeadlight );
2910           myModel->getViewer3d()->SetLightOn( aLight );
2911         }
2912       }
2913       else if ( paramName == "background" )        {
2914   QString bg = paramValue.replace( "$", "=" );
2915   bgData = Qtx::stringToBackground( bg );
2916       }
2917     }
2918   }
2919   else // old format - "1.000e+00*0.000e+00..."
2920   {
2921     int idx = 0;
2922     params.scale     = data.count() > idx ? data[idx++].toDouble() : 1.0;
2923     params.centerX   = data.count() > idx ? data[idx++].toDouble() : 0.0;
2924     params.centerY   = data.count() > idx ? data[idx++].toDouble() : 0.0;
2925     params.projX     = data.count() > idx ? data[idx++].toDouble() : sqrt(1./3);
2926     params.projY     = data.count() > idx ? data[idx++].toDouble() : -sqrt(1./3);
2927     params.projZ     = data.count() > idx ? data[idx++].toDouble() : sqrt(1./3);
2928     params.twist     = data.count() > idx ? data[idx++].toDouble() : 0.0;
2929     params.atX       = data.count() > idx ? data[idx++].toDouble() : 0.0;
2930     params.atY       = data.count() > idx ? data[idx++].toDouble() : 0.0;
2931     params.atZ       = data.count() > idx ? data[idx++].toDouble() : 0.0;
2932     params.eyeX      = data.count() > idx ? data[idx++].toDouble() : sqrt(250000./3);
2933     params.eyeY      = data.count() > idx ? data[idx++].toDouble() : -sqrt(250000./3);
2934     params.eyeZ      = data.count() > idx ? data[idx++].toDouble() : sqrt(250000./3);
2935     params.scaleX    = data.count() > idx ? data[idx++].toDouble() : 1.0;
2936     params.scaleY    = data.count() > idx ? data[idx++].toDouble() : 1.0;
2937     params.scaleZ    = data.count() > idx ? data[idx++].toDouble() : 1.0;
2938     params.isVisible = data.count() > idx ? data[idx++].toInt()    : 1;
2939     params.size      = data.count() > idx ? data[idx++].toDouble() : 100.0;
2940   }
2941   performRestoring( params );
2942   setBackground( bgData );
2943   myModel->setClipPlanes(aClipPlanes);
2944 }
2945
2946 /*!
2947   \brief Handle show event.
2948
2949   Emits Show() signal.
2950
2951   \param theEvent show event
2952 */
2953 void OCCViewer_ViewWindow::showEvent( QShowEvent* theEvent )
2954 {
2955   emit Show( theEvent );
2956 }
2957
2958 /*!
2959   \brief Handle hide event.
2960
2961   Emits Hide() signal.
2962
2963   \param theEvent hide event
2964 */
2965 void OCCViewer_ViewWindow::hideEvent( QHideEvent* theEvent )
2966 {
2967   emit Hide( theEvent );
2968 }
2969
2970
2971 /*!
2972     Save old cursor. [ protected ]
2973 */
2974 void OCCViewer_ViewWindow::saveCursor()
2975 {
2976   QCursor* aCursor = NULL;
2977   if ( myViewPort )
2978     aCursor = myViewPort->getDefaultCursor();
2979   myCursor = ( aCursor ? *aCursor : cursor() );
2980 }
2981
2982
2983 /*!
2984     Creates default sketcher. [ virtual protected ]
2985 */
2986 OCCViewer_ViewSketcher* OCCViewer_ViewWindow::createSketcher( int type )
2987 {
2988   if ( type == Rect )
2989     return new OCCViewer_RectSketcher( this, type );
2990   if ( type == Polygon )
2991     return new OCCViewer_PolygonSketcher( this, type );
2992   return 0;
2993 }
2994
2995 void OCCViewer_ViewWindow::initSketchers()
2996 {
2997   if ( mySketchers.isEmpty() )
2998   {
2999     mySketchers.append( createSketcher( Rect ) );
3000     mySketchers.append( createSketcher( Polygon ) );
3001   }
3002 }
3003
3004 OCCViewer_ViewSketcher* OCCViewer_ViewWindow::getSketcher( const int typ )
3005 {
3006   OCCViewer_ViewSketcher* sketcher = 0;
3007   QList<OCCViewer_ViewSketcher*>::Iterator it;
3008   for ( it = mySketchers.begin(); it != mySketchers.end() && !sketcher; ++it )
3009   {
3010     OCCViewer_ViewSketcher* sk = (*it);
3011     if ( sk->type() == typ )
3012       sketcher = sk;
3013   }
3014   return sketcher;
3015 }
3016
3017 /*!
3018     Handles requests for sketching in the active view. [ virtual public ]
3019 */
3020 void OCCViewer_ViewWindow::activateSketching( int type )
3021 {
3022   OCCViewer_ViewPort3d* vp = getViewPort();
3023   if ( !vp )
3024     return;
3025
3026   if ( !vp->isSketchingEnabled() )
3027     return;
3028
3029   /* Finish current sketching */
3030   if ( type == NoSketching )
3031   {
3032     if ( mypSketcher )
3033     {
3034       onSketchingFinished();
3035       mypSketcher->deactivate();
3036       mypSketcher = 0;
3037     }
3038   }
3039   /* Activate new sketching */
3040   else
3041   {
3042     activateSketching( NoSketching );  /* concurrency not suported */
3043     mypSketcher = getSketcher( type );
3044     if ( mypSketcher )
3045     {
3046       mypSketcher->activate();
3047       onSketchingStarted();
3048     }
3049   }
3050 }
3051
3052 /*!
3053     Unhilights detected entities. [ virtual protected ]
3054 */
3055 void OCCViewer_ViewWindow::onSketchingStarted()
3056 {
3057 }
3058
3059 /*!
3060     Selection by rectangle or polygon. [ virtual protected ]
3061 */
3062 void OCCViewer_ViewWindow::onSketchingFinished()
3063 {
3064   MESSAGE("OCCViewer_ViewWindow::onSketchingFinished()")
3065   if ( mypSketcher && mypSketcher->result() == OCCViewer_ViewSketcher::Accept )
3066   {
3067     Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
3068     bool append = bool( mypSketcher->buttonState() && mypSketcher->isHasShift() );
3069     switch( mypSketcher->type() )
3070     {
3071     case Rect:
3072       {
3073         QRect* aRect = (QRect*)mypSketcher->data();
3074         if( aRect )
3075         {
3076           int aLeft = aRect->left();
3077           int aRight = aRect->right();
3078           int aTop = aRect->top();
3079           int aBottom = aRect->bottom();
3080 //           myRect = aRect;
3081
3082           if( append )
3083             ic->ShiftSelect( aLeft, aBottom, aRight, aTop, getViewPort()->getView(), Standard_False );
3084           else
3085             ic->Select( aLeft, aBottom, aRight, aTop, getViewPort()->getView(), Standard_False );
3086         }
3087       }
3088       break;
3089     case Polygon:
3090       {
3091         QPolygon* aPolygon = (QPolygon*)mypSketcher->data();
3092         if( aPolygon )
3093         {
3094           int size = aPolygon->size();
3095           TColgp_Array1OfPnt2d anArray( 1, size );
3096
3097           QPolygon::Iterator it = aPolygon->begin();
3098           QPolygon::Iterator itEnd = aPolygon->end();
3099           for( int index = 1; it != itEnd; ++it, index++ )
3100           {
3101             QPoint aPoint = *it;
3102             anArray.SetValue( index, gp_Pnt2d( aPoint.x(), aPoint.y() ) );
3103           }
3104
3105           if( append )
3106             ic->ShiftSelect( anArray, getViewPort()->getView(), Standard_False );
3107           else
3108             ic->Select( anArray, getViewPort()->getView(), Standard_False );
3109         }
3110       }
3111       break;
3112     default:
3113       break;
3114     }
3115
3116     OCCViewer_ViewManager* aViewMgr = ( OCCViewer_ViewManager* )getViewManager();
3117     aViewMgr->getOCCViewer()->performSelectionChanged();
3118   }
3119 }
3120
3121 OCCViewer_ViewPort3d* OCCViewer_ViewWindow::getViewPort()
3122 {
3123   return myViewPort;
3124 }
3125
3126 bool OCCViewer_ViewWindow::transformRequested() const
3127 {
3128   return ( myOperation != NOTHING );
3129 }
3130
3131 bool OCCViewer_ViewWindow::transformInProcess() const
3132 {
3133   return myEventStarted;
3134 }
3135
3136 void OCCViewer_ViewWindow::setTransformInProcess( bool bOn )
3137 {
3138   myEventStarted = bOn;
3139 }
3140
3141 /*!
3142   Set enabled state of transformation (rotate, zoom, etc)
3143 */
3144 void OCCViewer_ViewWindow::setTransformEnabled( const OperationType id, const bool on )
3145 {
3146   if ( id != NOTHING ) myStatus.insert( id, on );
3147 }
3148
3149 /*!
3150   \return enabled state of transformation (rotate, zoom, etc)
3151 */
3152 bool OCCViewer_ViewWindow::transformEnabled( const OperationType id ) const
3153 {
3154   return myStatus.contains( id ) ? myStatus[ id ] : true;
3155 }
3156
3157 void OCCViewer_ViewWindow::onMaximizedView()
3158 {
3159   setMaximized(!isMaximized());
3160 }
3161
3162 void OCCViewer_ViewWindow::returnTo3dView()
3163 {
3164   setReturnedTo3dView( true );
3165 }
3166
3167 void OCCViewer_ViewWindow::setReturnedTo3dView(bool isVisible3dView)
3168 {
3169   if ( !toolMgr()->action( ReturnTo3dViewId ) ||
3170     toolMgr()->isShown(ReturnTo3dViewId) != isVisible3dView ) return;
3171   if ( !isVisible3dView )
3172     toolMgr()->show( ReturnTo3dViewId );
3173   else
3174     toolMgr()->hide( ReturnTo3dViewId );
3175   if ( isVisible3dView ) emit returnedTo3d( );
3176 }
3177
3178
3179 void OCCViewer_ViewWindow::setMaximized(bool toMaximize, bool toSendSignal)
3180 {
3181   QAction* anAction =  toolMgr()->action( MaximizedId );
3182   QAction* anAction2 =  toolMgr()->action( ReturnTo3dViewId );
3183   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
3184   if ( toMaximize ) {
3185     anAction->setText( tr( "MNU_MINIMIZE_VIEW" ) );
3186     anAction->setToolTip( tr( "MNU_MINIMIZE_VIEW" ) );
3187     anAction->setIcon( aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_MINIMIZE" ) ) );
3188     anAction->setStatusTip( tr( "DSC_MINIMIZE_VIEW" ) );
3189     if ( anAction2 && my2dMode != No2dMode ) toolMgr()->show( ReturnTo3dViewId );
3190     if (toSendSignal) {
3191       emit maximized( this, true );
3192     }
3193   }
3194   else {
3195     anAction->setText( tr( "MNU_MAXIMIZE_VIEW" ) );
3196     anAction->setToolTip( tr( "MNU_MAXIMIZE_VIEW" ) );
3197     anAction->setIcon( aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_MAXIMIZE" ) ) );
3198     anAction->setStatusTip( tr( "DSC_MAXIMIZE_VIEW" ) );
3199     if ( anAction2 && my2dMode != No2dMode ) toolMgr()->hide( ReturnTo3dViewId );
3200     if (toSendSignal) {
3201       emit maximized( this, false );
3202     }
3203   }
3204 }
3205
3206 bool OCCViewer_ViewWindow::isMaximized() const
3207 {
3208   return !(toolMgr()->action( MaximizedId )->text() == tr( "MNU_MAXIMIZE_VIEW" ));
3209 }
3210
3211 void OCCViewer_ViewWindow::setSketcherStyle( bool enable )
3212 {
3213   IsSketcherStyle = enable;
3214 }
3215
3216 bool OCCViewer_ViewWindow::isSketcherStyle() const
3217 {
3218   return IsSketcherStyle;
3219 }
3220
3221
3222 void OCCViewer_ViewWindow::set2dMode(Mode2dType theType)
3223 {
3224   my2dMode = theType;
3225 }
3226
3227 int OCCViewer_ViewWindow::projectionType() const
3228 {
3229   int mode = Orthographic;
3230   Handle(V3d_View) aView3d = myViewPort->getView();
3231   if ( !aView3d.IsNull() ) {
3232     Handle(Graphic3d_Camera) aCamera = aView3d->Camera();
3233     if (aCamera->ProjectionType() == Graphic3d_Camera::Projection_Perspective)
3234       mode = Perspective;
3235     if (aCamera->ProjectionType() == Graphic3d_Camera::Projection_Orthographic)
3236       mode = Orthographic;
3237     if (aCamera->ProjectionType() == Graphic3d_Camera::Projection_Stereo)
3238       mode = Stereo;
3239   }
3240   return mode;
3241 }
3242
3243 void OCCViewer_ViewWindow::setStereoType( int type )
3244 {
3245   Handle(V3d_View) aView3d = myViewPort->getView();
3246   if ( !aView3d.IsNull() ) {
3247     Graphic3d_RenderingParams* aParams = &aView3d->ChangeRenderingParams();
3248     aParams->StereoMode = (Graphic3d_StereoMode)type;
3249   }
3250 }
3251
3252 int OCCViewer_ViewWindow::stereoType() const
3253 {
3254   int type = QuadBuffer;
3255   Handle(V3d_View) aView3d = myViewPort->getView();
3256   if ( !aView3d.IsNull() ) {
3257     Graphic3d_RenderingParams* aParams = &aView3d->ChangeRenderingParams();
3258     type = (OCCViewer_ViewWindow::StereoType)aParams->StereoMode;
3259   }
3260   return type;
3261 }
3262
3263 void OCCViewer_ViewWindow::setAnaglyphFilter( int type )
3264 {
3265   Handle(V3d_View) aView3d = myViewPort->getView();
3266   if ( !aView3d.IsNull() ) {
3267     Graphic3d_RenderingParams* aParams = &aView3d->ChangeRenderingParams();
3268     if (type == RedCyan)
3269       aParams->AnaglyphFilter = Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized;
3270     if (type == YellowBlue)
3271       aParams->AnaglyphFilter = Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized;
3272     if (type == GreenMagenta)
3273       aParams->AnaglyphFilter = Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple;
3274   }
3275 }
3276
3277 int OCCViewer_ViewWindow::anaglyphFilter() const
3278 {
3279   int type = RedCyan;
3280   Handle(V3d_View) aView3d = myViewPort->getView();
3281   if ( !aView3d.IsNull() ) {
3282     Graphic3d_RenderingParams* aParams = &aView3d->ChangeRenderingParams();
3283     if (aParams->AnaglyphFilter == Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized)
3284       type = RedCyan;
3285     if (aParams->AnaglyphFilter == Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized)
3286       type = YellowBlue;
3287     if (aParams->AnaglyphFilter == Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple)
3288       type = GreenMagenta;
3289   }
3290   return type;
3291 }
3292
3293 void OCCViewer_ViewWindow::setStereographicFocus( int type, double value )
3294 {
3295   Handle(V3d_View) aView3d = myViewPort->getView();
3296   if ( !aView3d.IsNull() ) {
3297     Handle(Graphic3d_Camera) aCamera = aView3d->Camera();
3298     aCamera->SetZFocus( (Graphic3d_Camera::FocusType) type, value );
3299   }
3300 }
3301
3302 int OCCViewer_ViewWindow::stereographicFocusType() const
3303 {
3304   int type = Relative;
3305   Handle(V3d_View) aView3d = myViewPort->getView();
3306   if ( !aView3d.IsNull() ) {
3307     Handle(Graphic3d_Camera) aCamera = aView3d->Camera();
3308     type = (OCCViewer_ViewWindow::FocusIODType)aCamera->ZFocusType();
3309   }
3310   return type;
3311 }
3312
3313 double OCCViewer_ViewWindow::stereographicFocusValue() const
3314 {
3315   double value = 1.0;
3316   Handle(V3d_View) aView3d = myViewPort->getView();
3317   if ( !aView3d.IsNull() ) {
3318     Handle(Graphic3d_Camera) aCamera = aView3d->Camera();
3319     value = aCamera->ZFocus();
3320   }
3321   return value;
3322 }
3323
3324 void OCCViewer_ViewWindow::setInterocularDistance( int type, double value )
3325 {
3326   Handle(V3d_View) aView3d = myViewPort->getView();
3327   if ( !aView3d.IsNull() ) {
3328     Handle(Graphic3d_Camera) aCamera = aView3d->Camera();
3329     aCamera->SetIOD( (Graphic3d_Camera::IODType) type, value );
3330   }
3331 }
3332
3333 int OCCViewer_ViewWindow::interocularDistanceType() const
3334 {
3335   int type = Relative;
3336   Handle(V3d_View) aView3d = myViewPort->getView();
3337   if ( !aView3d.IsNull() ) {
3338     Handle(Graphic3d_Camera) aCamera = aView3d->Camera();
3339     type = (OCCViewer_ViewWindow::FocusIODType)aCamera->GetIODType();
3340   }
3341   return type;
3342 }
3343
3344 double OCCViewer_ViewWindow::interocularDistanceValue() const
3345 {
3346   double value = 0.05;
3347   Handle(V3d_View) aView3d = myViewPort->getView();
3348   if ( !aView3d.IsNull() ) {
3349     Handle(Graphic3d_Camera) aCamera = aView3d->Camera();
3350     value = aCamera->IOD();
3351   }
3352   return value;
3353 }
3354
3355 void OCCViewer_ViewWindow::setReverseStereo( bool reverse )
3356 {
3357   Handle(V3d_View) aView3d = myViewPort->getView();
3358   if ( !aView3d.IsNull() ) {
3359     Graphic3d_RenderingParams* aParams = &aView3d->ChangeRenderingParams();
3360     aParams->ToReverseStereo = reverse;
3361   }
3362 }
3363
3364 bool OCCViewer_ViewWindow::isReverseStereo() const
3365 {
3366   int reverse = false;
3367   Handle(V3d_View) aView3d = myViewPort->getView();
3368   if ( !aView3d.IsNull() ) {
3369     Graphic3d_RenderingParams* aParams = &aView3d->ChangeRenderingParams();
3370     reverse = aParams->ToReverseStereo;
3371   }
3372   return reverse;
3373 }
3374
3375 void OCCViewer_ViewWindow::setVSync( bool enable )
3376 {
3377   Handle(AIS_InteractiveContext) anIntCont = myModel->getAISContext();
3378   if ( !anIntCont.IsNull() ) {
3379     Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(anIntCont->CurrentViewer()->Driver());
3380     OpenGl_Caps* aCaps = &aDriver->ChangeOptions();
3381     aCaps->swapInterval = enable;
3382   }
3383 }
3384
3385 bool OCCViewer_ViewWindow::isVSync() const
3386 {
3387   int enable = true;
3388   Handle(AIS_InteractiveContext) anIntCont = myModel->getAISContext();
3389   if ( !anIntCont.IsNull() ) {
3390     Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(anIntCont->CurrentViewer()->Driver());
3391     OpenGl_Caps* aCaps = &aDriver->ChangeOptions();
3392     enable = aCaps->swapInterval;
3393   }
3394   return enable;
3395 }
3396
3397 void OCCViewer_ViewWindow::setQuadBufferSupport( bool enable )
3398 {
3399   Handle(AIS_InteractiveContext) anIntCont = myModel->getAISContext();
3400   if ( !anIntCont.IsNull() ) {
3401     Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(anIntCont->CurrentViewer()->Driver());
3402     OpenGl_Caps* aCaps = &aDriver->ChangeOptions();
3403     aCaps->contextStereo = enable;
3404   }
3405 }
3406
3407 bool OCCViewer_ViewWindow::isQuadBufferSupport() const
3408 {
3409   int enable = true;
3410   Handle(AIS_InteractiveContext) anIntCont = myModel->getAISContext();
3411   if ( !anIntCont.IsNull() ) {
3412     Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(anIntCont->CurrentViewer()->Driver());
3413     OpenGl_Caps* aCaps = &aDriver->ChangeOptions();
3414     enable = aCaps->contextStereo;
3415   }
3416   return enable;
3417 }
3418
3419 bool OCCViewer_ViewWindow::isOpenGlStereoSupport() const
3420 {
3421   GLboolean support[1];
3422   glGetBooleanv (GL_STEREO, support);
3423   if ( support[0] )
3424     return true;
3425   return false;
3426 }
3427
3428 // obsolete
3429 QColor OCCViewer_ViewWindow::backgroundColor() const
3430 {
3431   return myViewPort ? myViewPort->backgroundColor() : Qt::black;
3432 }
3433
3434 // obsolete
3435 void OCCViewer_ViewWindow::setBackgroundColor( const QColor& theColor )
3436 {
3437   if ( myViewPort ) myViewPort->setBackgroundColor( theColor );
3438 }
3439
3440 Qtx::BackgroundData OCCViewer_ViewWindow::background() const
3441 {
3442   return myViewPort ? myViewPort->background() : Qtx::BackgroundData();
3443 }
3444
3445 void OCCViewer_ViewWindow::setBackground( const Qtx::BackgroundData& theBackground )
3446 {
3447   if ( myViewPort ) myViewPort->setBackground( theBackground );
3448 }
3449
3450 void OCCViewer_ViewWindow::showStaticTrihedron( bool on )
3451 {
3452   if ( myViewPort ) myViewPort->showStaticTrihedron( on );
3453 }
3454
3455 /*!
3456   Clears view aspects
3457 */
3458 void OCCViewer_ViewWindow::clearViewAspects()
3459 {
3460   myViewAspects.clear();
3461 }
3462
3463 /*!
3464   \return const reference to list of view aspects
3465 */
3466 const viewAspectList& OCCViewer_ViewWindow::getViewAspects()
3467 {
3468   return myViewAspects;
3469 }
3470
3471 /*!
3472   Appends new view aspect
3473   \param aParams - new view aspects
3474 */
3475 void OCCViewer_ViewWindow::appendViewAspect( const viewAspect& aParams )
3476 {
3477   myViewAspects.append( aParams );
3478 }
3479
3480 /*!
3481   Replaces old view aspects by new ones
3482   \param aViewList - list of new view aspects
3483 */
3484 void OCCViewer_ViewWindow::updateViewAspects( const viewAspectList& aViewList )
3485 {
3486   myViewAspects = aViewList;
3487 }
3488
3489 /*!
3490   Get camera properties for the OCC view window.
3491   \return shared pointer on camera properties.
3492 */
3493 SUIT_CameraProperties OCCViewer_ViewWindow::cameraProperties()
3494 {
3495   SUIT_CameraProperties aProps;
3496
3497   Handle(V3d_View) aSourceView = getViewPort()->getView();
3498   if ( aSourceView.IsNull() )
3499     return aProps;
3500
3501   if ( get2dMode() == No2dMode ) {
3502     aProps.setDimension( SUIT_CameraProperties::Dim3D );
3503   }
3504   else {
3505     aProps.setDimension( SUIT_CameraProperties::Dim2D );
3506     aProps.setViewSide( (SUIT_CameraProperties::ViewSide)(int)get2dMode() );
3507   }
3508
3509   // read common properites of the view
3510   Standard_Real anUp[3];
3511   Standard_Real anAt[3];
3512   Standard_Real anEye[3];
3513   Standard_Real aProj[3];
3514   Standard_Real anAxialScale[3];
3515
3516   aSourceView->Up( anUp[0], anUp[1], anUp[2] );
3517   aSourceView->At( anAt[0], anAt[1], anAt[2] );
3518   aSourceView->Proj( aProj[0], aProj[1], aProj[2] );
3519   getViewPort()->getAxialScale( anAxialScale[0], anAxialScale[1], anAxialScale[2] );
3520
3521   aProps.setAxialScale( anAxialScale[0], anAxialScale[1], anAxialScale[2] );
3522   aProps.setViewUp( anUp[0], anUp[1], anUp[2] );
3523
3524   aSourceView->Eye( anEye[0], anEye[1], anEye[2] );
3525
3526   // store camera properties "as is": it is up to synchronized
3527   // view classes to provide necessary property conversion.
3528   aProps.setPosition( anEye[0], anEye[1], anEye[2] );
3529   aProps.setFocalPoint( anAt[0], anAt[1], anAt[2] );
3530
3531   if ( aSourceView->Camera()->IsOrthographic() )
3532   {
3533     aProps.setProjection( SUIT_CameraProperties::PrjOrthogonal );
3534     aProps.setViewAngle( 0.0 );
3535   }
3536   else
3537   {
3538     aProps.setProjection( SUIT_CameraProperties::PrjPerspective );
3539     aProps.setViewAngle( aSourceView->Camera()->FOVy() );
3540   }
3541   aProps.setMappingScale( aSourceView->Camera()->Scale() );
3542
3543   return aProps;
3544 }
3545
3546 /*!
3547   Synchronize views.
3548   This implementation synchronizes OCC view's camera propreties.
3549 */
3550 void OCCViewer_ViewWindow::synchronize( SUIT_ViewWindow* theView )
3551 {
3552   bool blocked = blockSignals( true );
3553
3554   SUIT_CameraProperties aProps = theView->cameraProperties();
3555   if ( !cameraProperties().isCompatible( aProps ) ) {
3556     // other view, this one is being currently synchronized to, seems has become incompatible
3557     // we have to break synchronization
3558     updateSyncViews();
3559     return;
3560   }
3561
3562   Handle(V3d_View) aDestView = getViewPort()->getView();
3563
3564   aDestView->SetImmediateUpdate( Standard_False );
3565
3566   double anUpDir[3];
3567   double aPosition[3];
3568   double aFocalPoint[3];
3569   double anAxialScale[3];
3570
3571   // get common properties
3572   aProps.getFocalPoint( aFocalPoint[0], aFocalPoint[1], aFocalPoint[2] );
3573   aProps.getPosition( aPosition[0], aPosition[1], aPosition[2] );
3574   aProps.getViewUp( anUpDir[0], anUpDir[1], anUpDir[2] );
3575   aProps.getAxialScale( anAxialScale[0], anAxialScale[1], anAxialScale[2] );
3576
3577   aDestView->SetAt( aFocalPoint[0], aFocalPoint[1], aFocalPoint[2] );
3578   aDestView->SetEye( aPosition[0], aPosition[1], aPosition[2] );
3579   aDestView->SetUp( anUpDir[0], anUpDir[1], anUpDir[2] );
3580   aDestView->Camera()->SetScale( aProps.getMappingScale() );
3581
3582   getViewPort()->setAxialScale( anAxialScale[0], anAxialScale[1], anAxialScale[2] );
3583   aDestView->SetImmediateUpdate( Standard_True );
3584   aDestView->Redraw();
3585
3586   blockSignals( blocked );
3587 }
3588
3589 /*!
3590   \brief Indicates whether preselection is enabled
3591   \return true if preselection is enabled
3592 */
3593 bool OCCViewer_ViewWindow::isPreselectionEnabled() const
3594 {
3595   return myPreselectionEnabled;
3596 }
3597
3598 /*!
3599   \brief Enables/disables preselection
3600   \param theIsToEnable if true - preselection will be enabled
3601 */
3602 void OCCViewer_ViewWindow::enablePreselection( bool theIsToEnable )
3603 {
3604   onSwitchPreselection( theIsToEnable );
3605 }
3606
3607 /*!
3608   \brief Indicates whether selection is enabled
3609   \return true if selection is enabled
3610 */
3611 bool OCCViewer_ViewWindow::isSelectionEnabled() const
3612 {
3613   return mySelectionEnabled;
3614 }
3615
3616 /*!
3617   \brief Enables/disables selection
3618   \param theIsToEnable if true - selection will be enabled
3619 */
3620 void OCCViewer_ViewWindow::enableSelection( bool theIsToEnable )
3621 {
3622   onSwitchSelection( theIsToEnable );
3623 }
3624
3625
3626 /*!
3627   \brief called if clipping operation is activated / deactivated.
3628
3629   Enables/disables clipping plane displaying.
3630
3631   \parma on action state
3632 */
3633 void OCCViewer_ViewWindow::onClipping (bool theIsOn)
3634 {
3635   if(!myModel) return;
3636   OCCViewer_ClippingDlg* aClippingDlg = myModel->getClippingDlg();
3637
3638   if (theIsOn) {
3639     if (!aClippingDlg) {
3640       aClippingDlg = new OCCViewer_ClippingDlg (this, myModel);
3641       myModel->setClippingDlg(aClippingDlg);
3642     }
3643     if (!aClippingDlg->isVisible())
3644       aClippingDlg->show();
3645   } else {
3646     if ( aClippingDlg ) {
3647       aClippingDlg->close();
3648       myModel->setClippingDlg(0);
3649     }
3650   }
3651
3652   SUIT_ViewManager* mgr = getViewManager();
3653   if( mgr ) {
3654     QVector<SUIT_ViewWindow*> aViews = mgr->getViews();
3655     for(int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
3656       if(SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
3657         QtxActionToolMgr* mgr = aViewWindow->toolMgr();
3658         if(!mgr) continue;
3659         QAction* a = toolMgr()->action( ClippingId );
3660         if(!a) continue;
3661         if(theIsOn != a->isChecked()){
3662           disconnect (a, SIGNAL (toggled (bool)), aViewWindow, SLOT (onClipping (bool)));
3663           a->setChecked(theIsOn);
3664           connect (a, SIGNAL (toggled (bool)), aViewWindow, SLOT (onClipping (bool)));
3665         }
3666       }
3667     }
3668   }
3669 }
3670
3671 void OCCViewer_ViewWindow::onRayTracing()
3672 {
3673   if( !OCCViewer_Utilities::isDialogOpened( this, OCCViewer_RayTracingDlg::getName() ) ) {
3674     QDialog* aDlg = new OCCViewer_RayTracingDlg( this );
3675     if ( aDlg != NULL )
3676       aDlg->show();
3677   }
3678 }
3679
3680 void OCCViewer_ViewWindow::onEnvTexture()
3681 {
3682   if( !OCCViewer_Utilities::isDialogOpened( this, OCCViewer_EnvTextureDlg::getName() ) ) {
3683     QDialog* aDlg = new OCCViewer_EnvTextureDlg( this );
3684     if ( aDlg != NULL )
3685       aDlg->show();
3686   }
3687 }
3688
3689 void OCCViewer_ViewWindow::onLightSource()
3690 {
3691   if( !OCCViewer_Utilities::isDialogOpened( this, OCCViewer_LightSourceDlg::getName() ) ) {
3692     QDialog* aDlg = new OCCViewer_LightSourceDlg( this, myModel );
3693     if ( aDlg != NULL )
3694       aDlg->show();
3695   }
3696 }
3697
3698 bool OCCViewer_ViewWindow::isActionVisible( ActionId theId ) const
3699 {
3700   QAction* a = toolMgr()->action( theId );
3701   return a && a->isVisible();
3702 }
3703
3704 void OCCViewer_ViewWindow::setActionVisible( ActionId theId, bool isVisible )
3705 {
3706   QAction* a = toolMgr()->action( theId );
3707   if( a )
3708     a->setVisible( isVisible );
3709 }
3710
3711 void OCCViewer_ViewWindow::projAndPanToGravity(V3d_TypeOfOrientation CamOri)
3712 {
3713   const bool USE_XY = true;
3714
3715   Handle(V3d_View) aView3d = myViewPort->getView();
3716   if (aView3d.IsNull())
3717     return;
3718
3719   bool IsGr = false;
3720   double X = 0, Y = 0, Z = 0;
3721   if( USE_XY )
3722   {
3723     const double EPS = 1E-6;
3724     int xp = myViewPort->width()/2, yp = myViewPort->height()/2, xp1, yp1;
3725     aView3d->Convert( xp, yp, X, Y, Z );
3726
3727     gp_Dir d = aView3d->Camera()->Direction();
3728     if( fabs( d.Z() ) > EPS )
3729     {
3730       double t = -Z/d.Z();
3731       X += t*d.X();
3732       Y += t*d.Y();
3733       Z += t*d.Z();
3734     }
3735   }
3736
3737   // It is really necessary to compute gravity center even if it is not used in part of code below.
3738   // Without this calculation the SetProj() method and other methods are not correct.
3739   double X2, Y2, Z2;
3740   IsGr = computeGravityCenter(X2, Y2, Z2);
3741   if ( !IsGr )
3742     IsGr = OCCViewer_Utilities::computeSceneBBCenter(aView3d, X2, Y2, Z2);
3743
3744   aView3d->SetProj(CamOri);
3745   if (IsGr)
3746   {
3747     //aView3d->Update();
3748     Handle(Graphic3d_Camera) Cam = aView3d->Camera();
3749     gp_XYZ gp(X, Y, Z);
3750     gp_Vec dir (Cam->Direction());
3751     gp_Pnt eye = Cam->Eye();
3752     gp_Vec V1(eye, gp);
3753     Standard_Real D = dir.Dot(V1);
3754     gp_Pnt ppdir = eye.Translated(D*dir);
3755     gp_Vec V2(ppdir, gp);
3756     gp_XYZ trEye = eye.XYZ() + V2.XYZ();
3757
3758     double xat, yat, zat;
3759     aView3d->At(xat, yat, zat);
3760     gp_Pnt At(xat, yat, zat);
3761     gp_XYZ trAt = At.XYZ() + V2.XYZ();
3762     aView3d->SetEye(trEye.X(), trEye.Y(), trEye.Z());
3763     aView3d->SetAt(trAt.X(), trAt.Y(), trAt.Z());
3764   }
3765 }
3766
3767
3768 bool OCCViewer_ViewWindow::isAutomaticZoom() const
3769 {
3770   return myAutomaticZoom;
3771 }
3772
3773 void OCCViewer_ViewWindow::setAutomaticZoom(const bool isOn)
3774 {
3775   myAutomaticZoom = isOn;
3776 }
3777