1 // SALOME OCCViewer : build OCC Viewer into Salome desktop
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : OCCViewer_ViewPort3d.cxx
25 // Author : Nicolas REJNERI
29 #include "OCCViewer_ViewPort3d.h"
32 #include "QAD_Settings.h"
33 #include "QAD_Config.h"
34 #include "QAD_Desktop.h"
35 #include "QAD_Study.h"
36 #include "SALOME_Selection.h"
39 #include <qcolordialog.h>
41 // Open CASCADE Includes
42 #include <V3d_PerspectiveView.hxx>
43 #include <V3d_OrthographicView.hxx>
54 OCCViewer_ViewPort3d::OCCViewer_ViewPort3d( QWidget* parent, const Handle( V3d_Viewer)& viewer,
55 V3d_TypeOfView viewType, OCCViewer_ViewPort* prevView ,
56 const QRect* magnify ) :
57 OCCViewer_ViewPort( parent )
59 if ( viewType == V3d_ORTHOGRAPHIC )
60 myActiveView = new V3d_OrthographicView( viewer );
62 myActiveView = new V3d_PerspectiveView( viewer );
67 myActiveView->SetDegenerateModeOn();
69 if ( prevView ) setOriginalView( prevView, *magnify );
75 OCCViewer_ViewPort3d::~OCCViewer_ViewPort3d()
77 myActiveView->Remove();
83 void OCCViewer_ViewPort3d::onCreatePopup()
86 QAD_Desktop* Desktop = (QAD_Desktop*) QAD_Application::getDesktop();
89 QString theParent("Viewer");
92 Desktop->definePopup( theContext, theParent, theObject );
93 Desktop->createPopup( myPopup, theContext, theParent, theObject);
94 Desktop->customPopup( myPopup, theContext, theParent, theObject );
96 // if (Sel->IObjectCount() == 0 && myPopup->count()<1) {
97 if ( myPopup->count() > 0 )
98 myIDs.append ( myPopup->insertSeparator() );
100 myIDs.append ( id = myPopup->insertItem (tr ("MEN_VP3D_CHANGEBGR")) );
101 QAD_ASSERT ( myPopup->connectItem ( id, this, SLOT(onChangeBackgroundColor())) );
107 Sets new CASCADE view on viewport.
108 Returns the previous view or null if the view hasn't been set.
110 Handle (V3d_View) OCCViewer_ViewPort3d::setView( const Handle (V3d_View)& view )
112 Handle (V3d_View) oldView = myActiveView;
114 if ( hasWindow() ) setWindow();
119 Returns CasCade 3D view
121 Handle(V3d_View) OCCViewer_ViewPort3d::getView() const
127 Returns CasCade 3D viewer
129 Handle (V3d_Viewer) OCCViewer_ViewPort3d::getViewer() const
131 Handle (V3d_Viewer) viewer;
132 if ( !myActiveView.IsNull() )
133 viewer = myActiveView->Viewer();
138 Passed the handle of native window of the component to CASCADE view
139 (sets window handle for V3d_View class instance).
141 bool OCCViewer_ViewPort3d::setWindow()
143 if ( !myActiveView.IsNull() )
145 int windowHandle = (int) winId();
146 if ( windowHandle == 0 )
150 lo = (short) windowHandle;
151 hi = (short) (windowHandle >> 16);
153 OCCViewer_ViewPort* preView = getOriginalView();
156 /* Create new window ( 'magnify' operation )
158 Handle (V3d_View) view;
159 QRect rect = getMagnifyRect();
160 if ( preView->inherits("OCCViewer_ViewPort3d") )
162 view = ((OCCViewer_ViewPort3d*)preView)->getView();
164 if ( !view.IsNull() && !rect.isEmpty() )
166 OCCViewer_VService::SetMagnify(myActiveView, (int)hi, (int) lo, view,
168 rect.x() + rect.width(), rect.y() + rect.height(),
174 /* CasCade will use our widget as the drawing window */
175 OCCViewer_VService::SetWindow( myActiveView, (int) hi, (int) lo, Xw_WQ_SAMEQUALITY);
182 Forces the CASCADE view to resize window
184 void OCCViewer_ViewPort3d::windowResize()
186 QApplication::syncX();
187 if ( !myActiveView.IsNull() )
188 myActiveView->MustBeResized();
192 Called at 'window fit' transformation
194 void OCCViewer_ViewPort3d::fitWindow(const QRect& rect)
196 if ( !myActiveView.IsNull() )
198 myActiveView->WindowFit( rect.x(), rect.y(), rect.x() + rect.width(),
199 rect.y() + rect.height());
204 Called at 'zoom' transformation
206 void OCCViewer_ViewPort3d::zoom(int x0, int y0, int x, int y)
208 if ( !myActiveView.IsNull() )
209 myActiveView->Zoom(x0, y0, x, y);
215 void OCCViewer_ViewPort3d::setCenter(int x, int y)
217 if ( !myActiveView.IsNull() )
219 myActiveView->Place(x, y, myCurScale);
224 Called at 'pan' transformation
226 void OCCViewer_ViewPort3d::pan(int dx, int dy)
228 if ( !myActiveView.IsNull() )
230 myActiveView->Pan(dx, dy, 1.0);
235 Inits 'rotation' transformation
237 void OCCViewer_ViewPort3d::startRotation(int x0, int y0)
239 if ( !myActiveView.IsNull() )
241 myDegenerated = myActiveView->DegenerateModeIsOn();
242 myActiveView->SetDegenerateModeOn();
243 myActiveView->StartRotation(x0, y0);
250 void OCCViewer_ViewPort3d::rotate(int x, int y)
252 if ( !myActiveView.IsNull() )
254 myActiveView->Rotation(x, y);
259 Resets the viewport after 'rotation'
261 void OCCViewer_ViewPort3d::endRotation()
263 if ( !myActiveView.IsNull() )
265 if ( !myDegenerated )
266 myActiveView->SetDegenerateModeOff();
271 Called to update the background color
273 QColor OCCViewer_ViewPort3d::backgroundColor() const
275 if ( !myActiveView.IsNull() )
277 Standard_Real aRed, aGreen, aBlue;
278 myActiveView->BackgroundColor(Quantity_TOC_RGB, aRed, aGreen, aBlue);
279 int red = (int) (aRed * 255);
280 int green = (int) (aGreen * 255);
281 int blue = (int) (aBlue * 255);
282 return QColor(red, green, blue);
284 return OCCViewer_ViewPort::backgroundColor();
288 Sets the background color
290 void OCCViewer_ViewPort3d::setBackgroundColor( const QColor& color)
292 if ( !myActiveView.IsNull() )
294 myActiveView->SetBackgroundColor( Quantity_TOC_RGB, color.red()/255.,
295 color.green()/255., color.blue()/255.);
297 /* VSR : PAL5420 ---------------------------------------------------
298 QAD_CONFIG->addSetting( "OCCViewer:BackgroundColorRed", color.red() );
299 QAD_CONFIG->addSetting( "OCCViewer:BackgroundColorGreen", color.green() );
300 QAD_CONFIG->addSetting( "OCCViewer:BackgroundColorBlue", color.blue() );
301 VSR : PAL5420 --------------------------------------------------- */
302 myActiveView->Update();
307 Sets the background color with color selection dialog
309 void OCCViewer_ViewPort3d::onChangeBackgroundColor()
311 if ( !myActiveView.IsNull() )
313 QColor selColor = QColorDialog::getColor ( backgroundColor(), this );
314 if ( selColor.isValid() )
316 setBackgroundColor( selColor );
322 Repaints the viewport
324 void OCCViewer_ViewPort3d::paintEvent (QPaintEvent* e )
326 if ( myActiveView.IsNull() )
328 QPainter thePainter (this);
329 thePainter.drawText(50, 50, QObject::tr("ERR_VP_NOVIEW"), 7);
333 QApplication::syncX();
336 myHasWindow = setWindow();
337 if ( myHasWindow ) fitAll();
341 QRect visibleRect = e->rect();
342 if ( (!myDrawRect) && (!myPaintersRedrawing) )
344 myActiveView->Redraw ( visibleRect.x(), visibleRect.y(), visibleRect.width(),
345 visibleRect.height());
349 OCCViewer_ViewPort::paintEvent( e );
353 Called at 'global panning' transformation
355 void OCCViewer_ViewPort3d::activateGlobalPanning()
357 if ( !myActiveView.IsNull() )
359 myCurScale = myActiveView->Scale();
360 myActiveView->FitAll(0.01, false);
362 OCCViewer_ViewPort::activateGlobalPanning();
366 Fits all objects in view
368 void OCCViewer_ViewPort3d::fitAll( bool withZ )
370 if ( !myActiveView.IsNull() )
372 myActiveView->FitAll(0.01, true);
373 if ( withZ ) myActiveView->ZFitAll(0.0);
380 void OCCViewer_ViewPort3d::reset()
382 if ( !myActiveView.IsNull() )
384 myActiveView->Reset();
391 void OCCViewer_ViewPort3d::incrementalPan( const int incrX, const int incrY )
393 this->pan( incrX, incrY );
399 void OCCViewer_ViewPort3d::incrementalZoom( const int incr )
401 int cx = width() / 2;
402 int cy = height() / 2;
403 this->zoom( cx, cy, cx + incr, cy + incr );
409 void OCCViewer_ViewPort3d::incrementalRotate( const int incrX, const int incrY )
411 int cx = width() / 2;
412 int cy = height() / 2;
413 this->startRotation( cx, cy );
414 this->rotate( cx + incrX, cy + incrY );