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>
23 #include <QMouseEvent>
26 #include <WNT_Window.hxx>
28 #include <Xw_Window.hxx>
31 XGUI_Viewer::InteractionStyle2StatesMap XGUI_Viewer::myStateMap;
32 XGUI_Viewer::InteractionStyle2ButtonsMap XGUI_Viewer::myButtonMap;
33 static bool isInitialized = false;
36 Creates viewer 3d [ static ]
38 Handle(V3d_Viewer) CreateViewer(const Standard_ExtString name, const Standard_CString displayName,
39 const Standard_CString domain, const Standard_Real viewSize,
40 const V3d_TypeOfOrientation viewProjection,
41 const Standard_Boolean computedMode,
42 const Standard_Boolean defaultComputedMode)
44 static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
45 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, Quantity_NOC_GRAY30,
56 V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT, computedMode, defaultComputedMode,
60 // VSR: Uncomment below line to allow texture background support in OCC viewer
61 #define OCC_ENABLE_TEXTURED_BACKGROUND
64 Get data for supported background modes: gradient types, identifiers and supported image formats
66 QString XGUI_Viewer::backgroundData(QStringList& gradList, QIntList& idList, QIntList& txtList)
68 gradList << tr("Horizontal gradient") << tr("Vertical gradient")
69 << tr("First diagonal gradient") << tr("Second diagonal gradient")
70 << tr("First corner gradient") << tr("Second corner gradient")
71 << tr("Third corner gradient") << tr("Fourth corner gradient");
72 idList << XGUI::HorizontalGradient << XGUI::VerticalGradient << XGUI::Diagonal1Gradient
73 << XGUI::Diagonal2Gradient << XGUI::Corner1Gradient << XGUI::Corner2Gradient
74 << XGUI::Corner3Gradient << XGUI::Corner4Gradient;
75 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
76 txtList << XGUI::CenterTexture << XGUI::TileTexture << XGUI::StretchTexture;
78 return tr("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
81 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
83 myMainWindow(theParent),
84 myPreselectionEnabled(true),
85 mySelectionEnabled(true),
86 myMultiSelectionEnabled(true),
88 myInteractionStyle(XGUI::STANDARD),
95 // standard interaction style
96 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
97 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
99 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
100 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
102 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
103 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
105 XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
106 XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
108 // "key free" interaction style
109 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::NoModifier;
110 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::RightButton;
112 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
113 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
115 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
116 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
118 XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
119 XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
122 // init CasCade viewers
123 myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(), "", "", 1000.0,
124 V3d_XposYnegZpos, Standard_True, Standard_True);
125 myV3dViewer->SetDefaultLights();
128 myAISContext = new AIS_InteractiveContext(myV3dViewer);
129 myAISContext->SelectionColor(Quantity_NOC_WHITE);
131 // display isoline on planar faces (box for ex.)
132 myAISContext->IsoOnPlane(true);
134 if (DisplayTrihedron) {
135 Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
136 myTrihedron = new AIS_Trihedron(anAxis);
137 myTrihedron->SetInfiniteState( Standard_True);
139 Quantity_Color Col(193 / 255., 205 / 255., 193 / 255., Quantity_TOC_RGB);
140 myTrihedron->SetArrowColor(Col.Name());
141 myTrihedron->SetSize(myTrihedronSize);
142 Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
143 if (drawer->HasDatumAspect()) {
144 Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
145 daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
146 daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
147 daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
150 // set zooming style to standard
151 //myZoomingStyle = 0;
153 QMdiArea* aMDI = myMainWindow->mdiArea();
154 connect(aMDI, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(onWindowActivated(QMdiSubWindow*)));
158 XGUI_Viewer::~XGUI_Viewer(void)
160 myAISContext.Nullify();
161 myV3dViewer.Nullify();
164 QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
167 XGUI_ViewWindow* view = new XGUI_ViewWindow(this, theType);
168 // get main view window (created by view frame)
169 //OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
170 // initialize main view window
172 // set default background for view window
173 //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
174 //// connect signal from viewport
175 //connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
176 //connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
177 if (myViews.size() == 0)
178 setTrihedronShown(true);
180 view->setBackground(XGUI_ViewBackground(XGUI::VerticalGradient, Qt::white, QColor(Qt::blue).lighter()));
181 //view->setBackground(XGUI_ViewBackground(Qt::black));
183 QMdiArea* aMDI = myMainWindow->mdiArea();
184 QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
186 aWnd->setGeometry(0, 0, aMDI->width() / 2, aMDI->height() / 2);
192 *\param theOper - hot operation
193 *\param theState - adding state to state map operations.
194 *\param theButton - adding state to button map operations.
196 void XGUI_Viewer::setHotButton(XGUI::InteractionStyle theInteractionStyle,
197 XGUI::HotOperation theOper, Qt::KeyboardModifiers theState,
198 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,
210 XGUI::HotOperation theOper, Qt::KeyboardModifiers& theState,
211 Qt::MouseButtons& theButton)
213 theState = myStateMap[theInteractionStyle][theOper];
214 theButton = myButtonMap[theInteractionStyle][theOper];
218 Changes visibility of trihedron to opposite
220 void XGUI_Viewer::toggleTrihedron()
222 setTrihedronShown(!isTrihedronVisible());
226 \return true if trihedron is visible
228 bool XGUI_Viewer::isTrihedronVisible() const
230 return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed(myTrihedron);
234 Sets visibility state of trihedron
235 \param on - new state
238 void XGUI_Viewer::setTrihedronShown(bool on)
240 if (myTrihedron.IsNull())
244 myAISContext->Display(myTrihedron);
245 myAISContext->Deactivate(myTrihedron);
247 myAISContext->Erase(myTrihedron);
252 \return trihedron size
254 double XGUI_Viewer::trihedronSize() const
257 if (!myTrihedron.IsNull())
258 sz = myTrihedron->Size();
263 Changes trihedron size
266 void XGUI_Viewer::setTrihedronSize(const double sz, bool isRelative)
268 if (myTrihedronSize != sz || isRelative != myIsRelative) {
269 myTrihedronSize = sz;
270 myIsRelative = isRelative;
276 * Update the size of the trihedron
278 void XGUI_Viewer::updateTrihedron()
280 if (myTrihedron.IsNull())
286 if (computeTrihedronSize(newSz, oldSz))
287 myTrihedron->SetSize(newSz);
289 } else if (myTrihedron->Size() != myTrihedronSize) {
290 myTrihedron->SetSize(myTrihedronSize);
295 Get new and current trihedron size corresponding to the current model size
297 bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
302 //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
303 Handle(V3d_Viewer) viewer = v3dViewer();
304 viewer->InitActiveViews();
305 if (!viewer->MoreActiveViews())
308 Handle(V3d_View) view3d = viewer->ActiveView();
314 double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
317 view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
319 if (Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() || Xmax == RealLast()
320 || Ymax == RealLast() || Zmax == RealLast())
323 aMaxSide = Xmax - Xmin;
324 if (aMaxSide < Ymax - Ymin)
325 aMaxSide = Ymax - Ymin;
326 if (aMaxSide < Zmax - Zmin)
327 aMaxSide = Zmax - Zmin;
330 // The boundary box of the view may be initialized but nullified
331 // (case of infinite objects)
332 if (aMaxSide < Precision::Confusion())
335 static float EPS = (float)5.0E-3;
336 theSize = trihedron()->Size();
337 //theNewSize = aMaxSide*aSizeInPercents / 100.0;
339 return fabs(theNewSize - theSize) > theSize * EPS || fabs(theNewSize - theSize) > theNewSize * EPS;
342 void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
347 emit deleteView( static_cast<XGUI_ViewWindow*>(theView->widget()) );
348 removeView( theView );
350 // if this is last view
351 if (myViews.size() == 0) {
352 Standard_Integer aViewsNb = 0;
353 for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
355 if ( aViewsNb < 2 ) {
356 //clean up presentations before last view is closed
357 myAISContext->RemoveAll(Standard_False);
362 /*!Remove view window \a theView from view manager.
363 *And close the last view, if it has \a theView.
365 void XGUI_Viewer::removeView( QMdiSubWindow* theView )
367 XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
369 aWindow->disconnect( this );
370 myViews.removeAt( myViews.indexOf( theView ) );
371 if ( myActiveView == theView )
373 if ( myViews.size() == 0 )
374 emit lastViewClosed();
378 /*void XGUI_Viewer::onViewMapped()
380 setTrihedronShown(true);
384 void XGUI_Viewer::addView(QMdiSubWindow* theView)
386 XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
388 connect(aWindow, SIGNAL(closed(QMdiSubWindow*)),
389 this, SLOT(onViewClosed(QMdiSubWindow*)));
391 connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)),
392 this, SIGNAL(tryCloseView(XGUI_ViewWindow*)));
394 connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)),
395 this, SLOT(onMousePressed(XGUI_ViewWindow*, QMouseEvent*)));
397 connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
398 this, SIGNAL(mouseRelease(XGUI_ViewWindow*, QMouseEvent*)));
400 connect(aWindow, SIGNAL(mouseDoubleClicked(XGUI_ViewWindow*, QMouseEvent*)),
401 this, SIGNAL(mouseDoubleClick(XGUI_ViewWindow*, QMouseEvent*)));
403 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
404 this, SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)));
406 connect(aWindow, SIGNAL(keyPressed(XGUI_ViewWindow*, QKeyEvent*)),
407 this, SIGNAL(keyPress(XGUI_ViewWindow*, QKeyEvent*)));
409 connect(aWindow, SIGNAL(keyReleased(XGUI_ViewWindow*, QKeyEvent*)),
410 this, SIGNAL(keyRelease(XGUI_ViewWindow*, QKeyEvent*)));
412 // connect(aWindow, SIGNAL(contextMenuRequested( QContextMenuEvent* )),
413 // this, SLOT (onContextMenuRequested( QContextMenuEvent* )));
415 connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
416 this, SLOT(onMouseMove(XGUI_ViewWindow*, QMouseEvent*)));
418 connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
419 this, SLOT(onMouseReleased(XGUI_ViewWindow*, QMouseEvent*)));
421 myViews.append(theView);
425 Emit activated for view \a view.
427 void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)
429 if (view && (view != myActiveView) && (!view->isMinimized())) {
431 ((XGUI_ViewWindow*)myActiveView->widget())->windowActivated();
432 QList<QMdiSubWindow*>::iterator aIt;
433 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
434 if ((*aIt) != myActiveView) {
435 ((XGUI_ViewWindow*)(*aIt)->widget())->windowDeactivated();
442 void XGUI_Viewer::onWindowMinimized(QMdiSubWindow* theWnd)
444 if (myActiveView == theWnd) {
446 QList<QMdiSubWindow*>::iterator aIt;
447 for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
448 if (!(*aIt)->widget()->isMinimized()) {
450 onWindowActivated(*aIt);
458 SLOT: called on mouse move, processes hilighting
460 void XGUI_Viewer::onMouseMove(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
462 XGUI_ViewPort* aViewPort = theWindow->viewPort();
463 Handle(V3d_View) aView3d = aViewPort->getView();
465 if ( !aView3d.IsNull() )
466 myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
470 SLOT: called on mouse button release, finishes selection
472 void XGUI_Viewer::onMouseReleased(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
474 myAISContext->Select();
476 emit selectionChanged();