1 #include "XGUI_Viewer.h"
2 #include "XGUI_MainWindow.h"
3 #include "XGUI_ViewWindow.h"
4 #include "XGUI_ViewPort.h"
7 #include <QMdiSubWindow>
8 #include <QApplication>
10 #include <V3d_View.hxx>
12 #include <Aspect_DisplayConnection.hxx>
13 #include <Graphic3d.hxx>
14 #include <Graphic3d_GraphicDriver.hxx>
15 #include <Geom_Axis2Placement.hxx>
16 #include <AIS_Drawer.hxx>
17 #include <Prs3d_DatumAspect.hxx>
18 #include <Prs3d_LineAspect.hxx>
19 #include <V3d_View.hxx>
20 #include <Visual3d_View.hxx>
21 #include <AIS_ListOfInteractive.hxx>
22 #include <AIS_ListIteratorOfListOfInteractive.hxx>
23 #include <AIS_Shape.hxx>
25 #include <QMouseEvent>
28 #include <WNT_Window.hxx>
30 #include <Xw_Window.hxx>
33 XGUI_Viewer::InteractionStyle2StatesMap XGUI_Viewer::myStateMap;
34 XGUI_Viewer::InteractionStyle2ButtonsMap XGUI_Viewer::myButtonMap;
35 static bool isInitialized = false;
38 Creates viewer 3d [ static ]
40 Handle(V3d_Viewer) CreateViewer(const Standard_ExtString name, const Standard_CString displayName,
41 const Standard_CString domain, const Standard_Real viewSize,
42 const V3d_TypeOfOrientation viewProjection,
43 const Standard_Boolean computedMode,
44 const Standard_Boolean defaultComputedMode)
46 static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
47 if (aGraphicDriver.IsNull()) {
48 Handle(Aspect_DisplayConnection) aDisplayConnection;
50 aDisplayConnection = new Aspect_DisplayConnection( displayName );
52 aDisplayConnection = new Aspect_DisplayConnection();
54 aGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);
57 return new V3d_Viewer(aGraphicDriver, name, domain, viewSize, viewProjection, Quantity_NOC_GRAY30,
58 V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT, computedMode, defaultComputedMode,
62 // VSR: Uncomment below line to allow texture background support in OCC viewer
63 #define OCC_ENABLE_TEXTURED_BACKGROUND
66 Get data for supported background modes: gradient types, identifiers and supported image formats
68 QString XGUI_Viewer::backgroundData(QStringList& gradList, QIntList& idList, QIntList& txtList)
70 gradList << tr("Horizontal gradient") << tr("Vertical gradient")
71 << tr("First diagonal gradient") << tr("Second diagonal gradient")
72 << tr("First corner gradient") << tr("Second corner gradient")
73 << tr("Third corner gradient") << tr("Fourth corner gradient");
74 idList << XGUI::HorizontalGradient << XGUI::VerticalGradient << XGUI::Diagonal1Gradient
75 << XGUI::Diagonal2Gradient << XGUI::Corner1Gradient << XGUI::Corner2Gradient
76 << XGUI::Corner3Gradient << XGUI::Corner4Gradient;
77 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
78 txtList << XGUI::CenterTexture << XGUI::TileTexture << XGUI::StretchTexture;
80 return tr("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
83 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
85 myMainWindow(theParent),
86 myPreselectionEnabled(true),
87 mySelectionEnabled(true),
88 myMultiSelectionEnabled(true),
90 myInteractionStyle(XGUI::STANDARD),
97 // standard interaction style
98 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
99 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
101 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
102 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
104 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
105 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
107 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
108 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
110 // "key free" interaction style
111 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::NoModifier;
112 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::RightButton;
114 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
115 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
117 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
118 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
120 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
121 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
124 // init CasCade viewers
125 myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(), "", "", 1000.0,
126 V3d_XposYnegZpos, Standard_True, Standard_True);
127 myV3dViewer->SetDefaultLights();
130 myAISContext = new AIS_InteractiveContext(myV3dViewer);
131 myAISContext->SelectionColor(Quantity_NOC_WHITE);
133 // display isoline on planar faces (box for ex.)
134 myAISContext->IsoOnPlane(true);
136 if (DisplayTrihedron) {
137 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
138 myTrihedron = new AIS_Trihedron(anAxis);
139 myTrihedron->SetInfiniteState( Standard_True);
141 Quantity_Color Col(193 / 255., 205 / 255., 193 / 255., Quantity_TOC_RGB);
142 myTrihedron->SetArrowColor(Col.Name());
143 myTrihedron->SetSize(myTrihedronSize);
144 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
145 if (drawer->HasDatumAspect()) {
146 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
147 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
148 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
149 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
152 // set zooming style to standard
153 //myZoomingStyle = 0;
155 QMdiArea* aMDI = myMainWindow->mdiArea();
156 connect(aMDI, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(onWindowActivated(QMdiSubWindow*)));
160 XGUI_Viewer::~XGUI_Viewer(void)
162 myAISContext.Nullify();
163 myV3dViewer.Nullify();
166 QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
169 XGUI_ViewWindow* view = new XGUI_ViewWindow(this, theType);
170 // get main view window (created by view frame)
171 //OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
172 // initialize main view window
174 // set default background for view window
175 //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
176 //// connect signal from viewport
177 //connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
178 //connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
179 if (myViews.size() == 0)
180 setTrihedronShown(true);
182 view->setBackground(XGUI_ViewBackground(XGUI::VerticalGradient, Qt::white, QColor(Qt::blue).lighter()));
183 //view->setBackground(XGUI_ViewBackground(Qt::black));
185 QMdiArea* aMDI = myMainWindow->mdiArea();
186 QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
188 aWnd->setGeometry(0, 0, aMDI->width() / 2, aMDI->height() / 2);
194 void XGUI_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
197 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
198 theList.Append(myAISContext->SelectedInteractive());
201 void XGUI_Viewer::getSelectedShapes(NCollection_List<TopoDS_Shape>& theList)
203 Handle(AIS_InteractiveContext) ic = AISContext();
205 for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected()) {
206 TopoDS_Shape aShape = ic->SelectedShape();
207 if (!aShape.IsNull())
208 theList.Append(aShape);
212 void XGUI_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
214 AIS_ListIteratorOfListOfInteractive aIt;
215 for (aIt.Initialize(theList); aIt.More(); aIt.Next())
216 myAISContext->AddOrRemoveSelected(aIt.Value(), false);
217 myAISContext->UpdateCurrentViewer();
221 *\param theOper - hot operation
222 *\param theState - adding state to state map operations.
223 *\param theButton - adding state to button map operations.
225 void XGUI_Viewer::setHotButton(XGUI::InteractionStyle theInteractionStyle,
226 XGUI::HotOperation theOper, Qt::KeyboardModifiers theState,
227 Qt::MouseButtons theButton)
229 myStateMap[theInteractionStyle][theOper] = theState;
230 myButtonMap[theInteractionStyle][theOper] = theButton;
233 /*! Gets hot button for operation \a theOper.
234 *\param theOper - input hot operation
235 *\param theState - output state from state map operations.
236 *\param theButton - output state from button map operations.
238 void XGUI_Viewer::getHotButton(XGUI::InteractionStyle theInteractionStyle,
239 XGUI::HotOperation theOper, Qt::KeyboardModifiers& theState,
240 Qt::MouseButtons& theButton)
242 theState = myStateMap[theInteractionStyle][theOper];
243 theButton = myButtonMap[theInteractionStyle][theOper];
246 void XGUI_Viewer::setViewProjection(double theX, double theY, double theZ)
248 XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(myActiveView->widget());
250 Handle(V3d_View) aView3d = aWindow->viewPort()->getView();
251 if ( !aView3d.IsNull() )
252 aView3d->SetProj(theX, theY, theZ);
253 aWindow->viewPort()->fitAll();
258 Changes visibility of trihedron to opposite
260 void XGUI_Viewer::toggleTrihedron()
262 setTrihedronShown(!isTrihedronVisible());
266 \return true if trihedron is visible
268 bool XGUI_Viewer::isTrihedronVisible() const
270 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed(myTrihedron);
274 Sets visibility state of trihedron
275 \param on - new state
278 void XGUI_Viewer::setTrihedronShown(bool on)
280 if (myTrihedron.IsNull())
284 myAISContext->Display(myTrihedron);
285 myAISContext->Deactivate(myTrihedron);
287 myAISContext->Erase(myTrihedron);
292 \return trihedron size
294 double XGUI_Viewer::trihedronSize() const
297 if (!myTrihedron.IsNull())
298 sz = myTrihedron->Size();
303 Changes trihedron size
306 void XGUI_Viewer::setTrihedronSize(const double sz, bool isRelative)
308 if (myTrihedronSize != sz || isRelative != myIsRelative) {
309 myTrihedronSize = sz;
310 myIsRelative = isRelative;
316 * Update the size of the trihedron
318 void XGUI_Viewer::updateTrihedron()
320 if (myTrihedron.IsNull())
326 if (computeTrihedronSize(newSz, oldSz))
327 myTrihedron->SetSize(newSz);
329 } else if (myTrihedron->Size() != myTrihedronSize) {
330 myTrihedron->SetSize(myTrihedronSize);
335 Get new and current trihedron size corresponding to the current model size
337 bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
342 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
343 Handle(V3d_Viewer) viewer = v3dViewer();
344 viewer->InitActiveViews();
345 if (!viewer->MoreActiveViews())
348 Handle(V3d_View) view3d = viewer->ActiveView();
354 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
357 view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
359 if (Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() || Xmax == RealLast()
360 || Ymax == RealLast() || Zmax == RealLast())
363 aMaxSide = Xmax - Xmin;
364 if (aMaxSide < Ymax - Ymin)
365 aMaxSide = Ymax - Ymin;
366 if (aMaxSide < Zmax - Zmin)
367 aMaxSide = Zmax - Zmin;
370 // The boundary box of the view may be initialized but nullified
371 // (case of infinite objects)
372 if (aMaxSide < Precision::Confusion())
375 static float EPS = (float)5.0E-3;
376 theSize = trihedron()->Size();
377 //theNewSize = aMaxSide*aSizeInPercents / 100.0;
379 return fabs(theNewSize - theSize) > theSize * EPS || fabs(theNewSize - theSize) > theNewSize * EPS;
382 void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
387 emit deleteView( static_cast<XGUI_ViewWindow*>(theView->widget()) );
388 removeView( theView );
390 // if this is last view
391 if (myViews.size() == 0) {
392 Standard_Integer aViewsNb = 0;
393 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
395 if ( aViewsNb < 2 ) {
396 //clean up presentations before last view is closed
397 myAISContext->RemoveAll(Standard_False);
402 /*!Remove view window \a theView from view manager.
403 *And close the last view, if it has \a theView.
405 void XGUI_Viewer::removeView( QMdiSubWindow* theView )
407 XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
409 aWindow->disconnect( this );
410 myViews.removeAt( myViews.indexOf( theView ) );
411 if ( myActiveView == theView )
413 if ( myViews.size() == 0 )
414 emit lastViewClosed();
418 /*void XGUI_Viewer::onViewMapped()
420 setTrihedronShown(true);
424 void XGUI_Viewer::addView(QMdiSubWindow* theView)
426 XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
428 connect(aWindow, SIGNAL(closed(QMdiSubWindow*)),
429 this, SLOT(onViewClosed(QMdiSubWindow*)));
431 connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)),
432 this, SIGNAL(tryCloseView(XGUI_ViewWindow*)));
434 connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)),
435 this, SLOT(onMousePressed(XGUI_ViewWindow*, QMouseEvent*)));
437 connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
438 this, SIGNAL(mouseRelease(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* )));
455 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
456 this, SLOT(onMouseMove(XGUI_ViewWindow*, QMouseEvent*)));
458 connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
459 this, SLOT(onMouseReleased(XGUI_ViewWindow*, QMouseEvent*)));
461 myViews.append(theView);
465 Emit activated for view \a view.
467 void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)
469 if (view && (view != myActiveView) && (!view->isMinimized())) {
471 ((XGUI_ViewWindow*)myActiveView->widget())->windowActivated();
472 QList<QMdiSubWindow*>::iterator aIt;
473 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
474 if ((*aIt) != myActiveView) {
475 ((XGUI_ViewWindow*)(*aIt)->widget())->windowDeactivated();
482 void XGUI_Viewer::onWindowMinimized(QMdiSubWindow* theWnd)
484 if (myActiveView == theWnd) {
486 QList<QMdiSubWindow*>::iterator aIt;
487 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
488 if (!(*aIt)->widget()->isMinimized()) {
490 onWindowActivated(*aIt);
498 SLOT: called on mouse move, processes hilighting
500 void XGUI_Viewer::onMouseMove(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
502 XGUI_ViewPort* aViewPort = theWindow->viewPort();
503 Handle(V3d_View) aView3d = aViewPort->getView();
505 if ( !aView3d.IsNull() )
506 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
510 SLOT: called on mouse button release, finishes selection
512 void XGUI_Viewer::onMouseReleased(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
514 myAISContext->Select();
516 emit selectionChanged();