Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 <QTimer>
17 #include <QFileDialog>
18 #include <QStyleOptionToolBar>
19
20 #include <TopoDS_Shape.hxx>
21 #include <BRep_Tool.hxx>
22 #include <TopoDS.hxx>
23 #include <Visual3d_View.hxx>
24
25 #define BORDER_SIZE 2
26
27 const char* imageZoomCursor[] = { "32 32 3 1", ". c None", "a c #000000", "# c #ffffff",
28     "................................", "................................",
29     ".#######........................", "..aaaaaaa.......................",
30     "................................", ".............#####..............",
31     "...........##.aaaa##............", "..........#.aa.....a#...........",
32     ".........#.a.........#..........", ".........#a..........#a.........",
33     "........#.a...........#.........", "........#a............#a........",
34     "........#a............#a........", "........#a............#a........",
35     "........#a............#a........", ".........#...........#.a........",
36     ".........#a..........#a.........", ".........##.........#.a.........",
37     "........#####.....##.a..........", ".......###aaa#####.aa...........",
38     "......###aa...aaaaa.......#.....", ".....###aa................#a....",
39     "....###aa.................#a....", "...###aa...............#######..",
40     "....#aa.................aa#aaaa.", ".....a....................#a....",
41     "..........................#a....", "...........................a....",
42     "................................", "................................",
43     "................................", "................................" };
44
45 const char* imageRotateCursor[] = { "32 32 3 1", ". c None", "a c #000000", "# c #ffffff",
46     "................................", "................................",
47     "................................", "................................",
48     "........#.......................", ".......#.a......................",
49     "......#######...................", ".......#aaaaa#####..............",
50     "........#..##.a#aa##........##..", ".........a#.aa..#..a#.....##.aa.",
51     ".........#.a.....#...#..##.aa...", ".........#a.......#..###.aa.....",
52     "........#.a.......#a..#aa.......", "........#a.........#..#a........",
53     "........#a.........#a.#a........", "........#a.........#a.#a........",
54     "........#a.........#a.#a........", ".........#.........#a#.a........",
55     "........##a........#a#a.........", "......##.a#.......#.#.a.........",
56     "....##.aa..##.....##.a..........", "..##.aa.....a#####.aa...........",
57     "...aa.........aaa#a.............", "................#.a.............",
58     "...............#.a..............", "..............#.a...............",
59     "...............a................", "................................",
60     "................................", "................................",
61     "................................", "................................" };
62
63 const char* imageCrossCursor[] = { "32 32 3 1", ". c None", "a c #000000", "# c #ffffff",
64     "................................", "................................",
65     "................................", "................................",
66     "................................", "................................",
67     "................................", "...............#................",
68     "...............#a...............", "...............#a...............",
69     "...............#a...............", "...............#a...............",
70     "...............#a...............", "...............#a...............",
71     "...............#a...............", ".......#################........",
72     "........aaaaaaa#aaaaaaaaa.......", "...............#a...............",
73     "...............#a...............", "...............#a...............",
74     "...............#a...............", "...............#a...............",
75     "...............#a...............", "...............#a...............",
76     "................a...............", "................................",
77     "................................", "................................",
78     "................................", "................................",
79     "................................", "................................" };
80
81
82 ViewerToolbar::ViewerToolbar(QWidget* theParent, XGUI_ViewPort* thePort)
83   : QToolBar(theParent), myVPort(thePort), myResize(false)
84 {
85   connect(myVPort, SIGNAL(resized()), this, SLOT(onViewPortResized()));
86 }
87
88 void ViewerToolbar::paintEvent(QPaintEvent* theEvent)
89 {
90   //QToolBar::paintEvent(theEvent);
91   // Paint background
92   QPainter aPainter(this);
93   QRect aRect = rect();
94   QRect aVPRect = myVPort->rect();
95   QPoint aGlobPnt = mapToGlobal(aRect.topLeft());
96   QPoint aPnt = myVPort->mapFromGlobal(aGlobPnt);
97
98   QRect aImgRect(QRect(aPnt.x(), aPnt.y() + aVPRect.height() - aRect.height(),
99                        aRect.width(), aRect.height()));
100   QImage aImg = myVPort->dumpView(aImgRect, myResize);
101   if (!aImg.isNull())
102     aPainter.drawImage(aRect, aImg);
103   myResize = false;
104
105   // Paint foreground
106   QStyle *style = this->style();
107   QStyleOptionToolBar aOpt;
108   initStyleOption(&aOpt);
109
110   aOpt.rect = style->subElementRect(QStyle::SE_ToolBarHandle, &aOpt, this);
111   if (aOpt.rect.isValid())
112     style->drawPrimitive(QStyle::PE_IndicatorToolBarHandle, &aOpt, &aPainter, this);
113 }
114
115 //**************************************************************************
116 ViewerLabel::ViewerLabel(QWidget* theParent, XGUI_ViewPort* thePort)
117   : QLabel(theParent), myVPort(thePort), myResize(false)
118 {
119   connect(myVPort, SIGNAL(resized()), this, SLOT(onViewPortResized()));
120 }
121
122 void ViewerLabel::paintEvent(QPaintEvent* theEvent)
123 {
124   QRect aRect = rect();
125   QRect aVPRect = myVPort->rect();
126   QPoint aGlobPnt = mapToGlobal(aRect.topLeft());
127   QPoint aPnt = myVPort->mapFromGlobal(aGlobPnt);
128
129   QRect aImgRect(QRect(aPnt.x(), aPnt.y() + aVPRect.height() - aRect.height(), 
130                  aRect.width(), aRect.height()));
131   QImage aImg = myVPort->dumpView(aImgRect, myResize);
132   if (!aImg.isNull())
133     QPainter(this).drawImage(aRect, aImg);
134   QLabel::paintEvent(theEvent);
135 }
136
137 //**************************************************************************
138 //**************************************************************************
139 //**************************************************************************
140 XGUI_ViewWindow::XGUI_ViewWindow(XGUI_Viewer* theViewer, V3d_TypeOfView theType)
141     : QFrame(), 
142     myViewer(theViewer), 
143     myMoving(false), 
144     MinimizeIco(":pictures/wnd_minimize.png"), 
145     MaximizeIco(":pictures/wnd_maximize.png"), 
146     CloseIco(":pictures/wnd_close.png"), 
147     RestoreIco(":pictures/wnd_restore.png"), 
148     myInteractionStyle(XGUI::STANDARD), 
149     myRectBand(0), 
150     myIsKeyFree(false), 
151     my2dMode(XGUI::No2dMode), 
152     myCurrPointType(XGUI::GRAVITY), 
153     myPrevPointType(XGUI::GRAVITY), 
154     myRotationPointSelection(false),
155     myClosable(true),
156     myStartX(0), myStartY(0), myCurrX(0), myCurrY(0), myCurScale(0.0), myCurSketch(0),
157     myDrawRect(false), myEnableDrawMode(true), myCursorIsHand(false), myEventStarted(false),
158     myIsActive(false),
159     myLastState(WindowNormalState), myOperation(NOTHING)
160 {
161   mySelectedPoint = gp_Pnt(0., 0., 0.);
162   setFrameStyle(QFrame::Raised);
163   setFrameShape(QFrame::Panel);
164   setLineWidth(BORDER_SIZE);
165   setMouseTracking(true);
166
167   QVBoxLayout* aLay = new QVBoxLayout(this);
168   aLay->setContentsMargins(BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE);
169   myViewPort = new XGUI_ViewPort(this, myViewer->v3dViewer(), theType);
170   myViewPort->installEventFilter(this);
171   myViewPort->setCursor(Qt::ArrowCursor);
172   aLay->addWidget(myViewPort);
173
174   myPicture = new QLabel(this);
175   myPicture->setFrameStyle(QFrame::Sunken);
176   myPicture->setFrameShape(QFrame::Panel);
177   myPicture->setMouseTracking(true);
178   myPicture->installEventFilter(this);
179   aLay->addWidget(myPicture);
180   myPicture->hide();
181
182   QVBoxLayout* aVPLay = new QVBoxLayout(myViewPort);
183   aVPLay->setMargin(0);
184   aVPLay->setSpacing(0);
185   aVPLay->setContentsMargins(0,0,0,0);
186
187   QHBoxLayout* aToolLay = new QHBoxLayout();
188   aToolLay->setMargin(0);
189   aToolLay->setSpacing(0);
190   aToolLay->setContentsMargins(0,0,0,0);
191   aVPLay->addLayout(aToolLay);
192   aVPLay->addStretch(); 
193
194   myGripWgt = new ViewerLabel(this, myViewPort);
195   myGripWgt->setPixmap(QPixmap(":pictures/wnd_grip.png"));
196   myGripWgt->setMouseTracking(true);
197   myGripWgt->installEventFilter(this);
198   myGripWgt->setCursor(Qt::OpenHandCursor);
199   aToolLay->addWidget(myGripWgt);
200
201     // Create Viewer management buttons
202   myViewBar = new ViewerToolbar(this, myViewPort);
203   myViewBar->setCursor(Qt::PointingHandCursor);
204   aToolLay->addWidget(myViewBar); 
205   aToolLay->addStretch();
206
207   QAction* aBtn;
208
209   // Dump view
210   aBtn = new QAction(QIcon(":pictures/occ_view_camera_dump.png"), tr("Dump view"), myViewBar);
211   connect(aBtn, SIGNAL(triggered()), SLOT(dumpView()));
212   myViewBar->addAction(aBtn);
213   // Fit all
214   aBtn = new QAction(QIcon(":pictures/occ_view_fitall.png"), tr("Fit all"), myViewBar);
215   connect(aBtn, SIGNAL(triggered()), SLOT(fitAll()));
216   myViewBar->addAction(aBtn);
217   // Fit area
218   aBtn = new QAction(QIcon(":pictures/occ_view_fitarea.png"), tr("Fit area"), myViewBar);
219   connect(aBtn, SIGNAL(triggered()), SLOT(activateWindowFit()));
220   myViewBar->addAction(aBtn);
221   // Zoom
222   aBtn = new QAction(QIcon(":pictures/occ_view_zoom.png"), tr("Zoom"), myViewBar);
223   connect(aBtn, SIGNAL(triggered()), SLOT(activateZoom()));
224   myViewBar->addAction(aBtn);
225   // Pan
226   aBtn = new QAction(QIcon(":pictures/occ_view_pan.png"), tr("Panning"), myViewBar);
227   connect(aBtn, SIGNAL(triggered()), SLOT(activatePanning()));
228   myViewBar->addAction(aBtn);
229   // Global Panning
230   aBtn = new QAction(QIcon(":pictures/occ_view_glpan.png"), tr("Global panning"), myViewBar);
231   connect(aBtn, SIGNAL(triggered()), SLOT(activateGlobalPanning()));
232   myViewBar->addAction(aBtn);
233   // Rotation
234   aBtn = new QAction(QIcon(":pictures/occ_view_rotate.png"), tr("Rotate"), myViewBar);
235   connect(aBtn, SIGNAL(triggered()), SLOT(activateRotation()));
236   myViewBar->addAction(aBtn);
237   // Reset
238   aBtn = new QAction(QIcon(":pictures/occ_view_reset.png"), tr("Reset"), myViewBar);
239   connect(aBtn, SIGNAL(triggered()), SLOT(reset()));
240   myViewBar->addAction(aBtn);
241   // Front view
242   aBtn = new QAction(QIcon(":pictures/occ_view_front.png"), tr("Front"), myViewBar);
243   connect(aBtn, SIGNAL(triggered()), SLOT(frontView()));
244   myViewBar->addAction(aBtn);
245   // Back view
246   aBtn = new QAction(QIcon(":pictures/occ_view_back.png"), tr("Back"), myViewBar);
247   connect(aBtn, SIGNAL(triggered()), SLOT(backView()));
248   myViewBar->addAction(aBtn);
249   // Top view
250   aBtn = new QAction(QIcon(":pictures/occ_view_top.png"), tr("Top"), myViewBar);
251   connect(aBtn, SIGNAL(triggered()), SLOT(topView()));
252   myViewBar->addAction(aBtn);
253   // Bottom view
254   aBtn = new QAction(QIcon(":pictures/occ_view_bottom.png"), tr("Bottom"), myViewBar);
255   connect(aBtn, SIGNAL(triggered()), SLOT(bottomView()));
256   myViewBar->addAction(aBtn);
257   // Left view
258   aBtn = new QAction(QIcon(":pictures/occ_view_left.png"), tr("Left"), myViewBar);
259   connect(aBtn, SIGNAL(triggered()), SLOT(leftView()));
260   myViewBar->addAction(aBtn);
261   // Right view
262   aBtn = new QAction(QIcon(":pictures/occ_view_right.png"), tr("Right"), myViewBar);
263   connect(aBtn, SIGNAL(triggered()), SLOT(rightView()));
264   myViewBar->addAction(aBtn);
265   // Clone view
266   aBtn = new QAction(QIcon(":pictures/occ_view_clone.png"), tr("Clone"), myViewBar);
267   connect(aBtn, SIGNAL(triggered()), SLOT(cloneView()));
268   myViewBar->addAction(aBtn);
269
270     // Create Window management buttons
271   myWindowBar = new ViewerToolbar(this, myViewPort);
272   myWindowBar->setCursor(Qt::PointingHandCursor);
273   aToolLay->addWidget(myWindowBar);
274
275   myMinimizeBtn = new QAction(myWindowBar);
276   myMinimizeBtn->setIcon(MinimizeIco);
277   myWindowBar->addAction(myMinimizeBtn);
278   connect(myMinimizeBtn, SIGNAL(triggered()), SLOT(onMinimize()));
279
280   myMaximizeBtn = new QAction(myWindowBar);
281   myMaximizeBtn->setIcon(MaximizeIco);
282   myWindowBar->addAction(myMaximizeBtn);
283   connect(myMaximizeBtn, SIGNAL(triggered()), SLOT(onMaximize()));
284
285   aBtn = new QAction(myWindowBar);
286   aBtn->setIcon(CloseIco);
287   myWindowBar->addAction(aBtn);
288   connect(aBtn, SIGNAL(triggered()), SLOT(onClose()));
289
290   //Support copy of background on updating of viewer
291   connect(myViewPort, SIGNAL(vpTransformed()), this, SLOT(updateToolBar()));
292   connect(myViewPort, SIGNAL(vpUpdated()), this, SLOT(updateToolBar()));
293   connect(this, SIGNAL(vpTransformationFinished(XGUI_ViewWindow::OperationType)), 
294           this, SLOT(updateToolBar()));
295
296 }
297
298 //****************************************************************
299 XGUI_ViewWindow::~XGUI_ViewWindow()
300 {
301 }
302
303
304 //****************************************************************
305 void XGUI_ViewWindow::showEvent(QShowEvent* theEvent)
306 {
307   QFrame::showEvent(theEvent);
308   myWindowBar->setFixedSize(myWindowBar->sizeHint());
309 }
310
311 //****************************************************************
312 void XGUI_ViewWindow::changeEvent(QEvent* theEvent)
313 {
314
315   if (theEvent->type() == QEvent::WindowStateChange) {
316     if (isMinimized()) {
317       if (myPicture->isHidden()) {
318         myViewBar->hide();
319         myGripWgt->hide(); 
320         myWindowBar->hide();
321         myViewPort->hide();
322         myPicture->show();
323       }
324     } else {
325       if (myPicture->isVisible()) {
326         myPicture->hide();
327         myViewPort->show();
328       }
329       if (isMaximized()) {
330         myMinimizeBtn->setIcon(MinimizeIco);
331         myMaximizeBtn->setIcon(RestoreIco);
332       }
333       myViewBar->setVisible(myIsActive);
334       myWindowBar->setVisible(myIsActive);
335       myGripWgt->setVisible(myIsActive && (!isMaximized()));
336     }
337   } else
338     QWidget::changeEvent(theEvent);
339 }
340
341
342
343 //****************************************************************
344 void XGUI_ViewWindow::windowActivated()
345 {
346   if (!(isMinimized() || parentWidget()->isMinimized())) {
347     myIsActive = true;
348     if (isMaximized() || parentWidget()->isMaximized()) {
349       myMaximizeBtn->setIcon(RestoreIco);
350     } else {
351       myMaximizeBtn->setIcon(MaximizeIco);
352     }
353     myViewBar->show();
354     myWindowBar->show();
355     myGripWgt->setVisible(!(isMaximized() || isMinimized() ||
356         parentWidget()->isMaximized() || parentWidget()->isMinimized()));
357   } else 
358     myIsActive = false;
359 }
360
361 //****************************************************************
362 void XGUI_ViewWindow::windowDeactivated()
363 {
364   myIsActive = false;
365   if (!(isMinimized() || parentWidget()->isMinimized())) {
366     if (isMaximized() || parentWidget()->isMaximized()) {
367       myMaximizeBtn->setIcon(RestoreIco);
368     } else {
369       myMaximizeBtn->setIcon(MaximizeIco);
370     }
371     myViewBar->hide();
372     myWindowBar->hide();
373     myGripWgt->hide(); 
374   }
375 }
376
377
378 //****************************************************************
379 void XGUI_ViewWindow::onClose()
380 {
381   if (parentWidget()) {
382     emit tryClosing(this);
383     if (closable()) {
384       emit closed(static_cast<QMdiSubWindow*>(parentWidget()));
385       parentWidget()->close();
386     }
387   }
388 }
389
390 //****************************************************************
391 void XGUI_ViewWindow::onMinimize()
392 {
393   QPixmap aPMap = QPixmap::fromImage(myViewPort->dumpView());
394   int aW = width();
395   int aH = height();
396   double aR = aW / 100.;
397   int aNewH = int(aH / aR);
398   myPicture->setPixmap(aPMap.scaled(100,  aNewH));
399
400   myLastState = (isMaximized() || parentWidget()->isMaximized()) ? MaximizedState : WindowNormalState;
401   showMinimized();
402   parentWidget()->showMinimized();
403   parentWidget()->setGeometry(parentWidget()->x(), parentWidget()->y(), 100, aNewH);
404   parentWidget()->lower();
405   windowDeactivated();
406   myViewer->onWindowMinimized((QMdiSubWindow*)parentWidget());
407 }
408
409 //****************************************************************
410 void XGUI_ViewWindow::onMaximize()
411 {
412   if (isMaximized() || parentWidget()->isMaximized()) {
413     myMaximizeBtn->setIcon(MaximizeIco);
414     myGripWgt->show();
415     showNormal();
416     parentWidget()->showNormal();
417   } else {
418     myMaximizeBtn->setIcon(RestoreIco);
419     myGripWgt->hide();
420     showMaximized();
421     parentWidget()->showMaximized();
422   }
423   parentWidget()->activateWindow();
424   myMinimizeBtn->setIcon(MinimizeIco);
425   
426   //  In order to avoid frosen background in toolbars when it shown as a second view
427   QTimer::singleShot(50, parentWidget(), SLOT(setFocus()));
428 }
429
430 //****************************************************************
431 bool XGUI_ViewWindow::processWindowControls(QObject *theObj, QEvent *theEvent)
432 {
433   switch(theEvent->type()) {
434   case QEvent::MouseButtonPress: {
435     QMouseEvent* aEvent = static_cast<QMouseEvent*>(theEvent);
436     if ((aEvent->button() == Qt::LeftButton) && (!myMoving)) {
437       myMoving = true;
438       myMousePnt = aEvent->globalPos();
439       return true;
440     }
441   }
442     break;
443   case QEvent::MouseButtonRelease: {
444     QMouseEvent* aEvent = static_cast<QMouseEvent*>(theEvent);
445     if ((aEvent->button() == Qt::LeftButton) && myMoving) {
446       myMoving = false;
447       return true;
448     }
449   }
450     break;
451   case QEvent::MouseMove: {
452     QMouseEvent* aEvent = static_cast<QMouseEvent*>(theEvent);
453     if (myMoving) {
454       QMdiSubWindow* aParent = static_cast<QMdiSubWindow*>(parentWidget());
455       QMdiArea* aMDIArea = aParent->mdiArea();
456
457       QPoint aPnt = aEvent->globalPos();
458       QPoint aMDIPnt = aMDIArea->mapFromGlobal(aPnt);
459       if (aMDIArea->rect().contains(aMDIPnt)) {
460                     int aX = aParent->x() + (aPnt.x() - myMousePnt.x());
461                     int aY = aParent->y() + (aPnt.y() - myMousePnt.y());
462                     aParent->move(aX, aY);
463         myMousePnt = aPnt;
464       }
465       return true;
466     }
467   }
468     break;
469   case QEvent::MouseButtonDblClick:
470     if (theObj == myPicture) {
471       myMoving = false;
472       if (myLastState == MaximizedState) {
473         showMaximized();
474       } else {
475         showNormal();
476       }
477       myViewer->onWindowActivated((QMdiSubWindow*)parentWidget());
478
479       //  In order to avoid frosen background in toolbars when it shown as a second view
480       QTimer::singleShot(20, parentWidget(), SLOT(setFocus()));
481
482       return true;
483     }
484   }
485   return false;
486 }
487
488 //****************************************************************
489 bool XGUI_ViewWindow::processViewPort(QEvent *theEvent)
490 {
491   switch(theEvent->type()) {
492   case QEvent::MouseButtonPress:
493     vpMousePressEvent((QMouseEvent*) theEvent);
494     return true;
495
496   case QEvent::MouseButtonRelease:
497     vpMouseReleaseEvent((QMouseEvent*) theEvent);
498     return true;
499
500   case QEvent::MouseMove:
501     vpMouseMoveEvent((QMouseEvent*) theEvent);
502     return true;
503
504   case QEvent::MouseButtonDblClick:
505     emit mouseDoubleClicked(this, (QMouseEvent*) theEvent);
506     return true;
507     case QEvent::Wheel:
508         {
509             QWheelEvent* aEvent = (QWheelEvent*) theEvent;
510             myViewPort->startZoomAtPoint( aEvent->x(), aEvent->y() );
511             double aDelta = (double)( aEvent->delta() ) / ( 15 * 8 );
512             int x  = aEvent->x();
513             int y  = aEvent->y();
514             int x1 = (int)( aEvent->x() + width()*aDelta/100 );
515             int y1 = (int)( aEvent->y() + height()*aDelta/100 );
516             myViewPort->zoom( x, y, x1, y1 );
517         }
518         return true;
519   }
520   return false;
521 }
522
523 //****************************************************************
524 bool XGUI_ViewWindow::eventFilter(QObject *theObj, QEvent *theEvent)
525 {
526   if ((theObj == myGripWgt) || (theObj == myPicture)) {
527     if (processWindowControls(theObj, theEvent))
528       return true;
529   } else if (theObj == myViewPort) {
530     if (processViewPort(theEvent)) {
531       return true;
532     }
533   }
534   return QFrame::eventFilter(theObj, theEvent);
535 }
536
537 //****************************************************************
538 XGUI_ViewWindow::OperationType XGUI_ViewWindow::getButtonState(
539     QMouseEvent* theEvent, XGUI::InteractionStyle theInteractionStyle)
540 {
541   OperationType aOp = NOTHING;
542   XGUI::InteractionStyle aStyle = (XGUI::InteractionStyle) theInteractionStyle;
543   if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::ZOOM])
544       && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::ZOOM]))
545     aOp = ZOOMVIEW;
546   else if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::PAN])
547       && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::PAN]))
548     aOp = PANVIEW;
549   else if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::ROTATE])
550       && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::ROTATE])
551       && (my2dMode == XGUI::No2dMode))
552     aOp = ROTATE;
553
554   return aOp;
555 }
556
557 //****************************************************************
558 void XGUI_ViewWindow::vpMousePressEvent(QMouseEvent* theEvent)
559 {
560   myStartX = theEvent->x();
561   myStartY = theEvent->y();
562   XGUI::InteractionStyle anInteractionStyle = interactionStyle();
563
564   // in "key free" interaction style zoom operation is activated by two buttons (simultaneously pressed),
565   // which are assigned for pan and rotate - these operations are activated immediately after pressing 
566   // of the first button, so it is necessary to switch to zoom when the second button is pressed
567   bool aSwitchToZoom = false;
568   if ((anInteractionStyle == XGUI::KEY_FREE) && (myOperation == PANVIEW || myOperation == ROTATE)) {
569     aSwitchToZoom = getButtonState(theEvent, anInteractionStyle) == ZOOMVIEW;
570   }
571
572   switch(myOperation) {
573   case WINDOWFIT:
574     if (theEvent->button() == Qt::LeftButton)
575       emit vpTransformationStarted(WINDOWFIT);
576     break;
577
578   case PANGLOBAL:
579     if (theEvent->button() == Qt::LeftButton)
580       emit vpTransformationStarted(PANGLOBAL);
581     break;
582
583   case ZOOMVIEW:
584     if (theEvent->button() == Qt::LeftButton) {
585       myViewPort->startZoomAtPoint(myStartX, myStartY);
586       emit vpTransformationStarted(ZOOMVIEW);
587     }
588     break;
589
590   case PANVIEW:
591     if (aSwitchToZoom) {
592       myViewPort->startZoomAtPoint(myStartX, myStartY);
593       activateZoom();
594     } else if (theEvent->button() == Qt::LeftButton)
595       emit vpTransformationStarted(PANVIEW);
596     break;
597
598   case ROTATE:
599     if (aSwitchToZoom) {
600       myViewPort->startZoomAtPoint(myStartX, myStartY);
601       activateZoom();
602     } else if (theEvent->button() == Qt::LeftButton) {
603       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
604       emit vpTransformationStarted(ROTATE);
605     }
606     break;
607
608   default:
609     /*  Try to activate a transformation */
610     OperationType aState;
611     if (interactionStyle() == XGUI::STANDARD)
612       aState = getButtonState(theEvent, anInteractionStyle);
613     else {
614       aState = XGUI_ViewWindow::NOTHING;
615       myIsKeyFree = true;
616     }
617     switch(aState) {
618     case ZOOMVIEW:
619       myViewPort->startZoomAtPoint(myStartX, myStartY);
620       activateZoom();
621       break;
622     case PANVIEW:
623       activatePanning();
624       break;
625     case ROTATE:
626       activateRotation();
627       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
628       break;
629     default:
630       if (myRotationPointSelection) {
631         if (theEvent->button() == Qt::LeftButton) {
632           Handle(AIS_InteractiveContext) ic = myViewer->AISContext();
633           ic->Select();
634           for(ic->InitSelected(); ic->MoreSelected(); ic->NextSelected()) {
635             TopoDS_Shape aShape = ic->SelectedShape();
636             if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX) {
637               gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(ic->SelectedShape()));
638               /*if ( mySetRotationPointDlg ) {
639                myRotationPointSelection = false;
640                mySetRotationPointDlg->setCoords(aPnt.X(), aPnt.Y(), aPnt.Z());
641                }*/
642             } else {
643               myCurrPointType = myPrevPointType;
644               break;
645             }
646           }
647           if (ic->NbSelected() == 0)
648             myCurrPointType = myPrevPointType;
649           //if ( mySetRotationPointDlg ) mySetRotationPointDlg->toggleChange();
650           ic->CloseAllContexts();
651           myOperation = NOTHING;
652           myViewPort->setCursor(myCursor);
653           myCursorIsHand = false;
654           myRotationPointSelection = false;
655         }
656       } else
657         emit mousePressed(this, theEvent);
658       break;
659     }
660     /* notify that we start a transformation */
661     if (transformRequested())
662       emit vpTransformationStarted(myOperation);
663   }
664   if (transformRequested())
665     setTransformInProcess(true);
666
667   /* we may need it for sketching... */
668   /*    if ( l_mbPressEvent )
669    delete l_mbPressEvent;
670    l_mbPressEvent = new QMouseEvent( *theEvent );*/
671 }
672
673 //****************************************************************
674 void XGUI_ViewWindow::vpMouseReleaseEvent(QMouseEvent* theEvent)
675 {
676   switch(myOperation) {
677   case NOTHING: {
678     int prevState = myCurSketch;
679     /*            if(theEvent->button() == Qt::RightButton) {
680      QList<OCCViewer_ViewSketcher*>::Iterator it;
681      for ( it = mySketchers.begin(); it != mySketchers.end() && myCurSketch != -1; ++it ) {
682      OCCViewer_ViewSketcher* sk = (*it);
683      if( ( sk->sketchButton() & theEvent->button() ) && sk->sketchButton() == myCurSketch )
684      myCurSketch = -1;
685      }
686      }
687      */
688     emit mouseReleased(this, theEvent);
689     if (theEvent->button() == Qt::RightButton && prevState == -1) {
690       QContextMenuEvent aEvent(QContextMenuEvent::Mouse, theEvent->pos(), theEvent->globalPos());
691       emit contextMenuRequested(&aEvent);
692     }
693   }
694     break;
695   case ROTATE:
696     myViewPort->endRotation();
697     resetState();
698     break;
699
700   case PANVIEW:
701   case ZOOMVIEW:
702     resetState();
703     break;
704
705   case PANGLOBAL:
706     if (theEvent->button() == Qt::LeftButton) {
707       myViewPort->setCenter(theEvent->x(), theEvent->y());
708       myViewPort->getView()->SetScale(myCurScale);
709       resetState();
710     }
711     break;
712
713   case WINDOWFIT:
714     if (theEvent->button() == Qt::LeftButton) {
715       myCurrX = theEvent->x();
716       myCurrY = theEvent->y();
717       drawRect();
718       QRect rect = makeRect(myStartX, myStartY, myCurrX, myCurrY);
719       if (!rect.isEmpty())
720         myViewPort->fitRect(rect);
721       endDrawRect();
722       resetState();
723     }
724     break;
725   }
726
727   // NOTE: viewer 3D detects a rectangle of selection using this event
728   // so we must emit it BEFORE resetting the selection rectangle
729   if (theEvent->button() == Qt::LeftButton && myDrawRect) {
730     drawRect();
731     endDrawRect();
732     resetState();
733     myViewPort->update();
734   }
735   /*    if ( l_mbPressEvent ) {
736    delete l_mbPressEvent;
737    l_mbPressEvent = 0;
738    }*/
739 }
740
741 //****************************************************************
742 void XGUI_ViewWindow::vpMouseMoveEvent(QMouseEvent* theEvent)
743 {
744   if (myIsKeyFree && interactionStyle() == XGUI::KEY_FREE) {
745     myIsKeyFree = false;
746     switch(getButtonState(theEvent, interactionStyle())) {
747     case ZOOMVIEW:
748       myViewPort->startZoomAtPoint(myStartX, myStartY);
749       activateZoom();
750       break;
751     case PANVIEW:
752       activatePanning();
753       break;
754     case ROTATE:
755       activateRotation();
756       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
757       break;
758     default:
759       break;
760     }
761   }
762
763   myCurrX = theEvent->x();
764   myCurrY = theEvent->y();
765   switch(myOperation) {
766   case ROTATE:
767     myViewPort->rotate(myCurrX, myCurrY, myCurrPointType, mySelectedPoint);
768     break;
769
770   case ZOOMVIEW:
771     myViewPort->zoom(myStartX, myStartY, myCurrX, myCurrY);
772     myStartX = myCurrX;
773     myStartY = myCurrY;
774     break;
775
776   case PANVIEW:
777     myViewPort->pan(myCurrX - myStartX, myStartY - myCurrY);
778     myStartX = myCurrX;
779     myStartY = myCurrY;
780     break;
781
782   case PANGLOBAL:
783     break;
784
785   default:
786     if (myRotationPointSelection /*|| isSketcherStyle()*/) {
787       emit mouseMoving(this, theEvent);
788     } else {
789       int aState = theEvent->modifiers();
790       int aButton = theEvent->buttons();
791       int anInteractionStyle = interactionStyle();
792       if (((anInteractionStyle == XGUI::STANDARD) && (aButton == Qt::LeftButton)
793           && (aState == Qt::NoModifier || Qt::ShiftModifier))
794           || ((anInteractionStyle == XGUI::KEY_FREE) && (aButton == Qt::LeftButton)
795               && (aState == Qt::ControlModifier
796                   || aState == (Qt::ControlModifier | Qt::ShiftModifier)))) {
797         myDrawRect = myEnableDrawMode;
798         if (myDrawRect) {
799           drawRect();
800           if (!myCursorIsHand) {   // we are going to sketch a rectangle
801             QCursor handCursor(Qt::PointingHandCursor);
802             myCursorIsHand = true;
803             myCursor = cursor();
804             myViewPort->setCursor(handCursor);
805           }
806         }
807         emit mouseMoving(this, theEvent);
808       } /* else if ( ( (anInteractionStyle == XGUI::STANDARD) &&
809        (aButton == Qt::RightButton) && 
810        ( aState == Qt::NoModifier || Qt::ShiftModifier ) ) ||
811        ( (anInteractionStyle == XGUI::KEY_FREE) &&
812        (aButton == Qt::RightButton) && 
813        ( aState == Qt::ControlModifier || aState == ( Qt::ControlModifier|Qt::ShiftModifier ) ) ) ) {
814        OCCViewer_ViewSketcher* sketcher = 0;
815        QList<OCCViewer_ViewSketcher*>::Iterator it;
816        for ( it = mySketchers.begin(); it != mySketchers.end() && !sketcher; ++it ) {
817        OCCViewer_ViewSketcher* sk = (*it);
818        if( sk->isDefault() && sk->sketchButton() == aButton )
819        sketcher = sk;
820        }
821        if ( sketcher && myCurSketch == -1 ) {
822        activateSketching( sketcher->type() );
823        if ( mypSketcher ) {
824        myCurSketch = mypSketcher->sketchButton();
825
826        if ( l_mbPressEvent )  {
827        QApplication::sendEvent( getViewPort(), l_mbPressEvent );
828        delete l_mbPressEvent;
829        l_mbPressEvent = 0;
830        }
831        QApplication::sendEvent( getViewPort(), theEvent );
832        }
833        }
834        } */else
835         emit mouseMoving(this, theEvent);
836     }
837   }
838 }
839
840 /*!
841  \brief Draw rubber band rectangle.
842  */
843 void XGUI_ViewWindow::drawRect()
844 {
845   if (!myRectBand) {
846     myRectBand = new XGUI_RectRubberBand(myViewPort);
847   }
848
849   myRectBand->setUpdatesEnabled(false);
850   QRect aRect = makeRect(myStartX, myStartY, myCurrX, myCurrY);
851   myRectBand->initGeometry(aRect);
852
853   if (!myRectBand->isVisible())
854     myRectBand->show();
855
856   myRectBand->setUpdatesEnabled(true);
857 }
858
859 /*!
860  \brief Clear rubber band rectangle on the end on the dragging operation.
861  */
862 void XGUI_ViewWindow::endDrawRect()
863 {
864   if (myRectBand) {
865     myRectBand->clearGeometry();
866     myRectBand->hide();
867   }
868 }
869
870 void XGUI_ViewWindow::activateZoom()
871 {
872   if (!transformRequested() && !myCursorIsHand)
873     myCursor = cursor(); /* save old cursor */
874
875   if (myOperation != ZOOMVIEW) {
876     QPixmap zoomPixmap(imageZoomCursor);
877     QCursor zoomCursor(zoomPixmap);
878     if (setTransformRequested(ZOOMVIEW))
879       myViewPort->setCursor(zoomCursor);
880   }
881 }
882
883 bool XGUI_ViewWindow::transformRequested() const
884 {
885   return (myOperation != NOTHING);
886 }
887
888 /*!
889  \brief Start delayed viewer operation.
890  */
891 bool XGUI_ViewWindow::setTransformRequested(OperationType op)
892 {
893   bool ok = transformEnabled(op);
894   myOperation = ok ? op : NOTHING;
895   myViewPort->setMouseTracking(myOperation == NOTHING);
896   return ok;
897 }
898
899 /*!
900  Set enabled state of transformation (rotate, zoom, etc)
901  */
902 void XGUI_ViewWindow::setTransformEnabled(const OperationType id, const bool on)
903 {
904   if (id != NOTHING)
905     myStatus.insert(id, on);
906 }
907
908 /*!
909  \return enabled state of transformation (rotate, zoom, etc)
910  */
911 bool XGUI_ViewWindow::transformEnabled(const OperationType id) const
912 {
913   return myStatus.contains(id) ? myStatus[id] : true;
914 }
915
916 /*!
917  \brief Start panning operation.
918
919  Sets the corresponding cursor for the widget.
920  */
921 void XGUI_ViewWindow::activatePanning()
922 {
923   if (!transformRequested() && !myCursorIsHand)
924     myCursor = cursor();                // save old cursor
925
926   if (myOperation != PANVIEW) {
927     QCursor panCursor(Qt::SizeAllCursor);
928     if (setTransformRequested(PANVIEW))
929       myViewPort->setCursor(panCursor);
930   }
931 }
932
933 /*!
934   \brief Start global panning operation
935
936   Sets the corresponding cursor for the widget.
937 */
938 void XGUI_ViewWindow::activateGlobalPanning()
939 {
940   Handle(V3d_View) aView3d = myViewPort->getView();
941   if ( !aView3d.IsNull() ) {
942     QPixmap globalPanPixmap (imageCrossCursor);
943     QCursor glPanCursor (globalPanPixmap);
944     myCurScale = aView3d->Scale();
945     aView3d->FitAll(0.01, false);
946     myCursor = cursor();                // save old cursor
947     myViewPort->fitAll(); // fits view before selecting a new scene center
948     if( setTransformRequested( PANGLOBAL ) )
949       myViewPort->setCursor( glPanCursor );
950   }
951 }
952
953 /*!
954  \brief Start rotation operation
955
956  Sets the corresponding cursor for the widget.
957  */
958 void XGUI_ViewWindow::activateRotation()
959 {
960   if (!transformRequested() && !myCursorIsHand)
961     myCursor = cursor();                // save old cursor
962
963   if (myOperation != ROTATE) {
964     QPixmap rotatePixmap(imageRotateCursor);
965     QCursor rotCursor(rotatePixmap);
966     if (setTransformRequested(ROTATE))
967       myViewPort->setCursor(rotCursor);
968   }
969 }
970
971 /*!
972  \brief Reset the viewport to its initial state
973  ( no transformations in process etc. )
974  */
975 void XGUI_ViewWindow::resetState()
976 {
977   myDrawRect = false;
978
979   if (myRotationPointSelection) {
980     QCursor handCursor(Qt::PointingHandCursor);
981     myViewPort->setCursor(handCursor);
982   } else {
983     if (transformRequested() || myCursorIsHand)
984       myViewPort->setCursor(myCursor);
985     myCursorIsHand = false;
986   }
987
988   if (transformRequested())
989     emit vpTransformationFinished(myOperation);
990
991   setTransformInProcess(false);
992   setTransformRequested(NOTHING);
993 }
994
995 XGUI_ViewBackground XGUI_ViewWindow::background() const
996 {
997   return myViewPort ? myViewPort->background() : XGUI_ViewBackground();
998 }
999
1000 void XGUI_ViewWindow::setBackground(const XGUI_ViewBackground& theBackground)
1001 {
1002   if (myViewPort) 
1003         myViewPort->setBackground( theBackground );
1004 }
1005
1006 /*!
1007    \brief Create one more window with same content.
1008 */
1009 void XGUI_ViewWindow::cloneView()
1010 {
1011   QMdiSubWindow* vw = myViewer->createView();
1012   XGUI_ViewWindow* aNewWnd = static_cast<XGUI_ViewWindow*>(vw->widget());
1013   aNewWnd->viewPort()->syncronizeWith(myViewPort);
1014
1015   emit viewCloned( vw );
1016
1017   //  In order to avoid frosen background in toolbars when it shown as a second view
1018   QTimer::singleShot(20, vw, SLOT(setFocus()));
1019 }
1020
1021 void XGUI_ViewWindow::dumpView()
1022 {
1023   QString aFilter(tr("Images Files (*.bmp *.png *.jpg *.jpeg *.eps *.ps)"));
1024   QString aSelectedFilter;
1025   QString aFileName = QFileDialog::getSaveFileName(this, "Save picture", QString(), aFilter, &aSelectedFilter);
1026   if (!aFileName.isNull()) {
1027     QApplication::setOverrideCursor( Qt::WaitCursor );
1028     QImage aPicture = myViewPort->dumpView();
1029
1030     QString aFmt = extension(aFileName).toUpper();
1031     if( aFmt.isEmpty() )
1032       aFmt = QString( "BMP" ); // default format
1033     else if( aFmt == "JPG" )
1034       aFmt = "JPEG";
1035
1036     Handle(Visual3d_View) a3dView = myViewPort->getView()->View();
1037     if (aFmt == "PS")
1038       a3dView->Export(strdup(qPrintable(aFileName)), Graphic3d_EF_PostScript);
1039     else if (aFmt == "EPS")
1040       a3dView->Export(strdup(qPrintable(aFileName)), Graphic3d_EF_EnhPostScript);
1041     else
1042       aPicture.save( aFileName, aFmt.toLatin1() );
1043     QApplication::restoreOverrideCursor();
1044   }
1045 }
1046
1047 void XGUI_ViewWindow::fitAll()
1048 {
1049   emit vpTransformationStarted( FITALLVIEW );
1050   myViewPort->fitAll();
1051   emit vpTransformationFinished( FITALLVIEW );
1052 }
1053
1054 /*!
1055   \brief Starts fit operation.
1056
1057   Sets the corresponding cursor for the widget.
1058 */
1059 void XGUI_ViewWindow::activateWindowFit()
1060 {
1061   if ( !transformRequested() && !myCursorIsHand )
1062     myCursor = cursor();                /* save old cursor */
1063
1064   if ( myOperation != WINDOWFIT ) {
1065     QCursor handCursor (Qt::PointingHandCursor);
1066     if( setTransformRequested ( WINDOWFIT ) ) {
1067       myViewPort->setCursor ( handCursor );
1068       myCursorIsHand = true;
1069     }
1070   }
1071 }
1072
1073
1074 /*!
1075   \brief Perform "front view" transformation.
1076 */
1077 void XGUI_ViewWindow::frontView()
1078 {
1079   emit vpTransformationStarted ( FRONTVIEW );
1080   Handle(V3d_View) aView3d = myViewPort->getView();
1081   if ( !aView3d.IsNull() ) 
1082     aView3d->SetProj (V3d_Xpos);
1083   myViewPort->fitAll();
1084   emit vpTransformationFinished ( FRONTVIEW );
1085 }
1086
1087 /*!
1088   \brief Perform "back view" transformation.
1089 */
1090 void XGUI_ViewWindow::backView()
1091 {
1092   emit vpTransformationStarted ( BACKVIEW );
1093   Handle(V3d_View) aView3d = myViewPort->getView();
1094   if ( !aView3d.IsNull() ) 
1095     aView3d->SetProj (V3d_Xneg);
1096   myViewPort->fitAll();
1097   emit vpTransformationFinished ( BACKVIEW );
1098 }
1099
1100 /*!
1101   \brief Perform "top view" transformation.
1102 */
1103 void XGUI_ViewWindow::topView()
1104 {
1105   emit vpTransformationStarted ( TOPVIEW );
1106   Handle(V3d_View) aView3d = myViewPort->getView();
1107   if ( !aView3d.IsNull() ) 
1108     aView3d->SetProj (V3d_Zpos);
1109   myViewPort->fitAll();
1110   emit vpTransformationFinished ( TOPVIEW );
1111 }
1112
1113 /*!
1114   \brief Perform "bottom view" transformation.
1115 */
1116 void XGUI_ViewWindow::bottomView()
1117 {
1118   emit vpTransformationStarted ( BOTTOMVIEW );
1119   Handle(V3d_View) aView3d = myViewPort->getView();
1120   if ( !aView3d.IsNull() ) 
1121     aView3d->SetProj (V3d_Zneg);
1122   myViewPort->fitAll();
1123   emit vpTransformationFinished ( BOTTOMVIEW );
1124 }
1125
1126 /*!
1127   \brief Perform "left view" transformation.
1128 */
1129 void XGUI_ViewWindow::leftView()
1130 {
1131   emit vpTransformationStarted ( LEFTVIEW );
1132   Handle(V3d_View) aView3d = myViewPort->getView();
1133   if ( !aView3d.IsNull() ) 
1134     aView3d->SetProj (V3d_Yneg);
1135   myViewPort->fitAll();
1136   emit vpTransformationFinished ( LEFTVIEW );
1137 }
1138
1139 /*!
1140   \brief Perform "right view" transformation.
1141 */
1142 void XGUI_ViewWindow::rightView()
1143 {
1144   emit vpTransformationStarted ( RIGHTVIEW );
1145   Handle(V3d_View) aView3d = myViewPort->getView();
1146   if ( !aView3d.IsNull() ) 
1147     aView3d->SetProj (V3d_Ypos);
1148   myViewPort->fitAll();
1149   emit vpTransformationFinished ( RIGHTVIEW );
1150 }
1151
1152 void XGUI_ViewWindow::reset()
1153 {
1154   emit vpTransformationStarted( RESETVIEW );
1155   bool upd = myViewPort->getView()->SetImmediateUpdate( false );
1156   myViewPort->getView()->Reset( false );
1157   myViewPort->fitAll( false, true, false );
1158   myViewPort->getView()->SetImmediateUpdate( upd );
1159   myViewPort->getView()->Update();
1160   emit vpTransformationFinished( RESETVIEW );
1161 }
1162
1163
1164 void XGUI_ViewWindow::updateToolBar()
1165 {
1166   myGripWgt->update();
1167   myViewBar->update();
1168   myWindowBar->update();
1169   //QTimer::singleShot(50, Qt::VeryCoarseTimer, this, SLOT(repaintToolBar()));
1170 }
1171
1172 /*void XGUI_ViewWindow::repaintToolBar()
1173 {
1174   QApplication::sync();
1175   myGripWgt->repaint();
1176   myViewBar->repaint();
1177   myWindowBar->repaint();
1178 }*/