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;
32 Creates viewer 3d [ static ]
34 Handle(V3d_Viewer) CreateViewer(const Standard_ExtString name, const Standard_CString displayName,
35 const Standard_CString domain, const Standard_Real viewSize,
36 const V3d_TypeOfOrientation viewProjection,
37 const Standard_Boolean computedMode,
38 const Standard_Boolean defaultComputedMode)
40 static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
41 if (aGraphicDriver.IsNull()) {
42 Handle(Aspect_DisplayConnection) aDisplayConnection;
44 aDisplayConnection = new Aspect_DisplayConnection( displayName );
46 aDisplayConnection = new Aspect_DisplayConnection();
48 aGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);
51 return new V3d_Viewer(aGraphicDriver, name, domain, viewSize, viewProjection, Quantity_NOC_GRAY30,
52 V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT, computedMode, defaultComputedMode,
56 // VSR: Uncomment below line to allow texture background support in OCC viewer
57 #define OCC_ENABLE_TEXTURED_BACKGROUND
60 Get data for supported background modes: gradient types, identifiers and supported image formats
62 QString XGUI_Viewer::backgroundData(QStringList& gradList, QIntList& idList, QIntList& txtList)
64 gradList << tr("Horizontal gradient") << tr("Vertical gradient")
65 << tr("First diagonal gradient") << tr("Second diagonal gradient")
66 << tr("First corner gradient") << tr("Second corner gradient")
67 << tr("Third corner gradient") << tr("Fourth corner gradient");
68 idList << XGUI::HorizontalGradient << XGUI::VerticalGradient << XGUI::Diagonal1Gradient
69 << XGUI::Diagonal2Gradient << XGUI::Corner1Gradient << XGUI::Corner2Gradient
70 << XGUI::Corner3Gradient << XGUI::Corner4Gradient;
71 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
72 txtList << XGUI::CenterTexture << XGUI::TileTexture << XGUI::StretchTexture;
74 return tr("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
77 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
79 myMainWindow(theParent),
80 myPreselectionEnabled(true),
81 mySelectionEnabled(true),
82 myMultiSelectionEnabled(true),
84 myInteractionStyle(XGUI::STANDARD),
91 // standard interaction style
92 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
93 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
95 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
96 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
98 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
99 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
101 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
102 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
104 // "key free" interaction style
105 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::NoModifier;
106 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::RightButton;
108 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
109 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
111 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
112 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
114 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
115 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
118 // init CasCade viewers
119 myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(), "", "", 1000.0,
120 V3d_XposYnegZpos, Standard_True, Standard_True);
121 myV3dViewer->SetDefaultLights();
124 myAISContext = new AIS_InteractiveContext(myV3dViewer);
125 myAISContext->SelectionColor(Quantity_NOC_WHITE);
127 // display isoline on planar faces (box for ex.)
128 myAISContext->IsoOnPlane(true);
130 if (DisplayTrihedron) {
131 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
132 myTrihedron = new AIS_Trihedron(anAxis);
133 myTrihedron->SetInfiniteState( Standard_True);
135 Quantity_Color Col(193 / 255., 205 / 255., 193 / 255., Quantity_TOC_RGB);
136 myTrihedron->SetArrowColor(Col.Name());
137 myTrihedron->SetSize(myTrihedronSize);
138 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
139 if (drawer->HasDatumAspect()) {
140 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
141 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
142 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
143 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
146 // set zooming style to standard
147 //myZoomingStyle = 0;
150 XGUI_Viewer::~XGUI_Viewer(void)
152 myAISContext.Nullify();
153 myV3dViewer.Nullify();
156 QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
159 XGUI_ViewWindow* view = new XGUI_ViewWindow(this, theType);
160 // get main view window (created by view frame)
161 //OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
162 // initialize main view window
164 // set default background for view window
165 //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
166 //// connect signal from viewport
167 //connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
168 //connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
169 if (myViews.size() == 0)
170 setTrihedronShown(true);
172 view->setBackground(XGUI_ViewBackground(XGUI::VerticalGradient, Qt::white, QColor(Qt::blue).lighter()));
173 //view->setBackground(XGUI_ViewBackground(Qt::black));
175 QMdiArea* aMDI = myMainWindow->mdiArea();
176 QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
178 aWnd->setGeometry(0, 0, aMDI->width() / 2, aMDI->height() / 2);
184 *\param theOper - hot operation
185 *\param theState - adding state to state map operations.
186 *\param theButton - adding state to button map operations.
188 void XGUI_Viewer::setHotButton(XGUI::InteractionStyle theInteractionStyle,
189 XGUI::HotOperation theOper, Qt::KeyboardModifiers theState,
190 Qt::MouseButtons theButton)
192 myStateMap[theInteractionStyle][theOper] = theState;
193 myButtonMap[theInteractionStyle][theOper] = theButton;
196 /*! Gets hot button for operation \a theOper.
197 *\param theOper - input hot operation
198 *\param theState - output state from state map operations.
199 *\param theButton - output state from button map operations.
201 void XGUI_Viewer::getHotButton(XGUI::InteractionStyle theInteractionStyle,
202 XGUI::HotOperation theOper, Qt::KeyboardModifiers& theState,
203 Qt::MouseButtons& theButton)
205 theState = myStateMap[theInteractionStyle][theOper];
206 theButton = myButtonMap[theInteractionStyle][theOper];
210 Changes visibility of trihedron to opposite
212 void XGUI_Viewer::toggleTrihedron()
214 setTrihedronShown(!isTrihedronVisible());
218 \return true if trihedron is visible
220 bool XGUI_Viewer::isTrihedronVisible() const
222 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed(myTrihedron);
226 Sets visibility state of trihedron
227 \param on - new state
230 void XGUI_Viewer::setTrihedronShown(const bool on)
232 if (myTrihedron.IsNull())
236 myAISContext->Display(myTrihedron);
237 myAISContext->Deactivate(myTrihedron);
239 myAISContext->Erase(myTrihedron);
244 \return trihedron size
246 double XGUI_Viewer::trihedronSize() const
249 if (!myTrihedron.IsNull())
250 sz = myTrihedron->Size();
255 Changes trihedron size
258 void XGUI_Viewer::setTrihedronSize(const double sz, bool isRelative)
260 if (myTrihedronSize != sz || isRelative != myIsRelative) {
261 myTrihedronSize = sz;
262 myIsRelative = isRelative;
268 * Update the size of the trihedron
270 void XGUI_Viewer::updateTrihedron()
272 if (myTrihedron.IsNull())
278 if (computeTrihedronSize(newSz, oldSz))
279 myTrihedron->SetSize(newSz);
281 } else if (myTrihedron->Size() != myTrihedronSize) {
282 myTrihedron->SetSize(myTrihedronSize);
287 Get new and current trihedron size corresponding to the current model size
289 bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
294 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
295 Handle(V3d_Viewer) viewer = v3dViewer();
296 viewer->InitActiveViews();
297 if (!viewer->MoreActiveViews())
300 Handle(V3d_View) view3d = viewer->ActiveView();
306 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
309 view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
311 if (Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() || Xmax == RealLast()
312 || Ymax == RealLast() || Zmax == RealLast())
315 aMaxSide = Xmax - Xmin;
316 if (aMaxSide < Ymax - Ymin)
317 aMaxSide = Ymax - Ymin;
318 if (aMaxSide < Zmax - Zmin)
319 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 || fabs(theNewSize - theSize) > theNewSize * EPS;
334 void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
339 emit deleteView( static_cast<XGUI_ViewWindow*>(theView->widget()) );
340 removeView( theView );
342 // if this is last view
343 if (myViews.size() == 0) {
344 Standard_Integer aViewsNb = 0;
345 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
347 if ( aViewsNb < 2 ) {
348 //clean up presentations before last view is closed
349 myAISContext->RemoveAll(Standard_False);
354 /*!Remove view window \a theView from view manager.
355 *And close the last view, if it has \a theView.
357 void XGUI_Viewer::removeView( QMdiSubWindow* theView )
359 XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
361 aWindow->disconnect( this );
362 myViews.removeAt( myViews.indexOf( theView ) );
363 if ( myActiveView == theView )
365 if ( myViews.size() == 0 )
366 emit lastViewClosed();
370 /*void XGUI_Viewer::onViewMapped()
372 setTrihedronShown(true);
376 void XGUI_Viewer::addView(QMdiSubWindow* theView)
378 XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
380 connect(aWindow, SIGNAL(closed(QMdiSubWindow*)),
381 this, SLOT(onViewClosed(QMdiSubWindow*)));
383 connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)),
384 this, SIGNAL(tryCloseView(XGUI_ViewWindow*)));
386 connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)),
387 this, SLOT(onMousePressed(XGUI_ViewWindow*, QMouseEvent*)));
389 connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
390 this, SIGNAL(mouseRelease(XGUI_ViewWindow*, QMouseEvent*)));
392 connect(aWindow, SIGNAL(mouseDoubleClicked(XGUI_ViewWindow*, QMouseEvent*)),
393 this, SIGNAL(mouseDoubleClick(XGUI_ViewWindow*, QMouseEvent*)));
395 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
396 this, SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)));
398 connect(aWindow, SIGNAL(keyPressed(XGUI_ViewWindow*, QKeyEvent*)),
399 this, SIGNAL(keyPress(XGUI_ViewWindow*, QKeyEvent*)));
401 connect(aWindow, SIGNAL(keyReleased(XGUI_ViewWindow*, QKeyEvent*)),
402 this, SIGNAL(keyRelease(XGUI_ViewWindow*, QKeyEvent*)));
404 connect(aWindow, SIGNAL(contextMenuRequested( QContextMenuEvent* )),
405 this, SLOT (onContextMenuRequested( QContextMenuEvent* )));
407 myViews.append(theView);
411 Emit activated for view \a view.
413 void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)