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