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
30 #include "OCCViewer_ViewPort3d.h"
33 #include "QAD_Settings.h"
34 #include "QAD_Config.h"
35 #include "QAD_Desktop.h"
36 #include "QAD_Study.h"
37 #include "SALOME_Selection.h"
40 #include <qcolordialog.h>
42 // Open CASCADE Includes
43 #include <V3d_PerspectiveView.hxx>
44 #include <V3d_OrthographicView.hxx>
53 OCCViewer_ViewPort3d::OCCViewer_ViewPort3d( QWidget* parent, const Handle( V3d_Viewer)& viewer,
54 V3d_TypeOfView viewType, OCCViewer_ViewPort* prevView ,
55 const QRect* magnify ) :
56 OCCViewer_ViewPort( parent )
58 if ( viewType == V3d_ORTHOGRAPHIC )
59 myActiveView = new V3d_OrthographicView( viewer );
61 myActiveView = new V3d_PerspectiveView( viewer );
66 myActiveView->SetDegenerateModeOn();
68 if ( prevView ) setOriginalView( prevView, *magnify );
74 OCCViewer_ViewPort3d::~OCCViewer_ViewPort3d()
76 myActiveView->Remove();
82 void OCCViewer_ViewPort3d::onCreatePopup()
85 QAD_Desktop* Desktop = (QAD_Desktop*) QAD_Application::getDesktop();
86 QAD_Study* myActiveStudy = Desktop->getActiveStudy();
87 SALOME_Selection* Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
90 QString theParent("Viewer");
93 Desktop->definePopup( theContext, theParent, theObject );
94 Desktop->createPopup( myPopup, theContext, theParent, theObject);
95 Desktop->customPopup( myPopup, theContext, theParent, theObject );
97 // if (Sel->IObjectCount() == 0 && myPopup->count()<1) {
98 if ( myPopup->count() > 0 )
99 myIDs.append ( myPopup->insertSeparator() );
101 myIDs.append ( id = myPopup->insertItem (tr ("MEN_VP3D_CHANGEBGR")) );
102 QAD_ASSERT ( myPopup->connectItem ( id, this, SLOT(onChangeBackgroundColor())) );
108 Sets new CASCADE view on viewport.
109 Returns the previous view or null if the view hasn't been set.
111 Handle (V3d_View) OCCViewer_ViewPort3d::setView( const Handle (V3d_View)& view )
113 Handle (V3d_View) oldView = myActiveView;
115 if ( hasWindow() ) setWindow();
120 Returns CasCade 3D view
122 Handle(V3d_View) OCCViewer_ViewPort3d::getView() const
128 Returns CasCade 3D viewer
130 Handle (V3d_Viewer) OCCViewer_ViewPort3d::getViewer() const
132 Handle (V3d_Viewer) viewer;
133 if ( !myActiveView.IsNull() )
134 viewer = myActiveView->Viewer();
139 Passed the handle of native window of the component to CASCADE view
140 (sets window handle for V3d_View class instance).
142 bool OCCViewer_ViewPort3d::setWindow()
144 if ( !myActiveView.IsNull() )
146 int windowHandle = (int) winId();
147 if ( windowHandle == 0 )
151 lo = (short) windowHandle;
152 hi = (short) (windowHandle >> 16);
154 OCCViewer_ViewPort* preView = getOriginalView();
157 /* Create new window ( 'magnify' operation )
159 Handle (V3d_View) view;
160 QRect rect = getMagnifyRect();
161 if ( preView->inherits("OCCViewer_ViewPort3d") )
163 view = ((OCCViewer_ViewPort3d*)preView)->getView();
165 if ( !view.IsNull() && !rect.isEmpty() )
167 OCCViewer_VService::SetMagnify(myActiveView, (int)hi, (int) lo, view,
169 rect.x() + rect.width(), rect.y() + rect.height(),
175 /* CasCade will use our widget as the drawing window */
176 OCCViewer_VService::SetWindow( myActiveView, (int) hi, (int) lo, Xw_WQ_SAMEQUALITY);
183 Forces the CASCADE view to resize window
185 void OCCViewer_ViewPort3d::windowResize()
187 QApplication::syncX();
188 if ( !myActiveView.IsNull() )
189 myActiveView->MustBeResized();
193 Called at 'window fit' transformation
195 void OCCViewer_ViewPort3d::fitWindow(const QRect& rect)
197 if ( !myActiveView.IsNull() )
199 myActiveView->WindowFit( rect.x(), rect.y(), rect.x() + rect.width(),
200 rect.y() + rect.height());
205 Called at 'zoom' transformation
207 void OCCViewer_ViewPort3d::zoom(int x0, int y0, int x, int y)
209 if ( !myActiveView.IsNull() )
210 myActiveView->Zoom(x0, y0, x, y);
216 void OCCViewer_ViewPort3d::setCenter(int x, int y)
218 if ( !myActiveView.IsNull() )
220 myActiveView->Place(x, y, myCurScale);
225 Called at 'pan' transformation
227 void OCCViewer_ViewPort3d::pan(int dx, int dy)
229 if ( !myActiveView.IsNull() )
231 myActiveView->Pan(dx, dy, 1.0);
236 Inits 'rotation' transformation
238 void OCCViewer_ViewPort3d::startRotation(int x0, int y0)
240 if ( !myActiveView.IsNull() )
242 myDegenerated = myActiveView->DegenerateModeIsOn();
243 myActiveView->SetDegenerateModeOn();
244 myActiveView->StartRotation(x0, y0);
251 void OCCViewer_ViewPort3d::rotate(int x, int y)
253 if ( !myActiveView.IsNull() )
255 myActiveView->Rotation(x, y);
260 Resets the viewport after 'rotation'
262 void OCCViewer_ViewPort3d::endRotation()
264 if ( !myActiveView.IsNull() )
266 if ( !myDegenerated )
267 myActiveView->SetDegenerateModeOff();
272 Called to update the background color
274 QColor OCCViewer_ViewPort3d::backgroundColor() const
276 if ( !myActiveView.IsNull() )
278 Standard_Real aRed, aGreen, aBlue;
279 myActiveView->BackgroundColor(Quantity_TOC_RGB, aRed, aGreen, aBlue);
280 int red = (int) (aRed * 255);
281 int green = (int) (aGreen * 255);
282 int blue = (int) (aBlue * 255);
283 return QColor(red, green, blue);
285 return OCCViewer_ViewPort::backgroundColor();
289 Sets the background color
291 void OCCViewer_ViewPort3d::setBackgroundColor( const QColor& color)
293 if ( !myActiveView.IsNull() )
295 myActiveView->SetBackgroundColor( Quantity_TOC_RGB, color.red()/255.,
296 color.green()/255., color.blue()/255.);
298 QAD_CONFIG->addSetting( "OCCViewer:BackgroundColorRed", color.red() );
299 QAD_CONFIG->addSetting( "OCCViewer:BackgroundColorGreen", color.green() );
300 QAD_CONFIG->addSetting( "OCCViewer:BackgroundColorBlue", color.blue() );
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();