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