1 #include "XGUI_Viewer.h"
2 #include "XGUI_MainWindow.h"
3 #include "XGUI_ViewWindow.h"
4 #include "XGUI_ViewPort.h"
5 #include "XGUI_Preferences.h"
7 #include <SUIT_ResourceMgr.h>
10 #include <QMdiSubWindow>
11 #include <QApplication>
12 #include <QMouseEvent>
15 #include <V3d_View.hxx>
17 #include <Aspect_DisplayConnection.hxx>
18 #include <Graphic3d.hxx>
19 #include <Graphic3d_GraphicDriver.hxx>
20 #include <Geom_Axis2Placement.hxx>
21 #include <AIS_Drawer.hxx>
22 #include <Prs3d_DatumAspect.hxx>
23 #include <Prs3d_LineAspect.hxx>
24 #include <V3d_View.hxx>
25 #include <Visual3d_View.hxx>
26 #include <AIS_ListOfInteractive.hxx>
27 #include <AIS_ListIteratorOfListOfInteractive.hxx>
28 #include <AIS_Shape.hxx>
32 #include <WNT_Window.hxx>
34 #include <Xw_Window.hxx>
37 XGUI_Viewer::InteractionStyle2StatesMap XGUI_Viewer::myStateMap;
38 XGUI_Viewer::InteractionStyle2ButtonsMap XGUI_Viewer::myButtonMap;
39 static bool isInitialized = false;
42 Creates viewer 3d [ static ]
44 Handle(V3d_Viewer) CreateViewer(const Standard_ExtString name, const Standard_CString displayName,
45 const Standard_CString domain, const Standard_Real viewSize,
46 const V3d_TypeOfOrientation viewProjection,
47 const Standard_Boolean computedMode,
48 const Standard_Boolean defaultComputedMode)
50 static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
51 if (aGraphicDriver.IsNull()) {
52 Handle(Aspect_DisplayConnection) aDisplayConnection;
54 aDisplayConnection = new Aspect_DisplayConnection( displayName );
56 aDisplayConnection = new Aspect_DisplayConnection();
58 aGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);
61 return new V3d_Viewer(aGraphicDriver, name, domain, viewSize, viewProjection, Quantity_NOC_GRAY30,
62 V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT, computedMode, defaultComputedMode,
66 // VSR: Uncomment below line to allow texture background support in OCC viewer
67 #define OCC_ENABLE_TEXTURED_BACKGROUND
70 Get data for supported background modes: gradient types, identifiers and supported image formats
72 QString XGUI_Viewer::backgroundData(QStringList& gradList, QIntList& idList, QIntList& txtList)
74 gradList << tr("Horizontal gradient") << tr("Vertical gradient")
75 << tr("First diagonal gradient") << tr("Second diagonal gradient")
76 << tr("First corner gradient") << tr("Second corner gradient")
77 << tr("Third corner gradient") << tr("Fourth corner gradient");
78 idList << XGUI::HorizontalGradient << XGUI::VerticalGradient << XGUI::Diagonal1Gradient
79 << XGUI::Diagonal2Gradient << XGUI::Corner1Gradient << XGUI::Corner2Gradient
80 << XGUI::Corner3Gradient << XGUI::Corner4Gradient;
81 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
82 txtList << XGUI::CenterTexture << XGUI::TileTexture << XGUI::StretchTexture;
84 return tr("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
87 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
89 myMainWindow(theParent),
90 myPreselectionEnabled(true),
91 mySelectionEnabled(true),
92 myMultiSelectionEnabled(true),
94 myInteractionStyle(XGUI::STANDARD),
100 isInitialized = true;
102 // standard interaction style
103 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
104 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
106 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
107 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
109 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
110 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
112 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
113 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
115 // "key free" interaction style
116 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::NoModifier;
117 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::RightButton;
119 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
120 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
122 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
123 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
125 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
126 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
129 // init CasCade viewers
130 myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(), "", "", 1000.0,
131 V3d_XposYnegZpos, Standard_True, Standard_True);
132 myV3dViewer->SetDefaultLights();
135 myAISContext = new AIS_InteractiveContext(myV3dViewer);
136 myAISContext->SelectionColor(Quantity_NOC_WHITE);
138 // display isoline on planar faces (box for ex.)
139 myAISContext->IsoOnPlane(true);
141 if (DisplayTrihedron) {
142 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
143 myTrihedron = new AIS_Trihedron(anAxis);
144 myTrihedron->SetInfiniteState( Standard_True);
146 Quantity_Color Col(193 / 255., 205 / 255., 193 / 255., Quantity_TOC_RGB);
147 myTrihedron->SetArrowColor(Col.Name());
148 myTrihedron->SetSize(myTrihedronSize);
149 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
150 if (drawer->HasDatumAspect()) {
151 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
152 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
153 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
154 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
157 // set zooming style to standard
158 //myZoomingStyle = 0;
160 QMdiArea* aMDI = myMainWindow->mdiArea();
161 connect(aMDI, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(onWindowActivated(QMdiSubWindow*)));
165 XGUI_Viewer::~XGUI_Viewer(void)
167 myAISContext.Nullify();
168 myV3dViewer.Nullify();
171 QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
174 XGUI_ViewWindow* view = new XGUI_ViewWindow(this, theType);
175 // get main view window (created by view frame)
176 //OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
177 // initialize main view window
179 // set default background for view window
180 //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
181 // connect signal from viewport
182 //connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
183 //connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
184 if (myViews.size() == 0)
185 setTrihedronShown(true);
187 Qtx::BackgroundData aBk = XGUI_Preferences::resourceMgr()->backgroundValue("Viewer", "background");
188 view->setBackground(aBk);
189 view->updateEnabledDrawMode();
191 QMdiArea* aMDI = myMainWindow->mdiArea();
192 QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
194 aWnd->setGeometry(0, 0, aMDI->width() / 2, aMDI->height() / 2);
196 aWnd->setWindowTitle(QString("Viewer #%1").arg(++myWndIdCount));
197 emit viewCreated(view);
201 void XGUI_Viewer::updateFromResources()
203 Qtx::BackgroundData aBk = XGUI_Preferences::resourceMgr()->backgroundValue("Viewer", "background");
204 foreach (QMdiSubWindow* aWnd, myViews) {
205 XGUI_ViewWindow* aView = dynamic_cast<XGUI_ViewWindow*>(aWnd->widget());
207 aView->setBackground(aBk);
212 XGUI_ViewWindow* XGUI_Viewer::activeViewWindow() const
215 return dynamic_cast<XGUI_ViewWindow*>(myActiveView->widget());
219 void XGUI_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
222 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
223 theList.Append(myAISContext->SelectedInteractive());
226 void XGUI_Viewer::getSelectedShapes(NCollection_List<TopoDS_Shape>& theList)
228 Handle(AIS_InteractiveContext) ic = AISContext();
230 for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected()) {
231 TopoDS_Shape aShape = ic->SelectedShape();
232 if (!aShape.IsNull())
233 theList.Append(aShape);
237 void XGUI_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
239 AIS_ListIteratorOfListOfInteractive aIt;
240 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
241 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
242 myAISContext->UpdateCurrentViewer();
246 *\param theOper - hot operation
247 *\param theState - adding state to state map operations.
248 *\param theButton - adding state to button map operations.
250 void XGUI_Viewer::setHotButton(XGUI::InteractionStyle theInteractionStyle,
251 XGUI::HotOperation theOper, Qt::KeyboardModifiers theState,
252 Qt::MouseButtons theButton)
254 myStateMap[theInteractionStyle][theOper] = theState;
255 myButtonMap[theInteractionStyle][theOper] = theButton;
258 /*! Gets hot button for operation \a theOper.
259 *\param theOper - input hot operation
260 *\param theState - output state from state map operations.
261 *\param theButton - output state from button map operations.
263 void XGUI_Viewer::getHotButton(XGUI::InteractionStyle theInteractionStyle,
264 XGUI::HotOperation theOper, Qt::KeyboardModifiers& theState,
265 Qt::MouseButtons& theButton)
267 theState = myStateMap[theInteractionStyle][theOper];
268 theButton = myButtonMap[theInteractionStyle][theOper];
272 Changes visibility of trihedron to opposite
274 void XGUI_Viewer::toggleTrihedron()
276 setTrihedronShown(!isTrihedronVisible());
280 \return true if trihedron is visible
282 bool XGUI_Viewer::isTrihedronVisible() const
284 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed(myTrihedron);
288 Sets visibility state of trihedron
289 \param on - new state
292 void XGUI_Viewer::setTrihedronShown(bool on)
294 if (myTrihedron.IsNull())
298 myAISContext->Display(myTrihedron);
299 myAISContext->Deactivate(myTrihedron);
301 myAISContext->Erase(myTrihedron);
306 \return trihedron size
308 double XGUI_Viewer::trihedronSize() const
311 if (!myTrihedron.IsNull())
312 sz = myTrihedron->Size();
317 Changes trihedron size
320 void XGUI_Viewer::setTrihedronSize(const double sz, bool isRelative)
322 if (myTrihedronSize != sz || isRelative != myIsRelative) {
323 myTrihedronSize = sz;
324 myIsRelative = isRelative;
330 * Update the size of the trihedron
332 void XGUI_Viewer::updateTrihedron()
334 if (myTrihedron.IsNull())
340 if (computeTrihedronSize(newSz, oldSz))
341 myTrihedron->SetSize(newSz);
343 } else if (myTrihedron->Size() != myTrihedronSize) {
344 myTrihedron->SetSize(myTrihedronSize);
349 Get new and current trihedron size corresponding to the current model size
351 bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
356 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
357 Handle(V3d_Viewer) viewer = v3dViewer();
358 viewer->InitActiveViews();
359 if (!viewer->MoreActiveViews())
362 Handle(V3d_View) view3d = viewer->ActiveView();
368 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
371 view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
373 if (Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() || Xmax == RealLast()
374 || Ymax == RealLast() || Zmax == RealLast())
377 aMaxSide = Xmax - Xmin;
378 if (aMaxSide < Ymax - Ymin)
379 aMaxSide = Ymax - Ymin;
380 if (aMaxSide < Zmax - Zmin)
381 aMaxSide = Zmax - Zmin;
384 // The boundary box of the view may be initialized but nullified
385 // (case of infinite objects)
386 if (aMaxSide < Precision::Confusion())
389 static float EPS = (float)5.0E-3;
390 theSize = trihedron()->Size();
391 //theNewSize = aMaxSide*aSizeInPercents / 100.0;
393 return fabs(theNewSize - theSize) > theSize * EPS || fabs(theNewSize - theSize) > theNewSize * EPS;
396 void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
401 emit deleteView( static_cast<XGUI_ViewWindow*>(theView->widget()) );
402 removeView( theView );
404 // if this is last view
405 if (myViews.size() == 0) {
406 Standard_Integer aViewsNb = 0;
407 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
409 if ( aViewsNb < 2 ) {
410 //clean up presentations before last view is closed
411 myAISContext->RemoveAll(Standard_False);
416 /*!Remove view window \a theView from view manager.
417 *And close the last view, if it has \a theView.
419 void XGUI_Viewer::removeView( QMdiSubWindow* theView )
421 XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
423 aWindow->disconnect( this );
424 myViews.removeAt( myViews.indexOf( theView ) );
425 if ( myActiveView == theView )
427 if ( myViews.size() == 0 )
428 emit lastViewClosed();
432 /*void XGUI_Viewer::onViewMapped()
434 setTrihedronShown(true);
438 void XGUI_Viewer::addView(QMdiSubWindow* theView)
440 XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
442 connect(aWindow, SIGNAL(closed(QMdiSubWindow*)),
443 this, SLOT(onViewClosed(QMdiSubWindow*)));
445 connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)),
446 this, SIGNAL(tryCloseView(XGUI_ViewWindow*)));
448 connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)),
449 this, SLOT(onMousePressed(XGUI_ViewWindow*, QMouseEvent*)));
451 connect(aWindow, SIGNAL(mouseDoubleClicked(XGUI_ViewWindow*, QMouseEvent*)),
452 this, SIGNAL(mouseDoubleClick(XGUI_ViewWindow*, QMouseEvent*)));
454 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
455 this, SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)));
457 connect(aWindow, SIGNAL(keyPressed(XGUI_ViewWindow*, QKeyEvent*)),
458 this, SIGNAL(keyPress(XGUI_ViewWindow*, QKeyEvent*)));
460 connect(aWindow, SIGNAL(keyReleased(XGUI_ViewWindow*, QKeyEvent*)),
461 this, SIGNAL(keyRelease(XGUI_ViewWindow*, QKeyEvent*)));
463 //connect(aWindow, SIGNAL(contextMenuRequested( QContextMenuEvent* )),
464 // this, SLOT (onContextMenuRequested( QContextMenuEvent* )));
465 connect(aWindow, SIGNAL( contextMenuRequested(QContextMenuEvent*) ),
466 this, SIGNAL( contextMenuRequested(QContextMenuEvent*) ) );
468 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
469 this, SLOT(onMouseMove(XGUI_ViewWindow*, QMouseEvent*)));
471 connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
472 this, SLOT(onMouseReleased(XGUI_ViewWindow*, QMouseEvent*)));
474 myViews.append(theView);
478 Emit activated for view \a view.
480 void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)
482 if (view && (view != myActiveView) && (!view->isMinimized())) {
484 ((XGUI_ViewWindow*)myActiveView->widget())->windowActivated();
485 QList<QMdiSubWindow*>::iterator aIt;
486 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
487 if ((*aIt) != myActiveView) {
488 ((XGUI_ViewWindow*)(*aIt)->widget())->windowDeactivated();
495 void XGUI_Viewer::onWindowMinimized(QMdiSubWindow* theWnd)
497 if (myActiveView == theWnd) {
499 QList<QMdiSubWindow*>::iterator aIt;
500 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
501 if (!(*aIt)->widget()->isMinimized()) {
503 onWindowActivated(*aIt);
511 SLOT: called on mouse button press, stores current mouse position as start point for transformations
513 void XGUI_Viewer::onMousePressed(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
515 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
516 emit mousePress(theWindow, theEvent);
520 SLOT: called on mouse move, processes hilighting
522 void XGUI_Viewer::onMouseMove(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
524 myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
525 if (!mySelectionEnabled) return;
527 Handle(V3d_View) aView3d = theWindow->viewPort()->getView();
528 if ( !aView3d.IsNull() ) {
529 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
534 SLOT: called on mouse button release, finishes selection
536 void XGUI_Viewer::onMouseReleased(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
538 if (!mySelectionEnabled || theEvent->button() != Qt::LeftButton) {
539 emit mouseRelease(theWindow, theEvent);
543 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
544 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
547 // emit deselection();
549 if (myStartPnt == myEndPnt) {
550 // the MoveTo is necessary for the second click in the same point. Otherwise the selection is lost.
551 Handle(V3d_View) aView3d = theWindow->viewPort()->getView();
552 if ( !aView3d.IsNull() ) {
553 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
555 if (aHasShift && myMultiSelectionEnabled)
556 myAISContext->ShiftSelect();
558 myAISContext->Select();
560 if (aHasShift && myMultiSelectionEnabled)
561 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
562 myEndPnt.x(), myEndPnt.y(),
563 theWindow->viewPort()->getView(), false );
565 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
566 myEndPnt.x(), myEndPnt.y(),
567 theWindow->viewPort()->getView(), false );
569 int Nb = myAISContext->NbSelected();
570 if( Nb>1 && !myMultiSelectionEnabled ) {
571 myAISContext->InitSelected();
572 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
573 if( !anOwner.IsNull() ) {
574 myAISContext->ClearSelected( Standard_False );
575 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
579 myAISContext->UpdateCurrentViewer();
581 emit mouseRelease(theWindow, theEvent);
582 emit selectionChanged();
585 //******************************************************
586 void XGUI_Viewer::setMultiSelectionEnabled(bool toEnable)
588 myMultiSelectionEnabled = toEnable;
589 updateViewsDrawMode();
592 //******************************************************
593 void XGUI_Viewer::setSelectionEnabled(bool toEnable)
595 mySelectionEnabled = toEnable;
596 updateViewsDrawMode();
599 //******************************************************
600 void XGUI_Viewer::updateViewsDrawMode() const
602 foreach(QMdiSubWindow* aWnd, myViews){
603 XGUI_ViewWindow* aView = static_cast<XGUI_ViewWindow*>(aWnd->widget());
604 aView->updateEnabledDrawMode();
608 //******************************************************
609 //void XGUI_Viewer::onContextMenuRequested(QContextMenuEvent* theEvent)
611 // XGUI_ViewWindow* aWnd = dynamic_cast<XGUI_ViewWindow*>(sender());
612 // if (!aWnd) return;
616 // // Include Viewer actions
617 // if (myActions.size() > 0) {
618 // aMenu.addActions(myActions);
619 // aMenu.addSeparator();
621 // if (aWnd->actions().size() > 0) {
622 // aMenu.addActions(aWnd->actions());
623 // aMenu.addSeparator();
626 // QMdiArea* aMDI = myMainWindow->mdiArea();
627 // if (aMenu.actions().size() > 0) {
628 // QMenu* aSubMenu = aMenu.addMenu(tr("Windows"));
629 // aSubMenu->addActions(aMDI->actions());
631 // aMenu.addActions(aMDI->actions());
633 // aMenu.exec(theEvent->globalPos());