Salome HOME
Auto-formatting according to the defined code standard.
[modules/shaper.git] / src / XGUI / XGUI_ViewWindow.cpp
1 #include "XGUI_ViewWindow.h"
2 #include "XGUI_ViewPort.h"
3 #include "XGUI_Viewer.h"
4 #include "XGUI_Tools.h"
5 #include "XGUI_RubberBand.h"
6
7 #include <QLayout>
8 #include <QLabel>
9 #include <QToolBar>
10 #include <QAction>
11 #include <QResizeEvent>
12 #include <QApplication>
13 #include <QMdiArea>
14 #include <QMdiSubWindow>
15 #include <QPainter>
16 #include <QTime>
17
18 #include <TopoDS_Shape.hxx>
19 #include <BRep_Tool.hxx>
20 #include <TopoDS.hxx>
21
22 #define BORDER_SIZE 2
23
24 const char* imageZoomCursor[] = { "32 32 3 1", ". c None", "a c #000000", "# c #ffffff",
25     "................................", "................................",
26     ".#######........................", "..aaaaaaa.......................",
27     "................................", ".............#####..............",
28     "...........##.aaaa##............", "..........#.aa.....a#...........",
29     ".........#.a.........#..........", ".........#a..........#a.........",
30     "........#.a...........#.........", "........#a............#a........",
31     "........#a............#a........", "........#a............#a........",
32     "........#a............#a........", ".........#...........#.a........",
33     ".........#a..........#a.........", ".........##.........#.a.........",
34     "........#####.....##.a..........", ".......###aaa#####.aa...........",
35     "......###aa...aaaaa.......#.....", ".....###aa................#a....",
36     "....###aa.................#a....", "...###aa...............#######..",
37     "....#aa.................aa#aaaa.", ".....a....................#a....",
38     "..........................#a....", "...........................a....",
39     "................................", "................................",
40     "................................", "................................" };
41
42 const char* imageRotateCursor[] = { "32 32 3 1", ". c None", "a c #000000", "# c #ffffff",
43     "................................", "................................",
44     "................................", "................................",
45     "........#.......................", ".......#.a......................",
46     "......#######...................", ".......#aaaaa#####..............",
47     "........#..##.a#aa##........##..", ".........a#.aa..#..a#.....##.aa.",
48     ".........#.a.....#...#..##.aa...", ".........#a.......#..###.aa.....",
49     "........#.a.......#a..#aa.......", "........#a.........#..#a........",
50     "........#a.........#a.#a........", "........#a.........#a.#a........",
51     "........#a.........#a.#a........", ".........#.........#a#.a........",
52     "........##a........#a#a.........", "......##.a#.......#.#.a.........",
53     "....##.aa..##.....##.a..........", "..##.aa.....a#####.aa...........",
54     "...aa.........aaa#a.............", "................#.a.............",
55     "...............#.a..............", "..............#.a...............",
56     "...............a................", "................................",
57     "................................", "................................",
58     "................................", "................................" };
59
60 const char* imageCrossCursor[] = { "32 32 3 1", ". c None", "a c #000000", "# c #ffffff",
61     "................................", "................................",
62     "................................", "................................",
63     "................................", "................................",
64     "................................", "...............#................",
65     "...............#a...............", "...............#a...............",
66     "...............#a...............", "...............#a...............",
67     "...............#a...............", "...............#a...............",
68     "...............#a...............", ".......#################........",
69     "........aaaaaaa#aaaaaaaaa.......", "...............#a...............",
70     "...............#a...............", "...............#a...............",
71     "...............#a...............", "...............#a...............",
72     "...............#a...............", "...............#a...............",
73     "................a...............", "................................",
74     "................................", "................................",
75     "................................", "................................",
76     "................................", "................................" };
77
78 //**************************************************************************
79 void ViewerToolbar::paintEvent(QPaintEvent* theEvent)
80 {
81   //QTime aTime;
82   //aTime.start();
83   QRect aRect = rect();
84   QRect aVPRect = myVPort->rect();
85   QPoint aGlobPnt = mapToGlobal(aRect.topLeft());
86   QPoint aPnt = myVPort->mapFromGlobal(aGlobPnt);
87
88   QRect aImgRect(
89       QRect(aPnt.x(), aPnt.y() + aVPRect.height() - aRect.height(), aRect.width(), aRect.height()));
90   QPainter(this).drawImage(aRect, myVPort->dumpView(aImgRect, false));
91   //QString aMsg = QString("### Painted in %1").arg(aTime.elapsed());
92   //qDebug(qPrintable(aMsg));
93 }
94
95 //**************************************************************************
96 void ViewerLabel::paintEvent(QPaintEvent* theEvent)
97 {
98   QRect aRect = rect();
99   QRect aVPRect = myVPort->rect();
100   QPoint aGlobPnt = mapToGlobal(aRect.topLeft());
101   QPoint aPnt = myVPort->mapFromGlobal(aGlobPnt);
102
103   QRect aImgRect(
104       QRect(aPnt.x(), aPnt.y() + aVPRect.height() - aRect.height(), aRect.width(), aRect.height()));
105   QPainter(this).drawImage(aRect, myVPort->dumpView(aImgRect, false));
106   QLabel::paintEvent(theEvent);
107 }
108
109 //**************************************************************************
110 //**************************************************************************
111 //**************************************************************************
112 XGUI_ViewWindow::XGUI_ViewWindow(XGUI_Viewer* theViewer, V3d_TypeOfView theType)
113     : QFrame(), myViewer(theViewer), myMoving(false), MinimizeIco(":pictures/wnd_minimize.png"), MaximizeIco(
114         ":pictures/wnd_maximize.png"), CloseIco(":pictures/wnd_close.png"), RestoreIco(
115         ":pictures/wnd_restore.png"), myInteractionStyle(XGUI::STANDARD), myRectBand(0), myIsKeyFree(
116         false), my2dMode(XGUI::No2dMode), myCurrPointType(XGUI::GRAVITY), myPrevPointType(
117         XGUI::GRAVITY), myRotationPointSelection(false)
118 {
119   mySelectedPoint = gp_Pnt(0., 0., 0.);
120   setFrameStyle(QFrame::Raised);
121   setFrameShape(QFrame::Panel);
122   setLineWidth(BORDER_SIZE);
123   setMouseTracking(true);
124
125   QVBoxLayout* aLay = new QVBoxLayout(this);
126   aLay->setContentsMargins(BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE);
127   myViewPort = new XGUI_ViewPort(this, myViewer->v3dViewer(), theType);
128   myViewPort->installEventFilter(this);
129   aLay->addWidget(myViewPort);
130
131   myPicture = new QLabel();
132   myPicture->setFrameStyle(QFrame::Sunken);
133   myPicture->setFrameShape(QFrame::Panel);
134   myPicture->setMouseTracking(true);
135   myPicture->installEventFilter(this);
136   myPicture->hide();
137
138   QStringList aPictures;
139   aPictures << ":pictures/occ_view_camera_dump.png" << ":pictures/occ_view_style_switch.png";
140   aPictures << ":pictures/occ_view_triedre.png" << ":pictures/occ_view_fitall.png";
141   aPictures << ":pictures/occ_view_fitarea.png" << ":pictures/occ_view_zoom.png";
142   aPictures << ":pictures/occ_view_pan.png" << ":pictures/occ_view_glpan.png";
143   aPictures << ":pictures/occ_view_rotate.png" << ":pictures/occ_view_front.png";
144   aPictures << ":pictures/occ_view_back.png" << ":pictures/occ_view_left.png";
145   aPictures << ":pictures/occ_view_right.png" << ":pictures/occ_view_top.png";
146   aPictures << ":pictures/occ_view_bottom.png" << ":pictures/occ_view_clone.png";
147
148   QStringList aTitles;
149   aTitles << "Dump view" << "Mouse style switch" << "Show trihedron" << "Fit all";
150   aTitles << "Fit area" << "Zoom" << "Panning" << "Global panning" << "Rotate";
151   aTitles << "Front" << "Back" << "Left" << "Right" << "Top" << "Bottom" << "Clone view";
152
153   myGripWgt = new ViewerLabel(this, myViewPort);
154   myGripWgt->setPixmap(QPixmap(":pictures/wnd_grip.png"));
155   myGripWgt->setGeometry(BORDER_SIZE + 2, BORDER_SIZE + 2, 19, 32);
156   myGripWgt->setMouseTracking(true);
157   myGripWgt->installEventFilter(this);
158   connect(myViewPort, SIGNAL(vpTransformed()), myGripWgt, SLOT(update()));
159   connect(myViewPort, SIGNAL(vpUpdated()), myGripWgt, SLOT(update()));
160
161   myViewBar = new ViewerToolbar(this, myViewPort);
162
163   QAction* aBtn;
164   for(int i = 0; i < aTitles.length(); i++) {
165     aBtn = new QAction(QIcon(aPictures.at(i)), aTitles.at(i), myViewBar);
166     myViewBar->addAction(aBtn);
167   }
168   connect(myViewPort, SIGNAL(vpTransformed()), myViewBar, SLOT(update()));
169   connect(myViewPort, SIGNAL(vpUpdated()), myViewBar, SLOT(update()));
170
171   myWindowBar = new ViewerToolbar(this, myViewPort);
172   connect(myViewPort, SIGNAL(vpTransformed()), myWindowBar, SLOT(update()));
173   connect(myViewPort, SIGNAL(vpUpdated()), myWindowBar, SLOT(update()));
174
175   myMinimizeBtn = new QAction(myWindowBar);
176   myMinimizeBtn->setIcon(MinimizeIco);
177   myWindowBar->addAction(myMinimizeBtn);
178   connect(myMinimizeBtn, SIGNAL(triggered()), SLOT(onMinimize()));
179
180   myMaximizeBtn = new QAction(myWindowBar);
181   myMaximizeBtn->setIcon(MaximizeIco);
182   myWindowBar->addAction(myMaximizeBtn);
183   connect(myMaximizeBtn, SIGNAL(triggered()), SLOT(onMaximize()));
184
185   aBtn = new QAction(myWindowBar);
186   aBtn->setIcon(CloseIco);
187   myWindowBar->addAction(aBtn);
188   connect(aBtn, SIGNAL(triggered()), SLOT(onClose()));
189
190   myViewBar->hide();
191   myWindowBar->hide();
192   myGripWgt->hide();
193 }
194
195 //****************************************************************
196 XGUI_ViewWindow::~XGUI_ViewWindow()
197 {
198 }
199
200 //****************************************************************
201 void XGUI_ViewWindow::resizeEvent(QResizeEvent* theEvent)
202 {
203   QSize aSize = theEvent->size();
204   QSize aWndBarSize = myWindowBar->sizeHint();
205   QSize myViewBarSize = myViewBar->sizeHint();
206
207   myWindowBar->move(aSize.width() - aWndBarSize.width() - BORDER_SIZE - 4,
208   BORDER_SIZE + 2);
209   int aViewBarWidth = aSize.width() - aWndBarSize.width() - myGripWgt->width() - 8;
210   if (aViewBarWidth > myViewBarSize.width())
211     aViewBarWidth = myViewBarSize.width();
212   myViewBar->setGeometry(BORDER_SIZE + 18, BORDER_SIZE + 2, aViewBarWidth, myViewBarSize.height());
213 }
214
215 //****************************************************************
216 void XGUI_ViewWindow::changeEvent(QEvent* theEvent)
217 {
218
219   if (theEvent->type() == QEvent::WindowStateChange) {
220     if (isMinimized()) {
221       if (!myPicture->parentWidget()) {
222         QMdiSubWindow* aParent = static_cast<QMdiSubWindow*>(parentWidget());
223         QMdiArea* aMDIArea = aParent->mdiArea();
224         myPicture->setParent(aMDIArea);
225       }
226       myPicture->move(parentWidget()->x(), parentWidget()->y());
227       myPicture->show();
228     } else {
229       myPicture->hide();
230       if (isMaximized()) {
231         myMinimizeBtn->setIcon(MinimizeIco);
232         myMaximizeBtn->setIcon(RestoreIco);
233       }
234     }
235   } else
236     QWidget::changeEvent(theEvent);
237 }
238
239 //****************************************************************
240 void XGUI_ViewWindow::onClose()
241 {
242   if (parentWidget())
243     parentWidget()->close();
244
245 }
246
247 //****************************************************************
248 void XGUI_ViewWindow::enterEvent(QEvent* theEvent)
249 {
250   if (!isMinimized()) {
251     myViewBar->show();
252     if (!isMaximized())
253       myGripWgt->show();
254   }
255   myWindowBar->show();
256 }
257
258 //****************************************************************
259 void XGUI_ViewWindow::leaveEvent(QEvent* theEvent)
260 {
261   myViewBar->hide();
262   myGripWgt->hide();
263   myWindowBar->hide();
264 }
265
266 //****************************************************************
267 void XGUI_ViewWindow::onMinimize()
268 {
269   QPixmap aPMap = QPixmap::fromImage(myViewPort->dumpView());
270   int aW = width();
271   int aH = height();
272   double aR = aW / 100.;
273   myPicture->setPixmap(aPMap.scaled(100, int(aH / aR)));
274
275   myLastState = isMaximized() ? MaximizedState : NormalState;
276   showMinimized();
277 }
278
279 //****************************************************************
280 void XGUI_ViewWindow::onMaximize()
281 {
282   if (isMaximized()) {
283     myMaximizeBtn->setIcon(MaximizeIco);
284     myGripWgt->show();
285     showNormal();
286   } else {
287     myMaximizeBtn->setIcon(RestoreIco);
288     myGripWgt->hide();
289     showMaximized();
290   }
291   myMinimizeBtn->setIcon(MinimizeIco);
292 }
293
294 //****************************************************************
295 bool XGUI_ViewWindow::processWindowControls(QObject *theObj, QEvent *theEvent)
296 {
297   QWidget* aWgt = (theObj == myPicture) ? myPicture : static_cast<QWidget*>(parentWidget());
298   switch(theEvent->type()) {
299   case QEvent::MouseButtonPress: {
300     QMouseEvent* aEvent = static_cast<QMouseEvent*>(theEvent);
301     if ((aEvent->button() == Qt::LeftButton) && (!myMoving)) {
302       myMoving = true;
303       myMousePnt = aEvent->globalPos();
304       return true;
305     }
306   }
307     break;
308   case QEvent::MouseButtonRelease: {
309     QMouseEvent* aEvent = static_cast<QMouseEvent*>(theEvent);
310     if ((aEvent->button() == Qt::LeftButton) && myMoving) {
311       myMoving = false;
312       return true;
313     }
314   }
315     break;
316   case QEvent::MouseMove: {
317     QMouseEvent* aEvent = static_cast<QMouseEvent*>(theEvent);
318     if (myMoving) {
319       QMdiSubWindow* aParent = static_cast<QMdiSubWindow*>(parentWidget());
320       QMdiArea* aMDIArea = aParent->mdiArea();
321
322       QPoint aPnt = aEvent->globalPos();
323       QPoint aMDIPnt = aMDIArea->mapFromGlobal(aPnt);
324       if (aMDIArea->rect().contains(aMDIPnt)) {
325         int aX = aWgt->x() + (aPnt.x() - myMousePnt.x());
326         int aY = aWgt->y() + (aPnt.y() - myMousePnt.y());
327         aWgt->move(aX, aY);
328         myMousePnt = aPnt;
329       }
330       return true;
331     }
332   }
333     break;
334   case QEvent::MouseButtonDblClick:
335     if (theObj == myPicture) {
336       myMoving = false;
337       if (myLastState == MaximizedState)
338         showMaximized();
339       else
340         showNormal();
341       return true;
342     }
343   }
344   return false;
345 }
346
347 //****************************************************************
348 bool XGUI_ViewWindow::processViewPort(QEvent *theEvent)
349 {
350   switch(theEvent->type()) {
351   case QEvent::MouseButtonPress:
352     vpMousePressEvent((QMouseEvent*) theEvent);
353     return true;
354
355   case QEvent::MouseButtonRelease:
356     vpMouseReleaseEvent((QMouseEvent*) theEvent);
357     return true;
358
359   case QEvent::MouseMove:
360     vpMouseMoveEvent((QMouseEvent*) theEvent);
361     return true;
362
363   case QEvent::MouseButtonDblClick:
364     emit mouseDoubleClicked(this, (QMouseEvent*) theEvent);
365     return true;
366   }
367   return false;
368 }
369
370 //****************************************************************
371 bool XGUI_ViewWindow::eventFilter(QObject *theObj, QEvent *theEvent)
372 {
373   if ((theObj == myGripWgt) || (theObj == myPicture)) {
374     if (processWindowControls(theObj, theEvent))
375       return true;
376   } else if (theObj == myViewPort) {
377     if (processViewPort(theEvent))
378       return true;
379   }
380   return QFrame::eventFilter(theObj, theEvent);
381 }
382
383 //****************************************************************
384 XGUI_ViewWindow::OperationType XGUI_ViewWindow::getButtonState(
385     QMouseEvent* theEvent, XGUI::InteractionStyle theInteractionStyle)
386 {
387   OperationType aOp = NOTHING;
388   XGUI::InteractionStyle aStyle = (XGUI::InteractionStyle) theInteractionStyle;
389   if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::ZOOM])
390       && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::ZOOM]))
391     aOp = ZOOMVIEW;
392   else if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::PAN])
393       && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::PAN]))
394     aOp = PANVIEW;
395   else if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::ROTATE])
396       && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::ROTATE])
397       && (my2dMode == XGUI::No2dMode))
398     aOp = ROTATE;
399
400   return aOp;
401 }
402
403 //****************************************************************
404 void XGUI_ViewWindow::vpMousePressEvent(QMouseEvent* theEvent)
405 {
406   myStartX = theEvent->x();
407   myStartY = theEvent->y();
408   XGUI::InteractionStyle anInteractionStyle = interactionStyle();
409
410   // in "key free" interaction style zoom operation is activated by two buttons (simultaneously pressed),
411   // which are assigned for pan and rotate - these operations are activated immediately after pressing 
412   // of the first button, so it is necessary to switch to zoom when the second button is pressed
413   bool aSwitchToZoom = false;
414   if ((anInteractionStyle == XGUI::KEY_FREE) && (myOperation == PANVIEW || myOperation == ROTATE)) {
415     aSwitchToZoom = getButtonState(theEvent, anInteractionStyle) == ZOOMVIEW;
416   }
417
418   switch(myOperation) {
419   case WINDOWFIT:
420     if (theEvent->button() == Qt::LeftButton)
421       emit vpTransformationStarted(WINDOWFIT);
422     break;
423
424   case PANGLOBAL:
425     if (theEvent->button() == Qt::LeftButton)
426       emit vpTransformationStarted(PANGLOBAL);
427     break;
428
429   case ZOOMVIEW:
430     if (theEvent->button() == Qt::LeftButton) {
431       myViewPort->startZoomAtPoint(myStartX, myStartY);
432       emit vpTransformationStarted(ZOOMVIEW);
433     }
434     break;
435
436   case PANVIEW:
437     if (aSwitchToZoom) {
438       myViewPort->startZoomAtPoint(myStartX, myStartY);
439       activateZoom();
440     } else if (theEvent->button() == Qt::LeftButton)
441       emit vpTransformationStarted(PANVIEW);
442     break;
443
444   case ROTATE:
445     if (aSwitchToZoom) {
446       myViewPort->startZoomAtPoint(myStartX, myStartY);
447       activateZoom();
448     } else if (theEvent->button() == Qt::LeftButton) {
449       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
450       emit vpTransformationStarted(ROTATE);
451     }
452     break;
453
454   default:
455     /*  Try to activate a transformation */
456     OperationType aState;
457     if (interactionStyle() == XGUI::STANDARD)
458       aState = getButtonState(theEvent, anInteractionStyle);
459     else {
460       aState = XGUI_ViewWindow::NOTHING;
461       myIsKeyFree = true;
462     }
463     switch(aState) {
464     case ZOOMVIEW:
465       myViewPort->startZoomAtPoint(myStartX, myStartY);
466       activateZoom();
467       break;
468     case PANVIEW:
469       activatePanning();
470       break;
471     case ROTATE:
472       activateRotation();
473       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
474       break;
475     default:
476       if (myRotationPointSelection) {
477         if (theEvent->button() == Qt::LeftButton) {
478           Handle(AIS_InteractiveContext) ic = myViewer->AISContext();
479           ic->Select();
480           for(ic->InitSelected(); ic->MoreSelected(); ic->NextSelected()) {
481             TopoDS_Shape aShape = ic->SelectedShape();
482             if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX) {
483               gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(ic->SelectedShape()));
484               /*if ( mySetRotationPointDlg ) {
485                myRotationPointSelection = false;
486                mySetRotationPointDlg->setCoords(aPnt.X(), aPnt.Y(), aPnt.Z());
487                }*/
488             } else {
489               myCurrPointType = myPrevPointType;
490               break;
491             }
492           }
493           if (ic->NbSelected() == 0)
494             myCurrPointType = myPrevPointType;
495           //if ( mySetRotationPointDlg ) mySetRotationPointDlg->toggleChange();
496           ic->CloseAllContexts();
497           myOperation = NOTHING;
498           myViewPort->setCursor(myCursor);
499           myCursorIsHand = false;
500           myRotationPointSelection = false;
501         }
502       } else
503         emit mousePressed(this, theEvent);
504       break;
505     }
506     /* notify that we start a transformation */
507     if (transformRequested())
508       emit vpTransformationStarted(myOperation);
509   }
510   if (transformRequested())
511     setTransformInProcess(true);
512
513   /* we may need it for sketching... */
514   /*    if ( l_mbPressEvent )
515    delete l_mbPressEvent;
516    l_mbPressEvent = new QMouseEvent( *theEvent );*/
517 }
518
519 //****************************************************************
520 void XGUI_ViewWindow::vpMouseReleaseEvent(QMouseEvent* theEvent)
521 {
522   switch(myOperation) {
523   case NOTHING: {
524     int prevState = myCurSketch;
525     /*            if(theEvent->button() == Qt::RightButton) {
526      QList<OCCViewer_ViewSketcher*>::Iterator it;
527      for ( it = mySketchers.begin(); it != mySketchers.end() && myCurSketch != -1; ++it ) {
528      OCCViewer_ViewSketcher* sk = (*it);
529      if( ( sk->sketchButton() & theEvent->button() ) && sk->sketchButton() == myCurSketch )
530      myCurSketch = -1;
531      }
532      }
533      */
534     emit mouseReleased(this, theEvent);
535     if (theEvent->button() == Qt::RightButton && prevState == -1) {
536       QContextMenuEvent aEvent(QContextMenuEvent::Mouse, theEvent->pos(), theEvent->globalPos());
537       emit contextMenuRequested(&aEvent);
538     }
539   }
540     break;
541   case ROTATE:
542     myViewPort->endRotation();
543     resetState();
544     break;
545
546   case PANVIEW:
547   case ZOOMVIEW:
548     resetState();
549     break;
550
551   case PANGLOBAL:
552     if (theEvent->button() == Qt::LeftButton) {
553       myViewPort->setCenter(theEvent->x(), theEvent->y());
554       myViewPort->getView()->SetScale(myCurScale);
555       resetState();
556     }
557     break;
558
559   case WINDOWFIT:
560     if (theEvent->button() == Qt::LeftButton) {
561       myCurrX = theEvent->x();
562       myCurrY = theEvent->y();
563       drawRect();
564       QRect rect = makeRect(myStartX, myStartY, myCurrX, myCurrY);
565       if (!rect.isEmpty())
566         myViewPort->fitRect(rect);
567       endDrawRect();
568       resetState();
569     }
570     break;
571   }
572
573   // NOTE: viewer 3D detects a rectangle of selection using this event
574   // so we must emit it BEFORE resetting the selection rectangle
575   if (theEvent->button() == Qt::LeftButton && myDrawRect) {
576     drawRect();
577     endDrawRect();
578     resetState();
579     myViewPort->update();
580   }
581   /*    if ( l_mbPressEvent ) {
582    delete l_mbPressEvent;
583    l_mbPressEvent = 0;
584    }*/
585 }
586
587 //****************************************************************
588 void XGUI_ViewWindow::vpMouseMoveEvent(QMouseEvent* theEvent)
589 {
590   if (myIsKeyFree && interactionStyle() == XGUI::KEY_FREE) {
591     myIsKeyFree = false;
592     switch(getButtonState(theEvent, interactionStyle())) {
593     case ZOOMVIEW:
594       myViewPort->startZoomAtPoint(myStartX, myStartY);
595       activateZoom();
596       break;
597     case PANVIEW:
598       activatePanning();
599       break;
600     case ROTATE:
601       activateRotation();
602       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
603       break;
604     default:
605       break;
606     }
607   }
608
609   myCurrX = theEvent->x();
610   myCurrY = theEvent->y();
611   switch(myOperation) {
612   case ROTATE:
613     myViewPort->rotate(myCurrX, myCurrY, myCurrPointType, mySelectedPoint);
614     break;
615
616   case ZOOMVIEW:
617     myViewPort->zoom(myStartX, myStartY, myCurrX, myCurrY);
618     myStartX = myCurrX;
619     myStartY = myCurrY;
620     break;
621
622   case PANVIEW:
623     myViewPort->pan(myCurrX - myStartX, myStartY - myCurrY);
624     myStartX = myCurrX;
625     myStartY = myCurrY;
626     break;
627
628   case PANGLOBAL:
629     break;
630
631   default:
632     if (myRotationPointSelection /*|| isSketcherStyle()*/) {
633       emit mouseMoving(this, theEvent);
634     } else {
635       int aState = theEvent->modifiers();
636       int aButton = theEvent->buttons();
637       int anInteractionStyle = interactionStyle();
638       if (((anInteractionStyle == XGUI::STANDARD) && (aButton == Qt::LeftButton)
639           && (aState == Qt::NoModifier || Qt::ShiftModifier))
640           || ((anInteractionStyle == XGUI::KEY_FREE) && (aButton == Qt::LeftButton)
641               && (aState == Qt::ControlModifier
642                   || aState == (Qt::ControlModifier | Qt::ShiftModifier)))) {
643         myDrawRect = myEnableDrawMode;
644         if (myDrawRect) {
645           drawRect();
646           if (!myCursorIsHand) {   // we are going to sketch a rectangle
647             QCursor handCursor(Qt::PointingHandCursor);
648             myCursorIsHand = true;
649             myCursor = cursor();
650             myViewPort->setCursor(handCursor);
651           }
652         }
653         emit mouseMoving(this, theEvent);
654       } /* else if ( ( (anInteractionStyle == XGUI::STANDARD) &&
655        (aButton == Qt::RightButton) && 
656        ( aState == Qt::NoModifier || Qt::ShiftModifier ) ) ||
657        ( (anInteractionStyle == XGUI::KEY_FREE) &&
658        (aButton == Qt::RightButton) && 
659        ( aState == Qt::ControlModifier || aState == ( Qt::ControlModifier|Qt::ShiftModifier ) ) ) ) {
660        OCCViewer_ViewSketcher* sketcher = 0;
661        QList<OCCViewer_ViewSketcher*>::Iterator it;
662        for ( it = mySketchers.begin(); it != mySketchers.end() && !sketcher; ++it ) {
663        OCCViewer_ViewSketcher* sk = (*it);
664        if( sk->isDefault() && sk->sketchButton() == aButton )
665        sketcher = sk;
666        }
667        if ( sketcher && myCurSketch == -1 ) {
668        activateSketching( sketcher->type() );
669        if ( mypSketcher ) {
670        myCurSketch = mypSketcher->sketchButton();
671
672        if ( l_mbPressEvent )  {
673        QApplication::sendEvent( getViewPort(), l_mbPressEvent );
674        delete l_mbPressEvent;
675        l_mbPressEvent = 0;
676        }
677        QApplication::sendEvent( getViewPort(), theEvent );
678        }
679        }
680        } */else
681         emit mouseMoving(this, theEvent);
682     }
683   }
684 }
685
686 /*!
687  \brief Draw rubber band rectangle.
688  */
689 void XGUI_ViewWindow::drawRect()
690 {
691   if (!myRectBand) {
692     myRectBand = new XGUI_RectRubberBand(myViewPort);
693   }
694
695   myRectBand->setUpdatesEnabled(false);
696   QRect aRect = makeRect(myStartX, myStartY, myCurrX, myCurrY);
697   myRectBand->initGeometry(aRect);
698
699   if (!myRectBand->isVisible())
700     myRectBand->show();
701
702   myRectBand->setUpdatesEnabled(true);
703 }
704
705 /*!
706  \brief Clear rubber band rectangle on the end on the dragging operation.
707  */
708 void XGUI_ViewWindow::endDrawRect()
709 {
710   if (myRectBand) {
711     myRectBand->clearGeometry();
712     myRectBand->hide();
713   }
714 }
715
716 void XGUI_ViewWindow::activateZoom()
717 {
718   if (!transformRequested() && !myCursorIsHand)
719     myCursor = cursor(); /* save old cursor */
720
721   if (myOperation != ZOOMVIEW) {
722     QPixmap zoomPixmap(imageZoomCursor);
723     QCursor zoomCursor(zoomPixmap);
724     if (setTransformRequested(ZOOMVIEW))
725       myViewPort->setCursor(zoomCursor);
726   }
727 }
728
729 bool XGUI_ViewWindow::transformRequested() const
730 {
731   return (myOperation != NOTHING);
732 }
733
734 /*!
735  \brief Start delayed viewer operation.
736  */
737 bool XGUI_ViewWindow::setTransformRequested(OperationType op)
738 {
739   bool ok = transformEnabled(op);
740   myOperation = ok ? op : NOTHING;
741   myViewPort->setMouseTracking(myOperation == NOTHING);
742   return ok;
743 }
744
745 /*!
746  Set enabled state of transformation (rotate, zoom, etc)
747  */
748 void XGUI_ViewWindow::setTransformEnabled(const OperationType id, const bool on)
749 {
750   if (id != NOTHING)
751     myStatus.insert(id, on);
752 }
753
754 /*!
755  \return enabled state of transformation (rotate, zoom, etc)
756  */
757 bool XGUI_ViewWindow::transformEnabled(const OperationType id) const
758 {
759   return myStatus.contains(id) ? myStatus[id] : true;
760 }
761
762 /*!
763  \brief Start panning operation.
764
765  Sets the corresponding cursor for the widget.
766  */
767 void XGUI_ViewWindow::activatePanning()
768 {
769   if (!transformRequested() && !myCursorIsHand)
770     myCursor = cursor();                // save old cursor
771
772   if (myOperation != PANVIEW) {
773     QCursor panCursor(Qt::SizeAllCursor);
774     if (setTransformRequested(PANVIEW))
775       myViewPort->setCursor(panCursor);
776   }
777 }
778
779 /*!
780  \brief Start rotation operation
781
782  Sets the corresponding cursor for the widget.
783  */
784 void XGUI_ViewWindow::activateRotation()
785 {
786   if (!transformRequested() && !myCursorIsHand)
787     myCursor = cursor();                // save old cursor
788
789   if (myOperation != ROTATE) {
790     QPixmap rotatePixmap(imageRotateCursor);
791     QCursor rotCursor(rotatePixmap);
792     if (setTransformRequested(ROTATE))
793       myViewPort->setCursor(rotCursor);
794   }
795 }
796
797 /*!
798  \brief Reset the viewport to its initial state
799  ( no transformations in process etc. )
800  */
801 void XGUI_ViewWindow::resetState()
802 {
803   myDrawRect = false;
804
805   if (myRotationPointSelection) {
806     QCursor handCursor(Qt::PointingHandCursor);
807     myViewPort->setCursor(handCursor);
808   } else {
809     if (transformRequested() || myCursorIsHand)
810       myViewPort->setCursor(myCursor);
811     myCursorIsHand = false;
812   }
813
814   if (transformRequested())
815     emit vpTransformationFinished(myOperation);
816
817   setTransformInProcess(false);
818   setTransformRequested(NOTHING);
819 }
820
821 XGUI_ViewBackground XGUI_ViewWindow::background() const
822 {
823   return myViewPort ? myViewPort->background() : XGUI_ViewBackground();
824 }
825
826 void XGUI_ViewWindow::setBackground(const XGUI_ViewBackground& theBackground)
827 {
828   if (myViewPort)
829     myViewPort->setBackground(theBackground);
830 }