1 #include "XGUI_Viewer.h"
2 #include "XGUI_MainWindow.h"
3 #include "XGUI_ViewWindow.h"
4 #include "XGUI_ViewPort.h"
7 #include <QMdiSubWindow>
9 #include <V3d_View.hxx>
11 #include <Aspect_DisplayConnection.hxx>
12 #include <Graphic3d.hxx>
13 #include <Graphic3d_GraphicDriver.hxx>
14 #include <Geom_Axis2Placement.hxx>
15 #include <AIS_Drawer.hxx>
16 #include <Prs3d_DatumAspect.hxx>
17 #include <Prs3d_LineAspect.hxx>
18 #include <V3d_View.hxx>
19 #include <Visual3d_View.hxx>
22 #include <WNT_Window.hxx>
24 #include <Xw_Window.hxx>
27 XGUI_Viewer::InteractionStyle2StatesMap XGUI_Viewer::myStateMap;
28 XGUI_Viewer::InteractionStyle2ButtonsMap XGUI_Viewer::myButtonMap;
29 static bool isInitialized = false;
33 Creates viewer 3d [ static ]
35 Handle(V3d_Viewer) CreateViewer( const Standard_ExtString name,
36 const Standard_CString displayName,
37 const Standard_CString domain,
38 const Standard_Real viewSize ,
39 const V3d_TypeOfOrientation viewProjection,
40 const Standard_Boolean computedMode,
41 const Standard_Boolean defaultComputedMode )
43 static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
44 if (aGraphicDriver.IsNull())
46 Handle(Aspect_DisplayConnection) aDisplayConnection;
48 aDisplayConnection = new Aspect_DisplayConnection( displayName );
50 aDisplayConnection = new Aspect_DisplayConnection();
52 aGraphicDriver = Graphic3d::InitGraphicDriver( aDisplayConnection );
55 return new V3d_Viewer( aGraphicDriver, name, domain, viewSize, viewProjection,
56 Quantity_NOC_GRAY30, V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT,
57 computedMode, defaultComputedMode, V3d_TEX_NONE );
61 // VSR: Uncomment below line to allow texture background support in OCC viewer
62 #define OCC_ENABLE_TEXTURED_BACKGROUND
65 Get data for supported background modes: gradient types, identifiers and supported image formats
67 QString XGUI_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
69 gradList << tr("GT_HORIZONTALGRADIENT") << tr("GT_VERTICALGRADIENT") <<
70 tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT") <<
71 tr("GT_FIRSTCORNERGRADIENT") << tr("GT_SECONDCORNERGRADIENT") <<
72 tr("GT_THIRDCORNERGRADIENT") << tr("GT_FORTHCORNERGRADIENT");
73 idList << XGUI::HorizontalGradient << XGUI::VerticalGradient <<
74 XGUI::Diagonal1Gradient << XGUI::Diagonal2Gradient <<
75 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("BG_IMAGE_FILES");
85 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron) :
87 myMainWindow(theParent),
88 myPreselectionEnabled(true),
89 mySelectionEnabled(true),
90 myMultiSelectionEnabled(true),
92 myInteractionStyle(XGUI::STANDARD),
96 if ( !isInitialized ) {
99 // standard interaction style
100 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
101 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
103 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
104 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
106 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
107 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
109 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
110 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
112 // "key free" interaction style
113 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::NoModifier;
114 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::RightButton;
116 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
117 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
119 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
120 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
122 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
123 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
126 // init CasCade viewers
127 myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(),
128 "", "", 1000.0, V3d_XposYnegZpos, Standard_True, Standard_True );
129 myV3dViewer->SetDefaultLights();
132 myAISContext = new AIS_InteractiveContext( myV3dViewer );
133 myAISContext->SelectionColor( Quantity_NOC_WHITE );
135 // display isoline on planar faces (box for ex.)
136 myAISContext->IsoOnPlane( true );
138 if ( DisplayTrihedron ) {
139 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
140 myTrihedron = new AIS_Trihedron(anAxis);
141 myTrihedron->SetInfiniteState( Standard_True );
143 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
144 myTrihedron->SetArrowColor( Col.Name() );
145 myTrihedron->SetSize(myTrihedronSize);
146 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
147 if (drawer->HasDatumAspect()) {
148 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
149 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
150 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
151 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
154 // set zooming style to standard
155 //myZoomingStyle = 0;
159 XGUI_Viewer::~XGUI_Viewer(void)
161 myAISContext.Nullify();
162 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::green, Qt::blue));
184 QMdiArea* aMDI = myMainWindow->mdiArea();
185 QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
187 aWnd->setGeometry(0,0, aMDI->width() / 2, aMDI->height() / 2);
193 *\param theOper - hot operation
194 *\param theState - adding state to state map operations.
195 *\param theButton - adding state to button map operations.
197 void XGUI_Viewer::setHotButton( XGUI::InteractionStyle theInteractionStyle, XGUI::HotOperation theOper,
198 Qt::KeyboardModifiers theState, Qt::MouseButtons theButton )
200 myStateMap[theInteractionStyle][theOper] = theState;
201 myButtonMap[theInteractionStyle][theOper] = theButton;
204 /*! Gets hot button for operation \a theOper.
205 *\param theOper - input hot operation
206 *\param theState - output state from state map operations.
207 *\param theButton - output state from button map operations.
209 void XGUI_Viewer::getHotButton( XGUI::InteractionStyle theInteractionStyle, XGUI::HotOperation theOper,
210 Qt::KeyboardModifiers& theState, Qt::MouseButtons& theButton )
212 theState = myStateMap[theInteractionStyle][theOper];
213 theButton = myButtonMap[theInteractionStyle][theOper];
217 Changes visibility of trihedron to opposite
219 void XGUI_Viewer::toggleTrihedron()
221 setTrihedronShown( !isTrihedronVisible() );
225 \return true if trihedron is visible
227 bool XGUI_Viewer::isTrihedronVisible() const
229 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
233 Sets visibility state of trihedron
234 \param on - new state
237 void XGUI_Viewer::setTrihedronShown( const bool on )
239 if ( myTrihedron.IsNull() )
243 myAISContext->Display( myTrihedron );
244 myAISContext->Deactivate(myTrihedron);
246 myAISContext->Erase( myTrihedron );
251 \return trihedron size
253 double XGUI_Viewer::trihedronSize() const
256 if ( !myTrihedron.IsNull() )
257 sz = myTrihedron->Size();
262 Changes trihedron size
265 void XGUI_Viewer::setTrihedronSize( const double sz, bool isRelative )
267 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
268 myTrihedronSize = sz;
269 myIsRelative = isRelative;
275 * Update the size of the trihedron
277 void XGUI_Viewer::updateTrihedron()
279 if ( myTrihedron.IsNull() )
285 if(computeTrihedronSize(newSz, oldSz))
286 myTrihedron->SetSize(newSz);
288 } else if(myTrihedron->Size() != myTrihedronSize) {
289 myTrihedron->SetSize(myTrihedronSize);
294 Get new and current trihedron size corresponding to the current model size
296 bool XGUI_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
301 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
302 Handle(V3d_Viewer) viewer = v3dViewer();
303 viewer->InitActiveViews();
304 if(!viewer->MoreActiveViews()) return false;
306 Handle(V3d_View) view3d = viewer->ActiveView();
309 if ( view3d.IsNull() )
312 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
315 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
317 if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
318 Xmax == RealLast() || Ymax == RealLast() || Zmax == RealLast() )
321 aMaxSide = Xmax - Xmin;
322 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
323 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
326 // The boundary box of the view may be initialized but nullified
327 // (case of infinite objects)
328 if ( aMaxSide < Precision::Confusion() )
331 static float EPS = 5.0E-3;
332 theSize = trihedron()->Size();
333 //theNewSize = aMaxSide*aSizeInPercents / 100.0;
335 return fabs( theNewSize - theSize ) > theSize * EPS ||
336 fabs( theNewSize - theSize) > theNewSize * EPS;
340 void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
345 emit deleteView( static_cast<XGUI_ViewWindow*>(theView->widget()) );
346 removeView( theView );
348 // if this is last view
349 if (myViews.size() == 0) {
350 Standard_Integer aViewsNb = 0;
351 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
353 if ( aViewsNb < 2 ) {
354 //clean up presentations before last view is closed
355 myAISContext->RemoveAll(Standard_False);
360 /*!Remove view window \a theView from view manager.
361 *And close the last view, if it has \a theView.
363 void XGUI_Viewer::removeView( QMdiSubWindow* theView )
365 XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
367 aWindow->disconnect( this );
368 myViews.removeAt( myViews.indexOf( theView ) );
369 if ( myActiveView == theView )
371 if ( myViews.size() == 0 )
372 emit lastViewClosed();
376 /*void XGUI_Viewer::onViewMapped()
378 setTrihedronShown( true );
382 void XGUI_Viewer::addView(QMdiSubWindow* theView)
384 XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
386 connect(aWindow, SIGNAL(closed(QMdiSubWindow*)),
387 this, SLOT(onViewClosed(QMdiSubWindow*)));
389 connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)),
390 this, SIGNAL(tryCloseView(XGUI_ViewWindow*)));
392 connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)),
393 this, SLOT(onMousePressed(XGUI_ViewWindow*, QMouseEvent*)));
395 connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
396 this, SIGNAL(mouseRelease(XGUI_ViewWindow*, QMouseEvent*)));
398 connect(aWindow, SIGNAL(mouseDoubleClicked(XGUI_ViewWindow*, QMouseEvent*)),
399 this, SIGNAL(mouseDoubleClick(XGUI_ViewWindow*, QMouseEvent*)));
401 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
402 this, SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)));
404 connect(aWindow, SIGNAL(keyPressed(XGUI_ViewWindow*, QKeyEvent*)),
405 this, SIGNAL(keyPress(XGUI_ViewWindow*, QKeyEvent*)));
407 connect(aWindow, SIGNAL(keyReleased(XGUI_ViewWindow*, QKeyEvent*)),
408 this, SIGNAL(keyRelease(XGUI_ViewWindow*, QKeyEvent*)));
410 connect(aWindow, SIGNAL(contextMenuRequested( QContextMenuEvent* )),
411 this, SLOT (onContextMenuRequested( QContextMenuEvent* )));
413 myViews.append(theView);
417 Emit activated for view \a view.
419 void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)