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"
11 #include <QResizeEvent>
12 #include <QApplication>
14 #include <QMdiSubWindow>
17 #include <QFileDialog>
18 #include <QStyleOptionToolBar>
20 #include <TopoDS_Shape.hxx>
21 #include <BRep_Tool.hxx>
23 #include <Visual3d_View.hxx>
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 "................................", "................................" };
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 "................................", "................................" };
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 "................................", "................................" };
81 ViewerToolbar::ViewerToolbar(QWidget* theParent, XGUI_ViewPort* thePort)
82 : QToolBar(theParent),
86 connect(myVPort, SIGNAL(resized()), this, SLOT(onViewPortResized()));
89 void ViewerToolbar::paintEvent(QPaintEvent* theEvent)
91 //QToolBar::paintEvent(theEvent);
93 QPainter aPainter(this);
95 QRect aVPRect = myVPort->rect();
96 QPoint aGlobPnt = mapToGlobal(aRect.topLeft());
97 QPoint aPnt = myVPort->mapFromGlobal(aGlobPnt);
100 QRect(aPnt.x(), aPnt.y() + aVPRect.height() - aRect.height(), aRect.width(), aRect.height()));
101 unsigned char* aData = 0;
102 QImage aImg = myVPort->dumpView(aData, aImgRect, myResize);
104 aPainter.drawImage(aRect, aImg);
108 QStyle *style = this->style();
109 QStyleOptionToolBar aOpt;
110 initStyleOption(&aOpt);
112 aOpt.rect = style->subElementRect(QStyle::SE_ToolBarHandle, &aOpt, this);
113 if (aOpt.rect.isValid())
114 style->drawPrimitive(QStyle::PE_IndicatorToolBarHandle, &aOpt, &aPainter, this);
119 //**************************************************************************
120 ViewerLabel::ViewerLabel(QWidget* theParent, XGUI_ViewPort* thePort)
125 connect(myVPort, SIGNAL(resized()), this, SLOT(onViewPortResized()));
128 void ViewerLabel::paintEvent(QPaintEvent* theEvent)
130 QRect aRect = rect();
131 QRect aVPRect = myVPort->rect();
132 QPoint aGlobPnt = mapToGlobal(aRect.topLeft());
133 QPoint aPnt = myVPort->mapFromGlobal(aGlobPnt);
136 QRect(aPnt.x(), aPnt.y() + aVPRect.height() - aRect.height(), aRect.width(), aRect.height()));
137 unsigned char* aData = 0;
138 QImage aImg = myVPort->dumpView(aData, aImgRect, myResize);
140 QPainter(this).drawImage(aRect, aImg);
142 QLabel::paintEvent(theEvent);
147 //**************************************************************************
148 //**************************************************************************
149 //**************************************************************************
150 XGUI_ViewWindow::XGUI_ViewWindow(XGUI_Viewer* theViewer, V3d_TypeOfView theType)
154 MinimizeIco(":pictures/wnd_minimize.png"),
155 MaximizeIco(":pictures/wnd_maximize.png"),
156 CloseIco(":pictures/wnd_close.png"),
157 RestoreIco(":pictures/wnd_restore.png"),
158 myInteractionStyle(XGUI::STANDARD),
161 my2dMode(XGUI::No2dMode),
162 myCurrPointType(XGUI::GRAVITY),
163 myPrevPointType(XGUI::GRAVITY),
164 myRotationPointSelection(false),
173 myEnableDrawMode(false),
174 myCursorIsHand(false),
175 myEventStarted(false),
177 myLastState(WindowNormalState),
178 myOperation(NOTHING),
182 mySelectedPoint = gp_Pnt(0., 0., 0.);
183 setFrameStyle(QFrame::Raised);
184 setFrameShape(QFrame::Panel);
185 setLineWidth(BORDER_SIZE);
186 setMouseTracking(true);
188 QVBoxLayout* aLay = new QVBoxLayout(this);
189 aLay->setContentsMargins(BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE);
190 myViewPort = new XGUI_ViewPort(this, myViewer->v3dViewer(), theType);
191 myViewPort->installEventFilter(this);
192 myViewPort->setCursor(Qt::ArrowCursor);
193 aLay->addWidget(myViewPort);
195 myPicture = new QLabel(this);
196 myPicture->setFrameStyle(QFrame::Sunken);
197 myPicture->setFrameShape(QFrame::Panel);
198 myPicture->setMouseTracking(true);
199 myPicture->installEventFilter(this);
200 aLay->addWidget(myPicture);
203 QVBoxLayout* aVPLay = new QVBoxLayout(myViewPort);
204 aVPLay->setMargin(0);
205 aVPLay->setSpacing(0);
206 aVPLay->setContentsMargins(0, 0, 0, 0);
208 QHBoxLayout* aToolLay = new QHBoxLayout();
209 aToolLay->setMargin(0);
210 aToolLay->setSpacing(0);
211 aToolLay->setContentsMargins(0, 0, 0, 0);
212 aVPLay->addLayout(aToolLay);
213 aVPLay->addStretch();
215 myGripWgt = new ViewerLabel(this, myViewPort);
216 myGripWgt->setPixmap(QPixmap(":pictures/wnd_grip.png"));
217 myGripWgt->setMouseTracking(true);
218 myGripWgt->installEventFilter(this);
219 myGripWgt->setCursor(Qt::OpenHandCursor);
220 aToolLay->addWidget(myGripWgt);
222 // Create Viewer management buttons
223 myViewBar = new ViewerToolbar(this, myViewPort);
224 myViewBar->setCursor(Qt::PointingHandCursor);
225 aToolLay->addWidget(myViewBar);
226 aToolLay->addStretch();
231 aBtn = new QAction(QIcon(":pictures/occ_view_camera_dump.png"), tr("Dump view"), myViewBar);
232 connect(aBtn, SIGNAL(triggered()), SLOT(dumpView()));
233 myViewBar->addAction(aBtn);
235 aBtn = new QAction(QIcon(":pictures/occ_view_fitall.png"), tr("Fit all"), myViewBar);
236 connect(aBtn, SIGNAL(triggered()), SLOT(fitAll()));
237 myViewBar->addAction(aBtn);
239 aBtn = new QAction(QIcon(":pictures/occ_view_fitarea.png"), tr("Fit area"), myViewBar);
240 connect(aBtn, SIGNAL(triggered()), SLOT(activateWindowFit()));
241 myViewBar->addAction(aBtn);
243 aBtn = new QAction(QIcon(":pictures/occ_view_zoom.png"), tr("Zoom"), myViewBar);
244 connect(aBtn, SIGNAL(triggered()), SLOT(activateZoom()));
245 myViewBar->addAction(aBtn);
247 aBtn = new QAction(QIcon(":pictures/occ_view_pan.png"), tr("Panning"), myViewBar);
248 connect(aBtn, SIGNAL(triggered()), SLOT(activatePanning()));
249 myViewBar->addAction(aBtn);
251 aBtn = new QAction(QIcon(":pictures/occ_view_glpan.png"), tr("Global panning"), myViewBar);
252 connect(aBtn, SIGNAL(triggered()), SLOT(activateGlobalPanning()));
253 myViewBar->addAction(aBtn);
255 aBtn = new QAction(QIcon(":pictures/occ_view_rotate.png"), tr("Rotate"), myViewBar);
256 connect(aBtn, SIGNAL(triggered()), SLOT(activateRotation()));
257 myViewBar->addAction(aBtn);
259 aBtn = new QAction(QIcon(":pictures/occ_view_reset.png"), tr("Reset"), myViewBar);
260 connect(aBtn, SIGNAL(triggered()), SLOT(reset()));
261 myViewBar->addAction(aBtn);
263 aBtn = new QAction(QIcon(":pictures/occ_view_front.png"), tr("Front"), myViewBar);
264 connect(aBtn, SIGNAL(triggered()), SLOT(frontView()));
265 myViewBar->addAction(aBtn);
267 aBtn = new QAction(QIcon(":pictures/occ_view_back.png"), tr("Back"), myViewBar);
268 connect(aBtn, SIGNAL(triggered()), SLOT(backView()));
269 myViewBar->addAction(aBtn);
271 aBtn = new QAction(QIcon(":pictures/occ_view_top.png"), tr("Top"), myViewBar);
272 connect(aBtn, SIGNAL(triggered()), SLOT(topView()));
273 myViewBar->addAction(aBtn);
275 aBtn = new QAction(QIcon(":pictures/occ_view_bottom.png"), tr("Bottom"), myViewBar);
276 connect(aBtn, SIGNAL(triggered()), SLOT(bottomView()));
277 myViewBar->addAction(aBtn);
279 aBtn = new QAction(QIcon(":pictures/occ_view_left.png"), tr("Left"), myViewBar);
280 connect(aBtn, SIGNAL(triggered()), SLOT(leftView()));
281 myViewBar->addAction(aBtn);
283 aBtn = new QAction(QIcon(":pictures/occ_view_right.png"), tr("Right"), myViewBar);
284 connect(aBtn, SIGNAL(triggered()), SLOT(rightView()));
285 myViewBar->addAction(aBtn);
287 aBtn = new QAction(QIcon(":pictures/occ_view_clone.png"), tr("Clone"), myViewBar);
288 connect(aBtn, SIGNAL(triggered()), SLOT(cloneView()));
289 myViewBar->addAction(aBtn);
291 // Create Window management buttons
292 myWindowBar = new ViewerToolbar(this, myViewPort);
293 myWindowBar->setCursor(Qt::PointingHandCursor);
294 aToolLay->addWidget(myWindowBar);
296 myMinimizeBtn = new QAction(myWindowBar);
297 myMinimizeBtn->setIcon(MinimizeIco);
298 myWindowBar->addAction(myMinimizeBtn);
299 connect(myMinimizeBtn, SIGNAL(triggered()), SLOT(onMinimize()));
301 myMaximizeBtn = new QAction(myWindowBar);
302 myMaximizeBtn->setIcon(MaximizeIco);
303 myWindowBar->addAction(myMaximizeBtn);
304 connect(myMaximizeBtn, SIGNAL(triggered()), SLOT(onMaximize()));
306 aBtn = new QAction(myWindowBar);
307 aBtn->setIcon(CloseIco);
308 myWindowBar->addAction(aBtn);
309 connect(aBtn, SIGNAL(triggered()), SLOT(onClose()));
311 //Support copy of background on updating of viewer
312 connect(myViewPort, SIGNAL(vpTransformed()), this, SLOT(updateToolBar()));
313 connect(myViewPort, SIGNAL(vpUpdated()), this, SLOT(updateToolBar()));
314 connect(this, SIGNAL(vpTransformationFinished(XGUI_ViewWindow::OperationType)), this,
315 SLOT(updateToolBar()));
318 //****************************************************************
319 XGUI_ViewWindow::~XGUI_ViewWindow()
323 //****************************************************************
324 void XGUI_ViewWindow::showEvent(QShowEvent* theEvent)
326 QFrame::showEvent(theEvent);
327 myWindowBar->setFixedSize(myWindowBar->sizeHint());
330 //****************************************************************
331 void XGUI_ViewWindow::changeEvent(QEvent* theEvent)
334 if (theEvent->type() == QEvent::WindowStateChange) {
336 if (myPicture->isHidden()) {
344 if (myPicture->isVisible()) {
349 myMinimizeBtn->setIcon(MinimizeIco);
350 myMaximizeBtn->setIcon(RestoreIco);
352 myViewBar->setVisible(myIsActive);
353 myWindowBar->setVisible(myIsActive);
354 myGripWgt->setVisible(myIsActive && (!isMaximized()));
357 QWidget::changeEvent(theEvent);
360 //****************************************************************
361 void XGUI_ViewWindow::windowActivated()
363 if (!(isMinimized() || parentWidget()->isMinimized())) {
365 if (isMaximized() || parentWidget()->isMaximized()) {
366 myMaximizeBtn->setIcon(RestoreIco);
368 myMaximizeBtn->setIcon(MaximizeIco);
372 myGripWgt->setVisible(
373 !(isMaximized() || isMinimized() || parentWidget()->isMaximized()
374 || parentWidget()->isMinimized()));
379 //****************************************************************
380 void XGUI_ViewWindow::windowDeactivated()
383 if (!(isMinimized() || parentWidget()->isMinimized())) {
384 if (isMaximized() || parentWidget()->isMaximized()) {
385 myMaximizeBtn->setIcon(RestoreIco);
387 myMaximizeBtn->setIcon(MaximizeIco);
395 //****************************************************************
396 void XGUI_ViewWindow::onClose()
398 if (parentWidget()) {
399 emit tryClosing(this);
401 emit closed(static_cast<QMdiSubWindow*>(parentWidget()));
402 parentWidget()->close();
407 //****************************************************************
408 void XGUI_ViewWindow::onMinimize()
410 unsigned char* aData = 0;
411 QPixmap aPMap = QPixmap::fromImage(myViewPort->dumpView(aData));
414 double aR = aW / 100.;
415 int aNewH = int(aH / aR);
416 myPicture->setPixmap(aPMap.scaled(100, aNewH));
419 (isMaximized() || parentWidget()->isMaximized()) ? MaximizedState : WindowNormalState;
421 parentWidget()->showMinimized();
422 parentWidget()->setGeometry(parentWidget()->x(), parentWidget()->y(), 100, aNewH);
423 parentWidget()->lower();
425 myViewer->onWindowMinimized((QMdiSubWindow*) parentWidget());
429 //****************************************************************
430 void XGUI_ViewWindow::onMaximize()
432 if (isMaximized() || parentWidget()->isMaximized()) {
433 myMaximizeBtn->setIcon(MaximizeIco);
436 parentWidget()->showNormal();
438 myMaximizeBtn->setIcon(RestoreIco);
441 parentWidget()->showMaximized();
443 parentWidget()->activateWindow();
444 myMinimizeBtn->setIcon(MinimizeIco);
446 // In order to avoid frosen background in toolbars when it shown as a second view
447 QTimer::singleShot(50, parentWidget(), SLOT(setFocus()));
450 //****************************************************************
451 bool XGUI_ViewWindow::processWindowControls(QObject *theObj, QEvent *theEvent)
453 switch (theEvent->type()) {
454 case QEvent::MouseButtonPress: {
455 QMouseEvent* aEvent = static_cast<QMouseEvent*>(theEvent);
456 if ((aEvent->button() == Qt::LeftButton) && (!myMoving)) {
458 myMousePnt = aEvent->globalPos();
463 case QEvent::MouseButtonRelease: {
464 QMouseEvent* aEvent = static_cast<QMouseEvent*>(theEvent);
465 if ((aEvent->button() == Qt::LeftButton) && myMoving) {
471 case QEvent::MouseMove: {
472 QMouseEvent* aEvent = static_cast<QMouseEvent*>(theEvent);
474 QMdiSubWindow* aParent = static_cast<QMdiSubWindow*>(parentWidget());
475 QMdiArea* aMDIArea = aParent->mdiArea();
477 QPoint aPnt = aEvent->globalPos();
478 QPoint aMDIPnt = aMDIArea->mapFromGlobal(aPnt);
479 if (aMDIArea->rect().contains(aMDIPnt)) {
480 int aX = aParent->x() + (aPnt.x() - myMousePnt.x());
481 int aY = aParent->y() + (aPnt.y() - myMousePnt.y());
482 aParent->move(aX, aY);
489 case QEvent::MouseButtonDblClick:
490 if (theObj == myPicture) {
492 if (myLastState == MaximizedState) {
497 myViewer->onWindowActivated((QMdiSubWindow*) parentWidget());
499 // In order to avoid frosen background in toolbars when it shown as a second view
500 QTimer::singleShot(20, parentWidget(), SLOT(setFocus()));
508 //****************************************************************
509 bool XGUI_ViewWindow::processViewPort(QEvent *theEvent)
511 switch (theEvent->type()) {
512 case QEvent::MouseButtonPress:
513 vpMousePressEvent((QMouseEvent*) theEvent);
516 case QEvent::MouseButtonRelease:
517 vpMouseReleaseEvent((QMouseEvent*) theEvent);
520 case QEvent::MouseMove:
521 vpMouseMoveEvent((QMouseEvent*) theEvent);
524 case QEvent::MouseButtonDblClick:
525 emit mouseDoubleClicked(this, (QMouseEvent*) theEvent);
527 case QEvent::Wheel: {
528 QWheelEvent* aEvent = (QWheelEvent*) theEvent;
529 myViewPort->startZoomAtPoint(aEvent->x(), aEvent->y());
530 double aDelta = (double) (aEvent->delta()) / (15 * 8);
533 int x1 = (int) (aEvent->x() + width() * aDelta / 100);
534 int y1 = (int) (aEvent->y() + height() * aDelta / 100);
535 myViewPort->zoom(x, y, x1, y1);
542 //****************************************************************
543 bool XGUI_ViewWindow::eventFilter(QObject *theObj, QEvent *theEvent)
545 if ((theObj == myGripWgt) || (theObj == myPicture)) {
546 if (processWindowControls(theObj, theEvent))
548 } else if (theObj == myViewPort) {
549 if (processViewPort(theEvent)) {
552 if (theEvent->type() == QEvent::KeyRelease) {
553 emit keyReleased(this, (QKeyEvent*) theEvent);
557 return QFrame::eventFilter(theObj, theEvent);
560 //****************************************************************
561 XGUI_ViewWindow::OperationType XGUI_ViewWindow::getButtonState(
562 QMouseEvent* theEvent, XGUI::InteractionStyle theInteractionStyle)
564 OperationType aOp = NOTHING;
565 XGUI::InteractionStyle aStyle = (XGUI::InteractionStyle) theInteractionStyle;
566 if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::ZOOM])
567 && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::ZOOM]))
569 else if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::PAN])
570 && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::PAN]))
572 else if ((theEvent->modifiers() == XGUI_Viewer::myStateMap[aStyle][XGUI::ROTATE])
573 && (theEvent->buttons() == XGUI_Viewer::myButtonMap[aStyle][XGUI::ROTATE])
574 && (my2dMode == XGUI::No2dMode))
580 //****************************************************************
581 void XGUI_ViewWindow::vpMousePressEvent(QMouseEvent* theEvent)
583 myStartX = theEvent->x();
584 myStartY = theEvent->y();
585 XGUI::InteractionStyle anInteractionStyle = interactionStyle();
587 // in "key free" interaction style zoom operation is activated by two buttons (simultaneously pressed),
588 // which are assigned for pan and rotate - these operations are activated immediately after pressing
589 // of the first button, so it is necessary to switch to zoom when the second button is pressed
590 bool aSwitchToZoom = false;
591 if ((anInteractionStyle == XGUI::KEY_FREE) && (myOperation == PANVIEW || myOperation == ROTATE)) {
592 aSwitchToZoom = getButtonState(theEvent, anInteractionStyle) == ZOOMVIEW;
595 switch (myOperation) {
597 if (theEvent->button() == Qt::LeftButton)
598 emit vpTransformationStarted(WINDOWFIT);
602 if (theEvent->button() == Qt::LeftButton)
603 emit vpTransformationStarted(PANGLOBAL);
607 if (theEvent->button() == Qt::LeftButton) {
608 myViewPort->startZoomAtPoint(myStartX, myStartY);
609 emit vpTransformationStarted(ZOOMVIEW);
615 myViewPort->startZoomAtPoint(myStartX, myStartY);
617 } else if (theEvent->button() == Qt::LeftButton)
618 emit vpTransformationStarted(PANVIEW);
623 myViewPort->startZoomAtPoint(myStartX, myStartY);
625 } else if (theEvent->button() == Qt::LeftButton) {
626 myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
627 emit vpTransformationStarted(ROTATE);
632 /* Try to activate a transformation */
633 OperationType aState;
634 if (interactionStyle() == XGUI::STANDARD)
635 aState = getButtonState(theEvent, anInteractionStyle);
637 aState = XGUI_ViewWindow::NOTHING;
642 myViewPort->startZoomAtPoint(myStartX, myStartY);
650 myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
653 if (myRotationPointSelection) {
654 if (theEvent->button() == Qt::LeftButton) {
655 Handle(AIS_InteractiveContext) ic = myViewer->AISContext();
657 for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected()) {
658 TopoDS_Shape aShape = ic->SelectedShape();
659 if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX) {
660 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(ic->SelectedShape()));
661 /*if ( mySetRotationPointDlg ) {
662 myRotationPointSelection = false;
663 mySetRotationPointDlg->setCoords(aPnt.X(), aPnt.Y(), aPnt.Z());
666 myCurrPointType = myPrevPointType;
670 if (ic->NbSelected() == 0)
671 myCurrPointType = myPrevPointType;
672 //if ( mySetRotationPointDlg ) mySetRotationPointDlg->toggleChange();
673 ic->CloseAllContexts();
674 myOperation = NOTHING;
675 myViewPort->setCursor(myCursor);
676 myCursorIsHand = false;
677 myRotationPointSelection = false;
680 emit mousePressed(this, theEvent);
683 /* notify that we start a transformation */
684 if (transformRequested())
685 emit vpTransformationStarted(myOperation);
687 if (transformRequested())
688 setTransformInProcess(true);
690 /* we may need it for sketching... */
691 /* if ( l_mbPressEvent )
692 delete l_mbPressEvent;
693 l_mbPressEvent = new QMouseEvent( *theEvent );*/
696 //****************************************************************
697 void XGUI_ViewWindow::contextMenuEvent(QContextMenuEvent* theEvent)
699 if (theEvent->modifiers() == Qt::NoModifier) {
700 // Temporary: has to be removed when viewer popup will be defined
701 //QFrame::contextMenuEvent(theEvent);
702 emit contextMenuRequested(theEvent);
706 //****************************************************************
707 void XGUI_ViewWindow::vpMouseReleaseEvent(QMouseEvent* theEvent)
709 switch (myOperation) {
711 int prevState = myCurSketch;
712 /* if(theEvent->button() == Qt::RightButton) {
713 QList<OCCViewer_ViewSketcher*>::Iterator it;
714 for ( it = mySketchers.begin(); it != mySketchers.end() && myCurSketch != -1; ++it ) {
715 OCCViewer_ViewSketcher* sk = (*it);
716 if( ( sk->sketchButton() & theEvent->button() ) && sk->sketchButton() == myCurSketch )
721 emit mouseReleased(this, theEvent);
725 myViewPort->endRotation();
735 if (theEvent->button() == Qt::LeftButton) {
736 myViewPort->setCenter(theEvent->x(), theEvent->y());
737 myViewPort->getView()->SetScale(myCurScale);
743 if (theEvent->button() == Qt::LeftButton) {
744 myCurrX = theEvent->x();
745 myCurrY = theEvent->y();
747 QRect rect = XGUI_Tools::makeRect(myStartX, myStartY, myCurrX, myCurrY);
749 myViewPort->fitRect(rect);
756 // NOTE: viewer 3D detects a rectangle of selection using this event
757 // so we must emit it BEFORE resetting the selection rectangle
758 if (theEvent->button() == Qt::LeftButton && myDrawRect) {
762 myViewPort->update();
764 /* if ( l_mbPressEvent ) {
765 delete l_mbPressEvent;
770 //****************************************************************
771 void XGUI_ViewWindow::vpMouseMoveEvent(QMouseEvent* theEvent)
773 if (myIsKeyFree && interactionStyle() == XGUI::KEY_FREE) {
775 switch (getButtonState(theEvent, interactionStyle())) {
777 myViewPort->startZoomAtPoint(myStartX, myStartY);
785 myViewPort->startRotation(myStartX, myStartY, myCurrPointType, mySelectedPoint);
792 myCurrX = theEvent->x();
793 myCurrY = theEvent->y();
794 switch (myOperation) {
796 myViewPort->rotate(myCurrX, myCurrY, myCurrPointType, mySelectedPoint);
800 myViewPort->zoom(myStartX, myStartY, myCurrX, myCurrY);
806 myViewPort->pan(myCurrX - myStartX, myStartY - myCurrY);
815 if (myRotationPointSelection /*|| isSketcherStyle()*/) {
816 emit mouseMoving(this, theEvent);
818 int aState = theEvent->modifiers();
819 int aButton = theEvent->buttons();
820 int anInteractionStyle = interactionStyle();
821 if (((anInteractionStyle == XGUI::STANDARD) && (aButton == Qt::LeftButton)
822 && (aState == Qt::NoModifier || Qt::ShiftModifier))
823 || ((anInteractionStyle == XGUI::KEY_FREE) && (aButton == Qt::LeftButton)
824 && (aState == Qt::ControlModifier
825 || aState == (Qt::ControlModifier | Qt::ShiftModifier)))) {
826 myDrawRect = myEnableDrawMode;
829 if (!myCursorIsHand) { // we are going to sketch a rectangle
830 QCursor handCursor(Qt::PointingHandCursor);
831 myCursorIsHand = true;
833 myViewPort->setCursor(handCursor);
836 emit mouseMoving(this, theEvent);
837 } /* else if ( ( (anInteractionStyle == XGUI::STANDARD) &&
838 (aButton == Qt::RightButton) &&
839 ( aState == Qt::NoModifier || Qt::ShiftModifier ) ) ||
840 ( (anInteractionStyle == XGUI::KEY_FREE) &&
841 (aButton == Qt::RightButton) &&
842 ( aState == Qt::ControlModifier || aState == ( Qt::ControlModifier|Qt::ShiftModifier ) ) ) ) {
843 OCCViewer_ViewSketcher* sketcher = 0;
844 QList<OCCViewer_ViewSketcher*>::Iterator it;
845 for ( it = mySketchers.begin(); it != mySketchers.end() && !sketcher; ++it ) {
846 OCCViewer_ViewSketcher* sk = (*it);
847 if( sk->isDefault() && sk->sketchButton() == aButton )
850 if ( sketcher && myCurSketch == -1 ) {
851 activateSketching( sketcher->type() );
853 myCurSketch = mypSketcher->sketchButton();
855 if ( l_mbPressEvent ) {
856 QApplication::sendEvent( getViewPort(), l_mbPressEvent );
857 delete l_mbPressEvent;
860 QApplication::sendEvent( getViewPort(), theEvent );
864 emit mouseMoving(this, theEvent);
870 \brief Draw rubber band rectangle.
872 void XGUI_ViewWindow::drawRect()
875 myRectBand = new XGUI_RectRubberBand(myViewPort);
878 myRectBand->setUpdatesEnabled(false);
879 QRect aRect = XGUI_Tools::makeRect(myStartX, myStartY, myCurrX, myCurrY);
880 myRectBand->initGeometry(aRect);
882 if (!myRectBand->isVisible())
885 myRectBand->setUpdatesEnabled(true);
889 \brief Clear rubber band rectangle on the end on the dragging operation.
891 void XGUI_ViewWindow::endDrawRect()
894 myRectBand->clearGeometry();
899 void XGUI_ViewWindow::activateZoom()
901 if (!transformRequested() && !myCursorIsHand)
902 myCursor = cursor(); /* save old cursor */
904 if (myOperation != ZOOMVIEW) {
905 QPixmap zoomPixmap(imageZoomCursor);
906 QCursor zoomCursor(zoomPixmap);
907 if (setTransformRequested(ZOOMVIEW))
908 myViewPort->setCursor(zoomCursor);
912 bool XGUI_ViewWindow::transformRequested() const
914 return (myOperation != NOTHING);
918 \brief Start delayed viewer operation.
920 bool XGUI_ViewWindow::setTransformRequested(OperationType op)
922 bool ok = transformEnabled(op);
923 myOperation = ok ? op : NOTHING;
924 myViewPort->setMouseTracking(myOperation == NOTHING);
929 Set enabled state of transformation (rotate, zoom, etc)
931 void XGUI_ViewWindow::setTransformEnabled(const OperationType id, const bool on)
934 myStatus.insert(id, on);
938 \return enabled state of transformation (rotate, zoom, etc)
940 bool XGUI_ViewWindow::transformEnabled(const OperationType id) const
942 return myStatus.contains(id) ? myStatus[id] : true;
946 \brief Start panning operation.
948 Sets the corresponding cursor for the widget.
950 void XGUI_ViewWindow::activatePanning()
952 if (!transformRequested() && !myCursorIsHand)
953 myCursor = cursor(); // save old cursor
955 if (myOperation != PANVIEW) {
956 QCursor panCursor(Qt::SizeAllCursor);
957 if (setTransformRequested(PANVIEW))
958 myViewPort->setCursor(panCursor);
963 \brief Start global panning operation
965 Sets the corresponding cursor for the widget.
967 void XGUI_ViewWindow::activateGlobalPanning()
969 Handle(V3d_View) aView3d = myViewPort->getView();
970 if (!aView3d.IsNull()) {
971 QPixmap globalPanPixmap(imageCrossCursor);
972 QCursor glPanCursor(globalPanPixmap);
973 myCurScale = aView3d->Scale();
974 aView3d->FitAll(0.01, false);
975 myCursor = cursor(); // save old cursor
976 myViewPort->fitAll(); // fits view before selecting a new scene center
977 if (setTransformRequested(PANGLOBAL))
978 myViewPort->setCursor(glPanCursor);
983 \brief Start rotation operation
985 Sets the corresponding cursor for the widget.
987 void XGUI_ViewWindow::activateRotation()
989 if (!transformRequested() && !myCursorIsHand)
990 myCursor = cursor(); // save old cursor
992 if (myOperation != ROTATE) {
993 QPixmap rotatePixmap(imageRotateCursor);
994 QCursor rotCursor(rotatePixmap);
995 if (setTransformRequested(ROTATE))
996 myViewPort->setCursor(rotCursor);
1001 \brief Reset the viewport to its initial state
1002 ( no transformations in process etc. )
1004 void XGUI_ViewWindow::resetState()
1008 if (myRotationPointSelection) {
1009 QCursor handCursor(Qt::PointingHandCursor);
1010 myViewPort->setCursor(handCursor);
1012 if (transformRequested() || myCursorIsHand)
1013 myViewPort->setCursor(myCursor);
1014 myCursorIsHand = false;
1017 if (transformRequested())
1018 emit vpTransformationFinished(myOperation);
1020 setTransformInProcess(false);
1021 setTransformRequested(NOTHING);
1024 Qtx::BackgroundData XGUI_ViewWindow::background() const
1026 return myViewPort ? myViewPort->background() : Qtx::BackgroundData();
1029 void XGUI_ViewWindow::setBackground(const Qtx::BackgroundData& theBackground)
1032 myViewPort->setBackground(theBackground);
1036 \brief Create one more window with same content.
1038 void XGUI_ViewWindow::cloneView()
1040 QMdiSubWindow* vw = myViewer->createView();
1041 XGUI_ViewWindow* aNewWnd = static_cast<XGUI_ViewWindow*>(vw->widget());
1042 aNewWnd->viewPort()->syncronizeWith(myViewPort);
1044 emit viewCloned(vw);
1046 // In order to avoid frosen background in toolbars when it shown as a second view
1047 QTimer::singleShot(20, vw, SLOT(setFocus()));
1050 void XGUI_ViewWindow::dumpView()
1052 QString aFilter(tr("Images Files (*.bmp *.png *.jpg *.jpeg *.eps *.ps)"));
1053 QString aSelectedFilter;
1054 QString aFileName = QFileDialog::getSaveFileName(this, "Save picture", QString(), aFilter,
1056 if (!aFileName.isNull()) {
1057 QApplication::setOverrideCursor(Qt::WaitCursor);
1058 unsigned char* aData = 0;
1059 QImage aPicture = myViewPort->dumpView(aData);
1061 QString aFmt = XGUI_Tools::extension(aFileName).toUpper();
1063 aFmt = QString("BMP"); // default format
1064 else if (aFmt == "JPG")
1067 Handle(Visual3d_View) a3dView = myViewPort->getView()->View();
1070 a3dView->Export(_strdup(qPrintable(aFileName)), Graphic3d_EF_PostScript);
1072 a3dView->Export(strdup(qPrintable(aFileName)), Graphic3d_EF_PostScript);
1074 else if (aFmt == "EPS")
1076 a3dView->Export(_strdup(qPrintable(aFileName)), Graphic3d_EF_EnhPostScript);
1078 a3dView->Export(strdup(qPrintable(aFileName)), Graphic3d_EF_EnhPostScript);
1081 aPicture.save(aFileName, aFmt.toLatin1());
1083 QApplication::restoreOverrideCursor();
1087 void XGUI_ViewWindow::fitAll()
1089 emit vpTransformationStarted(FITALLVIEW);
1090 myViewPort->fitAll();
1091 emit vpTransformationFinished(FITALLVIEW);
1095 \brief Starts fit operation.
1097 Sets the corresponding cursor for the widget.
1099 void XGUI_ViewWindow::activateWindowFit()
1101 if (!transformRequested() && !myCursorIsHand)
1102 myCursor = cursor(); /* save old cursor */
1104 if (myOperation != WINDOWFIT) {
1105 QCursor handCursor(Qt::PointingHandCursor);
1106 if (setTransformRequested(WINDOWFIT)) {
1107 myViewPort->setCursor(handCursor);
1108 myCursorIsHand = true;
1114 \brief Perform "front view" transformation.
1116 void XGUI_ViewWindow::frontView()
1118 emit vpTransformationStarted(FRONTVIEW);
1119 Handle(V3d_View) aView3d = myViewPort->getView();
1120 if (!aView3d.IsNull())
1121 aView3d->SetProj(V3d_Xpos);
1122 myViewPort->fitAll();
1123 emit vpTransformationFinished(FRONTVIEW);
1127 \brief Perform "back view" transformation.
1129 void XGUI_ViewWindow::backView()
1131 emit vpTransformationStarted(BACKVIEW);
1132 Handle(V3d_View) aView3d = myViewPort->getView();
1133 if (!aView3d.IsNull())
1134 aView3d->SetProj(V3d_Xneg);
1135 myViewPort->fitAll();
1136 emit vpTransformationFinished(BACKVIEW);
1140 \brief Perform "top view" transformation.
1142 void XGUI_ViewWindow::topView()
1144 emit vpTransformationStarted(TOPVIEW);
1145 Handle(V3d_View) aView3d = myViewPort->getView();
1146 if (!aView3d.IsNull())
1147 aView3d->SetProj(V3d_Zpos);
1148 myViewPort->fitAll();
1149 emit vpTransformationFinished(TOPVIEW);
1153 \brief Perform "bottom view" transformation.
1155 void XGUI_ViewWindow::bottomView()
1157 emit vpTransformationStarted(BOTTOMVIEW);
1158 Handle(V3d_View) aView3d = myViewPort->getView();
1159 if (!aView3d.IsNull())
1160 aView3d->SetProj(V3d_Zneg);
1161 myViewPort->fitAll();
1162 emit vpTransformationFinished(BOTTOMVIEW);
1166 \brief Perform "left view" transformation.
1168 void XGUI_ViewWindow::leftView()
1170 emit vpTransformationStarted(LEFTVIEW);
1171 Handle(V3d_View) aView3d = myViewPort->getView();
1172 if (!aView3d.IsNull())
1173 aView3d->SetProj(V3d_Yneg);
1174 myViewPort->fitAll();
1175 emit vpTransformationFinished(LEFTVIEW);
1179 \brief Perform "right view" transformation.
1181 void XGUI_ViewWindow::rightView()
1183 emit vpTransformationStarted(RIGHTVIEW);
1184 Handle(V3d_View) aView3d = myViewPort->getView();
1185 if (!aView3d.IsNull())
1186 aView3d->SetProj(V3d_Ypos);
1187 myViewPort->fitAll();
1188 emit vpTransformationFinished(RIGHTVIEW);
1191 void XGUI_ViewWindow::reset()
1193 emit vpTransformationStarted(RESETVIEW);
1194 bool upd = myViewPort->getView()->SetImmediateUpdate(false);
1195 myViewPort->getView()->Reset(false);
1196 myViewPort->fitAll(false, true, false);
1197 myViewPort->getView()->SetImmediateUpdate(upd);
1198 myViewPort->getView()->Update();
1199 emit vpTransformationFinished(RESETVIEW);
1202 void XGUI_ViewWindow::updateToolBar()
1204 myGripWgt->update();
1205 myViewBar->update();
1206 myWindowBar->update();
1210 \brief Update state of enable draw mode state.
1212 void XGUI_ViewWindow::updateEnabledDrawMode()
1214 myEnableDrawMode = myViewer->isSelectionEnabled() && myViewer->isMultiSelectionEnabled();