Salome HOME
Profile object realization. OCC Viewer embeded into the profile dialog box.
[modules/hydro.git] / src / HYDROCurveCreator / OCCViewer_ViewWidget.cxx
1
2 #include "OCCViewer_ViewWidget.h"
3
4 #include <SUIT_Desktop.h>
5 #include <SUIT_Session.h>
6 #include <SUIT_ResourceMgr.h>
7 #include <SUIT_MessageBox.h>
8 #include <OCCViewer_ViewPort3d.h>
9 #include <OCCViewer_VService.h>
10 #include <SUIT_ViewModel.h>
11 #include <SUIT_Tools.h>
12
13 #include <QtxAction.h>
14
15 #include <AIS_InteractiveObject.hxx>
16 #include <AIS_ListOfInteractive.hxx>
17 #include <V3d_Viewer.hxx>
18 #include <AIS_Shape.hxx>
19 #include <Prs3d_LineAspect.hxx>
20
21 #include <AIS_InteractiveContext.hxx>
22 #include <AIS_ListIteratorOfListOfInteractive.hxx>
23 #include <Geom_Axis2Placement.hxx>
24 #include <AIS_Drawer.hxx>
25 #include <Prs3d_DatumAspect.hxx>
26
27 #include <QLayout>
28 #include <QCursor>
29 #include <QHBoxLayout>
30 #include <QPushButton>
31 #include <QPainter>
32 #include <QLabel>
33 #include <QWheelEvent>
34 #include <QMouseEvent>
35 #include <QToolBar>
36
37 const int SPACING_SIZE = 5;
38 const int Z_FIT_ALL_SIZE = 1000;
39
40 const char* imageZoomCursor[] = { 
41 "32 32 3 1",
42 ". c None",
43 "a c #000000",
44 "# c #ffffff",
45 "................................",
46 "................................",
47 ".#######........................",
48 "..aaaaaaa.......................",
49 "................................",
50 ".............#####..............",
51 "...........##.aaaa##............",
52 "..........#.aa.....a#...........",
53 ".........#.a.........#..........",
54 ".........#a..........#a.........",
55 "........#.a...........#.........",
56 "........#a............#a........",
57 "........#a............#a........",
58 "........#a............#a........",
59 "........#a............#a........",
60 ".........#...........#.a........",
61 ".........#a..........#a.........",
62 ".........##.........#.a.........",
63 "........#####.....##.a..........",
64 ".......###aaa#####.aa...........",
65 "......###aa...aaaaa.......#.....",
66 ".....###aa................#a....",
67 "....###aa.................#a....",
68 "...###aa...............#######..",
69 "....#aa.................aa#aaaa.",
70 ".....a....................#a....",
71 "..........................#a....",
72 "...........................a....",
73 "................................",
74 "................................",
75 "................................",
76 "................................"};
77
78 const char* imageRotateCursor[] = { 
79 "32 32 3 1",
80 ". c None",
81 "a c #000000",
82 "# c #ffffff",
83 "................................",
84 "................................",
85 "................................",
86 "................................",
87 "........#.......................",
88 ".......#.a......................",
89 "......#######...................",
90 ".......#aaaaa#####..............",
91 "........#..##.a#aa##........##..",
92 ".........a#.aa..#..a#.....##.aa.",
93 ".........#.a.....#...#..##.aa...",
94 ".........#a.......#..###.aa.....",
95 "........#.a.......#a..#aa.......",
96 "........#a.........#..#a........",
97 "........#a.........#a.#a........",
98 "........#a.........#a.#a........",
99 "........#a.........#a.#a........",
100 ".........#.........#a#.a........",
101 "........##a........#a#a.........",
102 "......##.a#.......#.#.a.........",
103 "....##.aa..##.....##.a..........",
104 "..##.aa.....a#####.aa...........",
105 "...aa.........aaa#a.............",
106 "................#.a.............",
107 "...............#.a..............",
108 "..............#.a...............",
109 "...............a................",
110 "................................",
111 "................................",
112 "................................",
113 "................................",
114 "................................"};
115
116 const char* imageCrossCursor[] = { 
117   "32 32 3 1",
118   ". c None",
119   "a c #000000",
120   "# c #ffffff",
121   "................................",
122   "................................",
123   "................................",
124   "................................",
125   "................................",
126   "................................",
127   "................................",
128   "...............#................",
129   "...............#a...............",
130   "...............#a...............",
131   "...............#a...............",
132   "...............#a...............",
133   "...............#a...............",
134   "...............#a...............",
135   "...............#a...............",
136   ".......#################........",
137   "........aaaaaaa#aaaaaaaaa.......",
138   "...............#a...............",
139   "...............#a...............",
140   "...............#a...............",
141   "...............#a...............",
142   "...............#a...............",
143   "...............#a...............",
144   "...............#a...............",
145   "................a...............",
146   "................................",
147   "................................",
148   "................................",
149   "................................",
150   "................................",
151   "................................",
152   "................................"};
153
154 // ---------------- OCC view widget --------
155 OCCViewer_ViewWidget::OCCViewer_ViewWidget(QWidget* parent)
156   : QFrame(parent), myShowTrihedron(true)
157 {
158   setObjectName("OCC_view_widget");
159   mySelectedPoint = gp_Pnt(0.,0.,0.);
160   setFrameStyle(QFrame::NoFrame);
161
162   QGridLayout* anAnalLay = new QGridLayout(this);
163   anAnalLay->setMargin(0);
164   anAnalLay->setSpacing(SPACING_SIZE);
165
166   QWidget* aBtnBox = new QWidget(this);
167   QHBoxLayout* aBtnLay = new QHBoxLayout(aBtnBox);
168   aBtnLay->setMargin(0);
169   aBtnLay->setSpacing(SPACING_SIZE);
170
171   myToolBar = new QToolBar(aBtnBox);
172   aBtnLay->addWidget(myToolBar);
173   myZoomBtns = new QtxMultiAction(aBtnBox);
174   myZoomBtns->setObjectName("zoomBtn");
175   myToolBar->addAction(myZoomBtns);
176   myPanBtns = new QtxMultiAction(aBtnBox);
177   myPanBtns->setObjectName("panBtn");
178   myToolBar->addAction(myPanBtns);
179   myProjBtns = new QtxMultiAction(aBtnBox);
180   myProjBtns->setObjectName("projectionBtn");
181   myToolBar->addAction(myProjBtns);
182
183   // Rotation
184   aBtnLay->addStretch(1);
185   anAnalLay->addWidget(aBtnBox, 0, 0, 1, 2);
186
187   myV3dViewer = OCCViewer_VService::CreateViewer((short*) "Viewer3d", "", "", 1000., V3d_XposYnegZpos, true, true);
188   //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
189
190 #if OCC_VERSION_LARGE <= 0x06060000 // Porting to OCCT higher 6.6.0 version
191   myV3dCollector = OCCViewer_VService::CreateViewer((short*) "Collector3d", "", "", 1000., V3d_XposYnegZpos, true, true);
192   myV3dCollector->Init();
193 #endif
194
195   // init selector
196 #if OCC_VERSION_LARGE <= 0x06060000 
197   myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector );
198 #else
199   myAISContext = new AIS_InteractiveContext( myV3dViewer );
200 #endif
201   myAISContext->SelectionColor(Quantity_NOC_WHITE);
202   myAISContext->IsoOnPlane(true);
203
204   myViewPort = new OCCViewer_ViewPort3d(this, myV3dViewer, V3d_ORTHOGRAPHIC);
205   myViewPort->setBackgroundColor(Qt::black);
206   myViewPort->setMinimumHeight(300);
207   myViewPort->installEventFilter(this);
208   anAnalLay->addWidget(myViewPort, 1, 0, 1, 2);
209
210   Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
211   myTrihedron = new AIS_Trihedron(anAxis);
212   myTrihedron->SetInfiniteState(Standard_True);
213
214   Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
215   myTrihedron->SetArrowColor(Col.Name());
216   myTrihedron->SetSize(100);
217   Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
218   if (drawer->HasDatumAspect()) {
219       Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
220       daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
221       daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
222       daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
223   }
224   myAISContext->Display(myTrihedron);
225   myAISContext->Deactivate(myTrihedron);
226   
227   myTrihedron->SetTransformPersistence(Graphic3d_TMF_TriedronPers, gp_Pnt(-1, -1, 200));
228   myTrihedron->SetSize(100);
229
230   createActions();
231   myButsMap[TrihId]->setChecked(false);
232   onTrihChanged();
233 }
234
235 OCCViewer_ViewWidget::~OCCViewer_ViewWidget()
236 {
237   delete myViewPort;
238
239   myAISContext.Nullify();
240   myV3dViewer.Nullify();
241   #if OCC_VERSION_LARGE <= 0x06060000
242     myV3dCollector.Nullify();
243   #endif
244 }
245
246 void OCCViewer_ViewWidget::reset()
247 {
248   clearViewer();
249   myOperation = NOTHING;
250   myCurrPointType = OCCViewer_ViewWindow::GRAVITY;
251 }
252
253 void OCCViewer_ViewWidget::Display(const TopoDS_Shape shape, const bool theShaded,
254                                    const QColor& shapeColor)
255 {
256   clearViewer(false);
257   if (shape.IsNull()) {
258     myAISContext->UpdateCurrentViewer();
259     return;
260   }
261
262   Handle(AIS_InteractiveObject) io = new AIS_Shape(shape);
263   io->UnsetSelectionMode();
264   io->UnsetHilightMode();
265
266   Quantity_Color aColor(shapeColor.red()/255., shapeColor.green()/255.,
267                         shapeColor.blue()/255., Quantity_TOC_RGB);
268   io->SetColor(aColor);
269
270   myAISContext->Display(io, false);
271   if (theShaded) {
272     myAISContext->SetDisplayMode(io, AIS_Shaded, false);
273     myAISContext->SetMaterial(io, Graphic3d_NOM_PLASTIC, false);
274   }
275   else
276     myAISContext->SetDisplayMode(io, AIS_WireFrame, false);
277   viewerFitAll(false);
278   myAISContext->UpdateCurrentViewer();
279 }
280
281 void OCCViewer_ViewWidget::createActions()
282 {
283   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
284   QtxAction* aAction;
285
286   // FitAll
287   aAction = new QtxAction(tr("MNU_FITALL"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_FITALL")),
288                            tr("MNU_FITALL"), 0, this);
289   aAction->setStatusTip(tr("DSC_FITALL"));
290   connect(aAction, SIGNAL(activated()), this, SLOT(onActivated()));
291   myButsMap[ FitAllId ] = aAction;
292
293   // FitRect
294   aAction = new QtxAction(tr("MNU_FITRECT"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_FITAREA")),
295                            tr("MNU_FITRECT"), 0, this);
296   aAction->setStatusTip(tr("DSC_FITRECT"));
297   connect(aAction, SIGNAL(activated()), this, SLOT(onActivated()));
298   myButsMap[ FitRectId ] = aAction;
299
300   // Zoom
301   aAction = new QtxAction(tr("MNU_ZOOM_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_ZOOM")),
302                            tr("MNU_ZOOM_VIEW"), 0, this);
303   aAction->setStatusTip(tr("DSC_ZOOM_VIEW"));
304   connect(aAction, SIGNAL(activated()), this, SLOT(onActivated()));
305   myButsMap[ ZoomId ] = aAction;
306
307   // Panning
308   aAction = new QtxAction(tr("MNU_PAN_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_PAN")),
309                            tr("MNU_PAN_VIEW"), 0, this);
310   aAction->setStatusTip(tr("DSC_PAN_VIEW"));
311   connect(aAction, SIGNAL(activated()), this, SLOT(onActivated()));
312   myButsMap[ PanId ] = aAction;
313
314   // Global Panning
315   aAction = new QtxAction(tr("MNU_GLOBALPAN_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_GLOBALPAN")),
316                            tr("MNU_GLOBALPAN_VIEW"), 0, this);
317   aAction->setStatusTip(tr("DSC_GLOBALPAN_VIEW"));
318   connect(aAction, SIGNAL(activated()), this, SLOT(onActivated()));
319   myButsMap[ GlobalPanId ] = aAction;
320
321   // Projections
322   aAction = new QtxAction(tr("MNU_FRONT_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_FRONT")),
323                            tr("MNU_FRONT_VIEW"), 0, this);
324   aAction->setStatusTip(tr("DSC_FRONT_VIEW"));
325   connect(aAction, SIGNAL(activated()), this, SLOT(onFrontView()));
326   myButsMap[ FrontId ] = aAction;
327
328   aAction = new QtxAction(tr("MNU_BACK_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_BACK")),
329                            tr("MNU_BACK_VIEW"), 0, this);
330   aAction->setStatusTip(tr("DSC_BACK_VIEW"));
331   connect(aAction, SIGNAL(activated()), this, SLOT(onBackView()));
332   myButsMap[ BackId ] = aAction;
333
334   aAction = new QtxAction(tr("MNU_TOP_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_TOP")),
335                            tr("MNU_TOP_VIEW"), 0, this);
336   aAction->setStatusTip(tr("DSC_TOP_VIEW"));
337   connect(aAction, SIGNAL(activated()), this, SLOT(onTopView()));
338   myButsMap[ TopId ] = aAction;
339
340   aAction = new QtxAction(tr("MNU_BOTTOM_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_BOTTOM")),
341                            tr("MNU_BOTTOM_VIEW"), 0, this);
342   aAction->setStatusTip(tr("DSC_BOTTOM_VIEW"));
343   connect(aAction, SIGNAL(activated()), this, SLOT(onBottomView()));
344   myButsMap[ BottomId ] = aAction;
345
346   aAction = new QtxAction(tr("MNU_LEFT_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_LEFT")),
347                            tr("MNU_LEFT_VIEW"), 0, this);
348   aAction->setStatusTip(tr("DSC_LEFT_VIEW"));
349   connect(aAction, SIGNAL(activated()), this, SLOT(onLeftView()));
350   myButsMap[ LeftId ] = aAction;
351
352   aAction = new QtxAction(tr("MNU_RIGHT_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_RIGHT")),
353                            tr("MNU_RIGHT_VIEW"), 0, this);
354   aAction->setStatusTip(tr("DSC_RIGHT_VIEW"));
355   connect(aAction, SIGNAL(activated()), this, SLOT(onRightView()));
356   myButsMap[ RightId ] = aAction;
357
358   aAction = new QtxAction(tr("MNU_ROTATE_VIEW"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_ROTATE")),
359                           tr("MNU_ROTATE_VIEW"), 0, this);
360   aAction->setStatusTip(tr("DSC_ROTATE_VIEW"));
361   connect(aAction, SIGNAL(triggered()), this, SLOT(onActivated()));
362   myToolBar->addAction(aAction);
363   myButsMap[ RotationId ] = aAction;
364
365   aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRE"), aResMgr->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_TRIHEDRON")),
366                           tr("MNU_SHOW_TRIHEDRE"), 0, this);
367   aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRE"));
368   aAction->setCheckable(true);
369   connect(aAction, SIGNAL(triggered()), this, SLOT(onTrihChanged()));
370   myToolBar->addAction(aAction);
371   myButsMap[ TrihId ] = aAction;
372
373   myZoomBtns->insertAction(myButsMap[ FitAllId ]);
374   myZoomBtns->insertAction(myButsMap[ FitRectId ]);
375   myZoomBtns->insertAction(myButsMap[ ZoomId ]);
376
377   myPanBtns->insertAction(myButsMap[ PanId ]);
378   myPanBtns->insertAction(myButsMap[ GlobalPanId ]);
379
380   myProjBtns->insertAction(myButsMap[ FrontId ]);
381   myProjBtns->insertAction(myButsMap[ BackId ]);
382   myProjBtns->insertAction(myButsMap[ TopId ]);
383   myProjBtns->insertAction(myButsMap[ BottomId ]);
384   myProjBtns->insertAction(myButsMap[ LeftId ]);
385   myProjBtns->insertAction(myButsMap[ RightId ]);
386 }
387
388 /*!
389   Custom event handler
390 */
391 bool OCCViewer_ViewWidget::eventFilter(QObject* watched, QEvent* e)
392 {
393   if (e->type() == QEvent::ContextMenu)
394     return true;
395
396   bool aRes = false;
397   int aReturn = -1;
398   if (watched == myViewPort) {
399     int aType = e->type();
400     switch(aType) {
401     case QEvent::MouseButtonPress:
402       vpMousePressEvent((QMouseEvent*) e);
403       aRes = true;
404       aReturn = 1;
405       break;
406     case QEvent::MouseButtonRelease:
407       vpMouseReleaseEvent((QMouseEvent*) e);
408       aRes = true;
409       aReturn = 1;
410       break;
411     case QEvent::MouseMove:
412       vpMouseMoveEvent((QMouseEvent*) e);
413       aRes = true;
414       aReturn = 1;
415       break;
416     case QEvent::Wheel:
417       {
418         QWheelEvent* aEvent = (QWheelEvent*) e;
419         double aDelta = aEvent->delta();
420         double aScale = (aDelta < 0) ? 100./(-aDelta) : aDelta/100.; 
421         myViewPort->getView()->SetZoom(aScale);
422       }
423       aRes = true;
424       aReturn = 1;
425       break;
426     default:
427       break;
428     }
429   }
430   if (aReturn == -1)
431     aRes = QFrame::eventFilter(watched, e);
432
433   if (watched == myViewPort && !myViewPort->getView().IsNull()) {
434     if (e->type() == QEvent::MouseButtonRelease)
435       viewZFitAll();
436   }
437
438   return aRes;
439 }
440
441 void OCCViewer_ViewWidget::onActivated()
442 {
443   if(!sender() || !sender()->inherits("QtxAction"))
444     return;
445   QtxAction* anAction = (QtxAction*)sender();
446   ActionsMap::const_iterator anIt = myButsMap.begin(), last = myButsMap.end();
447   int aCurAction = -1;
448   for (; anIt != last && aCurAction == -1; anIt++) {
449     if (anIt.value() != anAction)
450       continue;
451     aCurAction = anIt.key();
452   }
453   switch(aCurAction) {
454     case RotationId:  activateRotation(); return;
455     case FitAllId:    viewerFitAll();  break;
456     case FitRectId:   activateWindowFit();   break;
457     case ZoomId:      activateZoom();        break;
458     case PanId:       activatePanning();     break;
459     case GlobalPanId: activateGlPanning();   break;
460   }
461   viewZFitAll();
462 }
463
464 void OCCViewer_ViewWidget::viewerFitAll(const bool theUpdate)
465 {
466   myViewPort->fitAll(false, true, theUpdate);
467   viewZFitAll();
468 }
469
470 void OCCViewer_ViewWidget::onTrihChanged()
471 {
472   QtxAction* anAction = myButsMap[TrihId];
473   myShowTrihedron = anAction->isChecked();
474   if (myShowTrihedron) {
475     myAISContext->Display(myTrihedron);
476     myAISContext->Deactivate(myTrihedron);
477   }
478   else 
479     myAISContext->Erase(myTrihedron);
480 }
481 /*!
482   Processes transformation "front view"
483 */
484 void OCCViewer_ViewWidget::onFrontView()
485 {
486   Handle(V3d_View) aView3d = myViewPort->getView();
487   if (!aView3d.IsNull()) aView3d->SetProj (V3d_Xpos);
488   viewerFitAll();
489 }
490
491 /*!
492   Processes transformation "back view"
493 */
494 void OCCViewer_ViewWidget::onBackView()
495 {
496   Handle(V3d_View) aView3d = myViewPort->getView();
497   if (!aView3d.IsNull()) aView3d->SetProj (V3d_Xneg);
498   viewerFitAll();
499 }
500
501 /*!
502   Processes transformation "top view"
503 */
504 void OCCViewer_ViewWidget::onTopView()
505 {
506   Handle(V3d_View) aView3d = myViewPort->getView();
507   if (!aView3d.IsNull()) aView3d->SetProj (V3d_Zpos);
508   viewerFitAll();
509 }
510
511 /*!
512   Processes transformation "bottom view"
513 */
514 void OCCViewer_ViewWidget::onBottomView()
515 {
516   Handle(V3d_View) aView3d = myViewPort->getView();
517   if (!aView3d.IsNull()) aView3d->SetProj (V3d_Zneg);
518   viewerFitAll();
519 }
520
521 /*!
522   Processes transformation "left view"
523 */
524 void OCCViewer_ViewWidget::onLeftView()
525 {
526   Handle(V3d_View) aView3d = myViewPort->getView();
527   if (!aView3d.IsNull()) aView3d->SetProj (V3d_Yneg);
528   viewerFitAll();
529 }
530
531 /*!
532   Processes transformation "right view"
533 */
534 void OCCViewer_ViewWidget::onRightView()
535 {
536   Handle(V3d_View) aView3d = myViewPort->getView();
537   if (!aView3d.IsNull()) aView3d->SetProj (V3d_Ypos);
538   viewerFitAll();
539 }
540
541
542 void OCCViewer_ViewWidget::viewZFitAll()
543 {
544    myViewPort->getView()->ZFitAll(Z_FIT_ALL_SIZE);
545 }
546
547 void OCCViewer_ViewWidget::activateZoom()
548 {
549   if (!transformRequested() && !myCursorIsHand)
550     myCursor = cursor();          /* save old cursor */
551   
552   if (myOperation != ZOOMVIEW) {
553     QPixmap zoomPixmap (imageZoomCursor);
554     QCursor zoomCursor (zoomPixmap);
555     if(setTransformRequested (ZOOMVIEW))
556       setCursor(zoomCursor);
557   }
558 }
559
560 void OCCViewer_ViewWidget::activateWindowFit()
561 {
562   if (!transformRequested() && !myCursorIsHand)
563     myCursor = cursor();          /* save old cursor */
564
565   if (myOperation != WINDOWFIT) {
566     QCursor handCursor (Qt::PointingHandCursor);
567     if(setTransformRequested (WINDOWFIT))
568       setCursor (handCursor);
569     myCursorIsHand = true;
570   }
571 }
572
573 void OCCViewer_ViewWidget::activateRotation()
574 {
575   if (!transformRequested() && !myCursorIsHand)
576     myCursor = cursor();        // save old cursor 
577   
578   if (myOperation != ROTATE) {
579     QPixmap rotatePixmap (imageRotateCursor);
580     QCursor rotCursor (rotatePixmap);
581     if(setTransformRequested (ROTATE))
582       setCursor(rotCursor);
583   }
584 }
585
586 void OCCViewer_ViewWidget::activatePanning()
587 {
588   if (!transformRequested() && !myCursorIsHand)
589     myCursor = cursor();        // save old cursor 
590   
591   if (myOperation != PANVIEW) {
592     QCursor panCursor (Qt::SizeAllCursor);
593     if(setTransformRequested (PANVIEW))
594       setCursor(panCursor);
595   }
596 }
597
598 void OCCViewer_ViewWidget::activateGlPanning()
599 {
600   Handle(V3d_View) aView3d = myViewPort->getView();
601   if (!aView3d.IsNull()) {
602     QPixmap globalPanPixmap (imageCrossCursor);
603     QCursor glPanCursor (globalPanPixmap);
604     myCurScale = aView3d->Scale();
605     aView3d->FitAll(0.01, false);
606     myCursor = cursor();        // save old cursor 
607     myViewPort->fitAll(); // fits view before selecting a new scene center 
608     if(setTransformRequested(PANGLOBAL))
609       setCursor(glPanCursor);
610   }
611 }
612
613 void OCCViewer_ViewWidget::vpMousePressEvent(QMouseEvent* theEvent)
614 {
615   myStartX = theEvent->x();
616   myStartY = theEvent->y();
617   switch (myOperation) {
618     case WINDOWFIT:
619     case PANGLOBAL:
620     case ZOOMVIEW:
621     case PANVIEW:
622     break;
623     case ROTATE:
624     if (theEvent->button() == Qt::LeftButton) {
625             myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
626           }
627     break;
628     default:
629   /*  Try to activate a transformation */
630     switch (getButtonState(theEvent)) {
631       case ZOOMVIEW:
632         activateZoom();
633         break;
634       case PANVIEW:
635         activatePanning();
636         break;
637       case ROTATE:
638         activateRotation();
639         //myViewPort->startRotation(myStartX, myStartY);//, 0, mySelectedPoint);
640         break;
641       default:
642         break;
643     }
644   }
645 }
646 OCCViewer_ViewWidget::OperationType OCCViewer_ViewWidget::getButtonState(QMouseEvent* theEvent)
647 {
648   OperationType aOp = NOTHING;
649   if((theEvent->modifiers() == SUIT_ViewModel::myStateMap[SUIT_ViewModel::STANDARD][SUIT_ViewModel::ZOOM]) &&
650       (theEvent->button() == SUIT_ViewModel::myButtonMap[SUIT_ViewModel::STANDARD][SUIT_ViewModel::ZOOM]))
651     aOp = ZOOMVIEW;
652   else if((theEvent->modifiers() == SUIT_ViewModel::myStateMap[SUIT_ViewModel::STANDARD][SUIT_ViewModel::PAN]) && 
653            (theEvent->button() == SUIT_ViewModel::myButtonMap[SUIT_ViewModel::STANDARD][SUIT_ViewModel::PAN]))
654     aOp = PANVIEW;
655   else if((theEvent->modifiers()  == SUIT_ViewModel::myStateMap[SUIT_ViewModel::STANDARD][SUIT_ViewModel::ROTATE]) &&
656            (theEvent->button() == SUIT_ViewModel::myButtonMap[SUIT_ViewModel::STANDARD][SUIT_ViewModel::ROTATE]))
657     aOp = ROTATE;
658
659   return aOp;
660 }
661
662 void OCCViewer_ViewWidget::vpMouseReleaseEvent(QMouseEvent* theEvent)
663 {
664   switch (myOperation) {
665   case NOTHING:
666     break;
667   case ROTATE:
668     myViewPort->endRotation();
669     resetState();
670     break;
671   case PANVIEW:
672   case ZOOMVIEW:
673     resetState();
674     break;
675     
676   case PANGLOBAL:
677     if (theEvent->button() == Qt::LeftButton) {
678       myViewPort->setCenter(theEvent->x(), theEvent->y());
679       myViewPort->getView()->SetScale(myCurScale);
680       resetState();
681     }
682     break;
683       
684   case WINDOWFIT:
685     if (theEvent->modifiers() == Qt::LeftButton) {
686       myCurrX = theEvent->x();
687       myCurrY = theEvent->y();
688       QRect rect = SUIT_Tools::makeRect(myStartX, myStartY, myCurrX, myCurrY);
689       if (!rect.isEmpty()) myViewPort->fitRect(rect);
690         resetState();
691     }
692     break;
693   }
694   
695   // NOTE: viewer 3D detects a rectangle of selection using this event
696   // so we must emit it BEFORE resetting the selection rectangle
697
698   if (theEvent->button() == Qt::LeftButton && myDrawRect) {
699     myDrawRect = false;
700     drawRect();
701     resetState(); 
702     myViewPort->update();
703   }
704 }
705
706 void OCCViewer_ViewWidget::vpMouseMoveEvent(QMouseEvent* theEvent)
707 {
708   myCurrX = theEvent->x();
709   myCurrY = theEvent->y();
710   switch (myOperation) {
711   case ROTATE:
712     myViewPort->rotate(myCurrX, myCurrY, myCurrPointType, mySelectedPoint);
713     break;
714   case ZOOMVIEW:
715     myViewPort->zoom(myStartX, myStartY, myCurrX, myCurrY);
716     myStartX = myCurrX;
717     myStartY = myCurrY;
718     break;
719     
720   case PANVIEW:
721     myViewPort->pan(myCurrX - myStartX, myStartY - myCurrY);
722     myStartX = myCurrX;
723     myStartY = myCurrY;
724     break;
725     
726   case PANGLOBAL:
727     break;
728
729   default: {
730     int aState = theEvent->modifiers();
731     if (aState == Qt::LeftButton ||
732       aState == ((int) Qt::LeftButton | (int) Qt::ShiftModifier)) {
733         myDrawRect = true;
734         if (myDrawRect) {
735           drawRect();
736           if (!myCursorIsHand) {   // we are going to sketch a rectangle
737             QCursor handCursor (Qt::PointingHandCursor);
738             myCursorIsHand = true;
739             myCursor = cursor();
740             setCursor(handCursor);
741           }
742         }
743       }
744     }
745   }
746 }
747
748 /*!
749   Sets the viewport to its initial state
750   (no transformations in process etc.)
751 */
752 void OCCViewer_ViewWidget::resetState()
753 {
754   myDrawRect = false;
755   myRect.setLeft(2);
756   myRect.setRight(0);
757
758   /* make rectangle empty (left > right) */
759   if (transformRequested() || myCursorIsHand)
760     setCursor(myCursor);
761   myCursorIsHand = false;
762
763   setTransformRequested(NOTHING);
764 }
765
766 void OCCViewer_ViewWidget::drawRect()
767 {
768   QPainter aPainter(myViewPort);
769   aPainter.setCompositionMode(QPainter::CompositionMode_Xor);
770   aPainter.setPen(Qt::white);
771   QRect aRect = SUIT_Tools::makeRect(myStartX, myStartY, myCurrX, myCurrY);
772   if (!myRect.isEmpty())
773     aPainter.drawRect(myRect);
774   aPainter.drawRect(aRect);
775   myRect = aRect;
776 }
777
778 bool OCCViewer_ViewWidget::setTransformRequested (OperationType op)
779 {
780   myOperation = op;
781   myViewPort->setMouseTracking(myOperation == NOTHING);
782   return true;
783 }
784
785 void OCCViewer_ViewWidget::clearViewer(const bool theUpdate)
786 {
787   // check if trihedron is displayed
788   Standard_Boolean isTrihedronDisplayed = myAISContext->IsDisplayed(myTrihedron);
789
790   // get objects to be erased (all currently displayed objects)
791   AIS_ListOfInteractive aList;
792   myAISContext->DisplayedObjects(aList);
793   AIS_ListIteratorOfListOfInteractive anIter(aList);
794   for (; anIter.More(); anIter.Next()) {
795     if (isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron))
796       continue;
797     // erase an object
798     Handle(AIS_InteractiveObject) anIO = anIter.Value();
799 #if OCC_VERSION_LARGE <= 0x06060000 
800     myAISContext->Erase(anIO, false, false);
801 #else
802     myAISContext->Erase(anIO, false);
803 #endif
804   }
805
806   // display trihedron if necessary
807   if (isTrihedronDisplayed)
808     myAISContext->Display(myTrihedron, theUpdate);
809   else if (theUpdate)
810     myV3dViewer->Update();
811   if (theUpdate)
812     myAISContext->UpdateCurrentViewer();
813 }