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     if (theEvent->type() == QEvent::KeyRelease) {
534       emit keyReleased(this, (QKeyEvent*) theEvent);
535       return true;
536     }
537   }
538   return QFrame::eventFilter(theObj, theEvent);
539 }
540
541 //****************************************************************
542 XGUI_ViewWindow::OperationType XGUI_ViewWindow::getButtonState(
543     QMouseEvent* theEvent, XGUI::InteractionStyle theInteractionStyle)
544 {
545   OperationType aOp = NOTHING;
546   XGUI::InteractionStyle aStyle = (XGUI::InteractionStyle) theInteractionStyle;
547   if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::ZOOM])
548       && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::ZOOM]))
549     aOp = ZOOMVIEW;
550   else if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::PAN])
551       && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::PAN]))
552     aOp = PANVIEW;
553   else if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::ROTATE])
554       && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::ROTATE])
555       && (my2dMode == XGUI::No2dMode))
556     aOp = ROTATE;
557
558   return aOp;
559 }
560
561 //****************************************************************
562 void XGUI_ViewWindow::vpMousePressEvent(QMouseEvent* theEvent)
563 {
564   myStartX = theEvent->x();
565   myStartY = theEvent->y();
566   XGUI::InteractionStyle anInteractionStyle = interactionStyle();
567
568   // in "key free" interaction style zoom operation is activated by two buttons (simultaneously pressed),
569   // which are assigned for pan and rotate - these operations are activated immediately after pressing 
570   // of the first button, so it is necessary to switch to zoom when the second button is pressed
571   bool aSwitchToZoom = false;
572   if ((anInteractionStyle == XGUI::KEY_FREE) && (myOperation == PANVIEW || myOperation == ROTATE)) {
573     aSwitchToZoom = getButtonState(theEvent, anInteractionStyle) == ZOOMVIEW;
574   }
575
576   switch(myOperation) {
577   case WINDOWFIT:
578     if (theEvent->button() == Qt::LeftButton)
579       emit vpTransformationStarted(WINDOWFIT);
580     break;
581
582   case PANGLOBAL:
583     if (theEvent->button() == Qt::LeftButton)
584       emit vpTransformationStarted(PANGLOBAL);
585     break;
586
587   case ZOOMVIEW:
588     if (theEvent->button() == Qt::LeftButton) {
589       myViewPort->startZoomAtPoint(myStartX, myStartY);
590       emit vpTransformationStarted(ZOOMVIEW);
591     }
592     break;
593
594   case PANVIEW:
595     if (aSwitchToZoom) {
596       myViewPort->startZoomAtPoint(myStartX, myStartY);
597       activateZoom();
598     } else if (theEvent->button() == Qt::LeftButton)
599       emit vpTransformationStarted(PANVIEW);
600     break;
601
602   case ROTATE:
603     if (aSwitchToZoom) {
604       myViewPort->startZoomAtPoint(myStartX, myStartY);
605       activateZoom();
606     } else if (theEvent->button() == Qt::LeftButton) {
607       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
608       emit vpTransformationStarted(ROTATE);
609     }
610     break;
611
612   default:
613     /*  Try to activate a transformation */
614     OperationType aState;
615     if (interactionStyle() == XGUI::STANDARD)
616       aState = getButtonState(theEvent, anInteractionStyle);
617     else {
618       aState = XGUI_ViewWindow::NOTHING;
619       myIsKeyFree = true;
620     }
621     switch(aState) {
622     case ZOOMVIEW:
623       myViewPort->startZoomAtPoint(myStartX, myStartY);
624       activateZoom();
625       break;
626     case PANVIEW:
627       activatePanning();
628       break;
629     case ROTATE:
630       activateRotation();
631       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
632       break;
633     default:
634       if (myRotationPointSelection) {
635         if (theEvent->button() == Qt::LeftButton) {
636           Handle(AIS_InteractiveContext) ic = myViewer->AISContext();
637           ic->Select();
638           for(ic->InitSelected(); ic->MoreSelected(); ic->NextSelected()) {
639             TopoDS_Shape aShape = ic->SelectedShape();
640             if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX) {
641               gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(ic->SelectedShape()));
642               /*if ( mySetRotationPointDlg ) {
643                myRotationPointSelection = false;
644                mySetRotationPointDlg->setCoords(aPnt.X(), aPnt.Y(), aPnt.Z());
645                }*/
646             } else {
647               myCurrPointType = myPrevPointType;
648               break;
649             }
650           }
651           if (ic->NbSelected() == 0)
652             myCurrPointType = myPrevPointType;
653           //if ( mySetRotationPointDlg ) mySetRotationPointDlg->toggleChange();
654           ic->CloseAllContexts();
655           myOperation = NOTHING;
656           myViewPort->setCursor(myCursor);
657           myCursorIsHand = false;
658           myRotationPointSelection = false;
659         }
660       } else
661         emit mousePressed(this, theEvent);
662       break;
663     }
664     /* notify that we start a transformation */
665     if (transformRequested())
666       emit vpTransformationStarted(myOperation);
667   }
668   if (transformRequested())
669     setTransformInProcess(true);
670
671   /* we may need it for sketching... */
672   /*    if ( l_mbPressEvent )
673    delete l_mbPressEvent;
674    l_mbPressEvent = new QMouseEvent( *theEvent );*/
675 }
676
677 //****************************************************************
678 void XGUI_ViewWindow::vpMouseReleaseEvent(QMouseEvent* theEvent)
679 {
680   switch(myOperation) {
681   case NOTHING: {
682     int prevState = myCurSketch;
683     /*            if(theEvent->button() == Qt::RightButton) {
684      QList<OCCViewer_ViewSketcher*>::Iterator it;
685      for ( it = mySketchers.begin(); it != mySketchers.end() && myCurSketch != -1; ++it ) {
686      OCCViewer_ViewSketcher* sk = (*it);
687      if( ( sk->sketchButton() & theEvent->button() ) && sk->sketchButton() == myCurSketch )
688      myCurSketch = -1;
689      }
690      }
691      */
692     emit mouseReleased(this, theEvent);
693     if (theEvent->button() == Qt::RightButton && prevState == -1) {
694       QContextMenuEvent aEvent(QContextMenuEvent::Mouse, theEvent->pos(), theEvent->globalPos());
695       emit contextMenuRequested(&aEvent);
696     }
697   }
698     break;
699   case ROTATE:
700     myViewPort->endRotation();
701     resetState();
702     break;
703
704   case PANVIEW:
705   case ZOOMVIEW:
706     resetState();
707     break;
708
709   case PANGLOBAL:
710     if (theEvent->button() == Qt::LeftButton) {
711       myViewPort->setCenter(theEvent->x(), theEvent->y());
712       myViewPort->getView()->SetScale(myCurScale);
713       resetState();
714     }
715     break;
716
717   case WINDOWFIT:
718     if (theEvent->button() == Qt::LeftButton) {
719       myCurrX = theEvent->x();
720       myCurrY = theEvent->y();
721       drawRect();
722       QRect rect = makeRect(myStartX, myStartY, myCurrX, myCurrY);
723       if (!rect.isEmpty())
724         myViewPort->fitRect(rect);
725       endDrawRect();
726       resetState();
727     }
728     break;
729   }
730
731   // NOTE: viewer 3D detects a rectangle of selection using this event
732   // so we must emit it BEFORE resetting the selection rectangle
733   if (theEvent->button() == Qt::LeftButton && myDrawRect) {
734     drawRect();
735     endDrawRect();
736     resetState();
737     myViewPort->update();
738   }
739   /*    if ( l_mbPressEvent ) {
740    delete l_mbPressEvent;
741    l_mbPressEvent = 0;
742    }*/
743 }
744
745 //****************************************************************
746 void XGUI_ViewWindow::vpMouseMoveEvent(QMouseEvent* theEvent)
747 {
748   if (myIsKeyFree && interactionStyle() == XGUI::KEY_FREE) {
749     myIsKeyFree = false;
750     switch(getButtonState(theEvent, interactionStyle())) {
751     case ZOOMVIEW:
752       myViewPort->startZoomAtPoint(myStartX, myStartY);
753       activateZoom();
754       break;
755     case PANVIEW:
756       activatePanning();
757       break;
758     case ROTATE:
759       activateRotation();
760       myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
761       break;
762     default:
763       break;
764     }
765   }
766
767   myCurrX = theEvent->x();
768   myCurrY = theEvent->y();
769   switch(myOperation) {
770   case ROTATE:
771     myViewPort->rotate(myCurrX, myCurrY, myCurrPointType, mySelectedPoint);
772     break;
773
774   case ZOOMVIEW:
775     myViewPort->zoom(myStartX, myStartY, myCurrX, myCurrY);
776     myStartX = myCurrX;
777     myStartY = myCurrY;
778     break;
779
780   case PANVIEW:
781     myViewPort->pan(myCurrX - myStartX, myStartY - myCurrY);
782     myStartX = myCurrX;
783     myStartY = myCurrY;
784     break;
785
786   case PANGLOBAL:
787     break;
788
789   default:
790     if (myRotationPointSelection /*|| isSketcherStyle()*/) {
791       emit mouseMoving(this, theEvent);
792     } else {
793       int aState = theEvent->modifiers();
794       int aButton = theEvent->buttons();
795       int anInteractionStyle = interactionStyle();
796       if (((anInteractionStyle == XGUI::STANDARD) && (aButton == Qt::LeftButton)
797           && (aState == Qt::NoModifier || Qt::ShiftModifier))
798           || ((anInteractionStyle == XGUI::KEY_FREE) && (aButton == Qt::LeftButton)
799               && (aState == Qt::ControlModifier
800                   || aState == (Qt::ControlModifier | Qt::ShiftModifier)))) {
801         myDrawRect = myEnableDrawMode;
802         if (myDrawRect) {
803           drawRect();
804           if (!myCursorIsHand) {   // we are going to sketch a rectangle
805             QCursor handCursor(Qt::PointingHandCursor);
806             myCursorIsHand = true;
807             myCursor = cursor();
808             myViewPort->setCursor(handCursor);
809           }
810         }
811         emit mouseMoving(this, theEvent);
812       } /* else if ( ( (anInteractionStyle == XGUI::STANDARD) &&
813        (aButton == Qt::RightButton) && 
814        ( aState == Qt::NoModifier || Qt::ShiftModifier ) ) ||
815        ( (anInteractionStyle == XGUI::KEY_FREE) &&
816        (aButton == Qt::RightButton) && 
817        ( aState == Qt::ControlModifier || aState == ( Qt::ControlModifier|Qt::ShiftModifier ) ) ) ) {
818        OCCViewer_ViewSketcher* sketcher = 0;
819        QList<OCCViewer_ViewSketcher*>::Iterator it;
820        for ( it = mySketchers.begin(); it != mySketchers.end() && !sketcher; ++it ) {
821        OCCViewer_ViewSketcher* sk = (*it);
822        if( sk->isDefault() && sk->sketchButton() == aButton )
823        sketcher = sk;
824        }
825        if ( sketcher && myCurSketch == -1 ) {
826        activateSketching( sketcher->type() );
827        if ( mypSketcher ) {
828        myCurSketch = mypSketcher->sketchButton();
829
830        if ( l_mbPressEvent )  {
831        QApplication::sendEvent( getViewPort(), l_mbPressEvent );
832        delete l_mbPressEvent;
833        l_mbPressEvent = 0;
834        }
835        QApplication::sendEvent( getViewPort(), theEvent );
836        }
837        }
838        } */else
839         emit mouseMoving(this, theEvent);
840     }
841   }
842 }
843
844 /*!
845  \brief Draw rubber band rectangle.
846  */
847 void XGUI_ViewWindow::drawRect()
848 {
849   if (!myRectBand) {
850     myRectBand = new XGUI_RectRubberBand(myViewPort);
851   }
852
853   myRectBand->setUpdatesEnabled(false);
854   QRect aRect = makeRect(myStartX, myStartY, myCurrX, myCurrY);
855   myRectBand->initGeometry(aRect);
856
857   if (!myRectBand->isVisible())
858     myRectBand->show();
859
860   myRectBand->setUpdatesEnabled(true);
861 }
862
863 /*!
864  \brief Clear rubber band rectangle on the end on the dragging operation.
865  */
866 void XGUI_ViewWindow::endDrawRect()
867 {
868   if (myRectBand) {
869     myRectBand->clearGeometry();
870     myRectBand->hide();
871   }
872 }
873
874 void XGUI_ViewWindow::activateZoom()
875 {
876   if (!transformRequested() && !myCursorIsHand)
877     myCursor = cursor(); /* save old cursor */
878
879   if (myOperation != ZOOMVIEW) {
880     QPixmap zoomPixmap(imageZoomCursor);
881     QCursor zoomCursor(zoomPixmap);
882     if (setTransformRequested(ZOOMVIEW))
883       myViewPort->setCursor(zoomCursor);
884   }
885 }
886
887 bool XGUI_ViewWindow::transformRequested() const
888 {
889   return (myOperation != NOTHING);
890 }
891
892 /*!
893  \brief Start delayed viewer operation.
894  */
895 bool XGUI_ViewWindow::setTransformRequested(OperationType op)
896 {
897   bool ok = transformEnabled(op);
898   myOperation = ok ? op : NOTHING;
899   myViewPort->setMouseTracking(myOperation == NOTHING);
900   return ok;
901 }
902
903 /*!
904  Set enabled state of transformation (rotate, zoom, etc)
905  */
906 void XGUI_ViewWindow::setTransformEnabled(const OperationType id, const bool on)
907 {
908   if (id != NOTHING)
909     myStatus.insert(id, on);
910 }
911
912 /*!
913  \return enabled state of transformation (rotate, zoom, etc)
914  */
915 bool XGUI_ViewWindow::transformEnabled(const OperationType id) const
916 {
917   return myStatus.contains(id) ? myStatus[id] : true;
918 }
919
920 /*!
921  \brief Start panning operation.
922
923  Sets the corresponding cursor for the widget.
924  */
925 void XGUI_ViewWindow::activatePanning()
926 {
927   if (!transformRequested() && !myCursorIsHand)
928     myCursor = cursor();                // save old cursor
929
930   if (myOperation != PANVIEW) {
931     QCursor panCursor(Qt::SizeAllCursor);
932     if (setTransformRequested(PANVIEW))
933       myViewPort->setCursor(panCursor);
934   }
935 }
936
937 /*!
938   \brief Start global panning operation
939
940   Sets the corresponding cursor for the widget.
941 */
942 void XGUI_ViewWindow::activateGlobalPanning()
943 {
944   Handle(V3d_View) aView3d = myViewPort->getView();
945   if ( !aView3d.IsNull() ) {
946     QPixmap globalPanPixmap (imageCrossCursor);
947     QCursor glPanCursor (globalPanPixmap);
948     myCurScale = aView3d->Scale();
949     aView3d->FitAll(0.01, false);
950     myCursor = cursor();                // save old cursor
951     myViewPort->fitAll(); // fits view before selecting a new scene center
952     if( setTransformRequested( PANGLOBAL ) )
953       myViewPort->setCursor( glPanCursor );
954   }
955 }
956
957 /*!
958  \brief Start rotation operation
959
960  Sets the corresponding cursor for the widget.
961  */
962 void XGUI_ViewWindow::activateRotation()
963 {
964   if (!transformRequested() && !myCursorIsHand)
965     myCursor = cursor();                // save old cursor
966
967   if (myOperation != ROTATE) {
968     QPixmap rotatePixmap(imageRotateCursor);
969     QCursor rotCursor(rotatePixmap);
970     if (setTransformRequested(ROTATE))
971       myViewPort->setCursor(rotCursor);
972   }
973 }
974
975 /*!
976  \brief Reset the viewport to its initial state
977  ( no transformations in process etc. )
978  */
979 void XGUI_ViewWindow::resetState()
980 {
981   myDrawRect = false;
982
983   if (myRotationPointSelection) {
984     QCursor handCursor(Qt::PointingHandCursor);
985     myViewPort->setCursor(handCursor);
986   } else {
987     if (transformRequested() || myCursorIsHand)
988       myViewPort->setCursor(myCursor);
989     myCursorIsHand = false;
990   }
991
992   if (transformRequested())
993     emit vpTransformationFinished(myOperation);
994
995   setTransformInProcess(false);
996   setTransformRequested(NOTHING);
997 }
998
999 XGUI_ViewBackground XGUI_ViewWindow::background() const
1000 {
1001   return myViewPort ? myViewPort->background() : XGUI_ViewBackground();
1002 }
1003
1004 void XGUI_ViewWindow::setBackground(const XGUI_ViewBackground& theBackground)
1005 {
1006   if (myViewPort) 
1007         myViewPort->setBackground( theBackground );
1008 }
1009
1010 /*!
1011    \brief Create one more window with same content.
1012 */
1013 void XGUI_ViewWindow::cloneView()
1014 {
1015   QMdiSubWindow* vw = myViewer->createView();
1016   XGUI_ViewWindow* aNewWnd = static_cast<XGUI_ViewWindow*>(vw->widget());
1017   aNewWnd->viewPort()->syncronizeWith(myViewPort);
1018
1019   emit viewCloned( vw );
1020
1021   //  In order to avoid frosen background in toolbars when it shown as a second view
1022   QTimer::singleShot(20, vw, SLOT(setFocus()));
1023 }
1024
1025 void XGUI_ViewWindow::dumpView()
1026 {
1027   QString aFilter(tr("Images Files (*.bmp *.png *.jpg *.jpeg *.eps *.ps)"));
1028   QString aSelectedFilter;
1029   QString aFileName = QFileDialog::getSaveFileName(this, "Save picture", QString(), aFilter, &aSelectedFilter);
1030   if (!aFileName.isNull()) {
1031     QApplication::setOverrideCursor( Qt::WaitCursor );
1032     QImage aPicture = myViewPort->dumpView();
1033
1034     QString aFmt = extension(aFileName).toUpper();
1035     if( aFmt.isEmpty() )
1036       aFmt = QString( "BMP" ); // default format
1037     else if( aFmt == "JPG" )
1038       aFmt = "JPEG";
1039
1040     Handle(Visual3d_View) a3dView = myViewPort->getView()->View();
1041     if (aFmt == "PS")
1042       a3dView->Export(strdup(qPrintable(aFileName)), Graphic3d_EF_PostScript);
1043     else if (aFmt == "EPS")
1044       a3dView->Export(strdup(qPrintable(aFileName)), Graphic3d_EF_EnhPostScript);
1045     else
1046       aPicture.save( aFileName, aFmt.toLatin1() );
1047     QApplication::restoreOverrideCursor();
1048   }
1049 }
1050
1051 void XGUI_ViewWindow::fitAll()
1052 {
1053   emit vpTransformationStarted( FITALLVIEW );
1054   myViewPort->fitAll();
1055   emit vpTransformationFinished( FITALLVIEW );
1056 }
1057
1058 /*!
1059   \brief Starts fit operation.
1060
1061   Sets the corresponding cursor for the widget.
1062 */
1063 void XGUI_ViewWindow::activateWindowFit()
1064 {
1065   if ( !transformRequested() && !myCursorIsHand )
1066     myCursor = cursor();                /* save old cursor */
1067
1068   if ( myOperation != WINDOWFIT ) {
1069     QCursor handCursor (Qt::PointingHandCursor);
1070     if( setTransformRequested ( WINDOWFIT ) ) {
1071       myViewPort->setCursor ( handCursor );
1072       myCursorIsHand = true;
1073     }
1074   }
1075 }
1076
1077
1078 /*!
1079   \brief Perform "front view" transformation.
1080 */
1081 void XGUI_ViewWindow::frontView()
1082 {
1083   emit vpTransformationStarted ( FRONTVIEW );
1084   Handle(V3d_View) aView3d = myViewPort->getView();
1085   if ( !aView3d.IsNull() ) 
1086     aView3d->SetProj (V3d_Xpos);
1087   myViewPort->fitAll();
1088   emit vpTransformationFinished ( FRONTVIEW );
1089 }
1090
1091 /*!
1092   \brief Perform "back view" transformation.
1093 */
1094 void XGUI_ViewWindow::backView()
1095 {
1096   emit vpTransformationStarted ( BACKVIEW );
1097   Handle(V3d_View) aView3d = myViewPort->getView();
1098   if ( !aView3d.IsNull() ) 
1099     aView3d->SetProj (V3d_Xneg);
1100   myViewPort->fitAll();
1101   emit vpTransformationFinished ( BACKVIEW );
1102 }
1103
1104 /*!
1105   \brief Perform "top view" transformation.
1106 */
1107 void XGUI_ViewWindow::topView()
1108 {
1109   emit vpTransformationStarted ( TOPVIEW );
1110   Handle(V3d_View) aView3d = myViewPort->getView();
1111   if ( !aView3d.IsNull() ) 
1112     aView3d->SetProj (V3d_Zpos);
1113   myViewPort->fitAll();
1114   emit vpTransformationFinished ( TOPVIEW );
1115 }
1116
1117 /*!
1118   \brief Perform "bottom view" transformation.
1119 */
1120 void XGUI_ViewWindow::bottomView()
1121 {
1122   emit vpTransformationStarted ( BOTTOMVIEW );
1123   Handle(V3d_View) aView3d = myViewPort->getView();
1124   if ( !aView3d.IsNull() ) 
1125     aView3d->SetProj (V3d_Zneg);
1126   myViewPort->fitAll();
1127   emit vpTransformationFinished ( BOTTOMVIEW );
1128 }
1129
1130 /*!
1131   \brief Perform "left view" transformation.
1132 */
1133 void XGUI_ViewWindow::leftView()
1134 {
1135   emit vpTransformationStarted ( LEFTVIEW );
1136   Handle(V3d_View) aView3d = myViewPort->getView();
1137   if ( !aView3d.IsNull() ) 
1138     aView3d->SetProj (V3d_Yneg);
1139   myViewPort->fitAll();
1140   emit vpTransformationFinished ( LEFTVIEW );
1141 }
1142
1143 /*!
1144   \brief Perform "right view" transformation.
1145 */
1146 void XGUI_ViewWindow::rightView()
1147 {
1148   emit vpTransformationStarted ( RIGHTVIEW );
1149   Handle(V3d_View) aView3d = myViewPort->getView();
1150   if ( !aView3d.IsNull() ) 
1151     aView3d->SetProj (V3d_Ypos);
1152   myViewPort->fitAll();
1153   emit vpTransformationFinished ( RIGHTVIEW );
1154 }
1155
1156 void XGUI_ViewWindow::reset()
1157 {
1158   emit vpTransformationStarted( RESETVIEW );
1159   bool upd = myViewPort->getView()->SetImmediateUpdate( false );
1160   myViewPort->getView()->Reset( false );
1161   myViewPort->fitAll( false, true, false );
1162   myViewPort->getView()->SetImmediateUpdate( upd );
1163   myViewPort->getView()->Update();
1164   emit vpTransformationFinished( RESETVIEW );
1165 }
1166
1167
1168 void XGUI_ViewWindow::updateToolBar()
1169 {
1170   myGripWgt->update();
1171   myViewBar->update();
1172   myWindowBar->update();
1173 }
1174