2 // File : OCCViewer_ViewPort3d.cxx
3 // Created : Wed Mar 20 10:47:09 2002
4 // Author : Nicolas REJNERI
7 // Copyright : Open CASCADE 2002
10 #include "OCCViewer_ViewPort3d.h"
13 #include "QAD_Settings.h"
14 #include "QAD_Config.h"
15 #include "QAD_Desktop.h"
16 #include "QAD_Study.h"
17 #include "SALOME_Selection.h"
20 #include <qcolordialog.h>
22 // Open CASCADE Includes
23 #include <V3d_PerspectiveView.hxx>
24 #include <V3d_OrthographicView.hxx>
33 OCCViewer_ViewPort3d::OCCViewer_ViewPort3d( QWidget* parent, const Handle( V3d_Viewer)& viewer,
34 V3d_TypeOfView viewType, OCCViewer_ViewPort* prevView ,
35 const QRect* magnify ) :
36 OCCViewer_ViewPort( parent )
38 if ( viewType == V3d_ORTHOGRAPHIC )
39 myActiveView = new V3d_OrthographicView( viewer );
41 myActiveView = new V3d_PerspectiveView( viewer );
46 myActiveView->SetDegenerateModeOn();
48 if ( prevView ) setOriginalView( prevView, *magnify );
54 OCCViewer_ViewPort3d::~OCCViewer_ViewPort3d()
56 myActiveView->Remove();
62 void OCCViewer_ViewPort3d::onCreatePopup()
65 QAD_Desktop* Desktop = (QAD_Desktop*) QAD_Application::getDesktop();
66 QAD_Study* myActiveStudy = Desktop->getActiveStudy();
67 SALOME_Selection* Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
70 QString theParent("Viewer");
73 Desktop->definePopup( theContext, theParent, theObject );
74 Desktop->createPopup( myPopup, theContext, theParent, theObject);
75 Desktop->customPopup( myPopup, theContext, theParent, theObject );
77 // if (Sel->IObjectCount() == 0 && myPopup->count()<1) {
78 if ( myPopup->count() > 0 )
79 myIDs.append ( myPopup->insertSeparator() );
81 myIDs.append ( id = myPopup->insertItem (tr ("MEN_VP3D_CHANGEBGR")) );
82 QAD_ASSERT ( myPopup->connectItem ( id, this, SLOT(onChangeBackgroundColor())) );
88 Sets new CASCADE view on viewport.
89 Returns the previous view or null if the view hasn't been set.
91 Handle (V3d_View) OCCViewer_ViewPort3d::setView( const Handle (V3d_View)& view )
93 Handle (V3d_View) oldView = myActiveView;
95 if ( hasWindow() ) setWindow();
100 Returns CasCade 3D view
102 Handle(V3d_View) OCCViewer_ViewPort3d::getView() const
108 Returns CasCade 3D viewer
110 Handle (V3d_Viewer) OCCViewer_ViewPort3d::getViewer() const
112 Handle (V3d_Viewer) viewer;
113 if ( !myActiveView.IsNull() )
114 viewer = myActiveView->Viewer();
119 Passed the handle of native window of the component to CASCADE view
120 (sets window handle for V3d_View class instance).
122 bool OCCViewer_ViewPort3d::setWindow()
124 if ( !myActiveView.IsNull() )
126 int windowHandle = (int) winId();
127 if ( windowHandle == 0 )
131 lo = (short) windowHandle;
132 hi = (short) (windowHandle >> 16);
134 OCCViewer_ViewPort* preView = getOriginalView();
137 /* Create new window ( 'magnify' operation )
139 Handle (V3d_View) view;
140 QRect rect = getMagnifyRect();
141 if ( preView->inherits("OCCViewer_ViewPort3d") )
143 view = ((OCCViewer_ViewPort3d*)preView)->getView();
145 if ( !view.IsNull() && !rect.isEmpty() )
147 OCCViewer_VService::SetMagnify(myActiveView, (int)hi, (int) lo, view,
149 rect.x() + rect.width(), rect.y() + rect.height(),
155 /* CasCade will use our widget as the drawing window */
156 OCCViewer_VService::SetWindow( myActiveView, (int) hi, (int) lo, Xw_WQ_SAMEQUALITY);
163 Forces the CASCADE view to resize window
165 void OCCViewer_ViewPort3d::windowResize()
167 QApplication::syncX();
168 if ( !myActiveView.IsNull() )
169 myActiveView->MustBeResized();
173 Called at 'window fit' transformation
175 void OCCViewer_ViewPort3d::fitWindow(const QRect& rect)
177 if ( !myActiveView.IsNull() )
179 myActiveView->WindowFit( rect.x(), rect.y(), rect.x() + rect.width(),
180 rect.y() + rect.height());
185 Called at 'zoom' transformation
187 void OCCViewer_ViewPort3d::zoom(int x0, int y0, int x, int y)
189 if ( !myActiveView.IsNull() )
190 myActiveView->Zoom(x0, y0, x, y);
196 void OCCViewer_ViewPort3d::setCenter(int x, int y)
198 if ( !myActiveView.IsNull() )
200 myActiveView->Place(x, y, myCurScale);
205 Called at 'pan' transformation
207 void OCCViewer_ViewPort3d::pan(int dx, int dy)
209 if ( !myActiveView.IsNull() )
211 myActiveView->Pan(dx, dy, 1.0);
216 Inits 'rotation' transformation
218 void OCCViewer_ViewPort3d::startRotation(int x0, int y0)
220 if ( !myActiveView.IsNull() )
222 myDegenerated = myActiveView->DegenerateModeIsOn();
223 myActiveView->SetDegenerateModeOn();
224 myActiveView->StartRotation(x0, y0);
231 void OCCViewer_ViewPort3d::rotate(int x, int y)
233 if ( !myActiveView.IsNull() )
235 myActiveView->Rotation(x, y);
240 Resets the viewport after 'rotation'
242 void OCCViewer_ViewPort3d::endRotation()
244 if ( !myActiveView.IsNull() )
246 if ( !myDegenerated )
247 myActiveView->SetDegenerateModeOff();
252 Called to update the background color
254 QColor OCCViewer_ViewPort3d::backgroundColor() const
256 if ( !myActiveView.IsNull() )
258 Standard_Real aRed, aGreen, aBlue;
259 myActiveView->BackgroundColor(Quantity_TOC_RGB, aRed, aGreen, aBlue);
260 int red = (int) (aRed * 255);
261 int green = (int) (aGreen * 255);
262 int blue = (int) (aBlue * 255);
263 return QColor(red, green, blue);
265 return OCCViewer_ViewPort::backgroundColor();
269 Sets the background color
271 void OCCViewer_ViewPort3d::setBackgroundColor( const QColor& color)
273 if ( !myActiveView.IsNull() )
275 myActiveView->SetBackgroundColor( Quantity_TOC_RGB, color.red()/255.,
276 color.green()/255., color.blue()/255.);
278 QAD_CONFIG->addSetting( "OCCViewer:BackgroundColorRed", color.red() );
279 QAD_CONFIG->addSetting( "OCCViewer:BackgroundColorGreen", color.green() );
280 QAD_CONFIG->addSetting( "OCCViewer:BackgroundColorBlue", color.blue() );
282 myActiveView->Update();
287 Sets the background color with color selection dialog
289 void OCCViewer_ViewPort3d::onChangeBackgroundColor()
291 if ( !myActiveView.IsNull() )
293 QColor selColor = QColorDialog::getColor ( backgroundColor(), this );
294 if ( selColor.isValid() )
296 setBackgroundColor( selColor );
302 Repaints the viewport
304 void OCCViewer_ViewPort3d::paintEvent (QPaintEvent* e )
306 if ( myActiveView.IsNull() )
308 QPainter thePainter (this);
309 thePainter.drawText(50, 50, QObject::tr("ERR_VP_NOVIEW"), 7);
313 QApplication::syncX();
316 myHasWindow = setWindow();
317 if ( myHasWindow ) fitAll();
321 QRect visibleRect = e->rect();
322 if ( (!myDrawRect) && (!myPaintersRedrawing) )
324 myActiveView->Redraw ( visibleRect.x(), visibleRect.y(), visibleRect.width(),
325 visibleRect.height());
329 OCCViewer_ViewPort::paintEvent( e );
333 Called at 'global panning' transformation
335 void OCCViewer_ViewPort3d::activateGlobalPanning()
337 if ( !myActiveView.IsNull() )
339 myCurScale = myActiveView->Scale();
340 myActiveView->FitAll(0.01, false);
342 OCCViewer_ViewPort::activateGlobalPanning();
346 Fits all objects in view
348 void OCCViewer_ViewPort3d::fitAll( bool withZ )
350 if ( !myActiveView.IsNull() )
352 myActiveView->FitAll(0.01, true);
353 if ( withZ ) myActiveView->ZFitAll(0.0);
360 void OCCViewer_ViewPort3d::reset()
362 if ( !myActiveView.IsNull() )
364 myActiveView->Reset();