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),
95 if ( !isInitialized ) {
98 // standard interaction style
99 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
100 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
102 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
103 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
105 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
106 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
108 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
109 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
111 // "key free" interaction style
112 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::NoModifier;
113 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::RightButton;
115 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
116 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
118 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
119 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
121 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
122 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
125 // init CasCade viewers
126 myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(),
127 "", "", 1000.0, V3d_XposYnegZpos, Standard_True, Standard_True );
128 myV3dViewer->SetDefaultLights();
131 myAISContext = new AIS_InteractiveContext( myV3dViewer );
132 myAISContext->SelectionColor( Quantity_NOC_WHITE );
134 // display isoline on planar faces (box for ex.)
135 myAISContext->IsoOnPlane( true );
137 if ( DisplayTrihedron ) {
138 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
139 myTrihedron = new AIS_Trihedron(anAxis);
140 myTrihedron->SetInfiniteState( Standard_True );
142 Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
143 myTrihedron->SetArrowColor( Col.Name() );
144 myTrihedron->SetSize(myTrihedronSize);
145 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
146 if (drawer->HasDatumAspect()) {
147 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
148 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
149 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
150 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
153 // set zooming style to standard
154 //myZoomingStyle = 0;
158 XGUI_Viewer::~XGUI_Viewer(void)
160 myAISContext.Nullify();
161 myV3dViewer.Nullify();
165 QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
168 XGUI_ViewWindow* view = new XGUI_ViewWindow(this, theType);
169 // get main view window (created by view frame)
170 //OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
171 // initialize main view window
173 // set default background for view window
174 //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
175 //// connect signal from viewport
176 connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
177 connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
179 view->setBackground(XGUI_ViewBackground(XGUI::VerticalGradient, Qt::green, Qt::blue));
181 QMdiArea* aMDI = myMainWindow->mdiArea();
182 QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
183 aWnd->setGeometry(0,0, aMDI->width() / 2, aMDI->height() / 2);
189 *\param theOper - hot operation
190 *\param theState - adding state to state map operations.
191 *\param theButton - adding state to button map operations.
193 void XGUI_Viewer::setHotButton( XGUI::InteractionStyle theInteractionStyle, XGUI::HotOperation theOper,
194 Qt::KeyboardModifiers theState, Qt::MouseButtons theButton )
196 myStateMap[theInteractionStyle][theOper] = theState;
197 myButtonMap[theInteractionStyle][theOper] = theButton;
200 /*! Gets hot button for operation \a theOper.
201 *\param theOper - input hot operation
202 *\param theState - output state from state map operations.
203 *\param theButton - output state from button map operations.
205 void XGUI_Viewer::getHotButton( XGUI::InteractionStyle theInteractionStyle, XGUI::HotOperation theOper,
206 Qt::KeyboardModifiers& theState, Qt::MouseButtons& theButton )
208 theState = myStateMap[theInteractionStyle][theOper];
209 theButton = myButtonMap[theInteractionStyle][theOper];
213 Changes visibility of trihedron to opposite
215 void XGUI_Viewer::toggleTrihedron()
217 setTrihedronShown( !isTrihedronVisible() );
221 \return true if trihedron is visible
223 bool XGUI_Viewer::isTrihedronVisible() const
225 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
229 Sets visibility state of trihedron
230 \param on - new state
233 void XGUI_Viewer::setTrihedronShown( const bool on )
235 if ( myTrihedron.IsNull() )
239 myAISContext->Display( myTrihedron );
240 myAISContext->Deactivate(myTrihedron);
242 myAISContext->Erase( myTrihedron );
247 \return trihedron size
249 double XGUI_Viewer::trihedronSize() const
252 if ( !myTrihedron.IsNull() )
253 sz = myTrihedron->Size();
258 Changes trihedron size
261 void XGUI_Viewer::setTrihedronSize( const double sz, bool isRelative )
263 if ( myTrihedronSize != sz || isRelative != myIsRelative) {
264 myTrihedronSize = sz;
265 myIsRelative = isRelative;
271 * Update the size of the trihedron
273 void XGUI_Viewer::updateTrihedron()
275 if ( myTrihedron.IsNull() )
281 if(computeTrihedronSize(newSz, oldSz))
282 myTrihedron->SetSize(newSz);
284 } else if(myTrihedron->Size() != myTrihedronSize) {
285 myTrihedron->SetSize(myTrihedronSize);
290 Get new and current trihedron size corresponding to the current model size
292 bool XGUI_Viewer::computeTrihedronSize( double& theNewSize, double& theSize )
297 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
298 Handle(V3d_Viewer) viewer = v3dViewer();
299 viewer->InitActiveViews();
300 if(!viewer->MoreActiveViews()) return false;
302 Handle(V3d_View) view3d = viewer->ActiveView();
305 if ( view3d.IsNull() )
308 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
311 view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
313 if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
314 Xmax == RealLast() || Ymax == RealLast() || Zmax == RealLast() )
317 aMaxSide = Xmax - Xmin;
318 if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
319 if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
322 // The boundary box of the view may be initialized but nullified
323 // (case of infinite objects)
324 if ( aMaxSide < Precision::Confusion() )
327 static float EPS = 5.0E-3;
328 theSize = trihedron()->Size();
329 //theNewSize = aMaxSide*aSizeInPercents / 100.0;
331 return fabs( theNewSize - theSize ) > theSize * EPS ||
332 fabs( theNewSize - theSize) > theNewSize * EPS;
336 void XGUI_Viewer::onViewClosed()
338 Standard_Integer aViewsNb = 0;
339 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
341 if ( aViewsNb < 2 ) {
342 //clean up presentations before last view is closed
343 myAISContext->RemoveAll(Standard_False);
347 void XGUI_Viewer::onViewMapped()
349 setTrihedronShown( true );