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