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