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 XGUI_ViewWindow* XGUI_Viewer::activeViewWindow() const
204 return dynamic_cast<XGUI_ViewWindow*>(myActiveView->widget());
208 void XGUI_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
211 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
212 theList.Append(myAISContext->SelectedInteractive());
215 void XGUI_Viewer::getSelectedShapes(NCollection_List<TopoDS_Shape>& theList)
217 Handle(AIS_InteractiveContext) ic = AISContext();
219 for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected()) {
220 TopoDS_Shape aShape = ic->SelectedShape();
221 if (!aShape.IsNull())
222 theList.Append(aShape);
226 void XGUI_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
228 AIS_ListIteratorOfListOfInteractive aIt;
229 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
230 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
231 myAISContext->UpdateCurrentViewer();
235 *\param theOper - hot operation
236 *\param theState - adding state to state map operations.
237 *\param theButton - adding state to button map operations.
239 void XGUI_Viewer::setHotButton(XGUI::InteractionStyle theInteractionStyle,
240 XGUI::HotOperation theOper, Qt::KeyboardModifiers theState,
241 Qt::MouseButtons theButton)
243 myStateMap[theInteractionStyle][theOper] = theState;
244 myButtonMap[theInteractionStyle][theOper] = theButton;
247 /*! Gets hot button for operation \a theOper.
248 *\param theOper - input hot operation
249 *\param theState - output state from state map operations.
250 *\param theButton - output state from button map operations.
252 void XGUI_Viewer::getHotButton(XGUI::InteractionStyle theInteractionStyle,
253 XGUI::HotOperation theOper, Qt::KeyboardModifiers& theState,
254 Qt::MouseButtons& theButton)
256 theState = myStateMap[theInteractionStyle][theOper];
257 theButton = myButtonMap[theInteractionStyle][theOper];
261 Changes visibility of trihedron to opposite
263 void XGUI_Viewer::toggleTrihedron()
265 setTrihedronShown(!isTrihedronVisible());
269 \return true if trihedron is visible
271 bool XGUI_Viewer::isTrihedronVisible() const
273 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed(myTrihedron);
277 Sets visibility state of trihedron
278 \param on - new state
281 void XGUI_Viewer::setTrihedronShown(bool on)
283 if (myTrihedron.IsNull())
287 myAISContext->Display(myTrihedron);
288 myAISContext->Deactivate(myTrihedron);
290 myAISContext->Erase(myTrihedron);
295 \return trihedron size
297 double XGUI_Viewer::trihedronSize() const
300 if (!myTrihedron.IsNull())
301 sz = myTrihedron->Size();
306 Changes trihedron size
309 void XGUI_Viewer::setTrihedronSize(const double sz, bool isRelative)
311 if (myTrihedronSize != sz || isRelative != myIsRelative) {
312 myTrihedronSize = sz;
313 myIsRelative = isRelative;
319 * Update the size of the trihedron
321 void XGUI_Viewer::updateTrihedron()
323 if (myTrihedron.IsNull())
329 if (computeTrihedronSize(newSz, oldSz))
330 myTrihedron->SetSize(newSz);
332 } else if (myTrihedron->Size() != myTrihedronSize) {
333 myTrihedron->SetSize(myTrihedronSize);
338 Get new and current trihedron size corresponding to the current model size
340 bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
345 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
346 Handle(V3d_Viewer) viewer = v3dViewer();
347 viewer->InitActiveViews();
348 if (!viewer->MoreActiveViews())
351 Handle(V3d_View) view3d = viewer->ActiveView();
357 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
360 view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
362 if (Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() || Xmax == RealLast()
363 || Ymax == RealLast() || Zmax == RealLast())
366 aMaxSide = Xmax - Xmin;
367 if (aMaxSide < Ymax - Ymin)
368 aMaxSide = Ymax - Ymin;
369 if (aMaxSide < Zmax - Zmin)
370 aMaxSide = Zmax - Zmin;
373 // The boundary box of the view may be initialized but nullified
374 // (case of infinite objects)
375 if (aMaxSide < Precision::Confusion())
378 static float EPS = (float)5.0E-3;
379 theSize = trihedron()->Size();
380 //theNewSize = aMaxSide*aSizeInPercents / 100.0;
382 return fabs(theNewSize - theSize) > theSize * EPS || fabs(theNewSize - theSize) > theNewSize * EPS;
385 void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
390 emit deleteView( static_cast<XGUI_ViewWindow*>(theView->widget()) );
391 removeView( theView );
393 // if this is last view
394 if (myViews.size() == 0) {
395 Standard_Integer aViewsNb = 0;
396 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
398 if ( aViewsNb < 2 ) {
399 //clean up presentations before last view is closed
400 myAISContext->RemoveAll(Standard_False);
405 /*!Remove view window \a theView from view manager.
406 *And close the last view, if it has \a theView.
408 void XGUI_Viewer::removeView( QMdiSubWindow* theView )
410 XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
412 aWindow->disconnect( this );
413 myViews.removeAt( myViews.indexOf( theView ) );
414 if ( myActiveView == theView )
416 if ( myViews.size() == 0 )
417 emit lastViewClosed();
421 /*void XGUI_Viewer::onViewMapped()
423 setTrihedronShown(true);
427 void XGUI_Viewer::addView(QMdiSubWindow* theView)
429 XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
431 connect(aWindow, SIGNAL(closed(QMdiSubWindow*)),
432 this, SLOT(onViewClosed(QMdiSubWindow*)));
434 connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)),
435 this, SIGNAL(tryCloseView(XGUI_ViewWindow*)));
437 connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)),
438 this, SLOT(onMousePressed(XGUI_ViewWindow*, QMouseEvent*)));
440 connect(aWindow, SIGNAL(mouseDoubleClicked(XGUI_ViewWindow*, QMouseEvent*)),
441 this, SIGNAL(mouseDoubleClick(XGUI_ViewWindow*, QMouseEvent*)));
443 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
444 this, SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)));
446 connect(aWindow, SIGNAL(keyPressed(XGUI_ViewWindow*, QKeyEvent*)),
447 this, SIGNAL(keyPress(XGUI_ViewWindow*, QKeyEvent*)));
449 connect(aWindow, SIGNAL(keyReleased(XGUI_ViewWindow*, QKeyEvent*)),
450 this, SIGNAL(keyRelease(XGUI_ViewWindow*, QKeyEvent*)));
452 //connect(aWindow, SIGNAL(contextMenuRequested( QContextMenuEvent* )),
453 // this, SLOT (onContextMenuRequested( QContextMenuEvent* )));
454 connect(aWindow, SIGNAL( contextMenuRequested(QContextMenuEvent*) ),
455 this, SIGNAL( contextMenuRequested(QContextMenuEvent*) ) );
457 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
458 this, SLOT(onMouseMove(XGUI_ViewWindow*, QMouseEvent*)));
460 connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
461 this, SLOT(onMouseReleased(XGUI_ViewWindow*, QMouseEvent*)));
463 myViews.append(theView);
467 Emit activated for view \a view.
469 void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)
471 if (view && (view != myActiveView) && (!view->isMinimized())) {
473 ((XGUI_ViewWindow*)myActiveView->widget())->windowActivated();
474 QList<QMdiSubWindow*>::iterator aIt;
475 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
476 if ((*aIt) != myActiveView) {
477 ((XGUI_ViewWindow*)(*aIt)->widget())->windowDeactivated();
484 void XGUI_Viewer::onWindowMinimized(QMdiSubWindow* theWnd)
486 if (myActiveView == theWnd) {
488 QList<QMdiSubWindow*>::iterator aIt;
489 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
490 if (!(*aIt)->widget()->isMinimized()) {
492 onWindowActivated(*aIt);
500 SLOT: called on mouse button press, stores current mouse position as start point for transformations
502 void XGUI_Viewer::onMousePressed(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
504 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
505 emit mousePress(theWindow, theEvent);
509 SLOT: called on mouse move, processes hilighting
511 void XGUI_Viewer::onMouseMove(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
513 myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
514 if (!mySelectionEnabled) return;
516 Handle(V3d_View) aView3d = theWindow->viewPort()->getView();
517 if ( !aView3d.IsNull() ) {
518 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
523 SLOT: called on mouse button release, finishes selection
525 void XGUI_Viewer::onMouseReleased(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
527 if (!mySelectionEnabled || theEvent->button() != Qt::LeftButton) {
528 emit mouseRelease(theWindow, theEvent);
532 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
533 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
536 // emit deselection();
538 if (myStartPnt == myEndPnt) {
539 // the MoveTo is necessary for the second click in the same point. Otherwise the selection is lost.
540 Handle(V3d_View) aView3d = theWindow->viewPort()->getView();
541 if ( !aView3d.IsNull() ) {
542 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
544 if (aHasShift && myMultiSelectionEnabled)
545 myAISContext->ShiftSelect();
547 myAISContext->Select();
549 if (aHasShift && myMultiSelectionEnabled)
550 myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
551 myEndPnt.x(), myEndPnt.y(),
552 theWindow->viewPort()->getView(), false );
554 myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
555 myEndPnt.x(), myEndPnt.y(),
556 theWindow->viewPort()->getView(), false );
558 int Nb = myAISContext->NbSelected();
559 if( Nb>1 && !myMultiSelectionEnabled ) {
560 myAISContext->InitSelected();
561 Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
562 if( !anOwner.IsNull() ) {
563 myAISContext->ClearSelected( Standard_False );
564 myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
568 myAISContext->UpdateCurrentViewer();
570 emit mouseRelease(theWindow, theEvent);
571 emit selectionChanged();
574 //******************************************************
575 void XGUI_Viewer::setMultiSelectionEnabled(bool toEnable)
577 myMultiSelectionEnabled = toEnable;
578 updateViewsDrawMode();
581 //******************************************************
582 void XGUI_Viewer::setSelectionEnabled(bool toEnable)
584 mySelectionEnabled = toEnable;
585 updateViewsDrawMode();
588 //******************************************************
589 void XGUI_Viewer::updateViewsDrawMode() const
591 foreach(QMdiSubWindow* aWnd, myViews){
592 XGUI_ViewWindow* aView = static_cast<XGUI_ViewWindow*>(aWnd->widget());
593 aView->updateEnabledDrawMode();
597 //******************************************************
598 //void XGUI_Viewer::onContextMenuRequested(QContextMenuEvent* theEvent)
600 // XGUI_ViewWindow* aWnd = dynamic_cast<XGUI_ViewWindow*>(sender());
601 // if (!aWnd) return;
605 // // Include Viewer actions
606 // if (myActions.size() > 0) {
607 // aMenu.addActions(myActions);
608 // aMenu.addSeparator();
610 // if (aWnd->actions().size() > 0) {
611 // aMenu.addActions(aWnd->actions());
612 // aMenu.addSeparator();
615 // QMdiArea* aMDI = myMainWindow->mdiArea();
616 // if (aMenu.actions().size() > 0) {
617 // QMenu* aSubMenu = aMenu.addMenu(tr("Windows"));
618 // aSubMenu->addActions(aMDI->actions());
620 // aMenu.addActions(aMDI->actions());
622 // aMenu.exec(theEvent->globalPos());