Salome HOME
8efbfc0c820a953abefae0050556bc9daf0051ed
[modules/shaper.git] / src / XGUI / XGUI_ViewPort.cpp
1
2 #include "XGUI_ViewPort.h"
3 #include "XGUI_ViewWindow.h"
4 #include "XGUI_Viewer.h"
5 #include "XGUI_Constants.h"
6
7 #include <QPaintEvent>
8 #include <QPainter>
9 #include <QFileInfo>
10 #include <QApplication>
11
12 #include <V3d_OrthographicView.hxx>
13 #include <V3d_PerspectiveView.hxx>
14 #include <Visual3d_View.hxx>
15 #include <Graphic3d_GraphicDriver.hxx>
16
17 #ifdef WIN32
18 #include <WNT_Window.hxx>
19 #else
20 #include <Xw_Window.hxx>
21 #endif
22
23 #include <GL/gl.h>
24
25 static double rx = 0.;
26 static double ry = 0.;
27 static int sx = 0;
28 static int sy = 0;
29 static Standard_Boolean zRotation = Standard_False;
30
31
32 /*!
33  Create native view window for CasCade view [ static ]
34  */
35 Handle(Aspect_Window) CreateCasWindow(const Handle(V3d_View)& view, WId winId)
36 {
37   Aspect_Handle aWindowHandle = (Aspect_Handle) winId;
38 #ifdef WIN32
39   Handle(WNT_Window) viewWindow = new WNT_Window(aWindowHandle);
40 #else
41   Handle(Aspect_DisplayConnection) aDispConnection = view->Viewer()->Driver()->GetDisplayConnection();
42   Handle(Xw_Window) viewWindow = new Xw_Window( aDispConnection, aWindowHandle );
43 #endif
44   return viewWindow;
45 }
46
47
48 //************************************************************************
49 //************************************************************************
50 //************************************************************************
51 XGUI_ViewPort::XGUI_ViewPort(XGUI_ViewWindow* theParent,
52                              const Handle(V3d_Viewer)& theViewer,
53                              V3d_TypeOfView theType)
54     : QWidget(theParent),
55       myPaintersRedrawing(false),
56       myScale(1.0),
57       myIsAdvancedZoomingEnabled(false),
58       myBgImgHeight(0),
59       myBgImgWidth(0)
60 {
61   setMouseTracking(true);
62   setBackgroundRole(QPalette::NoRole);
63
64   // set focus policy to threat QContextMenuEvent from keyboard  
65   setFocusPolicy(Qt::StrongFocus);
66   setAttribute(Qt::WA_PaintOnScreen);
67   setAttribute(Qt::WA_NoSystemBackground);
68   setAutoFillBackground(false);
69
70   if (theType == V3d_ORTHOGRAPHIC) {
71     myOrthoView = new V3d_OrthographicView(theViewer);
72     myActiveView = myOrthoView;
73     myPerspView = 0;
74   } else {
75     myPerspView = new V3d_PerspectiveView(theViewer);
76     myActiveView = myPerspView;
77   }
78   myActiveView->SetSurfaceDetail(V3d_TEX_ALL);
79 }
80
81 //***********************************************
82 XGUI_ViewPort::~XGUI_ViewPort()
83 {
84 }
85
86 //***********************************************
87 bool XGUI_ViewPort::mapView(const Handle(V3d_View)& theView)
88 {
89   if (!setWindow(theView))
90     return false;
91
92   if (!mapped(theView)) {
93     theView->SetWindow(myWindow);
94     //if (theView != activeView())
95       //theView->View()->Deactivate();
96   }
97
98   /* create static trihedron (16551: EDF PAL 501) */
99   //OCCViewer_ViewWindow* aVW = dynamic_cast<OCCViewer_ViewWindow*>( parentWidget()->parentWidget()->parentWidget() );
100   //if ( aVW ) {
101   //    OCCViewer_Viewer* aViewModel = dynamic_cast<OCCViewer_Viewer*>( aVW->getViewManager()->getViewModel() );
102   //    if ( aViewModel && aViewModel->isStaticTrihedronDisplayed() ){
103   //theView->ZBufferTriedronSetup();
104   theView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_WHITE, 0.05, V3d_ZBUFFER);
105   //    }
106   //}
107
108   emit(vpMapped());
109
110   return true;
111 }
112
113 //***********************************************
114 bool XGUI_ViewPort::setWindow(const Handle(V3d_View)& theView)
115 {
116   if (!myWindow.IsNull())
117     return true;
118
119   if (theView.IsNull())
120     return false;
121
122   attachWindow(theView, CreateCasWindow(theView, winId()));
123
124   myWindow = theView->Window();
125   return !myWindow.IsNull();
126 }
127
128 //***********************************************
129 bool XGUI_ViewPort::mapped(const Handle(V3d_View)& theView) const
130 {
131   return (!theView.IsNull() && theView->View()->IsDefined());
132 }
133
134 //***********************************************
135 void XGUI_ViewPort::updateBackground()
136 {
137   if (activeView().IsNull())
138     return;
139   if (!myBackground.isValid())
140     return;
141
142   // VSR: Important note on below code.
143   // In OCCT (in version 6.5.2), things about the background drawing
144   // are not straightforward and not clearly understandable:
145   // - Horizontal gradient is drawn vertically (!), well ok, from top side to bottom one.
146   // - Vertical gradient is drawn horizontally (!), from right side to left one (!!!).
147   // - First and second diagonal gradients are confused.
148   // - Image texture, once set, can not be removed (!).
149   // - Texture image fill mode Aspect_FM_NONE is not taken into account (and means the same
150   //   as Aspect_FM_CENTERED).
151   // - The only way to cancel gradient background (and get back to single colored) is to
152   //   set gradient background style to Aspect_GFM_NONE while passing two colors is also needed
153   //   (see V3d_View::SetBgGradientColors() function).
154   // - Also, it is impossible to draw texture image above the gradiented background (only above
155   //   single-colored).
156   // In OCCT 6.5.3 all above mentioned problems are fixed; so, above comment should be removed as soon
157   // as SALOME is migrated to OCCT 6.5.3. The same concerns #ifdef statements in the below code
158   switch(myBackground.mode()) {
159   case XGUI::ColorBackground: {
160     QColor c = myBackground.color();
161     if (c.isValid()) {
162       // Unset texture should be done here
163       // ...
164       Quantity_Color qCol(c.red() / 255., c.green() / 255., c.blue() / 255., Quantity_TOC_RGB);
165       activeView()->SetBgGradientStyle(Aspect_GFM_NONE); // cancel gradient background
166       activeView()->SetBgImageStyle(Aspect_FM_NONE); // cancel texture background
167       // then change background color
168       activeView()->SetBackgroundColor(qCol);
169       // update viewer
170       activeView()->Update();
171     }
172     break;
173   }
174   case XGUI::SimpleGradientBackground: {
175     QColor c1, c2;
176     int type = myBackground.gradient(c1, c2);
177     if (c1.isValid() && type >= XGUI::HorizontalGradient && type <= XGUI::LastGradient) {
178       // Unset texture should be done here
179       // ...
180       // Get colors and set-up gradiented background
181       if (!c2.isValid())
182         c2 = c1;
183       Quantity_Color qCol1(c1.red() / 255., c1.green() / 255., c1.blue() / 255., Quantity_TOC_RGB);
184       Quantity_Color qCol2(c2.red() / 255., c2.green() / 255., c2.blue() / 255., Quantity_TOC_RGB);
185       activeView()->SetBgImageStyle(Aspect_FM_NONE); // cancel texture background
186       switch(type) {
187       case XGUI::HorizontalGradient:
188         activeView()->SetBgGradientColors(qCol1, qCol2, Aspect_GFM_HOR,
189         Standard_True);
190         break;
191       case XGUI::VerticalGradient:
192         activeView()->SetBgGradientColors(qCol1, qCol2, Aspect_GFM_VER,
193         Standard_True);
194         break;
195       case XGUI::Diagonal1Gradient:
196         activeView()->SetBgGradientColors(qCol1, qCol2, Aspect_GFM_DIAG1,
197         Standard_True);
198         break;
199       case XGUI::Diagonal2Gradient:
200         activeView()->SetBgGradientColors(qCol1, qCol2, Aspect_GFM_DIAG2,
201         Standard_True);
202         break;
203       case XGUI::Corner1Gradient:
204         activeView()->SetBgGradientColors(qCol1, qCol2, Aspect_GFM_CORNER1,
205         Standard_True);
206         break;
207       case XGUI::Corner2Gradient:
208         activeView()->SetBgGradientColors(qCol1, qCol2, Aspect_GFM_CORNER2,
209         Standard_True);
210         break;
211       case XGUI::Corner3Gradient:
212         activeView()->SetBgGradientColors(qCol1, qCol2, Aspect_GFM_CORNER3,
213         Standard_True);
214         break;
215       case XGUI::Corner4Gradient:
216         activeView()->SetBgGradientColors(qCol1, qCol2, Aspect_GFM_CORNER4,
217         Standard_True);
218         break;
219       default:
220         break;
221       }
222     }
223     break;
224   }
225   case XGUI::CustomGradientBackground:
226     // NOT IMPLEMENTED YET
227     break;
228   default:
229     break;
230   }
231   // VSR: In OCCT before v6.5.3 below code can't be used because of very ugly bug - it has been impossible to
232   // clear the background texture image as soon as it was once set to the viewer.
233   if (myBackground.isTextureShown()) {
234     QString fileName;
235     int textureMode = myBackground.texture(fileName);
236     QFileInfo fi(fileName);
237     if (!fileName.isEmpty() && fi.exists()) {
238       // set texture image: file name and fill mode
239       switch(textureMode) {
240       case XGUI::CenterTexture:
241         activeView()->SetBackgroundImage(fi.absoluteFilePath().toLatin1().constData(),
242                                          Aspect_FM_CENTERED);
243         break;
244       case XGUI::TileTexture:
245         activeView()->SetBackgroundImage(fi.absoluteFilePath().toLatin1().constData(),
246                                          Aspect_FM_TILED);
247         break;
248       case XGUI::StretchTexture:
249         activeView()->SetBackgroundImage(fi.absoluteFilePath().toLatin1().constData(),
250                                          Aspect_FM_STRETCH);
251         break;
252       default:
253         break;
254       }
255       activeView()->Update();
256     }
257   }
258 }
259
260 //***********************************************
261 void XGUI_ViewPort::attachWindow(const Handle(V3d_View)& theView,
262                                  const Handle(Aspect_Window)& theWnd)
263 {
264   if (!theView.IsNull()) {
265     theView->SetWindow(theWnd);
266     updateBackground();
267   }
268 }
269
270 //***********************************************
271 void XGUI_ViewPort::paintEvent(QPaintEvent* theEvent)
272 {
273 #ifndef WIN32
274   /* X11 : map before show doesn't work */
275   if ( !mapped( activeView() ) )
276   mapView( activeView() );
277 #endif
278   if (!myWindow.IsNull()) {
279     QApplication::syncX();
280     QRect rc = theEvent->rect();
281     //if ( !myPaintersRedrawing ) {
282     //activeView()->Redraw();
283     activeView()->Redraw(rc.x(), rc.y(), rc.width(), rc.height());
284     emit vpUpdated();
285     //}
286   }
287   //if ( myPaintersRedrawing ) {
288   //    QPainter p( this );
289   //    //emit vpDrawExternal( &p );
290   //    myPaintersRedrawing = false;
291   //}
292 }
293
294 //***********************************************
295 void XGUI_ViewPort::resizeEvent(QResizeEvent* theEvent)
296 {
297 #ifdef WIN32
298   /* Win32 : map before first show to avoid flicker */
299   if (!mapped(activeView()))
300     mapView(activeView());
301 #endif
302   QApplication::syncX();
303   if (!activeView().IsNull()) {
304     activeView()->MustBeResized();
305   }
306 }
307
308 //***********************************************
309 QImage XGUI_ViewPort::dumpView(QRect theRect, bool toUpdate)
310 {
311   Handle(V3d_View) view = getView();
312   if (view.IsNull())
313     return QImage();
314
315   int aWidth;
316   int aHeight;
317   if (theRect.isNull()) {
318     aWidth = width();
319     aHeight = height();
320   } else {
321     aWidth = theRect.width();
322     aHeight = theRect.height();
323   }
324   QApplication::syncX();
325
326   unsigned char* data = new unsigned char[aWidth * aHeight * 4];
327
328   QPoint p;
329   if (theRect.isNull()) {
330     if (toUpdate)
331       view->Redraw();
332     p = mapFromParent(geometry().topLeft());
333   } else {
334     if (toUpdate)
335       view->Redraw(theRect.x(), theRect.y(), theRect.width(), theRect.height());
336     p = theRect.topLeft();
337   }
338   glReadPixels(p.x(), p.y(), aWidth, aHeight, GL_RGBA, GL_UNSIGNED_BYTE, data);
339
340   QImage anImage(data, aWidth, aHeight, QImage::Format_ARGB32);
341   anImage = anImage.mirrored();
342   anImage = anImage.rgbSwapped();
343   return anImage;
344 }
345
346 /*!
347  Inits 'rotation' transformation.
348  */
349 void XGUI_ViewPort::startRotation(int x, int y, int theRotationPointType,
350                                   const gp_Pnt& theSelectedPoint)
351 {
352   if (!activeView().IsNull()) {
353     switch(theRotationPointType) {
354     case XGUI::GRAVITY:
355       activeView()->StartRotation(x, y, 0.45);
356       break;
357     case XGUI::SELECTED:
358       sx = x;
359       sy = y;
360
361       double X, Y;
362       activeView()->Size(X, Y);
363       rx = Standard_Real(activeView()->Convert(X));
364       ry = Standard_Real(activeView()->Convert(Y));
365
366       activeView()->Rotate(0., 0., 0., theSelectedPoint.X(), theSelectedPoint.Y(),
367                            theSelectedPoint.Z(),
368                            Standard_True);
369
370       Quantity_Ratio zRotationThreshold;
371       zRotation = Standard_False;
372       zRotationThreshold = 0.45;
373       if (zRotationThreshold > 0.) {
374         Standard_Real dx = Abs(sx - rx / 2.);
375         Standard_Real dy = Abs(sy - ry / 2.);
376         Standard_Real dd = zRotationThreshold * (rx + ry) / 2.;
377         if (dx > dd || dy > dd)
378           zRotation = Standard_True;
379       }
380       break;
381     default:
382       break;
383     }
384     activeView()->DepthFitAll();
385   }
386 }
387
388 /*!
389  Rotates the viewport. 
390  */
391 void XGUI_ViewPort::rotate(int x, int y, int theRotationPointType, const gp_Pnt& theSelectedPoint)
392 {
393   if (!activeView().IsNull()) {
394     switch(theRotationPointType) {
395     case XGUI::GRAVITY:
396       activeView()->Rotation(x, y);
397       break;
398     case XGUI::SELECTED:
399       double dx, dy, dz;
400       if (zRotation) {
401         dz = atan2(Standard_Real(x) - rx / 2., ry / 2. - Standard_Real(y))
402             - atan2(sx - rx / 2., ry / 2. - sy);
403         dx = dy = 0.;
404       } else {
405         dx = (Standard_Real(x) - sx) * M_PI / rx;
406         dy = (sy - Standard_Real(y)) * M_PI / ry;
407         dz = 0.;
408       }
409
410       activeView()->Rotate(dx, dy, dz, theSelectedPoint.X(), theSelectedPoint.Y(),
411                            theSelectedPoint.Z(),
412                            Standard_False);
413       break;
414     default:
415       break;
416     }
417     emit vpTransformed();
418   }
419   //  setZSize( getZSize() );
420 }
421
422 /*!
423  Resets the viewport after 'rotation'. 
424  */
425 void XGUI_ViewPort::endRotation()
426 {
427   if (!activeView().IsNull()) {
428     activeView()->ZFitAll(1.);
429     activeView()->SetZSize(0.);
430     activeView()->Update();
431     emit vpTransformed();
432   }
433 }
434
435 /*!
436  Inits 'zoom' transformation.
437  */
438 void XGUI_ViewPort::startZoomAtPoint(int x, int y)
439 {
440   if (!activeView().IsNull()/* && isAdvancedZoomingEnabled() */)
441     activeView()->StartZoomAtPoint(x, y);
442 }
443
444 /*!
445  Centers the viewport. 
446  */
447 void XGUI_ViewPort::setCenter(int x, int y)
448 {
449   if (!activeView().IsNull()) {
450     activeView()->Place(x, y, myScale);
451     emit vpTransformed();
452   }
453 }
454
455 /*!
456  Called at 'pan' transformation. 
457  */
458 void XGUI_ViewPort::pan(int dx, int dy)
459 {
460   if (!activeView().IsNull()) {
461     activeView()->Pan(dx, dy, 1.0);
462     emit vpTransformed();
463   }
464 }
465
466 /*!
467  Called at 'window fit' transformation.
468  */
469 void XGUI_ViewPort::fitRect(const QRect& rect)
470 {
471   if (!activeView().IsNull()) {
472     activeView()->WindowFit(rect.left(), rect.top(), rect.right(), rect.bottom());
473     emit vpTransformed();
474   }
475 }
476
477 /*!
478  Called at 'zoom' transformation.
479  */
480 void XGUI_ViewPort::zoom(int x0, int y0, int x, int y)
481 {
482   if (!activeView().IsNull()) {
483     if (isAdvancedZoomingEnabled())
484       activeView()->ZoomAtPoint(x0, y0, x, y);
485     else
486       activeView()->Zoom(x0 + y0, 0, x + y, 0);
487     emit vpTransformed();
488   }
489 }
490
491 /*!
492  Sets the background data
493  */
494 void XGUI_ViewPort::setBackground(const XGUI_ViewBackground& bgData)
495 {
496   if (bgData.isValid()) {
497     myBackground = bgData;
498     updateBackground();
499     emit vpChangeBackground(myBackground);
500   }
501 }
502
503 void XGUI_ViewPort::fitAll(bool theKeepScale, bool theWithZ, bool theUpd)
504 {
505   if ( activeView().IsNull() )
506     return;
507
508   if ( theKeepScale )
509     myScale = activeView()->Scale();
510
511   Standard_Real aMargin = 0.01;
512   activeView()->FitAll( aMargin, theWithZ, theUpd );
513   activeView()->SetZSize(0.);
514   emit vpTransformed( );
515 }
516
517 void XGUI_ViewPort::syncronizeWith( const XGUI_ViewPort* ref )
518 {
519   Handle(V3d_View) refView = ref->getView();
520   Handle(V3d_View) tgtView = getView();
521
522   /*  The following params are copied:
523       - view type( ortho/persp )
524       - position of view point
525       - orientation of high point
526       - position of the eye
527       - projection vector
528       - view center ( 2D )
529       - view twist
530       - view scale
531   */
532
533   /* we'll update after setting all params */
534   tgtView->SetImmediateUpdate( Standard_False );
535
536   /* perspective */
537   if ( refView->Type() == V3d_PERSPECTIVE )
538     tgtView->SetFocale( refView->Focale() );
539
540   /* copy params */
541   Standard_Real x, y, z;
542   refView->At( x, y, z ); tgtView->SetAt( x, y, z );
543   refView->Up( x, y, z ); tgtView->SetUp( x, y, z );
544   refView->Eye( x, y, z ); tgtView->SetEye( x, y, z );
545   refView->Proj( x, y, z ); tgtView->SetProj( x, y, z );
546   refView->Center( x, y ); tgtView->SetCenter( x, y );
547   tgtView->SetScale( refView->Scale() );
548   tgtView->SetTwist( refView->Twist() );
549
550   /* update */
551   tgtView->Update();
552   tgtView->SetImmediateUpdate( Standard_True );
553 }