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>
23 #include <WNT_Window.hxx>
25 #include <Xw_Window.hxx>
28 XGUI_Viewer::InteractionStyle2StatesMap XGUI_Viewer::myStateMap;
29 XGUI_Viewer::InteractionStyle2ButtonsMap XGUI_Viewer::myButtonMap;
30 static bool isInitialized = false;
33 Creates viewer 3d [ static ]
35 Handle(V3d_Viewer) CreateViewer(const Standard_ExtString name, const Standard_CString displayName,
36 const Standard_CString domain, const Standard_Real viewSize,
37 const V3d_TypeOfOrientation viewProjection,
38 const Standard_Boolean computedMode,
39 const Standard_Boolean defaultComputedMode)
41 static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
42 if (aGraphicDriver.IsNull()) {
43 Handle(Aspect_DisplayConnection) aDisplayConnection;
45 aDisplayConnection = new Aspect_DisplayConnection( displayName );
47 aDisplayConnection = new Aspect_DisplayConnection();
49 aGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);
52 return new V3d_Viewer(aGraphicDriver, name, domain, viewSize, viewProjection, Quantity_NOC_GRAY30,
53 V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT, computedMode, defaultComputedMode,
57 // VSR: Uncomment below line to allow texture background support in OCC viewer
58 #define OCC_ENABLE_TEXTURED_BACKGROUND
61 Get data for supported background modes: gradient types, identifiers and supported image formats
63 QString XGUI_Viewer::backgroundData(QStringList& gradList, QIntList& idList, QIntList& txtList)
65 gradList << tr("Horizontal gradient") << tr("Vertical gradient")
66 << tr("First diagonal gradient") << tr("Second diagonal gradient")
67 << tr("First corner gradient") << tr("Second corner gradient")
68 << tr("Third corner gradient") << tr("Fourth corner gradient");
69 idList << XGUI::HorizontalGradient << XGUI::VerticalGradient << XGUI::Diagonal1Gradient
70 << XGUI::Diagonal2Gradient << XGUI::Corner1Gradient << XGUI::Corner2Gradient
71 << XGUI::Corner3Gradient << XGUI::Corner4Gradient;
72 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
73 txtList << XGUI::CenterTexture << XGUI::TileTexture << XGUI::StretchTexture;
75 return tr("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
78 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
80 myMainWindow(theParent),
81 myPreselectionEnabled(true),
82 mySelectionEnabled(true),
83 myMultiSelectionEnabled(true),
85 myInteractionStyle(XGUI::STANDARD),
92 // standard interaction style
93 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
94 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
96 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
97 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
99 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
100 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
102 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
103 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
105 // "key free" interaction style
106 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::NoModifier;
107 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::RightButton;
109 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
110 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
112 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
113 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
115 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
116 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
119 // init CasCade viewers
120 myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(), "", "", 1000.0,
121 V3d_XposYnegZpos, Standard_True, Standard_True);
122 myV3dViewer->SetDefaultLights();
125 myAISContext = new AIS_InteractiveContext(myV3dViewer);
126 myAISContext->SelectionColor(Quantity_NOC_WHITE);
128 // display isoline on planar faces (box for ex.)
129 myAISContext->IsoOnPlane(true);
131 if (DisplayTrihedron) {
132 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
133 myTrihedron = new AIS_Trihedron(anAxis);
134 myTrihedron->SetInfiniteState( Standard_True);
136 Quantity_Color Col(193 / 255., 205 / 255., 193 / 255., Quantity_TOC_RGB);
137 myTrihedron->SetArrowColor(Col.Name());
138 myTrihedron->SetSize(myTrihedronSize);
139 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
140 if (drawer->HasDatumAspect()) {
141 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
142 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
143 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
144 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
147 // set zooming style to standard
148 //myZoomingStyle = 0;
150 QMdiArea* aMDI = myMainWindow->mdiArea();
151 connect(aMDI, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(onWindowActivated(QMdiSubWindow*)));
155 XGUI_Viewer::~XGUI_Viewer(void)
157 myAISContext.Nullify();
158 myV3dViewer.Nullify();
161 QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
164 XGUI_ViewWindow* view = new XGUI_ViewWindow(this, theType);
165 // get main view window (created by view frame)
166 //OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
167 // initialize main view window
169 // set default background for view window
170 //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
171 //// connect signal from viewport
172 //connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
173 //connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
174 if (myViews.size() == 0)
175 setTrihedronShown(true);
177 view->setBackground(XGUI_ViewBackground(XGUI::VerticalGradient, Qt::white, QColor(Qt::blue).lighter()));
178 //view->setBackground(XGUI_ViewBackground(Qt::black));
180 QMdiArea* aMDI = myMainWindow->mdiArea();
181 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,
194 XGUI::HotOperation theOper, Qt::KeyboardModifiers theState,
195 Qt::MouseButtons theButton)
197 myStateMap[theInteractionStyle][theOper] = theState;
198 myButtonMap[theInteractionStyle][theOper] = theButton;
201 /*! Gets hot button for operation \a theOper.
202 *\param theOper - input hot operation
203 *\param theState - output state from state map operations.
204 *\param theButton - output state from button map operations.
206 void XGUI_Viewer::getHotButton(XGUI::InteractionStyle theInteractionStyle,
207 XGUI::HotOperation theOper, Qt::KeyboardModifiers& theState,
208 Qt::MouseButtons& theButton)
210 theState = myStateMap[theInteractionStyle][theOper];
211 theButton = myButtonMap[theInteractionStyle][theOper];
215 Changes visibility of trihedron to opposite
217 void XGUI_Viewer::toggleTrihedron()
219 setTrihedronShown(!isTrihedronVisible());
223 \return true if trihedron is visible
225 bool XGUI_Viewer::isTrihedronVisible() const
227 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed(myTrihedron);
231 Sets visibility state of trihedron
232 \param on - new state
235 void XGUI_Viewer::setTrihedronShown(bool on)
237 if (myTrihedron.IsNull())
241 myAISContext->Display(myTrihedron);
242 myAISContext->Deactivate(myTrihedron);
244 myAISContext->Erase(myTrihedron);
249 \return trihedron size
251 double XGUI_Viewer::trihedronSize() const
254 if (!myTrihedron.IsNull())
255 sz = myTrihedron->Size();
260 Changes trihedron size
263 void XGUI_Viewer::setTrihedronSize(const double sz, bool isRelative)
265 if (myTrihedronSize != sz || isRelative != myIsRelative) {
266 myTrihedronSize = sz;
267 myIsRelative = isRelative;
273 * Update the size of the trihedron
275 void XGUI_Viewer::updateTrihedron()
277 if (myTrihedron.IsNull())
283 if (computeTrihedronSize(newSz, oldSz))
284 myTrihedron->SetSize(newSz);
286 } else if (myTrihedron->Size() != myTrihedronSize) {
287 myTrihedron->SetSize(myTrihedronSize);
292 Get new and current trihedron size corresponding to the current model size
294 bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
299 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
300 Handle(V3d_Viewer) viewer = v3dViewer();
301 viewer->InitActiveViews();
302 if (!viewer->MoreActiveViews())
305 Handle(V3d_View) view3d = viewer->ActiveView();
311 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
314 view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
316 if (Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() || Xmax == RealLast()
317 || Ymax == RealLast() || Zmax == RealLast())
320 aMaxSide = Xmax - Xmin;
321 if (aMaxSide < Ymax - Ymin)
322 aMaxSide = Ymax - Ymin;
323 if (aMaxSide < Zmax - Zmin)
324 aMaxSide = Zmax - Zmin;
327 // The boundary box of the view may be initialized but nullified
328 // (case of infinite objects)
329 if (aMaxSide < Precision::Confusion())
332 static float EPS = (float)5.0E-3;
333 theSize = trihedron()->Size();
334 //theNewSize = aMaxSide*aSizeInPercents / 100.0;
336 return fabs(theNewSize - theSize) > theSize * EPS || fabs(theNewSize - theSize) > theNewSize * EPS;
339 void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
344 emit deleteView( static_cast<XGUI_ViewWindow*>(theView->widget()) );
345 removeView( theView );
347 // if this is last view
348 if (myViews.size() == 0) {
349 Standard_Integer aViewsNb = 0;
350 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
352 if ( aViewsNb < 2 ) {
353 //clean up presentations before last view is closed
354 myAISContext->RemoveAll(Standard_False);
359 /*!Remove view window \a theView from view manager.
360 *And close the last view, if it has \a theView.
362 void XGUI_Viewer::removeView( QMdiSubWindow* theView )
364 XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
366 aWindow->disconnect( this );
367 myViews.removeAt( myViews.indexOf( theView ) );
368 if ( myActiveView == theView )
370 if ( myViews.size() == 0 )
371 emit lastViewClosed();
375 /*void XGUI_Viewer::onViewMapped()
377 setTrihedronShown(true);
381 void XGUI_Viewer::addView(QMdiSubWindow* theView)
383 XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
385 connect(aWindow, SIGNAL(closed(QMdiSubWindow*)),
386 this, SLOT(onViewClosed(QMdiSubWindow*)));
388 connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)),
389 this, SIGNAL(tryCloseView(XGUI_ViewWindow*)));
391 connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)),
392 this, SIGNAL(mousePress(XGUI_ViewWindow*, QMouseEvent*)));
394 connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
395 this, SIGNAL(mouseRelease(XGUI_ViewWindow*, QMouseEvent*)));
397 connect(aWindow, SIGNAL(mouseDoubleClicked(XGUI_ViewWindow*, QMouseEvent*)),
398 this, SIGNAL(mouseDoubleClick(XGUI_ViewWindow*, QMouseEvent*)));
400 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
401 this, SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)));
403 connect(aWindow, SIGNAL(keyPressed(XGUI_ViewWindow*, QKeyEvent*)),
404 this, SIGNAL(keyPress(XGUI_ViewWindow*, QKeyEvent*)));
406 connect(aWindow, SIGNAL(keyReleased(XGUI_ViewWindow*, QKeyEvent*)),
407 this, SIGNAL(keyRelease(XGUI_ViewWindow*, QKeyEvent*)));
409 // connect(aWindow, SIGNAL(contextMenuRequested( QContextMenuEvent* )),
410 // this, SLOT (onContextMenuRequested( QContextMenuEvent* )));
412 myViews.append(theView);
416 Emit activated for view \a view.
418 void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)
420 if (view && (view != myActiveView) && (!view->isMinimized())) {
422 ((XGUI_ViewWindow*)myActiveView->widget())->windowActivated();
423 QList<QMdiSubWindow*>::iterator aIt;
424 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
425 if ((*aIt) != myActiveView) {
426 ((XGUI_ViewWindow*)(*aIt)->widget())->windowDeactivated();
433 void XGUI_Viewer::onWindowMinimized(QMdiSubWindow* theWnd)
435 if (myActiveView == theWnd) {
437 QList<QMdiSubWindow*>::iterator aIt;
438 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
439 if (!(*aIt)->widget()->isMinimized()) {
441 onWindowActivated(*aIt);