Salome HOME
Fix for the "0052369: TC7.4.0: Visualization problem of default torus
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewWindow.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : OCCViewer_ViewWindow.cxx
24 // Author :
25
26 #include "OCCViewer_ViewWindow.h"
27 #include "OCCViewer_ViewModel.h"
28 #include "OCCViewer_ViewPort3d.h"
29 #include "OCCViewer_ViewManager.h"
30 #include "OCCViewer_ViewSketcher.h"
31 #include "OCCViewer_CreateRestoreViewDlg.h"
32 #include "OCCViewer_ClipPlane.h"
33 #include "OCCViewer_SetRotationPointDlg.h"
34 #include "OCCViewer_AxialScaleDlg.h"
35 #include "OCCViewer_CubeAxesDlg.h"
36 #include "OCCViewer_ClippingDlg.h"
37
38 #include <Basics_OCCTVersion.hxx>
39
40 #include <SUIT_Desktop.h>
41 #include <SUIT_Session.h>
42 #include <SUIT_ViewManager.h>
43 #include <SUIT_Tools.h>
44 #include <SUIT_ResourceMgr.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_Application.h>
47
48 #include <QtxActionToolMgr.h>
49 #include <QtxMultiAction.h>
50 #include <QtxRubberBand.h>
51
52 #include <OpenGLUtils_FrameBuffer.h>
53
54 #include <QPainter>
55 #include <QTime>
56 #include <QImage>
57 #include <QKeyEvent>
58 #include <QMouseEvent>
59 #include <QApplication>
60 #include <QMenu>
61
62 #include <AIS_ListOfInteractive.hxx>
63 #include <AIS_ListIteratorOfListOfInteractive.hxx>
64 #include <AIS_Shape.hxx>
65
66 #include <BRep_Tool.hxx>
67 #include <BRepBndLib.hxx>
68 #include <BRepGProp.hxx>
69 #include <GProp_GProps.hxx>
70 #include <TopoDS.hxx>
71
72 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
73 #include <Graphic3d_MapOfStructure.hxx>
74 #include <Graphic3d_Structure.hxx>
75 #include <Graphic3d_ExportFormat.hxx>
76
77 #include <Visual3d_View.hxx>
78 #include <V3d_Plane.hxx>
79 #include <V3d_Light.hxx>
80
81 #include <gp_Dir.hxx>
82 #include <gp_Pln.hxx>
83 #include <gp_GTrsf.hxx>
84 #include <TColgp_Array1OfPnt2d.hxx>
85
86 #if OCC_VERSION_LARGE > 0x06060000 
87 #include <Graphic3d_SequenceOfHClipPlane.hxx>
88 #include <Graphic3d_ClipPlane.hxx>
89
90 #endif
91
92 #include <Standard_Version.hxx>
93
94 #include "utilities.h"
95
96 // // OpenCV includes
97 // #include <cv.h>
98 // #include <highgui.h>
99
100 static QEvent* l_mbPressEvent = 0;
101
102 #ifdef WIN32
103 # include <QWindowsStyle>
104 #endif
105
106 #include <GL/gl.h>
107
108 const char* imageZoomCursor[] = {
109 "32 32 3 1",
110 ". c None",
111 "a c #000000",
112 "# c #ffffff",
113 "................................",
114 "................................",
115 ".#######........................",
116 "..aaaaaaa.......................",
117 "................................",
118 ".............#####..............",
119 "...........##.aaaa##............",
120 "..........#.aa.....a#...........",
121 ".........#.a.........#..........",
122 ".........#a..........#a.........",
123 "........#.a...........#.........",
124 "........#a............#a........",
125 "........#a............#a........",
126 "........#a............#a........",
127 "........#a............#a........",
128 ".........#...........#.a........",
129 ".........#a..........#a.........",
130 ".........##.........#.a.........",
131 "........#####.....##.a..........",
132 ".......###aaa#####.aa...........",
133 "......###aa...aaaaa.......#.....",
134 ".....###aa................#a....",
135 "....###aa.................#a....",
136 "...###aa...............#######..",
137 "....#aa.................aa#aaaa.",
138 ".....a....................#a....",
139 "..........................#a....",
140 "...........................a....",
141 "................................",
142 "................................",
143 "................................",
144 "................................"};
145
146 const char* imageRotateCursor[] = {
147 "32 32 3 1",
148 ". c None",
149 "a c #000000",
150 "# c #ffffff",
151 "................................",
152 "................................",
153 "................................",
154 "................................",
155 "........#.......................",
156 ".......#.a......................",
157 "......#######...................",
158 ".......#aaaaa#####..............",
159 "........#..##.a#aa##........##..",
160 ".........a#.aa..#..a#.....##.aa.",
161 ".........#.a.....#...#..##.aa...",
162 ".........#a.......#..###.aa.....",
163 "........#.a.......#a..#aa.......",
164 "........#a.........#..#a........",
165 "........#a.........#a.#a........",
166 "........#a.........#a.#a........",
167 "........#a.........#a.#a........",
168 ".........#.........#a#.a........",
169 "........##a........#a#a.........",
170 "......##.a#.......#.#.a.........",
171 "....##.aa..##.....##.a..........",
172 "..##.aa.....a#####.aa...........",
173 "...aa.........aaa#a.............",
174 "................#.a.............",
175 "...............#.a..............",
176 "..............#.a...............",
177 "...............a................",
178 "................................",
179 "................................",
180 "................................",
181 "................................",
182 "................................"};
183
184 const char* imageCrossCursor[] = {
185   "32 32 3 1",
186   ". c None",
187   "a c #000000",
188   "# c #ffffff",
189   "................................",
190   "................................",
191   "................................",
192   "................................",
193   "................................",
194   "................................",
195   "................................",
196   "...............#................",
197   "...............#a...............",
198   "...............#a...............",
199   "...............#a...............",
200   "...............#a...............",
201   "...............#a...............",
202   "...............#a...............",
203   "...............#a...............",
204   ".......#################........",
205   "........aaaaaaa#aaaaaaaaa.......",
206   "...............#a...............",
207   "...............#a...............",
208   "...............#a...............",
209   "...............#a...............",
210   "...............#a...............",
211   "...............#a...............",
212   "...............#a...............",
213   "................a...............",
214   "................................",
215   "................................",
216   "................................",
217   "................................",
218   "................................",
219   "................................",
220   "................................"};
221
222
223 /*!
224   \brief Constructor
225   \param theDesktop main window of application
226   \param theModel OCC 3D viewer
227 */
228 OCCViewer_ViewWindow::OCCViewer_ViewWindow( SUIT_Desktop*     theDesktop,
229                                             OCCViewer_Viewer* theModel )
230 : SUIT_ViewWindow( theDesktop )
231 {
232   myModel = theModel;
233   myRestoreFlag = 0;
234   myEnableDrawMode = false;
235   myDrawRect=false;
236   updateEnabledDrawMode();
237   myScalingDlg = 0;
238   mySetRotationPointDlg = 0;
239   myRectBand = 0;
240   
241   IsSketcherStyle = false;
242   myIsKeyFree = false;
243
244   mypSketcher = 0;
245   myCurSketch = -1;
246   my2dMode = No2dMode;
247
248   myInteractionStyle = SUIT_ViewModel::STANDARD;
249   myPreselectionEnabled = true;
250   mySelectionEnabled = true;
251
252
253   clearViewAspects();
254   
255 }
256
257 /*!
258   \brief Destructor.
259 */
260 OCCViewer_ViewWindow::~OCCViewer_ViewWindow()
261 {
262   endDrawRect();
263   qDeleteAll( mySketchers );
264 }
265
266 /*!
267   \brief Internal initialization.
268 */
269 void OCCViewer_ViewWindow::initLayout()
270 {
271   myViewPort = new OCCViewer_ViewPort3d( this, myModel->getViewer3d(), V3d_ORTHOGRAPHIC );
272   myViewPort->installEventFilter(this);
273   setCentralWidget(myViewPort);
274   myOperation = NOTHING;
275
276   myCurrPointType = GRAVITY;
277   myPrevPointType = GRAVITY;
278   mySelectedPoint = gp_Pnt(0.,0.,0.);
279   myRotationPointSelection = false;
280
281   setTransformRequested ( NOTHING );
282   setTransformInProcess ( false );
283
284   createActions();
285   createToolBar();
286
287   switch (my2dMode) {
288   case XYPlane:
289     onTopView();
290     break;
291   case XZPlane:
292     onLeftView();
293     break;
294   case YZPlane:
295     onFrontView();
296     break;
297   }
298
299   // Graduated axes dialog
300   QtxAction* anAction = dynamic_cast<QtxAction*>( toolMgr()->action( GraduatedAxesId ) );
301   myCubeAxesDlg = new OCCViewer_CubeAxesDlg( anAction, this, "OCCViewer_CubeAxesDlg" );
302   myCubeAxesDlg->initialize();
303   
304   connect( myViewPort, SIGNAL( vpTransformed( OCCViewer_ViewPort* ) ), this, SLOT( emitViewModified() ) );
305 }
306
307 OCCViewer_ViewWindow* OCCViewer_ViewWindow::getView( const int mode ) const
308 {
309   return mode == get2dMode() ? const_cast<OCCViewer_ViewWindow*>( this ) : 0;
310 }
311
312 /*!
313   \brief Detect viewer operation according the the mouse button pressed
314   and key modifiers used.
315   \param theEvent mouse event
316   \return type of the operation
317 */
318 OCCViewer_ViewWindow::OperationType
319 OCCViewer_ViewWindow::getButtonState( QMouseEvent* theEvent, int theInteractionStyle )
320 {
321   OperationType aOp = NOTHING;
322   SUIT_ViewModel::InteractionStyle aStyle = (SUIT_ViewModel::InteractionStyle)theInteractionStyle;
323   if( (theEvent->modifiers() == SUIT_ViewModel::myStateMap[aStyle][SUIT_ViewModel::ZOOM]) &&
324       (theEvent->buttons() == SUIT_ViewModel::myButtonMap[aStyle][SUIT_ViewModel::ZOOM]) )
325     aOp = ZOOMVIEW;
326   else if( (theEvent->modifiers() == SUIT_ViewModel::myStateMap[aStyle][SUIT_ViewModel::PAN]) &&
327            (theEvent->buttons() == SUIT_ViewModel::myButtonMap[aStyle][SUIT_ViewModel::PAN]) )
328     aOp = PANVIEW;
329   else if( (theEvent->modifiers()  == SUIT_ViewModel::myStateMap[aStyle][SUIT_ViewModel::ROTATE]) &&
330            (theEvent->buttons() == SUIT_ViewModel::myButtonMap[aStyle][SUIT_ViewModel::ROTATE]) &&
331            (my2dMode == No2dMode))
332     aOp = ROTATE;
333
334   return aOp;
335 }
336
337 /*!
338   \brief Customize event handling
339   \param watched event receiver object
340   \param e event
341   \return \c true if the event processing should be stopped
342 */
343 bool OCCViewer_ViewWindow::eventFilter( QObject* watched, QEvent* e )
344 {
345   if ( watched == myViewPort ) {
346     int aType = e->type();
347     switch(aType) {
348     case QEvent::MouseButtonPress:
349       vpMousePressEvent((QMouseEvent*) e);
350       return true;
351
352     case QEvent::MouseButtonRelease:
353       vpMouseReleaseEvent((QMouseEvent*) e);
354       return true;
355
356     case QEvent::MouseMove:
357       vpMouseMoveEvent((QMouseEvent*) e);
358       return true;
359
360     case QEvent::MouseButtonDblClick:
361       emit mouseDoubleClicked(this, (QMouseEvent*)e);
362       return true;
363
364     case QEvent::Wheel:
365       {
366         QWheelEvent* aEvent = (QWheelEvent*) e;
367   myViewPort->startZoomAtPoint( aEvent->x(), aEvent->y() );
368   double delta = (double)( aEvent->delta() ) / ( 15 * 8 );
369   int x  = aEvent->x();
370   int y  = aEvent->y();
371   int x1 = (int)( aEvent->x() + width()*delta/100 );
372   int y1 = (int)( aEvent->y() + height()*delta/100 );
373   myViewPort->zoom( x, y, x1, y1 );
374   myViewPort->getView()->ZFitAll();
375       }
376       return true;
377
378     case QEvent::ContextMenu:
379       {
380         QContextMenuEvent * aEvent = (QContextMenuEvent*)e;
381         if ( aEvent->reason() != QContextMenuEvent::Mouse )
382           emit contextMenuRequested( aEvent );
383       }
384       return true;
385
386     case QEvent::KeyPress:
387       emit keyPressed(this, (QKeyEvent*) e);
388       return true;
389
390     default:
391       break;
392     }
393   }
394   return SUIT_ViewWindow::eventFilter(watched, e);
395 }
396
397 /*!
398   \brief Update state of enable draw mode state.
399 */
400 void OCCViewer_ViewWindow::updateEnabledDrawMode()
401 {
402   if ( myModel )
403     myEnableDrawMode = myModel->isSelectionEnabled() && myModel->isMultiSelectionEnabled();
404 }
405
406 /*!
407   \brief Handle mouse press event
408   \param theEvent mouse event
409 */
410 void OCCViewer_ViewWindow::vpMousePressEvent( QMouseEvent* theEvent )
411 {
412   myStartX = theEvent->x();
413   myStartY = theEvent->y();
414   int anInteractionStyle = interactionStyle();
415
416   // in "key free" interaction style zoom operation is activated by two buttons (simultaneously pressed),
417   // which are assigned for pan and rotate - these operations are activated immediately after pressing 
418   // of the first button, so it is necessary to switch to zoom when the second button is pressed
419   bool aSwitchToZoom = false;
420   if ( anInteractionStyle == SUIT_ViewModel::KEY_FREE && 
421        ( myOperation == PANVIEW || myOperation == ROTATE ) ) {
422     aSwitchToZoom = getButtonState( theEvent, anInteractionStyle ) == ZOOMVIEW;
423   }
424
425   switch ( myOperation ) {
426   case WINDOWFIT:
427     if ( theEvent->button() == Qt::LeftButton )
428       emit vpTransformationStarted ( WINDOWFIT );
429     break;
430
431   case PANGLOBAL:
432     if ( theEvent->button() == Qt::LeftButton )
433       emit vpTransformationStarted ( PANGLOBAL );
434     break;
435
436   case ZOOMVIEW:
437     if ( theEvent->button() == Qt::LeftButton ) {
438       myViewPort->startZoomAtPoint( myStartX, myStartY );
439       emit vpTransformationStarted ( ZOOMVIEW );
440     }
441     break;
442
443   case PANVIEW:
444     if ( aSwitchToZoom ) {
445       myViewPort->startZoomAtPoint( myStartX, myStartY );
446       activateZoom();
447     }
448     else if ( theEvent->button() == Qt::LeftButton )
449       emit vpTransformationStarted ( PANVIEW );
450     break;
451
452   case ROTATE:
453     if ( aSwitchToZoom ) {
454       myViewPort->startZoomAtPoint( myStartX, myStartY );
455       activateZoom();
456     }
457     else if ( theEvent->button() == Qt::LeftButton ) {
458       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
459       emit vpTransformationStarted ( ROTATE );
460     }
461     break;
462
463   default:
464   /*  Try to activate a transformation */
465     OperationType aState;
466     if ( interactionStyle() == SUIT_ViewModel::STANDARD )
467       aState = getButtonState(theEvent, anInteractionStyle);
468     else {
469       aState = OCCViewer_ViewWindow::NOTHING;
470       myIsKeyFree = true;
471     }
472     switch ( aState ) {
473     case ZOOMVIEW:
474       myViewPort->startZoomAtPoint( myStartX, myStartY );
475       activateZoom();
476       break;
477     case PANVIEW:
478       activatePanning();
479       break;
480     case ROTATE:
481       activateRotation();
482       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
483       break;
484     default:
485       if ( myRotationPointSelection )
486       {
487         if ( theEvent->button() == Qt::LeftButton )
488         {
489           Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
490           ic->Select();
491           for ( ic->InitSelected(); ic->MoreSelected(); ic->NextSelected() )
492           {
493             TopoDS_Shape aShape = ic->SelectedShape();
494             GProp_GProps aSystem;
495             gp_Pnt aPnt;
496             if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
497             {
498               aPnt = BRep_Tool::Pnt( TopoDS::Vertex( aShape ) );
499             }
500             else if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE )
501             {
502               BRepGProp::LinearProperties( aShape, aSystem );
503               aPnt = aSystem.CentreOfMass();
504             }
505             else if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE )
506             {
507               BRepGProp::SurfaceProperties( aShape, aSystem );
508               aPnt = aSystem.CentreOfMass();
509             }
510             else if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID )
511             {
512               BRepGProp::VolumeProperties( aShape, aSystem );
513               aPnt = aSystem.CentreOfMass();
514             }
515             else
516             {
517               myCurrPointType = myPrevPointType;
518               break;
519             }
520
521             if ( mySetRotationPointDlg )
522             {
523               myRotationPointSelection = false;
524               mySetRotationPointDlg->setCoords(aPnt.X(), aPnt.Y(), aPnt.Z());
525             }
526           }
527           if ( ic->NbSelected() == 0 ) myCurrPointType = myPrevPointType;
528           if ( mySetRotationPointDlg ) mySetRotationPointDlg->toggleChange();
529           ic->CloseAllContexts();
530           myOperation = NOTHING;
531           myViewPort->setCursor( myCursor );
532           myCursorIsHand = false;
533           myRotationPointSelection = false;
534         }
535       }
536       else
537         emit mousePressed(this, theEvent);
538       break;
539     }
540     /* notify that we start a transformation */
541     if ( transformRequested() )
542             emit vpTransformationStarted ( myOperation );
543   }
544   if ( transformRequested() )
545     setTransformInProcess( true );
546
547   /* we may need it for sketching... */
548   if ( l_mbPressEvent )
549     delete l_mbPressEvent;
550   l_mbPressEvent = new QMouseEvent( *theEvent );
551 }
552
553
554 /*!
555   \brief Start zooming operation.
556
557   Sets the corresponding cursor for the widget.
558 */
559 void OCCViewer_ViewWindow::activateZoom()
560 {
561   if ( !transformRequested() && !myCursorIsHand )
562     myCursor = cursor();                /* save old cursor */
563
564   if ( myOperation != ZOOMVIEW ) {
565     QPixmap zoomPixmap (imageZoomCursor);
566     QCursor zoomCursor (zoomPixmap);
567     if( setTransformRequested ( ZOOMVIEW ) )
568       myViewPort->setCursor( zoomCursor );
569   }
570 }
571
572
573 /*!
574   \brief Start panning operation.
575
576   Sets the corresponding cursor for the widget.
577 */
578 void OCCViewer_ViewWindow::activatePanning()
579 {
580   if ( !transformRequested() && !myCursorIsHand )
581     myCursor = cursor();                // save old cursor
582
583   if ( myOperation != PANVIEW ) {
584     QCursor panCursor (Qt::SizeAllCursor);
585     if( setTransformRequested ( PANVIEW ) )
586       myViewPort->setCursor( panCursor );
587   }
588 }
589
590 /*!
591   \brief Start rotation operation
592
593   Sets the corresponding cursor for the widget.
594 */
595 void OCCViewer_ViewWindow::activateRotation()
596 {
597   if ( !transformRequested() && !myCursorIsHand )
598     myCursor = cursor();                // save old cursor
599
600   if ( myOperation != ROTATE ) {
601     QPixmap rotatePixmap (imageRotateCursor);
602     QCursor rotCursor (rotatePixmap);
603     if( setTransformRequested ( ROTATE ) )
604       myViewPort->setCursor( rotCursor );
605   }
606 }
607
608 /*!
609   \brief Compute the gravity center.
610   \param theX used to return X coordinate of the gravity center
611   \param theY used to return Y coordinate of the gravity center
612   \param theZ used to return Z coordinate of the gravity center
613   \return \c true if the gravity center is computed
614 */
615 bool OCCViewer_ViewWindow::computeGravityCenter( double& theX, double& theY, double& theZ )
616 {
617   Handle(V3d_View) aView3d = myViewPort->getView();
618
619   // Project boundaries points and add to avergae gravity
620   // the ones which lie within the screen limits
621   Standard_Real aScreenLimits[4] = { 0.0, 0.0, 0.0, 0.0 };
622
623 #if OCC_VERSION_LARGE > 0x06070100
624   // NDC space screen limits
625   aScreenLimits[0] = -1.0;
626   aScreenLimits[1] =  1.0;
627   aScreenLimits[2] = -1.0;
628   aScreenLimits[3] =  1.0;
629 #else
630   aView3d->View()->ViewMapping().WindowLimit( aScreenLimits[0],
631                                               aScreenLimits[1],
632                                               aScreenLimits[2],
633                                               aScreenLimits[3] );
634 #endif
635
636   Standard_Integer aPointsNb = 0;
637
638   Standard_Real aXmin = 0.0;
639   Standard_Real aYmin = 0.0;
640   Standard_Real aZmin = 0.0;
641   Standard_Real aXmax = 0.0;
642   Standard_Real aYmax = 0.0;
643   Standard_Real aZmax = 0.0;
644
645   Graphic3d_MapOfStructure aSetOfStructures;
646   aView3d->View()->DisplayedStructures( aSetOfStructures );
647   Graphic3d_MapIteratorOfMapOfStructure aStructureIt( aSetOfStructures );
648
649   for( ; aStructureIt.More(); aStructureIt.Next() ) {
650     const Handle(Graphic3d_Structure)& aStructure = aStructureIt.Key();
651     if ( aStructure->IsEmpty() ) {
652       continue;
653     }
654
655     aStructure->MinMaxValues( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
656
657     // Infinite structures are skipped
658     Standard_Real aLIM = ShortRealLast() - 1.0;
659     if ( Abs( aXmin ) > aLIM || Abs( aYmin ) > aLIM || Abs( aZmin ) > aLIM
660       || Abs( aXmax ) > aLIM || Abs( aYmax ) > aLIM || Abs( aZmax ) > aLIM ) {
661       continue;
662     }
663
664     gp_Pnt aPoints[8] = {
665       gp_Pnt( aXmin, aYmin, aZmin ), gp_Pnt( aXmin, aYmin, aZmax ),
666       gp_Pnt( aXmin, aYmax, aZmin ), gp_Pnt( aXmin, aYmax, aZmax ),
667       gp_Pnt( aXmax, aYmin, aZmin ), gp_Pnt( aXmax, aYmin, aZmax ),
668       gp_Pnt( aXmax, aYmax, aZmin ), gp_Pnt( aXmax, aYmax, aZmax )
669     };
670
671     for ( Standard_Integer aPointIt = 0; aPointIt < 8; ++aPointIt ) {
672       const gp_Pnt& aBBPoint = aPoints[aPointIt];
673
674 #if OCC_VERSION_LARGE > 0x06070100
675       gp_Pnt aProjected = aView3d->Camera()->Project( aBBPoint );
676       const Standard_Real& U = aProjected.X();
677       const Standard_Real& V = aProjected.Y();
678 #else
679       Standard_Real U = 0.0;
680       Standard_Real V = 0.0;
681       Standard_Real W = 0.0;
682       aView3d->View()->Projects( aBBPoint.X(), aBBPoint.Y(), aBBPoint.Z(), U, V, W );
683 #endif
684
685       if (U >= aScreenLimits[0]
686        && U <= aScreenLimits[1]
687        && V >= aScreenLimits[2]
688        && V <= aScreenLimits[3])
689       {
690         aPointsNb++;
691         theX += aBBPoint.X();
692         theY += aBBPoint.Y();
693         theZ += aBBPoint.Z();
694       }
695     }
696   }
697
698   if ( aPointsNb > 0 )
699   {
700     theX /= aPointsNb;
701     theY /= aPointsNb;
702     theZ /= aPointsNb;
703   }
704   return true;
705 }
706
707 /*!
708   \brief Set the gravity center as a rotation point.
709 */
710 void OCCViewer_ViewWindow::activateSetRotationGravity()
711 {
712   if ( myRotationPointSelection )
713   {
714     Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
715     ic->CloseAllContexts();
716     myOperation = NOTHING;
717     myViewPort->setCursor( myCursor );
718     myCursorIsHand = false;
719     myRotationPointSelection = false;
720   }
721
722   myPrevPointType = myCurrPointType;
723   myCurrPointType = GRAVITY;
724
725   Standard_Real Xcenter, Ycenter, Zcenter;
726   if ( computeGravityCenter( Xcenter, Ycenter, Zcenter ) )
727     mySetRotationPointDlg->setCoords( Xcenter, Ycenter, Zcenter );
728 }
729
730 /*!
731   \brief Update gravity center in the "Set Rotation Point" dialog box.
732   \sa OCCViewer_SetRotationPointDlg class
733 */
734 void OCCViewer_ViewWindow::updateGravityCoords()
735 {
736   if ( mySetRotationPointDlg && mySetRotationPointDlg->isVisible() && myCurrPointType == GRAVITY )
737   {
738     Standard_Real Xcenter, Ycenter, Zcenter;
739     if ( computeGravityCenter( Xcenter, Ycenter, Zcenter ) )
740       mySetRotationPointDlg->setCoords( Xcenter, Ycenter, Zcenter );
741   }
742 }
743
744 /*!
745   \brief Set the point selected by the user as a rotation point.
746   \param theX X coordinate of the rotation point
747   \param theY Y coordinate of the rotation point
748   \param theZ Z coordinate of the rotation point
749 */
750 void OCCViewer_ViewWindow::activateSetRotationSelected( double theX, double theY, double theZ )
751 {
752   if ( myRotationPointSelection )
753   {
754     Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
755     ic->CloseAllContexts();
756     myOperation = NOTHING;
757     myViewPort->setCursor( myCursor );
758     myCursorIsHand = false;
759     myRotationPointSelection = false;
760   }
761
762   myPrevPointType = myCurrPointType;
763   myCurrPointType = SELECTED;
764   mySelectedPoint.SetCoord(theX,theY,theZ);
765 }
766
767 /*!
768   \brief Start the shape selection process.
769 */
770 void OCCViewer_ViewWindow::activateStartPointSelection( TopAbs_ShapeEnum theShapeType )
771 {
772   myPrevPointType = myCurrPointType;
773   myCurrPointType = SELECTED;
774
775   // activate selection ------>
776   Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
777
778   ic->OpenLocalContext();
779
780   AIS_ListOfInteractive aList;
781   ic->DisplayedObjects( aList );
782   for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
783   {
784     Handle(AIS_InteractiveObject) anObj = it.Value();
785     if ( !anObj.IsNull() && anObj->HasPresentation() &&
786          anObj->IsKind( STANDARD_TYPE(AIS_Shape) ) )
787     {
788       ic->Load(anObj,-1);
789       ic->Activate(anObj,AIS_Shape::SelectionMode(theShapeType));
790      }
791   }
792   // activate selection <------
793
794   if ( !myCursorIsHand )
795   {
796     QCursor handCursor (Qt::PointingHandCursor);
797     myCursorIsHand = true;
798     myCursor = cursor();
799     myViewPort->setCursor( handCursor );
800   }
801   myRotationPointSelection = true;
802 }
803
804 /*!
805   \brief Start global panning operation
806
807   Sets the corresponding cursor for the widget.
808 */
809 void OCCViewer_ViewWindow::activateGlobalPanning()
810 {
811   Handle(V3d_View) aView3d = myViewPort->getView();
812   if ( !aView3d.IsNull() ) {
813     QPixmap globalPanPixmap (imageCrossCursor);
814     QCursor glPanCursor (globalPanPixmap);
815     myCurScale = aView3d->Scale();
816     aView3d->FitAll(0.01, false);
817     myCursor = cursor();                // save old cursor
818     myViewPort->fitAll(); // fits view before selecting a new scene center
819     if( setTransformRequested( PANGLOBAL ) )
820       myViewPort->setCursor( glPanCursor );
821   }
822 }
823
824 /*!
825   \brief Starts fit operation.
826
827   Sets the corresponding cursor for the widget.
828 */
829 void OCCViewer_ViewWindow::activateWindowFit()
830 {
831   if ( !transformRequested() && !myCursorIsHand )
832     myCursor = cursor();                /* save old cursor */
833
834   if ( myOperation != WINDOWFIT ) {
835     QCursor handCursor (Qt::PointingHandCursor);
836     if( setTransformRequested ( WINDOWFIT ) )
837     {
838       myViewPort->setCursor ( handCursor );
839       myCursorIsHand = true;
840     }
841   }
842 }
843
844 /*!
845   \brief Start delayed viewer operation.
846 */
847 bool OCCViewer_ViewWindow::setTransformRequested( OperationType op )
848 {
849   bool ok = transformEnabled( op );
850   myOperation = ok ? op : NOTHING;
851   myViewPort->setMouseTracking( myOperation == NOTHING );  
852   return ok;
853 }
854
855 /*!
856   \brief Handle mouse move event.
857   \param theEvent mouse event
858 */
859 void OCCViewer_ViewWindow::vpMouseMoveEvent( QMouseEvent* theEvent )
860 {
861   if ( myIsKeyFree && interactionStyle() == SUIT_ViewModel::KEY_FREE ) {
862     myIsKeyFree = false;
863     switch ( getButtonState( theEvent, interactionStyle() ) ) {
864     case ZOOMVIEW:
865       myViewPort->startZoomAtPoint( myStartX, myStartY );
866       activateZoom();
867       break;
868     case PANVIEW:
869       activatePanning();
870       break;
871     case ROTATE:
872       activateRotation();
873       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
874       break;
875     default:
876       break;
877     }
878   }
879
880   myCurrX = theEvent->x();
881   myCurrY = theEvent->y();
882   switch (myOperation) {
883   case ROTATE:
884     myViewPort->rotate(myCurrX, myCurrY, myCurrPointType, mySelectedPoint);
885     break;
886
887   case ZOOMVIEW:
888     myViewPort->zoom(myStartX, myStartY, myCurrX, myCurrY);
889     myStartX = myCurrX;
890     myStartY = myCurrY;
891     break;
892
893   case PANVIEW:
894     myViewPort->pan(myCurrX - myStartX, myStartY - myCurrY);
895     myStartX = myCurrX;
896     myStartY = myCurrY;
897     break;
898
899 /*    case WINDOWFIT:
900     myDrawRect = true;
901     repaint();
902     break;
903 */
904   case PANGLOBAL:
905     break;
906
907   default:
908     if ( myRotationPointSelection || isSketcherStyle() )
909     {
910       emit mouseMoving( this, theEvent );
911     }
912     else
913     {
914       int aState = theEvent->modifiers();
915       int aButton = theEvent->buttons();
916       int anInteractionStyle = interactionStyle();
917       if ( ( anInteractionStyle == SUIT_ViewModel::STANDARD &&
918            aButton == Qt::LeftButton && ( aState == Qt::NoModifier || Qt::ShiftModifier ) ) ||
919          ( anInteractionStyle == SUIT_ViewModel::KEY_FREE &&
920          aButton == Qt::LeftButton && ( aState == Qt::ControlModifier || aState == ( Qt::ControlModifier|Qt::ShiftModifier ) ) ) ) {
921         myDrawRect = myEnableDrawMode;
922         if ( myDrawRect ) {
923           drawRect();
924           if ( !myCursorIsHand )        {   // we are going to sketch a rectangle
925             QCursor handCursor (Qt::PointingHandCursor);
926             myCursorIsHand = true;
927             myCursor = cursor();
928             myViewPort->setCursor( handCursor );
929           }
930         }
931         emit mouseMoving( this, theEvent );
932       }
933       else if ( ( anInteractionStyle == SUIT_ViewModel::STANDARD &&
934                 aButton == Qt::RightButton && ( aState == Qt::NoModifier || Qt::ShiftModifier ) ) ||
935                 ( anInteractionStyle == SUIT_ViewModel::KEY_FREE &&
936                 aButton == Qt::RightButton && ( aState == Qt::ControlModifier || aState == ( Qt::ControlModifier|Qt::ShiftModifier ) ) ) ) {
937         OCCViewer_ViewSketcher* sketcher = 0;
938         QList<OCCViewer_ViewSketcher*>::Iterator it;
939         for ( it = mySketchers.begin(); it != mySketchers.end() && !sketcher; ++it )
940         {
941           OCCViewer_ViewSketcher* sk = (*it);
942           if( sk->isDefault() && sk->sketchButton() == aButton )
943             sketcher = sk;
944         }
945         if ( sketcher && myCurSketch == -1 )
946         {
947           activateSketching( sketcher->type() );
948           if ( mypSketcher )
949           {
950             myCurSketch = mypSketcher->sketchButton();
951
952             if ( l_mbPressEvent )
953             {
954               QApplication::sendEvent( getViewPort(), l_mbPressEvent );
955               delete l_mbPressEvent;
956               l_mbPressEvent = 0;
957             }
958             QApplication::sendEvent( getViewPort(), theEvent );
959           }
960         }
961       }
962       else
963         emit mouseMoving( this, theEvent );
964     }
965   }
966 }
967
968 /*!
969   \brief Handle mouse release event.
970   \param theEvent mouse event
971 */
972 void OCCViewer_ViewWindow::vpMouseReleaseEvent(QMouseEvent* theEvent)
973 {
974   switch ( myOperation ) {
975   case NOTHING:
976     {
977       int prevState = myCurSketch;
978       if(theEvent->button() == Qt::RightButton)
979       {
980         QList<OCCViewer_ViewSketcher*>::Iterator it;
981         for ( it = mySketchers.begin(); it != mySketchers.end() && myCurSketch != -1; ++it )
982         {
983           OCCViewer_ViewSketcher* sk = (*it);
984           if( ( sk->sketchButton() & theEvent->button() ) && sk->sketchButton() == myCurSketch )
985             myCurSketch = -1;
986         }
987       }
988
989       emit mouseReleased(this, theEvent);
990       if(theEvent->button() == Qt::RightButton && prevState == -1)
991       {
992         QContextMenuEvent aEvent( QContextMenuEvent::Mouse,
993                                   theEvent->pos(), theEvent->globalPos() );
994         emit contextMenuRequested( &aEvent );
995       }
996     }
997     break;
998   case ROTATE:
999     myViewPort->endRotation();
1000     resetState();
1001     break;
1002
1003   case PANVIEW:
1004   case ZOOMVIEW:
1005     myViewPort->getView()->ZFitAll();
1006     resetState();
1007     break;
1008
1009   case PANGLOBAL:
1010     if ( theEvent->button() == Qt::LeftButton ) {
1011       myViewPort->setCenter( theEvent->x(), theEvent->y() );
1012       myViewPort->getView()->SetScale(myCurScale);
1013       resetState();
1014     }
1015     break;
1016
1017   case WINDOWFIT:
1018     if ( theEvent->button() == Qt::LeftButton ) {
1019       myCurrX = theEvent->x();
1020       myCurrY = theEvent->y();
1021       drawRect();
1022       QRect rect = SUIT_Tools::makeRect(myStartX, myStartY, myCurrX, myCurrY);
1023       if ( !rect.isEmpty() ) myViewPort->fitRect(rect);
1024       endDrawRect();
1025       resetState();
1026     }
1027     break;
1028   }
1029
1030   // NOTE: viewer 3D detects a rectangle of selection using this event
1031   // so we must emit it BEFORE resetting the selection rectangle
1032
1033   if ( theEvent->button() == Qt::LeftButton && myDrawRect ) {
1034     drawRect();
1035     endDrawRect();
1036     resetState();
1037     myViewPort->update();
1038   }
1039
1040   if ( l_mbPressEvent )
1041   {
1042     delete l_mbPressEvent;
1043     l_mbPressEvent = 0;
1044   }
1045 }
1046
1047 /*!
1048   \brief Reset the viewport to its initial state
1049   ( no transformations in process etc. )
1050 */
1051 void OCCViewer_ViewWindow::resetState()
1052 {
1053   myDrawRect = false;
1054
1055   if ( myRotationPointSelection )
1056   {
1057     QCursor handCursor (Qt::PointingHandCursor);
1058     myViewPort->setCursor( handCursor );
1059   }
1060   else
1061   {
1062     if ( transformRequested() || myCursorIsHand )
1063       myViewPort->setCursor( myCursor );
1064     myCursorIsHand = false;
1065   }
1066
1067   if ( transformRequested() )
1068     emit vpTransformationFinished (myOperation);
1069
1070   setTransformInProcess( false );
1071   setTransformRequested( NOTHING );
1072 }
1073
1074
1075 /*!
1076   \brief Draw rubber band rectangle.
1077 */
1078 void OCCViewer_ViewWindow::drawRect()
1079 {
1080   if ( !myRectBand ) {
1081     myRectBand = new QtxRectRubberBand( myViewPort );
1082     //QPalette palette;
1083     //palette.setColor(myRectBand->foregroundRole(), Qt::white);
1084     //myRectBand->setPalette(palette);
1085   }
1086   //myRectBand->hide();
1087   
1088   myRectBand->setUpdatesEnabled ( false );
1089   QRect aRect = SUIT_Tools::makeRect(myStartX, myStartY, myCurrX, myCurrY);
1090   myRectBand->initGeometry( aRect );
1091
1092   if ( !myRectBand->isVisible() )
1093     myRectBand->show();
1094
1095   myRectBand->setUpdatesEnabled ( true );
1096   //myRectBand->repaint();
1097
1098   //myRectBand->setVisible( aRect.isValid() );
1099   //if ( myRectBand->isVisible() )
1100   //  myRectBand->repaint();
1101   //else
1102   //  myRectBand->show();
1103   //myRectBand->repaint();
1104 }
1105
1106 /*!
1107   \brief Clear rubber band rectangle on the end on the dragging operation.
1108 */
1109 void OCCViewer_ViewWindow::endDrawRect()
1110 {
1111   //delete myRectBand;
1112   //myRectBand = 0;
1113   if ( myRectBand )
1114     {
1115       myRectBand->clearGeometry();
1116       myRectBand->hide();
1117     }
1118 }
1119
1120 /*!
1121   \brief Create actions.
1122 */
1123 void OCCViewer_ViewWindow::createActions()
1124 {
1125   if( !toolMgr()->isEmpty() )
1126     return;
1127   
1128   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
1129
1130   QtxAction* aAction;
1131
1132   // Dump view
1133   aAction = new QtxAction(tr("MNU_DUMP_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_DUMP" ) ),
1134                            tr( "MNU_DUMP_VIEW" ), 0, this);
1135   aAction->setStatusTip(tr("DSC_DUMP_VIEW"));
1136   connect(aAction, SIGNAL(triggered()), this, SLOT(onDumpView()));
1137   toolMgr()->registerAction( aAction, DumpId );
1138
1139   // FitAll
1140   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_FITALL" ) ),
1141                            tr( "MNU_FITALL" ), 0, this);
1142   aAction->setStatusTip(tr("DSC_FITALL"));
1143   connect(aAction, SIGNAL(triggered()), this, SLOT(onFitAll()));
1144   toolMgr()->registerAction( aAction, FitAllId );
1145
1146   // FitRect
1147   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_FITAREA" ) ),
1148                            tr( "MNU_FITRECT" ), 0, this);
1149   aAction->setStatusTip(tr("DSC_FITRECT"));
1150   connect(aAction, SIGNAL(triggered()), this, SLOT(activateWindowFit()));
1151   toolMgr()->registerAction( aAction, FitRectId );
1152   
1153   // Zoom
1154   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_ZOOM" ) ),
1155                            tr( "MNU_ZOOM_VIEW" ), 0, this);
1156   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
1157   connect(aAction, SIGNAL(triggered()), this, SLOT(activateZoom()));
1158   toolMgr()->registerAction( aAction, ZoomId );
1159
1160   // Panning
1161   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_PAN" ) ),
1162                            tr( "MNU_PAN_VIEW" ), 0, this);
1163   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
1164   connect(aAction, SIGNAL(triggered()), this, SLOT(activatePanning()));
1165   toolMgr()->registerAction( aAction, PanId );
1166
1167   // Global Panning
1168   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_GLOBALPAN" ) ),
1169                            tr( "MNU_GLOBALPAN_VIEW" ), 0, this);
1170   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
1171   connect(aAction, SIGNAL(triggered()), this, SLOT(activateGlobalPanning()));
1172   toolMgr()->registerAction( aAction, GlobalPanId );
1173
1174   // Rotation Point
1175   mySetRotationPointAction = new QtxAction(tr("MNU_CHANGINGROTATIONPOINT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_ROTATION_POINT" ) ),
1176                            tr( "MNU_CHANGINGROTATIONPOINT_VIEW" ), 0, this);
1177   mySetRotationPointAction->setStatusTip(tr("DSC_CHANGINGROTATIONPOINT_VIEW"));
1178   mySetRotationPointAction->setCheckable( true );
1179   connect(mySetRotationPointAction, SIGNAL(toggled( bool )), this, SLOT(onSetRotationPoint( bool )));
1180   toolMgr()->registerAction( mySetRotationPointAction, ChangeRotationPointId );
1181
1182   // Rotation
1183   aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_ROTATE" ) ),
1184                            tr( "MNU_ROTATE_VIEW" ), 0, this);
1185   aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
1186   connect(aAction, SIGNAL(triggered()), this, SLOT(activateRotation()));
1187   toolMgr()->registerAction( aAction, RotationId );
1188
1189   // Projections
1190   aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_FRONT" ) ),
1191                            tr( "MNU_FRONT_VIEW" ), 0, this, false, "Viewers:Front view");
1192   aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
1193   connect(aAction, SIGNAL(triggered()), this, SLOT(onFrontView()));
1194   this->addAction(aAction);
1195   toolMgr()->registerAction( aAction, FrontId );
1196
1197   aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_BACK" ) ),
1198                            tr( "MNU_BACK_VIEW" ), 0, this, false, "Viewers:Back view");
1199   aAction->setStatusTip(tr("DSC_BACK_VIEW"));
1200   connect(aAction, SIGNAL(triggered()), this, SLOT(onBackView()));
1201   this->addAction(aAction);
1202   toolMgr()->registerAction( aAction, BackId );
1203
1204   aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_TOP" ) ),
1205                            tr( "MNU_TOP_VIEW" ), 0, this, false, "Viewers:Top view");
1206   aAction->setStatusTip(tr("DSC_TOP_VIEW"));
1207   connect(aAction, SIGNAL(triggered()), this, SLOT(onTopView()));
1208   this->addAction(aAction);
1209   toolMgr()->registerAction( aAction, TopId );
1210
1211   aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_BOTTOM" ) ),
1212                            tr( "MNU_BOTTOM_VIEW" ), 0, this, false, "Viewers:Bottom view");
1213   aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
1214   connect(aAction, SIGNAL(triggered()), this, SLOT(onBottomView()));
1215   this->addAction(aAction);
1216   toolMgr()->registerAction( aAction, BottomId );
1217   
1218   aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_LEFT" ) ),
1219                            tr( "MNU_LEFT_VIEW" ), 0, this, false, "Viewers:Left view");
1220   aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
1221   connect(aAction, SIGNAL(triggered()), this, SLOT(onLeftView()));
1222   this->addAction(aAction);
1223   toolMgr()->registerAction( aAction, LeftId );
1224
1225   aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_RIGHT" ) ),
1226                            tr( "MNU_RIGHT_VIEW" ), 0, this, false, "Viewers:Right view");
1227   aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
1228   connect(aAction, SIGNAL(triggered()), this, SLOT(onRightView()));
1229   this->addAction(aAction);
1230   toolMgr()->registerAction( aAction, RightId );
1231
1232   // rotate anticlockwise
1233   aAction = new QtxAction(tr("MNU_ANTICLOCKWISE_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_ANTICLOCKWISE" ) ),
1234                            tr( "MNU_ANTICLOCKWISE_VIEW" ), 0, this, false, "Viewers:Rotate anticlockwise");
1235   aAction->setStatusTip(tr("DSC_ANTICLOCKWISE_VIEW"));
1236   connect(aAction, SIGNAL(triggered()), this, SLOT(onAntiClockWiseView()));
1237   this->addAction(aAction);
1238   toolMgr()->registerAction( aAction, AntiClockWiseId );
1239
1240   // rotate clockwise
1241   aAction = new QtxAction(tr("MNU_CLOCKWISE_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_CLOCKWISE" ) ),
1242                            tr( "MNU_CLOCKWISE_VIEW" ), 0, this, false, "Viewers:Rotate clockwise");
1243   aAction->setStatusTip(tr("DSC_CLOCKWISE_VIEW"));
1244   connect(aAction, SIGNAL(triggered()), this, SLOT(onClockWiseView()));
1245   this->addAction(aAction);
1246   toolMgr()->registerAction( aAction, ClockWiseId );
1247
1248   // Reset
1249   aAction = new QtxAction(tr("MNU_RESET_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_RESET" ) ),
1250                            tr( "MNU_RESET_VIEW" ), 0, this, false, "Viewers:Reset view");
1251   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
1252   connect(aAction, SIGNAL(triggered()), this, SLOT(onResetView()));
1253   this->addAction(aAction);
1254   toolMgr()->registerAction( aAction, ResetId );
1255
1256   // Clone
1257   aAction = new QtxAction(tr("MNU_CLONE_VIEW"),
1258                           aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_CLONE_VIEW")),
1259                           tr("MNU_CLONE_VIEW"), 0, this);
1260   aAction->setStatusTip(tr("DSC_CLONE_VIEW"));
1261   connect(aAction, SIGNAL(triggered()), this, SLOT(onCloneView()));
1262   toolMgr()->registerAction( aAction, CloneId );
1263
1264   aAction = new QtxAction (tr ("MNU_CLIPPING"), aResMgr->loadPixmap ("OCCViewer", tr ("ICON_OCCVIEWER_CLIPPING")),
1265                                       tr ("MNU_CLIPPING"), 0, this);
1266   aAction->setStatusTip (tr ("DSC_CLIPPING"));
1267   aAction->setCheckable (true);
1268   connect (aAction, SIGNAL (toggled (bool)), this, SLOT (onClipping (bool)));
1269   toolMgr()->registerAction (aAction, ClippingId);
1270
1271   aAction = new QtxAction(tr("MNU_SHOOT_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_SHOOT_VIEW" ) ),
1272                            tr( "MNU_SHOOT_VIEW" ), 0, this);
1273   aAction->setStatusTip(tr("DSC_SHOOT_VIEW"));
1274   connect(aAction, SIGNAL(triggered()), this, SLOT(onMemorizeView()));
1275   toolMgr()->registerAction( aAction, MemId );
1276
1277   aAction = new QtxAction(tr("MNU_PRESETS_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_PRESETS_VIEW" ) ),
1278                            tr( "MNU_PRESETS_VIEW" ), 0, this);
1279   aAction->setStatusTip(tr("DSC_PRESETS_VIEW"));
1280   connect(aAction, SIGNAL(triggered()), this, SLOT(onRestoreView()));
1281   toolMgr()->registerAction( aAction, RestoreId );
1282
1283   if (myModel->trihedronActivated()) {
1284     aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRE"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_VIEW_TRIHEDRON" ) ),
1285                              tr( "MNU_SHOW_TRIHEDRE" ), 0, this);
1286     aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRE"));
1287     connect(aAction, SIGNAL(triggered()), this, SLOT(onTrihedronShow()));
1288     toolMgr()->registerAction( aAction, TrihedronShowId );
1289   }
1290
1291   // Scale
1292   aAction = new QtxAction(tr("MNU_SCALING"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_SCALING" ) ),
1293                            tr( "MNU_SCALING" ), 0, this);
1294   aAction->setStatusTip(tr("DSC_SCALING"));
1295   connect(aAction, SIGNAL(triggered()), this, SLOT(onAxialScale()));
1296   toolMgr()->registerAction( aAction, AxialScaleId );
1297
1298   // Enable/disable preselection
1299   aAction = new QtxAction(tr("MNU_ENABLE_PRESELECTION"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_PRESELECTION" ) ),
1300                           tr( "MNU_ENABLE_PRESELECTION" ), 0, this);
1301   aAction->setStatusTip(tr("DSC_ENABLE_PRESELECTION"));
1302   aAction->setCheckable(true);
1303   connect(aAction, SIGNAL(toggled(bool)), this, SLOT(onSwitchPreselection(bool)));
1304   toolMgr()->registerAction( aAction, SwitchPreselectionId );
1305
1306   // Enable/disable selection
1307   aAction = new QtxAction(tr("MNU_ENABLE_SELECTION"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_SELECTION" ) ),
1308                           tr( "MNU_ENABLE_SELECTION" ), 0, this);
1309   aAction->setStatusTip(tr("DSC_ENABLE_SELECTION"));
1310   aAction->setCheckable(true);
1311   connect(aAction, SIGNAL(toggled(bool)), this, SLOT(onSwitchSelection(bool)));
1312   toolMgr()->registerAction( aAction, SwitchSelectionId );
1313
1314   // Graduated axes 
1315   aAction = new QtxAction(tr("MNU_GRADUATED_AXES"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_GRADUATED_AXES" ) ),
1316                            tr( "MNU_GRADUATED_AXES" ), 0, this);
1317   aAction->setStatusTip(tr("DSC_GRADUATED_AXES"));
1318   connect(aAction, SIGNAL(triggered()), this, SLOT(onGraduatedAxes()));
1319   toolMgr()->registerAction( aAction, GraduatedAxesId );
1320
1321   // Active only ambient light or not
1322   aAction = new QtxAction(tr("MNU_AMBIENT"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_AMBIENT" ) ),
1323                            tr( "MNU_AMBIENT" ), 0, this);
1324   aAction->setStatusTip(tr("DSC_AMBIENT"));
1325   connect(aAction, SIGNAL(triggered()), this, SLOT(onAmbientToogle()));
1326   toolMgr()->registerAction( aAction, AmbientId );
1327
1328   // Switch between interaction styles
1329   aAction = new QtxAction(tr("MNU_STYLE_SWITCH"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_STYLE_SWITCH" ) ),
1330                           tr( "MNU_STYLE_SWITCH" ), 0, this);
1331   aAction->setStatusTip(tr("DSC_STYLE_SWITCH"));
1332   aAction->setCheckable(true);
1333   connect(aAction, SIGNAL(toggled(bool)), this, SLOT(onSwitchInteractionStyle(bool)));
1334   toolMgr()->registerAction( aAction, SwitchInteractionStyleId );
1335
1336   // Switch between zooming styles
1337   aAction = new QtxAction(tr("MNU_ZOOMING_STYLE_SWITCH"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_ZOOMING_STYLE_SWITCH" ) ),
1338                           tr( "MNU_ZOOMING_STYLE_SWITCH" ), 0, this);
1339   aAction->setStatusTip(tr("DSC_ZOOMING_STYLE_SWITCH"));
1340   aAction->setCheckable(true);
1341   connect(aAction, SIGNAL(toggled(bool)), this, SLOT(onSwitchZoomingStyle(bool)));
1342   toolMgr()->registerAction( aAction, SwitchZoomingStyleId );
1343
1344   // Maximized view
1345   aAction = new QtxAction(tr("MNU_MINIMIZE_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_MINIMIZE" ) ),
1346                           tr( "MNU_MINIMIZE_VIEW" ), 0, this );
1347   aAction->setStatusTip(tr("DSC_MINIMIZE_VIEW"));
1348   connect(aAction, SIGNAL(triggered()), this, SLOT(onMaximizedView()));
1349   toolMgr()->registerAction( aAction, MaximizedId );
1350
1351   // Return to 3d view
1352   if (my2dMode!=No2dMode){
1353     aAction = new QtxAction(tr("MNU_RETURN_3D_VIEW"), aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_RETURN_3D_VIEW" ) ),
1354                             tr( "MNU_RETURN_3D_VIEW" ), 0, this );
1355     aAction->setStatusTip(tr("DSC_RETURN_3D_VIEW"));
1356     connect(aAction, SIGNAL(triggered()), this, SLOT(returnTo3dView()));
1357     toolMgr()->registerAction( aAction, ReturnTo3dViewId );
1358   }
1359
1360   // Synchronize View 
1361   toolMgr()->registerAction( synchronizeAction(), SynchronizeId );
1362 }
1363
1364 /*!
1365   \brief Create toolbar.
1366 */
1367 void OCCViewer_ViewWindow::createToolBar()
1368 {
1369   QString aToolbarName;
1370   switch (my2dMode) {
1371   case XYPlane:
1372     aToolbarName = tr( "LBL_XYTOOLBAR_LABEL" );
1373     break;
1374   case XZPlane:
1375     aToolbarName = tr( "LBL_XZTOOLBAR_LABEL" );
1376     break;
1377   case YZPlane:
1378     aToolbarName = tr( "LBL_YZTOOLBAR_LABEL" );
1379     break;
1380   default:
1381     aToolbarName = tr( "LBL_3DTOOLBAR_LABEL" );
1382   }
1383   
1384   int tid = toolMgr()->createToolBar( aToolbarName, false );
1385   if ( my2dMode != No2dMode ){
1386     toolMgr()->append( ReturnTo3dViewId, tid );
1387     toolMgr()->append( toolMgr()->separator(), tid );
1388   }
1389   toolMgr()->append( DumpId, tid );
1390   toolMgr()->append( SwitchInteractionStyleId, tid );
1391 #if OCC_VERSION_LARGE > 0x0603000A // available only with OCC-6.3-sp11 and higher version
1392   toolMgr()->append( SwitchZoomingStyleId, tid );
1393 #endif
1394   toolMgr()->append( SwitchPreselectionId, tid );
1395   toolMgr()->append( SwitchSelectionId, tid );
1396   if( myModel->trihedronActivated() )
1397     toolMgr()->append( TrihedronShowId, tid );
1398
1399   QtxMultiAction* aScaleAction = new QtxMultiAction( this );
1400   aScaleAction->insertAction( toolMgr()->action( FitAllId ) );
1401   aScaleAction->insertAction( toolMgr()->action( FitRectId ) );
1402   aScaleAction->insertAction( toolMgr()->action( ZoomId ) );
1403   toolMgr()->append( aScaleAction, tid );
1404
1405   QtxMultiAction* aPanningAction = new QtxMultiAction( this );
1406   aPanningAction->insertAction( toolMgr()->action( PanId ) );
1407   aPanningAction->insertAction( toolMgr()->action( GlobalPanId ) );
1408   toolMgr()->append( aPanningAction, tid );
1409
1410   if (my2dMode == No2dMode) {
1411     toolMgr()->append( ChangeRotationPointId, tid );
1412     toolMgr()->append( RotationId, tid );
1413
1414     QtxMultiAction* aViewsAction = new QtxMultiAction( this );
1415     aViewsAction->insertAction( toolMgr()->action( FrontId ) );
1416     aViewsAction->insertAction( toolMgr()->action( BackId ) );
1417     aViewsAction->insertAction( toolMgr()->action( TopId ) );
1418     aViewsAction->insertAction( toolMgr()->action( BottomId ) );
1419     aViewsAction->insertAction( toolMgr()->action( LeftId ) );
1420     aViewsAction->insertAction( toolMgr()->action( RightId ) );
1421     toolMgr()->append( aViewsAction, tid );
1422
1423     toolMgr()->append( AntiClockWiseId, tid );
1424     toolMgr()->append( ClockWiseId, tid );
1425
1426     toolMgr()->append( ResetId, tid );
1427   }
1428
1429   QtxMultiAction* aMemAction = new QtxMultiAction( this );
1430   aMemAction->insertAction( toolMgr()->action( MemId ) );
1431   aMemAction->insertAction( toolMgr()->action( RestoreId ) );
1432   toolMgr()->append( aMemAction, tid );
1433
1434   toolMgr()->append( toolMgr()->separator(), tid );
1435   toolMgr()->append( CloneId, tid );
1436   
1437   toolMgr()->append( toolMgr()->separator(), tid );
1438   toolMgr()->append( ClippingId, tid );
1439   toolMgr()->append( AxialScaleId, tid );
1440 #if OCC_VERSION_LARGE > 0x06030009 // available only with OCC-6.3-sp10 and higher version
1441   toolMgr()->append( GraduatedAxesId, tid );
1442 #endif
1443   toolMgr()->append( AmbientId, tid );
1444
1445   toolMgr()->append( MaximizedId, tid );
1446   toolMgr()->append( SynchronizeId, tid );
1447 }
1448
1449 /*!
1450   \brief Perform 'fit all' operation.
1451 */
1452 void OCCViewer_ViewWindow::onViewFitAll()
1453 {
1454   myViewPort->fitAll();
1455 }
1456
1457 /*!
1458   \brief Perform "front view" transformation.
1459 */
1460 void OCCViewer_ViewWindow::onFrontView()
1461 {
1462   emit vpTransformationStarted ( FRONTVIEW );
1463   Handle(V3d_View) aView3d = myViewPort->getView();
1464   if ( !aView3d.IsNull() ) aView3d->SetProj (V3d_Xpos);
1465   onViewFitAll();
1466   emit vpTransformationFinished ( FRONTVIEW );
1467 }
1468
1469 /*!
1470   \brief Perform "back view" transformation.
1471 */
1472 void OCCViewer_ViewWindow::onBackView()
1473 {
1474   emit vpTransformationStarted ( BACKVIEW );
1475   Handle(V3d_View) aView3d = myViewPort->getView();
1476   if ( !aView3d.IsNull() ) aView3d->SetProj (V3d_Xneg);
1477   onViewFitAll();
1478   emit vpTransformationFinished ( BACKVIEW );
1479 }
1480
1481 /*!
1482   \brief Perform "top view" transformation.
1483 */
1484 void OCCViewer_ViewWindow::onTopView()
1485 {
1486   emit vpTransformationStarted ( TOPVIEW );
1487   Handle(V3d_View) aView3d = myViewPort->getView();
1488   if ( !aView3d.IsNull() ) aView3d->SetProj (V3d_Zpos);
1489   onViewFitAll();
1490   emit vpTransformationFinished ( TOPVIEW );
1491 }
1492
1493 /*!
1494   \brief Perform "bottom view" transformation.
1495 */
1496 void OCCViewer_ViewWindow::onBottomView()
1497 {
1498   emit vpTransformationStarted ( BOTTOMVIEW );
1499   Handle(V3d_View) aView3d = myViewPort->getView();
1500   if ( !aView3d.IsNull() ) aView3d->SetProj (V3d_Zneg);
1501   onViewFitAll();
1502   emit vpTransformationFinished ( BOTTOMVIEW );
1503 }
1504
1505 /*!
1506   \brief Perform "left view" transformation.
1507 */
1508 void OCCViewer_ViewWindow::onLeftView()
1509 {
1510   emit vpTransformationStarted ( LEFTVIEW );
1511   Handle(V3d_View) aView3d = myViewPort->getView();
1512   if ( !aView3d.IsNull() ) aView3d->SetProj (V3d_Yneg);
1513   onViewFitAll();
1514   emit vpTransformationFinished ( LEFTVIEW );
1515 }
1516
1517 /*!
1518   \brief Perform "right view" transformation.
1519 */
1520 void OCCViewer_ViewWindow::onRightView()
1521 {
1522   emit vpTransformationStarted ( RIGHTVIEW );
1523   Handle(V3d_View) aView3d = myViewPort->getView();
1524   if ( !aView3d.IsNull() ) aView3d->SetProj (V3d_Ypos);
1525   onViewFitAll();
1526   emit vpTransformationFinished ( RIGHTVIEW );
1527 }
1528
1529 /*!
1530   \brief Rotate view 90 degrees clockwise
1531 */
1532 void OCCViewer_ViewWindow::onClockWiseView()
1533 {
1534   emit vpTransformationStarted ( CLOCKWISEVIEW );
1535   myViewPort->rotateXY( 90. );
1536   emit vpTransformationFinished ( CLOCKWISEVIEW );
1537 }
1538
1539 /*!
1540   \brief Rotate view 90 degrees conterclockwise
1541 */
1542 void OCCViewer_ViewWindow::onAntiClockWiseView()
1543 {
1544   emit vpTransformationStarted ( ANTICLOCKWISEVIEW );
1545   myViewPort->rotateXY( -90. );
1546   emit vpTransformationFinished ( ANTICLOCKWISEVIEW );
1547 }
1548
1549 /*!
1550   \brief Perform "reset view" transformation.
1551
1552   Sets default orientation of the viewport camera.
1553 */
1554 void OCCViewer_ViewWindow::onResetView()
1555 {
1556   emit vpTransformationStarted( RESETVIEW );
1557   bool upd = myViewPort->getView()->SetImmediateUpdate( false );
1558   myViewPort->getView()->Reset( false );
1559   myViewPort->fitAll( false, true, false );
1560   myViewPort->getView()->SetImmediateUpdate( upd );
1561   myViewPort->getView()->Update();
1562   emit vpTransformationFinished( RESETVIEW );
1563 }
1564
1565 /*!
1566   \brief Perform "fit all" transformation.
1567 */
1568 void OCCViewer_ViewWindow::onFitAll()
1569 {
1570   emit vpTransformationStarted( FITALLVIEW );
1571   myViewPort->fitAll();
1572   emit vpTransformationFinished( FITALLVIEW );
1573 }
1574
1575 /*!
1576   \brief Called if 'change rotation point' operation is activated.
1577   \param on action state
1578 */
1579 void OCCViewer_ViewWindow::onSetRotationPoint( bool on )
1580 {
1581   if (on)
1582   {
1583     if (!mySetRotationPointDlg)
1584     {
1585       mySetRotationPointDlg = new OCCViewer_SetRotationPointDlg (this);
1586       mySetRotationPointDlg->SetAction(mySetRotationPointAction);
1587     }
1588
1589     if (!mySetRotationPointDlg->isVisible())
1590     {
1591       //if (mySetRotationPointDlg->IsFirstShown())
1592       if (myCurrPointType == GRAVITY)
1593       {
1594         Standard_Real Xcenter, Ycenter, Zcenter;
1595         if (computeGravityCenter(Xcenter, Ycenter, Zcenter))
1596           mySetRotationPointDlg->setCoords(Xcenter, Ycenter, Zcenter);
1597       }
1598       mySetRotationPointDlg->show();
1599     }
1600   }
1601   else
1602   {
1603     if (mySetRotationPointDlg->isVisible())
1604       mySetRotationPointDlg->hide();
1605   }
1606 }
1607
1608 /*!
1609    \brief Create one more window with same content.
1610 */
1611 void OCCViewer_ViewWindow::onCloneView()
1612 {
1613   SUIT_ViewWindow* vw = myManager->createViewWindow();
1614   //vw->show();
1615   emit viewCloned( vw );
1616 }
1617
1618 /*!
1619   Creates one more window with same content
1620 */
1621 void OCCViewer_ViewWindow::onAxialScale()
1622 {
1623   if ( !myScalingDlg )
1624     myScalingDlg = new OCCViewer_AxialScaleDlg( this );
1625   
1626   if ( !myScalingDlg->isVisible() )
1627   {
1628     myScalingDlg->Update();
1629     myScalingDlg->show();
1630   }
1631 }
1632
1633 /*!
1634   Shows Graduated Axes dialog
1635 */
1636 void OCCViewer_ViewWindow::onGraduatedAxes()
1637 {
1638   myCubeAxesDlg->Update();
1639   myCubeAxesDlg->show();
1640 }
1641
1642 void OCCViewer_ViewWindow::onAmbientToogle()
1643 {
1644   Handle(V3d_Viewer) viewer = myViewPort->getViewer();
1645   viewer->InitDefinedLights();
1646   while(viewer->MoreDefinedLights())
1647     {
1648       Handle(V3d_Light) light = viewer->DefinedLight();
1649       if(light->Type() != V3d_AMBIENT)
1650         {
1651           Handle(V3d_View) aView3d = myViewPort->getView();
1652           if( aView3d->IsActiveLight(light) ) viewer->SetLightOff(light);
1653           else viewer->SetLightOn(light);
1654         }
1655       viewer->NextDefinedLights();
1656     }
1657   viewer->Update();
1658 }
1659
1660 /*!
1661   \brief Store view parameters.
1662 */
1663 void OCCViewer_ViewWindow::onMemorizeView()
1664 {
1665   appendViewAspect( getViewParams() );
1666 }
1667
1668 /*!
1669   \brief Restore view parameters.
1670 */
1671 void OCCViewer_ViewWindow::onRestoreView()
1672 {
1673   OCCViewer_CreateRestoreViewDlg* aDlg = new OCCViewer_CreateRestoreViewDlg( centralWidget(), this );
1674   connect( aDlg, SIGNAL( dlgOk() ), this, SLOT( setRestoreFlag() ) );
1675   aDlg->exec();
1676   updateViewAspects( aDlg->parameters() );
1677   if( myRestoreFlag && aDlg->parameters().count() )
1678     performRestoring( aDlg->currentItem() );
1679 }
1680
1681 /*!
1682   \brief Restore view parameters.
1683   \param anItem view parameters
1684 */
1685 void OCCViewer_ViewWindow::performRestoring( const viewAspect& anItem, bool baseParamsOnly )
1686 {
1687   Handle(V3d_View) aView3d = myViewPort->getView();
1688
1689   Standard_Boolean prev = aView3d->SetImmediateUpdate( Standard_False );
1690   aView3d->SetScale( anItem.scale );
1691   aView3d->SetTwist( anItem.twist );
1692   aView3d->SetAt( anItem.atX, anItem.atY, anItem.atZ );
1693   aView3d->SetImmediateUpdate( prev );
1694   aView3d->SetEye( anItem.eyeX, anItem.eyeY, anItem.eyeZ );
1695   aView3d->SetProj( anItem.projX, anItem.projY, anItem.projZ );
1696   aView3d->SetAxialScale( anItem.scaleX, anItem.scaleY, anItem.scaleZ );
1697
1698 #if OCC_VERSION_LARGE > 0x06070100
1699   if ( anItem.centerX != 0.0 || anItem.centerY != 0.0 )
1700   {
1701     double anUpX = 0.0, anUpY = 0.0, anUpZ = 0.0;
1702
1703     // "eye" and "at" require conversion to represent center panning
1704     // up direction is only available after setting angle of twist and
1705     // other view parameters
1706     aView3d->Up( anUpX, anUpY, anUpZ );
1707
1708     gp_Dir aProj( -anItem.projX, -anItem.projY, -anItem.projZ );
1709     gp_Dir anUp( anUpX, anUpY, anUpZ );
1710     gp_Pnt anAt( anItem.atX, anItem.atY, anItem.atZ );
1711     gp_Pnt anEye( anItem.eyeX, anItem.eyeY, anItem.eyeZ );
1712     gp_Dir aSide = aProj ^ anUp;
1713
1714     anAt.Translate( gp_Vec( aSide ) * anItem.centerX );
1715     anAt.Translate( gp_Vec( anUp  ) * anItem.centerY );
1716
1717     aView3d->SetAt( anAt.X(), anAt.Y(), anAt.Z() );
1718     aView3d->SetProj( anItem.projX, anItem.projY, anItem.projZ );
1719   }
1720 #else
1721   aView3d->SetCenter( anItem.centerX, anItem.centerY );
1722 #endif
1723
1724   if ( !baseParamsOnly ) {
1725
1726     myModel->setTrihedronShown( anItem.isVisible );
1727     myModel->setTrihedronSize( anItem.size );
1728         
1729 #if OCC_VERSION_LARGE > 0x06030009 // available only with OCC-6.3-sp10 and higher version
1730     // graduated trihedron
1731     bool anIsVisible = anItem.gtIsVisible;
1732     OCCViewer_AxisWidget::AxisData anAxisData[3];
1733     anAxisData[0].DrawName = anItem.gtDrawNameX;
1734     anAxisData[1].DrawName = anItem.gtDrawNameZ;
1735     anAxisData[2].DrawName = anItem.gtDrawNameZ;
1736     anAxisData[0].Name = anItem.gtNameX;
1737     anAxisData[1].Name = anItem.gtNameZ;
1738     anAxisData[2].Name = anItem.gtNameZ;
1739     anAxisData[0].NameColor = QColor( anItem.gtNameColorRX,
1740               anItem.gtNameColorGX,
1741               anItem.gtNameColorBX );
1742     anAxisData[1].NameColor = QColor( anItem.gtNameColorRY,
1743               anItem.gtNameColorGY,
1744               anItem.gtNameColorBY );
1745     anAxisData[2].NameColor = QColor( anItem.gtNameColorRZ,
1746               anItem.gtNameColorGZ,
1747               anItem.gtNameColorBZ );
1748     anAxisData[0].DrawValues = anItem.gtDrawValuesX;
1749     anAxisData[1].DrawValues = anItem.gtDrawValuesY;
1750     anAxisData[2].DrawValues = anItem.gtDrawValuesZ;
1751     anAxisData[0].NbValues = anItem.gtNbValuesX;
1752     anAxisData[1].NbValues = anItem.gtNbValuesY;
1753     anAxisData[2].NbValues = anItem.gtNbValuesZ;
1754     anAxisData[0].Offset = anItem.gtOffsetX;
1755     anAxisData[1].Offset = anItem.gtOffsetY;
1756     anAxisData[2].Offset = anItem.gtOffsetZ;
1757     anAxisData[0].Color = QColor( anItem.gtColorRX,
1758           anItem.gtColorGX,
1759           anItem.gtColorBX );
1760     anAxisData[1].Color = QColor( anItem.gtColorRY,
1761           anItem.gtColorGY,
1762           anItem.gtColorBY );
1763     anAxisData[2].Color = QColor( anItem.gtColorRZ,
1764           anItem.gtColorGZ,
1765           anItem.gtColorBZ );
1766     anAxisData[0].DrawTickmarks = anItem.gtDrawTickmarksX;
1767     anAxisData[1].DrawTickmarks = anItem.gtDrawTickmarksY;
1768     anAxisData[2].DrawTickmarks = anItem.gtDrawTickmarksZ;
1769     anAxisData[0].TickmarkLength = anItem.gtTickmarkLengthX;
1770     anAxisData[1].TickmarkLength = anItem.gtTickmarkLengthY;
1771     anAxisData[2].TickmarkLength = anItem.gtTickmarkLengthZ;
1772
1773     myCubeAxesDlg->SetData( anIsVisible, anAxisData );
1774     myCubeAxesDlg->ApplyData( aView3d );
1775 #endif
1776
1777   } // if ( !baseParamsOnly )
1778
1779   myRestoreFlag = 0;
1780 }
1781
1782 /*!
1783   \brief Set restore flag.
1784 */
1785 void OCCViewer_ViewWindow::setRestoreFlag()
1786 {
1787   myRestoreFlag = 1;
1788 }
1789
1790 /*!
1791   \brief Called when action "show/hide trihedron" is activated.
1792 */
1793 void OCCViewer_ViewWindow::onTrihedronShow()
1794 {
1795   myModel->toggleTrihedron();
1796 }
1797
1798 /*!
1799   \brief Toggles preselection (highlighting) on/off
1800 */
1801 void OCCViewer_ViewWindow::onSwitchPreselection( bool on )
1802 {
1803   myPreselectionEnabled = on;
1804   myModel->setSelectionOptions( isPreselectionEnabled(), myModel->isSelectionEnabled() );
1805
1806   // unhighlight all highlighted objects
1807   /*if ( !on ) {
1808     myModel->unHighlightAll( true, false );
1809   }*/
1810
1811   // update action state if method is called outside
1812   QtxAction* a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchPreselectionId ) );
1813   if ( a && a->isChecked() != on ) {
1814     a->setChecked( on );
1815   }
1816 }
1817
1818 /*!
1819   \brief Toggles selection on/off
1820 */
1821 void OCCViewer_ViewWindow::onSwitchSelection( bool on )
1822 {
1823   mySelectionEnabled = on;
1824   myModel->setSelectionOptions( myModel->isPreselectionEnabled(), isSelectionEnabled() );
1825   
1826   // update action state if method is called outside
1827
1828   // preselection
1829   QtxAction* a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchPreselectionId ) );
1830   if ( a ) {
1831     a->setEnabled( on );
1832   }
1833
1834   // selection
1835   a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchSelectionId ) );
1836   if ( a && a->isChecked() != on ) {
1837     a->setChecked( on );
1838   }
1839 }
1840
1841 /*!
1842   \brief Switches "keyboard free" interaction style on/off
1843 */
1844 void OCCViewer_ViewWindow::onSwitchInteractionStyle( bool on )
1845 {
1846   myInteractionStyle = on ? (int)SUIT_ViewModel::KEY_FREE : (int)SUIT_ViewModel::STANDARD;
1847
1848   // update action state if method is called outside
1849   QtxAction* a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchInteractionStyleId ) );
1850   if ( a->isChecked() != on )
1851     a->setChecked( on );
1852 }
1853
1854 /*!
1855   \brief Toogles advanced zooming style (relatively to the cursor position) on/off
1856 */
1857 void OCCViewer_ViewWindow::onSwitchZoomingStyle( bool on )
1858 {
1859   myViewPort->setAdvancedZoomingEnabled( on );
1860
1861   // update action state if method is called outside
1862   QtxAction* a = dynamic_cast<QtxAction*>( toolMgr()->action( SwitchZoomingStyleId ) );
1863   if ( a->isChecked() != on )
1864     a->setChecked( on );
1865 }
1866
1867 /*!
1868   \brief Get current interaction style
1869   \return interaction style
1870 */
1871 int OCCViewer_ViewWindow::interactionStyle() const
1872 {
1873   return myInteractionStyle;
1874 }
1875
1876 /*!
1877   \brief Set current interaction style
1878   \param theStyle interaction style
1879 */
1880 void OCCViewer_ViewWindow::setInteractionStyle( const int theStyle )
1881 {
1882   onSwitchInteractionStyle( theStyle == (int)SUIT_ViewModel::KEY_FREE );
1883 }
1884
1885 /*!
1886   \brief Get current zooming style
1887   \return zooming style
1888 */
1889 int OCCViewer_ViewWindow::zoomingStyle() const
1890 {
1891   return myViewPort->isAdvancedZoomingEnabled() ? 1 : 0;
1892 }
1893
1894 /*!
1895   \brief Set current zooming style
1896   \param theStyle zooming style
1897 */
1898 void OCCViewer_ViewWindow::setZoomingStyle( const int theStyle )
1899 {
1900   onSwitchZoomingStyle( theStyle == 1 );
1901 }
1902
1903 /*!
1904   \brief Dump view window contents to the pixmap.
1905   \return pixmap containing all scene rendered in the window
1906 */
1907 QImage OCCViewer_ViewWindow::dumpView()
1908 {
1909   Handle(V3d_View) view = myViewPort->getView();
1910   if ( view.IsNull() )
1911     return QImage();
1912   
1913   int aWidth = myViewPort->width();
1914   int aHeight = myViewPort->height();
1915   QApplication::syncX();
1916   view->Redraw(); // In order to reactivate GL context
1917   //view->Update();
1918
1919   OpenGLUtils_FrameBuffer aFrameBuffer;
1920   if( aFrameBuffer.init( aWidth, aHeight ) )
1921   {
1922     QImage anImage( aWidth, aHeight, QImage::Format_RGB32 );
1923    
1924     glPushAttrib( GL_VIEWPORT_BIT );
1925     glViewport( 0, 0, aWidth, aHeight );
1926     aFrameBuffer.bind();
1927
1928     // draw scene
1929     view->Redraw();
1930
1931     aFrameBuffer.unbind();
1932     glPopAttrib();
1933
1934     aFrameBuffer.bind();
1935     glReadPixels( 0, 0, aWidth, aHeight, GL_RGBA, GL_UNSIGNED_BYTE, anImage.bits() );
1936     aFrameBuffer.unbind();
1937
1938     anImage = anImage.rgbSwapped();
1939     anImage = anImage.mirrored();
1940     return anImage;
1941   }
1942   // if frame buffers are unsupported, use old functionality
1943   //view->Redraw();
1944
1945   unsigned char* data = new unsigned char[ aWidth*aHeight*4 ];
1946
1947   QPoint p = myViewPort->mapFromParent(myViewPort->geometry().topLeft());
1948
1949   glReadPixels( p.x(), p.y(), aWidth, aHeight, GL_RGBA, GL_UNSIGNED_BYTE,
1950                 data);
1951
1952   QImage anImage( data, aWidth, aHeight, QImage::Format_ARGB32 );
1953   anImage = anImage.mirrored();
1954   anImage = anImage.rgbSwapped();
1955   return anImage;
1956 }
1957
1958 bool OCCViewer_ViewWindow::dumpViewToFormat( const QImage& img, 
1959                                              const QString& fileName, 
1960                                              const QString& format )
1961 {
1962   if ( format != "PS" && format != "EPS")
1963     return SUIT_ViewWindow::dumpViewToFormat( img, fileName, format );
1964
1965   Handle(Visual3d_View) a3dView = myViewPort->getView()->View();
1966
1967   if (format == "PS")
1968     a3dView->Export(strdup(qPrintable(fileName)), Graphic3d_EF_PostScript);
1969   else if (format == "EPS")
1970     a3dView->Export(strdup(qPrintable(fileName)), Graphic3d_EF_EnhPostScript);
1971
1972   return true;
1973 }
1974
1975
1976 QString OCCViewer_ViewWindow::filter() const
1977 {
1978   return tr( "OCC_IMAGE_FILES" );
1979 }
1980
1981
1982 /*!
1983   \brief Set parameters of the cutting plane
1984   \param on if \c true, cutting plane is enabled
1985   \param x X position of plane point
1986   \param y Y position of plane point
1987   \param z Z position of plane point
1988   \param dx X coordinate of plane normal
1989   \param dy Y coordinate of plane normal
1990   \param dz Z coordinate of plane normal
1991 */
1992 void OCCViewer_ViewWindow::setCuttingPlane( bool on, const double x,  const double y,  const double z,
1993                                             const double dx, const double dy, const double dz )
1994 {
1995   Handle(V3d_View) view = myViewPort->getView();
1996   if ( view.IsNull() )
1997     return;
1998
1999   if ( on ) {
2000     Handle(V3d_Viewer) viewer = myViewPort->getViewer();
2001
2002     // try to use already existing plane or create a new one
2003     Handle(V3d_Plane) clipPlane;
2004
2005     // calculate new a,b,c,d values for the plane
2006     gp_Pln pln (gp_Pnt(x, y, z), gp_Dir(dx, dy, dz));
2007     double a, b, c, d;
2008     pln.Coefficients(a, b, c, d);
2009     
2010     Graphic3d_SequenceOfHClipPlane aPlanes = view->GetClipPlanes();
2011     Handle(Graphic3d_ClipPlane) aClipPlane;
2012     if(aPlanes.Size() > 0 ) {
2013       Graphic3d_SequenceOfHClipPlane::Iterator anIter (aPlanes);
2014       aClipPlane = anIter.Value();
2015       aClipPlane->SetEquation(pln);
2016       aClipPlane->SetOn(Standard_True);
2017     } else {
2018       aClipPlane = new Graphic3d_ClipPlane(pln);
2019       view->AddClipPlane(aClipPlane);
2020       aClipPlane->SetOn(Standard_True);
2021     }
2022   }
2023   else {
2024     Graphic3d_SequenceOfHClipPlane aPlanes = view->GetClipPlanes();
2025     Graphic3d_SequenceOfHClipPlane::Iterator anIter (aPlanes);
2026     for( ;anIter.More();anIter.Next() ){
2027       Handle(Graphic3d_ClipPlane) aClipPlane = anIter.Value();
2028       aClipPlane->SetOn(Standard_False);
2029     }
2030   }
2031
2032   view->Update();
2033   view->Redraw();
2034 }
2035
2036 void OCCViewer_ViewWindow::setCuttingPlane( bool on, const gp_Pln pln )
2037 {
2038   gp_Dir aDir = pln.Axis().Direction();
2039   gp_Pnt aPnt = pln.Location();
2040   setCuttingPlane(on, aPnt.X(), aPnt.Y(), aPnt.Z(), aDir.X(), aDir.Y(), aDir.Z());
2041 }
2042
2043
2044 /*!
2045   \brief Check if any cutting plane is enabled
2046   \return \c true if at least one cutting plane is enabled
2047 */
2048 bool OCCViewer_ViewWindow::isCuttingPlane()
2049 {
2050   Handle(V3d_View) view = myViewPort->getView();
2051   bool res = false;
2052   Graphic3d_SequenceOfHClipPlane aPlanes = view->GetClipPlanes();
2053   Graphic3d_SequenceOfHClipPlane::Iterator anIter (aPlanes);
2054   for( ;anIter.More();anIter.Next() ) {
2055     Handle(Graphic3d_ClipPlane) aClipPlane = anIter.Value();
2056     if(aClipPlane->IsOn()) {
2057       res = true;
2058       break;
2059     }
2060   }
2061   return res;
2062 }
2063
2064 /*!
2065   \brief Get the visual parameters of the view window.
2066   \return visual parameters of view window
2067 */
2068 viewAspect OCCViewer_ViewWindow::getViewParams() const
2069 {
2070   double projX, projY, projZ, twist;
2071   double atX, atY, atZ, eyeX, eyeY, eyeZ;
2072   double aScaleX, aScaleY, aScaleZ;
2073
2074   Handle(V3d_View) aView3d = myViewPort->getView();
2075
2076   aView3d->Proj( projX, projY, projZ );
2077   aView3d->At( atX, atY, atZ );
2078   aView3d->Eye( eyeX, eyeY, eyeZ );
2079   twist = aView3d->Twist();
2080
2081   aView3d->AxialScale(aScaleX,aScaleY,aScaleZ);
2082
2083   bool isShown = myModel->isTrihedronVisible();
2084   double size = myModel->trihedronSize();
2085
2086   QString aName = QTime::currentTime().toString() + QString::fromLatin1( " h:m:s" );
2087
2088   viewAspect params;
2089   params.scale    = aView3d->Scale();
2090   params.projX    = projX;
2091   params.projY    = projY;
2092   params.projZ    = projZ;
2093   params.twist    = twist;
2094   params.atX      = atX;
2095   params.atY      = atY;
2096   params.atZ      = atZ;
2097   params.eyeX     = eyeX;
2098   params.eyeY     = eyeY;
2099   params.eyeZ     = eyeZ;
2100   params.scaleX   = aScaleX;
2101   params.scaleY   = aScaleY;
2102   params.scaleZ   = aScaleZ;
2103   params.name     = aName;
2104   params.isVisible= isShown;
2105   params.size     = size;
2106
2107 #if OCC_VERSION_LARGE <= 0x06070100 // the property is deprecated after OCCT 6.7.1
2108   aView3d->Center( params.centerX, params.centerY );
2109 #endif
2110
2111 #if OCC_VERSION_LARGE > 0x06030009 // available only with OCC-6.3-sp10 and higher version
2112   // graduated trihedron
2113   bool anIsVisible = false;
2114   OCCViewer_AxisWidget::AxisData anAxisData[3];
2115   myCubeAxesDlg->GetData( anIsVisible, anAxisData );
2116
2117   params.gtIsVisible = anIsVisible;
2118   params.gtDrawNameX = anAxisData[0].DrawName;
2119   params.gtDrawNameY = anAxisData[1].DrawName;
2120   params.gtDrawNameZ = anAxisData[2].DrawName;
2121   params.gtNameX = anAxisData[0].Name;
2122   params.gtNameY = anAxisData[1].Name;
2123   params.gtNameZ = anAxisData[2].Name;
2124   params.gtNameColorRX = anAxisData[0].NameColor.red();
2125   params.gtNameColorGX = anAxisData[0].NameColor.green();
2126   params.gtNameColorBX = anAxisData[0].NameColor.blue();
2127   params.gtNameColorRY = anAxisData[1].NameColor.red();
2128   params.gtNameColorGY = anAxisData[1].NameColor.green();
2129   params.gtNameColorBY = anAxisData[1].NameColor.blue();
2130   params.gtNameColorRZ = anAxisData[2].NameColor.red();
2131   params.gtNameColorGZ = anAxisData[2].NameColor.green();
2132   params.gtNameColorBZ = anAxisData[2].NameColor.blue();
2133   params.gtDrawValuesX = anAxisData[0].DrawValues;
2134   params.gtDrawValuesY = anAxisData[1].DrawValues;
2135   params.gtDrawValuesZ = anAxisData[2].DrawValues;
2136   params.gtNbValuesX = anAxisData[0].NbValues;
2137   params.gtNbValuesY = anAxisData[1].NbValues;
2138   params.gtNbValuesZ = anAxisData[2].NbValues;
2139   params.gtOffsetX = anAxisData[0].Offset;
2140   params.gtOffsetY = anAxisData[1].Offset;
2141   params.gtOffsetZ = anAxisData[2].Offset;
2142   params.gtColorRX = anAxisData[0].Color.red();
2143   params.gtColorGX = anAxisData[0].Color.green();
2144   params.gtColorBX = anAxisData[0].Color.blue();
2145   params.gtColorRY = anAxisData[1].Color.red();
2146   params.gtColorGY = anAxisData[1].Color.green();
2147   params.gtColorBY = anAxisData[1].Color.blue();
2148   params.gtColorRZ = anAxisData[2].Color.red();
2149   params.gtColorGZ = anAxisData[2].Color.green();
2150   params.gtColorBZ = anAxisData[2].Color.blue();
2151   params.gtDrawTickmarksX = anAxisData[0].DrawTickmarks;
2152   params.gtDrawTickmarksY = anAxisData[1].DrawTickmarks;
2153   params.gtDrawTickmarksZ = anAxisData[2].DrawTickmarks;
2154   params.gtTickmarkLengthX = anAxisData[0].TickmarkLength;
2155   params.gtTickmarkLengthY = anAxisData[1].TickmarkLength;
2156   params.gtTickmarkLengthZ = anAxisData[2].TickmarkLength;
2157 #endif
2158
2159   return params;
2160 }
2161
2162 /*!
2163   \brief Get visual parameters of this view window.
2164   \return visual parameters of view window
2165 */
2166 QString OCCViewer_ViewWindow::getVisualParameters()
2167 {
2168   viewAspect params = getViewParams();
2169
2170   QStringList data;
2171
2172   data << QString( "scale=%1" )    .arg( params.scale,   0, 'e', 12 );
2173 #if OCC_VERSION_LARGE <= 0x06070100 // the property is deprecated after OCCT 6.7.1
2174   data << QString( "centerX=%1" )  .arg( params.centerX, 0, 'e', 12 );
2175   data << QString( "centerY=%1" )  .arg( params.centerY, 0, 'e', 12 );
2176 #endif
2177   data << QString( "projX=%1" )    .arg( params.projX,   0, 'e', 12 );
2178   data << QString( "projY=%1" )    .arg( params.projY,   0, 'e', 12 );
2179   data << QString( "projZ=%1" )    .arg( params.projZ,   0, 'e', 12 );
2180   data << QString( "twist=%1" )    .arg( params.twist,   0, 'e', 12 );
2181   data << QString( "atX=%1" )      .arg( params.atX,     0, 'e', 12 );
2182   data << QString( "atY=%1" )      .arg( params.atY,     0, 'e', 12 );
2183   data << QString( "atZ=%1" )      .arg( params.atZ,     0, 'e', 12 );
2184   data << QString( "eyeX=%1" )     .arg( params.eyeX,    0, 'e', 12 );
2185   data << QString( "eyeY=%1" )     .arg( params.eyeY,    0, 'e', 12 );
2186   data << QString( "eyeZ=%1" )     .arg( params.eyeZ,    0, 'e', 12 );
2187   data << QString( "scaleX=%1" )   .arg( params.scaleX,  0, 'e', 12 );
2188   data << QString( "scaleY=%1" )   .arg( params.scaleY,  0, 'e', 12 );
2189   data << QString( "scaleZ=%1" )   .arg( params.scaleZ,  0, 'e', 12 );
2190   data << QString( "isVisible=%1" ).arg( params.isVisible );
2191   data << QString( "size=%1" )     .arg( params.size,    0, 'f',  2 );
2192
2193   ClipPlanesList aPlanes =  myModel->getClipPlanes();
2194   for ( int i=0; i < aPlanes.size(); i++ )
2195   {
2196     OCCViewer_ClipPlane& aPlane = aPlanes[i];
2197     QString ClippingPlane = QString( "ClippingPlane%1=").arg( i+1 );
2198     ClippingPlane +=  QString( "Mode~%1;").arg( (int)aPlane.Mode );
2199     ClippingPlane +=  QString( "IsActive~%1;").arg( aPlane.IsOn );
2200     switch ( aPlane.Mode )
2201     {
2202       case OCCViewer_ClipPlane::Absolute :
2203       {
2204         ClippingPlane += QString( "AbsoluteOrientation~%1;" ).arg( aPlane.OrientationType );
2205
2206         if ( aPlane.OrientationType == OCCViewer_ClipPlane::AbsoluteCustom )
2207         {
2208           ClippingPlane += QString( "Dx~%1;" ).arg( aPlane.AbsoluteOrientation.Dx );
2209           ClippingPlane += QString( "Dy~%1;" ).arg( aPlane.AbsoluteOrientation.Dy );
2210           ClippingPlane += QString( "Dz~%1;" ).arg( aPlane.AbsoluteOrientation.Dz );
2211         }
2212         else
2213         {
2214           ClippingPlane += QString( "IsInvert~%1;" ).arg( aPlane.AbsoluteOrientation.IsInvert );
2215         }
2216       }
2217       break;
2218
2219       case OCCViewer_ClipPlane::Relative :
2220       {
2221         ClippingPlane += QString( "RelativeOrientation~%1;" ).arg( aPlane.OrientationType );
2222         ClippingPlane += QString( "Rotation1~%1;" ).arg( aPlane.RelativeOrientation.Rotation1 );
2223         ClippingPlane += QString( "Rotation2~%1" ).arg( aPlane.RelativeOrientation.Rotation2 );
2224       }
2225       break;
2226     }
2227
2228     ClippingPlane +=  QString( "X~%1;" ).arg( aPlane.X );
2229     ClippingPlane +=  QString( "Y~%1;" ).arg( aPlane.Y );
2230     ClippingPlane +=  QString( "Z~%1;" ).arg( aPlane.Z );
2231     data << ClippingPlane;
2232   }
2233
2234
2235 #if OCC_VERSION_LARGE > 0x06030009 // available only with OCC-6.3-sp10 or newer version
2236   // graduated trihedron
2237   data << QString( "gtIsVisible=%1" )      .arg( params.gtIsVisible );
2238   data << QString( "gtDrawNameX=%1" )      .arg( params.gtDrawNameX );
2239   data << QString( "gtDrawNameY=%1" )      .arg( params.gtDrawNameY );
2240   data << QString( "gtDrawNameZ=%1" )      .arg( params.gtDrawNameZ );
2241   data << QString( "gtNameX=%1" )          .arg( params.gtNameX );
2242   data << QString( "gtNameY=%1" )          .arg( params.gtNameY );
2243   data << QString( "gtNameZ=%1" )          .arg( params.gtNameZ );
2244   data << QString( "gtNameColorRX=%1" )    .arg( params.gtNameColorRX );
2245   data << QString( "gtNameColorGX=%1" )    .arg( params.gtNameColorGX );
2246   data << QString( "gtNameColorBX=%1" )    .arg( params.gtNameColorBX );
2247   data << QString( "gtNameColorRY=%1" )    .arg( params.gtNameColorRY );
2248   data << QString( "gtNameColorGY=%1" )    .arg( params.gtNameColorGY );
2249   data << QString( "gtNameColorBY=%1" )    .arg( params.gtNameColorBY );
2250   data << QString( "gtNameColorRZ=%1" )    .arg( params.gtNameColorRZ );
2251   data << QString( "gtNameColorGZ=%1" )    .arg( params.gtNameColorGZ );
2252   data << QString( "gtNameColorBZ=%1" )    .arg( params.gtNameColorBZ );
2253   data << QString( "gtDrawValuesX=%1" )    .arg( params.gtDrawValuesX );
2254   data << QString( "gtDrawValuesY=%1" )    .arg( params.gtDrawValuesY );
2255   data << QString( "gtDrawValuesZ=%1" )    .arg( params.gtDrawValuesZ );
2256   data << QString( "gtNbValuesX=%1" )      .arg( params.gtNbValuesX );
2257   data << QString( "gtNbValuesY=%1" )      .arg( params.gtNbValuesY );
2258   data << QString( "gtNbValuesZ=%1" )      .arg( params.gtNbValuesZ );
2259   data << QString( "gtOffsetX=%1" )        .arg( params.gtOffsetX );
2260   data << QString( "gtOffsetY=%1" )        .arg( params.gtOffsetY );
2261   data << QString( "gtOffsetZ=%1" )        .arg( params.gtOffsetZ );
2262   data << QString( "gtColorRX=%1" )        .arg( params.gtColorRX );
2263   data << QString( "gtColorGX=%1" )        .arg( params.gtColorGX );
2264   data << QString( "gtColorBX=%1" )        .arg( params.gtColorBX );
2265   data << QString( "gtColorRY=%1" )        .arg( params.gtColorRY );
2266   data << QString( "gtColorGY=%1" )        .arg( params.gtColorGY );
2267   data << QString( "gtColorBY=%1" )        .arg( params.gtColorBY );
2268   data << QString( "gtColorRZ=%1" )        .arg( params.gtColorRZ );
2269   data << QString( "gtColorGZ=%1" )        .arg( params.gtColorGZ );
2270   data << QString( "gtColorBZ=%1" )        .arg( params.gtColorBZ );
2271   data << QString( "gtDrawTickmarksX=%1" ) .arg( params.gtDrawTickmarksX );
2272   data << QString( "gtDrawTickmarksY=%1" ) .arg( params.gtDrawTickmarksY );
2273   data << QString( "gtDrawTickmarksZ=%1" ) .arg( params.gtDrawTickmarksZ );
2274   data << QString( "gtTickmarkLengthX=%1" ).arg( params.gtTickmarkLengthX );
2275   data << QString( "gtTickmarkLengthY=%1" ).arg( params.gtTickmarkLengthY );
2276   data << QString( "gtTickmarkLengthZ=%1" ).arg( params.gtTickmarkLengthZ );
2277 #endif
2278   QString bg = Qtx::backgroundToString( background() ).replace( "=", "$" );
2279   data << QString( "background=%1" ).arg( bg );
2280
2281   return data.join("*");
2282 }
2283
2284 /*!
2285   \brief Restore visual parameters of the view window.
2286   \param parameters visual parameters of view window
2287 */
2288 void OCCViewer_ViewWindow::setVisualParameters( const QString& parameters )
2289 {
2290   viewAspect params;
2291   ClipPlanesList aClipPlanes;
2292   QStringList data = parameters.split( '*' );
2293   Qtx::BackgroundData bgData;
2294   if ( parameters.contains( '=' )  ) // new format - "scale=1.000e+00*centerX=0.000e+00..."
2295   {
2296     foreach( QString param, data ) {
2297       QString paramName  = param.section( '=', 0, 0 ).trimmed();
2298       QString paramValue = param.section( '=', 1, 1 ).trimmed();
2299       if      ( paramName == "scale" )             params.scale             = paramValue.toDouble();
2300       else if ( paramName == "centerX" )           params.centerX           = paramValue.toDouble();
2301       else if ( paramName == "centerY" )           params.centerY           = paramValue.toDouble();
2302       else if ( paramName == "projX" )             params.projX             = paramValue.toDouble();
2303       else if ( paramName == "projY" )             params.projY             = paramValue.toDouble();
2304       else if ( paramName == "projZ" )             params.projZ             = paramValue.toDouble();
2305       else if ( paramName == "twist" )             params.twist             = paramValue.toDouble();
2306       else if ( paramName == "atX" )               params.atX               = paramValue.toDouble();
2307       else if ( paramName == "atY" )               params.atY               = paramValue.toDouble();
2308       else if ( paramName == "atZ" )               params.atZ               = paramValue.toDouble();
2309       else if ( paramName == "eyeX" )              params.eyeX              = paramValue.toDouble();
2310       else if ( paramName == "eyeY" )              params.eyeY              = paramValue.toDouble();
2311       else if ( paramName == "eyeZ" )              params.eyeZ              = paramValue.toDouble();
2312       else if ( paramName == "scaleX" )            params.scaleX            = paramValue.toDouble();
2313       else if ( paramName == "scaleY" )            params.scaleY            = paramValue.toDouble();
2314       else if ( paramName == "scaleZ" )            params.scaleZ            = paramValue.toDouble();
2315       else if ( paramName == "isVisible" )         params.isVisible         = paramValue.toInt();
2316       else if ( paramName == "size" )              params.size              = paramValue.toDouble();
2317       else if ( paramName.contains( "ClippingPlane" ) )
2318       {
2319         QStringList ClipPlaneData = paramValue.split( ';' );
2320         OCCViewer_ClipPlane aPlane;
2321         foreach( QString ClipPlaneParam, ClipPlaneData )
2322         {
2323           QString ClipPlane_paramName  = ClipPlaneParam.section( '~', 0, 0 ).trimmed();
2324           QString ClipPlane_paramValue = ClipPlaneParam.section( '~', 1, 1 ).trimmed();
2325           if ( ClipPlane_paramName == "Mode" )
2326           {
2327             aPlane.Mode = ( OCCViewer_ClipPlane::PlaneMode ) ClipPlane_paramValue.toInt();
2328           }
2329           else if ( ClipPlane_paramName == "IsActive" ) aPlane.IsOn = ClipPlane_paramValue.toInt();
2330           else if ( ClipPlane_paramName == "X" )        aPlane.X    = ClipPlane_paramValue.toDouble();
2331           else if ( ClipPlane_paramName == "Y" )        aPlane.Y    = ClipPlane_paramValue.toDouble();
2332           else if ( ClipPlane_paramName == "Z" )        aPlane.Z    = ClipPlane_paramValue.toDouble();
2333           else
2334           {
2335             switch ( aPlane.Mode )
2336             {
2337               case OCCViewer_ClipPlane::Absolute :
2338                 if      ( ClipPlane_paramName == "Dx" ) aPlane.AbsoluteOrientation.Dx = ClipPlane_paramValue.toDouble();
2339                 else if ( ClipPlane_paramName == "Dy" ) aPlane.AbsoluteOrientation.Dy = ClipPlane_paramValue.toDouble();
2340                 else if ( ClipPlane_paramName == "Dz" ) aPlane.AbsoluteOrientation.Dz = ClipPlane_paramValue.toDouble();
2341                 else if ( ClipPlane_paramName == "IsInvert" ) aPlane.AbsoluteOrientation.IsInvert = ClipPlane_paramValue.toInt();
2342                 else if ( ClipPlane_paramName == "AbsoluteOrientation" ) aPlane.OrientationType = ClipPlane_paramValue.toInt();
2343                 break;
2344
2345               case OCCViewer_ClipPlane::Relative :
2346                 if      ( ClipPlane_paramName == "RelativeOrientation" ) aPlane.OrientationType = ClipPlane_paramValue.toInt();
2347                 else if ( ClipPlane_paramName == "Rotation1" )           aPlane.RelativeOrientation.Rotation1 = ClipPlane_paramValue.toDouble();
2348                 else if ( ClipPlane_paramName == "Rotation2" )           aPlane.RelativeOrientation.Rotation2 = ClipPlane_paramValue.toDouble();
2349                 break;
2350             }
2351           }
2352         }
2353         aClipPlanes.push_back(aPlane);
2354       }
2355       // graduated trihedron
2356       else if ( paramName == "gtIsVisible" )       params.gtIsVisible       = paramValue.toInt();
2357       else if ( paramName == "gtDrawNameX" )       params.gtDrawNameX       = paramValue.toInt();
2358       else if ( paramName == "gtDrawNameY" )       params.gtDrawNameY       = paramValue.toInt();
2359       else if ( paramName == "gtDrawNameZ" )       params.gtDrawNameZ       = paramValue.toInt();
2360       else if ( paramName == "gtNameX" )           params.gtNameX           = paramValue;
2361       else if ( paramName == "gtNameY" )           params.gtNameY           = paramValue;
2362       else if ( paramName == "gtNameZ" )           params.gtNameZ           = paramValue;
2363       else if ( paramName == "gtNameColorRX" )     params.gtNameColorRX     = paramValue.toInt();
2364       else if ( paramName == "gtNameColorGX" )     params.gtNameColorGX     = paramValue.toInt();
2365       else if ( paramName == "gtNameColorBX" )     params.gtNameColorBX     = paramValue.toInt();
2366       else if ( paramName == "gtNameColorRY" )     params.gtNameColorRY     = paramValue.toInt();
2367       else if ( paramName == "gtNameColorGY" )     params.gtNameColorGY     = paramValue.toInt();
2368       else if ( paramName == "gtNameColorBY" )     params.gtNameColorBY     = paramValue.toInt();
2369       else if ( paramName == "gtNameColorRZ" )     params.gtNameColorRZ     = paramValue.toInt();
2370       else if ( paramName == "gtNameColorGZ" )     params.gtNameColorGZ     = paramValue.toInt();
2371       else if ( paramName == "gtNameColorBZ" )     params.gtNameColorBZ     = paramValue.toInt();
2372       else if ( paramName == "gtDrawValuesX" )     params.gtDrawValuesX     = paramValue.toInt();
2373       else if ( paramName == "gtDrawValuesY" )     params.gtDrawValuesY     = paramValue.toInt();
2374       else if ( paramName == "gtDrawValuesZ" )     params.gtDrawValuesZ     = paramValue.toInt();
2375       else if ( paramName == "gtNbValuesX" )       params.gtNbValuesX       = paramValue.toInt();
2376       else if ( paramName == "gtNbValuesY" )       params.gtNbValuesY       = paramValue.toInt();
2377       else if ( paramName == "gtNbValuesZ" )       params.gtNbValuesZ       = paramValue.toInt();
2378       else if ( paramName == "gtOffsetX" )         params.gtOffsetX         = paramValue.toInt();
2379       else if ( paramName == "gtOffsetY" )         params.gtOffsetY         = paramValue.toInt();
2380       else if ( paramName == "gtOffsetZ" )         params.gtOffsetZ         = paramValue.toInt();
2381       else if ( paramName == "gtColorRX" )         params.gtColorRX         = paramValue.toInt();
2382       else if ( paramName == "gtColorGX" )         params.gtColorGX         = paramValue.toInt();
2383       else if ( paramName == "gtColorBX" )         params.gtColorBX         = paramValue.toInt();
2384       else if ( paramName == "gtColorRY" )         params.gtColorRY         = paramValue.toInt();
2385       else if ( paramName == "gtColorGY" )         params.gtColorGY         = paramValue.toInt();
2386       else if ( paramName == "gtColorBY" )         params.gtColorBY         = paramValue.toInt();
2387       else if ( paramName == "gtColorRZ" )         params.gtColorRZ         = paramValue.toInt();
2388       else if ( paramName == "gtColorGZ" )         params.gtColorGZ         = paramValue.toInt();
2389       else if ( paramName == "gtColorBZ" )         params.gtColorBZ         = paramValue.toInt();
2390       else if ( paramName == "gtDrawTickmarksX" )  params.gtDrawTickmarksX  = paramValue.toInt();
2391       else if ( paramName == "gtDrawTickmarksY" )  params.gtDrawTickmarksY  = paramValue.toInt();
2392       else if ( paramName == "gtDrawTickmarksZ" )  params.gtDrawTickmarksZ  = paramValue.toInt();
2393       else if ( paramName == "gtTickmarkLengthX" ) params.gtTickmarkLengthX = paramValue.toInt();
2394       else if ( paramName == "gtTickmarkLengthY" ) params.gtTickmarkLengthY = paramValue.toInt();
2395       else if ( paramName == "gtTickmarkLengthZ" ) params.gtTickmarkLengthZ = paramValue.toInt();
2396       else if ( paramName == "background" )        {
2397   QString bg = paramValue.replace( "$", "=" );
2398   bgData = Qtx::stringToBackground( bg );
2399       }
2400     }
2401   }
2402   else // old format - "1.000e+00*0.000e+00..."
2403   {
2404     int idx = 0;
2405     params.scale     = data.count() > idx ? data[idx++].toDouble() : 1.0;
2406     params.centerX   = data.count() > idx ? data[idx++].toDouble() : 0.0;
2407     params.centerY   = data.count() > idx ? data[idx++].toDouble() : 0.0;
2408     params.projX     = data.count() > idx ? data[idx++].toDouble() : sqrt(1./3);
2409     params.projY     = data.count() > idx ? data[idx++].toDouble() : -sqrt(1./3);
2410     params.projZ     = data.count() > idx ? data[idx++].toDouble() : sqrt(1./3);
2411     params.twist     = data.count() > idx ? data[idx++].toDouble() : 0.0;
2412     params.atX       = data.count() > idx ? data[idx++].toDouble() : 0.0;
2413     params.atY       = data.count() > idx ? data[idx++].toDouble() : 0.0;
2414     params.atZ       = data.count() > idx ? data[idx++].toDouble() : 0.0;
2415     params.eyeX      = data.count() > idx ? data[idx++].toDouble() : sqrt(250000./3);
2416     params.eyeY      = data.count() > idx ? data[idx++].toDouble() : -sqrt(250000./3);
2417     params.eyeZ      = data.count() > idx ? data[idx++].toDouble() : sqrt(250000./3);
2418     params.scaleX    = data.count() > idx ? data[idx++].toDouble() : 1.0;
2419     params.scaleY    = data.count() > idx ? data[idx++].toDouble() : 1.0;
2420     params.scaleZ    = data.count() > idx ? data[idx++].toDouble() : 1.0;
2421     params.isVisible = data.count() > idx ? data[idx++].toInt()    : 1;
2422     params.size      = data.count() > idx ? data[idx++].toDouble() : 100.0;
2423   }
2424   performRestoring( params );  
2425   setBackground( bgData );
2426   myModel->setClipPlanes(aClipPlanes);
2427 }
2428
2429 /*!
2430   \brief Handle show event.
2431
2432   Emits Show() signal.
2433
2434   \param theEvent show event
2435 */
2436 void OCCViewer_ViewWindow::showEvent( QShowEvent* theEvent )
2437 {
2438   emit Show( theEvent );
2439 }
2440
2441 /*!
2442   \brief Handle hide event.
2443
2444   Emits Hide() signal.
2445
2446   \param theEvent hide event
2447 */
2448 void OCCViewer_ViewWindow::hideEvent( QHideEvent* theEvent )
2449 {
2450   emit Hide( theEvent );
2451 }
2452
2453
2454 /*!
2455     Creates default sketcher. [ virtual protected ]
2456 */
2457 OCCViewer_ViewSketcher* OCCViewer_ViewWindow::createSketcher( int type )
2458 {
2459   if ( type == Rect )
2460     return new OCCViewer_RectSketcher( this, type );
2461   if ( type == Polygon )
2462     return new OCCViewer_PolygonSketcher( this, type );
2463   return 0;
2464 }
2465
2466 void OCCViewer_ViewWindow::initSketchers()
2467 {
2468   if ( mySketchers.isEmpty() )
2469   {
2470     mySketchers.append( createSketcher( Rect ) );
2471     mySketchers.append( createSketcher( Polygon ) );
2472   }
2473 }
2474
2475 OCCViewer_ViewSketcher* OCCViewer_ViewWindow::getSketcher( const int typ )
2476 {
2477   OCCViewer_ViewSketcher* sketcher = 0;
2478   QList<OCCViewer_ViewSketcher*>::Iterator it;
2479   for ( it = mySketchers.begin(); it != mySketchers.end() && !sketcher; ++it )
2480   {
2481     OCCViewer_ViewSketcher* sk = (*it);
2482     if ( sk->type() == typ )
2483       sketcher = sk;
2484   }
2485   return sketcher;
2486 }
2487
2488 /*!
2489     Handles requests for sketching in the active view. [ virtual public ]
2490 */
2491 void OCCViewer_ViewWindow::activateSketching( int type )
2492 {
2493   OCCViewer_ViewPort3d* vp = getViewPort();
2494   if ( !vp )
2495     return;
2496
2497   if ( !vp->isSketchingEnabled() )
2498     return;
2499
2500   /* Finish current sketching */
2501   if ( type == NoSketching )
2502   {
2503     if ( mypSketcher )
2504     {
2505       onSketchingFinished();
2506       mypSketcher->deactivate();
2507       mypSketcher = 0;
2508     }
2509   }
2510   /* Activate new sketching */
2511   else
2512   {
2513     activateSketching( NoSketching );  /* concurrency not suported */
2514     mypSketcher = getSketcher( type );
2515     if ( mypSketcher )
2516     {
2517       mypSketcher->activate();
2518       onSketchingStarted();
2519     }
2520   }
2521 }
2522
2523 /*!
2524     Unhilights detected entities. [ virtual protected ]
2525 */
2526 void OCCViewer_ViewWindow::onSketchingStarted()
2527 {
2528 }
2529
2530 /*!
2531     Selection by rectangle or polygon. [ virtual protected ]
2532 */
2533 void OCCViewer_ViewWindow::onSketchingFinished()
2534 {
2535   MESSAGE("OCCViewer_ViewWindow::onSketchingFinished()")
2536   if ( mypSketcher && mypSketcher->result() == OCCViewer_ViewSketcher::Accept )
2537   {
2538     Handle(AIS_InteractiveContext) ic = myModel->getAISContext();
2539     bool append = bool( mypSketcher->buttonState() && mypSketcher->isHasShift() );
2540     switch( mypSketcher->type() )
2541     {
2542     case Rect:
2543       {
2544         QRect* aRect = (QRect*)mypSketcher->data();
2545         if( aRect )
2546         {
2547           int aLeft = aRect->left();
2548           int aRight = aRect->right();
2549           int aTop = aRect->top();
2550           int aBottom = aRect->bottom();
2551 //           myRect = aRect;
2552
2553           if( append )
2554             ic->ShiftSelect( aLeft, aBottom, aRight, aTop, getViewPort()->getView(), Standard_False );
2555           else
2556             ic->Select( aLeft, aBottom, aRight, aTop, getViewPort()->getView(), Standard_False );
2557         }
2558       }
2559       break;
2560     case Polygon:
2561       {
2562         QPolygon* aPolygon = (QPolygon*)mypSketcher->data();
2563         if( aPolygon )
2564         {
2565           int size = aPolygon->size();
2566           TColgp_Array1OfPnt2d anArray( 1, size );
2567
2568           QPolygon::Iterator it = aPolygon->begin();
2569           QPolygon::Iterator itEnd = aPolygon->end();
2570           for( int index = 1; it != itEnd; ++it, index++ )
2571           {
2572             QPoint aPoint = *it;
2573             anArray.SetValue( index, gp_Pnt2d( aPoint.x(), aPoint.y() ) );
2574           }
2575
2576           if( append )
2577             ic->ShiftSelect( anArray, getViewPort()->getView(), Standard_False );
2578           else
2579             ic->Select( anArray, getViewPort()->getView(), Standard_False );
2580         }
2581       }
2582       break;
2583     default:
2584       break;
2585     }
2586
2587     OCCViewer_ViewManager* aViewMgr = ( OCCViewer_ViewManager* )getViewManager();
2588     aViewMgr->getOCCViewer()->performSelectionChanged();
2589   }
2590 }
2591
2592 OCCViewer_ViewPort3d* OCCViewer_ViewWindow::getViewPort()
2593 {
2594   return myViewPort;
2595 }
2596
2597 bool OCCViewer_ViewWindow::transformRequested() const
2598 {
2599   return ( myOperation != NOTHING );
2600 }
2601
2602 bool OCCViewer_ViewWindow::transformInProcess() const
2603 {
2604   return myEventStarted;
2605 }
2606
2607 void OCCViewer_ViewWindow::setTransformInProcess( bool bOn )
2608 {
2609   myEventStarted = bOn;
2610 }
2611
2612 /*!
2613   Set enabled state of transformation (rotate, zoom, etc)
2614 */
2615 void OCCViewer_ViewWindow::setTransformEnabled( const OperationType id, const bool on )
2616 {
2617   if ( id != NOTHING ) myStatus.insert( id, on );
2618 }
2619
2620 /*!
2621   \return enabled state of transformation (rotate, zoom, etc)
2622 */
2623 bool OCCViewer_ViewWindow::transformEnabled( const OperationType id ) const
2624 {
2625   return myStatus.contains( id ) ? myStatus[ id ] : true;
2626 }
2627
2628 void OCCViewer_ViewWindow::onMaximizedView()
2629 {
2630   setMaximized(!isMaximized());
2631 }
2632
2633 void OCCViewer_ViewWindow::returnTo3dView()
2634 {
2635   setReturnedTo3dView( true );
2636 }
2637
2638 void OCCViewer_ViewWindow::setReturnedTo3dView(bool isVisible3dView)
2639 {
2640   if ( !toolMgr()->action( ReturnTo3dViewId ) ||
2641     toolMgr()->isShown(ReturnTo3dViewId) != isVisible3dView ) return;
2642   if ( !isVisible3dView )
2643     toolMgr()->show( ReturnTo3dViewId );
2644   else
2645     toolMgr()->hide( ReturnTo3dViewId );
2646   if ( isVisible3dView ) emit returnedTo3d( );
2647 }
2648
2649
2650 void OCCViewer_ViewWindow::setMaximized(bool toMaximize, bool toSendSignal)
2651 {
2652   QAction* anAction =  toolMgr()->action( MaximizedId );
2653   QAction* anAction2 =  toolMgr()->action( ReturnTo3dViewId );
2654   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
2655   if ( toMaximize ) {
2656     anAction->setText( tr( "MNU_MINIMIZE_VIEW" ) );  
2657     anAction->setToolTip( tr( "MNU_MINIMIZE_VIEW" ) );  
2658     anAction->setIcon( aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_MINIMIZE" ) ) );
2659     anAction->setStatusTip( tr( "DSC_MINIMIZE_VIEW" ) );
2660     if ( anAction2 && my2dMode != No2dMode ) toolMgr()->show( ReturnTo3dViewId );
2661     if (toSendSignal) {
2662       emit maximized( this, true );
2663     }
2664   }
2665   else {
2666     anAction->setText( tr( "MNU_MAXIMIZE_VIEW" ) );  
2667     anAction->setToolTip( tr( "MNU_MAXIMIZE_VIEW" ) );  
2668     anAction->setIcon( aResMgr->loadPixmap( "OCCViewer", tr( "ICON_OCCVIEWER_MAXIMIZE" ) ) );
2669     anAction->setStatusTip( tr( "DSC_MAXIMIZE_VIEW" ) );
2670     if ( anAction2 && my2dMode != No2dMode ) toolMgr()->hide( ReturnTo3dViewId );
2671     if (toSendSignal) {
2672       emit maximized( this, false );
2673     }
2674   }
2675 }
2676
2677 bool OCCViewer_ViewWindow::isMaximized() const
2678 {
2679   return !(toolMgr()->action( MaximizedId )->text() == tr( "MNU_MAXIMIZE_VIEW" ));
2680 }
2681
2682 void OCCViewer_ViewWindow::setSketcherStyle( bool enable )
2683
2684   IsSketcherStyle = enable; 
2685 }
2686
2687 bool OCCViewer_ViewWindow::isSketcherStyle() const 
2688
2689   return IsSketcherStyle; 
2690 }
2691
2692
2693 void OCCViewer_ViewWindow::set2dMode(Mode2dType theType)
2694 {
2695   my2dMode = theType;
2696 }
2697
2698 // obsolete   
2699 QColor OCCViewer_ViewWindow::backgroundColor() const
2700 {
2701   return myViewPort ? myViewPort->backgroundColor() : Qt::black;
2702 }
2703    
2704 // obsolete
2705 void OCCViewer_ViewWindow::setBackgroundColor( const QColor& theColor )
2706 {
2707   if ( myViewPort ) myViewPort->setBackgroundColor( theColor );
2708 }
2709
2710 Qtx::BackgroundData OCCViewer_ViewWindow::background() const
2711 {
2712   return myViewPort ? myViewPort->background() : Qtx::BackgroundData();
2713 }
2714    
2715 void OCCViewer_ViewWindow::setBackground( const Qtx::BackgroundData& theBackground )
2716 {
2717   if ( myViewPort ) myViewPort->setBackground( theBackground );
2718 }
2719
2720 void OCCViewer_ViewWindow::showStaticTrihedron( bool on )
2721 {
2722   if ( myViewPort ) myViewPort->showStaticTrihedron( on );
2723 }
2724
2725 /*!
2726   Clears view aspects
2727 */
2728 void OCCViewer_ViewWindow::clearViewAspects()
2729 {
2730   myViewAspects.clear();
2731 }
2732
2733 /*!
2734   \return const reference to list of view aspects
2735 */
2736 const viewAspectList& OCCViewer_ViewWindow::getViewAspects()
2737 {
2738   return myViewAspects;
2739 }
2740
2741 /*!
2742   Appends new view aspect
2743   \param aParams - new view aspects
2744 */
2745 void OCCViewer_ViewWindow::appendViewAspect( const viewAspect& aParams )
2746 {
2747   myViewAspects.append( aParams );
2748 }
2749
2750 /*!
2751   Replaces old view aspects by new ones
2752   \param aViewList - list of new view aspects
2753 */
2754 void OCCViewer_ViewWindow::updateViewAspects( const viewAspectList& aViewList )
2755 {
2756   myViewAspects = aViewList;
2757 }
2758
2759 /*!
2760   Get camera properties for the OCC view window.
2761   \return shared pointer on camera properties.
2762 */
2763 SUIT_CameraProperties OCCViewer_ViewWindow::cameraProperties()
2764 {
2765   SUIT_CameraProperties aProps;
2766
2767   Handle(V3d_View) aSourceView = getViewPort()->getView();
2768   if ( aSourceView.IsNull() )
2769     return aProps;
2770
2771   if ( get2dMode() == No2dMode ) {
2772     aProps.setDimension( SUIT_CameraProperties::Dim3D );
2773   }
2774   else {
2775     aProps.setDimension( SUIT_CameraProperties::Dim2D );
2776     aProps.setViewSide( (SUIT_CameraProperties::ViewSide)(int)get2dMode() );
2777   }
2778
2779   // read common properites of the view
2780   Standard_Real anUp[3];
2781   Standard_Real anAt[3];
2782   Standard_Real anEye[3];
2783   Standard_Real aProj[3];
2784   Standard_Real anAxialScale[3];
2785
2786   aSourceView->Up( anUp[0], anUp[1], anUp[2] );
2787   aSourceView->At( anAt[0], anAt[1], anAt[2] );
2788   aSourceView->Proj( aProj[0], aProj[1], aProj[2] );
2789   getViewPort()->getAxialScale( anAxialScale[0], anAxialScale[1], anAxialScale[2] );
2790
2791   aProps.setAxialScale( anAxialScale[0], anAxialScale[1], anAxialScale[2] );
2792   aProps.setViewUp( anUp[0], anUp[1], anUp[2] );
2793
2794 #if OCC_VERSION_LARGE > 0x06070100
2795   aSourceView->Eye( anEye[0], anEye[1], anEye[2] );
2796
2797   // store camera properties "as is": it is up to synchronized
2798   // view classes to provide necessary property conversion.
2799   aProps.setPosition( anEye[0], anEye[1], anEye[2] );
2800   aProps.setFocalPoint( anAt[0], anAt[1], anAt[2] );
2801
2802   if ( aSourceView->Camera()->IsOrthographic() )
2803   {
2804     aProps.setProjection( SUIT_CameraProperties::PrjOrthogonal );
2805     aProps.setViewAngle( 0.0 );
2806   }
2807   else
2808   {
2809     aProps.setProjection( SUIT_CameraProperties::PrjPerspective );
2810     aProps.setViewAngle( aSourceView->Camera()->FOVy() );
2811   }
2812   aProps.setMappingScale( aSourceView->Camera()->Scale() );
2813 #else
2814   Standard_Real aCameraDepth = aSourceView->Depth() + aSourceView->ZSize() * 0.5;
2815
2816   // generate view orientation matrix for transforming OCC projection reference point
2817   // into a camera (eye) position.
2818   gp_Dir aLeftDir = gp_Dir( anUp[0], anUp[1], anUp[2] ) ^ gp_Dir( aProj[0], aProj[1], aProj[2] );
2819
2820   gp_GTrsf aTrsf;
2821   aTrsf.SetValue( 1, 1, aLeftDir.X() );
2822   aTrsf.SetValue( 2, 1, aLeftDir.Y() );
2823   aTrsf.SetValue( 3, 1, aLeftDir.Z() );
2824
2825   aTrsf.SetValue( 1, 2, anUp[0] );
2826   aTrsf.SetValue( 2, 2, anUp[1] );
2827   aTrsf.SetValue( 3, 2, anUp[2] );
2828
2829   aTrsf.SetValue( 1, 3, aProj[0] );
2830   aTrsf.SetValue( 2, 3, aProj[1] );
2831   aTrsf.SetValue( 3, 3, aProj[2] );
2832
2833   aTrsf.SetValue( 1, 4, anAt[0] );
2834   aTrsf.SetValue( 2, 4, anAt[1] );
2835   aTrsf.SetValue( 3, 4, anAt[2] );
2836
2837   Graphic3d_Vertex aProjRef = aSourceView->ViewMapping().ProjectionReferencePoint();
2838
2839   // transform to world-space coordinate system
2840   gp_XYZ aPosition( aProjRef.X(), aProjRef.Y(), aCameraDepth );
2841   aTrsf.Transforms( aPosition );
2842
2843   // compute focal point
2844   double aFocalPoint[3];
2845
2846   aFocalPoint[0] = aPosition.X() - aProj[0] * aCameraDepth;
2847   aFocalPoint[1] = aPosition.Y() - aProj[1] * aCameraDepth;
2848   aFocalPoint[2] = aPosition.Z() - aProj[2] * aCameraDepth;
2849
2850   aProps.setFocalPoint( aFocalPoint[0], aFocalPoint[1], aFocalPoint[2] );
2851   aProps.setPosition( aPosition.X(), aPosition.Y(), aPosition.Z() );
2852
2853   Standard_Real aViewScale[2];
2854   aSourceView->Size( aViewScale[0], aViewScale[1] );
2855   aProps.setMappingScale( aViewScale[1] );
2856 #endif
2857
2858   return aProps;
2859 }
2860
2861 /*!
2862   Synchronize views.
2863   This implementation synchronizes OCC view's camera propreties.
2864 */
2865 void OCCViewer_ViewWindow::synchronize( SUIT_ViewWindow* theView )
2866 {
2867   bool blocked = blockSignals( true );
2868
2869   SUIT_CameraProperties aProps = theView->cameraProperties();
2870   if ( !cameraProperties().isCompatible( aProps ) ) {
2871     // other view, this one is being currently synchronized to, seems has become incompatible
2872     // we have to break synchronization
2873     updateSyncViews();
2874     return;
2875   }
2876
2877   Handle(V3d_View) aDestView = getViewPort()->getView();
2878
2879   aDestView->SetImmediateUpdate( Standard_False );
2880
2881   double anUpDir[3];
2882   double aPosition[3];
2883   double aFocalPoint[3];
2884   double anAxialScale[3];
2885
2886   // get common properties
2887   aProps.getFocalPoint( aFocalPoint[0], aFocalPoint[1], aFocalPoint[2] );
2888   aProps.getPosition( aPosition[0], aPosition[1], aPosition[2] );
2889   aProps.getViewUp( anUpDir[0], anUpDir[1], anUpDir[2] );
2890   aProps.getAxialScale( anAxialScale[0], anAxialScale[1], anAxialScale[2] );
2891
2892 #if OCC_VERSION_LARGE > 0x06070100
2893   aDestView->SetAt( aFocalPoint[0], aFocalPoint[1], aFocalPoint[2] );
2894   aDestView->SetEye( aPosition[0], aPosition[1], aPosition[2] );
2895   aDestView->SetUp( anUpDir[0], anUpDir[1], anUpDir[2] );
2896   aDestView->Camera()->SetScale( aProps.getMappingScale() );
2897 #else
2898   gp_Dir aProjDir( aPosition[0] - aFocalPoint[0],
2899                    aPosition[1] - aFocalPoint[1],
2900                    aPosition[2] - aFocalPoint[2] );
2901
2902   // get custom view translation
2903   Standard_Real aTranslation[3];
2904   aDestView->At( aTranslation[0], aTranslation[1], aTranslation[2] );
2905
2906   gp_Dir aLeftDir = gp_Dir( anUpDir[0], anUpDir[1], anUpDir[2] )
2907                   ^ gp_Dir( aProjDir.X(), aProjDir.Y(), aProjDir.Z() );
2908
2909   gp_GTrsf aTrsf;
2910   aTrsf.SetValue( 1, 1, aLeftDir.X() );
2911   aTrsf.SetValue( 2, 1, aLeftDir.Y() );
2912   aTrsf.SetValue( 3, 1, aLeftDir.Z() );
2913
2914   aTrsf.SetValue( 1, 2, anUpDir[0] );
2915   aTrsf.SetValue( 2, 2, anUpDir[1] );
2916   aTrsf.SetValue( 3, 2, anUpDir[2] );
2917
2918   aTrsf.SetValue( 1, 3, aProjDir.X() );
2919   aTrsf.SetValue( 2, 3, aProjDir.Y() );
2920   aTrsf.SetValue( 3, 3, aProjDir.Z() );
2921
2922   aTrsf.SetValue( 1, 4, aTranslation[0] );
2923   aTrsf.SetValue( 2, 4, aTranslation[1] );
2924   aTrsf.SetValue( 3, 4, aTranslation[2] );
2925   aTrsf.Invert();
2926
2927   // transform to view-space coordinate system
2928   gp_XYZ aProjRef( aPosition[0], aPosition[1], aPosition[2] );
2929   aTrsf.Transforms( aProjRef );
2930
2931   // set view camera properties using low-level approach. this is done
2932   // in order to avoid interference with static variables in v3d view used
2933   // when rotation is in process in another view.
2934   Visual3d_ViewMapping aMapping = aDestView->View()->ViewMapping();
2935   Visual3d_ViewOrientation anOrientation = aDestView->View()->ViewOrientation();
2936
2937   Graphic3d_Vector aMappingProj( aProjDir.X(), aProjDir.Y(), aProjDir.Z() );
2938   Graphic3d_Vector aMappingUp( anUpDir[0], anUpDir[1], anUpDir[2] );
2939
2940   aMappingProj.Normalize();
2941   aMappingUp.Normalize();
2942
2943   anOrientation.SetViewReferencePlane( aMappingProj );
2944   anOrientation.SetViewReferenceUp( aMappingUp );
2945
2946   aDestView->SetViewMapping( aMapping );
2947   aDestView->SetViewOrientation( anOrientation );
2948
2949   // set panning
2950   aDestView->SetCenter( aProjRef.X(), aProjRef.Y() );
2951
2952   // set mapping scale
2953   double aMapScaling = aProps.getMappingScale();
2954   Standard_Real aWidth, aHeight;
2955   aDestView->Size( aWidth, aHeight );
2956   aDestView->SetSize ( aWidth > aHeight ? aMapScaling * (aWidth / aHeight) : aMapScaling );
2957 #endif
2958
2959   getViewPort()->setAxialScale( anAxialScale[0], anAxialScale[1], anAxialScale[2] );
2960
2961   aDestView->ZFitAll();
2962   aDestView->SetImmediateUpdate( Standard_True );
2963   aDestView->Redraw();
2964
2965   blockSignals( blocked );
2966 }
2967
2968 /*!
2969   \brief Indicates whether preselection is enabled
2970   \return true if preselection is enabled
2971 */
2972 bool OCCViewer_ViewWindow::isPreselectionEnabled() const
2973 {
2974   return myPreselectionEnabled;
2975 }
2976
2977 /*!
2978   \brief Enables/disables preselection
2979   \param theIsToEnable if true - preselection will be enabled
2980 */
2981 void OCCViewer_ViewWindow::enablePreselection( bool theIsToEnable )
2982 {
2983   onSwitchPreselection( theIsToEnable );
2984 }
2985
2986 /*!
2987   \brief Indicates whether selection is enabled
2988   \return true if selection is enabled
2989 */
2990 bool OCCViewer_ViewWindow::isSelectionEnabled() const
2991 {
2992   return mySelectionEnabled;
2993 }
2994
2995 /*!
2996   \brief Enables/disables selection
2997   \param theIsToEnable if true - selection will be enabled
2998 */
2999 void OCCViewer_ViewWindow::enableSelection( bool theIsToEnable )
3000 {
3001   onSwitchSelection( theIsToEnable );
3002 }
3003
3004
3005 /*!
3006   \brief called if clipping operation is activated / deactivated.
3007
3008   Enables/disables clipping plane displaying.
3009
3010   \parma on action state
3011 */
3012 void OCCViewer_ViewWindow::onClipping (bool theIsOn)
3013 {
3014   if(!myModel) return;
3015   OCCViewer_ClippingDlg* aClippingDlg = myModel->getClippingDlg();
3016   
3017   if (theIsOn) {
3018     if (!aClippingDlg) {
3019       aClippingDlg = new OCCViewer_ClippingDlg (this, myModel);
3020       myModel->setClippingDlg(aClippingDlg);
3021     }
3022     if (!aClippingDlg->isVisible())
3023       aClippingDlg->show();
3024   } else {
3025     if ( aClippingDlg ) {
3026       aClippingDlg->close();
3027       myModel->setClippingDlg(0);
3028     }
3029   }
3030
3031   SUIT_ViewManager* mgr = getViewManager();
3032   if( mgr ) {
3033     QVector<SUIT_ViewWindow*> aViews = mgr->getViews();
3034     for(int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
3035       if(SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
3036         QtxActionToolMgr* mgr = aViewWindow->toolMgr();
3037         if(!mgr) continue;
3038         QAction* a = toolMgr()->action( ClippingId );
3039         if(!a) continue;
3040         if(theIsOn != a->isChecked()){
3041           disconnect (a, SIGNAL (toggled (bool)), aViewWindow, SLOT (onClipping (bool)));
3042           a->setChecked(theIsOn);
3043           connect (a, SIGNAL (toggled (bool)), aViewWindow, SLOT (onClipping (bool)));
3044         }
3045       }
3046     }
3047   }
3048 }