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