Salome HOME
6d207e991b56425e08d9a8e3bca975d39d2f6586
[modules/shaper.git] / src / XGUI / XGUI_Viewer.cpp
1 #include "XGUI_Viewer.h"
2 #include "XGUI_MainWindow.h"
3 #include "XGUI_ViewWindow.h"
4 #include "XGUI_ViewPort.h"
5
6 #include <QMdiArea>
7 #include <QMdiSubWindow>
8
9 #include <V3d_View.hxx>
10
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>
20
21 #ifdef WIN32
22 #include <WNT_Window.hxx>
23 #else
24 #include <Xw_Window.hxx>
25 #endif
26
27 XGUI_Viewer::InteractionStyle2StatesMap XGUI_Viewer::myStateMap;
28 XGUI_Viewer::InteractionStyle2ButtonsMap XGUI_Viewer::myButtonMap;
29 static bool isInitialized = false;
30
31 /*!
32  Creates viewer 3d [ static ]
33  */
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)
39 {
40   static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
41   if (aGraphicDriver.IsNull()) {
42     Handle(Aspect_DisplayConnection) aDisplayConnection;
43 #ifndef WIN32
44     aDisplayConnection = new Aspect_DisplayConnection( displayName );
45 #else
46     aDisplayConnection = new Aspect_DisplayConnection();
47 #endif
48     aGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);
49   }
50
51   return new V3d_Viewer(aGraphicDriver, name, domain, viewSize, viewProjection, Quantity_NOC_GRAY30,
52                         V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT, computedMode, defaultComputedMode,
53                         V3d_TEX_NONE);
54 }
55
56 // VSR: Uncomment below line to allow texture background support in OCC viewer
57 #define OCC_ENABLE_TEXTURED_BACKGROUND
58
59 /*!
60  Get data for supported background modes: gradient types, identifiers and supported image formats
61  */
62 QString XGUI_Viewer::backgroundData(QStringList& gradList, QIntList& idList, QIntList& txtList)
63 {
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;
73 #endif
74   return tr("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
75 }
76
77 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
78     : QObject(theParent), 
79     myMainWindow(theParent), 
80     myPreselectionEnabled(true), 
81     mySelectionEnabled(true), 
82     myMultiSelectionEnabled(true), 
83     myIsRelative(true), 
84     myInteractionStyle(XGUI::STANDARD), 
85     myTrihedronSize(100),
86     myActiveView(0)
87 {
88   if (!isInitialized) {
89     isInitialized = true;
90
91     // standard interaction style
92     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
93     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
94
95     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
96     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
97
98     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
99     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
100
101     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
102     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
103
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;
107
108     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
109     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
110
111     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
112     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
113
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
116   }
117
118   // init CasCade viewers
119   myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(), "", "", 1000.0,
120                              V3d_XposYnegZpos, Standard_True, Standard_True);
121   myV3dViewer->SetDefaultLights();
122
123   // init selector
124   myAISContext = new AIS_InteractiveContext(myV3dViewer);
125   myAISContext->SelectionColor(Quantity_NOC_WHITE);
126
127   // display isoline on planar faces (box for ex.)
128   myAISContext->IsoOnPlane(true);
129
130   if (DisplayTrihedron) {
131     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
132     myTrihedron = new AIS_Trihedron(anAxis);
133     myTrihedron->SetInfiniteState( Standard_True);
134
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));
144     }
145   }
146   // set zooming style to standard
147   //myZoomingStyle = 0;
148
149   QMdiArea* aMDI = myMainWindow->mdiArea();
150   connect(aMDI, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(onWindowActivated(QMdiSubWindow*)));
151
152 }
153
154 XGUI_Viewer::~XGUI_Viewer(void)
155 {
156   myAISContext.Nullify();
157   myV3dViewer.Nullify();
158 }
159
160 QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
161 {
162   // create view frame
163   XGUI_ViewWindow* view = new XGUI_ViewWindow(this, theType);
164   // get main view window (created by view frame)
165   //OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
166   // initialize main view window
167   //initView( vw );
168   // set default background for view window
169   //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
170   //// connect signal from viewport
171     //connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
172     //connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
173     if (myViews.size() == 0) 
174         setTrihedronShown(true);
175
176     view->setBackground(XGUI_ViewBackground(XGUI::VerticalGradient, Qt::white, QColor(Qt::blue).lighter()));
177   //view->setBackground(XGUI_ViewBackground(Qt::black));
178
179   QMdiArea* aMDI = myMainWindow->mdiArea();
180   QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
181     addView(aWnd);
182   aWnd->setGeometry(0, 0, aMDI->width() / 2, aMDI->height() / 2);
183   aWnd->show();
184   return aWnd;
185 }
186
187 /*! Sets hot button
188  *\param theOper - hot operation
189  *\param theState - adding state to state map operations.
190  *\param theButton - adding state to button map operations.
191  */
192 void XGUI_Viewer::setHotButton(XGUI::InteractionStyle theInteractionStyle,
193                                XGUI::HotOperation theOper, Qt::KeyboardModifiers theState,
194                                Qt::MouseButtons theButton)
195 {
196   myStateMap[theInteractionStyle][theOper] = theState;
197   myButtonMap[theInteractionStyle][theOper] = theButton;
198 }
199
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.
204  */
205 void XGUI_Viewer::getHotButton(XGUI::InteractionStyle theInteractionStyle,
206                                XGUI::HotOperation theOper, Qt::KeyboardModifiers& theState,
207                                Qt::MouseButtons& theButton)
208 {
209   theState = myStateMap[theInteractionStyle][theOper];
210   theButton = myButtonMap[theInteractionStyle][theOper];
211 }
212
213 /*!
214  Changes visibility of trihedron to opposite
215  */
216 void XGUI_Viewer::toggleTrihedron()
217 {
218   setTrihedronShown(!isTrihedronVisible());
219 }
220
221 /*!
222  \return true if trihedron is visible
223  */
224 bool XGUI_Viewer::isTrihedronVisible() const
225 {
226   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed(myTrihedron);
227 }
228
229 /*!
230  Sets visibility state of trihedron
231  \param on - new state
232  */
233
234 void XGUI_Viewer::setTrihedronShown(bool on)
235 {
236   if (myTrihedron.IsNull())
237     return;
238
239   if (on) {
240     myAISContext->Display(myTrihedron);
241     myAISContext->Deactivate(myTrihedron);
242   } else {
243     myAISContext->Erase(myTrihedron);
244   }
245 }
246
247 /*!
248  \return trihedron size
249  */
250 double XGUI_Viewer::trihedronSize() const
251 {
252   double sz = 0;
253   if (!myTrihedron.IsNull())
254     sz = myTrihedron->Size();
255   return sz;
256 }
257
258 /*!
259  Changes trihedron size
260  \param sz - new size
261  */
262 void XGUI_Viewer::setTrihedronSize(const double sz, bool isRelative)
263 {
264   if (myTrihedronSize != sz || isRelative != myIsRelative) {
265     myTrihedronSize = sz;
266     myIsRelative = isRelative;
267     updateTrihedron();
268   }
269 }
270
271 /*! 
272  * Update the size of the trihedron
273  */
274 void XGUI_Viewer::updateTrihedron()
275 {
276   if (myTrihedron.IsNull())
277     return;
278
279   if (myIsRelative) {
280     double newSz, oldSz;
281
282     if (computeTrihedronSize(newSz, oldSz))
283       myTrihedron->SetSize(newSz);
284
285   } else if (myTrihedron->Size() != myTrihedronSize) {
286     myTrihedron->SetSize(myTrihedronSize);
287   }
288 }
289
290 /*!
291  Get new and current trihedron size corresponding to the current model size
292  */
293 bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
294 {
295   theNewSize = 100;
296   theSize = 100;
297
298   //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
299   Handle(V3d_Viewer) viewer = v3dViewer();
300   viewer->InitActiveViews();
301   if (!viewer->MoreActiveViews())
302     return false;
303
304   Handle(V3d_View) view3d = viewer->ActiveView();
305   //SRN: END of fix
306
307   if (view3d.IsNull())
308     return false;
309
310   double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
311   double aMaxSide;
312
313   view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
314
315   if (Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() || Xmax == RealLast()
316       || Ymax == RealLast() || Zmax == RealLast())
317     return false;
318
319   aMaxSide = Xmax - Xmin;
320   if (aMaxSide < Ymax - Ymin)
321     aMaxSide = Ymax - Ymin;
322   if (aMaxSide < Zmax - Zmin)
323     aMaxSide = Zmax - Zmin;
324
325   // IPAL21687
326   // The boundary box of the view may be initialized but nullified
327   // (case of infinite objects)
328   if (aMaxSide < Precision::Confusion())
329     return false;
330
331   static float EPS = (float)5.0E-3;
332   theSize = trihedron()->Size();
333   //theNewSize = aMaxSide*aSizeInPercents / 100.0;
334
335   return fabs(theNewSize - theSize) > theSize * EPS || fabs(theNewSize - theSize) > theNewSize * EPS;
336 }
337
338 void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
339 {
340     if ( !theView )
341         return;
342
343     emit deleteView( static_cast<XGUI_ViewWindow*>(theView->widget()) );
344     removeView( theView );
345
346     // if this is last view
347     if (myViews.size() == 0) {
348         Standard_Integer aViewsNb = 0;
349         for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
350             ++aViewsNb;
351         if ( aViewsNb < 2 ) {
352             //clean up presentations before last view is closed
353             myAISContext->RemoveAll(Standard_False);
354         }
355     }
356 }
357
358 /*!Remove view window \a theView from view manager.
359  *And close the last view, if it has \a theView.
360 */
361 void XGUI_Viewer::removeView( QMdiSubWindow* theView )
362 {
363     XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
364
365     aWindow->disconnect( this );
366     myViews.removeAt( myViews.indexOf( theView ) );
367     if ( myActiveView == theView )
368         myActiveView = 0;
369     if ( myViews.size() == 0 )
370         emit lastViewClosed();
371 }
372
373
374 /*void XGUI_Viewer::onViewMapped()
375 {
376   setTrihedronShown(true);
377 }*/
378
379
380 void XGUI_Viewer::addView(QMdiSubWindow* theView)
381 {
382     XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
383
384     connect(aWindow, SIGNAL(closed(QMdiSubWindow*)),
385             this,    SLOT(onViewClosed(QMdiSubWindow*)));
386
387     connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)),
388             this,    SIGNAL(tryCloseView(XGUI_ViewWindow*)));
389
390     connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)),
391             this,    SIGNAL(mousePress(XGUI_ViewWindow*, QMouseEvent*)));
392
393     connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
394             this,    SIGNAL(mouseRelease(XGUI_ViewWindow*, QMouseEvent*)));
395
396     connect(aWindow, SIGNAL(mouseDoubleClicked(XGUI_ViewWindow*, QMouseEvent*)),
397             this,    SIGNAL(mouseDoubleClick(XGUI_ViewWindow*, QMouseEvent*)));
398
399     connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
400             this,    SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)));
401
402     connect(aWindow, SIGNAL(keyPressed(XGUI_ViewWindow*, QKeyEvent*)),
403             this,    SIGNAL(keyPress(XGUI_ViewWindow*, QKeyEvent*)));
404
405     connect(aWindow, SIGNAL(keyReleased(XGUI_ViewWindow*, QKeyEvent*)),
406             this,    SIGNAL(keyRelease(XGUI_ViewWindow*, QKeyEvent*)));
407
408 //    connect(aWindow, SIGNAL(contextMenuRequested( QContextMenuEvent* )),
409 //            this,    SLOT  (onContextMenuRequested( QContextMenuEvent* )));
410
411     myViews.append(theView);
412 }
413
414 /*!
415     Emit activated for view \a view.
416 */
417 void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)
418 {
419   if (view && (view != myActiveView)) {
420     myActiveView = view;
421     ((XGUI_ViewWindow*)myActiveView->widget())->windowActivated();
422     QList<QMdiSubWindow*>::iterator aIt;
423     for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt)
424       if ((*aIt) != myActiveView)
425         ((XGUI_ViewWindow*)(*aIt)->widget())->windowDeactivated();
426   }
427 }