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