1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 /***************************************************************************
20 ** Class: OCCViewer_ViewPort3D
21 ** Descr: Visualisation canvas with CasCade 3D view
23 ** Created: UI team, 05.09.00
24 ****************************************************************************/
26 #include "OCCViewer_ViewPort3d.h"
28 #include "OCCViewer_VService.h"
32 #include <qapplication.h>
34 #include <Visual3d_View.hxx>
35 #include <V3d_PerspectiveView.hxx>
36 #include <V3d_OrthographicView.hxx>
39 #include <WNT_Window.hxx>
41 #include <Xw_Window.hxx>
47 OCCViewer_ViewPort3d::OCCViewer_ViewPort3d( QWidget* parent, const Handle( V3d_Viewer)& viewer, V3d_TypeOfView type )
48 : OCCViewer_ViewPort( parent ),
50 myDegenerated( true ),
55 if ( type == V3d_ORTHOGRAPHIC ) {
56 myOrthoView = new V3d_OrthographicView( viewer );
57 myActiveView = myOrthoView;
60 myPerspView = new V3d_PerspectiveView( viewer );
61 myActiveView = myPerspView;
64 activeView()->SetDegenerateModeOn();
70 OCCViewer_ViewPort3d::~OCCViewer_ViewPort3d()
72 Handle(V3d_View) aView = activeView();
78 Activates the desired 'type' of view in the viewer
79 ( view of 'type' is created if it doesn't exist ). [ public ]
81 /*void OCCViewer_ViewPort3d::setActive( V3d_TypeOfView type )
83 if ( activeView().IsNull() )
86 if ( activeView()->Type() != type )
88 if ( type == V3d_ORTHOGRAPHIC )
89 setView( myOrthoView );
90 if ( type == V3d_PERSPECTIVE )
91 setView( myPerspView );
96 Maps CasCade 'view' to this viewport. [ private ]
98 bool OCCViewer_ViewPort3d::mapView( const Handle(V3d_View)& view )
100 if ( !setWindow( view ) )
103 if ( !mapped( view ) )
105 view->SetWindow( myWindow );
106 if ( view != activeView() )
107 view->View()->Deactivate();
113 Sets new CASCADE view on viewport. Returns the previous active view. [ public ]
115 Handle( V3d_View ) OCCViewer_ViewPort3d::setView( const Handle( V3d_View )& view )
117 /* map the new view */
118 if ( view == activeView() || !mapView( view ) )
121 /* activate the new view*/
122 Handle( V3d_View ) oldView = activeView();
123 if ( !oldView.IsNull() )
125 oldView->View()->Deactivate();
126 view->SetBackgroundColor( oldView->BackgroundColor() );
129 view->SetDegenerateModeOn();
131 view->SetDegenerateModeOff();
133 view->View()->Activate();
139 Returns CasCade 3D view. [ public ]
141 Handle(V3d_View) OCCViewer_ViewPort3d::getView() const
147 Returns CasCade 3D viewer [ public ]
149 Handle(V3d_Viewer) OCCViewer_ViewPort3d::getViewer() const
151 Handle(V3d_Viewer) viewer;
152 if ( !activeView().IsNull() )
153 viewer = activeView()->Viewer();
158 Syncronizes visual state of this viewport with 'ref'
159 ( scale, projection, eye etc ) Returns 'true' if copied OK,
160 'false' otherwise. [ virtual public ]
162 bool OCCViewer_ViewPort3d::syncronize( const OCCViewer_ViewPort3d* ref )
164 OCCViewer_ViewPort3d* ref3d = (OCCViewer_ViewPort3d*)ref;
165 Handle(V3d_View) refView = ref3d->getView();
166 Handle(V3d_View) tgtView = getView();
168 /* Syncronize view types */
169 /* if ( tgtView->Type() != refView->Type() )
171 setActive( refView->Type() );
175 /* The following params are copied:
176 - view type( ortho/persp )
177 - position of view point
178 - orientation of high point
179 - position of the eye
186 /* we'll update after setting all params */
187 tgtView->SetImmediateUpdate( Standard_False );
190 if ( refView->Type() == V3d_PERSPECTIVE )
191 tgtView->SetFocale( refView->Focale() );
194 Standard_Real x, y, z;
195 refView->At( x, y, z ); tgtView->SetAt( x, y, z );
196 refView->Up( x, y, z ); tgtView->SetUp( x, y, z );
197 refView->Eye( x, y, z ); tgtView->SetEye( x, y, z );
198 refView->Proj( x, y, z ); tgtView->SetProj( x, y, z );
199 refView->Center( x, y ); tgtView->SetCenter( x, y );
200 tgtView->SetScale( refView->Scale() );
201 tgtView->SetTwist( refView->Twist() );
205 tgtView->SetImmediateUpdate( Standard_True );
210 Returns Z-size of this view. [ public ]
212 double OCCViewer_ViewPort3d::getZSize() const
214 if ( !activeView().IsNull() )
215 return activeView()->ZSize();
220 Sets Z-size of this view ( for both orthographic and perspective ). [ public ]
222 void OCCViewer_ViewPort3d::setZSize( double zsize )
224 myActiveView->SetZSize( zsize );
225 /* if ( !myOrthoView.IsNull() )
226 myOrthoView->SetZSize( zsize );
227 if ( !myPerspView.IsNull() )
228 myPerspView->SetZSize( zsize );*/
232 Returns the background color [ virtual public ]
234 QColor OCCViewer_ViewPort3d::backgroundColor() const
236 if ( !activeView().IsNull() )
238 Standard_Real aRed, aGreen, aBlue;
239 activeView()->BackgroundColor( Quantity_TOC_RGB, aRed, aGreen, aBlue );
240 int red = (int) (aRed * 255);
241 int green = (int) (aGreen * 255);
242 int blue = (int) (aBlue * 255);
243 return QColor( red, green, blue );
245 return OCCViewer_ViewPort::backgroundColor();
249 Sets the background color [ virtual public ]
251 void OCCViewer_ViewPort3d::setBackgroundColor( const QColor& color )
253 if ( !activeView().IsNull() )
255 activeView()->SetBackgroundColor( Quantity_TOC_RGB, color.red()/255.,
256 color.green()/255., color.blue()/255.);
257 activeView()->Update();
258 emit vpChangeBGColor( color );
262 void OCCViewer_ViewPort3d::setAnimationMode(bool theDegenerated)
264 if ( !activeView().IsNull() )
266 myAnimate = theDegenerated;
267 activeView()->SetAnimationMode(true, theDegenerated);
272 Updates the active viewport. [ virtual public ]
274 void OCCViewer_ViewPort3d::onUpdate()
276 if ( !activeView().IsNull() )
277 activeView()->Update();
281 Called at 'window fit' transformation. [ virtual protected ]
283 void OCCViewer_ViewPort3d::fitRect( const QRect& rect )
285 if ( !activeView().IsNull() )
286 activeView()->WindowFit( rect.left(), rect.top(), rect.right(), rect.bottom() );
290 Called at 'zoom' transformation. [ virtual protected ]
292 void OCCViewer_ViewPort3d::zoom( int x0, int y0, int x, int y )
294 if ( !activeView().IsNull() )
295 activeView()->Zoom( x0, y0, x, y );
299 Centers the viewport. [ virtual protected ]
301 void OCCViewer_ViewPort3d::setCenter( int x, int y )
303 if ( !activeView().IsNull() )
304 activeView()->Place( x, y, myScale );
308 Called at 'pan' transformation. [ virtual protected ]
310 void OCCViewer_ViewPort3d::pan( int dx, int dy )
312 if ( !activeView().IsNull() )
313 activeView()->Pan( dx, dy, 1.0 );
317 Inits 'rotation' transformation. [ protected ]
319 void OCCViewer_ViewPort3d::startRotation( int x, int y )
321 if ( !activeView().IsNull() )
323 myDegenerated = activeView()->DegenerateModeIsOn();
324 activeView()->SetDegenerateModeOn();
325 if (myAnimate) activeView()->SetAnimationModeOn();
326 activeView()->StartRotation( x, y, 0.45 );
331 Rotates the viewport. [ protected ]
333 void OCCViewer_ViewPort3d::rotate( int x, int y )
335 if ( !activeView().IsNull() )
336 activeView()->Rotation( x, y );
337 // setZSize( getZSize() );
341 Resets the viewport after 'rotation'. [ protected ]
343 void OCCViewer_ViewPort3d::endRotation()
345 if ( !activeView().IsNull() )
347 if (myAnimate) activeView()->SetAnimationModeOff();
348 if ( !myDegenerated )
349 activeView()->SetDegenerateModeOff();
350 activeView()->ZFitAll(1.);
355 Repaints the viewport. [ virtual protected ]
357 void OCCViewer_ViewPort3d::paintEvent( QPaintEvent* e )
360 /* X11 : map before show doesn't work */
361 if ( !mapped( activeView() ) )
362 mapView( activeView() );
364 if ( !myWindow.IsNull() )
366 QApplication::syncX();
367 QRect rc = e->rect();
368 if ( !myPaintersRedrawing )
369 activeView()->Redraw( rc.x(), rc.y(), rc.width(), rc.height() );
371 OCCViewer_ViewPort::paintEvent( e );
375 Resizes the viewport. [ virtual protected ]
377 void OCCViewer_ViewPort3d::resizeEvent( QResizeEvent* e )
380 /* Win32 : map before first show to avoid flicker */
381 if ( !mapped( activeView() ) )
382 mapView( activeView() );
384 QApplication::syncX();
385 if ( !activeView().IsNull() )
386 activeView()->MustBeResized();
390 Fits all objects in view. [ virtual protected ]
392 void OCCViewer_ViewPort3d::fitAll( bool keepScale, bool withZ, bool upd )
394 if ( activeView().IsNull() )
399 myScale = activeView()->Scale();
401 Standard_Real margin = 0.01;
402 activeView()->FitAll( margin, withZ, upd );
406 Resets the view. [ virtual protected ]
408 void OCCViewer_ViewPort3d::reset()
410 // double zsize = getZSize();
411 if ( !activeView().IsNull() )
412 activeView()->Reset();
413 // setZSize( zsize );
417 Passed the handle of native window of the component to CASCADE view. [ private ]
419 bool OCCViewer_ViewPort3d::setWindow( const Handle(V3d_View)& view )
421 if ( !myWindow.IsNull() )
427 int hwnd = (int)winId();
431 /* set this widget as the drawing window */
432 short lo = (short)hwnd;
433 short hi = (short)( hwnd >> 16 );
434 OCCViewer_VService::SetWindow( view, (int)hi, (int)lo, Xw_WQ_SAMEQUALITY );
435 myWindow = view->Window();
436 return !myWindow.IsNull();
440 Returns the current active view. [ private ]
442 Handle(V3d_View) OCCViewer_ViewPort3d::activeView() const
448 Returns the current inactive view [ private ]
450 /*Handle(V3d_View) OCCViewer_ViewPort3d::inactiveView() const
452 return ( activeView() == myOrthoView ? myPerspView : myOrthoView );
456 Returns 'true' if the given view is mapped to window. [ private ]
458 bool OCCViewer_ViewPort3d::mapped( const Handle(V3d_View)& view ) const
460 return ( !view.IsNull() && view->View()->IsDefined() );