1 /***************************************************************************
2 ** Class: OCCViewer_ViewPort3D
3 ** Descr: Visualisation canvas with CasCade 3D view
5 ** Created: UI team, 05.09.00
6 ****************************************************************************/
8 #include "OCCViewer_ViewPort3d.h"
10 #include "OCCViewer_VService.h"
14 #include <qapplication.h>
16 #include <Visual3d_View.hxx>
17 #include <V3d_PerspectiveView.hxx>
18 #include <V3d_OrthographicView.hxx>
21 #include <WNT_Window.hxx>
23 #include <Xw_Window.hxx>
29 OCCViewer_ViewPort3d::OCCViewer_ViewPort3d( QWidget* parent, const Handle( V3d_Viewer)& viewer, V3d_TypeOfView type )
30 : OCCViewer_ViewPort( parent ),
32 myDegenerated( true ),
37 if ( type == V3d_ORTHOGRAPHIC ) {
38 myOrthoView = new V3d_OrthographicView( viewer );
39 myActiveView = myOrthoView;
42 myPerspView = new V3d_PerspectiveView( viewer );
43 myActiveView = myPerspView;
46 activeView()->SetDegenerateModeOn();
52 OCCViewer_ViewPort3d::~OCCViewer_ViewPort3d()
54 Handle(V3d_View) aView = activeView();
60 Activates the desired 'type' of view in the viewer
61 ( view of 'type' is created if it doesn't exist ). [ public ]
63 /*void OCCViewer_ViewPort3d::setActive( V3d_TypeOfView type )
65 if ( activeView().IsNull() )
68 if ( activeView()->Type() != type )
70 if ( type == V3d_ORTHOGRAPHIC )
71 setView( myOrthoView );
72 if ( type == V3d_PERSPECTIVE )
73 setView( myPerspView );
78 Maps CasCade 'view' to this viewport. [ private ]
80 bool OCCViewer_ViewPort3d::mapView( const Handle(V3d_View)& view )
82 if ( !setWindow( view ) )
85 if ( !mapped( view ) )
87 view->SetWindow( myWindow );
88 if ( view != activeView() )
89 view->View()->Deactivate();
95 Sets new CASCADE view on viewport. Returns the previous active view. [ public ]
97 Handle( V3d_View ) OCCViewer_ViewPort3d::setView( const Handle( V3d_View )& view )
99 /* map the new view */
100 if ( view == activeView() || !mapView( view ) )
103 /* activate the new view*/
104 Handle( V3d_View ) oldView = activeView();
105 if ( !oldView.IsNull() )
107 oldView->View()->Deactivate();
108 view->SetBackgroundColor( oldView->BackgroundColor() );
111 view->SetDegenerateModeOn();
113 view->SetDegenerateModeOff();
115 view->View()->Activate();
121 Returns CasCade 3D view. [ public ]
123 Handle(V3d_View) OCCViewer_ViewPort3d::getView() const
129 Returns CasCade 3D viewer [ public ]
131 Handle(V3d_Viewer) OCCViewer_ViewPort3d::getViewer() const
133 Handle(V3d_Viewer) viewer;
134 if ( !activeView().IsNull() )
135 viewer = activeView()->Viewer();
140 Syncronizes visual state of this viewport with 'ref'
141 ( scale, projection, eye etc ) Returns 'true' if copied OK,
142 'false' otherwise. [ virtual public ]
144 bool OCCViewer_ViewPort3d::syncronize( const OCCViewer_ViewPort3d* ref )
146 OCCViewer_ViewPort3d* ref3d = (OCCViewer_ViewPort3d*)ref;
147 Handle(V3d_View) refView = ref3d->getView();
148 Handle(V3d_View) tgtView = getView();
150 /* Syncronize view types */
151 /* if ( tgtView->Type() != refView->Type() )
153 setActive( refView->Type() );
157 /* The following params are copied:
158 - view type( ortho/persp )
159 - position of view point
160 - orientation of high point
161 - position of the eye
168 /* we'll update after setting all params */
169 tgtView->SetImmediateUpdate( Standard_False );
172 if ( refView->Type() == V3d_PERSPECTIVE )
173 tgtView->SetFocale( refView->Focale() );
176 Standard_Real x, y, z;
177 refView->At( x, y, z ); tgtView->SetAt( x, y, z );
178 refView->Up( x, y, z ); tgtView->SetUp( x, y, z );
179 refView->Eye( x, y, z ); tgtView->SetEye( x, y, z );
180 refView->Proj( x, y, z ); tgtView->SetProj( x, y, z );
181 refView->Center( x, y ); tgtView->SetCenter( x, y );
182 tgtView->SetScale( refView->Scale() );
183 tgtView->SetTwist( refView->Twist() );
187 tgtView->SetImmediateUpdate( Standard_True );
192 Returns Z-size of this view. [ public ]
194 double OCCViewer_ViewPort3d::getZSize() const
196 if ( !activeView().IsNull() )
197 return activeView()->ZSize();
202 Sets Z-size of this view ( for both orthographic and perspective ). [ public ]
204 void OCCViewer_ViewPort3d::setZSize( double zsize )
206 myActiveView->SetZSize( zsize );
207 /* if ( !myOrthoView.IsNull() )
208 myOrthoView->SetZSize( zsize );
209 if ( !myPerspView.IsNull() )
210 myPerspView->SetZSize( zsize );*/
214 Returns the background color [ virtual public ]
216 QColor OCCViewer_ViewPort3d::backgroundColor() const
218 if ( !activeView().IsNull() )
220 Standard_Real aRed, aGreen, aBlue;
221 activeView()->BackgroundColor( Quantity_TOC_RGB, aRed, aGreen, aBlue );
222 int red = (int) (aRed * 255);
223 int green = (int) (aGreen * 255);
224 int blue = (int) (aBlue * 255);
225 return QColor( red, green, blue );
227 return OCCViewer_ViewPort::backgroundColor();
231 Sets the background color [ virtual public ]
233 void OCCViewer_ViewPort3d::setBackgroundColor( const QColor& color )
235 if ( !activeView().IsNull() )
237 activeView()->SetBackgroundColor( Quantity_TOC_RGB, color.red()/255.,
238 color.green()/255., color.blue()/255.);
239 activeView()->Update();
240 emit vpChangeBGColor( color );
244 void OCCViewer_ViewPort3d::setAnimationMode(bool theDegenerated)
246 if ( !activeView().IsNull() )
248 myAnimate = theDegenerated;
249 activeView()->SetAnimationMode(true, theDegenerated);
254 Updates the active viewport. [ virtual public ]
256 void OCCViewer_ViewPort3d::onUpdate()
258 if ( !activeView().IsNull() )
259 activeView()->Update();
263 Called at 'window fit' transformation. [ virtual protected ]
265 void OCCViewer_ViewPort3d::fitRect( const QRect& rect )
267 if ( !activeView().IsNull() )
268 activeView()->WindowFit( rect.left(), rect.top(), rect.right(), rect.bottom() );
272 Called at 'zoom' transformation. [ virtual protected ]
274 void OCCViewer_ViewPort3d::zoom( int x0, int y0, int x, int y )
276 if ( !activeView().IsNull() )
277 activeView()->Zoom( x0, y0, x, y );
281 Centers the viewport. [ virtual protected ]
283 void OCCViewer_ViewPort3d::setCenter( int x, int y )
285 if ( !activeView().IsNull() )
286 activeView()->Place( x, y, myScale );
290 Called at 'pan' transformation. [ virtual protected ]
292 void OCCViewer_ViewPort3d::pan( int dx, int dy )
294 if ( !activeView().IsNull() )
295 activeView()->Pan( dx, dy, 1.0 );
299 Inits 'rotation' transformation. [ protected ]
301 void OCCViewer_ViewPort3d::startRotation( int x, int y )
303 if ( !activeView().IsNull() )
305 myDegenerated = activeView()->DegenerateModeIsOn();
306 activeView()->SetDegenerateModeOn();
307 if (myAnimate) activeView()->SetAnimationModeOn();
308 activeView()->StartRotation( x, y, 0.45 );
313 Rotates the viewport. [ protected ]
315 void OCCViewer_ViewPort3d::rotate( int x, int y )
317 if ( !activeView().IsNull() )
318 activeView()->Rotation( x, y );
319 // setZSize( getZSize() );
323 Resets the viewport after 'rotation'. [ protected ]
325 void OCCViewer_ViewPort3d::endRotation()
327 if ( !activeView().IsNull() )
329 if (myAnimate) activeView()->SetAnimationModeOff();
330 if ( !myDegenerated )
331 activeView()->SetDegenerateModeOff();
332 activeView()->ZFitAll(1.);
337 Repaints the viewport. [ virtual protected ]
339 void OCCViewer_ViewPort3d::paintEvent( QPaintEvent* e )
342 /* X11 : map before show doesn't work */
343 if ( !mapped( activeView() ) )
344 mapView( activeView() );
346 if ( !myWindow.IsNull() )
348 QApplication::syncX();
349 QRect rc = e->rect();
350 if ( !myPaintersRedrawing )
351 activeView()->Redraw( rc.x(), rc.y(), rc.width(), rc.height() );
353 OCCViewer_ViewPort::paintEvent( e );
357 Resizes the viewport. [ virtual protected ]
359 void OCCViewer_ViewPort3d::resizeEvent( QResizeEvent* e )
362 /* Win32 : map before first show to avoid flicker */
363 if ( !mapped( activeView() ) )
364 mapView( activeView() );
366 QApplication::syncX();
367 if ( !activeView().IsNull() )
368 activeView()->MustBeResized();
372 Fits all objects in view. [ virtual protected ]
374 void OCCViewer_ViewPort3d::fitAll( bool keepScale, bool withZ )
376 if ( activeView().IsNull() )
381 myScale = activeView()->Scale();
383 Standard_Real margin = 0.01;
384 activeView()->FitAll( margin, withZ );
386 // double zsize = getZSize();
387 // setZSize( zsize );
391 Resets the view. [ virtual protected ]
393 void OCCViewer_ViewPort3d::reset()
395 // double zsize = getZSize();
396 if ( !activeView().IsNull() )
397 activeView()->Reset();
398 // setZSize( zsize );
402 Passed the handle of native window of the component to CASCADE view. [ private ]
404 bool OCCViewer_ViewPort3d::setWindow( const Handle(V3d_View)& view )
406 if ( !myWindow.IsNull() )
412 int hwnd = (int)winId();
416 /* set this widget as the drawing window */
417 short lo = (short)hwnd;
418 short hi = (short)( hwnd >> 16 );
419 OCCViewer_VService::SetWindow( view, (int)hi, (int)lo, Xw_WQ_SAMEQUALITY );
420 myWindow = view->Window();
421 return !myWindow.IsNull();
425 Returns the current active view. [ private ]
427 Handle(V3d_View) OCCViewer_ViewPort3d::activeView() const
433 Returns the current inactive view [ private ]
435 /*Handle(V3d_View) OCCViewer_ViewPort3d::inactiveView() const
437 return ( activeView() == myOrthoView ? myPerspView : myOrthoView );
441 Returns 'true' if the given view is mapped to window. [ private ]
443 bool OCCViewer_ViewPort3d::mapped( const Handle(V3d_View)& view ) const
445 return ( !view.IsNull() && view->View()->IsDefined() );