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